feat: Assign store default sales channel in cart creation (#7495)

This commit is contained in:
Oli Juhl
2024-05-28 12:12:01 +02:00
committed by GitHub
parent ef6d748784
commit c9de1d28a4
2 changed files with 59 additions and 11 deletions

View File

@@ -16,6 +16,7 @@ import {
IPromotionModuleService,
IRegionModuleService,
ISalesChannelModuleService,
IStoreModuleService,
ITaxModuleService,
ProductStatus,
} from "@medusajs/types"
@@ -56,8 +57,8 @@ medusaIntegrationTestRunner({
let remoteLinkService
let regionService: IRegionModuleService
let paymentService: IPaymentModuleService
let storeService: IStoreModuleService
let defaultRegion
let region
let store
@@ -75,6 +76,7 @@ medusaIntegrationTestRunner({
taxModule = appContainer.resolve(ModuleRegistrationName.TAX)
regionService = appContainer.resolve(ModuleRegistrationName.REGION)
paymentService = appContainer.resolve(ModuleRegistrationName.PAYMENT)
storeService = appContainer.resolve(ModuleRegistrationName.STORE)
fulfillmentModule = appContainer.resolve(
ModuleRegistrationName.FULFILLMENT
)
@@ -86,8 +88,12 @@ medusaIntegrationTestRunner({
beforeEach(async () => {
await createAdminUser(dbConnection, adminHeaders, appContainer)
const { region } = await seedStorefrontDefaults(appContainer, "dkk")
defaultRegion = region
const { store: defaultStore } = await seedStorefrontDefaults(
appContainer,
"dkk"
)
store = defaultStore
})
describe("POST /store/carts", () => {
@@ -298,6 +304,31 @@ medusaIntegrationTestRunner({
)
})
it("should create cart with default store sales channel", async () => {
const sc = await scModule.create({
name: "Webshop",
})
await storeService.update(store.id, {
default_sales_channel_id: sc.id,
})
const response = await api.post(`/store/carts`, {
email: "tony@stark.com",
currency_code: "usd",
})
expect(response.status).toEqual(200)
expect(response.data.cart).toEqual(
expect.objectContaining({
id: response.data.cart.id,
currency_code: "usd",
email: "tony@stark.com",
sales_channel_id: sc.id,
})
)
})
it("should create cart with region currency code", async () => {
const region = await regionModule.create({
name: "US",

View File

@@ -1,6 +1,10 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { ISalesChannelModuleService, SalesChannelDTO } from "@medusajs/types"
import { MedusaError } from "@medusajs/utils"
import {
ISalesChannelModuleService,
IStoreModuleService,
SalesChannelDTO,
} from "@medusajs/types"
import { MedusaError, isDefined } from "@medusajs/utils"
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
interface StepInput {
@@ -14,16 +18,29 @@ export const findSalesChannelStep = createStep(
const salesChannelService = container.resolve<ISalesChannelModuleService>(
ModuleRegistrationName.SALES_CHANNEL
)
if (data.salesChannelId === null) {
return new StepResponse(null)
}
const storeModule = container.resolve<IStoreModuleService>(
ModuleRegistrationName.STORE
)
let salesChannel: SalesChannelDTO | undefined
if (data.salesChannelId) {
salesChannel = await salesChannelService.retrieve(data.salesChannelId)
} else {
// TODO: Find default sales channel from store
} else if (!isDefined(data.salesChannelId)) {
const [store] = await storeModule.list(
{},
{ select: ["default_sales_channel_id"] }
)
if (store?.default_sales_channel_id) {
salesChannel = await salesChannelService.retrieve(
store.default_sales_channel_id
)
}
}
if (!salesChannel) {
return new StepResponse(null)
}
if (salesChannel?.is_disabled) {