chore: pick the default store region (#7369)
* chore: pick the default store region * chore: fix test * chore: remove from defaults workflow and move to seeds * chore: undo payment provider change * chore: rearrange conditionals
This commit is contained in:
40
integration-tests/helpers/seed-storefront-defaults.ts
Normal file
40
integration-tests/helpers/seed-storefront-defaults.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import { createDefaultsWorkflow } from "@medusajs/core-flows"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import {
|
||||
IRegionModuleService,
|
||||
IStoreModuleService,
|
||||
MedusaContainer,
|
||||
} from "@medusajs/types"
|
||||
|
||||
export const seedStorefrontDefaults = async (
|
||||
container: MedusaContainer,
|
||||
defaultCurrency: string = "usd"
|
||||
) => {
|
||||
const regionModule: IRegionModuleService = container.resolve(
|
||||
ModuleRegistrationName.REGION
|
||||
)
|
||||
const storeModule: IStoreModuleService = container.resolve(
|
||||
ModuleRegistrationName.STORE
|
||||
)
|
||||
|
||||
// Creates the stores & default sales channel
|
||||
await createDefaultsWorkflow(container).run()
|
||||
|
||||
const region = await regionModule.create({
|
||||
name: "Default Region",
|
||||
currency_code: defaultCurrency,
|
||||
})
|
||||
|
||||
let [store] = await storeModule.list({})
|
||||
|
||||
store = await storeModule.update(store.id, {
|
||||
default_region_id: region.id,
|
||||
supported_currency_codes: [region.currency_code],
|
||||
default_currency_code: region.currency_code,
|
||||
})
|
||||
|
||||
return {
|
||||
region,
|
||||
store,
|
||||
}
|
||||
}
|
||||
@@ -32,6 +32,7 @@ import {
|
||||
adminHeaders,
|
||||
createAdminUser,
|
||||
} from "../../../../helpers/create-admin-user"
|
||||
import { seedStorefrontDefaults } from "../../../../helpers/seed-storefront-defaults"
|
||||
|
||||
jest.setTimeout(200000)
|
||||
|
||||
@@ -89,11 +90,9 @@ medusaIntegrationTestRunner({
|
||||
beforeEach(async () => {
|
||||
await createAdminUser(dbConnection, adminHeaders, appContainer)
|
||||
|
||||
// Here, so we don't have to create a region for each test
|
||||
defaultRegion = await regionModuleService.create({
|
||||
name: "Default Region",
|
||||
currency_code: "dkk",
|
||||
})
|
||||
const { region } = await seedStorefrontDefaults(appContainer, "dkk")
|
||||
|
||||
defaultRegion = region
|
||||
})
|
||||
|
||||
describe("CreateCartWorkflow", () => {
|
||||
|
||||
@@ -27,6 +27,7 @@ import {
|
||||
} from "@medusajs/utils"
|
||||
import { medusaIntegrationTestRunner } from "medusa-test-utils"
|
||||
import { createAdminUser } from "../../../../helpers/create-admin-user"
|
||||
import { seedStorefrontDefaults } from "../../../../helpers/seed-storefront-defaults"
|
||||
import { createAuthenticatedCustomer } from "../../../helpers/create-authenticated-customer"
|
||||
import { setupTaxStructure } from "../../fixtures"
|
||||
|
||||
@@ -57,6 +58,7 @@ medusaIntegrationTestRunner({
|
||||
|
||||
let defaultRegion
|
||||
let region
|
||||
let store
|
||||
|
||||
beforeAll(async () => {
|
||||
appContainer = getContainer()
|
||||
@@ -83,11 +85,8 @@ medusaIntegrationTestRunner({
|
||||
beforeEach(async () => {
|
||||
await createAdminUser(dbConnection, adminHeaders, appContainer)
|
||||
|
||||
// Here, so we don't have to create a region for each test
|
||||
defaultRegion = await regionModule.create({
|
||||
name: "Default Region",
|
||||
currency_code: "dkk",
|
||||
})
|
||||
const { region } = await seedStorefrontDefaults(appContainer, "dkk")
|
||||
defaultRegion = region
|
||||
})
|
||||
|
||||
describe("POST /store/carts", () => {
|
||||
|
||||
@@ -86,6 +86,7 @@ module.exports = {
|
||||
[Modules.PRODUCT]: true,
|
||||
[Modules.PRICING]: true,
|
||||
[Modules.PROMOTION]: true,
|
||||
[Modules.REGION]: true,
|
||||
[Modules.CUSTOMER]: true,
|
||||
[Modules.SALES_CHANNEL]: true,
|
||||
[Modules.CART]: true,
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { IRegionModuleService } from "@medusajs/types"
|
||||
import { IRegionModuleService, IStoreModuleService } from "@medusajs/types"
|
||||
import { MedusaError } from "@medusajs/utils"
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
|
||||
@@ -11,21 +11,29 @@ export const findOneOrAnyRegionStep = createStep(
|
||||
ModuleRegistrationName.REGION
|
||||
)
|
||||
|
||||
if (!data.regionId) {
|
||||
// TODO: Pick up the default store region if none is provided
|
||||
const regions = await service.list({})
|
||||
const storeModule = container.resolve<IStoreModuleService>(
|
||||
ModuleRegistrationName.STORE
|
||||
)
|
||||
|
||||
if (!regions?.length) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.INVALID_DATA,
|
||||
"No regions found"
|
||||
)
|
||||
}
|
||||
if (data.regionId) {
|
||||
const region = await service.retrieve(data.regionId)
|
||||
|
||||
return new StepResponse(regions[0])
|
||||
return new StepResponse(region)
|
||||
}
|
||||
|
||||
const region = await service.retrieve(data.regionId)
|
||||
const [store] = await storeModule.list()
|
||||
|
||||
if (!store) {
|
||||
throw new MedusaError(MedusaError.Types.NOT_FOUND, "Store not found")
|
||||
}
|
||||
|
||||
const [region] = await service.list({
|
||||
id: store.default_region_id,
|
||||
})
|
||||
|
||||
if (!region) {
|
||||
throw new MedusaError(MedusaError.Types.INVALID_DATA, "No regions found")
|
||||
}
|
||||
|
||||
return new StepResponse(region)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user