|
1
|
import { medusaIntegrationTestRunner } from "@medusajs/test-utils" |
|
2
|
import { |
|
3
|
createStoreCreditAccountsWorkflow, |
|
4
|
creditAccountsWorkflow, |
|
5
|
debitAccountsWorkflow, |
|
6
|
} from "@medusajs/loyalty-plugin/workflows" |
|
7
|
import { |
|
8
|
IStoreCreditModuleService, |
|
9
|
PluginModule, |
|
10
|
} from "@medusajs/loyalty-plugin/types" |
|
11
|
import { |
|
12
|
adminHeaders, |
|
13
|
createAdminUser, |
|
14
|
generatePublishableKey, |
|
15
|
generateStoreHeaders, |
|
16
|
} from "../../../../helpers/create-admin-user" |
|
17
|
import { createAuthenticatedCustomer } from "../../../helpers/create-authenticated-customer" |
|
18
|
|
|
19
|
jest.setTimeout(60 * 1000) |
|
20
|
|
|
21
|
medusaIntegrationTestRunner({ |
|
22
|
testSuite: ({ dbConnection, api, getContainer, dbUtils }) => { |
|
23
|
let customer, storeCreditAccount, storeHeaders |
|
24
|
|
|
25
|
beforeAll(async () => { |
|
26
|
await createAdminUser(dbConnection, adminHeaders, getContainer()) |
|
27
|
const publishableKey = await generatePublishableKey(getContainer()) |
|
28
|
storeHeaders = generateStoreHeaders({ publishableKey }) |
|
29
|
|
|
30
|
const user = await createAuthenticatedCustomer(api, storeHeaders, { |
|
31
|
email: "initial@customer.com", |
|
32
|
}) |
|
33
|
storeHeaders.headers["Authorization"] = `Bearer ${user.jwt}` |
|
34
|
customer = user.customer |
|
35
|
|
|
36
|
const { result: storeCreditAccounts } = |
|
37
|
await createStoreCreditAccountsWorkflow.run({ |
|
38
|
input: [ |
|
39
|
{ |
|
40
|
customer_id: customer.id, |
|
41
|
currency_code: "USD", |
|
42
|
}, |
|
43
|
], |
|
44
|
container: getContainer(), |
|
45
|
}) |
|
46
|
|
|
47
|
storeCreditAccount = storeCreditAccounts[0] |
|
48
|
|
|
49
|
await dbUtils.snapshot() |
|
50
|
}) |
|
51
|
|
|
52
|
describe("debitAccountsWorkflow", () => { |
|
53
|
beforeEach(async () => { |
|
54
|
await creditAccountsWorkflow.run({ |
|
55
|
input: [ |
|
56
|
{ |
|
57
|
account_id: storeCreditAccount.id, |
|
58
|
amount: 100, |
|
59
|
reference: "test", |
|
60
|
reference_id: "test-id", |
|
61
|
}, |
|
62
|
], |
|
63
|
container: getContainer(), |
|
64
|
}) |
|
65
|
}) |
|
66
|
|
|
67
|
it("successfully debits the account", async () => { |
|
68
|
const { result: transactions } = await debitAccountsWorkflow.run({ |
|
69
|
input: [ |
|
70
|
{ |
|
71
|
account_id: storeCreditAccount.id, |
|
72
|
amount: 100, |
|
73
|
reference: "test", |
|
74
|
reference_id: "test-id", |
|
75
|
}, |
|
76
|
], |
|
77
|
container: getContainer(), |
|
78
|
}) |
|
79
|
|
|
80
|
expect(transactions).toEqual([ |
|
81
|
expect.objectContaining({ |
|
82
|
account_id: storeCreditAccount.id, |
|
83
|
amount: 100, |
|
84
|
reference: "test", |
|
85
|
reference_id: "test-id", |
|
86
|
}), |
|
87
|
]) |
|
88
|
}) |
|
89
|
|
|
90
|
it("should throw if the amount is greater than the account balance", async () => { |
|
91
|
const { errors } = await debitAccountsWorkflow.run({ |
|
92
|
input: [ |
|
93
|
{ |
|
94
|
account_id: storeCreditAccount.id, |
|
95
|
amount: 201, |
|
96
|
reference: "test", |
|
97
|
reference_id: "test-id", |
|
98
|
}, |
|
99
|
], |
|
100
|
container: getContainer(), |
|
101
|
throwOnError: false, |
|
102
|
}) |
|
103
|
|
|
104
|
expect(errors).toEqual([ |
|
105
|
expect.objectContaining({ |
|
106
|
error: expect.objectContaining({ |
|
107
|
message: "Insufficient balance", |
|
108
|
type: "invalid_data", |
|
109
|
}), |
|
110
|
}), |
|
111
|
]) |
|
112
|
}) |
|
113
|
|
|
114
|
it("should throw if account does not exist", async () => { |
|
115
|
const { errors } = await debitAccountsWorkflow.run({ |
|
116
|
input: [ |
|
117
|
{ |
|
118
|
account_id: "does-not-exist", |
|
119
|
amount: 201, |
|
120
|
reference: "test", |
|
121
|
reference_id: "test-id", |
|
122
|
}, |
|
123
|
], |
|
124
|
container: getContainer(), |
|
125
|
throwOnError: false, |
|
126
|
}) |
|
127
|
|
|
128
|
expect(errors).toEqual([ |
|
129
|
expect.objectContaining({ |
|
130
|
error: expect.objectContaining({ |
|
131
|
message: |
|
132
|
"StoreCreditAccount with id: does-not-exist was not found", |
|
133
|
type: "not_found", |
|
134
|
}), |
|
135
|
}), |
|
136
|
]) |
|
137
|
}) |
|
138
|
|
|
139
|
it("should throw if amount does not exist", async () => { |
|
140
|
const { errors } = await debitAccountsWorkflow.run({ |
|
141
|
input: [ |
|
142
|
{ |
|
143
|
account_id: storeCreditAccount.id, |
|
144
|
reference: "test", |
|
145
|
reference_id: "test-id", |
|
146
|
}, |
|
147
|
], |
|
148
|
container: getContainer(), |
|
149
|
throwOnError: false, |
|
150
|
}) |
|
151
|
|
|
152
|
expect(errors).toEqual([ |
|
153
|
expect.objectContaining({ |
|
154
|
error: expect.objectContaining({ |
|
155
|
message: "Amount is required", |
|
156
|
type: "invalid_data", |
|
157
|
}), |
|
158
|
}), |
|
159
|
]) |
|
160
|
}) |
|
161
|
}) |
|
162
|
|
|
163
|
describe("retrieveAccountStats", () => { |
|
164
|
// `softDeleteAccountTransactions` is generated by `MedusaService` but is |
|
165
|
// not declared on `IStoreCreditModuleService`. |
|
166
|
let storeCreditModule: IStoreCreditModuleService & { |
|
167
|
softDeleteAccountTransactions: (ids: string[]) => Promise<void> |
|
168
|
} |
|
169
|
|
|
170
|
beforeEach(() => { |
|
171
|
storeCreditModule = getContainer().resolve(PluginModule.STORE_CREDIT) |
|
172
|
}) |
|
173
|
|
|
174
|
it("should return a zero balance for an account without transactions", async () => { |
|
175
|
const stats = await storeCreditModule.retrieveAccountStats({ |
|
176
|
account_id: storeCreditAccount.id, |
|
177
|
}) |
|
178
|
|
|
179
|
expect(stats).toEqual({ |
|
180
|
id: storeCreditAccount.id, |
|
181
|
balance: 0, |
|
182
|
credits: 0, |
|
183
|
debits: 0, |
|
184
|
}) |
|
185
|
}) |
|
186
|
|
|
187
|
it("should exclude soft deleted transactions from the balance", async () => { |
|
188
|
const { result: credits } = await creditAccountsWorkflow.run({ |
|
189
|
input: [ |
|
190
|
{ |
|
191
|
account_id: storeCreditAccount.id, |
|
192
|
amount: 100, |
|
193
|
reference: "test", |
|
194
|
reference_id: "test-id", |
|
195
|
}, |
|
196
|
{ |
|
197
|
account_id: storeCreditAccount.id, |
|
198
|
amount: 50, |
|
199
|
reference: "test", |
|
200
|
reference_id: "test-id-2", |
|
201
|
}, |
|
202
|
], |
|
203
|
container: getContainer(), |
|
204
|
}) |
|
205
|
|
|
206
|
const { result: debits } = await debitAccountsWorkflow.run({ |
|
207
|
input: [ |
|
208
|
{ |
|
209
|
account_id: storeCreditAccount.id, |
|
210
|
amount: 30, |
|
211
|
reference: "test", |
|
212
|
reference_id: "test-id-3", |
|
213
|
}, |
|
214
|
], |
|
215
|
container: getContainer(), |
|
216
|
}) |
|
217
|
|
|
218
|
await storeCreditModule.softDeleteAccountTransactions([credits[1].id]) |
|
219
|
|
|
220
|
expect( |
|
221
|
await storeCreditModule.retrieveAccountStats({ |
|
222
|
account_id: storeCreditAccount.id, |
|
223
|
}) |
|
224
|
).toEqual({ |
|
225
|
id: storeCreditAccount.id, |
|
226
|
balance: 70, |
|
227
|
credits: 100, |
|
228
|
debits: 30, |
|
229
|
}) |
|
230
|
|
|
231
|
await storeCreditModule.softDeleteAccountTransactions([debits[0].id]) |
|
232
|
|
|
233
|
expect( |
|
234
|
await storeCreditModule.retrieveAccountStats({ |
|
235
|
account_id: storeCreditAccount.id, |
|
236
|
}) |
|
237
|
).toEqual({ |
|
238
|
id: storeCreditAccount.id, |
|
239
|
balance: 100, |
|
240
|
credits: 100, |
|
241
|
debits: 0, |
|
242
|
}) |
|
243
|
}) |
|
244
|
|
|
245
|
it("should not spend the value of a soft deleted credit", async () => { |
|
246
|
const { result: credits } = await creditAccountsWorkflow.run({ |
|
247
|
input: [ |
|
248
|
{ |
|
249
|
account_id: storeCreditAccount.id, |
|
250
|
amount: 100, |
|
251
|
reference: "test", |
|
252
|
reference_id: "test-id", |
|
253
|
}, |
|
254
|
], |
|
255
|
container: getContainer(), |
|
256
|
}) |
|
257
|
|
|
258
|
await storeCreditModule.softDeleteAccountTransactions([credits[0].id]) |
|
259
|
|
|
260
|
const { errors } = await debitAccountsWorkflow.run({ |
|
261
|
input: [ |
|
262
|
{ |
|
263
|
account_id: storeCreditAccount.id, |
|
264
|
amount: 100, |
|
265
|
reference: "test", |
|
266
|
reference_id: "test-id-2", |
|
267
|
}, |
|
268
|
], |
|
269
|
container: getContainer(), |
|
270
|
throwOnError: false, |
|
271
|
}) |
|
272
|
|
|
273
|
expect(errors).toEqual([ |
|
274
|
expect.objectContaining({ |
|
275
|
error: expect.objectContaining({ |
|
276
|
message: "Insufficient balance", |
|
277
|
type: "invalid_data", |
|
278
|
}), |
|
279
|
}), |
|
280
|
]) |
|
281
|
}) |
|
282
|
}) |
|
283
|
}, |
|
284
|
}) |