|
1
|
import { IPromotionModuleService } from "@medusajs/framework/types" |
|
2
|
import { Modules } from "@medusajs/framework/utils" |
|
3
|
import { moduleIntegrationTestRunner } from "@medusajs/test-utils" |
|
4
|
import { CampaignBudgetType } from "../../../../../../core/utils/src/promotion/index" |
|
5
|
import { createCampaigns } from "../../../__fixtures__/campaigns" |
|
6
|
import { |
|
7
|
createDefaultPromotion, |
|
8
|
createPromotions, |
|
9
|
} from "../../../__fixtures__/promotion" |
|
10
|
|
|
11
|
jest.setTimeout(30000) |
|
12
|
|
|
13
|
moduleIntegrationTestRunner<IPromotionModuleService>({ |
|
14
|
moduleName: Modules.PROMOTION, |
|
15
|
testSuite: ({ MikroOrmWrapper, service }) => { |
|
16
|
describe("Promotion Module Service: Campaigns", () => { |
|
17
|
describe("listAndCountCampaigns", () => { |
|
18
|
beforeEach(async () => { |
|
19
|
await createCampaigns(MikroOrmWrapper.forkManager()) |
|
20
|
}) |
|
21
|
|
|
22
|
it("should return all campaigns and its count", async () => { |
|
23
|
const [campaigns, count] = await service.listAndCountCampaigns() |
|
24
|
|
|
25
|
expect(count).toEqual(2) |
|
26
|
expect(campaigns).toEqual([ |
|
27
|
{ |
|
28
|
id: "campaign-id-1", |
|
29
|
name: "campaign 1", |
|
30
|
description: "test description", |
|
31
|
|
|
32
|
campaign_identifier: "test-1", |
|
33
|
starts_at: expect.any(Date), |
|
34
|
ends_at: expect.any(Date), |
|
35
|
budget: expect.any(Object), |
|
36
|
created_at: expect.any(Date), |
|
37
|
updated_at: expect.any(Date), |
|
38
|
deleted_at: null, |
|
39
|
}, |
|
40
|
{ |
|
41
|
id: "campaign-id-2", |
|
42
|
name: "campaign 1", |
|
43
|
description: "test description", |
|
44
|
|
|
45
|
campaign_identifier: "test-2", |
|
46
|
starts_at: expect.any(Date), |
|
47
|
ends_at: expect.any(Date), |
|
48
|
budget: expect.any(Object), |
|
49
|
created_at: expect.any(Date), |
|
50
|
updated_at: expect.any(Date), |
|
51
|
deleted_at: null, |
|
52
|
}, |
|
53
|
]) |
|
54
|
}) |
|
55
|
|
|
56
|
it("should return all campaigns based on config select and relations param", async () => { |
|
57
|
const [campaigns, count] = await service.listAndCountCampaigns( |
|
58
|
{ |
|
59
|
id: ["campaign-id-1"], |
|
60
|
}, |
|
61
|
{ |
|
62
|
relations: ["budget"], |
|
63
|
select: ["name", "budget.limit"], |
|
64
|
} |
|
65
|
) |
|
66
|
|
|
67
|
expect(count).toEqual(1) |
|
68
|
expect(campaigns).toEqual([ |
|
69
|
{ |
|
70
|
id: "campaign-id-1", |
|
71
|
name: "campaign 1", |
|
72
|
budget: expect.objectContaining({ |
|
73
|
id: expect.any(String), |
|
74
|
limit: 1000, |
|
75
|
}), |
|
76
|
}, |
|
77
|
]) |
|
78
|
}) |
|
79
|
}) |
|
80
|
|
|
81
|
describe("createCampaigns", () => { |
|
82
|
it("should throw an error when required params are not passed", async () => { |
|
83
|
const error = await service |
|
84
|
.createCampaigns([ |
|
85
|
{ |
|
86
|
name: "test", |
|
87
|
} as any, |
|
88
|
]) |
|
89
|
.catch((e) => e) |
|
90
|
|
|
91
|
expect(error.message).toContain( |
|
92
|
"Value for Campaign.campaign_identifier is required, 'undefined' found" |
|
93
|
) |
|
94
|
}) |
|
95
|
|
|
96
|
it("should throw an error when required budget params are not met", async () => { |
|
97
|
const error = await service |
|
98
|
.createCampaigns([ |
|
99
|
{ |
|
100
|
name: "test", |
|
101
|
campaign_identifier: "test", |
|
102
|
budget: { |
|
103
|
limit: 1000, |
|
104
|
type: "spend", |
|
105
|
used: 10, |
|
106
|
}, |
|
107
|
}, |
|
108
|
]) |
|
109
|
.catch((e) => e) |
|
110
|
|
|
111
|
expect(error.message).toContain( |
|
112
|
"Campaign Budget type is a required field" |
|
113
|
) |
|
114
|
}) |
|
115
|
|
|
116
|
it("should create a basic campaign successfully", async () => { |
|
117
|
const startsAt = new Date("01/01/2024") |
|
118
|
const endsAt = new Date("01/01/2025") |
|
119
|
const [createdCampaign] = await service.createCampaigns([ |
|
120
|
{ |
|
121
|
name: "test", |
|
122
|
campaign_identifier: "test", |
|
123
|
starts_at: startsAt, |
|
124
|
ends_at: endsAt, |
|
125
|
}, |
|
126
|
]) |
|
127
|
|
|
128
|
const campaign = await service.retrieveCampaign(createdCampaign.id) |
|
129
|
|
|
130
|
expect(campaign).toEqual( |
|
131
|
expect.objectContaining({ |
|
132
|
name: "test", |
|
133
|
campaign_identifier: "test", |
|
134
|
starts_at: startsAt, |
|
135
|
ends_at: endsAt, |
|
136
|
}) |
|
137
|
) |
|
138
|
}) |
|
139
|
|
|
140
|
it("should create a campaign with campaign budget successfully", async () => { |
|
141
|
const startsAt = new Date("01/01/2024") |
|
142
|
const endsAt = new Date("01/01/2025") |
|
143
|
|
|
144
|
const [createdPromotion] = await service.createCampaigns([ |
|
145
|
{ |
|
146
|
name: "test", |
|
147
|
campaign_identifier: "test", |
|
148
|
starts_at: startsAt, |
|
149
|
ends_at: endsAt, |
|
150
|
budget: { |
|
151
|
limit: 1000, |
|
152
|
type: "usage", |
|
153
|
used: 10, |
|
154
|
}, |
|
155
|
}, |
|
156
|
]) |
|
157
|
|
|
158
|
const campaign = await service.retrieveCampaign(createdPromotion.id, { |
|
159
|
relations: ["budget"], |
|
160
|
}) |
|
161
|
|
|
162
|
expect(campaign).toEqual( |
|
163
|
expect.objectContaining({ |
|
164
|
name: "test", |
|
165
|
campaign_identifier: "test", |
|
166
|
starts_at: startsAt, |
|
167
|
ends_at: endsAt, |
|
168
|
budget: expect.objectContaining({ |
|
169
|
limit: 1000, |
|
170
|
type: "usage", |
|
171
|
used: 10, |
|
172
|
}), |
|
173
|
}) |
|
174
|
) |
|
175
|
}) |
|
176
|
|
|
177
|
it("should create a campaign with a campaign identitifer of a deleted campaign", async () => { |
|
178
|
const campaign = await service.createCampaigns({ |
|
179
|
name: "test", |
|
180
|
campaign_identifier: "test", |
|
181
|
}) |
|
182
|
|
|
183
|
await service.softDeleteCampaigns(campaign.id) |
|
184
|
|
|
185
|
const recreatedCampaign = await service.createCampaigns({ |
|
186
|
name: "test", |
|
187
|
campaign_identifier: "test", |
|
188
|
}) |
|
189
|
|
|
190
|
expect(recreatedCampaign).toEqual( |
|
191
|
expect.objectContaining({ |
|
192
|
name: "test", |
|
193
|
campaign_identifier: "test", |
|
194
|
}) |
|
195
|
) |
|
196
|
|
|
197
|
const error = await service |
|
198
|
.restoreCampaigns(campaign.id) |
|
199
|
.catch((e) => e) |
|
200
|
|
|
201
|
expect(error.message).toEqual( |
|
202
|
"Promotion campaign with campaign_identifier: test, already exists." |
|
203
|
) |
|
204
|
}) |
|
205
|
}) |
|
206
|
|
|
207
|
describe("updateCampaigns", () => { |
|
208
|
it("should throw an error when required params are not passed", async () => { |
|
209
|
const error = await service |
|
210
|
.updateCampaigns([ |
|
211
|
{ |
|
212
|
name: "test", |
|
213
|
} as any, |
|
214
|
]) |
|
215
|
.catch((e) => e) |
|
216
|
|
|
217
|
expect(error.message).toContain('Campaign with id "" not found') |
|
218
|
}) |
|
219
|
|
|
220
|
it("should update the attributes of a campaign successfully", async () => { |
|
221
|
await createCampaigns(MikroOrmWrapper.forkManager()) |
|
222
|
|
|
223
|
const [updatedCampaign] = await service.updateCampaigns([ |
|
224
|
{ |
|
225
|
id: "campaign-id-1", |
|
226
|
description: "test description 1", |
|
227
|
campaign_identifier: "new", |
|
228
|
starts_at: new Date("01/01/2024"), |
|
229
|
ends_at: new Date("01/01/2025"), |
|
230
|
}, |
|
231
|
]) |
|
232
|
|
|
233
|
expect(updatedCampaign).toEqual( |
|
234
|
expect.objectContaining({ |
|
235
|
description: "test description 1", |
|
236
|
campaign_identifier: "new", |
|
237
|
starts_at: new Date("01/01/2024"), |
|
238
|
ends_at: new Date("01/01/2025"), |
|
239
|
}) |
|
240
|
) |
|
241
|
}) |
|
242
|
|
|
243
|
it("should update the attributes of a campaign budget successfully", async () => { |
|
244
|
await createCampaigns(MikroOrmWrapper.forkManager()) |
|
245
|
|
|
246
|
const [updatedCampaign] = await service.updateCampaigns([ |
|
247
|
{ |
|
248
|
id: "campaign-id-1", |
|
249
|
budget: { |
|
250
|
limit: 100, |
|
251
|
used: 100, |
|
252
|
}, |
|
253
|
}, |
|
254
|
]) |
|
255
|
|
|
256
|
expect(updatedCampaign).toEqual( |
|
257
|
expect.objectContaining({ |
|
258
|
budget: expect.objectContaining({ |
|
259
|
limit: 100, |
|
260
|
used: 100, |
|
261
|
}), |
|
262
|
}) |
|
263
|
) |
|
264
|
}) |
|
265
|
|
|
266
|
it("should create a campaign budget if not present successfully", async () => { |
|
267
|
await createCampaigns(MikroOrmWrapper.forkManager(), [ |
|
268
|
{ |
|
269
|
id: "campaign-id-new", |
|
270
|
name: "campaign 1", |
|
271
|
description: "test description", |
|
272
|
campaign_identifier: "test-1", |
|
273
|
} as any, |
|
274
|
]) |
|
275
|
|
|
276
|
const [updatedCampaign] = await service.updateCampaigns([ |
|
277
|
{ |
|
278
|
id: "campaign-id-new", |
|
279
|
budget: { |
|
280
|
type: CampaignBudgetType.SPEND, |
|
281
|
limit: 100, |
|
282
|
used: 100, |
|
283
|
}, |
|
284
|
}, |
|
285
|
]) |
|
286
|
|
|
287
|
expect(updatedCampaign).toEqual( |
|
288
|
expect.objectContaining({ |
|
289
|
budget: expect.objectContaining({ |
|
290
|
limit: 100, |
|
291
|
used: 100, |
|
292
|
}), |
|
293
|
}) |
|
294
|
) |
|
295
|
}) |
|
296
|
}) |
|
297
|
|
|
298
|
describe("retrieveCampaign", () => { |
|
299
|
beforeEach(async () => { |
|
300
|
await createCampaigns(MikroOrmWrapper.forkManager()) |
|
301
|
}) |
|
302
|
|
|
303
|
const id = "campaign-id-1" |
|
304
|
|
|
305
|
it("should return campaign for the given id", async () => { |
|
306
|
const campaign = await service.retrieveCampaign(id) |
|
307
|
|
|
308
|
expect(campaign).toEqual( |
|
309
|
expect.objectContaining({ |
|
310
|
id, |
|
311
|
}) |
|
312
|
) |
|
313
|
}) |
|
314
|
|
|
315
|
it("should throw an error when campaign with id does not exist", async () => { |
|
316
|
let error |
|
317
|
|
|
318
|
try { |
|
319
|
await service.retrieveCampaign("does-not-exist") |
|
320
|
} catch (e) { |
|
321
|
error = e |
|
322
|
} |
|
323
|
|
|
324
|
expect(error.message).toEqual( |
|
325
|
"Campaign with id: does-not-exist was not found" |
|
326
|
) |
|
327
|
}) |
|
328
|
|
|
329
|
it("should throw an error when a id is not provided", async () => { |
|
330
|
let error |
|
331
|
|
|
332
|
try { |
|
333
|
await service.retrieveCampaign(undefined as unknown as string) |
|
334
|
} catch (e) { |
|
335
|
error = e |
|
336
|
} |
|
337
|
|
|
338
|
expect(error.message).toEqual("campaign - id must be defined") |
|
339
|
}) |
|
340
|
|
|
341
|
it("should return campaign based on config select param", async () => { |
|
342
|
const campaign = await service.retrieveCampaign(id, { |
|
343
|
select: ["id"], |
|
344
|
}) |
|
345
|
|
|
346
|
const serialized = JSON.parse(JSON.stringify(campaign)) |
|
347
|
|
|
348
|
expect(serialized).toEqual({ |
|
349
|
id, |
|
350
|
}) |
|
351
|
}) |
|
352
|
}) |
|
353
|
|
|
354
|
describe("deleteCampaigns", () => { |
|
355
|
it("should delete the campaigns given an id successfully", async () => { |
|
356
|
const [createdCampaign] = await service.createCampaigns([ |
|
357
|
{ |
|
358
|
name: "test", |
|
359
|
campaign_identifier: "test", |
|
360
|
starts_at: new Date("01/01/2024"), |
|
361
|
ends_at: new Date("01/01/2025"), |
|
362
|
}, |
|
363
|
]) |
|
364
|
|
|
365
|
await service.deleteCampaigns([createdCampaign.id]) |
|
366
|
|
|
367
|
const campaigns = await service.listCampaigns( |
|
368
|
{ |
|
369
|
id: [createdCampaign.id], |
|
370
|
}, |
|
371
|
{ withDeleted: true } |
|
372
|
) |
|
373
|
|
|
374
|
expect(campaigns).toHaveLength(0) |
|
375
|
}) |
|
376
|
}) |
|
377
|
|
|
378
|
describe("softDeleteCampaigns", () => { |
|
379
|
it("should soft delete the campaigns given an id successfully", async () => { |
|
380
|
const [createdCampaign] = await service.createCampaigns([ |
|
381
|
{ |
|
382
|
name: "test", |
|
383
|
campaign_identifier: "test", |
|
384
|
starts_at: new Date("01/01/2024"), |
|
385
|
ends_at: new Date("01/01/2025"), |
|
386
|
}, |
|
387
|
]) |
|
388
|
|
|
389
|
await service.softDeleteCampaigns([createdCampaign.id]) |
|
390
|
|
|
391
|
const campaigns = await service.listCampaigns({ |
|
392
|
id: [createdCampaign.id], |
|
393
|
}) |
|
394
|
|
|
395
|
expect(campaigns).toHaveLength(0) |
|
396
|
}) |
|
397
|
}) |
|
398
|
|
|
399
|
describe("restoreCampaigns", () => { |
|
400
|
it("should restore the campaigns given an id successfully", async () => { |
|
401
|
const [createdCampaign] = await service.createCampaigns([ |
|
402
|
{ |
|
403
|
name: "test", |
|
404
|
campaign_identifier: "test", |
|
405
|
starts_at: new Date("01/01/2024"), |
|
406
|
ends_at: new Date("01/01/2025"), |
|
407
|
}, |
|
408
|
]) |
|
409
|
|
|
410
|
await service.softDeleteCampaigns([createdCampaign.id]) |
|
411
|
|
|
412
|
let campaigns = await service.listCampaigns({ |
|
413
|
id: [createdCampaign.id], |
|
414
|
}) |
|
415
|
|
|
416
|
expect(campaigns).toHaveLength(0) |
|
417
|
await service.restoreCampaigns([createdCampaign.id]) |
|
418
|
|
|
419
|
campaigns = await service.listCampaigns({ id: [createdCampaign.id] }) |
|
420
|
expect(campaigns).toHaveLength(1) |
|
421
|
}) |
|
422
|
}) |
|
423
|
|
|
424
|
describe("addPromotionsToCampaign", () => { |
|
425
|
beforeEach(async () => { |
|
426
|
await createCampaigns(MikroOrmWrapper.forkManager()) |
|
427
|
await createPromotions(MikroOrmWrapper.forkManager()) |
|
428
|
|
|
429
|
await service.addPromotionsToCampaign({ |
|
430
|
id, |
|
431
|
promotion_ids: ["promotion-id-1"], |
|
432
|
}) |
|
433
|
}) |
|
434
|
|
|
435
|
const id = "campaign-id-1" |
|
436
|
|
|
437
|
it("should add promotions to a campaign", async () => { |
|
438
|
await service.addPromotionsToCampaign({ |
|
439
|
id, |
|
440
|
promotion_ids: ["promotion-id-2"], |
|
441
|
}) |
|
442
|
|
|
443
|
const campaign = await service.retrieveCampaign(id, { |
|
444
|
relations: ["promotions"], |
|
445
|
}) |
|
446
|
|
|
447
|
expect(campaign.promotions).toHaveLength(2) |
|
448
|
expect(campaign).toEqual( |
|
449
|
expect.objectContaining({ |
|
450
|
id, |
|
451
|
promotions: expect.arrayContaining([ |
|
452
|
expect.objectContaining({ id: "promotion-id-1" }), |
|
453
|
expect.objectContaining({ id: "promotion-id-2" }), |
|
454
|
]), |
|
455
|
}) |
|
456
|
) |
|
457
|
}) |
|
458
|
}) |
|
459
|
|
|
460
|
describe("removePromotionsFromCampaign", () => { |
|
461
|
beforeEach(async () => { |
|
462
|
await createCampaigns(MikroOrmWrapper.forkManager()) |
|
463
|
await createPromotions(MikroOrmWrapper.forkManager()) |
|
464
|
|
|
465
|
await service.addPromotionsToCampaign({ |
|
466
|
id, |
|
467
|
promotion_ids: ["promotion-id-1", "promotion-id-2"], |
|
468
|
}) |
|
469
|
}) |
|
470
|
|
|
471
|
const id = "campaign-id-1" |
|
472
|
|
|
473
|
it("should remove promotions to a campaign", async () => { |
|
474
|
await service.removePromotionsFromCampaign({ |
|
475
|
id, |
|
476
|
promotion_ids: ["promotion-id-1"], |
|
477
|
}) |
|
478
|
|
|
479
|
const campaign = await service.retrieveCampaign(id, { |
|
480
|
relations: ["promotions"], |
|
481
|
}) |
|
482
|
|
|
483
|
expect(campaign.promotions).toHaveLength(1) |
|
484
|
expect(campaign).toEqual( |
|
485
|
expect.objectContaining({ |
|
486
|
id, |
|
487
|
promotions: expect.arrayContaining([ |
|
488
|
expect.objectContaining({ id: "promotion-id-2" }), |
|
489
|
]), |
|
490
|
}) |
|
491
|
) |
|
492
|
}) |
|
493
|
}) |
|
494
|
|
|
495
|
describe("campaignBudgetUsage", () => { |
|
496
|
it("should create a campaign budget by attribute usage successfully", async () => { |
|
497
|
const [createdCampaign] = await service.createCampaigns([ |
|
498
|
{ |
|
499
|
name: "test", |
|
500
|
campaign_identifier: "test", |
|
501
|
budget: { |
|
502
|
type: CampaignBudgetType.USE_BY_ATTRIBUTE, |
|
503
|
attribute: "customer_id", |
|
504
|
limit: 5, |
|
505
|
}, |
|
506
|
}, |
|
507
|
]) |
|
508
|
|
|
509
|
let campaigns = await service.listCampaigns( |
|
510
|
{ |
|
511
|
id: [createdCampaign.id], |
|
512
|
}, |
|
513
|
{ relations: ["budget", "budget.usages"] } |
|
514
|
) |
|
515
|
|
|
516
|
expect(campaigns).toHaveLength(1) |
|
517
|
|
|
518
|
expect(campaigns[0]).toEqual( |
|
519
|
expect.objectContaining({ |
|
520
|
budget: expect.objectContaining({ |
|
521
|
usages: [], |
|
522
|
limit: 5, |
|
523
|
type: CampaignBudgetType.USE_BY_ATTRIBUTE, |
|
524
|
}), |
|
525
|
}) |
|
526
|
) |
|
527
|
}) |
|
528
|
}) |
|
529
|
}) |
|
530
|
}, |
|
531
|
}) |