|
1
|
import { ProductStatus } from "@medusajs/framework/utils" |
|
2
|
import { medusaIntegrationTestRunner } from "@medusajs/test-utils" |
|
3
|
import { |
|
4
|
adminHeaders, |
|
5
|
createAdminUser, |
|
6
|
generatePublishableKey, |
|
7
|
generateStoreHeaders, |
|
8
|
} from "../../../../helpers/create-admin-user" |
|
9
|
import { createAuthenticatedCustomer } from "../../../../modules/helpers/create-authenticated-customer" |
|
10
|
|
|
11
|
jest.setTimeout(60 * 1000) |
|
12
|
|
|
13
|
const giftCardPayload = { |
|
14
|
currency_code: "usd", |
|
15
|
value: 400, |
|
16
|
code: "TEST1", |
|
17
|
line_item_id: "lin_123", |
|
18
|
} |
|
19
|
|
|
20
|
medusaIntegrationTestRunner({ |
|
21
|
testSuite: ({ dbConnection, api, getContainer, dbUtils }) => { |
|
22
|
let customer |
|
23
|
let giftCard, largeGiftCard |
|
24
|
let product |
|
25
|
let storeHeaders, anonymousHeaders |
|
26
|
let cart |
|
27
|
let cheapVariant, expensiveVariant |
|
28
|
let region, salesChannel |
|
29
|
|
|
30
|
beforeAll(async () => { |
|
31
|
await createAdminUser(dbConnection, adminHeaders, getContainer()) |
|
32
|
const publishableKey = await generatePublishableKey(getContainer()) |
|
33
|
storeHeaders = generateStoreHeaders({ publishableKey }) |
|
34
|
anonymousHeaders = generateStoreHeaders({ publishableKey }) |
|
35
|
|
|
36
|
const user = await createAuthenticatedCustomer(api, storeHeaders, { |
|
37
|
email: "initial@customer.com", |
|
38
|
}) |
|
39
|
|
|
40
|
product = ( |
|
41
|
await api.post( |
|
42
|
`/admin/products`, |
|
43
|
{ |
|
44
|
title: "test product 1", |
|
45
|
status: ProductStatus.PUBLISHED, |
|
46
|
options: [{ title: "size", values: ["large", "small"] }], |
|
47
|
variants: [ |
|
48
|
{ |
|
49
|
title: "expensive", |
|
50
|
manage_inventory: false, |
|
51
|
options: { |
|
52
|
size: "large", |
|
53
|
}, |
|
54
|
prices: [{ amount: 800, currency_code: "usd" }], |
|
55
|
}, |
|
56
|
{ |
|
57
|
title: "cheap", |
|
58
|
manage_inventory: false, |
|
59
|
options: { |
|
60
|
size: "large", |
|
61
|
}, |
|
62
|
prices: [{ amount: 400, currency_code: "usd" }], |
|
63
|
}, |
|
64
|
], |
|
65
|
}, |
|
66
|
adminHeaders |
|
67
|
) |
|
68
|
).data.product |
|
69
|
|
|
70
|
await api.post( |
|
71
|
"/admin/product-types", |
|
72
|
{ value: "GiftCard" }, |
|
73
|
adminHeaders |
|
74
|
) |
|
75
|
|
|
76
|
cheapVariant = product.variants.find((v) => v.title === "cheap") |
|
77
|
expensiveVariant = product.variants.find((v) => v.title === "expensive") |
|
78
|
|
|
79
|
storeHeaders.headers["Authorization"] = `Bearer ${user.jwt}` |
|
80
|
customer = user.customer |
|
81
|
|
|
82
|
giftCard = ( |
|
83
|
await api.post( |
|
84
|
`/admin/gift-cards`, |
|
85
|
{ ...giftCardPayload }, |
|
86
|
adminHeaders |
|
87
|
) |
|
88
|
).data.gift_card |
|
89
|
|
|
90
|
largeGiftCard = ( |
|
91
|
await api.post( |
|
92
|
`/admin/gift-cards`, |
|
93
|
{ |
|
94
|
...giftCardPayload, |
|
95
|
code: "LARGE", |
|
96
|
value: 1000, |
|
97
|
}, |
|
98
|
adminHeaders |
|
99
|
) |
|
100
|
).data.gift_card |
|
101
|
|
|
102
|
salesChannel = ( |
|
103
|
await api.post( |
|
104
|
`/admin/sales-channels`, |
|
105
|
{ name: "test-sales-channel" }, |
|
106
|
adminHeaders |
|
107
|
) |
|
108
|
).data.sales_channel |
|
109
|
|
|
110
|
region = ( |
|
111
|
await api.post( |
|
112
|
"/admin/regions", |
|
113
|
{ |
|
114
|
payment_providers: ["pp_system_default"], |
|
115
|
name: "test-region", |
|
116
|
currency_code: "usd", |
|
117
|
}, |
|
118
|
adminHeaders |
|
119
|
) |
|
120
|
).data.region |
|
121
|
|
|
122
|
const { |
|
123
|
data: { cart: cart2 }, |
|
124
|
} = await api.post( |
|
125
|
`/store/carts`, |
|
126
|
{ |
|
127
|
region_id: region.id, |
|
128
|
sales_channel_id: salesChannel.id, |
|
129
|
items: [{ variant_id: expensiveVariant.id, quantity: 10 }], |
|
130
|
}, |
|
131
|
storeHeaders |
|
132
|
) |
|
133
|
|
|
134
|
cart = cart2 |
|
135
|
|
|
136
|
await api.post( |
|
137
|
`/store/carts/${cart.id}/gift-cards`, |
|
138
|
{ code: giftCard.code }, |
|
139
|
storeHeaders |
|
140
|
) |
|
141
|
|
|
142
|
await api.post( |
|
143
|
`/store/carts/${cart.id}/gift-cards`, |
|
144
|
{ code: largeGiftCard.code }, |
|
145
|
storeHeaders |
|
146
|
) |
|
147
|
|
|
148
|
const { |
|
149
|
data: { payment_collection }, |
|
150
|
} = await api.post( |
|
151
|
`/store/payment-collections`, |
|
152
|
{ cart_id: cart.id }, |
|
153
|
storeHeaders |
|
154
|
) |
|
155
|
|
|
156
|
await api.post( |
|
157
|
`/store/payment-collections/${payment_collection.id}/payment-sessions`, |
|
158
|
{ provider_id: "pp_system_default" }, |
|
159
|
storeHeaders |
|
160
|
) |
|
161
|
|
|
162
|
await dbUtils.snapshot() |
|
163
|
}) |
|
164
|
|
|
165
|
describe("POST /store/orders/:id/credit-lines", () => { |
|
166
|
it("should throw if refund is issued for a non-registered customer", async () => { |
|
167
|
const guestCart = ( |
|
168
|
await api.post( |
|
169
|
`/store/carts`, |
|
170
|
{ |
|
171
|
region_id: region.id, |
|
172
|
sales_channel_id: salesChannel.id, |
|
173
|
items: [{ variant_id: expensiveVariant.id, quantity: 2 }], |
|
174
|
}, |
|
175
|
anonymousHeaders |
|
176
|
) |
|
177
|
).data.cart |
|
178
|
|
|
179
|
await api.post( |
|
180
|
`/store/carts/${guestCart.id}`, |
|
181
|
{ email: "anoncustomer@test.com" }, |
|
182
|
anonymousHeaders |
|
183
|
) |
|
184
|
|
|
185
|
await api.post( |
|
186
|
`/store/carts/${guestCart.id}/gift-cards`, |
|
187
|
{ code: largeGiftCard.code }, |
|
188
|
anonymousHeaders |
|
189
|
) |
|
190
|
|
|
191
|
const { |
|
192
|
data: { payment_collection }, |
|
193
|
} = await api.post( |
|
194
|
`/store/payment-collections`, |
|
195
|
{ cart_id: guestCart.id }, |
|
196
|
anonymousHeaders |
|
197
|
) |
|
198
|
|
|
199
|
await api.post( |
|
200
|
`/store/payment-collections/${payment_collection.id}/payment-sessions`, |
|
201
|
{ provider_id: "pp_system_default" }, |
|
202
|
anonymousHeaders |
|
203
|
) |
|
204
|
|
|
205
|
const { |
|
206
|
data: { order }, |
|
207
|
} = await api.post( |
|
208
|
`/store/carts/${guestCart.id}/complete`, |
|
209
|
{}, |
|
210
|
anonymousHeaders |
|
211
|
) |
|
212
|
|
|
213
|
await api.post( |
|
214
|
"/admin/order-edits", |
|
215
|
{ |
|
216
|
order_id: order.id, |
|
217
|
description: "Test", |
|
218
|
}, |
|
219
|
adminHeaders |
|
220
|
) |
|
221
|
|
|
222
|
const item = order.items[0] |
|
223
|
|
|
224
|
await api.post( |
|
225
|
`/admin/order-edits/${order.id}/items/item/${item.id}`, |
|
226
|
{ quantity: 1 }, |
|
227
|
adminHeaders |
|
228
|
) |
|
229
|
|
|
230
|
await api.post( |
|
231
|
`/admin/order-edits/${order.id}/request`, |
|
232
|
{}, |
|
233
|
adminHeaders |
|
234
|
) |
|
235
|
|
|
236
|
await api.post( |
|
237
|
`/admin/order-edits/${order.id}/confirm`, |
|
238
|
{}, |
|
239
|
adminHeaders |
|
240
|
) |
|
241
|
|
|
242
|
const reponse = await api |
|
243
|
.post( |
|
244
|
`/admin/orders/${order.id}/credit-lines`, |
|
245
|
{ |
|
246
|
amount: -200, |
|
247
|
reference: "order", |
|
248
|
reference_id: order.id, |
|
249
|
}, |
|
250
|
adminHeaders |
|
251
|
) |
|
252
|
.catch((err) => err.response) |
|
253
|
|
|
254
|
expect(reponse.status).toEqual(400) |
|
255
|
expect(reponse.data.message).toEqual( |
|
256
|
"Store credit refunds can only be issued to registered customers" |
|
257
|
) |
|
258
|
}) |
|
259
|
|
|
260
|
it("should refund to customers store credit account upon credit line refunds", async () => { |
|
261
|
let customerAccounts = ( |
|
262
|
await api.get( |
|
263
|
`/admin/store-credit-accounts?customer_id=${customer.id}¤cy_code=usd`, |
|
264
|
adminHeaders |
|
265
|
) |
|
266
|
).data.store_credit_accounts |
|
267
|
|
|
268
|
expect(customerAccounts.length).toEqual(0) |
|
269
|
|
|
270
|
const { |
|
271
|
data: { order }, |
|
272
|
} = await api.post(`/store/carts/${cart.id}/complete`, {}, storeHeaders) |
|
273
|
|
|
274
|
expect(order).toEqual( |
|
275
|
expect.objectContaining({ |
|
276
|
credit_lines: expect.arrayContaining([ |
|
277
|
expect.objectContaining({ |
|
278
|
amount: 400, |
|
279
|
reference: "gift-card", |
|
280
|
reference_id: giftCard.id, |
|
281
|
}), |
|
282
|
expect.objectContaining({ |
|
283
|
amount: 1000, |
|
284
|
reference: "gift-card", |
|
285
|
reference_id: largeGiftCard.id, |
|
286
|
}), |
|
287
|
]), |
|
288
|
}) |
|
289
|
) |
|
290
|
|
|
291
|
await api.post( |
|
292
|
"/admin/order-edits", |
|
293
|
{ |
|
294
|
order_id: order.id, |
|
295
|
description: "Test", |
|
296
|
}, |
|
297
|
adminHeaders |
|
298
|
) |
|
299
|
|
|
300
|
const item = order.items[0] |
|
301
|
|
|
302
|
await api.post( |
|
303
|
`/admin/order-edits/${order.id}/items/item/${item.id}`, |
|
304
|
{ quantity: 0 }, |
|
305
|
adminHeaders |
|
306
|
) |
|
307
|
|
|
308
|
await api.post( |
|
309
|
`/admin/order-edits/${order.id}/request`, |
|
310
|
{}, |
|
311
|
adminHeaders |
|
312
|
) |
|
313
|
|
|
314
|
await api.post( |
|
315
|
`/admin/order-edits/${order.id}/confirm`, |
|
316
|
{}, |
|
317
|
adminHeaders |
|
318
|
) |
|
319
|
|
|
320
|
await api.get(`/admin/orders/${order.id}`, adminHeaders) |
|
321
|
|
|
322
|
const { |
|
323
|
data: { order: order2 }, |
|
324
|
} = await api.post( |
|
325
|
`/admin/orders/${order.id}/credit-lines`, |
|
326
|
{ |
|
327
|
amount: -106, |
|
328
|
reference: "order", |
|
329
|
reference_id: order.id, |
|
330
|
}, |
|
331
|
adminHeaders |
|
332
|
) |
|
333
|
|
|
334
|
expect(order2).toEqual( |
|
335
|
expect.objectContaining({ |
|
336
|
credit_lines: expect.arrayContaining([ |
|
337
|
expect.objectContaining({ |
|
338
|
amount: 400, |
|
339
|
reference: "gift-card", |
|
340
|
reference_id: giftCard.id, |
|
341
|
}), |
|
342
|
expect.objectContaining({ |
|
343
|
amount: 1000, |
|
344
|
reference: "gift-card", |
|
345
|
reference_id: largeGiftCard.id, |
|
346
|
}), |
|
347
|
expect.objectContaining({ |
|
348
|
amount: -106, |
|
349
|
reference: "order", |
|
350
|
reference_id: order.id, |
|
351
|
}), |
|
352
|
]), |
|
353
|
}) |
|
354
|
) |
|
355
|
|
|
356
|
customerAccounts = ( |
|
357
|
await api.get( |
|
358
|
`/admin/store-credit-accounts?customer_id=${customer.id}¤cy_code=usd&fields=*transactions`, |
|
359
|
adminHeaders |
|
360
|
) |
|
361
|
).data.store_credit_accounts |
|
362
|
|
|
363
|
// as part of the refund a customer store credit account was created |
|
364
|
expect(customerAccounts.length).toEqual(1) |
|
365
|
|
|
366
|
expect(customerAccounts[0]).toEqual( |
|
367
|
expect.objectContaining({ |
|
368
|
currency_code: "usd", |
|
369
|
customer_id: customer.id, |
|
370
|
balance: 106, |
|
371
|
credits: 106, |
|
372
|
debits: 0, |
|
373
|
transactions: expect.arrayContaining([ |
|
374
|
expect.objectContaining({ |
|
375
|
amount: 106, |
|
376
|
type: "credit", |
|
377
|
reference: "order", |
|
378
|
reference_id: order.id, |
|
379
|
}), |
|
380
|
]), |
|
381
|
}) |
|
382
|
) |
|
383
|
|
|
384
|
const { |
|
385
|
data: { order: order3 }, |
|
386
|
} = await api.post( |
|
387
|
`/admin/orders/${order.id}/credit-lines`, |
|
388
|
{ |
|
389
|
amount: -1200, |
|
390
|
reference: "order", |
|
391
|
reference_id: order.id, |
|
392
|
}, |
|
393
|
adminHeaders |
|
394
|
) |
|
395
|
|
|
396
|
expect(order3).toEqual( |
|
397
|
expect.objectContaining({ |
|
398
|
credit_lines: expect.arrayContaining([ |
|
399
|
expect.objectContaining({ |
|
400
|
amount: 400, |
|
401
|
reference: "gift-card", |
|
402
|
reference_id: giftCard.id, |
|
403
|
}), |
|
404
|
expect.objectContaining({ |
|
405
|
amount: 1000, |
|
406
|
reference: "gift-card", |
|
407
|
reference_id: largeGiftCard.id, |
|
408
|
}), |
|
409
|
expect.objectContaining({ |
|
410
|
amount: -106, |
|
411
|
reference: "order", |
|
412
|
reference_id: order.id, |
|
413
|
}), |
|
414
|
expect.objectContaining({ |
|
415
|
amount: -1200, |
|
416
|
reference: "order", |
|
417
|
reference_id: order.id, |
|
418
|
}), |
|
419
|
]), |
|
420
|
}) |
|
421
|
) |
|
422
|
|
|
423
|
customerAccounts = ( |
|
424
|
await api.get( |
|
425
|
`/admin/store-credit-accounts?customer_id=${customer.id}¤cy_code=usd&fields=*transactions`, |
|
426
|
adminHeaders |
|
427
|
) |
|
428
|
).data.store_credit_accounts |
|
429
|
|
|
430
|
// now the customer account is updated |
|
431
|
expect(customerAccounts.length).toEqual(1) |
|
432
|
|
|
433
|
expect(customerAccounts[0]).toEqual( |
|
434
|
expect.objectContaining({ |
|
435
|
currency_code: "usd", |
|
436
|
customer_id: customer.id, |
|
437
|
balance: 106 + 1200, |
|
438
|
credits: 106 + 1200, |
|
439
|
debits: 0, |
|
440
|
transactions: expect.arrayContaining([ |
|
441
|
expect.objectContaining({ |
|
442
|
amount: 106, |
|
443
|
type: "credit", |
|
444
|
reference: "order", |
|
445
|
reference_id: order.id, |
|
446
|
}), |
|
447
|
expect.objectContaining({ |
|
448
|
amount: 1200, |
|
449
|
type: "credit", |
|
450
|
reference: "order", |
|
451
|
reference_id: order2.id, |
|
452
|
}), |
|
453
|
]), |
|
454
|
}) |
|
455
|
) |
|
456
|
}) |
|
457
|
}) |
|
458
|
}, |
|
459
|
}) |