fix(medusa): Remove shipping on updates to cart.items (#4715)
* rm shipping on line item updates * Add tests * remove verbose flag * Create real-items-rhyme.md
This commit is contained in:
@@ -73,9 +73,9 @@ export default async (req, res) => {
|
||||
if (validated.quantity === 0) {
|
||||
await cartService.withTransaction(m).removeLineItem(id, line_id)
|
||||
} else {
|
||||
const cart = await cartService
|
||||
.withTransaction(m)
|
||||
.retrieve(id, { relations: ["items", "items.variant"] })
|
||||
const cart = await cartService.withTransaction(m).retrieve(id, {
|
||||
relations: ["items", "items.variant", "shipping_methods"],
|
||||
})
|
||||
|
||||
const existing = cart.items.find((i) => i.id === line_id)
|
||||
if (!existing) {
|
||||
|
||||
@@ -3,40 +3,40 @@ import { isEmpty, isEqual } from "lodash"
|
||||
import { MedusaError, isDefined } from "medusa-core-utils"
|
||||
import { DeepPartial, EntityManager, In, IsNull, Not } from "typeorm"
|
||||
import {
|
||||
CustomShippingOptionService,
|
||||
CustomerService,
|
||||
DiscountService,
|
||||
EventBusService,
|
||||
GiftCardService,
|
||||
LineItemAdjustmentService,
|
||||
LineItemService,
|
||||
NewTotalsService,
|
||||
PaymentProviderService,
|
||||
ProductService,
|
||||
ProductVariantInventoryService,
|
||||
ProductVariantService,
|
||||
RegionService,
|
||||
SalesChannelService,
|
||||
ShippingOptionService,
|
||||
StoreService,
|
||||
TaxProviderService,
|
||||
TotalsService,
|
||||
CustomShippingOptionService,
|
||||
CustomerService,
|
||||
DiscountService,
|
||||
EventBusService,
|
||||
GiftCardService,
|
||||
LineItemAdjustmentService,
|
||||
LineItemService,
|
||||
NewTotalsService,
|
||||
PaymentProviderService,
|
||||
ProductService,
|
||||
ProductVariantInventoryService,
|
||||
ProductVariantService,
|
||||
RegionService,
|
||||
SalesChannelService,
|
||||
ShippingOptionService,
|
||||
StoreService,
|
||||
TaxProviderService,
|
||||
TotalsService,
|
||||
} from "."
|
||||
import { IPriceSelectionStrategy, TransactionBaseService } from "../interfaces"
|
||||
import SalesChannelFeatureFlag from "../loaders/feature-flags/sales-channels"
|
||||
import {
|
||||
Address,
|
||||
Cart,
|
||||
CustomShippingOption,
|
||||
Customer,
|
||||
Discount,
|
||||
DiscountRule,
|
||||
DiscountRuleType,
|
||||
LineItem,
|
||||
PaymentSession,
|
||||
PaymentSessionStatus,
|
||||
SalesChannel,
|
||||
ShippingMethod,
|
||||
Address,
|
||||
Cart,
|
||||
CustomShippingOption,
|
||||
Customer,
|
||||
Discount,
|
||||
DiscountRule,
|
||||
DiscountRuleType,
|
||||
LineItem,
|
||||
PaymentSession,
|
||||
PaymentSessionStatus,
|
||||
SalesChannel,
|
||||
ShippingMethod,
|
||||
} from "../models"
|
||||
import { AddressRepository } from "../repositories/address"
|
||||
import { CartRepository } from "../repositories/cart"
|
||||
@@ -44,18 +44,18 @@ import { LineItemRepository } from "../repositories/line-item"
|
||||
import { PaymentSessionRepository } from "../repositories/payment-session"
|
||||
import { ShippingMethodRepository } from "../repositories/shipping-method"
|
||||
import {
|
||||
CartCreateProps,
|
||||
CartUpdateProps,
|
||||
FilterableCartProps,
|
||||
LineItemUpdate,
|
||||
LineItemValidateData,
|
||||
isCart,
|
||||
CartCreateProps,
|
||||
CartUpdateProps,
|
||||
FilterableCartProps,
|
||||
LineItemUpdate,
|
||||
LineItemValidateData,
|
||||
isCart,
|
||||
} from "../types/cart"
|
||||
import {
|
||||
AddressPayload,
|
||||
FindConfig,
|
||||
TotalField,
|
||||
WithRequiredProperty,
|
||||
AddressPayload,
|
||||
FindConfig,
|
||||
TotalField,
|
||||
WithRequiredProperty,
|
||||
} from "../types/common"
|
||||
import { PaymentSessionInput } from "../types/payment"
|
||||
import { buildQuery, isString, setMetadata } from "../utils"
|
||||
@@ -480,7 +480,11 @@ class CartService extends TransactionBaseService {
|
||||
return await this.atomicPhase_(
|
||||
async (transactionManager: EntityManager) => {
|
||||
const cart = await this.retrieve(cartId, {
|
||||
relations: ["items.variant.product.profiles", "payment_sessions"],
|
||||
relations: [
|
||||
"items.variant.product.profiles",
|
||||
"payment_sessions",
|
||||
"shipping_methods",
|
||||
],
|
||||
})
|
||||
|
||||
const lineItem = cart.items.find((item) => item.id === lineItemId)
|
||||
@@ -488,7 +492,6 @@ class CartService extends TransactionBaseService {
|
||||
return cart
|
||||
}
|
||||
|
||||
// Remove shipping methods if they are not needed
|
||||
if (cart.shipping_methods?.length) {
|
||||
await this.shippingOptionService_
|
||||
.withTransaction(transactionManager)
|
||||
@@ -620,7 +623,10 @@ class CartService extends TransactionBaseService {
|
||||
|
||||
return await this.atomicPhase_(
|
||||
async (transactionManager: EntityManager) => {
|
||||
let cart = await this.retrieve(cartId, { select })
|
||||
let cart = await this.retrieve(cartId, {
|
||||
select,
|
||||
relations: ["shipping_methods"],
|
||||
})
|
||||
|
||||
if (this.featureFlagRouter_.isFeatureEnabled("sales_channels")) {
|
||||
if (config.validateSalesChannels) {
|
||||
@@ -710,6 +716,12 @@ class CartService extends TransactionBaseService {
|
||||
throw err
|
||||
})
|
||||
|
||||
if (cart.shipping_methods?.length) {
|
||||
await this.shippingOptionService_
|
||||
.withTransaction(transactionManager)
|
||||
.deleteShippingMethods(cart.shipping_methods)
|
||||
}
|
||||
|
||||
cart = await this.retrieve(cart.id, {
|
||||
relations: [
|
||||
"items.variant.product.profiles",
|
||||
@@ -920,6 +932,18 @@ class CartService extends TransactionBaseService {
|
||||
): Promise<Cart> {
|
||||
return await this.atomicPhase_(
|
||||
async (transactionManager: EntityManager) => {
|
||||
const select: (keyof Cart)[] = ["id"]
|
||||
if (
|
||||
this.featureFlagRouter_.isFeatureEnabled(SalesChannelFeatureFlag.key)
|
||||
) {
|
||||
select.push("sales_channel_id")
|
||||
}
|
||||
|
||||
const cart = await this.retrieve(cartId, {
|
||||
select: select,
|
||||
relations: ["shipping_methods"],
|
||||
})
|
||||
|
||||
const lineItem = await this.lineItemService_.retrieve(lineItemId, {
|
||||
select: ["id", "quantity", "variant_id", "cart_id"],
|
||||
})
|
||||
@@ -934,17 +958,6 @@ class CartService extends TransactionBaseService {
|
||||
|
||||
if (lineItemUpdate.quantity) {
|
||||
if (lineItem.variant_id) {
|
||||
const select: (keyof Cart)[] = ["id"]
|
||||
if (
|
||||
this.featureFlagRouter_.isFeatureEnabled(
|
||||
SalesChannelFeatureFlag.key
|
||||
)
|
||||
) {
|
||||
select.push("sales_channel_id")
|
||||
}
|
||||
|
||||
const cart = await this.retrieve(cartId, { select: select })
|
||||
|
||||
const hasInventory =
|
||||
await this.productVariantInventoryService_.confirmInventory(
|
||||
lineItem.variant_id,
|
||||
@@ -961,6 +974,12 @@ class CartService extends TransactionBaseService {
|
||||
}
|
||||
}
|
||||
|
||||
if (cart.shipping_methods?.length) {
|
||||
await this.shippingOptionService_
|
||||
.withTransaction(transactionManager)
|
||||
.deleteShippingMethods(cart.shipping_methods)
|
||||
}
|
||||
|
||||
await this.lineItemService_
|
||||
.withTransaction(transactionManager)
|
||||
.update(lineItemId, lineItemUpdate)
|
||||
|
||||
@@ -485,10 +485,6 @@ class LineItemService extends TransactionBaseService {
|
||||
async deleteWithTaxLines(id: string): Promise<LineItem | undefined | null> {
|
||||
return await this.atomicPhase_(
|
||||
async (transactionManager: EntityManager) => {
|
||||
const lineItemRepository = transactionManager.withRepository(
|
||||
this.lineItemRepository_
|
||||
)
|
||||
|
||||
await this.taxProviderService_
|
||||
.withTransaction(transactionManager)
|
||||
.clearLineItemsTaxLines([id])
|
||||
|
||||
Reference in New Issue
Block a user