fix(core-flows): cart validate sales channel (#11024)

This commit is contained in:
Frane Polić
2025-01-18 16:48:58 +01:00
committed by GitHub
parent 45847fef19
commit c1385c7002
4 changed files with 35 additions and 34 deletions

View File

@@ -313,32 +313,6 @@ medusaIntegrationTestRunner({
) )
}) })
it("should throw when required fields of a cart are not present", async () => {
cart = (
await api.post(
`/store/carts`,
{
region_id: region.id,
currency_code: "usd",
sales_channel_id: null,
email: "test@admin.com",
items: [],
},
storeHeaders
)
).data.cart
const { response } = await api
.get(`/store/shipping-options?cart_id=${cart.id}`, storeHeaders)
.catch((e) => e)
expect(response.data).toEqual({
type: "invalid_data",
message:
"Field(s) are required to have value to continue - sales_channel_id",
})
})
it("should throw error when cart_id is not passed as a parameter", async () => { it("should throw error when cart_id is not passed as a parameter", async () => {
const { response } = await api const { response } = await api
.get(`/store/shipping-options`, storeHeaders) .get(`/store/shipping-options`, storeHeaders)

View File

@@ -0,0 +1,20 @@
import { MedusaError } from "@medusajs/framework/utils"
import { createStep, StepResponse } from "@medusajs/framework/workflows-sdk"
import { SalesChannelDTO } from "@medusajs/types"
export const validateSalesChannelStep = createStep(
"validate-sales-channel",
async (data: { salesChannel: SalesChannelDTO }) => {
const { salesChannel } = data
if (!salesChannel?.id) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
"Sales channel is required when creating a cart. Either provide a sales channel ID or set the default sales channel for the store."
)
}
return new StepResponse(void 0)
}
)

View File

@@ -35,6 +35,7 @@ import { confirmVariantInventoryWorkflow } from "./confirm-variant-inventory"
import { refreshPaymentCollectionForCartWorkflow } from "./refresh-payment-collection" import { refreshPaymentCollectionForCartWorkflow } from "./refresh-payment-collection"
import { updateCartPromotionsWorkflow } from "./update-cart-promotions" import { updateCartPromotionsWorkflow } from "./update-cart-promotions"
import { updateTaxLinesWorkflow } from "./update-tax-lines" import { updateTaxLinesWorkflow } from "./update-tax-lines"
import { validateSalesChannelStep } from "../steps/validate-sales-channel"
/** /**
* The data to create the cart, along with custom data that's passed to the workflow's hooks. * The data to create the cart, along with custom data that's passed to the workflow's hooks.
@@ -96,6 +97,8 @@ export const createCartWorkflow = createWorkflow(
}) })
) )
validateSalesChannelStep({ salesChannel })
// TODO: This is on par with the context used in v1.*, but we can be more flexible. // TODO: This is on par with the context used in v1.*, but we can be more flexible.
const pricingContext = transform( const pricingContext = transform(
{ input, region, customerData }, { input, region, customerData },
@@ -155,9 +158,7 @@ export const createCartWorkflow = createWorkflow(
data_.email = data.input?.email ?? data.customerData.customer.email data_.email = data.input?.email ?? data.customerData.customer.email
} }
if (data.salesChannel?.id) { data_.sales_channel_id = data.salesChannel!.id
data_.sales_channel_id = data.salesChannel.id
}
// If there is only one country in the region, we prepare a shipping address with that country's code. // If there is only one country in the region, we prepare a shipping address with that country's code.
if ( if (

View File

@@ -28,6 +28,7 @@ import {
updateCartsStep, updateCartsStep,
} from "../steps" } from "../steps"
import { refreshCartItemsWorkflow } from "./refresh-cart-items" import { refreshCartItemsWorkflow } from "./refresh-cart-items"
import { validateSalesChannelStep } from "../steps/validate-sales-channel"
/** /**
* The data to update the cart, along with custom data that's passed to the workflow's hooks. * The data to update the cart, along with custom data that's passed to the workflow's hooks.
@@ -89,6 +90,7 @@ export const updateCartWorkflow = createWorkflow(
"id", "id",
"email", "email",
"customer_id", "customer_id",
"sales_channel_id",
"shipping_address.*", "shipping_address.*",
"region.*", "region.*",
"region.countries.*", "region.countries.*",
@@ -97,8 +99,10 @@ export const updateCartWorkflow = createWorkflow(
throw_if_key_not_found: true, throw_if_key_not_found: true,
}).config({ name: "get-cart" }) }).config({ name: "get-cart" })
const customerDataInput = transform({ input, cartToUpdate }, (data) => { const cartDataInput = transform({ input, cartToUpdate }, (data) => {
return { return {
sales_channel_id:
data.input.sales_channel_id ?? data.cartToUpdate.sales_channel_id,
customer_id: data.cartToUpdate.customer_id, customer_id: data.cartToUpdate.customer_id,
email: data.input.email ?? data.cartToUpdate.email, email: data.input.email ?? data.cartToUpdate.email,
} }
@@ -106,14 +110,16 @@ export const updateCartWorkflow = createWorkflow(
const [salesChannel, customer] = parallelize( const [salesChannel, customer] = parallelize(
findSalesChannelStep({ findSalesChannelStep({
salesChannelId: input.sales_channel_id, salesChannelId: cartDataInput.sales_channel_id,
}), }),
findOrCreateCustomerStep({ findOrCreateCustomerStep({
customerId: customerDataInput.customer_id, customerId: cartDataInput.customer_id,
email: customerDataInput.email, email: cartDataInput.email,
}) })
) )
validateSalesChannelStep({ salesChannel })
const newRegion = when({ input }, (data) => { const newRegion = when({ input }, (data) => {
return !!data.input.region_id return !!data.input.region_id
}).then(() => { }).then(() => {
@@ -200,7 +206,7 @@ export const updateCartWorkflow = createWorkflow(
} }
if (isDefined(updateCartData.sales_channel_id)) { if (isDefined(updateCartData.sales_channel_id)) {
data_.sales_channel_id = data.salesChannel?.id || null data_.sales_channel_id = data.salesChannel!.id
} }
return data_ return data_