fix: validate regions exist for shipping option price update (#9364)
**What** - Adds a step to `updateShippingOptionsWorkflow` and `createShippingOptionsWorkflow` that validates if the region prices being updated have corresponding regions configured. **Why** - Previously, if you tried to send a region price update for a region that had been deleted the backend would throw an error when attempting to insert the region price. The error comes from a not-null constraint in the db, but it is better to validate that the regions we are trying to create prices for exist. Fixes CC-542
This commit is contained in:
@@ -204,6 +204,61 @@ medusaIntegrationTestRunner({
|
||||
)
|
||||
})
|
||||
|
||||
it("should fail with invalid region price", async () => {
|
||||
const regionService = container.resolve(
|
||||
Modules.REGION
|
||||
) as IRegionModuleService
|
||||
|
||||
const [region] = await regionService.createRegions([
|
||||
{
|
||||
name: "Test region",
|
||||
currency_code: "eur",
|
||||
countries: ["fr"],
|
||||
},
|
||||
])
|
||||
await regionService.softDeleteRegions([region.id])
|
||||
|
||||
const shippingOptionData: FulfillmentWorkflow.CreateShippingOptionsWorkflowInput =
|
||||
{
|
||||
name: "Test shipping option",
|
||||
price_type: "flat",
|
||||
service_zone_id: serviceZone.id,
|
||||
shipping_profile_id: shippingProfile.id,
|
||||
provider_id,
|
||||
type: {
|
||||
code: "manual-type",
|
||||
label: "Manual Type",
|
||||
description: "Manual Type Description",
|
||||
},
|
||||
prices: [
|
||||
{
|
||||
currency_code: "usd",
|
||||
amount: 10,
|
||||
},
|
||||
{
|
||||
region_id: region.id,
|
||||
amount: 100,
|
||||
},
|
||||
],
|
||||
rules: [
|
||||
{
|
||||
attribute: "total",
|
||||
operator: RuleOperator.EQ,
|
||||
value: "100",
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
const { errors } = await createShippingOptionsWorkflow(container).run({
|
||||
input: [shippingOptionData],
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
expect(errors[0].error.message).toEqual(
|
||||
`Cannot create prices for non-existent regions. Region with ids [${region.id}] were not found.`
|
||||
)
|
||||
})
|
||||
|
||||
it("should revert the shipping options and prices", async () => {
|
||||
const regionService = container.resolve(
|
||||
Modules.REGION
|
||||
|
||||
@@ -257,6 +257,133 @@ medusaIntegrationTestRunner({
|
||||
)
|
||||
})
|
||||
|
||||
it("should fail on non-existent region update shipping options", async () => {
|
||||
const regionService = container.resolve(
|
||||
Modules.REGION
|
||||
) as IRegionModuleService
|
||||
|
||||
const [region] = await regionService.createRegions([
|
||||
{
|
||||
name: "Test region",
|
||||
currency_code: "eur",
|
||||
countries: ["fr"],
|
||||
},
|
||||
])
|
||||
|
||||
const shippingOptionData: FulfillmentWorkflow.CreateShippingOptionsWorkflowInput =
|
||||
{
|
||||
name: "Test shipping option",
|
||||
price_type: "flat",
|
||||
service_zone_id: serviceZone.id,
|
||||
shipping_profile_id: shippingProfile.id,
|
||||
provider_id,
|
||||
type: {
|
||||
code: "manual-type",
|
||||
label: "Manual Type",
|
||||
description: "Manual Type Description",
|
||||
},
|
||||
prices: [
|
||||
{
|
||||
currency_code: "usd",
|
||||
amount: 10,
|
||||
},
|
||||
{
|
||||
region_id: region.id,
|
||||
amount: 100,
|
||||
},
|
||||
{
|
||||
currency_code: "dkk",
|
||||
amount: 1000,
|
||||
},
|
||||
],
|
||||
rules: [
|
||||
{
|
||||
attribute: "total",
|
||||
operator: RuleOperator.EQ,
|
||||
value: "100",
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
const { result } = await createShippingOptionsWorkflow(container).run({
|
||||
input: [shippingOptionData],
|
||||
})
|
||||
|
||||
const remoteQuery = container.resolve(
|
||||
ContainerRegistrationKeys.REMOTE_QUERY
|
||||
)
|
||||
|
||||
let remoteQueryObject = remoteQueryObjectFromString({
|
||||
entryPoint: "shipping_option",
|
||||
variables: {
|
||||
id: result[0].id,
|
||||
},
|
||||
fields: [
|
||||
"id",
|
||||
"name",
|
||||
"price_type",
|
||||
"service_zone_id",
|
||||
"shipping_profile_id",
|
||||
"provider_id",
|
||||
"data",
|
||||
"metadata",
|
||||
"type.*",
|
||||
"created_at",
|
||||
"updated_at",
|
||||
"deleted_at",
|
||||
"shipping_option_type_id",
|
||||
"prices.*",
|
||||
],
|
||||
})
|
||||
|
||||
const [createdShippingOption] = await remoteQuery(remoteQueryObject)
|
||||
|
||||
const usdPrice = createdShippingOption.prices.find((price) => {
|
||||
return price.currency_code === "usd"
|
||||
})
|
||||
|
||||
const dkkPrice = createdShippingOption.prices.find((price) => {
|
||||
return price.currency_code === "dkk"
|
||||
})
|
||||
|
||||
const updateData: UpdateShippingOptionsWorkflowInput = {
|
||||
id: createdShippingOption.id,
|
||||
name: "Test shipping option",
|
||||
price_type: "flat",
|
||||
type: {
|
||||
code: "manual-type",
|
||||
label: "Manual Type",
|
||||
description: "Manual Type Description",
|
||||
},
|
||||
prices: [
|
||||
// We keep the usd price as is
|
||||
// update the dkk price to 100
|
||||
// delete the third price eur
|
||||
// create a new eur one instead
|
||||
usdPrice,
|
||||
{
|
||||
...dkkPrice,
|
||||
amount: 100,
|
||||
},
|
||||
{
|
||||
region_id: region.id,
|
||||
amount: 1000,
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
await regionService.softDeleteRegions([region.id])
|
||||
|
||||
const { errors } = await updateShippingOptionsWorkflow(container).run({
|
||||
input: [updateData],
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
expect(errors[0].error.message).toEqual(
|
||||
`Cannot create prices for non-existent regions. Region with ids [${region.id}] were not found.`
|
||||
)
|
||||
})
|
||||
|
||||
it("should revert the shipping options", async () => {
|
||||
const regionService = container.resolve(
|
||||
Modules.REGION
|
||||
|
||||
Reference in New Issue
Block a user