@medusajs/pricing 0.2.0-snapshot-20240408084642 → 0.2.0-snapshot-20240412124109

Sign up to get free protection for your applications and to get access to all the features.
@@ -57,6 +57,7 @@ export default class PricingModuleService<TPriceSet extends PriceSet = PriceSet,
57
57
  upsert(data: UpsertPriceSetDTO, sharedContext?: Context): Promise<PriceSetDTO>;
58
58
  update(id: string, data: PricingTypes.UpdatePriceSetDTO, sharedContext?: Context): Promise<PriceSetDTO>;
59
59
  update(selector: PricingTypes.FilterablePriceSetProps, data: PricingTypes.UpdatePriceSetDTO, sharedContext?: Context): Promise<PriceSetDTO[]>;
60
+ private normalizeUpdateData;
60
61
  protected update_(data: UpdatePriceSetInput[], sharedContext?: Context): Promise<PriceSet[]>;
61
62
  addRules(data: PricingTypes.AddRulesDTO, sharedContext?: Context): Promise<PricingTypes.PriceSetDTO>;
62
63
  addRules(data: PricingTypes.AddRulesDTO[], sharedContext?: Context): Promise<PricingTypes.PriceSetDTO[]>;
@@ -134,11 +134,63 @@ class PricingModuleService extends utils_1.ModulesSdkUtils.abstractModuleService
134
134
  const priceSets = await this.baseRepository_.serialize(updateResult);
135
135
  return (0, utils_1.isString)(idOrSelector) ? priceSets[0] : priceSets;
136
136
  }
137
+ async normalizeUpdateData(data, sharedContext) {
138
+ const ruleAttributes = data
139
+ .map((d) => d.prices?.map((p) => Object.keys(p.rules ?? [])) ?? [])
140
+ .flat(Infinity)
141
+ .filter(Boolean);
142
+ const ruleTypes = await this.ruleTypeService_.list({ rule_attribute: ruleAttributes }, { take: null }, sharedContext);
143
+ const ruleTypeMap = ruleTypes.reduce((acc, curr) => {
144
+ acc.set(curr.rule_attribute, curr);
145
+ return acc;
146
+ }, new Map());
147
+ return data.map((priceSet) => {
148
+ const prices = priceSet.prices?.map((price) => {
149
+ const rules = Object.entries(price.rules ?? {}).map(([attribute, value]) => {
150
+ return {
151
+ price_set_id: priceSet.id,
152
+ rule_type_id: ruleTypeMap.get(attribute).id,
153
+ value,
154
+ };
155
+ });
156
+ const hasRulesInput = (0, utils_1.isPresent)(price.rules);
157
+ delete price.rules;
158
+ return {
159
+ ...price,
160
+ price_set_id: priceSet.id,
161
+ price_rules: hasRulesInput ? rules : undefined,
162
+ rules_count: hasRulesInput ? rules.length : undefined
163
+ };
164
+ });
165
+ return {
166
+ ...priceSet,
167
+ prices,
168
+ };
169
+ });
170
+ }
137
171
  async update_(data, sharedContext = {}) {
138
172
  // TODO: We are not handling rule types, rules, etc. here, add support after data models are finalized
139
173
  // TODO: Since money IDs are rarely passed, this will delete all previous data and insert new entries.
140
174
  // We can make the `insert` inside upsertWithReplace do an `upsert` instead to avoid this
141
- return this.priceSetService_.upsertWithReplace(data, { relations: ["prices"] }, sharedContext);
175
+ const normalizedData = await this.normalizeUpdateData(data, sharedContext);
176
+ const prices = normalizedData.flatMap((priceSet) => priceSet.prices || []);
177
+ const upsertedPrices = await this.priceService_.upsertWithReplace(prices, {
178
+ relations: ["price_rules"],
179
+ }, sharedContext);
180
+ const priceSetsToUpsert = normalizedData.map((priceSet) => {
181
+ const { prices, ...rest } = priceSet;
182
+ return {
183
+ ...rest,
184
+ prices: upsertedPrices
185
+ .filter((p) => p.price_set_id === priceSet.id)
186
+ .map((price) => {
187
+ // @ts-ignore
188
+ delete price.price_rules;
189
+ return price;
190
+ }),
191
+ };
192
+ });
193
+ return await this.priceSetService_.upsertWithReplace(priceSetsToUpsert, { relations: ["prices"] }, sharedContext);
142
194
  }
143
195
  async addRules(data, sharedContext = {}) {
144
196
  const inputs = Array.isArray(data) ? data : [data];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@medusajs/pricing",
3
- "version": "0.2.0-snapshot-20240408084642",
3
+ "version": "0.2.0-snapshot-20240412124109",
4
4
  "description": "Medusa Pricing module",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -40,7 +40,7 @@
40
40
  "@mikro-orm/cli": "5.9.7",
41
41
  "cross-env": "^5.2.1",
42
42
  "jest": "^29.6.3",
43
- "medusa-test-utils": "1.1.44-snapshot-20240408084642",
43
+ "medusa-test-utils": "1.1.44-snapshot-20240412124109",
44
44
  "rimraf": "^3.0.2",
45
45
  "ts-jest": "^29.1.1",
46
46
  "ts-node": "^10.9.1",
@@ -48,9 +48,9 @@
48
48
  "typescript": "^5.1.6"
49
49
  },
50
50
  "dependencies": {
51
- "@medusajs/modules-sdk": "1.12.11-snapshot-20240408084642",
52
- "@medusajs/types": "1.12.0-snapshot-20240408084642",
53
- "@medusajs/utils": "1.12.0-snapshot-20240408084642",
51
+ "@medusajs/modules-sdk": "1.12.11-snapshot-20240412124109",
52
+ "@medusajs/types": "1.12.0-snapshot-20240412124109",
53
+ "@medusajs/utils": "1.12.0-snapshot-20240412124109",
54
54
  "@mikro-orm/core": "5.9.7",
55
55
  "@mikro-orm/migrations": "5.9.7",
56
56
  "@mikro-orm/postgresql": "5.9.7",