fix(medusa,pricing,types): rules only gets updated/deleted upon passing an explicit object (#5711)
This commit is contained in:
1 parent
07934cdaac
commit
fc1ef29ed9
5 files changed
+97
-26
No files matched your search
@@ -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)) {
|
if (featureFlagRouter.isFeatureEnabled(MedusaV2Flag.key)) {
|
||||||
const updateVariantsWorkflow = updatePriceLists(req.scope)
|
const updateVariantsWorkflow = updatePriceLists(req.scope)
|
||||||
const rules: PricingTypes.CreatePriceListRules = {}
|
const customerGroups = validated.customer_groups
|
||||||
const customerGroups = validated.customer_groups || []
|
|
||||||
delete validated.customer_groups
|
delete validated.customer_groups
|
||||||
|
|
||||||
if (customerGroups.length) {
|
const updatePriceListInput = {
|
||||||
rules["customer_group_id"] = customerGroups.map((group) => group.id)
|
id,
|
||||||
|
...validated,
|
||||||
|
} as PricingTypes.UpdatePriceListDTO
|
||||||
|
|
||||||
|
if (Array.isArray(customerGroups)) {
|
||||||
|
updatePriceListInput.rules = {
|
||||||
|
customer_group_id: customerGroups.map((group) => group.id),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const input = {
|
const input = {
|
||||||
price_lists: [
|
price_lists: [updatePriceListInput],
|
||||||
{
|
|
||||||
id,
|
|
||||||
...validated,
|
|
||||||
rules,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
} as WorkflowTypes.PriceListWorkflow.UpdatePriceListWorkflowInputDTO
|
} as WorkflowTypes.PriceListWorkflow.UpdatePriceListWorkflowInputDTO
|
||||||
|
|
||||||
await updateVariantsWorkflow.run({
|
await updateVariantsWorkflow.run({
|
||||||
|
|||||||
@@ -1499,10 +1499,15 @@ export default class PricingModuleService<
|
|||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
) {
|
) {
|
||||||
const updatedPriceLists: PricingTypes.PriceListDTO[] = []
|
const updatedPriceLists: PricingTypes.PriceListDTO[] = []
|
||||||
const priceListIds = data.map((d) => d.id)
|
const ruleAttributes: string[] = []
|
||||||
const ruleAttributes = data
|
const priceListIds: string[] = []
|
||||||
.map((priceListData) => Object.keys(priceListData.rules || {}))
|
|
||||||
.flat()
|
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(
|
const existingPriceLists = await this.listPriceLists(
|
||||||
{ id: priceListIds },
|
{ id: priceListIds },
|
||||||
@@ -1542,21 +1547,25 @@ export default class PricingModuleService<
|
|||||||
)
|
)
|
||||||
|
|
||||||
for (const priceListData of data) {
|
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(
|
const [updatedPriceList] = (await this.priceListService_.update(
|
||||||
[
|
[updatePriceListData],
|
||||||
{
|
|
||||||
...priceListOnlyData,
|
|
||||||
number_rules: Object.keys(rules).length,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
sharedContext
|
sharedContext
|
||||||
)) as unknown as PricingTypes.PriceListDTO[]
|
)) as unknown as PricingTypes.PriceListDTO[]
|
||||||
|
|
||||||
updatedPriceLists.push(updatedPriceList)
|
updatedPriceLists.push(updatedPriceList)
|
||||||
|
|
||||||
for (const [ruleAttribute, ruleValues = []] of Object.entries(rules)) {
|
for (const [ruleAttribute, ruleValues = []] of Object.entries(
|
||||||
|
rules || {}
|
||||||
|
)) {
|
||||||
let ruleType = ruleTypeMap.get(ruleAttribute)
|
let ruleType = ruleTypeMap.get(ruleAttribute)
|
||||||
|
|
||||||
if (!ruleType) {
|
if (!ruleType) {
|
||||||
|
|||||||
@@ -117,7 +117,7 @@ export interface PriceListPriceDTO extends CreateMoneyAmountDTO {
|
|||||||
* @interface
|
* @interface
|
||||||
*
|
*
|
||||||
* The price list's rules to be set. Each key of the object is a rule type's `rule_attribute`, and its value
|
* The price list's rules to be set. Each key of the object is a rule type's `rule_attribute`, and its value
|
||||||
* is the values of the rule.
|
* is the values of the rule.
|
||||||
*/
|
*/
|
||||||
export interface CreatePriceListRules extends Record<string, string[]> {}
|
export interface CreatePriceListRules extends Record<string, string[]> {}
|
||||||
|
|
||||||
@@ -182,11 +182,11 @@ export interface UpdatePriceListDTO {
|
|||||||
/**
|
/**
|
||||||
* The price list is enabled starting from this date.
|
* The price list is enabled starting from this date.
|
||||||
*/
|
*/
|
||||||
starts_at?: string
|
starts_at?: string | null
|
||||||
/**
|
/**
|
||||||
* The price list expires after this date.
|
* The price list expires after this date.
|
||||||
*/
|
*/
|
||||||
ends_at?: string
|
ends_at?: string | null
|
||||||
/**
|
/**
|
||||||
* The price list's status.
|
* The price list's status.
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in new issue
Block a user