fix(medusa,pricing,types): rules only gets updated/deleted upon passing an explicit object (#5711)
This commit is contained in:
7
.changeset/olive-penguins-cross.md
Normal file
7
.changeset/olive-penguins-cross.md
Normal file
@@ -0,0 +1,7 @@
|
||||
---
|
||||
"@medusajs/pricing": patch
|
||||
"@medusajs/medusa": patch
|
||||
"@medusajs/types": patch
|
||||
---
|
||||
|
||||
fix(medusa,pricing,types): rules only gets updated/deleted upon passing an explicit object
|
||||
@@ -223,4 +223,59 @@ describe("POST /admin/price-lists/:id", () => {
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should not delete customer groups if customer_groups is not passed as a param", async () => {
|
||||
await createVariantPriceSet({
|
||||
container: appContainer,
|
||||
variantId: variant2.id,
|
||||
prices: [],
|
||||
})
|
||||
|
||||
const [priceList] = await pricingModuleService.createPriceLists([
|
||||
{
|
||||
title: "test price list",
|
||||
description: "test",
|
||||
status: PriceListStatus.DRAFT,
|
||||
rules: {
|
||||
customer_group_id: ["customer-group-2"],
|
||||
},
|
||||
prices: [],
|
||||
},
|
||||
])
|
||||
|
||||
await createVariantPriceSet({
|
||||
container: appContainer,
|
||||
variantId: variant.id,
|
||||
prices: [],
|
||||
})
|
||||
|
||||
const api = useApi() as any
|
||||
const data = {
|
||||
status: PriceListStatus.ACTIVE,
|
||||
}
|
||||
|
||||
await api.post(`admin/price-lists/${priceList.id}`, data, adminHeaders)
|
||||
|
||||
const response = await api.get(
|
||||
`/admin/price-lists/${priceList.id}`,
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.price_list).toEqual(
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
customer_groups: [
|
||||
{
|
||||
id: expect.any(String),
|
||||
created_at: expect.any(String),
|
||||
updated_at: expect.any(String),
|
||||
deleted_at: null,
|
||||
name: "Test Group 2",
|
||||
metadata: null,
|
||||
},
|
||||
],
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -99,22 +99,22 @@ export default async (req, res) => {
|
||||
|
||||
if (featureFlagRouter.isFeatureEnabled(MedusaV2Flag.key)) {
|
||||
const updateVariantsWorkflow = updatePriceLists(req.scope)
|
||||
const rules: PricingTypes.CreatePriceListRules = {}
|
||||
const customerGroups = validated.customer_groups || []
|
||||
const customerGroups = validated.customer_groups
|
||||
delete validated.customer_groups
|
||||
|
||||
if (customerGroups.length) {
|
||||
rules["customer_group_id"] = customerGroups.map((group) => group.id)
|
||||
const updatePriceListInput = {
|
||||
id,
|
||||
...validated,
|
||||
} as PricingTypes.UpdatePriceListDTO
|
||||
|
||||
if (Array.isArray(customerGroups)) {
|
||||
updatePriceListInput.rules = {
|
||||
customer_group_id: customerGroups.map((group) => group.id),
|
||||
}
|
||||
}
|
||||
|
||||
const input = {
|
||||
price_lists: [
|
||||
{
|
||||
id,
|
||||
...validated,
|
||||
rules,
|
||||
},
|
||||
],
|
||||
price_lists: [updatePriceListInput],
|
||||
} as WorkflowTypes.PriceListWorkflow.UpdatePriceListWorkflowInputDTO
|
||||
|
||||
await updateVariantsWorkflow.run({
|
||||
|
||||
@@ -1499,10 +1499,15 @@ export default class PricingModuleService<
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
) {
|
||||
const updatedPriceLists: PricingTypes.PriceListDTO[] = []
|
||||
const priceListIds = data.map((d) => d.id)
|
||||
const ruleAttributes = data
|
||||
.map((priceListData) => Object.keys(priceListData.rules || {}))
|
||||
.flat()
|
||||
const ruleAttributes: string[] = []
|
||||
const priceListIds: string[] = []
|
||||
|
||||
for (const priceListData of data) {
|
||||
if (typeof priceListData.rules === "object") {
|
||||
ruleAttributes.push(...Object.keys(priceListData.rules))
|
||||
priceListIds.push(priceListData.id)
|
||||
}
|
||||
}
|
||||
|
||||
const existingPriceLists = await this.listPriceLists(
|
||||
{ id: priceListIds },
|
||||
@@ -1542,21 +1547,25 @@ export default class PricingModuleService<
|
||||
)
|
||||
|
||||
for (const priceListData of data) {
|
||||
const { rules = {}, ...priceListOnlyData } = priceListData
|
||||
const { rules, ...priceListOnlyData } = priceListData
|
||||
const updatePriceListData = {
|
||||
...priceListOnlyData,
|
||||
}
|
||||
|
||||
if (typeof rules === "object") {
|
||||
updatePriceListData.number_rules = Object.keys(rules).length
|
||||
}
|
||||
|
||||
const [updatedPriceList] = (await this.priceListService_.update(
|
||||
[
|
||||
{
|
||||
...priceListOnlyData,
|
||||
number_rules: Object.keys(rules).length,
|
||||
},
|
||||
],
|
||||
[updatePriceListData],
|
||||
sharedContext
|
||||
)) as unknown as PricingTypes.PriceListDTO[]
|
||||
|
||||
updatedPriceLists.push(updatedPriceList)
|
||||
|
||||
for (const [ruleAttribute, ruleValues = []] of Object.entries(rules)) {
|
||||
for (const [ruleAttribute, ruleValues = []] of Object.entries(
|
||||
rules || {}
|
||||
)) {
|
||||
let ruleType = ruleTypeMap.get(ruleAttribute)
|
||||
|
||||
if (!ruleType) {
|
||||
|
||||
@@ -182,11 +182,11 @@ export interface UpdatePriceListDTO {
|
||||
/**
|
||||
* The price list is enabled starting from this date.
|
||||
*/
|
||||
starts_at?: string
|
||||
starts_at?: string | null
|
||||
/**
|
||||
* The price list expires after this date.
|
||||
*/
|
||||
ends_at?: string
|
||||
ends_at?: string | null
|
||||
/**
|
||||
* The price list's status.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user