@medusajs/promotion 0.0.5-snapshot-20240718073308 → 0.0.5-snapshot-20240718083016
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/index.d.ts +4 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +20 -5
- package/dist/initialize/index.d.ts +5 -0
- package/dist/initialize/index.d.ts.map +1 -0
- package/dist/initialize/index.js +16 -0
- package/dist/joiner-config.d.ts +8 -1
- package/dist/joiner-config.d.ts.map +1 -1
- package/dist/joiner-config.js +35 -4
- package/dist/loaders/connection.d.ts +5 -0
- package/dist/loaders/connection.d.ts.map +1 -0
- package/dist/loaders/connection.js +41 -0
- package/dist/loaders/container.d.ts +3 -0
- package/dist/loaders/container.d.ts.map +1 -0
- package/dist/loaders/container.js +34 -0
- package/dist/loaders/index.d.ts +3 -0
- package/dist/loaders/index.d.ts.map +1 -0
- package/dist/loaders/index.js +18 -0
- package/dist/migrations/Migration20240227120221.d.ts.map +1 -1
- package/dist/migrations/Migration20240227120221.js +2 -10
- package/dist/models/application-method.d.ts +6 -4
- package/dist/models/application-method.d.ts.map +1 -1
- package/dist/models/application-method.js +6 -16
- package/dist/models/campaign-budget.d.ts +5 -6
- package/dist/models/campaign-budget.d.ts.map +1 -1
- package/dist/models/campaign-budget.js +4 -8
- package/dist/models/campaign.d.ts +4 -3
- package/dist/models/campaign.d.ts.map +1 -1
- package/dist/models/campaign.js +13 -10
- package/dist/models/promotion-rule-value.d.ts +1 -2
- package/dist/models/promotion-rule-value.d.ts.map +1 -1
- package/dist/models/promotion-rule-value.js +1 -1
- package/dist/models/promotion-rule.d.ts +1 -1
- package/dist/models/promotion-rule.d.ts.map +1 -1
- package/dist/models/promotion.d.ts +4 -5
- package/dist/models/promotion.d.ts.map +1 -1
- package/dist/models/promotion.js +4 -8
- package/dist/module-definition.d.ts +5 -0
- package/dist/module-definition.d.ts.map +1 -0
- package/dist/module-definition.js +50 -0
- package/dist/repositories/campaign.d.ts +16 -0
- package/dist/repositories/campaign.d.ts.map +1 -0
- package/dist/repositories/campaign.js +90 -0
- package/dist/repositories/index.d.ts +3 -0
- package/dist/repositories/index.d.ts.map +1 -0
- package/dist/repositories/index.js +7 -0
- package/dist/services/promotion-module.d.ts +26 -40
- package/dist/services/promotion-module.d.ts.map +1 -1
- package/dist/services/promotion-module.js +62 -189
- package/dist/types/application-method.d.ts +10 -12
- package/dist/types/application-method.d.ts.map +1 -1
- package/dist/types/campaign-budget.d.ts +6 -8
- package/dist/types/campaign-budget.d.ts.map +1 -1
- package/dist/types/campaign.d.ts +8 -6
- package/dist/types/campaign.d.ts.map +1 -1
- package/dist/types/promotion.d.ts +2 -2
- package/dist/types/promotion.d.ts.map +1 -1
- package/dist/utils/compute-actions/buy-get.d.ts +2 -2
- package/dist/utils/compute-actions/buy-get.d.ts.map +1 -1
- package/dist/utils/compute-actions/buy-get.js +12 -12
- package/dist/utils/compute-actions/line-items.d.ts.map +1 -1
- package/dist/utils/compute-actions/line-items.js +4 -4
- package/dist/utils/compute-actions/shipping-methods.d.ts +2 -2
- package/dist/utils/compute-actions/shipping-methods.d.ts.map +1 -1
- package/dist/utils/compute-actions/shipping-methods.js +17 -14
- package/dist/utils/compute-actions/usage.d.ts +2 -2
- package/dist/utils/compute-actions/usage.d.ts.map +1 -1
- package/dist/utils/compute-actions/usage.js +3 -3
- package/dist/utils/validations/application-method.d.ts.map +1 -1
- package/dist/utils/validations/application-method.js +2 -2
- package/package.json +13 -8
- package/dist/migrations/Migration20240617102917.d.ts +0 -6
- package/dist/migrations/Migration20240617102917.d.ts.map +0 -1
- package/dist/migrations/Migration20240617102917.js +0 -15
- package/dist/migrations/Migration20240624153824.d.ts +0 -6
- package/dist/migrations/Migration20240624153824.d.ts.map +0 -1
- package/dist/migrations/Migration20240624153824.js +0 -15
@@ -18,14 +18,14 @@ const _models_1 = require("../models");
|
|
18
18
|
const _types_1 = require("../types");
|
19
19
|
const _utils_1 = require("../utils");
|
20
20
|
const joiner_config_1 = require("../joiner-config");
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
21
|
+
const generateMethodForModels = [
|
22
|
+
_models_1.ApplicationMethod,
|
23
|
+
_models_1.Campaign,
|
24
|
+
_models_1.CampaignBudget,
|
25
|
+
_models_1.PromotionRule,
|
26
|
+
_models_1.PromotionRuleValue,
|
27
|
+
];
|
28
|
+
class PromotionModuleService extends utils_1.ModulesSdkUtils.abstractModuleServiceFactory(_models_1.Promotion, generateMethodForModels, joiner_config_1.entityNameToLinkableKeysMap) {
|
29
29
|
constructor({ baseRepository, promotionService, applicationMethodService, promotionRuleService, promotionRuleValueService, campaignService, campaignBudgetService, }, moduleDeclaration) {
|
30
30
|
// @ts-ignore
|
31
31
|
super(...arguments);
|
@@ -47,7 +47,7 @@ class PromotionModuleService extends (0, utils_1.MedusaService)({
|
|
47
47
|
.filter(Boolean);
|
48
48
|
const promotionCodeCampaignBudgetMap = new Map();
|
49
49
|
const promotionCodeUsageMap = new Map();
|
50
|
-
const existingPromotions = await this.
|
50
|
+
const existingPromotions = await this.list({ code: promotionCodes }, { relations: ["application_method", "campaign", "campaign.budget"] }, sharedContext);
|
51
51
|
const existingPromotionsMap = new Map(existingPromotions.map((promotion) => [promotion.code, promotion]));
|
52
52
|
for (let computedAction of computedActions) {
|
53
53
|
if (!_utils_1.ComputeActionUtils.canRegisterUsage(computedAction)) {
|
@@ -63,9 +63,10 @@ class PromotionModuleService extends (0, utils_1.MedusaService)({
|
|
63
63
|
}
|
64
64
|
if (campaignBudget.type === utils_1.CampaignBudgetType.SPEND) {
|
65
65
|
const campaignBudgetData = promotionCodeCampaignBudgetMap.get(campaignBudget.id) || { id: campaignBudget.id, used: campaignBudget.used ?? 0 };
|
66
|
-
campaignBudgetData.used =
|
66
|
+
campaignBudgetData.used =
|
67
|
+
(campaignBudgetData.used ?? 0) + computedAction.amount;
|
67
68
|
if (campaignBudget.limit &&
|
68
|
-
|
69
|
+
campaignBudgetData.used > campaignBudget.limit) {
|
69
70
|
continue;
|
70
71
|
}
|
71
72
|
promotionCodeCampaignBudgetMap.set(campaignBudget.id, campaignBudgetData);
|
@@ -77,10 +78,10 @@ class PromotionModuleService extends (0, utils_1.MedusaService)({
|
|
77
78
|
}
|
78
79
|
const campaignBudgetData = {
|
79
80
|
id: campaignBudget.id,
|
80
|
-
used:
|
81
|
+
used: (campaignBudget.used ?? 0) + 1,
|
81
82
|
};
|
82
83
|
if (campaignBudget.limit &&
|
83
|
-
|
84
|
+
campaignBudgetData.used > campaignBudget.limit) {
|
84
85
|
continue;
|
85
86
|
}
|
86
87
|
promotionCodeCampaignBudgetMap.set(campaignBudget.id, campaignBudgetData);
|
@@ -103,7 +104,7 @@ class PromotionModuleService extends (0, utils_1.MedusaService)({
|
|
103
104
|
const methodIdPromoValueMap = new Map();
|
104
105
|
const automaticPromotions = preventAutoPromotions
|
105
106
|
? []
|
106
|
-
: await this.
|
107
|
+
: await this.list({ is_automatic: true }, { select: ["code"], take: null }, sharedContext);
|
107
108
|
// Promotions we need to apply includes all the codes that are passed as an argument
|
108
109
|
// to this method, along with any automatic promotions that can be applied to the context
|
109
110
|
const automaticPromotionCodes = automaticPromotions.map((p) => p.code);
|
@@ -131,7 +132,7 @@ class PromotionModuleService extends (0, utils_1.MedusaService)({
|
|
131
132
|
}
|
132
133
|
});
|
133
134
|
});
|
134
|
-
const promotions = await this.
|
135
|
+
const promotions = await this.list({
|
135
136
|
code: [
|
136
137
|
...promotionCodesToApply,
|
137
138
|
...appliedItemCodes,
|
@@ -208,13 +209,12 @@ class PromotionModuleService extends (0, utils_1.MedusaService)({
|
|
208
209
|
}
|
209
210
|
}
|
210
211
|
}
|
211
|
-
(0, utils_1.transformPropertiesToBigNumber)(computedActions, { include: ["amount"] });
|
212
212
|
return computedActions;
|
213
213
|
}
|
214
|
-
async
|
214
|
+
async create(data, sharedContext = {}) {
|
215
215
|
const input = Array.isArray(data) ? data : [data];
|
216
|
-
const createdPromotions = await this.
|
217
|
-
const promotions = await this.
|
216
|
+
const createdPromotions = await this.create_(input, sharedContext);
|
217
|
+
const promotions = await this.list({ id: createdPromotions.map((p) => p.id) }, {
|
218
218
|
relations: [
|
219
219
|
"application_method",
|
220
220
|
"application_method.target_rules",
|
@@ -230,45 +230,31 @@ class PromotionModuleService extends (0, utils_1.MedusaService)({
|
|
230
230
|
}, sharedContext);
|
231
231
|
return Array.isArray(data) ? promotions : promotions[0];
|
232
232
|
}
|
233
|
-
async
|
233
|
+
async create_(data, sharedContext = {}) {
|
234
234
|
const promotionsData = [];
|
235
235
|
const applicationMethodsData = [];
|
236
236
|
const campaignsData = [];
|
237
|
-
const existingCampaigns = await this.campaignService_.list({ id: data.map((d) => d.campaign_id).filter((id) => (0, utils_1.isString)(id)) }, { relations: ["budget"] }, sharedContext);
|
238
237
|
const promotionCodeApplicationMethodDataMap = new Map();
|
239
238
|
const promotionCodeRulesDataMap = new Map();
|
240
239
|
const methodTargetRulesMap = new Map();
|
241
240
|
const methodBuyRulesMap = new Map();
|
242
241
|
const promotionCodeCampaignMap = new Map();
|
243
242
|
for (const { application_method: applicationMethodData, rules: rulesData, campaign: campaignData, campaign_id: campaignId, ...promotionData } of data) {
|
244
|
-
|
243
|
+
if (applicationMethodData) {
|
244
|
+
promotionCodeApplicationMethodDataMap.set(promotionData.code, applicationMethodData);
|
245
|
+
}
|
245
246
|
if (rulesData) {
|
246
247
|
promotionCodeRulesDataMap.set(promotionData.code, rulesData);
|
247
248
|
}
|
248
249
|
if (campaignData && campaignId) {
|
249
250
|
throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `Provide either the 'campaign' or 'campaign_id' parameter; both cannot be used simultaneously.`);
|
250
251
|
}
|
251
|
-
if (!campaignData && !campaignId) {
|
252
|
-
promotionsData.push({ ...promotionData });
|
253
|
-
continue;
|
254
|
-
}
|
255
|
-
const existingCampaign = existingCampaigns.find((c) => c.id === campaignId);
|
256
|
-
if (campaignId && !existingCampaign) {
|
257
|
-
throw new utils_1.MedusaError(utils_1.MedusaError.Types.NOT_FOUND, `Could not find campaign with id - ${campaignId}`);
|
258
|
-
}
|
259
|
-
const campaignCurrency = campaignData?.budget?.currency_code ||
|
260
|
-
existingCampaigns.find((c) => c.id === campaignId)?.budget
|
261
|
-
?.currency_code;
|
262
|
-
if (campaignData?.budget?.type === utils_1.CampaignBudgetType.SPEND &&
|
263
|
-
campaignCurrency !== applicationMethodData?.currency_code) {
|
264
|
-
throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `Currency between promotion and campaigns should match`);
|
265
|
-
}
|
266
252
|
if (campaignData) {
|
267
253
|
promotionCodeCampaignMap.set(promotionData.code, campaignData);
|
268
254
|
}
|
269
255
|
promotionsData.push({
|
270
256
|
...promotionData,
|
271
|
-
|
257
|
+
campaign: campaignId,
|
272
258
|
});
|
273
259
|
}
|
274
260
|
const createdPromotions = await this.promotionService_.create(promotionsData, sharedContext);
|
@@ -311,14 +297,8 @@ class PromotionModuleService extends (0, utils_1.MedusaService)({
|
|
311
297
|
await this.createPromotionRulesAndValues_(promotionCodeRulesDataMap.get(promotion.code) || [], "promotions", promotion, sharedContext);
|
312
298
|
}
|
313
299
|
const createdApplicationMethods = await this.applicationMethodService_.create(applicationMethodsData, sharedContext);
|
314
|
-
|
315
|
-
|
316
|
-
const promotions = campaignData.promotions;
|
317
|
-
const campaign = createdCampaigns.find((c) => c.campaign_identifier === campaignData.campaign_identifier);
|
318
|
-
if (!campaign || !promotions || !promotions.length) {
|
319
|
-
continue;
|
320
|
-
}
|
321
|
-
await this.addPromotionsToCampaign({ id: campaign.id, promotion_ids: promotions.map((p) => p.id) }, sharedContext);
|
300
|
+
if (campaignsData.length) {
|
301
|
+
await this.createCampaigns(campaignsData, sharedContext);
|
322
302
|
}
|
323
303
|
for (const applicationMethod of createdApplicationMethods) {
|
324
304
|
await this.createPromotionRulesAndValues_(methodTargetRulesMap.get(applicationMethod.promotion.id) || [], "method_target_rules", applicationMethod, sharedContext);
|
@@ -326,10 +306,10 @@ class PromotionModuleService extends (0, utils_1.MedusaService)({
|
|
326
306
|
}
|
327
307
|
return createdPromotions;
|
328
308
|
}
|
329
|
-
async
|
309
|
+
async update(data, sharedContext = {}) {
|
330
310
|
const input = Array.isArray(data) ? data : [data];
|
331
|
-
const updatedPromotions = await this.
|
332
|
-
const promotions = await this.
|
311
|
+
const updatedPromotions = await this.update_(input, sharedContext);
|
312
|
+
const promotions = await this.list({ id: updatedPromotions.map((p) => p.id) }, {
|
333
313
|
relations: [
|
334
314
|
"application_method",
|
335
315
|
"application_method.target_rules",
|
@@ -343,34 +323,25 @@ class PromotionModuleService extends (0, utils_1.MedusaService)({
|
|
343
323
|
}, sharedContext);
|
344
324
|
return Array.isArray(data) ? promotions : promotions[0];
|
345
325
|
}
|
346
|
-
async
|
326
|
+
async update_(data, sharedContext = {}) {
|
347
327
|
const promotionIds = data.map((d) => d.id);
|
348
328
|
const existingPromotions = await this.promotionService_.list({ id: promotionIds }, { relations: ["application_method"] });
|
349
|
-
const existingCampaigns = await this.campaignService_.list({ id: data.map((d) => d.campaign_id).filter((d) => (0, utils_1.isPresent)(d)) }, { relations: ["budget"] });
|
350
329
|
const existingPromotionsMap = new Map(existingPromotions.map((promotion) => [promotion.id, promotion]));
|
351
330
|
const promotionsData = [];
|
352
331
|
const applicationMethodsData = [];
|
353
332
|
for (const { application_method: applicationMethodData, campaign_id: campaignId, ...promotionData } of data) {
|
354
|
-
|
355
|
-
|
356
|
-
const existingApplicationMethod = existingPromotion?.application_method;
|
357
|
-
const promotionCurrencyCode = existingApplicationMethod?.currency_code ||
|
358
|
-
applicationMethodData?.currency_code;
|
359
|
-
if (campaignId && !existingCampaign) {
|
360
|
-
throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `Could not find campaign with id ${campaignId}`);
|
361
|
-
}
|
362
|
-
if (campaignId &&
|
363
|
-
existingCampaign?.budget?.type === utils_1.CampaignBudgetType.SPEND &&
|
364
|
-
existingCampaign.budget.currency_code !== promotionCurrencyCode) {
|
365
|
-
throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `Currency code doesn't match for campaign (${campaignId}) and promotion (${existingPromotion.id})`);
|
366
|
-
}
|
367
|
-
if ((0, utils_1.isDefined)(campaignId)) {
|
368
|
-
promotionsData.push({ ...promotionData, campaign_id: campaignId });
|
333
|
+
if (campaignId) {
|
334
|
+
promotionsData.push({ ...promotionData, campaign: campaignId });
|
369
335
|
}
|
370
336
|
else {
|
371
337
|
promotionsData.push(promotionData);
|
372
338
|
}
|
373
|
-
if (!applicationMethodData
|
339
|
+
if (!applicationMethodData) {
|
340
|
+
continue;
|
341
|
+
}
|
342
|
+
const existingPromotion = existingPromotionsMap.get(promotionData.id);
|
343
|
+
const existingApplicationMethod = existingPromotion?.application_method;
|
344
|
+
if (!existingApplicationMethod) {
|
374
345
|
continue;
|
375
346
|
}
|
376
347
|
if (applicationMethodData.allocation &&
|
@@ -390,10 +361,9 @@ class PromotionModuleService extends (0, utils_1.MedusaService)({
|
|
390
361
|
}
|
391
362
|
return updatedPromotions;
|
392
363
|
}
|
393
|
-
// @ts-ignore
|
394
364
|
async updatePromotionRules(data, sharedContext = {}) {
|
395
365
|
const updatedPromotionRules = await this.updatePromotionRules_(data, sharedContext);
|
396
|
-
return
|
366
|
+
return this.listPromotionRules({ id: updatedPromotionRules.map((r) => r.id) }, { relations: ["values"] }, sharedContext);
|
397
367
|
}
|
398
368
|
async updatePromotionRules_(data, sharedContext = {}) {
|
399
369
|
const promotionRuleIds = data.map((d) => d.id);
|
@@ -443,7 +413,9 @@ class PromotionModuleService extends (0, utils_1.MedusaService)({
|
|
443
413
|
return await this.listPromotionRules({ id: createdPromotionRules.map((pr) => pr.id) }, { relations: ["values"] }, sharedContext);
|
444
414
|
}
|
445
415
|
async addPromotionBuyRules(promotionId, rulesData, sharedContext = {}) {
|
446
|
-
const promotion = await this.promotionService_.retrieve(promotionId, {
|
416
|
+
const promotion = await this.promotionService_.retrieve(promotionId, {
|
417
|
+
relations: ["application_method"],
|
418
|
+
});
|
447
419
|
const applicationMethod = promotion.application_method;
|
448
420
|
if (!applicationMethod) {
|
449
421
|
throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `application_method for promotion not found`);
|
@@ -519,19 +491,22 @@ class PromotionModuleService extends (0, utils_1.MedusaService)({
|
|
519
491
|
const campaignBudgetsData = [];
|
520
492
|
const campaignIdentifierBudgetMap = new Map();
|
521
493
|
for (const createCampaignData of data) {
|
522
|
-
const { budget: campaignBudgetData, ...campaignData } = createCampaignData;
|
494
|
+
const { budget: campaignBudgetData, promotions, ...campaignData } = createCampaignData;
|
495
|
+
const promotionsToAdd = promotions
|
496
|
+
? await this.list({ id: promotions.map((p) => p.id) }, { take: null }, sharedContext)
|
497
|
+
: [];
|
523
498
|
if (campaignBudgetData) {
|
524
499
|
campaignIdentifierBudgetMap.set(campaignData.campaign_identifier, campaignBudgetData);
|
525
500
|
}
|
526
501
|
campaignsData.push({
|
527
502
|
...campaignData,
|
503
|
+
promotions: promotionsToAdd,
|
528
504
|
});
|
529
505
|
}
|
530
506
|
const createdCampaigns = await this.campaignService_.create(campaignsData, sharedContext);
|
531
507
|
for (const createdCampaign of createdCampaigns) {
|
532
508
|
const campaignBudgetData = campaignIdentifierBudgetMap.get(createdCampaign.campaign_identifier);
|
533
509
|
if (campaignBudgetData) {
|
534
|
-
this.validateCampaignBudgetData(campaignBudgetData);
|
535
510
|
campaignBudgetsData.push({
|
536
511
|
...campaignBudgetData,
|
537
512
|
campaign: createdCampaign.id,
|
@@ -543,15 +518,6 @@ class PromotionModuleService extends (0, utils_1.MedusaService)({
|
|
543
518
|
}
|
544
519
|
return createdCampaigns;
|
545
520
|
}
|
546
|
-
validateCampaignBudgetData(data) {
|
547
|
-
if (!data.type) {
|
548
|
-
throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `Campaign Budget type is a required field`);
|
549
|
-
}
|
550
|
-
if (data.type === utils_1.CampaignBudgetType.SPEND &&
|
551
|
-
!(0, utils_1.isPresent)(data.currency_code)) {
|
552
|
-
throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `Campaign Budget type is a required field`);
|
553
|
-
}
|
554
|
-
}
|
555
521
|
async updateCampaigns(data, sharedContext = {}) {
|
556
522
|
const input = Array.isArray(data) ? data : [data];
|
557
523
|
const updatedCampaigns = await this.updateCampaigns_(input, sharedContext);
|
@@ -564,92 +530,27 @@ class PromotionModuleService extends (0, utils_1.MedusaService)({
|
|
564
530
|
async updateCampaigns_(data, sharedContext = {}) {
|
565
531
|
const campaignIds = data.map((d) => d.id);
|
566
532
|
const campaignsData = [];
|
567
|
-
const
|
568
|
-
const createBudgetData = [];
|
533
|
+
const campaignBudgetsData = [];
|
569
534
|
const existingCampaigns = await this.listCampaigns({ id: campaignIds }, { relations: ["budget"], take: null }, sharedContext);
|
570
535
|
const existingCampaignsMap = new Map(existingCampaigns.map((campaign) => [campaign.id, campaign]));
|
571
536
|
for (const updateCampaignData of data) {
|
572
|
-
const { budget:
|
537
|
+
const { budget: campaignBudgetData, ...campaignData } = updateCampaignData;
|
573
538
|
const existingCampaign = existingCampaignsMap.get(campaignData.id);
|
539
|
+
const existingCampaignBudget = existingCampaign?.budget;
|
574
540
|
campaignsData.push(campaignData);
|
575
|
-
|
576
|
-
|
577
|
-
|
578
|
-
|
579
|
-
|
580
|
-
delete budgetData?.currency_code;
|
581
|
-
throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `Campaign budget attributes (type, currency_code) are immutable`);
|
582
|
-
}
|
583
|
-
if (budgetData) {
|
584
|
-
if (existingCampaign?.budget) {
|
585
|
-
updateBudgetData.push({
|
586
|
-
id: existingCampaign.budget.id,
|
587
|
-
...budgetData,
|
588
|
-
});
|
589
|
-
}
|
590
|
-
else {
|
591
|
-
createBudgetData.push({
|
592
|
-
...budgetData,
|
593
|
-
campaign: existingCampaign.id,
|
594
|
-
});
|
595
|
-
}
|
541
|
+
if (existingCampaignBudget && campaignBudgetData) {
|
542
|
+
campaignBudgetsData.push({
|
543
|
+
id: existingCampaignBudget.id,
|
544
|
+
...campaignBudgetData,
|
545
|
+
});
|
596
546
|
}
|
597
547
|
}
|
598
548
|
const updatedCampaigns = await this.campaignService_.update(campaignsData, sharedContext);
|
599
|
-
if (
|
600
|
-
await this.campaignBudgetService_.update(
|
601
|
-
}
|
602
|
-
if (createBudgetData.length) {
|
603
|
-
await this.campaignBudgetService_.create(createBudgetData, sharedContext);
|
549
|
+
if (campaignBudgetsData.length) {
|
550
|
+
await this.campaignBudgetService_.update(campaignBudgetsData, sharedContext);
|
604
551
|
}
|
605
552
|
return updatedCampaigns;
|
606
553
|
}
|
607
|
-
async addPromotionsToCampaign(data, sharedContext) {
|
608
|
-
const ids = await this.addPromotionsToCampaign_(data, sharedContext);
|
609
|
-
return { ids };
|
610
|
-
}
|
611
|
-
// TODO:
|
612
|
-
// - introduce currency_code to promotion
|
613
|
-
// - allow promotions to be queried by currency code
|
614
|
-
// - when the above is present, validate adding promotion to campaign based on currency code
|
615
|
-
async addPromotionsToCampaign_(data, sharedContext = {}) {
|
616
|
-
const { id, promotion_ids: promotionIds = [] } = data;
|
617
|
-
const campaign = await this.campaignService_.retrieve(id, {}, sharedContext);
|
618
|
-
const promotionsToAdd = await this.promotionService_.list({ id: promotionIds, campaign_id: null }, { take: null, relations: ["application_method"] }, sharedContext);
|
619
|
-
const diff = (0, utils_1.arrayDifference)(promotionsToAdd.map((p) => p.id), promotionIds);
|
620
|
-
if (diff.length > 0) {
|
621
|
-
throw new utils_1.MedusaError(utils_1.MedusaError.Types.NOT_FOUND, `Cannot add promotions (${diff.join(",")}) to campaign. These promotions are either already part of a campaign or not found.`);
|
622
|
-
}
|
623
|
-
const promotionsWithInvalidCurrency = promotionsToAdd.filter((promotion) => campaign.budget?.type === utils_1.CampaignBudgetType.SPEND &&
|
624
|
-
promotion.application_method?.currency_code !==
|
625
|
-
campaign?.budget?.currency_code);
|
626
|
-
if (promotionsWithInvalidCurrency.length > 0) {
|
627
|
-
throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `Cannot add promotions to campaign where currency_code don't match.`);
|
628
|
-
}
|
629
|
-
await this.promotionService_.update(promotionsToAdd.map((promotion) => ({
|
630
|
-
id: promotion.id,
|
631
|
-
campaign_id: campaign.id,
|
632
|
-
})), sharedContext);
|
633
|
-
return promotionsToAdd.map((promo) => promo.id);
|
634
|
-
}
|
635
|
-
async removePromotionsFromCampaign(data, sharedContext) {
|
636
|
-
const ids = await this.removePromotionsFromCampaign_(data, sharedContext);
|
637
|
-
return { ids };
|
638
|
-
}
|
639
|
-
async removePromotionsFromCampaign_(data, sharedContext = {}) {
|
640
|
-
const { id, promotion_ids: promotionIds = [] } = data;
|
641
|
-
await this.campaignService_.retrieve(id, {}, sharedContext);
|
642
|
-
const promotionsToRemove = await this.promotionService_.list({ id: promotionIds }, { take: null }, sharedContext);
|
643
|
-
const diff = (0, utils_1.arrayDifference)(promotionsToRemove.map((p) => p.id), promotionIds);
|
644
|
-
if (diff.length > 0) {
|
645
|
-
throw new utils_1.MedusaError(utils_1.MedusaError.Types.NOT_FOUND, `Promotions with ids (${diff.join(",")}) not found.`);
|
646
|
-
}
|
647
|
-
await this.promotionService_.update(promotionsToRemove.map((promotion) => ({
|
648
|
-
id: promotion.id,
|
649
|
-
campaign_id: null,
|
650
|
-
})), sharedContext);
|
651
|
-
return promotionsToRemove.map((promo) => promo.id);
|
652
|
-
}
|
653
554
|
}
|
654
555
|
exports.default = PromotionModuleService;
|
655
556
|
__decorate([
|
@@ -672,32 +573,30 @@ __decorate([
|
|
672
573
|
__metadata("design:type", Function),
|
673
574
|
__metadata("design:paramtypes", [Object, Object]),
|
674
575
|
__metadata("design:returntype", Promise)
|
675
|
-
], PromotionModuleService.prototype, "
|
576
|
+
], PromotionModuleService.prototype, "create", null);
|
676
577
|
__decorate([
|
677
578
|
(0, utils_1.InjectTransactionManager)("baseRepository_"),
|
678
579
|
__param(1, (0, utils_1.MedusaContext)()),
|
679
580
|
__metadata("design:type", Function),
|
680
581
|
__metadata("design:paramtypes", [Array, Object]),
|
681
582
|
__metadata("design:returntype", Promise)
|
682
|
-
], PromotionModuleService.prototype, "
|
583
|
+
], PromotionModuleService.prototype, "create_", null);
|
683
584
|
__decorate([
|
684
585
|
(0, utils_1.InjectManager)("baseRepository_"),
|
685
586
|
__param(1, (0, utils_1.MedusaContext)()),
|
686
587
|
__metadata("design:type", Function),
|
687
588
|
__metadata("design:paramtypes", [Object, Object]),
|
688
589
|
__metadata("design:returntype", Promise)
|
689
|
-
], PromotionModuleService.prototype, "
|
590
|
+
], PromotionModuleService.prototype, "update", null);
|
690
591
|
__decorate([
|
691
592
|
(0, utils_1.InjectTransactionManager)("baseRepository_"),
|
692
593
|
__param(1, (0, utils_1.MedusaContext)()),
|
693
594
|
__metadata("design:type", Function),
|
694
595
|
__metadata("design:paramtypes", [Array, Object]),
|
695
596
|
__metadata("design:returntype", Promise)
|
696
|
-
], PromotionModuleService.prototype, "
|
597
|
+
], PromotionModuleService.prototype, "update_", null);
|
697
598
|
__decorate([
|
698
|
-
(0, utils_1.InjectManager)("baseRepository_")
|
699
|
-
// @ts-ignore
|
700
|
-
,
|
599
|
+
(0, utils_1.InjectManager)("baseRepository_"),
|
701
600
|
__param(1, (0, utils_1.MedusaContext)()),
|
702
601
|
__metadata("design:type", Function),
|
703
602
|
__metadata("design:paramtypes", [Array, Object]),
|
@@ -801,29 +700,3 @@ __decorate([
|
|
801
700
|
__metadata("design:paramtypes", [Array, Object]),
|
802
701
|
__metadata("design:returntype", Promise)
|
803
702
|
], PromotionModuleService.prototype, "updateCampaigns_", null);
|
804
|
-
__decorate([
|
805
|
-
(0, utils_1.InjectManager)("baseRepository_"),
|
806
|
-
__metadata("design:type", Function),
|
807
|
-
__metadata("design:paramtypes", [Object, Object]),
|
808
|
-
__metadata("design:returntype", Promise)
|
809
|
-
], PromotionModuleService.prototype, "addPromotionsToCampaign", null);
|
810
|
-
__decorate([
|
811
|
-
(0, utils_1.InjectTransactionManager)("baseRepository_"),
|
812
|
-
__param(1, (0, utils_1.MedusaContext)()),
|
813
|
-
__metadata("design:type", Function),
|
814
|
-
__metadata("design:paramtypes", [Object, Object]),
|
815
|
-
__metadata("design:returntype", Promise)
|
816
|
-
], PromotionModuleService.prototype, "addPromotionsToCampaign_", null);
|
817
|
-
__decorate([
|
818
|
-
(0, utils_1.InjectManager)("baseRepository_"),
|
819
|
-
__metadata("design:type", Function),
|
820
|
-
__metadata("design:paramtypes", [Object, Object]),
|
821
|
-
__metadata("design:returntype", Promise)
|
822
|
-
], PromotionModuleService.prototype, "removePromotionsFromCampaign", null);
|
823
|
-
__decorate([
|
824
|
-
(0, utils_1.InjectTransactionManager)("baseRepository_"),
|
825
|
-
__param(1, (0, utils_1.MedusaContext)()),
|
826
|
-
__metadata("design:type", Function),
|
827
|
-
__metadata("design:paramtypes", [Object, Object]),
|
828
|
-
__metadata("design:returntype", Promise)
|
829
|
-
], PromotionModuleService.prototype, "removePromotionsFromCampaign_", null);
|
@@ -1,26 +1,24 @@
|
|
1
|
-
import { ApplicationMethodAllocationValues, ApplicationMethodTargetTypeValues, ApplicationMethodTypeValues,
|
1
|
+
import { ApplicationMethodAllocationValues, ApplicationMethodTargetTypeValues, ApplicationMethodTypeValues, PromotionDTO } from "@medusajs/types";
|
2
2
|
import { Promotion } from "../models";
|
3
3
|
export interface CreateApplicationMethodDTO {
|
4
4
|
type: ApplicationMethodTypeValues;
|
5
5
|
target_type: ApplicationMethodTargetTypeValues;
|
6
6
|
allocation?: ApplicationMethodAllocationValues;
|
7
|
-
value?:
|
8
|
-
currency_code?: string | null;
|
7
|
+
value?: number;
|
9
8
|
promotion: Promotion | string | PromotionDTO;
|
10
|
-
max_quantity?:
|
11
|
-
buy_rules_min_quantity?:
|
12
|
-
apply_to_quantity?:
|
9
|
+
max_quantity?: number | null;
|
10
|
+
buy_rules_min_quantity?: number | null;
|
11
|
+
apply_to_quantity?: number | null;
|
13
12
|
}
|
14
13
|
export interface UpdateApplicationMethodDTO {
|
15
|
-
id
|
14
|
+
id: string;
|
16
15
|
type?: ApplicationMethodTypeValues;
|
17
16
|
target_type?: ApplicationMethodTargetTypeValues;
|
18
17
|
allocation?: ApplicationMethodAllocationValues;
|
19
|
-
value?:
|
20
|
-
currency_code?: string | null;
|
18
|
+
value?: number;
|
21
19
|
promotion?: Promotion | string | PromotionDTO;
|
22
|
-
max_quantity?:
|
23
|
-
buy_rules_min_quantity?:
|
24
|
-
apply_to_quantity?:
|
20
|
+
max_quantity?: number | null;
|
21
|
+
buy_rules_min_quantity?: number | null;
|
22
|
+
apply_to_quantity?: number | null;
|
25
23
|
}
|
26
24
|
//# sourceMappingURL=application-method.d.ts.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"application-method.d.ts","sourceRoot":"","sources":["../../src/types/application-method.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,iCAAiC,EACjC,iCAAiC,EACjC,2BAA2B,EAC3B,
|
1
|
+
{"version":3,"file":"application-method.d.ts","sourceRoot":"","sources":["../../src/types/application-method.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,iCAAiC,EACjC,iCAAiC,EACjC,2BAA2B,EAC3B,YAAY,EACb,MAAM,iBAAiB,CAAA;AAExB,OAAO,EAAE,SAAS,EAAE,MAAM,SAAS,CAAA;AAEnC,MAAM,WAAW,0BAA0B;IACzC,IAAI,EAAE,2BAA2B,CAAA;IACjC,WAAW,EAAE,iCAAiC,CAAA;IAC9C,UAAU,CAAC,EAAE,iCAAiC,CAAA;IAC9C,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,SAAS,EAAE,SAAS,GAAG,MAAM,GAAG,YAAY,CAAA;IAC5C,YAAY,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;IAC5B,sBAAsB,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;IACtC,iBAAiB,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;CAClC;AAED,MAAM,WAAW,0BAA0B;IACzC,EAAE,EAAE,MAAM,CAAA;IACV,IAAI,CAAC,EAAE,2BAA2B,CAAA;IAClC,WAAW,CAAC,EAAE,iCAAiC,CAAA;IAC/C,UAAU,CAAC,EAAE,iCAAiC,CAAA;IAC9C,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,SAAS,CAAC,EAAE,SAAS,GAAG,MAAM,GAAG,YAAY,CAAA;IAC7C,YAAY,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;IAC5B,sBAAsB,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;IACtC,iBAAiB,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;CAClC"}
|
@@ -1,17 +1,15 @@
|
|
1
|
-
import {
|
1
|
+
import { CampaignBudgetTypeValues } from "@medusajs/types";
|
2
2
|
import { Campaign } from "../models";
|
3
3
|
export interface CreateCampaignBudgetDTO {
|
4
|
-
type
|
5
|
-
limit
|
6
|
-
|
7
|
-
used?: BigNumberInput;
|
4
|
+
type: CampaignBudgetTypeValues;
|
5
|
+
limit: number | null;
|
6
|
+
used?: number;
|
8
7
|
campaign?: Campaign | string;
|
9
8
|
}
|
10
9
|
export interface UpdateCampaignBudgetDTO {
|
11
10
|
id: string;
|
12
11
|
type?: CampaignBudgetTypeValues;
|
13
|
-
limit?:
|
14
|
-
|
15
|
-
used?: BigNumberInput;
|
12
|
+
limit?: number | null;
|
13
|
+
used?: number;
|
16
14
|
}
|
17
15
|
//# sourceMappingURL=campaign-budget.d.ts.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"campaign-budget.d.ts","sourceRoot":"","sources":["../../src/types/campaign-budget.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
1
|
+
{"version":3,"file":"campaign-budget.d.ts","sourceRoot":"","sources":["../../src/types/campaign-budget.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,wBAAwB,EAAE,MAAM,iBAAiB,CAAA;AAC1D,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAA;AAElC,MAAM,WAAW,uBAAuB;IACtC,IAAI,EAAE,wBAAwB,CAAA;IAC9B,KAAK,EAAE,MAAM,GAAG,IAAI,CAAA;IACpB,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,QAAQ,CAAC,EAAE,QAAQ,GAAG,MAAM,CAAA;CAC7B;AAED,MAAM,WAAW,uBAAuB;IACtC,EAAE,EAAE,MAAM,CAAA;IACV,IAAI,CAAC,EAAE,wBAAwB,CAAA;IAC/B,KAAK,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;IACrB,IAAI,CAAC,EAAE,MAAM,CAAA;CACd"}
|
package/dist/types/campaign.d.ts
CHANGED
@@ -2,19 +2,21 @@ import { PromotionDTO } from "@medusajs/types";
|
|
2
2
|
import { Promotion } from "../models";
|
3
3
|
export interface CreateCampaignDTO {
|
4
4
|
name: string;
|
5
|
-
description?: string
|
5
|
+
description?: string;
|
6
|
+
currency?: string;
|
6
7
|
campaign_identifier: string;
|
7
|
-
starts_at
|
8
|
-
ends_at
|
8
|
+
starts_at: Date;
|
9
|
+
ends_at: Date;
|
9
10
|
promotions?: (PromotionDTO | Promotion)[];
|
10
11
|
}
|
11
12
|
export interface UpdateCampaignDTO {
|
12
13
|
id: string;
|
13
14
|
name?: string;
|
14
|
-
description?: string
|
15
|
+
description?: string;
|
16
|
+
currency?: string;
|
15
17
|
campaign_identifier?: string;
|
16
|
-
starts_at?: Date
|
17
|
-
ends_at?: Date
|
18
|
+
starts_at?: Date;
|
19
|
+
ends_at?: Date;
|
18
20
|
promotions?: (PromotionDTO | Promotion)[];
|
19
21
|
}
|
20
22
|
//# sourceMappingURL=campaign.d.ts.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"campaign.d.ts","sourceRoot":"","sources":["../../src/types/campaign.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAA;AAC9C,OAAO,EAAE,SAAS,EAAE,MAAM,SAAS,CAAA;AAEnC,MAAM,WAAW,iBAAiB;IAChC,IAAI,EAAE,MAAM,CAAA;IACZ,WAAW,CAAC,EAAE,MAAM,
|
1
|
+
{"version":3,"file":"campaign.d.ts","sourceRoot":"","sources":["../../src/types/campaign.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAA;AAC9C,OAAO,EAAE,SAAS,EAAE,MAAM,SAAS,CAAA;AAEnC,MAAM,WAAW,iBAAiB;IAChC,IAAI,EAAE,MAAM,CAAA;IACZ,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,mBAAmB,EAAE,MAAM,CAAA;IAC3B,SAAS,EAAE,IAAI,CAAA;IACf,OAAO,EAAE,IAAI,CAAA;IACb,UAAU,CAAC,EAAE,CAAC,YAAY,GAAG,SAAS,CAAC,EAAE,CAAA;CAC1C;AAED,MAAM,WAAW,iBAAiB;IAChC,EAAE,EAAE,MAAM,CAAA;IACV,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,mBAAmB,CAAC,EAAE,MAAM,CAAA;IAC5B,SAAS,CAAC,EAAE,IAAI,CAAA;IAChB,OAAO,CAAC,EAAE,IAAI,CAAA;IACd,UAAU,CAAC,EAAE,CAAC,YAAY,GAAG,SAAS,CAAC,EAAE,CAAA;CAC1C"}
|
@@ -3,13 +3,13 @@ export interface CreatePromotionDTO {
|
|
3
3
|
code: string;
|
4
4
|
type: PromotionTypeValues;
|
5
5
|
is_automatic?: boolean;
|
6
|
-
|
6
|
+
campaign?: string;
|
7
7
|
}
|
8
8
|
export interface UpdatePromotionDTO {
|
9
9
|
id: string;
|
10
10
|
code?: string;
|
11
11
|
type?: PromotionTypeValues;
|
12
12
|
is_automatic?: boolean;
|
13
|
-
|
13
|
+
campaign?: string;
|
14
14
|
}
|
15
15
|
//# sourceMappingURL=promotion.d.ts.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"promotion.d.ts","sourceRoot":"","sources":["../../src/types/promotion.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAA;AAErD,MAAM,WAAW,kBAAkB;IACjC,IAAI,EAAE,MAAM,CAAA;IACZ,IAAI,EAAE,mBAAmB,CAAA;IACzB,YAAY,CAAC,EAAE,OAAO,CAAA;IACtB,
|
1
|
+
{"version":3,"file":"promotion.d.ts","sourceRoot":"","sources":["../../src/types/promotion.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAA;AAErD,MAAM,WAAW,kBAAkB;IACjC,IAAI,EAAE,MAAM,CAAA;IACZ,IAAI,EAAE,mBAAmB,CAAA;IACzB,YAAY,CAAC,EAAE,OAAO,CAAA;IACtB,QAAQ,CAAC,EAAE,MAAM,CAAA;CAClB;AAED,MAAM,WAAW,kBAAkB;IACjC,EAAE,EAAE,MAAM,CAAA;IACV,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,IAAI,CAAC,EAAE,mBAAmB,CAAA;IAC1B,YAAY,CAAC,EAAE,OAAO,CAAA;IACtB,QAAQ,CAAC,EAAE,MAAM,CAAA;CAClB"}
|
@@ -1,5 +1,5 @@
|
|
1
|
-
import {
|
1
|
+
import { PromotionTypes } from "@medusajs/types";
|
2
2
|
import { ApplicationMethodTargetType } from "@medusajs/utils";
|
3
|
-
export declare function getComputedActionsForBuyGet(promotion: PromotionTypes.PromotionDTO, itemsContext: PromotionTypes.ComputeActionContext[ApplicationMethodTargetType.ITEMS], methodIdPromoValueMap: Map<string,
|
3
|
+
export declare function getComputedActionsForBuyGet(promotion: PromotionTypes.PromotionDTO, itemsContext: PromotionTypes.ComputeActionContext[ApplicationMethodTargetType.ITEMS], methodIdPromoValueMap: Map<string, number>): PromotionTypes.ComputeActions[];
|
4
4
|
export declare function sortByBuyGetType(a: any, b: any): 0 | 1 | -1;
|
5
5
|
//# sourceMappingURL=buy-get.d.ts.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"buy-get.d.ts","sourceRoot":"","sources":["../../../src/utils/compute-actions/buy-get.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,
|
1
|
+
{"version":3,"file":"buy-get.d.ts","sourceRoot":"","sources":["../../../src/utils/compute-actions/buy-get.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAA;AAChD,OAAO,EACL,2BAA2B,EAK5B,MAAM,iBAAiB,CAAA;AAKxB,wBAAgB,2BAA2B,CACzC,SAAS,EAAE,cAAc,CAAC,YAAY,EACtC,YAAY,EAAE,cAAc,CAAC,oBAAoB,CAAC,2BAA2B,CAAC,KAAK,CAAC,EACpF,qBAAqB,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,GACzC,cAAc,CAAC,cAAc,EAAE,CA6EjC;AAED,wBAAgB,gBAAgB,CAAC,CAAC,KAAA,EAAE,CAAC,KAAA,cAWpC"}
|