feat(core-flow, medusa): Create cart with Sales Channel (#6427)
This commit is contained in:
@@ -225,6 +225,21 @@ describe("Store Carts API", () => {
|
|||||||
).rejects.toThrow()
|
).rejects.toThrow()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it("should throw if sales channel is disabled", async () => {
|
||||||
|
const api = useApi() as any
|
||||||
|
|
||||||
|
const salesChannel = await scModuleService.create({
|
||||||
|
name: "Webshop",
|
||||||
|
is_disabled: true,
|
||||||
|
})
|
||||||
|
|
||||||
|
await expect(
|
||||||
|
api.post(`/store/carts`, {
|
||||||
|
sales_channel_id: salesChannel.id,
|
||||||
|
})
|
||||||
|
).rejects.toThrow()
|
||||||
|
})
|
||||||
|
|
||||||
describe("compensation", () => {
|
describe("compensation", () => {
|
||||||
it("should delete created customer if cart-creation fails", async () => {
|
it("should delete created customer if cart-creation fails", async () => {
|
||||||
expect.assertions(2)
|
expect.assertions(2)
|
||||||
|
|||||||
@@ -0,0 +1,34 @@
|
|||||||
|
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||||
|
import { ISalesChannelModuleService, SalesChannelDTO } from "@medusajs/types"
|
||||||
|
import { MedusaError } from "@medusajs/utils"
|
||||||
|
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||||
|
|
||||||
|
interface StepInput {
|
||||||
|
salesChannelId?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export const findSalesChannelStepId = "find-sales-channel"
|
||||||
|
export const findSalesChannelStep = createStep(
|
||||||
|
findSalesChannelStepId,
|
||||||
|
async (data: StepInput, { container }) => {
|
||||||
|
const salesChannelService = container.resolve<ISalesChannelModuleService>(
|
||||||
|
ModuleRegistrationName.SALES_CHANNEL
|
||||||
|
)
|
||||||
|
|
||||||
|
let salesChannel: SalesChannelDTO | undefined
|
||||||
|
if (data.salesChannelId) {
|
||||||
|
salesChannel = await salesChannelService.retrieve(data.salesChannelId)
|
||||||
|
} else {
|
||||||
|
// TODO: Find default sales channel from store
|
||||||
|
}
|
||||||
|
|
||||||
|
if (salesChannel?.is_disabled) {
|
||||||
|
throw new MedusaError(
|
||||||
|
MedusaError.Types.INVALID_DATA,
|
||||||
|
`Unable to assign cart to disabled Sales Channel: ${salesChannel.name}`
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
return new StepResponse(salesChannel)
|
||||||
|
}
|
||||||
|
)
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
export * from "./create-carts"
|
export * from "./create-carts"
|
||||||
export * from "./find-one-or-any-region"
|
export * from "./find-one-or-any-region"
|
||||||
export * from "./find-or-create-customer"
|
export * from "./find-or-create-customer"
|
||||||
|
export * from "./find-sales-channel"
|
||||||
export * from "./update-carts"
|
export * from "./update-carts"
|
||||||
|
|
||||||
|
|||||||
@@ -2,41 +2,54 @@ import { CartDTO, CreateCartWorkflowInputDTO } from "@medusajs/types"
|
|||||||
import {
|
import {
|
||||||
WorkflowData,
|
WorkflowData,
|
||||||
createWorkflow,
|
createWorkflow,
|
||||||
|
parallelize,
|
||||||
transform,
|
transform,
|
||||||
} from "@medusajs/workflows-sdk"
|
} from "@medusajs/workflows-sdk"
|
||||||
import {
|
import {
|
||||||
createCartsStep,
|
createCartsStep,
|
||||||
findOneOrAnyRegionStep,
|
findOneOrAnyRegionStep,
|
||||||
findOrCreateCustomerStep,
|
findOrCreateCustomerStep,
|
||||||
|
findSalesChannelStep,
|
||||||
} from "../steps"
|
} from "../steps"
|
||||||
|
|
||||||
export const createCartWorkflowId = "create-cart"
|
export const createCartWorkflowId = "create-cart"
|
||||||
export const createCartWorkflow = createWorkflow(
|
export const createCartWorkflow = createWorkflow(
|
||||||
createCartWorkflowId,
|
createCartWorkflowId,
|
||||||
(input: WorkflowData<CreateCartWorkflowInputDTO>): WorkflowData<CartDTO> => {
|
(input: WorkflowData<CreateCartWorkflowInputDTO>): WorkflowData<CartDTO> => {
|
||||||
const region = findOneOrAnyRegionStep({
|
const [salesChannel, region, customerData] = parallelize(
|
||||||
regionId: input.region_id,
|
findSalesChannelStep({
|
||||||
})
|
salesChannelId: input.sales_channel_id,
|
||||||
|
}),
|
||||||
|
findOneOrAnyRegionStep({
|
||||||
|
regionId: input.region_id,
|
||||||
|
}),
|
||||||
|
findOrCreateCustomerStep({
|
||||||
|
customerId: input.customer_id,
|
||||||
|
email: input.email,
|
||||||
|
})
|
||||||
|
)
|
||||||
|
|
||||||
const customerData = findOrCreateCustomerStep({
|
const cartInput = transform(
|
||||||
customerId: input.customer_id,
|
{ input, region, customerData, salesChannel },
|
||||||
email: input.email,
|
(data) => {
|
||||||
})
|
const data_ = {
|
||||||
|
...data.input,
|
||||||
|
currency_code: data.input.currency_code ?? data.region.currency_code,
|
||||||
|
region_id: data.region.id,
|
||||||
|
}
|
||||||
|
|
||||||
const cartInput = transform({ input, region, customerData }, (data) => {
|
if (data.customerData.customer?.id) {
|
||||||
const data_ = {
|
data_.customer_id = data.customerData.customer.id
|
||||||
...data.input,
|
data_.email = data.input?.email ?? data.customerData.customer.email
|
||||||
currency_code: data.input.currency_code ?? data.region.currency_code,
|
}
|
||||||
region_id: data.region.id,
|
|
||||||
|
if (data.salesChannel?.id) {
|
||||||
|
data_.sales_channel_id = data.salesChannel.id
|
||||||
|
}
|
||||||
|
|
||||||
|
return data_
|
||||||
}
|
}
|
||||||
|
)
|
||||||
if (data.customerData.customer?.id) {
|
|
||||||
data_.customer_id = data.customerData.customer.id
|
|
||||||
data_.email = data.input?.email ?? data.customerData.customer.email
|
|
||||||
}
|
|
||||||
|
|
||||||
return data_
|
|
||||||
})
|
|
||||||
|
|
||||||
// TODO: Add line items
|
// TODO: Add line items
|
||||||
|
|
||||||
|
|||||||
@@ -2,15 +2,15 @@ import { MiddlewaresConfig } from "../loaders/helpers/routing/types"
|
|||||||
import { adminCampaignRoutesMiddlewares } from "./admin/campaigns/middlewares"
|
import { adminCampaignRoutesMiddlewares } from "./admin/campaigns/middlewares"
|
||||||
import { adminCustomerGroupRoutesMiddlewares } from "./admin/customer-groups/middlewares"
|
import { adminCustomerGroupRoutesMiddlewares } from "./admin/customer-groups/middlewares"
|
||||||
import { adminCustomerRoutesMiddlewares } from "./admin/customers/middlewares"
|
import { adminCustomerRoutesMiddlewares } from "./admin/customers/middlewares"
|
||||||
|
import { adminInviteRoutesMiddlewares } from "./admin/invites/middlewares"
|
||||||
import { adminPromotionRoutesMiddlewares } from "./admin/promotions/middlewares"
|
import { adminPromotionRoutesMiddlewares } from "./admin/promotions/middlewares"
|
||||||
import { adminRegionRoutesMiddlewares } from "./admin/regions/middlewares"
|
import { adminRegionRoutesMiddlewares } from "./admin/regions/middlewares"
|
||||||
|
import { adminUserRoutesMiddlewares } from "./admin/users/middlewares"
|
||||||
import { adminWorkflowsExecutionsMiddlewares } from "./admin/workflows-executions/middlewares"
|
import { adminWorkflowsExecutionsMiddlewares } from "./admin/workflows-executions/middlewares"
|
||||||
import { authRoutesMiddlewares } from "./auth/middlewares"
|
import { authRoutesMiddlewares } from "./auth/middlewares"
|
||||||
import { storeCartRoutesMiddlewares } from "./store/carts/middlewares"
|
import { storeCartRoutesMiddlewares } from "./store/carts/middlewares"
|
||||||
import { storeCustomerRoutesMiddlewares } from "./store/customers/middlewares"
|
import { storeCustomerRoutesMiddlewares } from "./store/customers/middlewares"
|
||||||
import { adminUserRoutesMiddlewares } from "./admin/users/middlewares"
|
|
||||||
import { storeRegionRoutesMiddlewares } from "./store/regions/middlewares"
|
import { storeRegionRoutesMiddlewares } from "./store/regions/middlewares"
|
||||||
import { adminInviteRoutesMiddlewares } from "./admin/invites/middlewares"
|
|
||||||
|
|
||||||
export const config: MiddlewaresConfig = {
|
export const config: MiddlewaresConfig = {
|
||||||
routes: [
|
routes: [
|
||||||
|
|||||||
Reference in New Issue
Block a user