fix(fulfillment): Update shipping options rules (#7204)
**What** Fix the update shipping options to apply the rules update properly and remove unused code
This commit is contained in:
1 parent
6487931f54
commit
25b0ccc60a
4 files changed
+99
-31
No files matched your search
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"@medusajs/fulfillment": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
fix(fulfillment): Update shipping options rules
|
||||||
+42
-3
@@ -226,7 +226,18 @@ medusaIntegrationTestRunner({
|
|||||||
amount: 1000,
|
amount: 1000,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
rules: [shippingOptionRule],
|
rules: [
|
||||||
|
{
|
||||||
|
operator: RuleOperator.EQ,
|
||||||
|
attribute: "old_attr",
|
||||||
|
value: "old value",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
operator: RuleOperator.EQ,
|
||||||
|
attribute: "old_attr_2",
|
||||||
|
value: "true",
|
||||||
|
},
|
||||||
|
],
|
||||||
}
|
}
|
||||||
|
|
||||||
const response = await api.post(
|
const response = await api.post(
|
||||||
@@ -240,6 +251,14 @@ medusaIntegrationTestRunner({
|
|||||||
const eurPrice = response.data.shipping_option.prices.find(
|
const eurPrice = response.data.shipping_option.prices.find(
|
||||||
(p) => p.currency_code === "eur"
|
(p) => p.currency_code === "eur"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const oldAttrRule = response.data.shipping_option.rules.find(
|
||||||
|
(r) => r.attribute === "old_attr"
|
||||||
|
)
|
||||||
|
const oldAttr2Rule = response.data.shipping_option.rules.find(
|
||||||
|
(r) => r.attribute === "old_attr_2"
|
||||||
|
)
|
||||||
|
|
||||||
const updateShippingOptionPayload = {
|
const updateShippingOptionPayload = {
|
||||||
name: "Updated shipping option",
|
name: "Updated shipping option",
|
||||||
provider_id: "manual_test-provider",
|
provider_id: "manual_test-provider",
|
||||||
@@ -255,8 +274,22 @@ medusaIntegrationTestRunner({
|
|||||||
},
|
},
|
||||||
],
|
],
|
||||||
rules: [
|
rules: [
|
||||||
shippingOptionRule,
|
|
||||||
{
|
{
|
||||||
|
// Un touched
|
||||||
|
id: oldAttrRule.id,
|
||||||
|
operator: RuleOperator.EQ,
|
||||||
|
attribute: "old_attr",
|
||||||
|
value: "old value",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
// Updated
|
||||||
|
id: oldAttr2Rule.id,
|
||||||
|
operator: RuleOperator.EQ,
|
||||||
|
attribute: "old_attr_2",
|
||||||
|
value: "false",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
// Created
|
||||||
operator: RuleOperator.EQ,
|
operator: RuleOperator.EQ,
|
||||||
attribute: "new_attr",
|
attribute: "new_attr",
|
||||||
value: "true",
|
value: "true",
|
||||||
@@ -272,7 +305,7 @@ medusaIntegrationTestRunner({
|
|||||||
|
|
||||||
expect(updateResponse.status).toEqual(200)
|
expect(updateResponse.status).toEqual(200)
|
||||||
expect(updateResponse.data.shipping_option.prices).toHaveLength(2)
|
expect(updateResponse.data.shipping_option.prices).toHaveLength(2)
|
||||||
expect(updateResponse.data.shipping_option.rules).toHaveLength(2)
|
expect(updateResponse.data.shipping_option.rules).toHaveLength(3)
|
||||||
expect(updateResponse.data.shipping_option).toEqual(
|
expect(updateResponse.data.shipping_option).toEqual(
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
id: expect.any(String),
|
id: expect.any(String),
|
||||||
@@ -310,6 +343,12 @@ medusaIntegrationTestRunner({
|
|||||||
attribute: "old_attr",
|
attribute: "old_attr",
|
||||||
value: "old value",
|
value: "old value",
|
||||||
}),
|
}),
|
||||||
|
expect.objectContaining({
|
||||||
|
id: expect.any(String),
|
||||||
|
operator: "eq",
|
||||||
|
attribute: "old_attr_2",
|
||||||
|
value: "false",
|
||||||
|
}),
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
id: expect.any(String),
|
id: expect.any(String),
|
||||||
operator: "eq",
|
operator: "eq",
|
||||||
|
|||||||
+23
-8
@@ -593,6 +593,8 @@ moduleIntegrationTestRunner({
|
|||||||
shippingOptionData
|
shippingOptionData
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const existingRule = shippingOption.rules[0]!
|
||||||
|
|
||||||
const updateData: UpdateShippingOptionDTO = {
|
const updateData: UpdateShippingOptionDTO = {
|
||||||
id: shippingOption.id,
|
id: shippingOption.id,
|
||||||
name: "updated-test",
|
name: "updated-test",
|
||||||
@@ -609,6 +611,10 @@ moduleIntegrationTestRunner({
|
|||||||
amount: 2000,
|
amount: 2000,
|
||||||
},
|
},
|
||||||
rules: [
|
rules: [
|
||||||
|
{
|
||||||
|
...existingRule,
|
||||||
|
value: "false",
|
||||||
|
},
|
||||||
{
|
{
|
||||||
attribute: "new-test",
|
attribute: "new-test",
|
||||||
operator: "eq",
|
operator: "eq",
|
||||||
@@ -639,22 +645,31 @@ moduleIntegrationTestRunner({
|
|||||||
}),
|
}),
|
||||||
data: updateData.data,
|
data: updateData.data,
|
||||||
rules: expect.arrayContaining([
|
rules: expect.arrayContaining([
|
||||||
|
expect.objectContaining({
|
||||||
|
id: existingRule.id,
|
||||||
|
value: '"false"',
|
||||||
|
}),
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
id: expect.any(String),
|
id: expect.any(String),
|
||||||
attribute: updateData.rules[0].attribute,
|
attribute: updateData.rules[1].attribute,
|
||||||
operator: updateData.rules[0].operator,
|
operator: updateData.rules[1].operator,
|
||||||
value: updateData.rules[0].value,
|
value: updateData.rules[1].value,
|
||||||
}),
|
}),
|
||||||
]),
|
]),
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
|
||||||
const rules = await service.listShippingOptionRules()
|
const rules = await service.listShippingOptionRules()
|
||||||
expect(rules).toHaveLength(1)
|
expect(rules).toHaveLength(2)
|
||||||
expect(rules[0]).toEqual(
|
expect(rules).toEqual(
|
||||||
expect.objectContaining({
|
expect.arrayContaining([
|
||||||
id: updatedShippingOption.rules[0].id,
|
expect.objectContaining({
|
||||||
})
|
id: updatedShippingOption.rules[0].id,
|
||||||
|
}),
|
||||||
|
expect.objectContaining({
|
||||||
|
id: updatedShippingOption.rules[1].id,
|
||||||
|
}),
|
||||||
|
])
|
||||||
)
|
)
|
||||||
|
|
||||||
const types = await service.listShippingOptionTypes()
|
const types = await service.listShippingOptionTypes()
|
||||||
|
|||||||
@@ -38,9 +38,9 @@ import {
|
|||||||
ShippingOptionType,
|
ShippingOptionType,
|
||||||
ShippingProfile,
|
ShippingProfile,
|
||||||
} from "@models"
|
} from "@models"
|
||||||
import {isContextValid, validateAndNormalizeRules} from "@utils"
|
import { isContextValid, validateAndNormalizeRules } from "@utils"
|
||||||
import {entityNameToLinkableKeysMap, joinerConfig} from "../joiner-config"
|
import { entityNameToLinkableKeysMap, joinerConfig } from "../joiner-config"
|
||||||
import {UpdateShippingOptionsInput} from "../types/service"
|
import { UpdateShippingOptionsInput } from "../types/service"
|
||||||
import FulfillmentProviderService from "./fulfillment-provider"
|
import FulfillmentProviderService from "./fulfillment-provider"
|
||||||
|
|
||||||
const generateMethodForModels = [
|
const generateMethodForModels = [
|
||||||
@@ -1148,14 +1148,33 @@ export default class FulfillmentModuleService<
|
|||||||
shippingOption
|
shippingOption
|
||||||
)
|
)
|
||||||
|
|
||||||
const existingRulesMap = new Map(
|
const existingRulesMap: Map<
|
||||||
existingRules.map((rule) => [rule.id, rule])
|
string,
|
||||||
)
|
FulfillmentTypes.UpdateShippingOptionRuleDTO | ShippingOptionRule
|
||||||
|
> = new Map(existingRules.map((rule) => [rule.id, rule]))
|
||||||
|
|
||||||
const updatedRules = shippingOption.rules
|
const updatedRules = shippingOption.rules
|
||||||
const updatedRuleIds = updatedRules
|
.map((rule) => {
|
||||||
.map((r) => "id" in r && r.id)
|
if ("id" in rule) {
|
||||||
.filter((id): id is string => !!id)
|
const existingRule = (existingRulesMap.get(rule.id) ??
|
||||||
|
{}) as FulfillmentTypes.UpdateShippingOptionRuleDTO
|
||||||
|
|
||||||
|
const ruleData: FulfillmentTypes.UpdateShippingOptionRuleDTO = {
|
||||||
|
...existingRule,
|
||||||
|
...rule,
|
||||||
|
}
|
||||||
|
|
||||||
|
existingRulesMap.set(rule.id, ruleData)
|
||||||
|
return ruleData
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
})
|
||||||
|
.filter(Boolean) as FulfillmentTypes.UpdateShippingOptionRuleDTO[]
|
||||||
|
|
||||||
|
validateAndNormalizeRules(updatedRules)
|
||||||
|
|
||||||
|
const updatedRuleIds = updatedRules.map((r) => "id" in r && r.id)
|
||||||
|
|
||||||
const toDeleteRuleIds = arrayDifference(
|
const toDeleteRuleIds = arrayDifference(
|
||||||
updatedRuleIds,
|
updatedRuleIds,
|
||||||
@@ -1166,19 +1185,9 @@ export default class FulfillmentModuleService<
|
|||||||
ruleIdsToDelete.push(...toDeleteRuleIds)
|
ruleIdsToDelete.push(...toDeleteRuleIds)
|
||||||
}
|
}
|
||||||
|
|
||||||
const newRules = updatedRules
|
|
||||||
.map((rule) => {
|
|
||||||
if (!("id" in rule)) {
|
|
||||||
return rule
|
|
||||||
}
|
|
||||||
return
|
|
||||||
})
|
|
||||||
.filter(Boolean)
|
|
||||||
|
|
||||||
validateAndNormalizeRules(newRules as Record<string, unknown>[])
|
|
||||||
|
|
||||||
shippingOption.rules = shippingOption.rules.map((rule) => {
|
shippingOption.rules = shippingOption.rules.map((rule) => {
|
||||||
if (!("id" in rule)) {
|
if (!("id" in rule)) {
|
||||||
|
validateAndNormalizeRules([rule])
|
||||||
return rule
|
return rule
|
||||||
}
|
}
|
||||||
return existingRulesMap.get(rule.id)!
|
return existingRulesMap.get(rule.id)!
|
||||||
|
|||||||
Reference in new issue
Block a user