|
1
|
import { medusaIntegrationTestRunner } from "@medusajs/test-utils" |
|
2
|
import { |
|
3
|
adminHeaders, |
|
4
|
createAdminUser, |
|
5
|
} from "../../../../helpers/create-admin-user" |
|
6
|
|
|
7
|
jest.setTimeout(60 * 1000) |
|
8
|
|
|
9
|
const storeCreditAccountPayload = { |
|
10
|
customer_id: "cus_123", |
|
11
|
currency_code: "usd", |
|
12
|
} |
|
13
|
|
|
14
|
const storeCreditAccountResponse = { |
|
15
|
id: expect.any(String), |
|
16
|
customer_id: "cus_123", |
|
17
|
currency_code: "usd", |
|
18
|
metadata: null, |
|
19
|
} |
|
20
|
|
|
21
|
medusaIntegrationTestRunner({ |
|
22
|
testSuite: ({ dbConnection, api, getContainer, dbUtils }) => { |
|
23
|
let adminUser |
|
24
|
|
|
25
|
beforeAll(async () => { |
|
26
|
const user = await createAdminUser( |
|
27
|
dbConnection, |
|
28
|
adminHeaders, |
|
29
|
getContainer() |
|
30
|
) |
|
31
|
adminUser = user.user |
|
32
|
|
|
33
|
await dbUtils.snapshot() |
|
34
|
}) |
|
35
|
|
|
36
|
describe("GET /admin/store-credit-accounts", () => { |
|
37
|
let customer1, customer2 |
|
38
|
|
|
39
|
beforeEach(async () => { |
|
40
|
customer1 = ( |
|
41
|
await api.post( |
|
42
|
`/admin/customers`, |
|
43
|
{ email: "test@test.com" }, |
|
44
|
adminHeaders |
|
45
|
) |
|
46
|
).data.customer |
|
47
|
|
|
48
|
await api.post( |
|
49
|
`/admin/store-credit-accounts`, |
|
50
|
{ ...storeCreditAccountPayload, customer_id: customer1.id }, |
|
51
|
adminHeaders |
|
52
|
) |
|
53
|
|
|
54
|
customer2 = ( |
|
55
|
await api.post( |
|
56
|
`/admin/customers`, |
|
57
|
{ email: "test2@test.com" }, |
|
58
|
adminHeaders |
|
59
|
) |
|
60
|
).data.customer |
|
61
|
|
|
62
|
await api.post( |
|
63
|
`/admin/store-credit-accounts`, |
|
64
|
{ ...storeCreditAccountPayload, customer_id: customer2.id }, |
|
65
|
adminHeaders |
|
66
|
) |
|
67
|
}) |
|
68
|
|
|
69
|
it("successfully returns all store credit accounts", async () => { |
|
70
|
const { |
|
71
|
data: { store_credit_accounts }, |
|
72
|
} = await api.get(`/admin/store-credit-accounts`, adminHeaders) |
|
73
|
|
|
74
|
expect(store_credit_accounts).toHaveLength(2) |
|
75
|
expect(store_credit_accounts).toEqual( |
|
76
|
expect.arrayContaining([ |
|
77
|
expect.objectContaining({ |
|
78
|
currency_code: "usd", |
|
79
|
customer_id: customer1.id, |
|
80
|
customer: expect.objectContaining({ |
|
81
|
id: customer1.id, |
|
82
|
}), |
|
83
|
}), |
|
84
|
expect.objectContaining({ |
|
85
|
currency_code: "usd", |
|
86
|
customer_id: customer2.id, |
|
87
|
customer: expect.objectContaining({ |
|
88
|
id: customer2.id, |
|
89
|
}), |
|
90
|
}), |
|
91
|
]) |
|
92
|
) |
|
93
|
|
|
94
|
const { |
|
95
|
data: { store_credit_accounts: storeCreditAccounts2 }, |
|
96
|
} = await api.get( |
|
97
|
`/admin/store-credit-accounts?customer_id=${customer1.id}¤cy_code=usd`, |
|
98
|
adminHeaders |
|
99
|
) |
|
100
|
|
|
101
|
expect(storeCreditAccounts2).toEqual([ |
|
102
|
expect.objectContaining({ |
|
103
|
currency_code: "usd", |
|
104
|
customer_id: customer1.id, |
|
105
|
customer: expect.objectContaining({ |
|
106
|
id: customer1.id, |
|
107
|
}), |
|
108
|
}), |
|
109
|
]) |
|
110
|
}) |
|
111
|
}) |
|
112
|
|
|
113
|
describe("GET /admin/store-credit-accounts/:id", () => { |
|
114
|
it("should retrieve a store credit account by id", async () => { |
|
115
|
const { |
|
116
|
data: { store_credit_account }, |
|
117
|
} = await api.post( |
|
118
|
`/admin/store-credit-accounts`, |
|
119
|
storeCreditAccountPayload, |
|
120
|
adminHeaders |
|
121
|
) |
|
122
|
|
|
123
|
const { |
|
124
|
data: { store_credit_account: storeCreditAccount }, |
|
125
|
} = await api.get( |
|
126
|
`/admin/store-credit-accounts/${store_credit_account.id}`, |
|
127
|
adminHeaders |
|
128
|
) |
|
129
|
|
|
130
|
expect(storeCreditAccount).toEqual( |
|
131
|
expect.objectContaining(storeCreditAccountResponse) |
|
132
|
) |
|
133
|
}) |
|
134
|
|
|
135
|
it("should throw an error if the store credit account does not exist", async () => { |
|
136
|
const { response } = await api |
|
137
|
.get(`/admin/store-credit-accounts/does-not-exist`, adminHeaders) |
|
138
|
.catch((e) => e) |
|
139
|
|
|
140
|
expect(response.data).toEqual({ |
|
141
|
message: "StoreCreditAccount id not found: does-not-exist", |
|
142
|
type: "not_found", |
|
143
|
}) |
|
144
|
}) |
|
145
|
}) |
|
146
|
|
|
147
|
describe("POST /admin/store-credit-accounts", () => { |
|
148
|
it("successfully creates a store credit account", async () => { |
|
149
|
const { |
|
150
|
data: { store_credit_account }, |
|
151
|
} = await api.post( |
|
152
|
`/admin/store-credit-accounts`, |
|
153
|
storeCreditAccountPayload, |
|
154
|
adminHeaders |
|
155
|
) |
|
156
|
|
|
157
|
expect(store_credit_account).toEqual( |
|
158
|
expect.objectContaining({ |
|
159
|
...storeCreditAccountResponse, |
|
160
|
id: expect.stringMatching(/^sc_acc_/), |
|
161
|
}) |
|
162
|
) |
|
163
|
}) |
|
164
|
|
|
165
|
it("should enforce unique customer_id + currency_code constraint", async () => { |
|
166
|
await api.post( |
|
167
|
`/admin/store-credit-accounts`, |
|
168
|
storeCreditAccountPayload, |
|
169
|
adminHeaders |
|
170
|
) |
|
171
|
|
|
172
|
const { response } = await api |
|
173
|
.post(`/admin/store-credit-accounts`, storeCreditAccountPayload, adminHeaders) |
|
174
|
.catch((e) => e) |
|
175
|
|
|
176
|
expect(response.status).toEqual(400) |
|
177
|
}) |
|
178
|
|
|
179
|
it("should throw an error if required params are missing", async () => { |
|
180
|
const { response } = await api |
|
181
|
.post(`/admin/store-credit-accounts`, {}, adminHeaders) |
|
182
|
.catch((e) => e) |
|
183
|
|
|
184
|
expect(response.data).toEqual({ |
|
185
|
message: "Invalid request: Field 'currency_code' is required", |
|
186
|
type: "invalid_data", |
|
187
|
}) |
|
188
|
}) |
|
189
|
}) |
|
190
|
|
|
191
|
describe("POST /admin/store-credit-accounts/:id/credit", () => { |
|
192
|
it("successfully credits a store credit account", async () => { |
|
193
|
let { |
|
194
|
data: { store_credit_account }, |
|
195
|
} = await api.post( |
|
196
|
`/admin/store-credit-accounts?fields=*transactions`, |
|
197
|
{ currency_code: "usd" }, |
|
198
|
adminHeaders |
|
199
|
) |
|
200
|
|
|
201
|
expect(store_credit_account).toEqual( |
|
202
|
expect.objectContaining({ |
|
203
|
customer_id: null, |
|
204
|
currency_code: "usd", |
|
205
|
metadata: null, |
|
206
|
balance: 0, |
|
207
|
credits: 0, |
|
208
|
debits: 0, |
|
209
|
transactions: [], |
|
210
|
}) |
|
211
|
) |
|
212
|
|
|
213
|
store_credit_account = ( |
|
214
|
await api.post( |
|
215
|
`/admin/store-credit-accounts/${store_credit_account.id}/credit?fields=*transactions`, |
|
216
|
{ amount: 100, note: "Crediting an account 1" }, |
|
217
|
adminHeaders |
|
218
|
) |
|
219
|
).data.store_credit_account |
|
220
|
|
|
221
|
expect(store_credit_account).toEqual( |
|
222
|
expect.objectContaining({ |
|
223
|
customer_id: null, |
|
224
|
currency_code: "usd", |
|
225
|
metadata: null, |
|
226
|
balance: 100, |
|
227
|
credits: 100, |
|
228
|
debits: 0, |
|
229
|
transactions: [ |
|
230
|
expect.objectContaining({ |
|
231
|
id: expect.stringMatching(/^sc_trx_/), |
|
232
|
amount: 100, |
|
233
|
type: "credit", |
|
234
|
note: "Crediting an account 1", |
|
235
|
reference: "user", |
|
236
|
reference_id: adminUser.id, |
|
237
|
}), |
|
238
|
], |
|
239
|
}) |
|
240
|
) |
|
241
|
|
|
242
|
store_credit_account = ( |
|
243
|
await api.post( |
|
244
|
`/admin/store-credit-accounts/${store_credit_account.id}/credit?fields=*transactions`, |
|
245
|
{ amount: 150, note: "Crediting an account 2" }, |
|
246
|
adminHeaders |
|
247
|
) |
|
248
|
).data.store_credit_account |
|
249
|
|
|
250
|
expect(store_credit_account).toEqual( |
|
251
|
expect.objectContaining({ |
|
252
|
customer_id: null, |
|
253
|
currency_code: "usd", |
|
254
|
metadata: null, |
|
255
|
balance: 250, |
|
256
|
credits: 250, |
|
257
|
debits: 0, |
|
258
|
transactions: expect.arrayContaining([ |
|
259
|
expect.objectContaining({ |
|
260
|
amount: 100, |
|
261
|
note: "Crediting an account 1", |
|
262
|
type: "credit", |
|
263
|
reference: "user", |
|
264
|
reference_id: adminUser.id, |
|
265
|
}), |
|
266
|
expect.objectContaining({ |
|
267
|
amount: 150, |
|
268
|
type: "credit", |
|
269
|
note: "Crediting an account 2", |
|
270
|
reference: "user", |
|
271
|
reference_id: adminUser.id, |
|
272
|
}), |
|
273
|
]), |
|
274
|
}) |
|
275
|
) |
|
276
|
}) |
|
277
|
|
|
278
|
it("should list transactions for a store credit account", async () => { |
|
279
|
const { |
|
280
|
data: { store_credit_account }, |
|
281
|
} = await api.post( |
|
282
|
`/admin/store-credit-accounts`, |
|
283
|
{ currency_code: "usd" }, |
|
284
|
adminHeaders |
|
285
|
) |
|
286
|
|
|
287
|
await api.post( |
|
288
|
`/admin/store-credit-accounts/${store_credit_account.id}/credit`, |
|
289
|
{ amount: 50, note: "First credit" }, |
|
290
|
adminHeaders |
|
291
|
) |
|
292
|
|
|
293
|
await api.post( |
|
294
|
`/admin/store-credit-accounts/${store_credit_account.id}/credit`, |
|
295
|
{ amount: 75, note: "Second credit" }, |
|
296
|
adminHeaders |
|
297
|
) |
|
298
|
|
|
299
|
const { data } = await api.get( |
|
300
|
`/admin/store-credit-accounts/${store_credit_account.id}/transactions`, |
|
301
|
adminHeaders |
|
302
|
) |
|
303
|
|
|
304
|
expect(data.transactions).toHaveLength(2) |
|
305
|
expect(data).toEqual( |
|
306
|
expect.objectContaining({ |
|
307
|
count: 2, |
|
308
|
offset: expect.any(Number), |
|
309
|
limit: expect.any(Number), |
|
310
|
}) |
|
311
|
) |
|
312
|
expect(data.transactions).toEqual( |
|
313
|
expect.arrayContaining([ |
|
314
|
expect.objectContaining({ |
|
315
|
id: expect.stringMatching(/^sc_trx_/), |
|
316
|
amount: 50, |
|
317
|
type: "credit", |
|
318
|
note: "First credit", |
|
319
|
}), |
|
320
|
expect.objectContaining({ |
|
321
|
id: expect.stringMatching(/^sc_trx_/), |
|
322
|
amount: 75, |
|
323
|
type: "credit", |
|
324
|
note: "Second credit", |
|
325
|
}), |
|
326
|
]) |
|
327
|
) |
|
328
|
}) |
|
329
|
|
|
330
|
it("should throw an error if the amount is negative", async () => { |
|
331
|
let { |
|
332
|
data: { store_credit_account }, |
|
333
|
} = await api.post( |
|
334
|
`/admin/store-credit-accounts?fields=*transactions`, |
|
335
|
{ currency_code: "usd" }, |
|
336
|
adminHeaders |
|
337
|
) |
|
338
|
|
|
339
|
const { response } = await api |
|
340
|
.post( |
|
341
|
`/admin/store-credit-accounts/${store_credit_account.id}/credit`, |
|
342
|
{ amount: -20 }, |
|
343
|
adminHeaders |
|
344
|
) |
|
345
|
.catch((e) => e) |
|
346
|
|
|
347
|
expect(response.data).toEqual({ |
|
348
|
message: "Amount must be greater than 0", |
|
349
|
type: "invalid_data", |
|
350
|
}) |
|
351
|
}) |
|
352
|
}) |
|
353
|
|
|
354
|
describe("POST /admin/store-credit-accounts/:id/debit", () => { |
|
355
|
let account |
|
356
|
|
|
357
|
beforeEach(async () => { |
|
358
|
account = ( |
|
359
|
await api.post( |
|
360
|
`/admin/store-credit-accounts`, |
|
361
|
{ currency_code: "usd" }, |
|
362
|
adminHeaders |
|
363
|
) |
|
364
|
).data.store_credit_account |
|
365
|
|
|
366
|
await api.post( |
|
367
|
`/admin/store-credit-accounts/${account.id}/credit`, |
|
368
|
{ amount: 100, note: "Initial credit" }, |
|
369
|
adminHeaders |
|
370
|
) |
|
371
|
}) |
|
372
|
|
|
373
|
it("successfully debits a store credit account", async () => { |
|
374
|
const { |
|
375
|
data: { store_credit_account }, |
|
376
|
} = await api.post( |
|
377
|
`/admin/store-credit-accounts/${account.id}/debit?fields=*transactions`, |
|
378
|
{ amount: 40, note: "Credit issued by mistake" }, |
|
379
|
adminHeaders |
|
380
|
) |
|
381
|
|
|
382
|
expect(store_credit_account).toEqual( |
|
383
|
expect.objectContaining({ |
|
384
|
currency_code: "usd", |
|
385
|
balance: 60, |
|
386
|
credits: 100, |
|
387
|
debits: 40, |
|
388
|
transactions: expect.arrayContaining([ |
|
389
|
expect.objectContaining({ |
|
390
|
id: expect.stringMatching(/^sc_trx_/), |
|
391
|
amount: 40, |
|
392
|
type: "debit", |
|
393
|
note: "Credit issued by mistake", |
|
394
|
reference: "user", |
|
395
|
reference_id: adminUser.id, |
|
396
|
}), |
|
397
|
]), |
|
398
|
}) |
|
399
|
) |
|
400
|
}) |
|
401
|
|
|
402
|
it("should throw an error if the amount is greater than the balance", async () => { |
|
403
|
const { response } = await api |
|
404
|
.post( |
|
405
|
`/admin/store-credit-accounts/${account.id}/debit`, |
|
406
|
{ amount: 120 }, |
|
407
|
adminHeaders |
|
408
|
) |
|
409
|
.catch((e) => e) |
|
410
|
|
|
411
|
expect(response.data).toEqual({ |
|
412
|
message: "Insufficient balance", |
|
413
|
type: "invalid_data", |
|
414
|
}) |
|
415
|
}) |
|
416
|
|
|
417
|
it("should throw an error if the amount is negative", async () => { |
|
418
|
const { response } = await api |
|
419
|
.post( |
|
420
|
`/admin/store-credit-accounts/${account.id}/debit`, |
|
421
|
{ amount: -20 }, |
|
422
|
adminHeaders |
|
423
|
) |
|
424
|
.catch((e) => e) |
|
425
|
|
|
426
|
expect(response.data).toEqual({ |
|
427
|
message: "Amount must be greater than 0", |
|
428
|
type: "invalid_data", |
|
429
|
}) |
|
430
|
}) |
|
431
|
|
|
432
|
it("should throw an error if the store credit account does not exist", async () => { |
|
433
|
const { response } = await api |
|
434
|
.post( |
|
435
|
`/admin/store-credit-accounts/sca_does_not_exist/debit`, |
|
436
|
{ amount: 10 }, |
|
437
|
adminHeaders |
|
438
|
) |
|
439
|
.catch((e) => e) |
|
440
|
|
|
441
|
expect(response.status).toEqual(404) |
|
442
|
}) |
|
443
|
}) |
|
444
|
}, |
|
445
|
}) |