|
1
|
import { IPromotionModuleService } from "@medusajs/framework/types" |
|
2
|
import { Modules } from "@medusajs/framework/utils" |
|
3
|
import { moduleIntegrationTestRunner, SuiteOptions } from "@medusajs/test-utils" |
|
4
|
import { createCampaigns } from "../../../__fixtures__/campaigns" |
|
5
|
import { createDefaultPromotion } from "../../../__fixtures__/promotion" |
|
6
|
|
|
7
|
jest.setTimeout(30000) |
|
8
|
|
|
9
|
moduleIntegrationTestRunner({ |
|
10
|
moduleName: Modules.PROMOTION, |
|
11
|
testSuite: ({ |
|
12
|
MikroOrmWrapper, |
|
13
|
service, |
|
14
|
}: SuiteOptions<IPromotionModuleService>) => { |
|
15
|
describe("Promotion Service: campaign usage", () => { |
|
16
|
beforeEach(async () => { |
|
17
|
await createCampaigns(MikroOrmWrapper.forkManager()) |
|
18
|
}) |
|
19
|
|
|
20
|
describe("revertUsage", () => { |
|
21
|
it("should revert usage for type spend", async () => { |
|
22
|
const createdPromotion = await createDefaultPromotion(service, {}) |
|
23
|
const createdPromotion2 = await createDefaultPromotion(service, { |
|
24
|
code: "PROMO_2", |
|
25
|
campaign_id: createdPromotion.campaign?.id, |
|
26
|
}) |
|
27
|
const createdPromotion3 = await createDefaultPromotion(service, { |
|
28
|
code: "PROMO_3", |
|
29
|
campaign_id: createdPromotion.campaign?.id, |
|
30
|
}) |
|
31
|
|
|
32
|
await service.registerUsage([ |
|
33
|
{ amount: 200, code: createdPromotion.code! }, |
|
34
|
{ amount: 100, code: createdPromotion.code! }, |
|
35
|
]) |
|
36
|
|
|
37
|
await service.registerUsage([ |
|
38
|
{ amount: 100, code: createdPromotion2.code! }, |
|
39
|
{ amount: 200, code: createdPromotion2.code! }, |
|
40
|
]) |
|
41
|
|
|
42
|
await service.registerUsage([ |
|
43
|
{ amount: 50, code: createdPromotion3.code! }, |
|
44
|
{ amount: 250, code: createdPromotion3.code! }, |
|
45
|
]) |
|
46
|
|
|
47
|
await service.revertUsage([ |
|
48
|
{ amount: 200, code: createdPromotion.code! }, |
|
49
|
{ amount: 100, code: createdPromotion.code! }, |
|
50
|
]) |
|
51
|
|
|
52
|
await service.revertUsage([ |
|
53
|
{ amount: 50, code: createdPromotion3.code! }, |
|
54
|
{ amount: 250, code: createdPromotion3.code! }, |
|
55
|
]) |
|
56
|
|
|
57
|
const campaign = await service.retrieveCampaign( |
|
58
|
createdPromotion.campaign?.id!, |
|
59
|
{ relations: ["budget"] } |
|
60
|
) |
|
61
|
|
|
62
|
expect(campaign.budget).toEqual( |
|
63
|
expect.objectContaining({ |
|
64
|
type: "spend", |
|
65
|
limit: 1000, |
|
66
|
used: 300, |
|
67
|
}) |
|
68
|
) |
|
69
|
}) |
|
70
|
|
|
71
|
it("should revert usage for type usage", async () => { |
|
72
|
const campaignId = "campaign-id-2" |
|
73
|
const createdPromotion = await createDefaultPromotion(service, { |
|
74
|
code: "PROMO_1", |
|
75
|
campaign_id: campaignId, |
|
76
|
}) |
|
77
|
const createdPromotion2 = await createDefaultPromotion(service, { |
|
78
|
code: "PROMO_2", |
|
79
|
campaign_id: campaignId, |
|
80
|
}) |
|
81
|
const createdPromotion3 = await createDefaultPromotion(service, { |
|
82
|
code: "PROMO_3", |
|
83
|
campaign_id: campaignId, |
|
84
|
}) |
|
85
|
|
|
86
|
await service.registerUsage([ |
|
87
|
{ amount: 200, code: createdPromotion.code! }, |
|
88
|
{ amount: 500, code: createdPromotion.code! }, |
|
89
|
]) |
|
90
|
|
|
91
|
await service.registerUsage([ |
|
92
|
{ amount: 200, code: createdPromotion2.code! }, |
|
93
|
{ amount: 500, code: createdPromotion3.code! }, |
|
94
|
]) |
|
95
|
|
|
96
|
await service.revertUsage([ |
|
97
|
{ amount: 200, code: createdPromotion.code! }, |
|
98
|
{ amount: 500, code: createdPromotion.code! }, |
|
99
|
]) |
|
100
|
|
|
101
|
await service.revertUsage([ |
|
102
|
{ amount: 200, code: createdPromotion2.code! }, |
|
103
|
{ amount: 500, code: createdPromotion3.code! }, |
|
104
|
]) |
|
105
|
|
|
106
|
const campaign = await service.retrieveCampaign(campaignId, { |
|
107
|
relations: ["budget"], |
|
108
|
}) |
|
109
|
|
|
110
|
expect(campaign.budget).toEqual( |
|
111
|
expect.objectContaining({ |
|
112
|
type: "usage", |
|
113
|
limit: 1000, |
|
114
|
used: 0, |
|
115
|
}) |
|
116
|
) |
|
117
|
}) |
|
118
|
|
|
119
|
it("should not throw an error when compute action with code does not exist", async () => { |
|
120
|
const response = await service |
|
121
|
.revertUsage([{ amount: 200, code: "DOESNOTEXIST" }]) |
|
122
|
.catch((e) => e) |
|
123
|
|
|
124
|
expect(response).toEqual(undefined) |
|
125
|
}) |
|
126
|
}) |
|
127
|
}) |
|
128
|
}, |
|
129
|
}) |