fix(medusa,pricing,types): rules only gets updated/deleted upon passing an explicit object (#5711)

This commit is contained in:
Riqwan Thamir authored and GitHub committed 2023-11-24 13:54:23 +00:00
1 parent 07934cdaac
commit fc1ef29ed9
5 files changed
+124 -53

No files matched your search

+7
View 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)) { 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({
+21 -12
View File
@@ -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) {
+30 -30
View File
@@ -6,7 +6,7 @@ import { RuleTypeDTO } from "./rule-type"
/** /**
* @enum * @enum
* *
* The price list's status. * The price list's status.
*/ */
export enum PriceListStatus { export enum PriceListStatus {
@@ -22,7 +22,7 @@ export enum PriceListStatus {
/** /**
* @enum * @enum
* *
* The price list's type. * The price list's type.
*/ */
export enum PriceListType { export enum PriceListType {
@@ -38,7 +38,7 @@ export enum PriceListType {
/** /**
* @interface * @interface
* *
* A price list's details. * A price list's details.
*/ */
export interface PriceListDTO { export interface PriceListDTO {
@@ -68,34 +68,34 @@ export interface PriceListDTO {
number_rules?: number number_rules?: number
/** /**
* The associated price set money amounts. * The associated price set money amounts.
* *
* @expandable * @expandable
*/ */
price_set_money_amounts?: PriceSetMoneyAmountDTO[] price_set_money_amounts?: PriceSetMoneyAmountDTO[]
/** /**
* The associated money amounts. * The associated money amounts.
* *
* @expandable * @expandable
*/ */
money_amounts?: MoneyAmountDTO[] money_amounts?: MoneyAmountDTO[]
/** /**
* The associated rule types. * The associated rule types.
* *
* @expandable * @expandable
*/ */
rule_types?: RuleTypeDTO[] rule_types?: RuleTypeDTO[]
/** /**
* The price set's rules. * The price set's rules.
* *
* @expandable * @expandable
*/ */
rules?: PriceListRuleDTO[] rules?: PriceListRuleDTO[]
/** /**
* The price set's rules. * The price set's rules.
* *
* @privateRemarks * @privateRemarks
* Do we need both this and `rules`? * Do we need both this and `rules`?
* *
* @expandable * @expandable
*/ */
price_list_rules?: PriceListRuleDTO[] price_list_rules?: PriceListRuleDTO[]
@@ -103,7 +103,7 @@ export interface PriceListDTO {
/** /**
* @interface * @interface
* *
* The prices associated with a price list. * The prices associated with a price list.
*/ */
export interface PriceListPriceDTO extends CreateMoneyAmountDTO { export interface PriceListPriceDTO extends CreateMoneyAmountDTO {
@@ -115,15 +115,15 @@ 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[]> {}
/** /**
* @interface * @interface
* *
* The price list to create. * The price list to create.
*/ */
export interface CreatePriceListDTO { export interface CreatePriceListDTO {
@@ -167,7 +167,7 @@ export interface CreatePriceListDTO {
/** /**
* @interface * @interface
* *
* The attributes to update in a price list. * The attributes to update in a price list.
*/ */
export interface UpdatePriceListDTO { export interface UpdatePriceListDTO {
@@ -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.
*/ */
@@ -203,7 +203,7 @@ export interface UpdatePriceListDTO {
/** /**
* @inteface * @inteface
* *
* Filters to apply on price lists. * Filters to apply on price lists.
*/ */
export interface FilterablePriceListProps export interface FilterablePriceListProps
@@ -232,7 +232,7 @@ export interface FilterablePriceListProps
/** /**
* @interface * @interface
* *
* Filters to apply on price list rules. * Filters to apply on price list rules.
*/ */
export interface FilterablePriceListRuleProps export interface FilterablePriceListRuleProps
@@ -264,7 +264,7 @@ export interface FilterablePriceListRuleValueProps
/** /**
* @interface * @interface
* *
* The price list rule's details. * The price list rule's details.
*/ */
export interface PriceListRuleDTO { export interface PriceListRuleDTO {
@@ -274,26 +274,26 @@ export interface PriceListRuleDTO {
id: string id: string
/** /**
* The value of the rule. * The value of the rule.
* *
* @privateRemarks * @privateRemarks
* Shouldn't this be in PriceListRuleValueDTO only? * Shouldn't this be in PriceListRuleValueDTO only?
*/ */
value: string value: string
/** /**
* The associated rule type. * The associated rule type.
* *
* @expandable * @expandable
*/ */
rule_type: RuleTypeDTO rule_type: RuleTypeDTO
/** /**
* The associated price list. * The associated price list.
* *
* @expandable * @expandable
*/ */
price_list: PriceListDTO price_list: PriceListDTO
/** /**
* The associated rule values. * The associated rule values.
* *
* @expandable * @expandable
*/ */
price_list_rule_values?: PriceListRuleValueDTO[] price_list_rule_values?: PriceListRuleValueDTO[]
@@ -301,7 +301,7 @@ export interface PriceListRuleDTO {
/** /**
* @interface * @interface
* *
* The price list rule to create. * The price list rule to create.
*/ */
export interface CreatePriceListRuleDTO { export interface CreatePriceListRuleDTO {
@@ -325,7 +325,7 @@ export interface CreatePriceListRuleDTO {
/** /**
* @interface * @interface
* *
* The attributes to update in a price list rule. * The attributes to update in a price list rule.
*/ */
export interface UpdatePriceListRuleDTO { export interface UpdatePriceListRuleDTO {
@@ -353,7 +353,7 @@ export interface UpdatePriceListRuleDTO {
/** /**
* @interface * @interface
* *
* The price list rule value's details. * The price list rule value's details.
*/ */
export interface PriceListRuleValueDTO { export interface PriceListRuleValueDTO {
@@ -367,7 +367,7 @@ export interface PriceListRuleValueDTO {
value: string value: string
/** /**
* The associated price list rule. * The associated price list rule.
* *
* @expandable * @expandable
*/ */
price_list_rule: PriceListRuleDTO price_list_rule: PriceListRuleDTO
@@ -387,7 +387,7 @@ export interface UpdatePriceListRuleValueDTO {
/** /**
* @interface * @interface
* *
* The prices to be added to a price list. * The prices to be added to a price list.
*/ */
export interface AddPriceListPricesDTO { export interface AddPriceListPricesDTO {
@@ -403,7 +403,7 @@ export interface AddPriceListPricesDTO {
/** /**
* @interface * @interface
* *
* The rules to add to a price list. * The rules to add to a price list.
*/ */
export interface SetPriceListRulesDTO { export interface SetPriceListRulesDTO {
@@ -420,7 +420,7 @@ export interface SetPriceListRulesDTO {
/** /**
* @interface * @interface
* *
* The rules to remove from a price list. * The rules to remove from a price list.
*/ */
export interface RemovePriceListRulesDTO { export interface RemovePriceListRulesDTO {