@medusajs/pricing 0.1.13-snapshot-20240531102054 → 0.1.13-snapshot-20240701122250
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/index.d.ts +2 -3
- package/dist/index.js +7 -4
- package/dist/joiner-config.d.ts +1 -8
- package/dist/joiner-config.js +10 -52
- package/dist/migrations/Migration20240626133555.d.ts +4 -0
- package/dist/migrations/Migration20240626133555.js +33 -0
- package/dist/models/index.d.ts +0 -3
- package/dist/models/index.js +1 -7
- package/dist/models/price-list-rule.d.ts +4 -7
- package/dist/models/price-list-rule.js +6 -27
- package/dist/models/price-list.d.ts +0 -2
- package/dist/models/price-list.js +0 -9
- package/dist/models/price-rule.d.ts +3 -8
- package/dist/models/price-rule.js +4 -43
- package/dist/models/price-set.d.ts +0 -4
- package/dist/models/price-set.js +0 -19
- package/dist/models/price.d.ts +3 -3
- package/dist/models/price.js +1 -1
- package/dist/repositories/pricing.js +25 -30
- package/dist/services/index.d.ts +0 -2
- package/dist/services/index.js +1 -5
- package/dist/services/pricing-module.d.ts +38 -52
- package/dist/services/pricing-module.js +405 -509
- package/dist/types/services/price-list.d.ts +9 -9
- package/dist/utils/events.d.ts +38 -0
- package/dist/utils/events.js +42 -0
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.js +1 -0
- package/package.json +7 -10
- package/dist/models/price-list-rule-value.d.ts +0 -17
- package/dist/models/price-list-rule-value.js +0 -102
- package/dist/models/price-set-rule-type.d.ts +0 -10
- package/dist/models/price-set-rule-type.js +0 -108
- package/dist/models/rule-type.d.ts +0 -17
- package/dist/models/rule-type.js +0 -100
- package/dist/module-definition.d.ts +0 -2
- package/dist/module-definition.js +0 -8
- package/dist/scripts/bin/run-seed.d.ts +0 -3
- package/dist/scripts/bin/run-seed.js +0 -38
- package/dist/scripts/seed.d.ts +0 -4
- package/dist/scripts/seed.js +0 -67
- package/dist/services/price-list.d.ts +0 -16
- package/dist/services/price-list.js +0 -34
- package/dist/services/rule-type.d.ts +0 -15
- 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
|
21
|
-
|
22
|
-
|
23
|
-
_models_1.
|
24
|
-
_models_1.
|
25
|
-
_models_1.
|
26
|
-
|
27
|
-
|
28
|
-
|
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
|
-
|
55
|
+
// @ts-expect-error
|
56
|
+
async listPriceSets(filters = {}, config = {}, sharedContext = {}) {
|
64
57
|
const pricingContext = this.setupCalculatedPriceConfig_(filters, config);
|
65
|
-
const priceSets = await super.
|
66
|
-
if (pricingContext
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
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
|
-
|
75
|
+
// @ts-expect-error
|
76
|
+
async listAndCountPriceSets(filters = {}, config = {}, sharedContext = {}) {
|
82
77
|
const pricingContext = this.setupCalculatedPriceConfig_(filters, config);
|
83
|
-
const [priceSets, count] = await super.
|
84
|
-
if (pricingContext
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
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,32 +135,34 @@ class PricingModuleService extends utils_1.ModulesSdkUtils.abstractModuleService
|
|
139
135
|
});
|
140
136
|
return JSON.parse(JSON.stringify(calculatedPrices));
|
141
137
|
}
|
142
|
-
async
|
138
|
+
async createPriceSets(data, sharedContext = {}) {
|
143
139
|
const input = Array.isArray(data) ? data : [data];
|
144
|
-
const priceSets = await this.
|
140
|
+
const priceSets = await this.createPriceSets_(input, sharedContext);
|
145
141
|
// TODO: Remove the need to refetch the data here
|
146
|
-
const dbPriceSets = await this.
|
142
|
+
const dbPriceSets = await this.listPriceSets({ id: priceSets.map((p) => p.id) }, {
|
143
|
+
relations: ["prices", "prices.price_rules"],
|
144
|
+
}, sharedContext);
|
147
145
|
// Ensure the output to be in the same order as the input
|
148
146
|
const results = priceSets.map((priceSet) => {
|
149
147
|
return dbPriceSets.find((p) => p.id === priceSet.id);
|
150
148
|
});
|
151
149
|
return await this.baseRepository_.serialize(Array.isArray(data) ? results : results[0]);
|
152
150
|
}
|
153
|
-
async
|
151
|
+
async upsertPriceSets(data, sharedContext = {}) {
|
154
152
|
const input = Array.isArray(data) ? data : [data];
|
155
153
|
const forUpdate = input.filter((priceSet) => !!priceSet.id);
|
156
154
|
const forCreate = input.filter((priceSet) => !priceSet.id);
|
157
155
|
const operations = [];
|
158
156
|
if (forCreate.length) {
|
159
|
-
operations.push(this.
|
157
|
+
operations.push(this.createPriceSets_(forCreate, sharedContext));
|
160
158
|
}
|
161
159
|
if (forUpdate.length) {
|
162
|
-
operations.push(this.
|
160
|
+
operations.push(this.updatePriceSets_(forUpdate, sharedContext));
|
163
161
|
}
|
164
162
|
const result = (await (0, utils_1.promiseAll)(operations)).flat();
|
165
163
|
return await this.baseRepository_.serialize(Array.isArray(data) ? result : result[0]);
|
166
164
|
}
|
167
|
-
async
|
165
|
+
async updatePriceSets(idOrSelector, data, sharedContext = {}) {
|
168
166
|
let normalizedInput = [];
|
169
167
|
if ((0, utils_1.isString)(idOrSelector)) {
|
170
168
|
// Check if the ID exists, it will throw if not.
|
@@ -178,26 +176,16 @@ class PricingModuleService extends utils_1.ModulesSdkUtils.abstractModuleService
|
|
178
176
|
...data,
|
179
177
|
}));
|
180
178
|
}
|
181
|
-
const updateResult = await this.
|
179
|
+
const updateResult = await this.updatePriceSets_(normalizedInput, sharedContext);
|
182
180
|
const priceSets = await this.baseRepository_.serialize(updateResult);
|
183
181
|
return (0, utils_1.isString)(idOrSelector) ? priceSets[0] : priceSets;
|
184
182
|
}
|
185
|
-
async normalizeUpdateData(data
|
186
|
-
const ruleAttributes = data
|
187
|
-
.map((d) => d.prices?.map((p) => Object.keys(p.rules ?? [])) ?? [])
|
188
|
-
.flat(Infinity)
|
189
|
-
.filter(Boolean);
|
190
|
-
const ruleTypes = await this.ruleTypeService_.list({ rule_attribute: ruleAttributes }, { take: null }, sharedContext);
|
191
|
-
const ruleTypeMap = ruleTypes.reduce((acc, curr) => {
|
192
|
-
acc.set(curr.rule_attribute, curr);
|
193
|
-
return acc;
|
194
|
-
}, new Map());
|
183
|
+
async normalizeUpdateData(data) {
|
195
184
|
return data.map((priceSet) => {
|
196
185
|
const prices = priceSet.prices?.map((price) => {
|
197
186
|
const rules = Object.entries(price.rules ?? {}).map(([attribute, value]) => {
|
198
187
|
return {
|
199
|
-
|
200
|
-
rule_type_id: ruleTypeMap.get(attribute).id,
|
188
|
+
attribute,
|
201
189
|
value,
|
202
190
|
};
|
203
191
|
});
|
@@ -216,15 +204,12 @@ class PricingModuleService extends utils_1.ModulesSdkUtils.abstractModuleService
|
|
216
204
|
};
|
217
205
|
});
|
218
206
|
}
|
219
|
-
async
|
220
|
-
// TODO: We are not handling rule types, rules, etc. here, add support after data models are finalized
|
207
|
+
async updatePriceSets_(data, sharedContext = {}) {
|
221
208
|
// TODO: Since money IDs are rarely passed, this will delete all previous data and insert new entries.
|
222
209
|
// We can make the `insert` inside upsertWithReplace do an `upsert` instead to avoid this
|
223
|
-
const normalizedData = await this.normalizeUpdateData(data
|
210
|
+
const normalizedData = await this.normalizeUpdateData(data);
|
224
211
|
const prices = normalizedData.flatMap((priceSet) => priceSet.prices || []);
|
225
|
-
const upsertedPrices = await this.priceService_.upsertWithReplace(prices, {
|
226
|
-
relations: ["price_rules"],
|
227
|
-
}, sharedContext);
|
212
|
+
const { entities: upsertedPrices } = await this.priceService_.upsertWithReplace(prices, { relations: ["price_rules"] }, sharedContext);
|
228
213
|
const priceSetsToUpsert = normalizedData.map((priceSet) => {
|
229
214
|
const { prices, ...rest } = priceSet;
|
230
215
|
return {
|
@@ -238,61 +223,28 @@ class PricingModuleService extends utils_1.ModulesSdkUtils.abstractModuleService
|
|
238
223
|
}),
|
239
224
|
};
|
240
225
|
});
|
241
|
-
|
242
|
-
|
243
|
-
async addRules(data, sharedContext = {}) {
|
244
|
-
const inputs = Array.isArray(data) ? data : [data];
|
245
|
-
const priceSets = await this.addRules_(inputs, sharedContext);
|
246
|
-
const dbPriceSets = await this.list({ id: priceSets.map(({ id }) => id) }, { relations: ["rule_types"] });
|
247
|
-
const orderedPriceSets = priceSets.map((priceSet) => {
|
248
|
-
return dbPriceSets.find((p) => p.id === priceSet.id);
|
249
|
-
});
|
250
|
-
return Array.isArray(data) ? orderedPriceSets : orderedPriceSets[0];
|
226
|
+
const { entities: priceSets } = await this.priceSetService_.upsertWithReplace(priceSetsToUpsert, { relations: ["prices"] }, sharedContext);
|
227
|
+
return priceSets;
|
251
228
|
}
|
252
229
|
async addPrices(data, sharedContext = {}) {
|
253
230
|
const input = Array.isArray(data) ? data : [data];
|
254
231
|
await this.addPrices_(input, sharedContext);
|
255
|
-
const dbPrices = await this.
|
232
|
+
const dbPrices = await this.listPriceSets({ id: input.map((d) => d.priceSetId) }, { relations: ["prices"] }, sharedContext);
|
256
233
|
const orderedPriceSets = input.map((inputItem) => {
|
257
234
|
return dbPrices.find((p) => p.id === inputItem.priceSetId);
|
258
235
|
});
|
259
236
|
return Array.isArray(data) ? orderedPriceSets : orderedPriceSets[0];
|
260
237
|
}
|
261
|
-
|
262
|
-
const priceSets = await this.priceSetService_.list({ id: data.map((d) => d.id) }, {}, sharedContext);
|
263
|
-
const priceSetIds = priceSets.map((ps) => ps.id);
|
264
|
-
const ruleTypes = await this.ruleTypeService_.list({
|
265
|
-
rule_attribute: data.map((d) => d.rules || []).flat(),
|
266
|
-
}, { take: null }, sharedContext);
|
267
|
-
const ruleTypeIds = ruleTypes.map((rt) => rt.id);
|
268
|
-
const priceSetRuleTypes = await this.priceSetRuleTypeService_.list({ price_set_id: priceSetIds, rule_type_id: ruleTypeIds }, { take: null }, sharedContext);
|
269
|
-
const priceRules = await this.priceRuleService_.list({ price_set_id: priceSetIds, rule_type_id: ruleTypeIds }, { select: ["price"], take: null }, sharedContext);
|
270
|
-
await this.priceSetRuleTypeService_.delete(priceSetRuleTypes.map((psrt) => psrt.id), sharedContext);
|
271
|
-
await this.priceService_.delete(priceRules.map((pr) => pr.price.id), sharedContext);
|
272
|
-
}
|
238
|
+
// @ts-ignore
|
273
239
|
async createPriceLists(data, sharedContext = {}) {
|
274
240
|
const priceLists = await this.createPriceLists_(data, sharedContext);
|
275
241
|
return await this.baseRepository_.serialize(priceLists);
|
276
242
|
}
|
243
|
+
// @ts-ignore
|
277
244
|
async updatePriceLists(data, sharedContext = {}) {
|
278
245
|
const priceLists = await this.updatePriceLists_(data, sharedContext);
|
279
246
|
return await this.baseRepository_.serialize(priceLists);
|
280
247
|
}
|
281
|
-
async createPriceListRules(data, sharedContext = {}) {
|
282
|
-
const priceLists = await this.createPriceListRules_(data, sharedContext);
|
283
|
-
return await this.baseRepository_.serialize(priceLists, {
|
284
|
-
populate: true,
|
285
|
-
});
|
286
|
-
}
|
287
|
-
async createPriceListRules_(data, sharedContext = {}) {
|
288
|
-
return await this.priceListRuleService_.create(data, sharedContext);
|
289
|
-
}
|
290
|
-
async updatePriceListRules(data, sharedContext = {}) {
|
291
|
-
const priceLists = await this.priceListRuleService_.update(data, sharedContext);
|
292
|
-
return await this.baseRepository_.serialize(priceLists, {
|
293
|
-
populate: true,
|
294
|
-
});
|
295
|
-
}
|
296
248
|
async updatePriceListPrices(data, sharedContext = {}) {
|
297
249
|
const prices = await this.updatePriceListPrices_(data, sharedContext);
|
298
250
|
return await this.baseRepository_.serialize(prices);
|
@@ -312,302 +264,273 @@ class PricingModuleService extends utils_1.ModulesSdkUtils.abstractModuleService
|
|
312
264
|
const [priceList] = await this.removePriceListRules_([data], sharedContext);
|
313
265
|
return await this.baseRepository_.serialize(priceList);
|
314
266
|
}
|
315
|
-
async
|
267
|
+
async createPriceSets_(data, sharedContext = {}) {
|
316
268
|
const input = Array.isArray(data) ? data : [data];
|
317
|
-
const ruleAttributes = (0, utils_1.deduplicate)(data.map((d) => d.rules?.map((r) => r.rule_attribute) ?? []).flat());
|
318
|
-
const ruleTypes = await this.ruleTypeService_.list({ rule_attribute: ruleAttributes }, { take: null }, sharedContext);
|
319
|
-
const ruleTypeMap = ruleTypes.reduce((acc, curr) => {
|
320
|
-
acc.set(curr.rule_attribute, curr);
|
321
|
-
return acc;
|
322
|
-
}, new Map());
|
323
|
-
const invalidRuleAttributes = ruleAttributes.filter((r) => !ruleTypeMap.has(r));
|
324
|
-
if (invalidRuleAttributes.length > 0) {
|
325
|
-
throw new utils_1.MedusaError(utils_1.MedusaError.Types.NOT_FOUND, `Rule types don't exist for: ${invalidRuleAttributes.join(", ")}`);
|
326
|
-
}
|
327
|
-
const invalidMoneyAmountRule = data
|
328
|
-
.map((d) => d.prices?.map((ma) => Object.keys(ma?.rules ?? {})).flat() ?? [])
|
329
|
-
.flat()
|
330
|
-
.filter((r) => !ruleTypeMap.has(r));
|
331
|
-
if (invalidMoneyAmountRule.length > 0) {
|
332
|
-
throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `Rule types don't exist for money amounts with rule attribute: ${invalidMoneyAmountRule.join(", ")}`);
|
333
|
-
}
|
334
|
-
const ruleSetRuleTypeToCreateMap = new Map();
|
335
269
|
const toCreate = input.map((inputData) => {
|
336
|
-
const
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
};
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
const
|
350
|
-
|
351
|
-
|
352
|
-
Object.entries(priceRules).map(([attribute, value]) => {
|
353
|
-
const rule = {
|
354
|
-
price_set_id: id,
|
355
|
-
rule_type_id: ruleTypeMap.get(attribute).id,
|
356
|
-
value,
|
357
|
-
};
|
358
|
-
rulesDataMap.set(JSON.stringify(rule), rule);
|
359
|
-
const priceSetRuleType = {
|
360
|
-
rule_type_id: ruleTypeMap.get(attribute).id,
|
361
|
-
price_set_id: id,
|
362
|
-
};
|
363
|
-
ruleSetRuleTypeToCreateMap.set(JSON.stringify(priceSetRuleType), priceSetRuleType);
|
364
|
-
});
|
365
|
-
return {
|
366
|
-
...rest,
|
367
|
-
title: "", // TODO: accept title
|
368
|
-
rules_count: numberOfRules,
|
369
|
-
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,
|
370
286
|
};
|
287
|
+
rulesDataMap.set(JSON.stringify(rule), rule);
|
371
288
|
});
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
};
|
289
|
+
return {
|
290
|
+
...rest,
|
291
|
+
rules_count: numberOfRules,
|
292
|
+
price_rules: Array.from(rulesDataMap.values()),
|
293
|
+
};
|
294
|
+
});
|
295
|
+
entry.prices = pricesData;
|
296
|
+
return entry;
|
378
297
|
});
|
379
298
|
// Bulk create price sets
|
380
299
|
const createdPriceSets = await this.priceSetService_.create(toCreate, sharedContext);
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
const priceSetRuleTypeMap = new Map(priceSets.map((priceSet) => [
|
389
|
-
priceSet.id,
|
390
|
-
new Map([...priceSet.rule_types].map((rt) => [rt.rule_attribute, rt])),
|
391
|
-
]));
|
392
|
-
const priceSetMap = new Map(priceSets.map((p) => [p.id, p]));
|
393
|
-
const invalidPriceSetInputs = inputs.filter((d) => !priceSetMap.has(d.priceSetId));
|
394
|
-
if (invalidPriceSetInputs.length) {
|
395
|
-
throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `PriceSets with ids: ${invalidPriceSetInputs
|
396
|
-
.map((d) => d.priceSetId)
|
397
|
-
.join(", ")} was not found`);
|
398
|
-
}
|
399
|
-
const ruleTypes = await this.ruleTypeService_.list({
|
400
|
-
rule_attribute: inputs
|
401
|
-
.map((data) => data.rules.map((r) => r.attribute))
|
402
|
-
.flat(),
|
403
|
-
}, { take: null }, sharedContext);
|
404
|
-
const ruleTypeMap = new Map(ruleTypes.map((rt) => [rt.rule_attribute, rt]));
|
405
|
-
const invalidRuleAttributeInputs = inputs
|
406
|
-
.map((d) => d.rules.map((r) => r.attribute))
|
407
|
-
.flat()
|
408
|
-
.filter((r) => !ruleTypeMap.has(r));
|
409
|
-
if (invalidRuleAttributeInputs.length) {
|
410
|
-
throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `Rule types don't exist for attributes: ${[
|
411
|
-
...new Set(invalidRuleAttributeInputs),
|
412
|
-
].join(", ")}`);
|
413
|
-
}
|
414
|
-
const priceSetRuleTypesCreate = [];
|
415
|
-
inputs.forEach((data) => {
|
416
|
-
for (const rule of data.rules) {
|
417
|
-
if (priceSetRuleTypeMap.get(data.priceSetId).has(rule.attribute)) {
|
418
|
-
continue;
|
419
|
-
}
|
420
|
-
priceSetRuleTypesCreate.push({
|
421
|
-
rule_type_id: ruleTypeMap.get(rule.attribute).id,
|
422
|
-
price_set_id: priceSetMap.get(data.priceSetId).id,
|
300
|
+
const eventsData = createdPriceSets.reduce((eventsData, priceSet) => {
|
301
|
+
eventsData.priceSets.push({
|
302
|
+
id: priceSet.id,
|
303
|
+
});
|
304
|
+
priceSet.prices.map((price) => {
|
305
|
+
eventsData.prices.push({
|
306
|
+
id: price.id,
|
423
307
|
});
|
424
|
-
|
308
|
+
price.price_rules.map((priceRule) => {
|
309
|
+
eventsData.priceRules.push({
|
310
|
+
id: priceRule.id,
|
311
|
+
});
|
312
|
+
});
|
313
|
+
});
|
314
|
+
return eventsData;
|
315
|
+
}, {
|
316
|
+
priceSets: [],
|
317
|
+
priceRules: [],
|
318
|
+
prices: [],
|
425
319
|
});
|
426
|
-
|
427
|
-
|
320
|
+
_utils_1.eventBuilders.createdPriceSet({
|
321
|
+
data: eventsData.priceSets,
|
322
|
+
sharedContext,
|
323
|
+
});
|
324
|
+
_utils_1.eventBuilders.createdPrice({
|
325
|
+
data: eventsData.prices,
|
326
|
+
sharedContext,
|
327
|
+
});
|
328
|
+
_utils_1.eventBuilders.createdPriceRule({
|
329
|
+
data: eventsData.priceRules,
|
330
|
+
sharedContext,
|
331
|
+
});
|
332
|
+
return createdPriceSets;
|
428
333
|
}
|
429
334
|
async addPrices_(input, sharedContext = {}) {
|
430
|
-
const priceSets = await this.
|
335
|
+
const priceSets = await this.listPriceSets({ id: input.map((d) => d.priceSetId) }, {}, sharedContext);
|
431
336
|
const priceSetMap = new Map(priceSets.map((p) => [p.id, p]));
|
432
|
-
|
433
|
-
priceSet.id,
|
434
|
-
new Map(priceSet.rule_types?.map((rt) => [rt.rule_attribute, rt]) ?? []),
|
435
|
-
]));
|
436
|
-
input.forEach(({ priceSetId, prices }) => {
|
337
|
+
input.forEach(({ priceSetId }) => {
|
437
338
|
const priceSet = priceSetMap.get(priceSetId);
|
438
339
|
if (!priceSet) {
|
439
340
|
throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `Price set with id: ${priceSetId} not found`);
|
440
341
|
}
|
441
|
-
const ruleAttributes = prices
|
442
|
-
.map((d) => (d.rules ? Object.keys(d.rules) : []))
|
443
|
-
.flat();
|
444
|
-
const invalidRuleAttributes = ruleAttributes.filter((r) => !ruleTypeMap.get(priceSetId).has(r));
|
445
|
-
if (invalidRuleAttributes.length > 0) {
|
446
|
-
throw new utils_1.MedusaError(utils_1.MedusaError.Types.NOT_FOUND, `Rule types don't exist for: ${invalidRuleAttributes.join(", ")}`);
|
447
|
-
}
|
448
342
|
});
|
449
343
|
const pricesToCreate = input.flatMap(({ priceSetId, prices }) => prices.map((price) => {
|
450
344
|
const numberOfRules = Object.entries(price?.rules ?? {}).length;
|
451
345
|
const priceRules = Object.entries(price.rules ?? {}).map(([attribute, value]) => ({
|
452
|
-
rule_type_id: ruleTypeMap.get(priceSetId).get(attribute).id,
|
453
346
|
price_set_id: priceSetId,
|
347
|
+
attribute: attribute,
|
454
348
|
value,
|
455
349
|
}));
|
456
350
|
return {
|
457
351
|
...price,
|
458
352
|
price_set_id: priceSetId,
|
459
|
-
title: "test", // TODO: accept title
|
460
353
|
rules_count: numberOfRules,
|
461
|
-
priceRules,
|
354
|
+
price_rules: priceRules,
|
462
355
|
};
|
463
356
|
}));
|
464
|
-
await this.priceService_.create(pricesToCreate, sharedContext);
|
357
|
+
const prices = await this.priceService_.create(pricesToCreate, sharedContext);
|
358
|
+
/**
|
359
|
+
* Preparing data for emitting events
|
360
|
+
*/
|
361
|
+
const eventsData = prices.reduce((eventsData, price) => {
|
362
|
+
eventsData.prices.push({
|
363
|
+
id: price.id,
|
364
|
+
});
|
365
|
+
price.price_rules.map((priceRule) => {
|
366
|
+
eventsData.priceRules.push({
|
367
|
+
id: priceRule.id,
|
368
|
+
});
|
369
|
+
});
|
370
|
+
return eventsData;
|
371
|
+
}, {
|
372
|
+
priceRules: [],
|
373
|
+
prices: [],
|
374
|
+
});
|
375
|
+
/**
|
376
|
+
* Emitting events for all created entities
|
377
|
+
*/
|
378
|
+
_utils_1.eventBuilders.createdPrice({
|
379
|
+
data: eventsData.prices,
|
380
|
+
sharedContext,
|
381
|
+
});
|
382
|
+
_utils_1.eventBuilders.createdPriceRule({
|
383
|
+
data: eventsData.priceRules,
|
384
|
+
sharedContext,
|
385
|
+
});
|
386
|
+
return prices;
|
465
387
|
}
|
466
388
|
async createPriceLists_(data, sharedContext = {}) {
|
467
|
-
const
|
468
|
-
|
469
|
-
const
|
470
|
-
|
471
|
-
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
const ruleType = ruleTypeMap.get(attribute);
|
488
|
-
return {
|
489
|
-
price_list_id: id,
|
490
|
-
rule_type_id: ruleType.id,
|
491
|
-
price_list_rule_values: value.map((v) => ({ value: v })),
|
492
|
-
};
|
493
|
-
});
|
494
|
-
const pricesData = prices.map((price) => {
|
495
|
-
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
|
+
});
|
496
409
|
return {
|
497
|
-
|
498
|
-
|
499
|
-
|
410
|
+
...rest,
|
411
|
+
rules_count: numberOfRules,
|
412
|
+
price_rules: Array.from(rulesDataMap.values()),
|
500
413
|
};
|
501
414
|
});
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
|
508
|
-
|
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;
|
435
|
+
});
|
436
|
+
const priceLists = await this.priceListService_.create(priceListsToCreate, sharedContext);
|
437
|
+
/**
|
438
|
+
* Preparing data for emitting events
|
439
|
+
*/
|
440
|
+
const eventsData = priceLists.reduce((eventsData, priceList) => {
|
441
|
+
eventsData.priceList.push({
|
442
|
+
id: priceList.id,
|
509
443
|
});
|
510
|
-
|
511
|
-
|
512
|
-
|
513
|
-
|
514
|
-
|
515
|
-
|
516
|
-
|
444
|
+
priceList.price_list_rules.map((listRule) => {
|
445
|
+
eventsData.priceListRules.push({
|
446
|
+
id: listRule.id,
|
447
|
+
});
|
448
|
+
});
|
449
|
+
priceList.prices.map((price) => {
|
450
|
+
eventsData.prices.push({
|
451
|
+
id: price.id,
|
452
|
+
});
|
453
|
+
price.price_rules.map((priceRule) => {
|
454
|
+
eventsData.priceRules.push({
|
455
|
+
id: priceRule.id,
|
456
|
+
});
|
457
|
+
});
|
458
|
+
});
|
459
|
+
return eventsData;
|
460
|
+
}, {
|
461
|
+
priceList: [],
|
462
|
+
priceListRules: [],
|
463
|
+
priceRules: [],
|
464
|
+
prices: [],
|
465
|
+
});
|
466
|
+
/**
|
467
|
+
* Emitting events for all created entities
|
468
|
+
*/
|
469
|
+
_utils_1.eventBuilders.createdPriceList({
|
470
|
+
data: eventsData.priceList,
|
471
|
+
sharedContext,
|
472
|
+
});
|
473
|
+
_utils_1.eventBuilders.createdPriceListRule({
|
474
|
+
data: eventsData.priceListRules,
|
475
|
+
sharedContext,
|
517
476
|
});
|
518
|
-
|
477
|
+
_utils_1.eventBuilders.createdPrice({
|
478
|
+
data: eventsData.prices,
|
479
|
+
sharedContext,
|
480
|
+
});
|
481
|
+
_utils_1.eventBuilders.createdPriceRule({
|
482
|
+
data: eventsData.priceRules,
|
483
|
+
sharedContext,
|
484
|
+
});
|
485
|
+
return priceLists;
|
519
486
|
}
|
520
487
|
async updatePriceLists_(data, sharedContext = {}) {
|
521
|
-
const
|
522
|
-
|
523
|
-
|
524
|
-
|
525
|
-
if (typeof priceListData.rules === "object") {
|
526
|
-
ruleAttributes.push(...Object.keys(priceListData.rules));
|
527
|
-
priceListIds.push(priceListData.id);
|
528
|
-
}
|
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`);
|
529
492
|
}
|
530
|
-
const
|
531
|
-
|
532
|
-
|
533
|
-
|
534
|
-
|
535
|
-
if (existingPriceListRules.length) {
|
536
|
-
await this.deletePriceListRules(existingPriceListRules.map((plr) => plr.id), sharedContext);
|
537
|
-
}
|
538
|
-
const ruleTypes = await this.listRuleTypes({ rule_attribute: ruleAttributes }, { take: null }, sharedContext);
|
539
|
-
const ruleTypeMap = new Map(ruleTypes.map((rt) => [rt.rule_attribute, rt]));
|
540
|
-
for (const priceListData of data) {
|
541
|
-
const { rules, ...priceListOnlyData } = priceListData;
|
542
|
-
const updatePriceListData = {
|
543
|
-
...priceListOnlyData,
|
493
|
+
const normalizedData = this.normalizePriceListDate(data).map((priceList) => {
|
494
|
+
const entry = {
|
495
|
+
...priceList,
|
496
|
+
rules: undefined,
|
497
|
+
price_list_rules: undefined,
|
544
498
|
};
|
545
|
-
(
|
546
|
-
|
547
|
-
|
548
|
-
|
549
|
-
|
550
|
-
|
551
|
-
|
552
|
-
|
553
|
-
|
554
|
-
|
555
|
-
|
556
|
-
|
557
|
-
|
558
|
-
|
559
|
-
|
560
|
-
|
561
|
-
rule_type_id: ruleType?.id,
|
562
|
-
},
|
563
|
-
], sharedContext);
|
564
|
-
for (const ruleValue of ruleValues) {
|
565
|
-
await this.priceListRuleValueService_.create([{ price_list_rule_id: priceListRule.id, value: ruleValue }], sharedContext);
|
566
|
-
}
|
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;
|
567
515
|
}
|
568
|
-
|
569
|
-
|
516
|
+
return entry;
|
517
|
+
});
|
518
|
+
const { entities } = await this.priceListService_.upsertWithReplace(normalizedData, {
|
519
|
+
relations: ["price_list_rules"],
|
520
|
+
});
|
521
|
+
return entities;
|
570
522
|
}
|
571
523
|
async updatePriceListPrices_(data, sharedContext = {}) {
|
572
|
-
const ruleTypeAttributes = [];
|
573
524
|
const priceListIds = [];
|
574
525
|
const priceIds = [];
|
575
|
-
const priceSetIds = data
|
576
|
-
.map((d) => d.prices.map((price) => price.price_set_id))
|
577
|
-
.flat();
|
578
526
|
for (const priceListData of data) {
|
579
527
|
priceListIds.push(priceListData.price_list_id);
|
580
528
|
for (const price of priceListData.prices) {
|
581
529
|
priceIds.push(price.id);
|
582
|
-
ruleTypeAttributes.push(...Object.keys(price.rules || {}));
|
583
530
|
}
|
584
531
|
}
|
585
532
|
const prices = await this.listPrices({ id: priceIds }, { take: null, relations: ["price_rules"] }, sharedContext);
|
586
533
|
const priceMap = new Map(prices.map((price) => [price.id, price]));
|
587
|
-
const ruleTypes = await this.listRuleTypes({ rule_attribute: ruleTypeAttributes }, { take: null }, sharedContext);
|
588
|
-
const ruleTypeMap = new Map(ruleTypes.map((rt) => [rt.rule_attribute, rt]));
|
589
|
-
const priceSets = await this.list({ id: priceSetIds }, { relations: ["rule_types"] }, sharedContext);
|
590
|
-
const priceSetRuleTypeMap = priceSets.reduce((acc, curr) => {
|
591
|
-
const priceSetRuleAttributeSet = acc.get(curr.id) || new Set();
|
592
|
-
for (const rt of curr.rule_types ?? []) {
|
593
|
-
priceSetRuleAttributeSet.add(rt.rule_attribute);
|
594
|
-
}
|
595
|
-
acc.set(curr.id, priceSetRuleAttributeSet);
|
596
|
-
return acc;
|
597
|
-
}, new Map());
|
598
|
-
const ruleTypeErrors = [];
|
599
|
-
for (const priceListData of data) {
|
600
|
-
for (const price of priceListData.prices) {
|
601
|
-
for (const ruleAttribute of Object.keys(price.rules ?? {})) {
|
602
|
-
if (!priceSetRuleTypeMap.get(price.price_set_id)?.has(ruleAttribute)) {
|
603
|
-
ruleTypeErrors.push(`rule_attribute "${ruleAttribute}" in price set ${price.price_set_id}`);
|
604
|
-
}
|
605
|
-
}
|
606
|
-
}
|
607
|
-
}
|
608
|
-
if (ruleTypeErrors.length) {
|
609
|
-
throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `Invalid rule type configuration: Price set rules doesn't exist for ${ruleTypeErrors.join(", ")}`);
|
610
|
-
}
|
611
534
|
const priceLists = await this.listPriceLists({ id: priceListIds }, { take: null }, sharedContext);
|
612
535
|
const priceListMap = new Map(priceLists.map((p) => [p.id, p]));
|
613
536
|
const pricesToUpdate = [];
|
@@ -624,7 +547,7 @@ class PricingModuleService extends utils_1.ModulesSdkUtils.abstractModuleService
|
|
624
547
|
const priceRules = price.price_rules;
|
625
548
|
priceRulesToCreate.push(...Object.entries(rules).map(([ruleAttribute, ruleValue]) => ({
|
626
549
|
price_set_id,
|
627
|
-
|
550
|
+
attribute: ruleAttribute,
|
628
551
|
value: ruleValue,
|
629
552
|
price_id: price.id,
|
630
553
|
})));
|
@@ -646,40 +569,10 @@ class PricingModuleService extends utils_1.ModulesSdkUtils.abstractModuleService
|
|
646
569
|
await this.priceService_.delete(ids, sharedContext);
|
647
570
|
}
|
648
571
|
async addPriceListPrices_(data, sharedContext = {}) {
|
649
|
-
const ruleTypeAttributes = [];
|
650
572
|
const priceListIds = [];
|
651
|
-
const priceSetIds = [];
|
652
573
|
for (const priceListData of data) {
|
653
574
|
priceListIds.push(priceListData.price_list_id);
|
654
|
-
for (const price of priceListData.prices) {
|
655
|
-
ruleTypeAttributes.push(...Object.keys(price.rules || {}));
|
656
|
-
priceSetIds.push(price.price_set_id);
|
657
|
-
}
|
658
|
-
}
|
659
|
-
const ruleTypes = await this.listRuleTypes({ rule_attribute: ruleTypeAttributes }, { take: null }, sharedContext);
|
660
|
-
const priceSets = await this.list({ id: priceSetIds }, { relations: ["rule_types"] }, sharedContext);
|
661
|
-
const priceSetRuleTypeMap = priceSets.reduce((acc, curr) => {
|
662
|
-
const priceSetRuleAttributeSet = acc.get(curr.id) || new Set();
|
663
|
-
for (const rt of curr.rule_types ?? []) {
|
664
|
-
priceSetRuleAttributeSet.add(rt.rule_attribute);
|
665
|
-
}
|
666
|
-
acc.set(curr.id, priceSetRuleAttributeSet);
|
667
|
-
return acc;
|
668
|
-
}, new Map());
|
669
|
-
const ruleTypeErrors = [];
|
670
|
-
for (const priceListData of data) {
|
671
|
-
for (const price of priceListData.prices) {
|
672
|
-
for (const rule_attribute of Object.keys(price.rules ?? {})) {
|
673
|
-
if (!priceSetRuleTypeMap.get(price.price_set_id)?.has(rule_attribute)) {
|
674
|
-
ruleTypeErrors.push(`rule_attribute "${rule_attribute}" in price set ${price.price_set_id}`);
|
675
|
-
}
|
676
|
-
}
|
677
|
-
}
|
678
|
-
}
|
679
|
-
if (ruleTypeErrors.length) {
|
680
|
-
throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `Invalid rule type configuration: Price set rules doesn't exist for ${ruleTypeErrors.join(", ")}`);
|
681
575
|
}
|
682
|
-
const ruleTypeMap = new Map(ruleTypes.map((rt) => [rt.rule_attribute, rt]));
|
683
576
|
const priceLists = await this.listPriceLists({ id: priceListIds }, {}, sharedContext);
|
684
577
|
const priceListMap = new Map(priceLists.map((p) => [p.id, p]));
|
685
578
|
const pricesToCreate = [];
|
@@ -693,8 +586,8 @@ class PricingModuleService extends utils_1.ModulesSdkUtils.abstractModuleService
|
|
693
586
|
const noOfRules = Object.keys(priceRules).length;
|
694
587
|
const priceRulesToCreate = Object.entries(priceRules).map(([ruleAttribute, ruleValue]) => {
|
695
588
|
return {
|
696
|
-
|
697
|
-
|
589
|
+
price_list_id: priceData.price_set_id,
|
590
|
+
attribute: ruleAttribute,
|
698
591
|
value: ruleValue,
|
699
592
|
};
|
700
593
|
});
|
@@ -709,106 +602,143 @@ class PricingModuleService extends utils_1.ModulesSdkUtils.abstractModuleService
|
|
709
602
|
});
|
710
603
|
pricesToCreate.push(...priceListPricesToCreate);
|
711
604
|
}
|
712
|
-
|
605
|
+
const createdPrices = await this.priceService_.create(pricesToCreate, sharedContext);
|
606
|
+
const eventsData = createdPrices.reduce((eventsData, price) => {
|
607
|
+
eventsData.prices.push({
|
608
|
+
id: price.id,
|
609
|
+
});
|
610
|
+
price.price_rules.map((priceRule) => {
|
611
|
+
eventsData.priceRules.push({
|
612
|
+
id: priceRule.id,
|
613
|
+
});
|
614
|
+
});
|
615
|
+
return eventsData;
|
616
|
+
}, {
|
617
|
+
priceRules: [],
|
618
|
+
prices: [],
|
619
|
+
});
|
620
|
+
_utils_1.eventBuilders.createdPrice({
|
621
|
+
data: eventsData.prices,
|
622
|
+
sharedContext,
|
623
|
+
});
|
624
|
+
_utils_1.eventBuilders.createdPriceRule({
|
625
|
+
data: eventsData.priceRules,
|
626
|
+
sharedContext,
|
627
|
+
});
|
628
|
+
return createdPrices;
|
713
629
|
}
|
714
630
|
async setPriceListRules_(data, sharedContext = {}) {
|
715
631
|
// TODO: re think this method
|
716
|
-
const priceLists = await this.priceListService_.list({ id: data.map((d) => d.price_list_id) }, {
|
717
|
-
|
718
|
-
|
719
|
-
const
|
720
|
-
|
721
|
-
|
722
|
-
|
723
|
-
for (const { price_list_id: priceListId, rules } of data) {
|
724
|
-
const priceList = priceListMap.get(priceListId);
|
725
|
-
if (!priceList) {
|
726
|
-
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, []);
|
727
639
|
}
|
728
|
-
|
729
|
-
|
730
|
-
Object.entries(rules).map(async ([key, value]) => {
|
731
|
-
const ruleType = ruleTypeMap.get(key);
|
732
|
-
if (!ruleType) {
|
733
|
-
throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `Rule type with attribute: ${key} not found`);
|
734
|
-
}
|
735
|
-
const rule = priceListRulesMap.get(key);
|
736
|
-
priceListRuleValues.set(ruleType.id, Array.isArray(value) ? value : [value]);
|
737
|
-
if (!rule) {
|
738
|
-
rulesToCreate.push({
|
739
|
-
rule_type_id: ruleType.id,
|
740
|
-
price_list_id: priceListId,
|
741
|
-
});
|
742
|
-
}
|
743
|
-
else {
|
744
|
-
ruleIdsToUpdate.push(rule.id);
|
745
|
-
}
|
640
|
+
Object.entries(rule.rules).forEach(([key, value]) => {
|
641
|
+
rulesMap.get(rule.price_list_id).push([key, value]);
|
746
642
|
});
|
747
|
-
|
748
|
-
|
749
|
-
|
750
|
-
|
751
|
-
|
752
|
-
|
753
|
-
|
754
|
-
|
755
|
-
|
756
|
-
if (!ruleValues) {
|
757
|
-
continue;
|
758
|
-
}
|
759
|
-
const values = ruleValues.get(rule_type_id);
|
760
|
-
if (!values) {
|
761
|
-
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;
|
762
652
|
}
|
763
|
-
|
764
|
-
|
765
|
-
price_list_rule_id: id,
|
766
|
-
value: v,
|
767
|
-
});
|
653
|
+
rules.forEach(([key, value]) => {
|
654
|
+
allRules.set(key, value);
|
768
655
|
});
|
769
|
-
|
770
|
-
|
771
|
-
|
772
|
-
|
773
|
-
|
774
|
-
|
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;
|
775
668
|
}
|
776
669
|
async removePriceListRules_(data, sharedContext = {}) {
|
777
|
-
|
778
|
-
const
|
779
|
-
|
780
|
-
|
781
|
-
|
782
|
-
|
783
|
-
|
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, []);
|
784
678
|
}
|
785
|
-
|
786
|
-
|
787
|
-
const rule = priceListRulesMap.get(rule_attribute);
|
788
|
-
if (rule) {
|
789
|
-
idsToDelete.push(rule.id);
|
790
|
-
}
|
679
|
+
rule.rules.forEach((key) => {
|
680
|
+
rulesMap.get(rule.price_list_id).push([key, undefined]);
|
791
681
|
});
|
792
|
-
}
|
793
|
-
|
794
|
-
|
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
|
+
});
|
795
721
|
}
|
796
722
|
}
|
797
723
|
exports.default = PricingModuleService;
|
798
724
|
__decorate([
|
799
|
-
(0, utils_1.InjectManager)("baseRepository_")
|
725
|
+
(0, utils_1.InjectManager)("baseRepository_")
|
726
|
+
// @ts-expect-error
|
727
|
+
,
|
800
728
|
__param(2, (0, utils_1.MedusaContext)()),
|
801
729
|
__metadata("design:type", Function),
|
802
730
|
__metadata("design:paramtypes", [Object, Object, Object]),
|
803
731
|
__metadata("design:returntype", Promise)
|
804
|
-
], PricingModuleService.prototype, "
|
732
|
+
], PricingModuleService.prototype, "listPriceSets", null);
|
805
733
|
__decorate([
|
806
|
-
(0, utils_1.InjectManager)("baseRepository_")
|
734
|
+
(0, utils_1.InjectManager)("baseRepository_")
|
735
|
+
// @ts-expect-error
|
736
|
+
,
|
807
737
|
__param(2, (0, utils_1.MedusaContext)()),
|
808
738
|
__metadata("design:type", Function),
|
809
739
|
__metadata("design:paramtypes", [Object, Object, Object]),
|
810
740
|
__metadata("design:returntype", Promise)
|
811
|
-
], PricingModuleService.prototype, "
|
741
|
+
], PricingModuleService.prototype, "listAndCountPriceSets", null);
|
812
742
|
__decorate([
|
813
743
|
(0, utils_1.InjectManager)("baseRepository_"),
|
814
744
|
__param(2, (0, utils_1.MedusaContext)()),
|
@@ -818,88 +748,60 @@ __decorate([
|
|
818
748
|
], PricingModuleService.prototype, "calculatePrices", null);
|
819
749
|
__decorate([
|
820
750
|
(0, utils_1.InjectManager)("baseRepository_"),
|
751
|
+
(0, utils_1.EmitEvents)(),
|
821
752
|
__param(1, (0, utils_1.MedusaContext)()),
|
822
753
|
__metadata("design:type", Function),
|
823
754
|
__metadata("design:paramtypes", [Object, Object]),
|
824
755
|
__metadata("design:returntype", Promise)
|
825
|
-
], PricingModuleService.prototype, "
|
756
|
+
], PricingModuleService.prototype, "createPriceSets", null);
|
826
757
|
__decorate([
|
827
758
|
(0, utils_1.InjectManager)("baseRepository_"),
|
828
759
|
__param(1, (0, utils_1.MedusaContext)()),
|
829
760
|
__metadata("design:type", Function),
|
830
761
|
__metadata("design:paramtypes", [Object, Object]),
|
831
762
|
__metadata("design:returntype", Promise)
|
832
|
-
], PricingModuleService.prototype, "
|
763
|
+
], PricingModuleService.prototype, "upsertPriceSets", null);
|
833
764
|
__decorate([
|
834
765
|
(0, utils_1.InjectManager)("baseRepository_"),
|
835
766
|
__param(2, (0, utils_1.MedusaContext)()),
|
836
767
|
__metadata("design:type", Function),
|
837
768
|
__metadata("design:paramtypes", [Object, Object, Object]),
|
838
769
|
__metadata("design:returntype", Promise)
|
839
|
-
], PricingModuleService.prototype, "
|
770
|
+
], PricingModuleService.prototype, "updatePriceSets", null);
|
840
771
|
__decorate([
|
841
772
|
(0, utils_1.InjectTransactionManager)("baseRepository_"),
|
842
773
|
__param(1, (0, utils_1.MedusaContext)()),
|
843
774
|
__metadata("design:type", Function),
|
844
775
|
__metadata("design:paramtypes", [Array, Object]),
|
845
776
|
__metadata("design:returntype", Promise)
|
846
|
-
], PricingModuleService.prototype, "
|
847
|
-
__decorate([
|
848
|
-
(0, utils_1.InjectManager)("baseRepository_"),
|
849
|
-
__param(1, (0, utils_1.MedusaContext)()),
|
850
|
-
__metadata("design:type", Function),
|
851
|
-
__metadata("design:paramtypes", [Object, Object]),
|
852
|
-
__metadata("design:returntype", Promise)
|
853
|
-
], PricingModuleService.prototype, "addRules", null);
|
777
|
+
], PricingModuleService.prototype, "updatePriceSets_", null);
|
854
778
|
__decorate([
|
855
779
|
(0, utils_1.InjectManager)("baseRepository_"),
|
780
|
+
(0, utils_1.EmitEvents)(),
|
856
781
|
__param(1, (0, utils_1.MedusaContext)()),
|
857
782
|
__metadata("design:type", Function),
|
858
783
|
__metadata("design:paramtypes", [Object, Object]),
|
859
784
|
__metadata("design:returntype", Promise)
|
860
785
|
], PricingModuleService.prototype, "addPrices", null);
|
861
|
-
__decorate([
|
862
|
-
(0, utils_1.InjectTransactionManager)("baseRepository_"),
|
863
|
-
__param(1, (0, utils_1.MedusaContext)()),
|
864
|
-
__metadata("design:type", Function),
|
865
|
-
__metadata("design:paramtypes", [Array, Object]),
|
866
|
-
__metadata("design:returntype", Promise)
|
867
|
-
], PricingModuleService.prototype, "removeRules", null);
|
868
786
|
__decorate([
|
869
787
|
(0, utils_1.InjectManager)("baseRepository_"),
|
788
|
+
(0, utils_1.EmitEvents)()
|
789
|
+
// @ts-ignore
|
790
|
+
,
|
870
791
|
__param(1, (0, utils_1.MedusaContext)()),
|
871
792
|
__metadata("design:type", Function),
|
872
793
|
__metadata("design:paramtypes", [Array, Object]),
|
873
794
|
__metadata("design:returntype", Promise)
|
874
795
|
], PricingModuleService.prototype, "createPriceLists", null);
|
875
796
|
__decorate([
|
876
|
-
(0, utils_1.InjectTransactionManager)("baseRepository_")
|
797
|
+
(0, utils_1.InjectTransactionManager)("baseRepository_")
|
798
|
+
// @ts-ignore
|
799
|
+
,
|
877
800
|
__param(1, (0, utils_1.MedusaContext)()),
|
878
801
|
__metadata("design:type", Function),
|
879
802
|
__metadata("design:paramtypes", [Array, Object]),
|
880
803
|
__metadata("design:returntype", Promise)
|
881
804
|
], PricingModuleService.prototype, "updatePriceLists", null);
|
882
|
-
__decorate([
|
883
|
-
(0, utils_1.InjectManager)("baseRepository_"),
|
884
|
-
__param(1, (0, utils_1.MedusaContext)()),
|
885
|
-
__metadata("design:type", Function),
|
886
|
-
__metadata("design:paramtypes", [Array, Object]),
|
887
|
-
__metadata("design:returntype", Promise)
|
888
|
-
], PricingModuleService.prototype, "createPriceListRules", null);
|
889
|
-
__decorate([
|
890
|
-
(0, utils_1.InjectTransactionManager)("baseRepository_"),
|
891
|
-
__param(1, (0, utils_1.MedusaContext)()),
|
892
|
-
__metadata("design:type", Function),
|
893
|
-
__metadata("design:paramtypes", [Array, Object]),
|
894
|
-
__metadata("design:returntype", Promise)
|
895
|
-
], PricingModuleService.prototype, "createPriceListRules_", null);
|
896
|
-
__decorate([
|
897
|
-
(0, utils_1.InjectTransactionManager)("baseRepository_"),
|
898
|
-
__param(1, (0, utils_1.MedusaContext)()),
|
899
|
-
__metadata("design:type", Function),
|
900
|
-
__metadata("design:paramtypes", [Array, Object]),
|
901
|
-
__metadata("design:returntype", Promise)
|
902
|
-
], PricingModuleService.prototype, "updatePriceListRules", null);
|
903
805
|
__decorate([
|
904
806
|
(0, utils_1.InjectManager)("baseRepository_"),
|
905
807
|
__param(1, (0, utils_1.MedusaContext)()),
|
@@ -916,6 +818,7 @@ __decorate([
|
|
916
818
|
], PricingModuleService.prototype, "removePrices", null);
|
917
819
|
__decorate([
|
918
820
|
(0, utils_1.InjectManager)("baseRepository_"),
|
821
|
+
(0, utils_1.EmitEvents)(),
|
919
822
|
__param(1, (0, utils_1.MedusaContext)()),
|
920
823
|
__metadata("design:type", Function),
|
921
824
|
__metadata("design:paramtypes", [Array, Object]),
|
@@ -941,14 +844,7 @@ __decorate([
|
|
941
844
|
__metadata("design:type", Function),
|
942
845
|
__metadata("design:paramtypes", [Array, Object]),
|
943
846
|
__metadata("design:returntype", Promise)
|
944
|
-
], PricingModuleService.prototype, "
|
945
|
-
__decorate([
|
946
|
-
(0, utils_1.InjectTransactionManager)("baseRepository_"),
|
947
|
-
__param(1, (0, utils_1.MedusaContext)()),
|
948
|
-
__metadata("design:type", Function),
|
949
|
-
__metadata("design:paramtypes", [Array, Object]),
|
950
|
-
__metadata("design:returntype", Promise)
|
951
|
-
], PricingModuleService.prototype, "addRules_", null);
|
847
|
+
], PricingModuleService.prototype, "createPriceSets_", null);
|
952
848
|
__decorate([
|
953
849
|
(0, utils_1.InjectTransactionManager)("baseRepository_"),
|
954
850
|
__param(1, (0, utils_1.MedusaContext)()),
|