Skip to content

The complete open source platform to run your entire marketplace. Storefront, vendor panel, admin console, and integrations in one enterprise-grade platform you own.

fix(core): validate cart shipping profiles against the offer (#1417)

Viktor Holikd2c8a5abbd06
diff --git a/integration-tests/http/offer/order/order.spec.ts b/integration-tests/http/offer/order/order.spec.ts
index 68b7a1bf7..7f91598fe 100644
--- a/integration-tests/http/offer/order/order.spec.ts
+++ b/integration-tests/http/offer/order/order.spec.ts
@@ -45,6 +45,7 @@ medusaIntegrationTestRunner({
stocked: number
offerPrice: number
required_quantity?: number
+ existingProduct?: any
}) => {
const result = await createSellerUser(appContainer, {
email: opts.email,
@@ -132,16 +133,20 @@ medusaIntegrationTestRunner({
headers
)
- const product = await createVendorProduct(api, headers, {
- title: `Prod${uniqueSuffix}`,
- sku: `V${uniqueSuffix}`,
- })
+ const product =
+ opts.existingProduct ??
+ (await createVendorProduct(api, headers, {
+ title: `Prod${uniqueSuffix}`,
+ sku: `V${uniqueSuffix}`,
+ }))
- await api.post(
- `/vendor/sales-channels/${salesChannel.id}/products`,
- { add: [product.id] },
- headers
- )
+ if (!opts.existingProduct) {
+ await api.post(
+ `/vendor/sales-channels/${salesChannel.id}/products`,
+ { add: [product.id] },
+ headers
+ )
+ }
const offer = (
await api.post(
@@ -758,6 +763,54 @@ medusaIntegrationTestRunner({
expect(after.reserved).toEqual(0)
})
})
+
+ it("should complete a cart for an offer whose seller is not the product's default seller", async () => {
+ const sellerA = await seedSellerOfferWithShipping({
+ email: "[email protected]",
+ name: "ProfileA",
+ stocked: 10,
+ offerPrice: 2500,
+ })
+ const sellerB = await seedSellerOfferWithShipping({
+ email: "[email protected]",
+ name: "ProfileB",
+ stocked: 10,
+ offerPrice: 3500,
+ existingProduct: sellerA.product,
+ })
+
+ expect(sellerB.offer.shipping_profile_id).not.toEqual(
+ sellerA.offer.shipping_profile_id
+ )
+
+ const { completeResp } = await completeCartCheckout(
+ sellerB.offer.id,
+ sellerB.variant.id,
+ 1
+ )
+
+ expect(completeResp.status).toEqual(200)
+ expect(completeResp.data.type).toEqual("order_group")
+
+ const query = appContainer.resolve(
+ ContainerRegistrationKeys.QUERY
+ )
+ const { data: orderGroup } = await query.graph({
+ entity: "order_group",
+ filters: { id: completeResp.data.order_group.id },
+ fields: [
+ "id",
+ "orders.id",
+ "orders.seller.id",
+ "orders.items.offer.id",
+ ],
+ })
+
+ const orders = (orderGroup[0] as any).orders
+ expect(orders).toHaveLength(1)
+ expect(orders[0].seller.id).toEqual(sellerB.sellerId)
+ expect(orders[0].items[0].offer.id).toEqual(sellerB.offer.id)
+ })
})
},
})
diff --git a/packages/core/src/workflows/cart/steps/validate-seller-cart-shipping.ts b/packages/core/src/workflows/cart/steps/validate-seller-cart-shipping.ts
index 6364b1974..c2c5a525f 100644
--- a/packages/core/src/workflows/cart/steps/validate-seller-cart-shipping.ts
+++ b/packages/core/src/workflows/cart/steps/validate-seller-cart-shipping.ts
@@ -10,12 +10,16 @@ import { SellerDTO } from "@mercurjs/types"
type ValidateSellerCartShippingStepInput = {
cart: Omit<CartWorkflowDTO, "items"> & {
items: (CartLineItemDTO & {
- offer?: { id: string; seller_id?: string } | null
+ offer?: {
+ id: string
+ seller_id?: string
+ shipping_profile_id?: string | null
+ } | null
})[]
}
- shippingOptions: ShippingOptionDTO & {
+ shippingOptions: (ShippingOptionDTO & {
seller: SellerDTO
- }[]
+ })[]
}
export const validateSellerCartShippingStep = createStep(
@@ -43,6 +47,40 @@ export const validateSellerCartShippingStep = createStep(
)
}
+ const itemsRequiringShipping = (cart.items ?? []).filter(
+ (item) => item.requires_shipping
+ )
+ const shippingMethods = cart.shipping_methods ?? []
+
+ if (itemsRequiringShipping.length > 0 && shippingMethods.length === 0) {
+ throw new MedusaError(
+ MedusaError.Types.INVALID_DATA,
+ "No shipping method selected but the cart contains items that require shipping."
+ )
+ }
+
+ const optionProfileMap = new Map(
+ shippingOptions.map((so) => [so.id, so.shipping_profile_id])
+ )
+ const availableProfiles = new Set(
+ shippingMethods.map((method) =>
+ optionProfileMap.get(method.shipping_option_id as string)
+ )
+ )
+
+ // The offer, not the master product, owns the shipping profile a
+ // seller ships from.
+ const missingProfiles = itemsRequiringShipping
+ .map((item) => item.offer?.shipping_profile_id ?? undefined)
+ .filter((profileId) => !availableProfiles.has(profileId))
+
+ if (missingProfiles.length > 0) {
+ throw new MedusaError(
+ MedusaError.Types.INVALID_DATA,
+ "The cart items require shipping profiles that are not satisfied by the current shipping methods"
+ )
+ }
+
return new StepResponse(void 0)
}
)
diff --git a/packages/core/src/workflows/cart/workflows/complete-cart-with-split-orders.ts b/packages/core/src/workflows/cart/workflows/complete-cart-with-split-orders.ts
index 71d107f06..ade60c716 100644
--- a/packages/core/src/workflows/cart/workflows/complete-cart-with-split-orders.ts
+++ b/packages/core/src/workflows/cart/workflows/complete-cart-with-split-orders.ts
@@ -33,7 +33,6 @@ import {
updateCartsStep,
useQueryGraphStep,
validateCartPaymentsStep,
- validateShippingStep,
} from "@medusajs/medusa/core-flows"
import { CreateOrderGroupDTO, MercurModules, SellerDTO } from "@mercurjs/types"
import { createOrderGroupStep } from "../../order-group"
@@ -134,11 +133,7 @@ export const completeCartWithSplitOrdersWorkflow = createWorkflow(
})
validateSellerCartShippingStep({
cart: cartData.data,
- shippingOptions: shippingOptionsData.data as ShippingOptionDTO & { seller: SellerDTO }[],
- })
- validateShippingStep({
- cart: cartData.data,
- shippingOptions: shippingOptionsData.data,
+ shippingOptions: shippingOptionsData.data as (ShippingOptionDTO & { seller: SellerDTO })[],
})
const { sales_channel_id } = transform(
{ cart: cartData.data },