@medusajs/pricing 0.1.13-snapshot-20240607093839 → 0.1.13-snapshot-20240701122250

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. package/dist/index.d.ts +2 -3
  2. package/dist/index.js +7 -4
  3. package/dist/joiner-config.d.ts +1 -8
  4. package/dist/joiner-config.js +10 -52
  5. package/dist/migrations/Migration20240626133555.d.ts +4 -0
  6. package/dist/migrations/Migration20240626133555.js +33 -0
  7. package/dist/models/index.d.ts +0 -3
  8. package/dist/models/index.js +1 -7
  9. package/dist/models/price-list-rule.d.ts +4 -7
  10. package/dist/models/price-list-rule.js +6 -27
  11. package/dist/models/price-list.d.ts +0 -2
  12. package/dist/models/price-list.js +0 -9
  13. package/dist/models/price-rule.d.ts +3 -8
  14. package/dist/models/price-rule.js +4 -43
  15. package/dist/models/price-set.d.ts +0 -4
  16. package/dist/models/price-set.js +0 -19
  17. package/dist/models/price.d.ts +3 -3
  18. package/dist/models/price.js +1 -1
  19. package/dist/repositories/pricing.js +25 -30
  20. package/dist/services/index.d.ts +0 -2
  21. package/dist/services/index.js +1 -5
  22. package/dist/services/pricing-module.d.ts +37 -48
  23. package/dist/services/pricing-module.js +264 -477
  24. package/dist/types/services/price-list.d.ts +9 -9
  25. package/dist/utils/events.d.ts +0 -6
  26. package/dist/utils/events.js +6 -12
  27. package/package.json +7 -10
  28. package/dist/models/price-list-rule-value.d.ts +0 -17
  29. package/dist/models/price-list-rule-value.js +0 -102
  30. package/dist/models/price-set-rule-type.d.ts +0 -10
  31. package/dist/models/price-set-rule-type.js +0 -108
  32. package/dist/models/rule-type.d.ts +0 -17
  33. package/dist/models/rule-type.js +0 -100
  34. package/dist/module-definition.d.ts +0 -2
  35. package/dist/module-definition.js +0 -8
  36. package/dist/scripts/bin/run-seed.d.ts +0 -3
  37. package/dist/scripts/bin/run-seed.js +0 -38
  38. package/dist/scripts/seed.d.ts +0 -4
  39. package/dist/scripts/seed.js +0 -67
  40. package/dist/services/price-list.d.ts +0 -16
  41. package/dist/services/price-list.js +0 -34
  42. package/dist/services/rule-type.d.ts +0 -15
  43. package/dist/services/rule-type.js +0 -53
@@ -17,33 +17,25 @@ const utils_1 = require("@medusajs/utils");
17
17
  const _models_1 = require("../models");
18
18
  const _utils_1 = require("../utils");
19
19
  const joiner_config_1 = require("../joiner-config");
20
- const price_list_1 = require("../models/price-list");
21
- const price_set_1 = require("../models/price-set");
22
- const generateMethodForModels = [
23
- _models_1.PriceList,
24
- _models_1.PriceListRule,
25
- _models_1.PriceListRuleValue,
26
- _models_1.PriceRule,
27
- _models_1.Price,
28
- _models_1.PriceSetRuleType,
29
- _models_1.RuleType,
30
- ];
31
- class PricingModuleService extends utils_1.ModulesSdkUtils.abstractModuleServiceFactory(_models_1.PriceSet, generateMethodForModels, joiner_config_1.entityNameToLinkableKeysMap) {
32
- constructor({ baseRepository, pricingRepository, ruleTypeService, priceSetService, priceRuleService, priceSetRuleTypeService, priceService, priceListService, priceListRuleService, priceListRuleValueService, }, moduleDeclaration) {
20
+ const generateMethodForModels = {
21
+ PriceSet: _models_1.PriceSet,
22
+ PriceList: _models_1.PriceList,
23
+ PriceListRule: _models_1.PriceListRule,
24
+ PriceRule: _models_1.PriceRule,
25
+ Price: _models_1.Price,
26
+ };
27
+ class PricingModuleService extends utils_1.ModulesSdkUtils.MedusaService(generateMethodForModels, joiner_config_1.entityNameToLinkableKeysMap) {
28
+ constructor({ baseRepository, pricingRepository, priceSetService, priceRuleService, priceService, priceListService, priceListRuleService, }, moduleDeclaration) {
33
29
  // @ts-ignore
34
30
  super(...arguments);
35
31
  this.moduleDeclaration = moduleDeclaration;
36
32
  this.baseRepository_ = baseRepository;
37
33
  this.pricingRepository_ = pricingRepository;
38
- this.ruleTypeService_ = ruleTypeService;
39
34
  this.priceSetService_ = priceSetService;
40
- this.ruleTypeService_ = ruleTypeService;
41
35
  this.priceRuleService_ = priceRuleService;
42
- this.priceSetRuleTypeService_ = priceSetRuleTypeService;
43
36
  this.priceService_ = priceService;
44
37
  this.priceListService_ = priceListService;
45
38
  this.priceListRuleService_ = priceListRuleService;
46
- this.priceListRuleValueService_ = priceListRuleValueService;
47
39
  }
48
40
  __joinerConfig() {
49
41
  return joiner_config_1.joinerConfig;
@@ -60,39 +52,43 @@ class PricingModuleService extends utils_1.ModulesSdkUtils.abstractModuleService
60
52
  config.relations?.splice(fieldIdx, 1);
61
53
  return pricingContext;
62
54
  }
63
- async list(filters = {}, config = {}, sharedContext = {}) {
55
+ // @ts-expect-error
56
+ async listPriceSets(filters = {}, config = {}, sharedContext = {}) {
64
57
  const pricingContext = this.setupCalculatedPriceConfig_(filters, config);
65
- const priceSets = await super.list(filters, config, sharedContext);
66
- if (pricingContext && priceSets.length) {
67
- const priceSetIds = [];
68
- const priceSetMap = new Map();
69
- for (const priceSet of priceSets) {
70
- priceSetIds.push(priceSet.id);
71
- priceSetMap.set(priceSet.id, priceSet);
72
- }
73
- const calculatedPrices = await this.calculatePrices({ id: priceSets.map((p) => p.id) }, { context: pricingContext }, sharedContext);
74
- for (const calculatedPrice of calculatedPrices) {
75
- const priceSet = priceSetMap.get(calculatedPrice.id);
76
- priceSet.calculated_price = calculatedPrice;
77
- }
58
+ const priceSets = await super.listPriceSets(filters, config, sharedContext);
59
+ if (!pricingContext || !priceSets.length) {
60
+ return priceSets;
61
+ }
62
+ const priceSetIds = [];
63
+ const priceSetMap = new Map();
64
+ for (const priceSet of priceSets) {
65
+ priceSetIds.push(priceSet.id);
66
+ priceSetMap.set(priceSet.id, priceSet);
67
+ }
68
+ const calculatedPrices = await this.calculatePrices({ id: priceSetIds }, { context: pricingContext }, sharedContext);
69
+ for (const calculatedPrice of calculatedPrices) {
70
+ const priceSet = priceSetMap.get(calculatedPrice.id);
71
+ priceSet.calculated_price = calculatedPrice;
78
72
  }
79
73
  return priceSets;
80
74
  }
81
- async listAndCount(filters = {}, config = {}, sharedContext = {}) {
75
+ // @ts-expect-error
76
+ async listAndCountPriceSets(filters = {}, config = {}, sharedContext = {}) {
82
77
  const pricingContext = this.setupCalculatedPriceConfig_(filters, config);
83
- const [priceSets, count] = await super.listAndCount(filters, config, sharedContext);
84
- if (pricingContext && priceSets.length) {
85
- const priceSetIds = [];
86
- const priceSetMap = new Map();
87
- for (const priceSet of priceSets) {
88
- priceSetIds.push(priceSet.id);
89
- priceSetMap.set(priceSet.id, priceSet);
90
- }
91
- const calculatedPrices = await this.calculatePrices({ id: priceSets.map((p) => p.id) }, { context: pricingContext }, sharedContext);
92
- for (const calculatedPrice of calculatedPrices) {
93
- const priceSet = priceSetMap.get(calculatedPrice.id);
94
- priceSet.calculated_price = calculatedPrice;
95
- }
78
+ const [priceSets, count] = await super.listAndCountPriceSets(filters, config, sharedContext);
79
+ if (!pricingContext || !priceSets.length) {
80
+ return [priceSets, count];
81
+ }
82
+ const priceSetIds = [];
83
+ const priceSetMap = new Map();
84
+ for (const priceSet of priceSets) {
85
+ priceSetIds.push(priceSet.id);
86
+ priceSetMap.set(priceSet.id, priceSet);
87
+ }
88
+ const calculatedPrices = await this.calculatePrices({ id: priceSetIds }, { context: pricingContext }, sharedContext);
89
+ for (const calculatedPrice of calculatedPrices) {
90
+ const priceSet = priceSetMap.get(calculatedPrice.id);
91
+ priceSet.calculated_price = calculatedPrice;
96
92
  }
97
93
  return [priceSets, count];
98
94
  }
@@ -139,17 +135,12 @@ class PricingModuleService extends utils_1.ModulesSdkUtils.abstractModuleService
139
135
  });
140
136
  return JSON.parse(JSON.stringify(calculatedPrices));
141
137
  }
142
- async create(data, sharedContext = {}) {
138
+ async createPriceSets(data, sharedContext = {}) {
143
139
  const input = Array.isArray(data) ? data : [data];
144
- const priceSets = await this.create_(input, sharedContext);
140
+ const priceSets = await this.createPriceSets_(input, sharedContext);
145
141
  // TODO: Remove the need to refetch the data here
146
- const dbPriceSets = await this.list({ id: priceSets.map((p) => p.id) }, {
147
- relations: [
148
- "rule_types",
149
- "prices",
150
- "price_rules",
151
- "prices.price_rules",
152
- ],
142
+ const dbPriceSets = await this.listPriceSets({ id: priceSets.map((p) => p.id) }, {
143
+ relations: ["prices", "prices.price_rules"],
153
144
  }, sharedContext);
154
145
  // Ensure the output to be in the same order as the input
155
146
  const results = priceSets.map((priceSet) => {
@@ -157,21 +148,21 @@ class PricingModuleService extends utils_1.ModulesSdkUtils.abstractModuleService
157
148
  });
158
149
  return await this.baseRepository_.serialize(Array.isArray(data) ? results : results[0]);
159
150
  }
160
- async upsert(data, sharedContext = {}) {
151
+ async upsertPriceSets(data, sharedContext = {}) {
161
152
  const input = Array.isArray(data) ? data : [data];
162
153
  const forUpdate = input.filter((priceSet) => !!priceSet.id);
163
154
  const forCreate = input.filter((priceSet) => !priceSet.id);
164
155
  const operations = [];
165
156
  if (forCreate.length) {
166
- operations.push(this.create_(forCreate, sharedContext));
157
+ operations.push(this.createPriceSets_(forCreate, sharedContext));
167
158
  }
168
159
  if (forUpdate.length) {
169
- operations.push(this.update_(forUpdate, sharedContext));
160
+ operations.push(this.updatePriceSets_(forUpdate, sharedContext));
170
161
  }
171
162
  const result = (await (0, utils_1.promiseAll)(operations)).flat();
172
163
  return await this.baseRepository_.serialize(Array.isArray(data) ? result : result[0]);
173
164
  }
174
- async update(idOrSelector, data, sharedContext = {}) {
165
+ async updatePriceSets(idOrSelector, data, sharedContext = {}) {
175
166
  let normalizedInput = [];
176
167
  if ((0, utils_1.isString)(idOrSelector)) {
177
168
  // Check if the ID exists, it will throw if not.
@@ -185,26 +176,16 @@ class PricingModuleService extends utils_1.ModulesSdkUtils.abstractModuleService
185
176
  ...data,
186
177
  }));
187
178
  }
188
- const updateResult = await this.update_(normalizedInput, sharedContext);
179
+ const updateResult = await this.updatePriceSets_(normalizedInput, sharedContext);
189
180
  const priceSets = await this.baseRepository_.serialize(updateResult);
190
181
  return (0, utils_1.isString)(idOrSelector) ? priceSets[0] : priceSets;
191
182
  }
192
- async normalizeUpdateData(data, sharedContext) {
193
- const ruleAttributes = data
194
- .map((d) => d.prices?.map((p) => Object.keys(p.rules ?? [])) ?? [])
195
- .flat(Infinity)
196
- .filter(Boolean);
197
- const ruleTypes = await this.ruleTypeService_.list({ rule_attribute: ruleAttributes }, { take: null }, sharedContext);
198
- const ruleTypeMap = ruleTypes.reduce((acc, curr) => {
199
- acc.set(curr.rule_attribute, curr);
200
- return acc;
201
- }, new Map());
183
+ async normalizeUpdateData(data) {
202
184
  return data.map((priceSet) => {
203
185
  const prices = priceSet.prices?.map((price) => {
204
186
  const rules = Object.entries(price.rules ?? {}).map(([attribute, value]) => {
205
187
  return {
206
- price_set_id: priceSet.id,
207
- rule_type_id: ruleTypeMap.get(attribute).id,
188
+ attribute,
208
189
  value,
209
190
  };
210
191
  });
@@ -223,15 +204,12 @@ class PricingModuleService extends utils_1.ModulesSdkUtils.abstractModuleService
223
204
  };
224
205
  });
225
206
  }
226
- async update_(data, sharedContext = {}) {
227
- // TODO: We are not handling rule types, rules, etc. here, add support after data models are finalized
207
+ async updatePriceSets_(data, sharedContext = {}) {
228
208
  // TODO: Since money IDs are rarely passed, this will delete all previous data and insert new entries.
229
209
  // We can make the `insert` inside upsertWithReplace do an `upsert` instead to avoid this
230
- const normalizedData = await this.normalizeUpdateData(data, sharedContext);
210
+ const normalizedData = await this.normalizeUpdateData(data);
231
211
  const prices = normalizedData.flatMap((priceSet) => priceSet.prices || []);
232
- const { entities: upsertedPrices } = await this.priceService_.upsertWithReplace(prices, {
233
- relations: ["price_rules"],
234
- }, sharedContext);
212
+ const { entities: upsertedPrices } = await this.priceService_.upsertWithReplace(prices, { relations: ["price_rules"] }, sharedContext);
235
213
  const priceSetsToUpsert = normalizedData.map((priceSet) => {
236
214
  const { prices, ...rest } = priceSet;
237
215
  return {
@@ -248,40 +226,21 @@ class PricingModuleService extends utils_1.ModulesSdkUtils.abstractModuleService
248
226
  const { entities: priceSets } = await this.priceSetService_.upsertWithReplace(priceSetsToUpsert, { relations: ["prices"] }, sharedContext);
249
227
  return priceSets;
250
228
  }
251
- async addRules(data, sharedContext = {}) {
252
- const inputs = Array.isArray(data) ? data : [data];
253
- const priceSets = await this.addRules_(inputs, sharedContext);
254
- const dbPriceSets = await this.list({ id: priceSets.map(({ id }) => id) }, { relations: ["rule_types"] });
255
- const orderedPriceSets = priceSets.map((priceSet) => {
256
- return dbPriceSets.find((p) => p.id === priceSet.id);
257
- });
258
- return Array.isArray(data) ? orderedPriceSets : orderedPriceSets[0];
259
- }
260
229
  async addPrices(data, sharedContext = {}) {
261
230
  const input = Array.isArray(data) ? data : [data];
262
231
  await this.addPrices_(input, sharedContext);
263
- const dbPrices = await this.list({ id: input.map((d) => d.priceSetId) }, { relations: ["prices"] }, sharedContext);
232
+ const dbPrices = await this.listPriceSets({ id: input.map((d) => d.priceSetId) }, { relations: ["prices"] }, sharedContext);
264
233
  const orderedPriceSets = input.map((inputItem) => {
265
234
  return dbPrices.find((p) => p.id === inputItem.priceSetId);
266
235
  });
267
236
  return Array.isArray(data) ? orderedPriceSets : orderedPriceSets[0];
268
237
  }
269
- async removeRules(data, sharedContext = {}) {
270
- const priceSets = await this.priceSetService_.list({ id: data.map((d) => d.id) }, {}, sharedContext);
271
- const priceSetIds = priceSets.map((ps) => ps.id);
272
- const ruleTypes = await this.ruleTypeService_.list({
273
- rule_attribute: data.map((d) => d.rules || []).flat(),
274
- }, { take: null }, sharedContext);
275
- const ruleTypeIds = ruleTypes.map((rt) => rt.id);
276
- const priceSetRuleTypes = await this.priceSetRuleTypeService_.list({ price_set_id: priceSetIds, rule_type_id: ruleTypeIds }, { take: null }, sharedContext);
277
- const priceRules = await this.priceRuleService_.list({ price_set_id: priceSetIds, rule_type_id: ruleTypeIds }, { select: ["price"], take: null }, sharedContext);
278
- await this.priceSetRuleTypeService_.delete(priceSetRuleTypes.map((psrt) => psrt.id), sharedContext);
279
- await this.priceService_.delete(priceRules.map((pr) => pr.price.id), sharedContext);
280
- }
238
+ // @ts-ignore
281
239
  async createPriceLists(data, sharedContext = {}) {
282
240
  const priceLists = await this.createPriceLists_(data, sharedContext);
283
241
  return await this.baseRepository_.serialize(priceLists);
284
242
  }
243
+ // @ts-ignore
285
244
  async updatePriceLists(data, sharedContext = {}) {
286
245
  const priceLists = await this.updatePriceLists_(data, sharedContext);
287
246
  return await this.baseRepository_.serialize(priceLists);
@@ -305,69 +264,36 @@ class PricingModuleService extends utils_1.ModulesSdkUtils.abstractModuleService
305
264
  const [priceList] = await this.removePriceListRules_([data], sharedContext);
306
265
  return await this.baseRepository_.serialize(priceList);
307
266
  }
308
- async create_(data, sharedContext = {}) {
267
+ async createPriceSets_(data, sharedContext = {}) {
309
268
  const input = Array.isArray(data) ? data : [data];
310
- const ruleAttributes = (0, utils_1.deduplicate)(data.map((d) => d.rules?.map((r) => r.rule_attribute) ?? []).flat());
311
- const ruleTypes = await this.ruleTypeService_.list({ rule_attribute: ruleAttributes }, { take: null }, sharedContext);
312
- const ruleTypeMap = ruleTypes.reduce((acc, curr) => {
313
- acc.set(curr.rule_attribute, curr);
314
- return acc;
315
- }, new Map());
316
- const invalidRuleAttributes = ruleAttributes.filter((r) => !ruleTypeMap.has(r));
317
- if (invalidRuleAttributes.length > 0) {
318
- throw new utils_1.MedusaError(utils_1.MedusaError.Types.NOT_FOUND, `Rule types don't exist for: ${invalidRuleAttributes.join(", ")}`);
319
- }
320
- const invalidMoneyAmountRule = data
321
- .map((d) => d.prices?.map((ma) => Object.keys(ma?.rules ?? {})).flat() ?? [])
322
- .flat()
323
- .filter((r) => !ruleTypeMap.has(r));
324
- if (invalidMoneyAmountRule.length > 0) {
325
- throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `Rule types don't exist for money amounts with rule attribute: ${invalidMoneyAmountRule.join(", ")}`);
326
- }
327
- const ruleSetRuleTypeToCreateMap = new Map();
328
269
  const toCreate = input.map((inputData) => {
329
- const id = (0, utils_1.generateEntityId)(inputData.id, price_set_1.PriceSetIdPrefix);
330
- const { prices, rules = [], ...rest } = inputData;
331
- let pricesData = [];
332
- rules.forEach((rule) => {
333
- const priceSetRuleType = {
334
- rule_type_id: ruleTypeMap.get(rule.rule_attribute).id,
335
- price_set_id: id,
336
- };
337
- ruleSetRuleTypeToCreateMap.set(JSON.stringify(priceSetRuleType), priceSetRuleType);
338
- });
339
- if (inputData.prices) {
340
- pricesData = inputData.prices.map((price) => {
341
- let { rules: priceRules = {}, ...rest } = price;
342
- const cleanRules = priceRules ? (0, utils_1.removeNullish)(priceRules) : {};
343
- const numberOfRules = Object.keys(cleanRules).length;
344
- const rulesDataMap = new Map();
345
- Object.entries(priceRules).map(([attribute, value]) => {
346
- const rule = {
347
- price_set_id: id,
348
- rule_type_id: ruleTypeMap.get(attribute).id,
349
- value,
350
- };
351
- rulesDataMap.set(JSON.stringify(rule), rule);
352
- const priceSetRuleType = {
353
- rule_type_id: ruleTypeMap.get(attribute).id,
354
- price_set_id: id,
355
- };
356
- ruleSetRuleTypeToCreateMap.set(JSON.stringify(priceSetRuleType), priceSetRuleType);
357
- });
358
- return {
359
- ...rest,
360
- title: "", // TODO: accept title
361
- rules_count: numberOfRules,
362
- price_rules: Array.from(rulesDataMap.values()),
270
+ const entry = {
271
+ ...inputData,
272
+ };
273
+ if (!inputData.prices) {
274
+ return entry;
275
+ }
276
+ const pricesData = inputData.prices.map((price) => {
277
+ let { rules: priceRules = {}, ...rest } = price;
278
+ const cleanRules = priceRules ? (0, utils_1.removeNullish)(priceRules) : {};
279
+ const rules = Object.entries(cleanRules);
280
+ const numberOfRules = rules.length;
281
+ const rulesDataMap = new Map();
282
+ rules.map(([attribute, value]) => {
283
+ const rule = {
284
+ attribute,
285
+ value,
363
286
  };
287
+ rulesDataMap.set(JSON.stringify(rule), rule);
364
288
  });
365
- }
366
- return {
367
- ...rest,
368
- id,
369
- prices: pricesData,
370
- };
289
+ return {
290
+ ...rest,
291
+ rules_count: numberOfRules,
292
+ price_rules: Array.from(rulesDataMap.values()),
293
+ };
294
+ });
295
+ entry.prices = pricesData;
296
+ return entry;
371
297
  });
372
298
  // Bulk create price sets
373
299
  const createdPriceSets = await this.priceSetService_.create(toCreate, sharedContext);
@@ -403,85 +329,27 @@ class PricingModuleService extends utils_1.ModulesSdkUtils.abstractModuleService
403
329
  data: eventsData.priceRules,
404
330
  sharedContext,
405
331
  });
406
- if (ruleSetRuleTypeToCreateMap.size) {
407
- await this.priceSetRuleTypeService_.create(Array.from(ruleSetRuleTypeToCreateMap.values()), sharedContext);
408
- }
409
332
  return createdPriceSets;
410
333
  }
411
- async addRules_(inputs, sharedContext = {}) {
412
- const priceSets = await this.priceSetService_.list({ id: inputs.map((d) => d.priceSetId) }, { relations: ["rule_types"] }, sharedContext);
413
- const priceSetRuleTypeMap = new Map(priceSets.map((priceSet) => [
414
- priceSet.id,
415
- new Map([...priceSet.rule_types].map((rt) => [rt.rule_attribute, rt])),
416
- ]));
417
- const priceSetMap = new Map(priceSets.map((p) => [p.id, p]));
418
- const invalidPriceSetInputs = inputs.filter((d) => !priceSetMap.has(d.priceSetId));
419
- if (invalidPriceSetInputs.length) {
420
- throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `PriceSets with ids: ${invalidPriceSetInputs
421
- .map((d) => d.priceSetId)
422
- .join(", ")} was not found`);
423
- }
424
- const ruleTypes = await this.ruleTypeService_.list({
425
- rule_attribute: inputs
426
- .map((data) => data.rules.map((r) => r.attribute))
427
- .flat(),
428
- }, { take: null }, sharedContext);
429
- const ruleTypeMap = new Map(ruleTypes.map((rt) => [rt.rule_attribute, rt]));
430
- const invalidRuleAttributeInputs = inputs
431
- .map((d) => d.rules.map((r) => r.attribute))
432
- .flat()
433
- .filter((r) => !ruleTypeMap.has(r));
434
- if (invalidRuleAttributeInputs.length) {
435
- throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `Rule types don't exist for attributes: ${[
436
- ...new Set(invalidRuleAttributeInputs),
437
- ].join(", ")}`);
438
- }
439
- const priceSetRuleTypesCreate = [];
440
- inputs.forEach((data) => {
441
- for (const rule of data.rules) {
442
- if (priceSetRuleTypeMap.get(data.priceSetId).has(rule.attribute)) {
443
- continue;
444
- }
445
- priceSetRuleTypesCreate.push({
446
- rule_type_id: ruleTypeMap.get(rule.attribute).id,
447
- price_set_id: priceSetMap.get(data.priceSetId).id,
448
- });
449
- }
450
- });
451
- await this.priceSetRuleTypeService_.create(priceSetRuleTypesCreate, sharedContext);
452
- return priceSets;
453
- }
454
334
  async addPrices_(input, sharedContext = {}) {
455
- const priceSets = await this.list({ id: input.map((d) => d.priceSetId) }, { relations: ["rule_types"] }, sharedContext);
335
+ const priceSets = await this.listPriceSets({ id: input.map((d) => d.priceSetId) }, {}, sharedContext);
456
336
  const priceSetMap = new Map(priceSets.map((p) => [p.id, p]));
457
- const ruleTypeMap = new Map(priceSets.map((priceSet) => [
458
- priceSet.id,
459
- new Map(priceSet.rule_types?.map((rt) => [rt.rule_attribute, rt]) ?? []),
460
- ]));
461
- input.forEach(({ priceSetId, prices }) => {
337
+ input.forEach(({ priceSetId }) => {
462
338
  const priceSet = priceSetMap.get(priceSetId);
463
339
  if (!priceSet) {
464
340
  throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `Price set with id: ${priceSetId} not found`);
465
341
  }
466
- const ruleAttributes = prices
467
- .map((d) => (d.rules ? Object.keys(d.rules) : []))
468
- .flat();
469
- const invalidRuleAttributes = ruleAttributes.filter((r) => !ruleTypeMap.get(priceSetId).has(r));
470
- if (invalidRuleAttributes.length > 0) {
471
- throw new utils_1.MedusaError(utils_1.MedusaError.Types.NOT_FOUND, `Rule types don't exist for: ${invalidRuleAttributes.join(", ")}`);
472
- }
473
342
  });
474
343
  const pricesToCreate = input.flatMap(({ priceSetId, prices }) => prices.map((price) => {
475
344
  const numberOfRules = Object.entries(price?.rules ?? {}).length;
476
345
  const priceRules = Object.entries(price.rules ?? {}).map(([attribute, value]) => ({
477
- rule_type_id: ruleTypeMap.get(priceSetId).get(attribute).id,
478
346
  price_set_id: priceSetId,
347
+ attribute: attribute,
479
348
  value,
480
349
  }));
481
350
  return {
482
351
  ...price,
483
352
  price_set_id: priceSetId,
484
- title: "test", // TODO: accept title
485
353
  rules_count: numberOfRules,
486
354
  price_rules: priceRules,
487
355
  };
@@ -518,56 +386,52 @@ class PricingModuleService extends utils_1.ModulesSdkUtils.abstractModuleService
518
386
  return prices;
519
387
  }
520
388
  async createPriceLists_(data, sharedContext = {}) {
521
- const ruleTypeAttributes = [];
522
- for (const priceListData of data) {
523
- const { prices = [], rules: priceListRules = {} } = priceListData;
524
- ruleTypeAttributes.push(...Object.keys(priceListRules));
525
- for (const price of prices) {
526
- const { rules: priceListPriceRules = {} } = price;
527
- ruleTypeAttributes.push(...Object.keys(priceListPriceRules));
528
- }
529
- }
530
- const ruleTypes = await this.listRuleTypes({ rule_attribute: ruleTypeAttributes }, { take: null }, sharedContext);
531
- const invalidRuleTypes = (0, utils_1.arrayDifference)((0, utils_1.deduplicate)(ruleTypeAttributes), ruleTypes.map((ruleType) => ruleType.rule_attribute));
532
- if (invalidRuleTypes.length) {
533
- throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `Cannot find RuleTypes with rule_attribute - ${invalidRuleTypes.join(", ")}`);
534
- }
535
- const ruleTypeMap = new Map(ruleTypes.map((rt) => [rt.rule_attribute, rt]));
536
- const priceListsToCreate = data.map((priceListData) => {
537
- const id = (0, utils_1.generateEntityId)(priceListData.id, price_list_1.PriceListIdPrefix);
538
- const { prices = [], rules = {}, ...rest } = priceListData;
539
- (0, _utils_1.validatePriceListDates)(priceListData);
540
- const priceListRules = Object.entries(rules).map(([attribute, value]) => {
541
- const ruleType = ruleTypeMap.get(attribute);
542
- return {
543
- price_list_id: id,
544
- rule_type_id: ruleType.id,
545
- price_list_rule_values: value.map((v) => ({ value: v })),
546
- };
547
- });
548
- const pricesData = prices.map((price) => {
549
- const priceRules = Object.entries(price.rules ?? {}).map(([ruleAttribute, ruleValue]) => {
389
+ const normalized = this.normalizePriceListDate(data);
390
+ const priceListsToCreate = normalized.map((priceListData) => {
391
+ const entry = {
392
+ ...priceListData,
393
+ rules: undefined,
394
+ };
395
+ if (priceListData.prices) {
396
+ const pricesData = priceListData.prices.map((price) => {
397
+ let { rules: priceRules = {}, ...rest } = price;
398
+ const cleanRules = priceRules ? (0, utils_1.removeNullish)(priceRules) : {};
399
+ const rules = Object.entries(cleanRules);
400
+ const numberOfRules = rules.length;
401
+ const rulesDataMap = new Map();
402
+ rules.map(([attribute, value]) => {
403
+ const rule = {
404
+ attribute,
405
+ value,
406
+ };
407
+ rulesDataMap.set(JSON.stringify(rule), rule);
408
+ });
550
409
  return {
551
- price_set_id: price.price_set_id,
552
- rule_type_id: ruleTypeMap.get(ruleAttribute)?.id,
553
- value: ruleValue,
410
+ ...rest,
411
+ rules_count: numberOfRules,
412
+ price_rules: Array.from(rulesDataMap.values()),
554
413
  };
555
414
  });
556
- return {
557
- price_list_id: id,
558
- title: "test",
559
- rules_count: Object.keys(price.rules ?? {}).length,
560
- price_rules: priceRules,
561
- ...price,
562
- };
563
- });
564
- return {
565
- id,
566
- ...rest,
567
- rules_count: Object.keys(rules).length,
568
- price_list_rules: priceListRules,
569
- prices: pricesData,
570
- };
415
+ entry.prices = pricesData;
416
+ }
417
+ if (priceListData.rules) {
418
+ const cleanRules = priceListData.rules
419
+ ? (0, utils_1.removeNullish)(priceListData.rules)
420
+ : {};
421
+ const rules = Object.entries(cleanRules);
422
+ const numberOfRules = rules.length;
423
+ const rulesDataMap = new Map();
424
+ rules.map(([attribute, value]) => {
425
+ const rule = {
426
+ attribute,
427
+ value,
428
+ };
429
+ rulesDataMap.set(JSON.stringify(rule), rule);
430
+ });
431
+ entry.price_list_rules = Array.from(rulesDataMap.values());
432
+ entry.rules_count = numberOfRules;
433
+ }
434
+ return entry;
571
435
  });
572
436
  const priceLists = await this.priceListService_.create(priceListsToCreate, sharedContext);
573
437
  /**
@@ -621,96 +485,52 @@ class PricingModuleService extends utils_1.ModulesSdkUtils.abstractModuleService
621
485
  return priceLists;
622
486
  }
623
487
  async updatePriceLists_(data, sharedContext = {}) {
624
- const updatedPriceLists = [];
625
- const ruleAttributes = [];
626
- const priceListIds = [];
627
- for (const priceListData of data) {
628
- if (typeof priceListData.rules === "object") {
629
- ruleAttributes.push(...Object.keys(priceListData.rules));
630
- priceListIds.push(priceListData.id);
631
- }
632
- }
633
- const existingPriceLists = await this.listPriceLists({ id: priceListIds }, { relations: ["price_list_rules"] }, sharedContext);
634
- const priceListRuleIds = existingPriceLists
635
- .map((pl) => (pl.price_list_rules || []).map((plr) => plr.id))
636
- .flat();
637
- const existingPriceListRules = await this.listPriceListRules({ id: priceListRuleIds }, {}, sharedContext);
638
- if (existingPriceListRules.length) {
639
- await this.deletePriceListRules(existingPriceListRules.map((plr) => plr.id), sharedContext);
488
+ const existingPriceLists = await this.priceListService_.list({ id: data.map((d) => d.id) }, {}, sharedContext);
489
+ if (existingPriceLists.length !== data.length) {
490
+ const diff = (0, utils_1.arrayDifference)(data.map((d) => d.id), existingPriceLists.map((p) => p.id));
491
+ throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `Price lists with ids: '${diff.join(", ")}' not found`);
640
492
  }
641
- const ruleTypes = await this.listRuleTypes({ rule_attribute: ruleAttributes }, { take: null }, sharedContext);
642
- const ruleTypeMap = new Map(ruleTypes.map((rt) => [rt.rule_attribute, rt]));
643
- for (const priceListData of data) {
644
- const { rules, ...priceListOnlyData } = priceListData;
645
- const updatePriceListData = {
646
- ...priceListOnlyData,
493
+ const normalizedData = this.normalizePriceListDate(data).map((priceList) => {
494
+ const entry = {
495
+ ...priceList,
496
+ rules: undefined,
497
+ price_list_rules: undefined,
647
498
  };
648
- (0, _utils_1.validatePriceListDates)(updatePriceListData);
649
- if (typeof rules === "object") {
650
- updatePriceListData.rules_count = Object.keys(rules).length;
651
- }
652
- const [updatedPriceList] = (await this.priceListService_.update([updatePriceListData], sharedContext));
653
- updatedPriceLists.push(updatedPriceList);
654
- for (const [ruleAttribute, ruleValues = []] of Object.entries(rules || {})) {
655
- let ruleType = ruleTypeMap.get(ruleAttribute);
656
- if (!ruleType) {
657
- ;
658
- [ruleType] = await this.createRuleTypes([{ name: ruleAttribute, rule_attribute: ruleAttribute }], sharedContext);
659
- ruleTypeMap.set(ruleAttribute, ruleType);
660
- }
661
- const [priceListRule] = await this.priceListRuleService_.create([
662
- {
663
- price_list_id: updatedPriceList.id,
664
- rule_type_id: ruleType?.id,
665
- },
666
- ], sharedContext);
667
- for (const ruleValue of ruleValues) {
668
- await this.priceListRuleValueService_.create([{ price_list_rule_id: priceListRule.id, value: ruleValue }], sharedContext);
669
- }
499
+ if (typeof priceList.rules === "object") {
500
+ const cleanRules = priceList.rules
501
+ ? (0, utils_1.removeNullish)(priceList.rules)
502
+ : {};
503
+ const rules = Object.entries(cleanRules);
504
+ const numberOfRules = rules.length;
505
+ const rulesDataMap = new Map();
506
+ rules.map(([attribute, value]) => {
507
+ const rule = {
508
+ attribute,
509
+ value,
510
+ };
511
+ rulesDataMap.set(JSON.stringify(rule), rule);
512
+ });
513
+ entry.price_list_rules = Array.from(rulesDataMap.values());
514
+ entry.rules_count = numberOfRules;
670
515
  }
671
- }
672
- return updatedPriceLists;
516
+ return entry;
517
+ });
518
+ const { entities } = await this.priceListService_.upsertWithReplace(normalizedData, {
519
+ relations: ["price_list_rules"],
520
+ });
521
+ return entities;
673
522
  }
674
523
  async updatePriceListPrices_(data, sharedContext = {}) {
675
- const ruleTypeAttributes = [];
676
524
  const priceListIds = [];
677
525
  const priceIds = [];
678
- const priceSetIds = data
679
- .map((d) => d.prices.map((price) => price.price_set_id))
680
- .flat();
681
526
  for (const priceListData of data) {
682
527
  priceListIds.push(priceListData.price_list_id);
683
528
  for (const price of priceListData.prices) {
684
529
  priceIds.push(price.id);
685
- ruleTypeAttributes.push(...Object.keys(price.rules || {}));
686
530
  }
687
531
  }
688
532
  const prices = await this.listPrices({ id: priceIds }, { take: null, relations: ["price_rules"] }, sharedContext);
689
533
  const priceMap = new Map(prices.map((price) => [price.id, price]));
690
- const ruleTypes = await this.listRuleTypes({ rule_attribute: ruleTypeAttributes }, { take: null }, sharedContext);
691
- const ruleTypeMap = new Map(ruleTypes.map((rt) => [rt.rule_attribute, rt]));
692
- const priceSets = await this.list({ id: priceSetIds }, { relations: ["rule_types"] }, sharedContext);
693
- const priceSetRuleTypeMap = priceSets.reduce((acc, curr) => {
694
- const priceSetRuleAttributeSet = acc.get(curr.id) || new Set();
695
- for (const rt of curr.rule_types ?? []) {
696
- priceSetRuleAttributeSet.add(rt.rule_attribute);
697
- }
698
- acc.set(curr.id, priceSetRuleAttributeSet);
699
- return acc;
700
- }, new Map());
701
- const ruleTypeErrors = [];
702
- for (const priceListData of data) {
703
- for (const price of priceListData.prices) {
704
- for (const ruleAttribute of Object.keys(price.rules ?? {})) {
705
- if (!priceSetRuleTypeMap.get(price.price_set_id)?.has(ruleAttribute)) {
706
- ruleTypeErrors.push(`rule_attribute "${ruleAttribute}" in price set ${price.price_set_id}`);
707
- }
708
- }
709
- }
710
- }
711
- if (ruleTypeErrors.length) {
712
- throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `Invalid rule type configuration: Price set rules doesn't exist for ${ruleTypeErrors.join(", ")}`);
713
- }
714
534
  const priceLists = await this.listPriceLists({ id: priceListIds }, { take: null }, sharedContext);
715
535
  const priceListMap = new Map(priceLists.map((p) => [p.id, p]));
716
536
  const pricesToUpdate = [];
@@ -727,7 +547,7 @@ class PricingModuleService extends utils_1.ModulesSdkUtils.abstractModuleService
727
547
  const priceRules = price.price_rules;
728
548
  priceRulesToCreate.push(...Object.entries(rules).map(([ruleAttribute, ruleValue]) => ({
729
549
  price_set_id,
730
- rule_type_id: ruleTypeMap.get(ruleAttribute).id,
550
+ attribute: ruleAttribute,
731
551
  value: ruleValue,
732
552
  price_id: price.id,
733
553
  })));
@@ -749,40 +569,10 @@ class PricingModuleService extends utils_1.ModulesSdkUtils.abstractModuleService
749
569
  await this.priceService_.delete(ids, sharedContext);
750
570
  }
751
571
  async addPriceListPrices_(data, sharedContext = {}) {
752
- const ruleTypeAttributes = [];
753
572
  const priceListIds = [];
754
- const priceSetIds = [];
755
573
  for (const priceListData of data) {
756
574
  priceListIds.push(priceListData.price_list_id);
757
- for (const price of priceListData.prices) {
758
- ruleTypeAttributes.push(...Object.keys(price.rules || {}));
759
- priceSetIds.push(price.price_set_id);
760
- }
761
575
  }
762
- const ruleTypes = await this.listRuleTypes({ rule_attribute: ruleTypeAttributes }, { take: null }, sharedContext);
763
- const priceSets = await this.list({ id: priceSetIds }, { relations: ["rule_types"] }, sharedContext);
764
- const priceSetRuleTypeMap = priceSets.reduce((acc, curr) => {
765
- const priceSetRuleAttributeSet = acc.get(curr.id) || new Set();
766
- for (const rt of curr.rule_types ?? []) {
767
- priceSetRuleAttributeSet.add(rt.rule_attribute);
768
- }
769
- acc.set(curr.id, priceSetRuleAttributeSet);
770
- return acc;
771
- }, new Map());
772
- const ruleTypeErrors = [];
773
- for (const priceListData of data) {
774
- for (const price of priceListData.prices) {
775
- for (const rule_attribute of Object.keys(price.rules ?? {})) {
776
- if (!priceSetRuleTypeMap.get(price.price_set_id)?.has(rule_attribute)) {
777
- ruleTypeErrors.push(`rule_attribute "${rule_attribute}" in price set ${price.price_set_id}`);
778
- }
779
- }
780
- }
781
- }
782
- if (ruleTypeErrors.length) {
783
- throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `Invalid rule type configuration: Price set rules doesn't exist for ${ruleTypeErrors.join(", ")}`);
784
- }
785
- const ruleTypeMap = new Map(ruleTypes.map((rt) => [rt.rule_attribute, rt]));
786
576
  const priceLists = await this.listPriceLists({ id: priceListIds }, {}, sharedContext);
787
577
  const priceListMap = new Map(priceLists.map((p) => [p.id, p]));
788
578
  const pricesToCreate = [];
@@ -796,8 +586,8 @@ class PricingModuleService extends utils_1.ModulesSdkUtils.abstractModuleService
796
586
  const noOfRules = Object.keys(priceRules).length;
797
587
  const priceRulesToCreate = Object.entries(priceRules).map(([ruleAttribute, ruleValue]) => {
798
588
  return {
799
- price_set_id: priceData.price_set_id,
800
- rule_type_id: ruleTypeMap.get(ruleAttribute)?.id,
589
+ price_list_id: priceData.price_set_id,
590
+ attribute: ruleAttribute,
801
591
  value: ruleValue,
802
592
  };
803
593
  });
@@ -839,102 +629,116 @@ class PricingModuleService extends utils_1.ModulesSdkUtils.abstractModuleService
839
629
  }
840
630
  async setPriceListRules_(data, sharedContext = {}) {
841
631
  // TODO: re think this method
842
- const priceLists = await this.priceListService_.list({ id: data.map((d) => d.price_list_id) }, { relations: ["price_list_rules", "price_list_rules.rule_type"] }, sharedContext);
843
- const priceListMap = new Map(priceLists.map((p) => [p.id, p]));
844
- const ruleTypes = await this.listRuleTypes({ rule_attribute: data.map((d) => Object.keys(d.rules)).flat() }, { take: null });
845
- const ruleTypeMap = new Map(ruleTypes.map((rt) => [rt.rule_attribute, rt]));
846
- const ruleIdsToUpdate = [];
847
- const rulesToCreate = [];
848
- const priceRuleValues = new Map();
849
- for (const { price_list_id: priceListId, rules } of data) {
850
- const priceList = priceListMap.get(priceListId);
851
- if (!priceList) {
852
- throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `Price list with id: ${priceListId} not found`);
632
+ const priceLists = await this.priceListService_.list({ id: data.map((d) => d.price_list_id) }, {
633
+ relations: ["price_list_rules"],
634
+ }, sharedContext);
635
+ const rulesMap = new Map();
636
+ data.forEach((rule) => {
637
+ if (!rulesMap.has(rule.price_list_id)) {
638
+ rulesMap.set(rule.price_list_id, []);
853
639
  }
854
- const priceListRulesMap = new Map(priceList.price_list_rules.map((p) => [p.rule_type.rule_attribute, p]));
855
- const priceListRuleValues = new Map();
856
- Object.entries(rules).map(async ([key, value]) => {
857
- const ruleType = ruleTypeMap.get(key);
858
- if (!ruleType) {
859
- throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `Rule type with attribute: ${key} not found`);
860
- }
861
- const rule = priceListRulesMap.get(key);
862
- priceListRuleValues.set(ruleType.id, Array.isArray(value) ? value : [value]);
863
- if (!rule) {
864
- rulesToCreate.push({
865
- rule_type_id: ruleType.id,
866
- price_list_id: priceListId,
867
- });
868
- }
869
- else {
870
- ruleIdsToUpdate.push(rule.id);
871
- }
640
+ Object.entries(rule.rules).forEach(([key, value]) => {
641
+ rulesMap.get(rule.price_list_id).push([key, value]);
872
642
  });
873
- priceRuleValues.set(priceListId, priceListRuleValues);
874
- }
875
- const [createdRules, priceListValuesToDelete] = await (0, utils_1.promiseAll)([
876
- this.priceListRuleService_.create(rulesToCreate),
877
- this.priceListRuleValueService_.list({ price_list_rule_id: ruleIdsToUpdate }, { take: null }),
878
- ]);
879
- const priceListRuleValuesToCreate = [];
880
- for (const { id, price_list_id, rule_type_id } of createdRules) {
881
- const ruleValues = priceRuleValues.get(price_list_id);
882
- if (!ruleValues) {
883
- continue;
884
- }
885
- const values = ruleValues.get(rule_type_id);
886
- if (!values) {
887
- continue;
643
+ });
644
+ const priceListsUpsert = priceLists
645
+ .map((priceList) => {
646
+ const allRules = new Map(priceList.price_list_rules
647
+ .toArray()
648
+ .map((r) => [r.attribute, r.value]));
649
+ const rules = rulesMap.get(priceList.id);
650
+ if (!rules?.length) {
651
+ return;
888
652
  }
889
- values.forEach((v) => {
890
- priceListRuleValuesToCreate.push({
891
- price_list_rule_id: id,
892
- value: v,
893
- });
653
+ rules.forEach(([key, value]) => {
654
+ allRules.set(key, value);
894
655
  });
895
- }
896
- await (0, utils_1.promiseAll)([
897
- this.priceListRuleValueService_.delete(priceListValuesToDelete.map((p) => p.id), sharedContext),
898
- this.priceListRuleValueService_.create(priceListRuleValuesToCreate, sharedContext),
899
- ]);
900
- return priceLists;
656
+ return {
657
+ ...priceList,
658
+ rules_count: allRules.size,
659
+ price_list_rules: Array.from(allRules).map(([attribute, value]) => ({
660
+ attribute,
661
+ value,
662
+ })),
663
+ };
664
+ })
665
+ .filter(Boolean);
666
+ const { entities } = await this.priceListService_.upsertWithReplace(priceListsUpsert, { relations: ["price_list_rules"] }, sharedContext);
667
+ return entities;
901
668
  }
902
669
  async removePriceListRules_(data, sharedContext = {}) {
903
- const priceLists = await this.priceListService_.list({ id: data.map((d) => d.price_list_id) }, { relations: ["price_list_rules", "price_list_rules.rule_type"] }, sharedContext);
904
- const priceListMap = new Map(priceLists.map((p) => [p.id, p]));
905
- const idsToDelete = [];
906
- for (const { price_list_id: priceListId, rules } of data) {
907
- const priceList = priceListMap.get(priceListId);
908
- if (!priceList) {
909
- throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `Price list with id: ${priceListId} not found`);
670
+ // TODO: re think this method
671
+ const priceLists = await this.priceListService_.list({ id: data.map((d) => d.price_list_id) }, {
672
+ relations: ["price_list_rules"],
673
+ }, sharedContext);
674
+ const rulesMap = new Map();
675
+ data.forEach((rule) => {
676
+ if (!rulesMap.has(rule.price_list_id)) {
677
+ rulesMap.set(rule.price_list_id, []);
910
678
  }
911
- const priceListRulesMap = new Map(priceList.price_list_rules.map((p) => [p.rule_type.rule_attribute, p]));
912
- rules.map(async (rule_attribute) => {
913
- const rule = priceListRulesMap.get(rule_attribute);
914
- if (rule) {
915
- idsToDelete.push(rule.id);
916
- }
679
+ rule.rules.forEach((key) => {
680
+ rulesMap.get(rule.price_list_id).push([key, undefined]);
917
681
  });
918
- }
919
- await this.priceListRuleService_.delete(idsToDelete);
920
- return priceLists;
682
+ });
683
+ const priceListsUpsert = priceLists
684
+ .map((priceList) => {
685
+ const allRules = new Map(priceList.price_list_rules
686
+ .toArray()
687
+ .map((r) => [r.attribute, r.value]));
688
+ const rules = rulesMap.get(priceList.id);
689
+ if (!rules?.length) {
690
+ return;
691
+ }
692
+ rules.forEach(([key, value]) => {
693
+ allRules.set(key, value);
694
+ });
695
+ return {
696
+ ...priceList,
697
+ rules_count: allRules.size,
698
+ price_list_rules: Array.from(allRules)
699
+ .map(([attribute, value]) => ({
700
+ attribute,
701
+ value,
702
+ }))
703
+ .filter((r) => !!r.value),
704
+ };
705
+ })
706
+ .filter(Boolean);
707
+ const { entities } = await this.priceListService_.upsertWithReplace(priceListsUpsert, { relations: ["price_list_rules"] }, sharedContext);
708
+ return entities;
709
+ }
710
+ normalizePriceListDate(data) {
711
+ return data.map((priceListData) => {
712
+ (0, _utils_1.validatePriceListDates)(priceListData);
713
+ if (!!priceListData.starts_at) {
714
+ priceListData.starts_at = (0, utils_1.GetIsoStringFromDate)(priceListData.starts_at);
715
+ }
716
+ if (!!priceListData.ends_at) {
717
+ priceListData.ends_at = (0, utils_1.GetIsoStringFromDate)(priceListData.ends_at);
718
+ }
719
+ return priceListData;
720
+ });
921
721
  }
922
722
  }
923
723
  exports.default = PricingModuleService;
924
724
  __decorate([
925
- (0, utils_1.InjectManager)("baseRepository_"),
725
+ (0, utils_1.InjectManager)("baseRepository_")
726
+ // @ts-expect-error
727
+ ,
926
728
  __param(2, (0, utils_1.MedusaContext)()),
927
729
  __metadata("design:type", Function),
928
730
  __metadata("design:paramtypes", [Object, Object, Object]),
929
731
  __metadata("design:returntype", Promise)
930
- ], PricingModuleService.prototype, "list", null);
732
+ ], PricingModuleService.prototype, "listPriceSets", null);
931
733
  __decorate([
932
- (0, utils_1.InjectManager)("baseRepository_"),
734
+ (0, utils_1.InjectManager)("baseRepository_")
735
+ // @ts-expect-error
736
+ ,
933
737
  __param(2, (0, utils_1.MedusaContext)()),
934
738
  __metadata("design:type", Function),
935
739
  __metadata("design:paramtypes", [Object, Object, Object]),
936
740
  __metadata("design:returntype", Promise)
937
- ], PricingModuleService.prototype, "listAndCount", null);
741
+ ], PricingModuleService.prototype, "listAndCountPriceSets", null);
938
742
  __decorate([
939
743
  (0, utils_1.InjectManager)("baseRepository_"),
940
744
  __param(2, (0, utils_1.MedusaContext)()),
@@ -949,35 +753,28 @@ __decorate([
949
753
  __metadata("design:type", Function),
950
754
  __metadata("design:paramtypes", [Object, Object]),
951
755
  __metadata("design:returntype", Promise)
952
- ], PricingModuleService.prototype, "create", null);
756
+ ], PricingModuleService.prototype, "createPriceSets", null);
953
757
  __decorate([
954
758
  (0, utils_1.InjectManager)("baseRepository_"),
955
759
  __param(1, (0, utils_1.MedusaContext)()),
956
760
  __metadata("design:type", Function),
957
761
  __metadata("design:paramtypes", [Object, Object]),
958
762
  __metadata("design:returntype", Promise)
959
- ], PricingModuleService.prototype, "upsert", null);
763
+ ], PricingModuleService.prototype, "upsertPriceSets", null);
960
764
  __decorate([
961
765
  (0, utils_1.InjectManager)("baseRepository_"),
962
766
  __param(2, (0, utils_1.MedusaContext)()),
963
767
  __metadata("design:type", Function),
964
768
  __metadata("design:paramtypes", [Object, Object, Object]),
965
769
  __metadata("design:returntype", Promise)
966
- ], PricingModuleService.prototype, "update", null);
770
+ ], PricingModuleService.prototype, "updatePriceSets", null);
967
771
  __decorate([
968
772
  (0, utils_1.InjectTransactionManager)("baseRepository_"),
969
773
  __param(1, (0, utils_1.MedusaContext)()),
970
774
  __metadata("design:type", Function),
971
775
  __metadata("design:paramtypes", [Array, Object]),
972
776
  __metadata("design:returntype", Promise)
973
- ], PricingModuleService.prototype, "update_", null);
974
- __decorate([
975
- (0, utils_1.InjectManager)("baseRepository_"),
976
- __param(1, (0, utils_1.MedusaContext)()),
977
- __metadata("design:type", Function),
978
- __metadata("design:paramtypes", [Object, Object]),
979
- __metadata("design:returntype", Promise)
980
- ], PricingModuleService.prototype, "addRules", null);
777
+ ], PricingModuleService.prototype, "updatePriceSets_", null);
981
778
  __decorate([
982
779
  (0, utils_1.InjectManager)("baseRepository_"),
983
780
  (0, utils_1.EmitEvents)(),
@@ -986,23 +783,20 @@ __decorate([
986
783
  __metadata("design:paramtypes", [Object, Object]),
987
784
  __metadata("design:returntype", Promise)
988
785
  ], PricingModuleService.prototype, "addPrices", null);
989
- __decorate([
990
- (0, utils_1.InjectTransactionManager)("baseRepository_"),
991
- __param(1, (0, utils_1.MedusaContext)()),
992
- __metadata("design:type", Function),
993
- __metadata("design:paramtypes", [Array, Object]),
994
- __metadata("design:returntype", Promise)
995
- ], PricingModuleService.prototype, "removeRules", null);
996
786
  __decorate([
997
787
  (0, utils_1.InjectManager)("baseRepository_"),
998
- (0, utils_1.EmitEvents)(),
788
+ (0, utils_1.EmitEvents)()
789
+ // @ts-ignore
790
+ ,
999
791
  __param(1, (0, utils_1.MedusaContext)()),
1000
792
  __metadata("design:type", Function),
1001
793
  __metadata("design:paramtypes", [Array, Object]),
1002
794
  __metadata("design:returntype", Promise)
1003
795
  ], PricingModuleService.prototype, "createPriceLists", null);
1004
796
  __decorate([
1005
- (0, utils_1.InjectTransactionManager)("baseRepository_"),
797
+ (0, utils_1.InjectTransactionManager)("baseRepository_")
798
+ // @ts-ignore
799
+ ,
1006
800
  __param(1, (0, utils_1.MedusaContext)()),
1007
801
  __metadata("design:type", Function),
1008
802
  __metadata("design:paramtypes", [Array, Object]),
@@ -1050,14 +844,7 @@ __decorate([
1050
844
  __metadata("design:type", Function),
1051
845
  __metadata("design:paramtypes", [Array, Object]),
1052
846
  __metadata("design:returntype", Promise)
1053
- ], PricingModuleService.prototype, "create_", null);
1054
- __decorate([
1055
- (0, utils_1.InjectTransactionManager)("baseRepository_"),
1056
- __param(1, (0, utils_1.MedusaContext)()),
1057
- __metadata("design:type", Function),
1058
- __metadata("design:paramtypes", [Array, Object]),
1059
- __metadata("design:returntype", Promise)
1060
- ], PricingModuleService.prototype, "addRules_", null);
847
+ ], PricingModuleService.prototype, "createPriceSets_", null);
1061
848
  __decorate([
1062
849
  (0, utils_1.InjectTransactionManager)("baseRepository_"),
1063
850
  __param(1, (0, utils_1.MedusaContext)()),