@medusajs/pricing 0.1.13-snapshot-20240718073308 → 0.1.13
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/index.d.ts +6 -6
- package/dist/index.js +9 -5
- package/dist/initialize/index.d.ts +4 -0
- package/dist/initialize/index.js +17 -0
- package/dist/joiner-config.d.ts +10 -2
- package/dist/joiner-config.js +52 -4
- package/dist/loaders/connection.d.ts +4 -0
- package/dist/loaders/connection.js +40 -0
- package/dist/loaders/container.d.ts +2 -0
- package/dist/loaders/container.js +34 -0
- package/dist/loaders/index.d.ts +2 -0
- package/dist/loaders/index.js +18 -0
- package/dist/migrations/Migration20230929122253.d.ts +0 -1
- package/dist/migrations/Migration20240322094407.d.ts +0 -1
- package/dist/migrations/Migration20240322113359.d.ts +0 -1
- package/dist/migrations/Migration20240322120125.d.ts +0 -1
- package/dist/models/index.d.ts +3 -2
- package/dist/models/index.js +7 -3
- package/dist/models/price-list-rule-value.d.ts +17 -0
- package/dist/models/price-list-rule-value.js +102 -0
- package/dist/models/price-list-rule.d.ts +8 -6
- package/dist/models/price-list-rule.js +29 -8
- package/dist/models/price-list.d.ts +3 -2
- package/dist/models/price-list.js +9 -0
- package/dist/models/price-rule.d.ts +9 -5
- package/dist/models/price-rule.js +45 -6
- package/dist/models/price-set-rule-type.d.ts +10 -0
- package/dist/models/price-set-rule-type.js +108 -0
- package/dist/models/price-set.d.ts +4 -1
- package/dist/models/price-set.js +19 -0
- package/dist/models/price.d.ts +4 -5
- package/dist/models/price.js +5 -5
- package/dist/models/rule-type.d.ts +17 -0
- package/dist/models/{price-preference.js → rule-type.js} +40 -35
- package/dist/module-definition.d.ts +4 -0
- package/dist/module-definition.js +50 -0
- package/dist/repositories/index.d.ts +0 -1
- package/dist/repositories/pricing.d.ts +0 -1
- package/dist/repositories/pricing.js +36 -29
- package/dist/schema/index.d.ts +0 -1
- package/dist/scripts/bin/run-seed.d.ts +3 -0
- package/dist/scripts/bin/run-seed.js +38 -0
- package/dist/scripts/seed.d.ts +4 -0
- package/dist/scripts/seed.js +67 -0
- package/dist/services/index.d.ts +2 -1
- package/dist/services/index.js +5 -1
- package/dist/services/price-list.d.ts +16 -0
- package/dist/services/price-list.js +34 -0
- package/dist/services/pricing-module.d.ts +53 -66
- package/dist/services/pricing-module.js +605 -594
- package/dist/services/rule-type.d.ts +15 -0
- package/dist/services/rule-type.js +53 -0
- package/dist/types/index.d.ts +0 -1
- package/dist/types/services/index.d.ts +2 -30
- package/dist/types/services/index.js +16 -0
- package/dist/types/services/price-list.d.ts +18 -0
- package/dist/types/services/price-list.js +2 -0
- package/dist/types/services/price-set.d.ts +4 -0
- package/dist/types/services/price-set.js +2 -0
- package/dist/utils/index.d.ts +0 -2
- package/dist/utils/index.js +0 -1
- package/dist/utils/validate-price-list-dates.d.ts +0 -1
- package/package.json +13 -8
- package/dist/index.d.ts.map +0 -1
- package/dist/joiner-config.d.ts.map +0 -1
- package/dist/migrations/Migration20230929122253.d.ts.map +0 -1
- package/dist/migrations/Migration20240322094407.d.ts.map +0 -1
- package/dist/migrations/Migration20240322113359.d.ts.map +0 -1
- package/dist/migrations/Migration20240322120125.d.ts.map +0 -1
- package/dist/migrations/Migration20240626133555.d.ts +0 -5
- package/dist/migrations/Migration20240626133555.d.ts.map +0 -1
- package/dist/migrations/Migration20240626133555.js +0 -33
- package/dist/migrations/Migration20240704094505.d.ts +0 -6
- package/dist/migrations/Migration20240704094505.d.ts.map +0 -1
- package/dist/migrations/Migration20240704094505.js +0 -15
- package/dist/models/index.d.ts.map +0 -1
- package/dist/models/price-list-rule.d.ts.map +0 -1
- package/dist/models/price-list.d.ts.map +0 -1
- package/dist/models/price-preference.d.ts +0 -13
- package/dist/models/price-preference.d.ts.map +0 -1
- package/dist/models/price-rule.d.ts.map +0 -1
- package/dist/models/price-set.d.ts.map +0 -1
- package/dist/models/price.d.ts.map +0 -1
- package/dist/repositories/index.d.ts.map +0 -1
- package/dist/repositories/pricing.d.ts.map +0 -1
- package/dist/schema/index.d.ts.map +0 -1
- package/dist/services/index.d.ts.map +0 -1
- package/dist/services/pricing-module.d.ts.map +0 -1
- package/dist/types/index.d.ts.map +0 -1
- package/dist/types/services/index.d.ts.map +0 -1
- package/dist/utils/events.d.ts +0 -63
- package/dist/utils/events.d.ts.map +0 -1
- package/dist/utils/events.js +0 -66
- package/dist/utils/index.d.ts.map +0 -1
- package/dist/utils/validate-price-list-dates.d.ts.map +0 -1
@@ -17,27 +17,33 @@ 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
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
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) {
|
30
33
|
// @ts-ignore
|
31
34
|
super(...arguments);
|
32
35
|
this.moduleDeclaration = moduleDeclaration;
|
33
36
|
this.baseRepository_ = baseRepository;
|
34
37
|
this.pricingRepository_ = pricingRepository;
|
38
|
+
this.ruleTypeService_ = ruleTypeService;
|
35
39
|
this.priceSetService_ = priceSetService;
|
40
|
+
this.ruleTypeService_ = ruleTypeService;
|
36
41
|
this.priceRuleService_ = priceRuleService;
|
42
|
+
this.priceSetRuleTypeService_ = priceSetRuleTypeService;
|
37
43
|
this.priceService_ = priceService;
|
38
|
-
this.pricePreferenceService_ = pricePreferenceService;
|
39
44
|
this.priceListService_ = priceListService;
|
40
45
|
this.priceListRuleService_ = priceListRuleService;
|
46
|
+
this.priceListRuleValueService_ = priceListRuleValueService;
|
41
47
|
}
|
42
48
|
__joinerConfig() {
|
43
49
|
return joiner_config_1.joinerConfig;
|
@@ -45,69 +51,61 @@ class PricingModuleService extends utils_1.ModulesSdkUtils.MedusaService(generat
|
|
45
51
|
setupCalculatedPriceConfig_(filters, config) {
|
46
52
|
const fieldIdx = config.relations?.indexOf("calculated_price");
|
47
53
|
const shouldCalculatePrice = fieldIdx > -1;
|
48
|
-
const pricingContext = filters.context ?? {};
|
49
|
-
delete filters.context;
|
50
54
|
if (!shouldCalculatePrice) {
|
51
55
|
return;
|
52
56
|
}
|
57
|
+
let pricingContext = filters.context ?? {};
|
53
58
|
// cleanup virtual field "calculated_price"
|
54
59
|
config.relations?.splice(fieldIdx, 1);
|
60
|
+
delete filters.context;
|
55
61
|
return pricingContext;
|
56
62
|
}
|
57
|
-
|
58
|
-
|
59
|
-
const
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
for (const calculatedPrice of calculatedPrices) {
|
73
|
-
calculatedPricesMap.set(calculatedPrice.id, calculatedPrice);
|
74
|
-
}
|
75
|
-
for (const priceSet of priceSets) {
|
76
|
-
const calculatedPrice = calculatedPricesMap.get(priceSet.id);
|
77
|
-
priceSet.calculated_price = calculatedPrice ?? null;
|
63
|
+
async list(filters = {}, config = {}, sharedContext = {}) {
|
64
|
+
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
|
+
}
|
78
78
|
}
|
79
79
|
return priceSets;
|
80
80
|
}
|
81
|
-
|
82
|
-
|
83
|
-
const
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
priceSet.calculated_price = calculatedPrice ?? null;
|
81
|
+
async listAndCount(filters = {}, config = {}, sharedContext = {}) {
|
82
|
+
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
|
+
}
|
97
96
|
}
|
98
97
|
return [priceSets, count];
|
99
98
|
}
|
100
99
|
async calculatePrices(pricingFilters, pricingContext = { context: {} }, sharedContext = {}) {
|
101
100
|
const results = await this.pricingRepository_.calculatePrices(pricingFilters, pricingContext, sharedContext);
|
102
101
|
const pricesSetPricesMap = (0, utils_1.groupBy)(results, "price_set_id");
|
103
|
-
const
|
104
|
-
|
102
|
+
const calculatedPrices = pricingFilters.id.map((priceSetId) => {
|
103
|
+
// This is where we select prices, for now we just do a first match based on the database results
|
104
|
+
// which is prioritized by rules_count first for exact match and then deafult_priority of the rule_type
|
105
|
+
// TODO: inject custom price selection here
|
106
|
+
const prices = pricesSetPricesMap.get(priceSetId) || [];
|
105
107
|
const priceListPrice = prices.find((p) => p.price_list_id);
|
106
108
|
const defaultPrice = prices?.find((p) => !p.price_list_id);
|
107
|
-
if (!prices.length || (!priceListPrice && !defaultPrice)) {
|
108
|
-
pricesSetPricesMap.delete(key);
|
109
|
-
return;
|
110
|
-
}
|
111
109
|
let calculatedPrice = defaultPrice;
|
112
110
|
let originalPrice = defaultPrice;
|
113
111
|
if (priceListPrice) {
|
@@ -116,43 +114,12 @@ class PricingModuleService extends utils_1.ModulesSdkUtils.MedusaService(generat
|
|
116
114
|
originalPrice = priceListPrice;
|
117
115
|
}
|
118
116
|
}
|
119
|
-
pricesSetPricesMap.set(key, { calculatedPrice, originalPrice });
|
120
|
-
priceIds.push(...(0, utils_1.deduplicate)([calculatedPrice?.id, originalPrice?.id].filter(Boolean)));
|
121
|
-
});
|
122
|
-
// We use the price rules to get the right preferences for the price
|
123
|
-
const priceRulesForPrices = await this.priceRuleService_.list({ price_id: priceIds }, { take: null });
|
124
|
-
const priceRulesPriceMap = (0, utils_1.groupBy)(priceRulesForPrices, "price_id");
|
125
|
-
// Note: For now the preferences are intentionally kept very simple and explicit - they use either the region or currency,
|
126
|
-
// so we hard-code those as the possible filters here. This can be made more flexible if needed later on.
|
127
|
-
const pricingPreferences = await this.pricePreferenceService_.list({
|
128
|
-
$or: Object.entries(pricingContext)
|
129
|
-
.filter(([key, val]) => {
|
130
|
-
return key === "region_id" || key === "currency_code";
|
131
|
-
})
|
132
|
-
.map(([key, val]) => ({
|
133
|
-
attribute: key,
|
134
|
-
value: val,
|
135
|
-
})),
|
136
|
-
}, {}, sharedContext);
|
137
|
-
const calculatedPrices = pricingFilters.id
|
138
|
-
.map((priceSetId) => {
|
139
|
-
const prices = pricesSetPricesMap.get(priceSetId);
|
140
|
-
if (!prices) {
|
141
|
-
return null;
|
142
|
-
}
|
143
|
-
const { calculatedPrice, originalPrice, } = prices;
|
144
117
|
return {
|
145
118
|
id: priceSetId,
|
146
119
|
is_calculated_price_price_list: !!calculatedPrice?.price_list_id,
|
147
|
-
|
148
|
-
calculated_amount: parseFloat(calculatedPrice?.amount || "") || null,
|
149
|
-
raw_calculated_amount: calculatedPrice?.raw_amount || null,
|
120
|
+
calculated_amount: parseInt(calculatedPrice?.amount || "") || null,
|
150
121
|
is_original_price_price_list: !!originalPrice?.price_list_id,
|
151
|
-
|
152
|
-
? isTaxInclusive(priceRulesPriceMap.get(originalPrice.id), pricingPreferences, originalPrice.currency_code || calculatedPrice.currency_code, pricingContext.context?.region_id)
|
153
|
-
: false,
|
154
|
-
original_amount: parseFloat(originalPrice?.amount || "") || null,
|
155
|
-
raw_original_amount: originalPrice?.raw_amount || null,
|
122
|
+
original_amount: parseInt(originalPrice?.amount || "") || null,
|
156
123
|
currency_code: calculatedPrice?.currency_code || null,
|
157
124
|
calculated_price: {
|
158
125
|
id: calculatedPrice?.id || null,
|
@@ -169,38 +136,35 @@ class PricingModuleService extends utils_1.ModulesSdkUtils.MedusaService(generat
|
|
169
136
|
max_quantity: parseInt(originalPrice?.max_quantity || "") || null,
|
170
137
|
},
|
171
138
|
};
|
172
|
-
})
|
173
|
-
.filter(Boolean);
|
139
|
+
});
|
174
140
|
return JSON.parse(JSON.stringify(calculatedPrices));
|
175
141
|
}
|
176
|
-
async
|
142
|
+
async create(data, sharedContext = {}) {
|
177
143
|
const input = Array.isArray(data) ? data : [data];
|
178
|
-
const priceSets = await this.
|
144
|
+
const priceSets = await this.create_(input, sharedContext);
|
179
145
|
// TODO: Remove the need to refetch the data here
|
180
|
-
const dbPriceSets = await this.
|
181
|
-
relations: ["prices", "prices.price_rules"],
|
182
|
-
}), sharedContext);
|
146
|
+
const dbPriceSets = await this.list({ id: priceSets.map((p) => p.id) }, { relations: ["rule_types", "prices", "price_rules"] }, sharedContext);
|
183
147
|
// Ensure the output to be in the same order as the input
|
184
148
|
const results = priceSets.map((priceSet) => {
|
185
149
|
return dbPriceSets.find((p) => p.id === priceSet.id);
|
186
150
|
});
|
187
151
|
return await this.baseRepository_.serialize(Array.isArray(data) ? results : results[0]);
|
188
152
|
}
|
189
|
-
async
|
153
|
+
async upsert(data, sharedContext = {}) {
|
190
154
|
const input = Array.isArray(data) ? data : [data];
|
191
155
|
const forUpdate = input.filter((priceSet) => !!priceSet.id);
|
192
156
|
const forCreate = input.filter((priceSet) => !priceSet.id);
|
193
157
|
const operations = [];
|
194
158
|
if (forCreate.length) {
|
195
|
-
operations.push(this.
|
159
|
+
operations.push(this.create_(forCreate, sharedContext));
|
196
160
|
}
|
197
161
|
if (forUpdate.length) {
|
198
|
-
operations.push(this.
|
162
|
+
operations.push(this.update_(forUpdate, sharedContext));
|
199
163
|
}
|
200
164
|
const result = (await (0, utils_1.promiseAll)(operations)).flat();
|
201
165
|
return await this.baseRepository_.serialize(Array.isArray(data) ? result : result[0]);
|
202
166
|
}
|
203
|
-
async
|
167
|
+
async update(idOrSelector, data, sharedContext = {}) {
|
204
168
|
let normalizedInput = [];
|
205
169
|
if ((0, utils_1.isString)(idOrSelector)) {
|
206
170
|
// Check if the ID exists, it will throw if not.
|
@@ -214,16 +178,53 @@ class PricingModuleService extends utils_1.ModulesSdkUtils.MedusaService(generat
|
|
214
178
|
...data,
|
215
179
|
}));
|
216
180
|
}
|
217
|
-
const updateResult = await this.
|
181
|
+
const updateResult = await this.update_(normalizedInput, sharedContext);
|
218
182
|
const priceSets = await this.baseRepository_.serialize(updateResult);
|
219
183
|
return (0, utils_1.isString)(idOrSelector) ? priceSets[0] : priceSets;
|
220
184
|
}
|
221
|
-
async
|
185
|
+
async normalizeUpdateData(data, sharedContext) {
|
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());
|
195
|
+
return data.map((priceSet) => {
|
196
|
+
const prices = priceSet.prices?.map((price) => {
|
197
|
+
const rules = Object.entries(price.rules ?? {}).map(([attribute, value]) => {
|
198
|
+
return {
|
199
|
+
price_set_id: priceSet.id,
|
200
|
+
rule_type_id: ruleTypeMap.get(attribute).id,
|
201
|
+
value,
|
202
|
+
};
|
203
|
+
});
|
204
|
+
const hasRulesInput = (0, utils_1.isPresent)(price.rules);
|
205
|
+
delete price.rules;
|
206
|
+
return {
|
207
|
+
...price,
|
208
|
+
price_set_id: priceSet.id,
|
209
|
+
price_rules: hasRulesInput ? rules : undefined,
|
210
|
+
rules_count: hasRulesInput ? rules.length : undefined,
|
211
|
+
};
|
212
|
+
});
|
213
|
+
return {
|
214
|
+
...priceSet,
|
215
|
+
prices,
|
216
|
+
};
|
217
|
+
});
|
218
|
+
}
|
219
|
+
async update_(data, sharedContext = {}) {
|
220
|
+
// TODO: We are not handling rule types, rules, etc. here, add support after data models are finalized
|
222
221
|
// TODO: Since money IDs are rarely passed, this will delete all previous data and insert new entries.
|
223
222
|
// We can make the `insert` inside upsertWithReplace do an `upsert` instead to avoid this
|
224
|
-
const normalizedData = await this.normalizeUpdateData(data);
|
223
|
+
const normalizedData = await this.normalizeUpdateData(data, sharedContext);
|
225
224
|
const prices = normalizedData.flatMap((priceSet) => priceSet.prices || []);
|
226
|
-
const
|
225
|
+
const upsertedPrices = await this.priceService_.upsertWithReplace(prices, {
|
226
|
+
relations: ["price_rules"],
|
227
|
+
}, sharedContext);
|
227
228
|
const priceSetsToUpsert = normalizedData.map((priceSet) => {
|
228
229
|
const { prices, ...rest } = priceSet;
|
229
230
|
return {
|
@@ -237,71 +238,61 @@ class PricingModuleService extends utils_1.ModulesSdkUtils.MedusaService(generat
|
|
237
238
|
}),
|
238
239
|
};
|
239
240
|
});
|
240
|
-
|
241
|
-
return priceSets;
|
241
|
+
return await this.priceSetService_.upsertWithReplace(priceSetsToUpsert, { relations: ["prices"] }, sharedContext);
|
242
242
|
}
|
243
|
-
async
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
});
|
250
|
-
}
|
251
|
-
normalizePrices(data, existingPrices, priceListId) {
|
252
|
-
const pricesToUpsert = new Map();
|
253
|
-
const existingPricesMap = new Map();
|
254
|
-
existingPrices?.forEach((price) => {
|
255
|
-
existingPricesMap.set(hashPrice(price), price);
|
256
|
-
});
|
257
|
-
data?.forEach((price) => {
|
258
|
-
const cleanRules = price.rules ? (0, utils_1.removeNullish)(price.rules) : {};
|
259
|
-
const ruleEntries = Object.entries(cleanRules);
|
260
|
-
const rules = ruleEntries.map(([attribute, value]) => {
|
261
|
-
return {
|
262
|
-
attribute,
|
263
|
-
value,
|
264
|
-
};
|
265
|
-
});
|
266
|
-
const hasRulesInput = (0, utils_1.isPresent)(price.rules);
|
267
|
-
const entry = {
|
268
|
-
...price,
|
269
|
-
price_list_id: priceListId,
|
270
|
-
price_rules: hasRulesInput ? rules : undefined,
|
271
|
-
rules_count: hasRulesInput ? ruleEntries.length : undefined,
|
272
|
-
};
|
273
|
-
delete entry.rules;
|
274
|
-
const entryHash = hashPrice(entry);
|
275
|
-
// We want to keep the existing rules as they might already have ids, but any other data should come from the updated input
|
276
|
-
const existing = existingPricesMap.get(entryHash);
|
277
|
-
pricesToUpsert.set(entryHash, {
|
278
|
-
...entry,
|
279
|
-
id: existing?.id ?? entry.id,
|
280
|
-
price_rules: existing?.price_rules ?? entry.price_rules,
|
281
|
-
});
|
282
|
-
return entry;
|
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);
|
283
249
|
});
|
284
|
-
return Array.
|
250
|
+
return Array.isArray(data) ? orderedPriceSets : orderedPriceSets[0];
|
285
251
|
}
|
286
252
|
async addPrices(data, sharedContext = {}) {
|
287
253
|
const input = Array.isArray(data) ? data : [data];
|
288
254
|
await this.addPrices_(input, sharedContext);
|
289
|
-
const dbPrices = await this.
|
255
|
+
const dbPrices = await this.list({ id: input.map((d) => d.priceSetId) }, { relations: ["prices"] }, sharedContext);
|
290
256
|
const orderedPriceSets = input.map((inputItem) => {
|
291
257
|
return dbPrices.find((p) => p.id === inputItem.priceSetId);
|
292
258
|
});
|
293
259
|
return Array.isArray(data) ? orderedPriceSets : orderedPriceSets[0];
|
294
260
|
}
|
295
|
-
|
261
|
+
async removeRules(data, sharedContext = {}) {
|
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
|
+
}
|
296
273
|
async createPriceLists(data, sharedContext = {}) {
|
297
274
|
const priceLists = await this.createPriceLists_(data, sharedContext);
|
298
275
|
return await this.baseRepository_.serialize(priceLists);
|
299
276
|
}
|
300
|
-
// @ts-ignore
|
301
277
|
async updatePriceLists(data, sharedContext = {}) {
|
302
278
|
const priceLists = await this.updatePriceLists_(data, sharedContext);
|
303
279
|
return await this.baseRepository_.serialize(priceLists);
|
304
280
|
}
|
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
|
+
}
|
305
296
|
async updatePriceListPrices(data, sharedContext = {}) {
|
306
297
|
const prices = await this.updatePriceListPrices_(data, sharedContext);
|
307
298
|
return await this.baseRepository_.serialize(prices);
|
@@ -321,455 +312,505 @@ class PricingModuleService extends utils_1.ModulesSdkUtils.MedusaService(generat
|
|
321
312
|
const [priceList] = await this.removePriceListRules_([data], sharedContext);
|
322
313
|
return await this.baseRepository_.serialize(priceList);
|
323
314
|
}
|
324
|
-
async
|
325
|
-
const normalized = Array.isArray(data) ? data : [data];
|
326
|
-
const preferences = await this.createPricePreferences_(normalized, sharedContext);
|
327
|
-
const serialized = await this.baseRepository_.serialize(preferences);
|
328
|
-
return Array.isArray(data) ? serialized : serialized[0];
|
329
|
-
}
|
330
|
-
async upsertPricePreferences(data, sharedContext = {}) {
|
315
|
+
async create_(data, sharedContext = {}) {
|
331
316
|
const input = Array.isArray(data) ? data : [data];
|
332
|
-
const
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
317
|
+
const ruleAttributes = data
|
318
|
+
.map((d) => d.rules?.map((r) => r.rule_attribute) ?? [])
|
319
|
+
.flat();
|
320
|
+
const ruleTypes = await this.ruleTypeService_.list({ rule_attribute: ruleAttributes }, { take: null }, sharedContext);
|
321
|
+
const ruleTypeMap = ruleTypes.reduce((acc, curr) => {
|
322
|
+
acc.set(curr.rule_attribute, curr);
|
323
|
+
return acc;
|
324
|
+
}, new Map());
|
325
|
+
const invalidRuleAttributes = ruleAttributes.filter((r) => !ruleTypeMap.has(r));
|
326
|
+
if (invalidRuleAttributes.length > 0) {
|
327
|
+
throw new utils_1.MedusaError(utils_1.MedusaError.Types.NOT_FOUND, `Rule types don't exist for: ${invalidRuleAttributes.join(", ")}`);
|
337
328
|
}
|
338
|
-
|
339
|
-
|
329
|
+
const invalidMoneyAmountRule = data
|
330
|
+
.map((d) => d.prices?.map((ma) => Object.keys(ma?.rules ?? {})).flat() ?? [])
|
331
|
+
.flat()
|
332
|
+
.filter((r) => !ruleTypeMap.has(r));
|
333
|
+
if (invalidMoneyAmountRule.length > 0) {
|
334
|
+
throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `Rule types don't exist for money amounts with rule attribute: ${invalidMoneyAmountRule.join(", ")}`);
|
340
335
|
}
|
341
|
-
const
|
342
|
-
return await this.baseRepository_.serialize(Array.isArray(data) ? result : result[0]);
|
343
|
-
}
|
344
|
-
async updatePricePreferences(idOrSelector, data, sharedContext = {}) {
|
345
|
-
let normalizedInput = [];
|
346
|
-
if ((0, utils_1.isString)(idOrSelector)) {
|
347
|
-
// Check if the ID exists, it will throw if not.
|
348
|
-
await this.pricePreferenceService_.retrieve(idOrSelector, {}, sharedContext);
|
349
|
-
normalizedInput = [{ id: idOrSelector, ...data }];
|
350
|
-
}
|
351
|
-
else {
|
352
|
-
const pricePreferences = await this.pricePreferenceService_.list(idOrSelector, {}, sharedContext);
|
353
|
-
normalizedInput = pricePreferences.map((pricePreference) => ({
|
354
|
-
id: pricePreference.id,
|
355
|
-
...data,
|
356
|
-
}));
|
357
|
-
}
|
358
|
-
const updateResult = await this.updatePricePreferences_(normalizedInput, sharedContext);
|
359
|
-
const pricePreferences = await this.baseRepository_.serialize(updateResult);
|
360
|
-
return (0, utils_1.isString)(idOrSelector) ? pricePreferences[0] : pricePreferences;
|
361
|
-
}
|
362
|
-
async createPricePreferences_(data, sharedContext = {}) {
|
363
|
-
const preferences = await this.pricePreferenceService_.create(data.map((d) => ({
|
364
|
-
...d,
|
365
|
-
is_tax_inclusive: d.is_tax_inclusive ?? false,
|
366
|
-
})), sharedContext);
|
367
|
-
return preferences;
|
368
|
-
}
|
369
|
-
async updatePricePreferences_(data, sharedContext = {}) {
|
370
|
-
const preferences = await this.pricePreferenceService_.update(data, sharedContext);
|
371
|
-
return preferences;
|
372
|
-
}
|
373
|
-
async createPriceSets_(data, sharedContext = {}) {
|
374
|
-
const input = Array.isArray(data) ? data : [data];
|
336
|
+
const ruleSetRuleTypeToCreateMap = new Map();
|
375
337
|
const toCreate = input.map((inputData) => {
|
376
|
-
const
|
377
|
-
|
378
|
-
|
338
|
+
const id = (0, utils_1.generateEntityId)(inputData.id, price_set_1.PriceSetIdPrefix);
|
339
|
+
const { prices, rules = [], ...rest } = inputData;
|
340
|
+
let pricesData = [];
|
341
|
+
rules.forEach((rule) => {
|
342
|
+
const priceSetRuleType = {
|
343
|
+
rule_type_id: ruleTypeMap.get(rule.rule_attribute).id,
|
344
|
+
price_set_id: id,
|
345
|
+
};
|
346
|
+
ruleSetRuleTypeToCreateMap.set(JSON.stringify(priceSetRuleType), priceSetRuleType);
|
347
|
+
});
|
348
|
+
if (inputData.prices) {
|
349
|
+
pricesData = inputData.prices.map((price) => {
|
350
|
+
let { rules: priceRules = {}, ...rest } = price;
|
351
|
+
const cleanRules = priceRules ? (0, utils_1.removeNullish)(priceRules) : {};
|
352
|
+
const numberOfRules = Object.keys(cleanRules).length;
|
353
|
+
const rulesDataMap = new Map();
|
354
|
+
Object.entries(priceRules).map(([attribute, value]) => {
|
355
|
+
const rule = {
|
356
|
+
price_set_id: id,
|
357
|
+
rule_type_id: ruleTypeMap.get(attribute).id,
|
358
|
+
value,
|
359
|
+
};
|
360
|
+
rulesDataMap.set(JSON.stringify(rule), rule);
|
361
|
+
const priceSetRuleType = {
|
362
|
+
rule_type_id: ruleTypeMap.get(attribute).id,
|
363
|
+
price_set_id: id,
|
364
|
+
};
|
365
|
+
ruleSetRuleTypeToCreateMap.set(JSON.stringify(priceSetRuleType), priceSetRuleType);
|
366
|
+
});
|
367
|
+
return {
|
368
|
+
...rest,
|
369
|
+
title: "",
|
370
|
+
rules_count: numberOfRules,
|
371
|
+
price_rules: Array.from(rulesDataMap.values()),
|
372
|
+
};
|
373
|
+
});
|
374
|
+
}
|
375
|
+
return {
|
376
|
+
...rest,
|
377
|
+
id,
|
378
|
+
prices: pricesData,
|
379
379
|
};
|
380
|
-
return entry;
|
381
380
|
});
|
382
381
|
// Bulk create price sets
|
383
382
|
const createdPriceSets = await this.priceSetService_.create(toCreate, sharedContext);
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
|
383
|
+
if (ruleSetRuleTypeToCreateMap.size) {
|
384
|
+
await this.priceSetRuleTypeService_.create(Array.from(ruleSetRuleTypeToCreateMap.values()), sharedContext);
|
385
|
+
}
|
386
|
+
return createdPriceSets;
|
387
|
+
}
|
388
|
+
async addRules_(inputs, sharedContext = {}) {
|
389
|
+
const priceSets = await this.priceSetService_.list({ id: inputs.map((d) => d.priceSetId) }, { relations: ["rule_types"] }, sharedContext);
|
390
|
+
const priceSetRuleTypeMap = new Map(priceSets.map((priceSet) => [
|
391
|
+
priceSet.id,
|
392
|
+
new Map([...priceSet.rule_types].map((rt) => [rt.rule_attribute, rt])),
|
393
|
+
]));
|
394
|
+
const priceSetMap = new Map(priceSets.map((p) => [p.id, p]));
|
395
|
+
const invalidPriceSetInputs = inputs.filter((d) => !priceSetMap.has(d.priceSetId));
|
396
|
+
if (invalidPriceSetInputs.length) {
|
397
|
+
throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `PriceSets with ids: ${invalidPriceSetInputs
|
398
|
+
.map((d) => d.priceSetId)
|
399
|
+
.join(", ")} was not found`);
|
400
|
+
}
|
401
|
+
const ruleTypes = await this.ruleTypeService_.list({
|
402
|
+
rule_attribute: inputs
|
403
|
+
.map((data) => data.rules.map((r) => r.attribute))
|
404
|
+
.flat(),
|
405
|
+
}, { take: null }, sharedContext);
|
406
|
+
const ruleTypeMap = new Map(ruleTypes.map((rt) => [rt.rule_attribute, rt]));
|
407
|
+
const invalidRuleAttributeInputs = inputs
|
408
|
+
.map((d) => d.rules.map((r) => r.attribute))
|
409
|
+
.flat()
|
410
|
+
.filter((r) => !ruleTypeMap.has(r));
|
411
|
+
if (invalidRuleAttributeInputs.length) {
|
412
|
+
throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `Rule types don't exist for attributes: ${[
|
413
|
+
...new Set(invalidRuleAttributeInputs),
|
414
|
+
].join(", ")}`);
|
415
|
+
}
|
416
|
+
const priceSetRuleTypesCreate = [];
|
417
|
+
inputs.forEach((data) => {
|
418
|
+
for (const rule of data.rules) {
|
419
|
+
if (priceSetRuleTypeMap.get(data.priceSetId).has(rule.attribute)) {
|
420
|
+
continue;
|
421
|
+
}
|
422
|
+
priceSetRuleTypesCreate.push({
|
423
|
+
rule_type_id: ruleTypeMap.get(rule.attribute).id,
|
424
|
+
price_set_id: priceSetMap.get(data.priceSetId).id,
|
396
425
|
});
|
397
|
-
}
|
398
|
-
return eventsData;
|
399
|
-
}, {
|
400
|
-
priceSets: [],
|
401
|
-
priceRules: [],
|
402
|
-
prices: [],
|
403
|
-
});
|
404
|
-
_utils_1.eventBuilders.createdPriceSet({
|
405
|
-
data: eventsData.priceSets,
|
406
|
-
sharedContext,
|
407
|
-
});
|
408
|
-
_utils_1.eventBuilders.createdPrice({
|
409
|
-
data: eventsData.prices,
|
410
|
-
sharedContext,
|
411
|
-
});
|
412
|
-
_utils_1.eventBuilders.createdPriceRule({
|
413
|
-
data: eventsData.priceRules,
|
414
|
-
sharedContext,
|
426
|
+
}
|
415
427
|
});
|
416
|
-
|
428
|
+
await this.priceSetRuleTypeService_.create(priceSetRuleTypesCreate, sharedContext);
|
429
|
+
return priceSets;
|
417
430
|
}
|
418
431
|
async addPrices_(input, sharedContext = {}) {
|
419
|
-
const priceSets = await this.
|
420
|
-
const existingPrices = priceSets
|
421
|
-
.map((p) => p.prices)
|
422
|
-
.flat();
|
423
|
-
const pricesToUpsert = input
|
424
|
-
.map((addPrice) => this.normalizePrices(addPrice.prices?.map((p) => ({
|
425
|
-
...p,
|
426
|
-
price_set_id: addPrice.priceSetId,
|
427
|
-
})), existingPrices))
|
428
|
-
.filter(Boolean)
|
429
|
-
.flat();
|
432
|
+
const priceSets = await this.list({ id: input.map((d) => d.priceSetId) }, { relations: ["rule_types"] }, sharedContext);
|
430
433
|
const priceSetMap = new Map(priceSets.map((p) => [p.id, p]));
|
431
|
-
|
432
|
-
|
434
|
+
const ruleTypeMap = new Map(priceSets.map((priceSet) => [
|
435
|
+
priceSet.id,
|
436
|
+
new Map(priceSet.rule_types?.map((rt) => [rt.rule_attribute, rt]) ?? []),
|
437
|
+
]));
|
438
|
+
input.forEach(({ priceSetId, prices }) => {
|
439
|
+
const priceSet = priceSetMap.get(priceSetId);
|
433
440
|
if (!priceSet) {
|
434
|
-
throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `Price set with id: ${
|
441
|
+
throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `Price set with id: ${priceSetId} not found`);
|
442
|
+
}
|
443
|
+
const ruleAttributes = prices
|
444
|
+
.map((d) => (d.rules ? Object.keys(d.rules) : []))
|
445
|
+
.flat();
|
446
|
+
const invalidRuleAttributes = ruleAttributes.filter((r) => !ruleTypeMap.get(priceSetId).has(r));
|
447
|
+
if (invalidRuleAttributes.length > 0) {
|
448
|
+
throw new utils_1.MedusaError(utils_1.MedusaError.Types.NOT_FOUND, `Rule types don't exist for: ${invalidRuleAttributes.join(", ")}`);
|
435
449
|
}
|
436
450
|
});
|
437
|
-
const {
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
});
|
454
|
-
_utils_1.eventBuilders.updatedPriceRule({
|
455
|
-
data: performedActions.updated[_models_1.PriceRule.name] ?? [],
|
456
|
-
sharedContext,
|
457
|
-
});
|
458
|
-
_utils_1.eventBuilders.deletedPriceRule({
|
459
|
-
data: performedActions.deleted[_models_1.PriceRule.name] ?? [],
|
460
|
-
sharedContext,
|
461
|
-
});
|
462
|
-
return entities;
|
451
|
+
const pricesToCreate = input.flatMap(({ priceSetId, prices }) => prices.map((price) => {
|
452
|
+
const numberOfRules = Object.entries(price?.rules ?? {}).length;
|
453
|
+
const priceRules = Object.entries(price.rules ?? {}).map(([attribute, value]) => ({
|
454
|
+
rule_type_id: ruleTypeMap.get(priceSetId).get(attribute).id,
|
455
|
+
price_set_id: priceSetId,
|
456
|
+
value,
|
457
|
+
}));
|
458
|
+
return {
|
459
|
+
...price,
|
460
|
+
price_set_id: priceSetId,
|
461
|
+
title: "test",
|
462
|
+
rules_count: numberOfRules,
|
463
|
+
priceRules,
|
464
|
+
};
|
465
|
+
}));
|
466
|
+
await this.priceService_.create(pricesToCreate, sharedContext);
|
463
467
|
}
|
464
468
|
async createPriceLists_(data, sharedContext = {}) {
|
465
|
-
const
|
466
|
-
const
|
467
|
-
const
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
|
472
|
-
entry.prices = this.normalizePrices(priceListData.prices, []);
|
469
|
+
const ruleTypeAttributes = [];
|
470
|
+
for (const priceListData of data) {
|
471
|
+
const { prices = [], rules: priceListRules = {} } = priceListData;
|
472
|
+
ruleTypeAttributes.push(...Object.keys(priceListRules));
|
473
|
+
for (const price of prices) {
|
474
|
+
const { rules: priceListPriceRules = {} } = price;
|
475
|
+
ruleTypeAttributes.push(...Object.keys(priceListPriceRules));
|
473
476
|
}
|
474
|
-
|
475
|
-
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
|
492
|
-
});
|
493
|
-
const priceLists = await this.priceListService_.create(priceListsToCreate, sharedContext);
|
494
|
-
/**
|
495
|
-
* Preparing data for emitting events
|
496
|
-
*/
|
497
|
-
const eventsData = priceLists.reduce((eventsData, priceList) => {
|
498
|
-
eventsData.priceList.push({
|
499
|
-
id: priceList.id,
|
500
|
-
});
|
501
|
-
priceList.price_list_rules.map((listRule) => {
|
502
|
-
eventsData.priceListRules.push({
|
503
|
-
id: listRule.id,
|
504
|
-
});
|
477
|
+
}
|
478
|
+
const ruleTypes = await this.listRuleTypes({ rule_attribute: ruleTypeAttributes }, { take: null }, sharedContext);
|
479
|
+
const invalidRuleTypes = (0, utils_1.arrayDifference)((0, utils_1.deduplicate)(ruleTypeAttributes), ruleTypes.map((ruleType) => ruleType.rule_attribute));
|
480
|
+
if (invalidRuleTypes.length) {
|
481
|
+
throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `Cannot find RuleTypes with rule_attribute - ${invalidRuleTypes.join(", ")}`);
|
482
|
+
}
|
483
|
+
const ruleTypeMap = new Map(ruleTypes.map((rt) => [rt.rule_attribute, rt]));
|
484
|
+
const priceListsToCreate = data.map((priceListData) => {
|
485
|
+
const id = (0, utils_1.generateEntityId)(priceListData.id, price_list_1.PriceListIdPrefix);
|
486
|
+
const { prices = [], rules = {}, ...rest } = priceListData;
|
487
|
+
(0, _utils_1.validatePriceListDates)(priceListData);
|
488
|
+
const priceListRules = Object.entries(rules).map(([attribute, value]) => {
|
489
|
+
const ruleType = ruleTypeMap.get(attribute);
|
490
|
+
return {
|
491
|
+
price_list_id: id,
|
492
|
+
rule_type_id: ruleType.id,
|
493
|
+
price_list_rule_values: value.map((v) => ({ value: v })),
|
494
|
+
};
|
505
495
|
});
|
506
|
-
|
507
|
-
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
|
512
|
-
|
513
|
-
});
|
496
|
+
const pricesData = prices.map((price) => {
|
497
|
+
const priceRules = Object.entries(price.rules ?? {}).map(([ruleAttribute, ruleValue]) => {
|
498
|
+
return {
|
499
|
+
price_set_id: price.price_set_id,
|
500
|
+
rule_type_id: ruleTypeMap.get(ruleAttribute)?.id,
|
501
|
+
value: ruleValue,
|
502
|
+
};
|
514
503
|
});
|
504
|
+
return {
|
505
|
+
price_list_id: id,
|
506
|
+
title: "test",
|
507
|
+
rules_count: Object.keys(price.rules ?? {}).length,
|
508
|
+
price_rules: priceRules,
|
509
|
+
...price,
|
510
|
+
};
|
515
511
|
});
|
516
|
-
return
|
517
|
-
|
518
|
-
|
519
|
-
|
520
|
-
|
521
|
-
|
522
|
-
|
523
|
-
/**
|
524
|
-
* Emitting events for all created entities
|
525
|
-
*/
|
526
|
-
_utils_1.eventBuilders.createdPriceList({
|
527
|
-
data: eventsData.priceList,
|
528
|
-
sharedContext,
|
529
|
-
});
|
530
|
-
_utils_1.eventBuilders.createdPriceListRule({
|
531
|
-
data: eventsData.priceListRules,
|
532
|
-
sharedContext,
|
533
|
-
});
|
534
|
-
_utils_1.eventBuilders.createdPrice({
|
535
|
-
data: eventsData.prices,
|
536
|
-
sharedContext,
|
537
|
-
});
|
538
|
-
_utils_1.eventBuilders.createdPriceRule({
|
539
|
-
data: eventsData.priceRules,
|
540
|
-
sharedContext,
|
512
|
+
return {
|
513
|
+
id,
|
514
|
+
...rest,
|
515
|
+
rules_count: Object.keys(rules).length,
|
516
|
+
price_list_rules: priceListRules,
|
517
|
+
prices: pricesData,
|
518
|
+
};
|
541
519
|
});
|
542
|
-
return
|
520
|
+
return await this.priceListService_.create(priceListsToCreate, sharedContext);
|
543
521
|
}
|
544
522
|
async updatePriceLists_(data, sharedContext = {}) {
|
545
|
-
const
|
546
|
-
|
547
|
-
|
548
|
-
|
523
|
+
const updatedPriceLists = [];
|
524
|
+
const ruleAttributes = [];
|
525
|
+
const priceListIds = [];
|
526
|
+
for (const priceListData of data) {
|
527
|
+
if (typeof priceListData.rules === "object") {
|
528
|
+
ruleAttributes.push(...Object.keys(priceListData.rules));
|
529
|
+
priceListIds.push(priceListData.id);
|
530
|
+
}
|
549
531
|
}
|
550
|
-
const
|
551
|
-
|
552
|
-
|
553
|
-
|
554
|
-
|
532
|
+
const existingPriceLists = await this.listPriceLists({ id: priceListIds }, { relations: ["price_list_rules"] }, sharedContext);
|
533
|
+
const priceListRuleIds = existingPriceLists
|
534
|
+
.map((pl) => (pl.price_list_rules || []).map((plr) => plr.id))
|
535
|
+
.flat();
|
536
|
+
const existingPriceListRules = await this.listPriceListRules({ id: priceListRuleIds }, {}, sharedContext);
|
537
|
+
if (existingPriceListRules.length) {
|
538
|
+
await this.deletePriceListRules(existingPriceListRules.map((plr) => plr.id), sharedContext);
|
539
|
+
}
|
540
|
+
const ruleTypes = await this.listRuleTypes({ rule_attribute: ruleAttributes }, { take: null }, sharedContext);
|
541
|
+
const ruleTypeMap = new Map(ruleTypes.map((rt) => [rt.rule_attribute, rt]));
|
542
|
+
for (const priceListData of data) {
|
543
|
+
const { rules, ...priceListOnlyData } = priceListData;
|
544
|
+
const updatePriceListData = {
|
545
|
+
...priceListOnlyData,
|
555
546
|
};
|
556
|
-
|
557
|
-
|
558
|
-
|
559
|
-
: {};
|
560
|
-
const rules = Object.entries(cleanRules);
|
561
|
-
const numberOfRules = rules.length;
|
562
|
-
const rulesDataMap = new Map();
|
563
|
-
rules.map(([attribute, value]) => {
|
564
|
-
const rule = {
|
565
|
-
attribute,
|
566
|
-
value,
|
567
|
-
};
|
568
|
-
rulesDataMap.set(JSON.stringify(rule), rule);
|
569
|
-
});
|
570
|
-
entry.price_list_rules = Array.from(rulesDataMap.values());
|
571
|
-
entry.rules_count = numberOfRules;
|
547
|
+
(0, _utils_1.validatePriceListDates)(updatePriceListData);
|
548
|
+
if (typeof rules === "object") {
|
549
|
+
updatePriceListData.rules_count = Object.keys(rules).length;
|
572
550
|
}
|
573
|
-
|
574
|
-
|
575
|
-
|
576
|
-
|
577
|
-
|
578
|
-
|
551
|
+
const [updatedPriceList] = (await this.priceListService_.update([updatePriceListData], sharedContext));
|
552
|
+
updatedPriceLists.push(updatedPriceList);
|
553
|
+
for (const [ruleAttribute, ruleValues = []] of Object.entries(rules || {})) {
|
554
|
+
let ruleType = ruleTypeMap.get(ruleAttribute);
|
555
|
+
if (!ruleType) {
|
556
|
+
;
|
557
|
+
[ruleType] = await this.createRuleTypes([{ name: ruleAttribute, rule_attribute: ruleAttribute }], sharedContext);
|
558
|
+
ruleTypeMap.set(ruleAttribute, ruleType);
|
559
|
+
}
|
560
|
+
const [priceListRule] = await this.priceListRuleService_.create([
|
561
|
+
{
|
562
|
+
price_list_id: updatedPriceList.id,
|
563
|
+
rule_type_id: ruleType?.id,
|
564
|
+
},
|
565
|
+
], sharedContext);
|
566
|
+
for (const ruleValue of ruleValues) {
|
567
|
+
await this.priceListRuleValueService_.create([{ price_list_rule_id: priceListRule.id, value: ruleValue }], sharedContext);
|
568
|
+
}
|
569
|
+
}
|
570
|
+
}
|
571
|
+
return updatedPriceLists;
|
579
572
|
}
|
580
573
|
async updatePriceListPrices_(data, sharedContext = {}) {
|
581
|
-
const
|
582
|
-
const
|
583
|
-
|
584
|
-
|
585
|
-
|
586
|
-
.map((addPrice) => this.normalizePrices(addPrice.prices, existingPrices, addPrice.price_list_id))
|
587
|
-
.filter(Boolean)
|
574
|
+
const ruleTypeAttributes = [];
|
575
|
+
const priceListIds = [];
|
576
|
+
const priceIds = [];
|
577
|
+
const priceSetIds = data
|
578
|
+
.map((d) => d.prices.map((price) => price.price_set_id))
|
588
579
|
.flat();
|
580
|
+
for (const priceListData of data) {
|
581
|
+
priceListIds.push(priceListData.price_list_id);
|
582
|
+
for (const price of priceListData.prices) {
|
583
|
+
priceIds.push(price.id);
|
584
|
+
ruleTypeAttributes.push(...Object.keys(price.rules || {}));
|
585
|
+
}
|
586
|
+
}
|
587
|
+
const prices = await this.listPrices({ id: priceIds }, { take: null, relations: ["price_rules"] }, sharedContext);
|
588
|
+
const priceMap = new Map(prices.map((price) => [price.id, price]));
|
589
|
+
const ruleTypes = await this.listRuleTypes({ rule_attribute: ruleTypeAttributes }, { take: null }, sharedContext);
|
590
|
+
const ruleTypeMap = new Map(ruleTypes.map((rt) => [rt.rule_attribute, rt]));
|
591
|
+
const priceSets = await this.list({ id: priceSetIds }, { relations: ["rule_types"] }, sharedContext);
|
592
|
+
const priceSetRuleTypeMap = priceSets.reduce((acc, curr) => {
|
593
|
+
const priceSetRuleAttributeSet = acc.get(curr.id) || new Set();
|
594
|
+
for (const rt of curr.rule_types ?? []) {
|
595
|
+
priceSetRuleAttributeSet.add(rt.rule_attribute);
|
596
|
+
}
|
597
|
+
acc.set(curr.id, priceSetRuleAttributeSet);
|
598
|
+
return acc;
|
599
|
+
}, new Map());
|
600
|
+
const ruleTypeErrors = [];
|
601
|
+
for (const priceListData of data) {
|
602
|
+
for (const price of priceListData.prices) {
|
603
|
+
for (const ruleAttribute of Object.keys(price.rules ?? {})) {
|
604
|
+
if (!priceSetRuleTypeMap.get(price.price_set_id)?.has(ruleAttribute)) {
|
605
|
+
ruleTypeErrors.push(`rule_attribute "${ruleAttribute}" in price set ${price.price_set_id}`);
|
606
|
+
}
|
607
|
+
}
|
608
|
+
}
|
609
|
+
}
|
610
|
+
if (ruleTypeErrors.length) {
|
611
|
+
throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `Invalid rule type configuration: Price set rules doesn't exist for ${ruleTypeErrors.join(", ")}`);
|
612
|
+
}
|
613
|
+
const priceLists = await this.listPriceLists({ id: priceListIds }, { take: null }, sharedContext);
|
589
614
|
const priceListMap = new Map(priceLists.map((p) => [p.id, p]));
|
615
|
+
const pricesToUpdate = [];
|
616
|
+
const priceRuleIdsToDelete = [];
|
617
|
+
const priceRulesToCreate = [];
|
590
618
|
for (const { price_list_id: priceListId, prices } of data) {
|
591
619
|
const priceList = priceListMap.get(priceListId);
|
592
620
|
if (!priceList) {
|
593
621
|
throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `Price list with id: ${priceListId} not found`);
|
594
622
|
}
|
623
|
+
for (const priceData of prices) {
|
624
|
+
const { rules = {}, price_set_id, ...rest } = priceData;
|
625
|
+
const price = priceMap.get(rest.id);
|
626
|
+
const priceRules = price.price_rules;
|
627
|
+
priceRulesToCreate.push(...Object.entries(rules).map(([ruleAttribute, ruleValue]) => ({
|
628
|
+
price_set_id,
|
629
|
+
rule_type_id: ruleTypeMap.get(ruleAttribute).id,
|
630
|
+
value: ruleValue,
|
631
|
+
price_id: price.id,
|
632
|
+
})));
|
633
|
+
pricesToUpdate.push({
|
634
|
+
...rest,
|
635
|
+
rules_count: Object.keys(rules).length,
|
636
|
+
});
|
637
|
+
priceRuleIdsToDelete.push(...priceRules.map((pr) => pr.id));
|
638
|
+
}
|
595
639
|
}
|
596
|
-
const
|
597
|
-
|
640
|
+
const [_deletedPriceRule, _createdPriceRule, updatedPrices] = await (0, utils_1.promiseAll)([
|
641
|
+
this.priceRuleService_.delete(priceRuleIdsToDelete),
|
642
|
+
this.priceRuleService_.create(priceRulesToCreate),
|
643
|
+
this.priceService_.update(pricesToUpdate),
|
644
|
+
]);
|
645
|
+
return updatedPrices;
|
598
646
|
}
|
599
647
|
async removePrices_(ids, sharedContext = {}) {
|
600
648
|
await this.priceService_.delete(ids, sharedContext);
|
601
649
|
}
|
602
650
|
async addPriceListPrices_(data, sharedContext = {}) {
|
603
|
-
const
|
604
|
-
const
|
605
|
-
|
606
|
-
|
607
|
-
|
608
|
-
|
609
|
-
|
610
|
-
|
651
|
+
const ruleTypeAttributes = [];
|
652
|
+
const priceListIds = [];
|
653
|
+
const priceSetIds = [];
|
654
|
+
for (const priceListData of data) {
|
655
|
+
priceListIds.push(priceListData.price_list_id);
|
656
|
+
for (const price of priceListData.prices) {
|
657
|
+
ruleTypeAttributes.push(...Object.keys(price.rules || {}));
|
658
|
+
priceSetIds.push(price.price_set_id);
|
659
|
+
}
|
660
|
+
}
|
661
|
+
const ruleTypes = await this.listRuleTypes({ rule_attribute: ruleTypeAttributes }, { take: null }, sharedContext);
|
662
|
+
const priceSets = await this.list({ id: priceSetIds }, { relations: ["rule_types"] }, sharedContext);
|
663
|
+
const priceSetRuleTypeMap = priceSets.reduce((acc, curr) => {
|
664
|
+
const priceSetRuleAttributeSet = acc.get(curr.id) || new Set();
|
665
|
+
for (const rt of curr.rule_types ?? []) {
|
666
|
+
priceSetRuleAttributeSet.add(rt.rule_attribute);
|
667
|
+
}
|
668
|
+
acc.set(curr.id, priceSetRuleAttributeSet);
|
669
|
+
return acc;
|
670
|
+
}, new Map());
|
671
|
+
const ruleTypeErrors = [];
|
672
|
+
for (const priceListData of data) {
|
673
|
+
for (const price of priceListData.prices) {
|
674
|
+
for (const rule_attribute of Object.keys(price.rules ?? {})) {
|
675
|
+
if (!priceSetRuleTypeMap.get(price.price_set_id)?.has(rule_attribute)) {
|
676
|
+
ruleTypeErrors.push(`rule_attribute "${rule_attribute}" in price set ${price.price_set_id}`);
|
677
|
+
}
|
678
|
+
}
|
679
|
+
}
|
680
|
+
}
|
681
|
+
if (ruleTypeErrors.length) {
|
682
|
+
throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `Invalid rule type configuration: Price set rules doesn't exist for ${ruleTypeErrors.join(", ")}`);
|
683
|
+
}
|
684
|
+
const ruleTypeMap = new Map(ruleTypes.map((rt) => [rt.rule_attribute, rt]));
|
685
|
+
const priceLists = await this.listPriceLists({ id: priceListIds }, {}, sharedContext);
|
611
686
|
const priceListMap = new Map(priceLists.map((p) => [p.id, p]));
|
612
|
-
|
613
|
-
|
687
|
+
const pricesToCreate = [];
|
688
|
+
for (const { price_list_id: priceListId, prices } of data) {
|
689
|
+
const priceList = priceListMap.get(priceListId);
|
614
690
|
if (!priceList) {
|
615
|
-
throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `Price list with id: ${
|
691
|
+
throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `Price list with id: ${priceListId} not found`);
|
616
692
|
}
|
617
|
-
|
618
|
-
|
619
|
-
|
620
|
-
|
621
|
-
|
622
|
-
|
623
|
-
|
624
|
-
|
625
|
-
|
626
|
-
|
627
|
-
|
628
|
-
|
629
|
-
|
630
|
-
|
631
|
-
|
632
|
-
|
633
|
-
|
634
|
-
|
635
|
-
|
636
|
-
|
637
|
-
|
638
|
-
|
639
|
-
_utils_1.eventBuilders.deletedPriceRule({
|
640
|
-
data: performedActions.deleted[_models_1.PriceRule.name] ?? [],
|
641
|
-
sharedContext,
|
642
|
-
});
|
643
|
-
return entities;
|
693
|
+
const priceListPricesToCreate = prices.map((priceData) => {
|
694
|
+
const priceRules = priceData.rules || {};
|
695
|
+
const noOfRules = Object.keys(priceRules).length;
|
696
|
+
const priceRulesToCreate = Object.entries(priceRules).map(([ruleAttribute, ruleValue]) => {
|
697
|
+
return {
|
698
|
+
price_set_id: priceData.price_set_id,
|
699
|
+
rule_type_id: ruleTypeMap.get(ruleAttribute)?.id,
|
700
|
+
value: ruleValue,
|
701
|
+
};
|
702
|
+
});
|
703
|
+
return {
|
704
|
+
...priceData,
|
705
|
+
price_set_id: priceData.price_set_id,
|
706
|
+
title: "test",
|
707
|
+
price_list_id: priceList.id,
|
708
|
+
rules_count: noOfRules,
|
709
|
+
price_rules: priceRulesToCreate,
|
710
|
+
};
|
711
|
+
});
|
712
|
+
pricesToCreate.push(...priceListPricesToCreate);
|
713
|
+
}
|
714
|
+
return await this.priceService_.create(pricesToCreate, sharedContext);
|
644
715
|
}
|
645
716
|
async setPriceListRules_(data, sharedContext = {}) {
|
646
717
|
// TODO: re think this method
|
647
|
-
const priceLists = await this.priceListService_.list({ id: data.map((d) => d.price_list_id) }, {
|
648
|
-
|
649
|
-
},
|
650
|
-
const
|
651
|
-
|
652
|
-
|
653
|
-
|
718
|
+
const priceLists = await this.priceListService_.list({ id: data.map((d) => d.price_list_id) }, { relations: ["price_list_rules", "price_list_rules.rule_type"] }, sharedContext);
|
719
|
+
const priceListMap = new Map(priceLists.map((p) => [p.id, p]));
|
720
|
+
const ruleTypes = await this.listRuleTypes({ rule_attribute: data.map((d) => Object.keys(d.rules)).flat() }, { take: null });
|
721
|
+
const ruleTypeMap = new Map(ruleTypes.map((rt) => [rt.rule_attribute, rt]));
|
722
|
+
const ruleIdsToUpdate = [];
|
723
|
+
const rulesToCreate = [];
|
724
|
+
const priceRuleValues = new Map();
|
725
|
+
for (const { price_list_id: priceListId, rules } of data) {
|
726
|
+
const priceList = priceListMap.get(priceListId);
|
727
|
+
if (!priceList) {
|
728
|
+
throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `Price list with id: ${priceListId} not found`);
|
654
729
|
}
|
655
|
-
|
656
|
-
|
730
|
+
const priceListRulesMap = new Map(priceList.price_list_rules.map((p) => [p.rule_type.rule_attribute, p]));
|
731
|
+
const priceListRuleValues = new Map();
|
732
|
+
Object.entries(rules).map(async ([key, value]) => {
|
733
|
+
const ruleType = ruleTypeMap.get(key);
|
734
|
+
if (!ruleType) {
|
735
|
+
throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `Rule type with attribute: ${key} not found`);
|
736
|
+
}
|
737
|
+
const rule = priceListRulesMap.get(key);
|
738
|
+
priceListRuleValues.set(ruleType.id, Array.isArray(value) ? value : [value]);
|
739
|
+
if (!rule) {
|
740
|
+
rulesToCreate.push({
|
741
|
+
rule_type_id: ruleType.id,
|
742
|
+
price_list_id: priceListId,
|
743
|
+
});
|
744
|
+
}
|
745
|
+
else {
|
746
|
+
ruleIdsToUpdate.push(rule.id);
|
747
|
+
}
|
657
748
|
});
|
658
|
-
|
659
|
-
|
660
|
-
|
661
|
-
|
662
|
-
|
663
|
-
|
664
|
-
|
665
|
-
|
666
|
-
|
749
|
+
priceRuleValues.set(priceListId, priceListRuleValues);
|
750
|
+
}
|
751
|
+
const [createdRules, priceListValuesToDelete] = await (0, utils_1.promiseAll)([
|
752
|
+
this.priceListRuleService_.create(rulesToCreate),
|
753
|
+
this.priceListRuleValueService_.list({ price_list_rule_id: ruleIdsToUpdate }, { take: null }),
|
754
|
+
]);
|
755
|
+
const priceListRuleValuesToCreate = [];
|
756
|
+
for (const { id, price_list_id, rule_type_id } of createdRules) {
|
757
|
+
const ruleValues = priceRuleValues.get(price_list_id);
|
758
|
+
if (!ruleValues) {
|
759
|
+
continue;
|
667
760
|
}
|
668
|
-
|
669
|
-
|
761
|
+
const values = ruleValues.get(rule_type_id);
|
762
|
+
if (!values) {
|
763
|
+
continue;
|
764
|
+
}
|
765
|
+
values.forEach((v) => {
|
766
|
+
priceListRuleValuesToCreate.push({
|
767
|
+
price_list_rule_id: id,
|
768
|
+
value: v,
|
769
|
+
});
|
670
770
|
});
|
671
|
-
|
672
|
-
|
673
|
-
|
674
|
-
|
675
|
-
|
676
|
-
|
677
|
-
})),
|
678
|
-
};
|
679
|
-
})
|
680
|
-
.filter(Boolean);
|
681
|
-
const { entities } = await this.priceListService_.upsertWithReplace(priceListsUpsert, { relations: ["price_list_rules"] }, sharedContext);
|
682
|
-
return entities;
|
771
|
+
}
|
772
|
+
await (0, utils_1.promiseAll)([
|
773
|
+
this.priceListRuleValueService_.delete(priceListValuesToDelete.map((p) => p.id), sharedContext),
|
774
|
+
this.priceListRuleValueService_.create(priceListRuleValuesToCreate, sharedContext),
|
775
|
+
]);
|
776
|
+
return priceLists;
|
683
777
|
}
|
684
778
|
async removePriceListRules_(data, sharedContext = {}) {
|
685
|
-
|
686
|
-
const
|
687
|
-
|
688
|
-
|
689
|
-
|
690
|
-
|
691
|
-
|
692
|
-
rulesMap.set(rule.price_list_id, []);
|
693
|
-
}
|
694
|
-
rule.rules.forEach((key) => {
|
695
|
-
rulesMap.get(rule.price_list_id).push([key, undefined]);
|
696
|
-
});
|
697
|
-
});
|
698
|
-
const priceListsUpsert = priceLists
|
699
|
-
.map((priceList) => {
|
700
|
-
const allRules = new Map(priceList.price_list_rules
|
701
|
-
.toArray()
|
702
|
-
.map((r) => [r.attribute, r.value]));
|
703
|
-
const rules = rulesMap.get(priceList.id);
|
704
|
-
if (!rules?.length) {
|
705
|
-
return;
|
779
|
+
const priceLists = await this.priceListService_.list({ id: data.map((d) => d.price_list_id) }, { relations: ["price_list_rules", "price_list_rules.rule_type"] }, sharedContext);
|
780
|
+
const priceListMap = new Map(priceLists.map((p) => [p.id, p]));
|
781
|
+
const idsToDelete = [];
|
782
|
+
for (const { price_list_id: priceListId, rules } of data) {
|
783
|
+
const priceList = priceListMap.get(priceListId);
|
784
|
+
if (!priceList) {
|
785
|
+
throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `Price list with id: ${priceListId} not found`);
|
706
786
|
}
|
707
|
-
|
708
|
-
|
787
|
+
const priceListRulesMap = new Map(priceList.price_list_rules.map((p) => [p.rule_type.rule_attribute, p]));
|
788
|
+
rules.map(async (rule_attribute) => {
|
789
|
+
const rule = priceListRulesMap.get(rule_attribute);
|
790
|
+
if (rule) {
|
791
|
+
idsToDelete.push(rule.id);
|
792
|
+
}
|
709
793
|
});
|
710
|
-
|
711
|
-
|
712
|
-
|
713
|
-
price_list_rules: Array.from(allRules)
|
714
|
-
.map(([attribute, value]) => ({
|
715
|
-
attribute,
|
716
|
-
value,
|
717
|
-
}))
|
718
|
-
.filter((r) => !!r.value),
|
719
|
-
};
|
720
|
-
})
|
721
|
-
.filter(Boolean);
|
722
|
-
const { entities } = await this.priceListService_.upsertWithReplace(priceListsUpsert, { relations: ["price_list_rules"] }, sharedContext);
|
723
|
-
return entities;
|
724
|
-
}
|
725
|
-
normalizePriceListDate(data) {
|
726
|
-
return data.map((priceListData) => {
|
727
|
-
(0, _utils_1.validatePriceListDates)(priceListData);
|
728
|
-
if (!!priceListData.starts_at) {
|
729
|
-
priceListData.starts_at = (0, utils_1.GetIsoStringFromDate)(priceListData.starts_at);
|
730
|
-
}
|
731
|
-
if (!!priceListData.ends_at) {
|
732
|
-
priceListData.ends_at = (0, utils_1.GetIsoStringFromDate)(priceListData.ends_at);
|
733
|
-
}
|
734
|
-
return priceListData;
|
735
|
-
});
|
736
|
-
}
|
737
|
-
normalizePriceSetConfig(config) {
|
738
|
-
return {
|
739
|
-
options: {
|
740
|
-
populateWhere: { prices: { price_list_id: null } },
|
741
|
-
},
|
742
|
-
...config,
|
743
|
-
};
|
794
|
+
}
|
795
|
+
await this.priceListRuleService_.delete(idsToDelete);
|
796
|
+
return priceLists;
|
744
797
|
}
|
745
798
|
}
|
746
799
|
exports.default = PricingModuleService;
|
747
800
|
__decorate([
|
748
|
-
(0, utils_1.InjectManager)("baseRepository_")
|
749
|
-
// @ts-expect-error
|
750
|
-
,
|
751
|
-
__metadata("design:type", Function),
|
752
|
-
__metadata("design:paramtypes", [String, Object, Object]),
|
753
|
-
__metadata("design:returntype", Promise)
|
754
|
-
], PricingModuleService.prototype, "retrievePriceSet", null);
|
755
|
-
__decorate([
|
756
|
-
(0, utils_1.InjectManager)("baseRepository_")
|
757
|
-
// @ts-expect-error
|
758
|
-
,
|
801
|
+
(0, utils_1.InjectManager)("baseRepository_"),
|
759
802
|
__param(2, (0, utils_1.MedusaContext)()),
|
760
803
|
__metadata("design:type", Function),
|
761
804
|
__metadata("design:paramtypes", [Object, Object, Object]),
|
762
805
|
__metadata("design:returntype", Promise)
|
763
|
-
], PricingModuleService.prototype, "
|
806
|
+
], PricingModuleService.prototype, "list", null);
|
764
807
|
__decorate([
|
765
|
-
(0, utils_1.InjectManager)("baseRepository_")
|
766
|
-
// @ts-expect-error
|
767
|
-
,
|
808
|
+
(0, utils_1.InjectManager)("baseRepository_"),
|
768
809
|
__param(2, (0, utils_1.MedusaContext)()),
|
769
810
|
__metadata("design:type", Function),
|
770
811
|
__metadata("design:paramtypes", [Object, Object, Object]),
|
771
812
|
__metadata("design:returntype", Promise)
|
772
|
-
], PricingModuleService.prototype, "
|
813
|
+
], PricingModuleService.prototype, "listAndCount", null);
|
773
814
|
__decorate([
|
774
815
|
(0, utils_1.InjectManager)("baseRepository_"),
|
775
816
|
__param(2, (0, utils_1.MedusaContext)()),
|
@@ -779,55 +820,62 @@ __decorate([
|
|
779
820
|
], PricingModuleService.prototype, "calculatePrices", null);
|
780
821
|
__decorate([
|
781
822
|
(0, utils_1.InjectManager)("baseRepository_"),
|
782
|
-
(0, utils_1.EmitEvents)(),
|
783
823
|
__param(1, (0, utils_1.MedusaContext)()),
|
784
824
|
__metadata("design:type", Function),
|
785
825
|
__metadata("design:paramtypes", [Object, Object]),
|
786
826
|
__metadata("design:returntype", Promise)
|
787
|
-
], PricingModuleService.prototype, "
|
827
|
+
], PricingModuleService.prototype, "create", null);
|
788
828
|
__decorate([
|
789
829
|
(0, utils_1.InjectManager)("baseRepository_"),
|
790
830
|
__param(1, (0, utils_1.MedusaContext)()),
|
791
831
|
__metadata("design:type", Function),
|
792
832
|
__metadata("design:paramtypes", [Object, Object]),
|
793
833
|
__metadata("design:returntype", Promise)
|
794
|
-
], PricingModuleService.prototype, "
|
834
|
+
], PricingModuleService.prototype, "upsert", null);
|
795
835
|
__decorate([
|
796
836
|
(0, utils_1.InjectManager)("baseRepository_"),
|
797
837
|
__param(2, (0, utils_1.MedusaContext)()),
|
798
838
|
__metadata("design:type", Function),
|
799
839
|
__metadata("design:paramtypes", [Object, Object, Object]),
|
800
840
|
__metadata("design:returntype", Promise)
|
801
|
-
], PricingModuleService.prototype, "
|
841
|
+
], PricingModuleService.prototype, "update", null);
|
802
842
|
__decorate([
|
803
843
|
(0, utils_1.InjectTransactionManager)("baseRepository_"),
|
804
844
|
__param(1, (0, utils_1.MedusaContext)()),
|
805
845
|
__metadata("design:type", Function),
|
806
846
|
__metadata("design:paramtypes", [Array, Object]),
|
807
847
|
__metadata("design:returntype", Promise)
|
808
|
-
], PricingModuleService.prototype, "
|
848
|
+
], PricingModuleService.prototype, "update_", null);
|
849
|
+
__decorate([
|
850
|
+
(0, utils_1.InjectManager)("baseRepository_"),
|
851
|
+
__param(1, (0, utils_1.MedusaContext)()),
|
852
|
+
__metadata("design:type", Function),
|
853
|
+
__metadata("design:paramtypes", [Object, Object]),
|
854
|
+
__metadata("design:returntype", Promise)
|
855
|
+
], PricingModuleService.prototype, "addRules", null);
|
809
856
|
__decorate([
|
810
857
|
(0, utils_1.InjectManager)("baseRepository_"),
|
811
|
-
(0, utils_1.EmitEvents)(),
|
812
858
|
__param(1, (0, utils_1.MedusaContext)()),
|
813
859
|
__metadata("design:type", Function),
|
814
860
|
__metadata("design:paramtypes", [Object, Object]),
|
815
861
|
__metadata("design:returntype", Promise)
|
816
862
|
], PricingModuleService.prototype, "addPrices", null);
|
863
|
+
__decorate([
|
864
|
+
(0, utils_1.InjectTransactionManager)("baseRepository_"),
|
865
|
+
__param(1, (0, utils_1.MedusaContext)()),
|
866
|
+
__metadata("design:type", Function),
|
867
|
+
__metadata("design:paramtypes", [Array, Object]),
|
868
|
+
__metadata("design:returntype", Promise)
|
869
|
+
], PricingModuleService.prototype, "removeRules", null);
|
817
870
|
__decorate([
|
818
871
|
(0, utils_1.InjectManager)("baseRepository_"),
|
819
|
-
(0, utils_1.EmitEvents)()
|
820
|
-
// @ts-ignore
|
821
|
-
,
|
822
872
|
__param(1, (0, utils_1.MedusaContext)()),
|
823
873
|
__metadata("design:type", Function),
|
824
874
|
__metadata("design:paramtypes", [Array, Object]),
|
825
875
|
__metadata("design:returntype", Promise)
|
826
876
|
], PricingModuleService.prototype, "createPriceLists", null);
|
827
877
|
__decorate([
|
828
|
-
(0, utils_1.InjectTransactionManager)("baseRepository_")
|
829
|
-
// @ts-ignore
|
830
|
-
,
|
878
|
+
(0, utils_1.InjectTransactionManager)("baseRepository_"),
|
831
879
|
__param(1, (0, utils_1.MedusaContext)()),
|
832
880
|
__metadata("design:type", Function),
|
833
881
|
__metadata("design:paramtypes", [Array, Object]),
|
@@ -839,79 +887,70 @@ __decorate([
|
|
839
887
|
__metadata("design:type", Function),
|
840
888
|
__metadata("design:paramtypes", [Array, Object]),
|
841
889
|
__metadata("design:returntype", Promise)
|
842
|
-
], PricingModuleService.prototype, "
|
890
|
+
], PricingModuleService.prototype, "createPriceListRules", null);
|
843
891
|
__decorate([
|
844
|
-
(0, utils_1.
|
892
|
+
(0, utils_1.InjectTransactionManager)("baseRepository_"),
|
845
893
|
__param(1, (0, utils_1.MedusaContext)()),
|
846
894
|
__metadata("design:type", Function),
|
847
895
|
__metadata("design:paramtypes", [Array, Object]),
|
848
896
|
__metadata("design:returntype", Promise)
|
849
|
-
], PricingModuleService.prototype, "
|
897
|
+
], PricingModuleService.prototype, "createPriceListRules_", null);
|
850
898
|
__decorate([
|
851
|
-
(0, utils_1.
|
852
|
-
(0, utils_1.EmitEvents)(),
|
899
|
+
(0, utils_1.InjectTransactionManager)("baseRepository_"),
|
853
900
|
__param(1, (0, utils_1.MedusaContext)()),
|
854
901
|
__metadata("design:type", Function),
|
855
902
|
__metadata("design:paramtypes", [Array, Object]),
|
856
903
|
__metadata("design:returntype", Promise)
|
857
|
-
], PricingModuleService.prototype, "
|
904
|
+
], PricingModuleService.prototype, "updatePriceListRules", null);
|
858
905
|
__decorate([
|
859
906
|
(0, utils_1.InjectManager)("baseRepository_"),
|
860
907
|
__param(1, (0, utils_1.MedusaContext)()),
|
861
908
|
__metadata("design:type", Function),
|
862
|
-
__metadata("design:paramtypes", [
|
909
|
+
__metadata("design:paramtypes", [Array, Object]),
|
863
910
|
__metadata("design:returntype", Promise)
|
864
|
-
], PricingModuleService.prototype, "
|
911
|
+
], PricingModuleService.prototype, "updatePriceListPrices", null);
|
865
912
|
__decorate([
|
866
913
|
(0, utils_1.InjectManager)("baseRepository_"),
|
867
914
|
__param(1, (0, utils_1.MedusaContext)()),
|
868
915
|
__metadata("design:type", Function),
|
869
|
-
__metadata("design:paramtypes", [
|
916
|
+
__metadata("design:paramtypes", [Array, Object]),
|
870
917
|
__metadata("design:returntype", Promise)
|
871
|
-
], PricingModuleService.prototype, "
|
918
|
+
], PricingModuleService.prototype, "removePrices", null);
|
872
919
|
__decorate([
|
873
920
|
(0, utils_1.InjectManager)("baseRepository_"),
|
874
|
-
(0, utils_1.EmitEvents)(),
|
875
921
|
__param(1, (0, utils_1.MedusaContext)()),
|
876
922
|
__metadata("design:type", Function),
|
877
|
-
__metadata("design:paramtypes", [
|
923
|
+
__metadata("design:paramtypes", [Array, Object]),
|
878
924
|
__metadata("design:returntype", Promise)
|
879
|
-
], PricingModuleService.prototype, "
|
925
|
+
], PricingModuleService.prototype, "addPriceListPrices", null);
|
880
926
|
__decorate([
|
881
927
|
(0, utils_1.InjectManager)("baseRepository_"),
|
882
928
|
__param(1, (0, utils_1.MedusaContext)()),
|
883
929
|
__metadata("design:type", Function),
|
884
930
|
__metadata("design:paramtypes", [Object, Object]),
|
885
931
|
__metadata("design:returntype", Promise)
|
886
|
-
], PricingModuleService.prototype, "
|
932
|
+
], PricingModuleService.prototype, "setPriceListRules", null);
|
887
933
|
__decorate([
|
888
934
|
(0, utils_1.InjectManager)("baseRepository_"),
|
889
|
-
__param(2, (0, utils_1.MedusaContext)()),
|
890
|
-
__metadata("design:type", Function),
|
891
|
-
__metadata("design:paramtypes", [Object, Object, Object]),
|
892
|
-
__metadata("design:returntype", Promise)
|
893
|
-
], PricingModuleService.prototype, "updatePricePreferences", null);
|
894
|
-
__decorate([
|
895
|
-
(0, utils_1.InjectTransactionManager)("baseRepository_"),
|
896
935
|
__param(1, (0, utils_1.MedusaContext)()),
|
897
936
|
__metadata("design:type", Function),
|
898
|
-
__metadata("design:paramtypes", [
|
937
|
+
__metadata("design:paramtypes", [Object, Object]),
|
899
938
|
__metadata("design:returntype", Promise)
|
900
|
-
], PricingModuleService.prototype, "
|
939
|
+
], PricingModuleService.prototype, "removePriceListRules", null);
|
901
940
|
__decorate([
|
902
941
|
(0, utils_1.InjectTransactionManager)("baseRepository_"),
|
903
942
|
__param(1, (0, utils_1.MedusaContext)()),
|
904
943
|
__metadata("design:type", Function),
|
905
944
|
__metadata("design:paramtypes", [Array, Object]),
|
906
945
|
__metadata("design:returntype", Promise)
|
907
|
-
], PricingModuleService.prototype, "
|
946
|
+
], PricingModuleService.prototype, "create_", null);
|
908
947
|
__decorate([
|
909
948
|
(0, utils_1.InjectTransactionManager)("baseRepository_"),
|
910
949
|
__param(1, (0, utils_1.MedusaContext)()),
|
911
950
|
__metadata("design:type", Function),
|
912
951
|
__metadata("design:paramtypes", [Array, Object]),
|
913
952
|
__metadata("design:returntype", Promise)
|
914
|
-
], PricingModuleService.prototype, "
|
953
|
+
], PricingModuleService.prototype, "addRules_", null);
|
915
954
|
__decorate([
|
916
955
|
(0, utils_1.InjectTransactionManager)("baseRepository_"),
|
917
956
|
__param(1, (0, utils_1.MedusaContext)()),
|
@@ -963,31 +1002,3 @@ __decorate([
|
|
963
1002
|
__metadata("design:paramtypes", [Array, Object]),
|
964
1003
|
__metadata("design:returntype", Promise)
|
965
1004
|
], PricingModuleService.prototype, "removePriceListRules_", null);
|
966
|
-
const isTaxInclusive = (priceRules, preferences, currencyCode, regionId) => {
|
967
|
-
const regionRule = priceRules?.find((rule) => rule.attribute === "region_id" && rule.value === regionId);
|
968
|
-
const regionPreference = preferences.find((p) => p.attribute === "region_id" && p.value === regionId);
|
969
|
-
const currencyPreference = preferences.find((p) => p.attribute === "currency_code" && p.value === currencyCode);
|
970
|
-
if (regionRule && regionPreference) {
|
971
|
-
return regionPreference.is_tax_inclusive;
|
972
|
-
}
|
973
|
-
if (currencyPreference) {
|
974
|
-
return currencyPreference.is_tax_inclusive;
|
975
|
-
}
|
976
|
-
return false;
|
977
|
-
};
|
978
|
-
const hashPrice = (price) => {
|
979
|
-
const data = Object.entries({
|
980
|
-
currency_code: price.currency_code,
|
981
|
-
price_set_id: "price_set_id" in price ? price.price_set_id ?? null : null,
|
982
|
-
price_list_id: "price_list_id" in price ? price.price_list_id ?? null : null,
|
983
|
-
min_quantity: price.min_quantity ? price.min_quantity.toString() : null,
|
984
|
-
max_quantity: price.max_quantity ? price.max_quantity.toString() : null,
|
985
|
-
...("price_rules" in price
|
986
|
-
? price.price_rules?.reduce((agg, pr) => {
|
987
|
-
agg[pr.attribute] = pr.value;
|
988
|
-
return agg;
|
989
|
-
}, {})
|
990
|
-
: {}),
|
991
|
-
}).sort(([a], [b]) => a.localeCompare(b));
|
992
|
-
return (0, utils_1.simpleHash)(JSON.stringify(data));
|
993
|
-
};
|