|
1
|
import { CreateOrderDTO, IOrderModuleService } from "@medusajs/framework/types" |
|
2
|
import { Modules, promiseAll } from "@medusajs/framework/utils" |
|
3
|
import { |
|
4
|
moduleIntegrationTestRunner, |
|
5
|
normalizeBigNumbers, |
|
6
|
} from "@medusajs/test-utils" |
|
7
|
|
|
8
|
jest.setTimeout(100000) |
|
9
|
|
|
10
|
moduleIntegrationTestRunner<IOrderModuleService>({ |
|
11
|
moduleName: Modules.ORDER, |
|
12
|
moduleOptions: { |
|
13
|
generateCustomDisplayId: async (order: CreateOrderDTO): Promise<string> => { |
|
14
|
return order.currency_code + "_1234567890" |
|
15
|
}, |
|
16
|
}, |
|
17
|
testSuite: ({ service, MikroOrmWrapper }) => { |
|
18
|
describe("Order Module Service", () => { |
|
19
|
const input = { |
|
20
|
email: "foo@bar.com", |
|
21
|
items: [ |
|
22
|
{ |
|
23
|
title: "Item 1", |
|
24
|
subtitle: "Subtitle 1", |
|
25
|
thumbnail: "thumbnail1.jpg", |
|
26
|
quantity: 1, |
|
27
|
product_id: "product1", |
|
28
|
product_title: "Product 1", |
|
29
|
product_description: "Description 1", |
|
30
|
product_subtitle: "Product Subtitle 1", |
|
31
|
product_type: "Type 1", |
|
32
|
product_type_id: "type_1", |
|
33
|
product_collection: "Collection 1", |
|
34
|
product_handle: "handle1", |
|
35
|
variant_id: "variant1", |
|
36
|
variant_sku: "SKU1", |
|
37
|
variant_barcode: "Barcode1", |
|
38
|
variant_title: "Variant 1", |
|
39
|
variant_option_values: { |
|
40
|
color: "Red", |
|
41
|
size: "Large", |
|
42
|
}, |
|
43
|
requires_shipping: true, |
|
44
|
is_discountable: true, |
|
45
|
is_tax_inclusive: true, |
|
46
|
compare_at_unit_price: 10, |
|
47
|
unit_price: 20, |
|
48
|
tax_lines: [ |
|
49
|
{ |
|
50
|
description: "Tax 1", |
|
51
|
tax_rate_id: "tax_usa", |
|
52
|
code: "code", |
|
53
|
rate: 10, |
|
54
|
provider_id: "taxify_master", |
|
55
|
}, |
|
56
|
], |
|
57
|
adjustments: [ |
|
58
|
{ |
|
59
|
code: "VIP_10", |
|
60
|
amount: 10, |
|
61
|
description: "VIP discount", |
|
62
|
promotion_id: "prom_123", |
|
63
|
provider_id: "coupon_kings", |
|
64
|
}, |
|
65
|
], |
|
66
|
}, |
|
67
|
{ |
|
68
|
title: "Item 2", |
|
69
|
quantity: 2, |
|
70
|
unit_price: 5, |
|
71
|
}, |
|
72
|
{ |
|
73
|
title: "Item 3", |
|
74
|
quantity: 1, |
|
75
|
unit_price: 30, |
|
76
|
}, |
|
77
|
], |
|
78
|
sales_channel_id: "test", |
|
79
|
shipping_address: { |
|
80
|
first_name: "Test", |
|
81
|
last_name: "Test", |
|
82
|
address_1: "Test", |
|
83
|
city: "Test", |
|
84
|
country_code: "US", |
|
85
|
postal_code: "12345", |
|
86
|
phone: "12345", |
|
87
|
}, |
|
88
|
billing_address: { |
|
89
|
first_name: "Test", |
|
90
|
last_name: "Test", |
|
91
|
address_1: "Test", |
|
92
|
city: "Test", |
|
93
|
country_code: "US", |
|
94
|
postal_code: "12345", |
|
95
|
}, |
|
96
|
shipping_methods: [ |
|
97
|
{ |
|
98
|
name: "Test shipping method", |
|
99
|
amount: 10, |
|
100
|
data: {}, |
|
101
|
tax_lines: [ |
|
102
|
{ |
|
103
|
description: "shipping Tax 1", |
|
104
|
tax_rate_id: "tax_usa_shipping", |
|
105
|
code: "code", |
|
106
|
rate: 10, |
|
107
|
}, |
|
108
|
], |
|
109
|
adjustments: [ |
|
110
|
{ |
|
111
|
code: "VIP_10", |
|
112
|
amount: 1, |
|
113
|
description: "VIP discount", |
|
114
|
promotion_id: "prom_123", |
|
115
|
}, |
|
116
|
], |
|
117
|
}, |
|
118
|
], |
|
119
|
transactions: [ |
|
120
|
{ |
|
121
|
amount: 48.9, |
|
122
|
currency_code: "USD", |
|
123
|
reference: "payment", |
|
124
|
reference_id: "pay_123", |
|
125
|
}, |
|
126
|
], |
|
127
|
currency_code: "usd", |
|
128
|
customer_id: "joe", |
|
129
|
} as CreateOrderDTO |
|
130
|
|
|
131
|
const expectation = expect.objectContaining({ |
|
132
|
id: expect.stringContaining("order_"), |
|
133
|
version: 1, |
|
134
|
display_id: 1, |
|
135
|
custom_display_id: "usd_1234567890", |
|
136
|
summary: expect.objectContaining({ |
|
137
|
// TODO: add all summary fields |
|
138
|
}), |
|
139
|
shipping_address: expect.objectContaining({ |
|
140
|
id: expect.stringContaining("ordaddr_"), |
|
141
|
}), |
|
142
|
billing_address: expect.objectContaining({ |
|
143
|
id: expect.stringContaining("ordaddr_"), |
|
144
|
}), |
|
145
|
items: expect.arrayContaining([ |
|
146
|
expect.objectContaining({ |
|
147
|
id: expect.stringContaining("ordli_"), |
|
148
|
quantity: 1, |
|
149
|
tax_lines: [ |
|
150
|
expect.objectContaining({ |
|
151
|
id: expect.stringContaining("ordlitxl_"), |
|
152
|
}), |
|
153
|
], |
|
154
|
adjustments: [ |
|
155
|
expect.objectContaining({ |
|
156
|
id: expect.stringContaining("ordliadj_"), |
|
157
|
}), |
|
158
|
], |
|
159
|
detail: expect.objectContaining({ |
|
160
|
id: expect.stringContaining("orditem_"), |
|
161
|
version: 1, |
|
162
|
quantity: 1, |
|
163
|
shipped_quantity: 0, |
|
164
|
}), |
|
165
|
}), |
|
166
|
expect.objectContaining({ |
|
167
|
id: expect.stringContaining("ordli_"), |
|
168
|
quantity: 2, |
|
169
|
tax_lines: [], |
|
170
|
adjustments: [], |
|
171
|
detail: expect.objectContaining({ |
|
172
|
id: expect.stringContaining("orditem_"), |
|
173
|
version: 1, |
|
174
|
quantity: 2, |
|
175
|
fulfilled_quantity: 0, |
|
176
|
}), |
|
177
|
}), |
|
178
|
expect.objectContaining({ |
|
179
|
id: expect.stringContaining("ordli_"), |
|
180
|
tax_lines: [], |
|
181
|
adjustments: [], |
|
182
|
detail: expect.objectContaining({ |
|
183
|
id: expect.stringContaining("orditem_"), |
|
184
|
version: 1, |
|
185
|
}), |
|
186
|
}), |
|
187
|
]), |
|
188
|
shipping_methods: [ |
|
189
|
expect.objectContaining({ |
|
190
|
id: expect.stringContaining("ordsm_"), |
|
191
|
tax_lines: [ |
|
192
|
expect.objectContaining({ |
|
193
|
id: expect.stringContaining("ordsmtxl_"), |
|
194
|
}), |
|
195
|
], |
|
196
|
adjustments: [ |
|
197
|
expect.objectContaining({ |
|
198
|
id: expect.stringContaining("ordsmadj_"), |
|
199
|
}), |
|
200
|
], |
|
201
|
}), |
|
202
|
], |
|
203
|
}) |
|
204
|
|
|
205
|
it("should create an order, shipping method and items. Including taxes and adjustments associated with them", async function () { |
|
206
|
const createdOrder = await service.createOrders(input) |
|
207
|
|
|
208
|
expect(normalizeBigNumbers(createdOrder)).toEqual(expectation) |
|
209
|
}) |
|
210
|
|
|
211
|
it("should create an order, shipping method and items. Including taxes and adjustments associated with them and add new transactions", async function () { |
|
212
|
const inpCopy = JSON.parse(JSON.stringify(input)) as CreateOrderDTO |
|
213
|
inpCopy.transactions!.push({ |
|
214
|
amount: 10, |
|
215
|
currency_code: "USD", |
|
216
|
}) |
|
217
|
const created = await service.createOrders(inpCopy) |
|
218
|
|
|
219
|
expect(normalizeBigNumbers(created.summary)).toEqual( |
|
220
|
expect.objectContaining({ |
|
221
|
transaction_total: 58.9, |
|
222
|
pending_difference: 0, |
|
223
|
paid_total: 58.9, |
|
224
|
refunded_total: 0, |
|
225
|
}) |
|
226
|
) |
|
227
|
|
|
228
|
const refund = await service.addOrderTransactions([ |
|
229
|
{ |
|
230
|
order_id: created.id, |
|
231
|
amount: -20, |
|
232
|
currency_code: "USD", |
|
233
|
}, |
|
234
|
]) |
|
235
|
|
|
236
|
const serializedOrder = JSON.parse( |
|
237
|
JSON.stringify( |
|
238
|
await service.retrieveOrder(created.id, { |
|
239
|
select: ["id", "summary", "total"], |
|
240
|
}) |
|
241
|
) |
|
242
|
) |
|
243
|
|
|
244
|
expect(serializedOrder.summary).toEqual( |
|
245
|
expect.objectContaining({ |
|
246
|
transaction_total: 38.9, |
|
247
|
pending_difference: 20, |
|
248
|
paid_total: 58.9, |
|
249
|
refunded_total: 20, |
|
250
|
}) |
|
251
|
) |
|
252
|
|
|
253
|
await service.softDeleteOrderTransactions([refund[0].id]) |
|
254
|
|
|
255
|
const serializedOrder2 = JSON.parse( |
|
256
|
JSON.stringify( |
|
257
|
await service.retrieveOrder(created.id, { |
|
258
|
select: ["id", "summary", "total"], |
|
259
|
}) |
|
260
|
) |
|
261
|
) |
|
262
|
|
|
263
|
expect(serializedOrder2.summary).toEqual( |
|
264
|
expect.objectContaining({ |
|
265
|
transaction_total: 58.9, |
|
266
|
pending_difference: 0, |
|
267
|
paid_total: 58.9, |
|
268
|
refunded_total: 0, |
|
269
|
}) |
|
270
|
) |
|
271
|
|
|
272
|
await service.addOrderTransactions([ |
|
273
|
{ |
|
274
|
order_id: created.id, |
|
275
|
amount: -50, |
|
276
|
currency_code: "USD", |
|
277
|
}, |
|
278
|
]) |
|
279
|
|
|
280
|
const serializedOrder3 = JSON.parse( |
|
281
|
JSON.stringify( |
|
282
|
await service.retrieveOrder(created.id, { |
|
283
|
select: ["id", "summary", "total"], |
|
284
|
}) |
|
285
|
) |
|
286
|
) |
|
287
|
|
|
288
|
expect(serializedOrder3.summary).toEqual( |
|
289
|
expect.objectContaining({ |
|
290
|
paid_total: 58.9, |
|
291
|
refunded_total: 50, |
|
292
|
transaction_total: 8.9, |
|
293
|
pending_difference: 50, |
|
294
|
}) |
|
295
|
) |
|
296
|
|
|
297
|
await service.restoreOrderTransactions([refund[0].id]) |
|
298
|
|
|
299
|
const serializedOrder4 = JSON.parse( |
|
300
|
JSON.stringify( |
|
301
|
await service.retrieveOrder(created.id, { |
|
302
|
select: ["id", "summary", "total"], |
|
303
|
}) |
|
304
|
) |
|
305
|
) |
|
306
|
|
|
307
|
expect(serializedOrder4.summary).toEqual( |
|
308
|
expect.objectContaining({ |
|
309
|
paid_total: 58.9, |
|
310
|
refunded_total: 70, |
|
311
|
transaction_total: -11.1, |
|
312
|
pending_difference: 70, |
|
313
|
}) |
|
314
|
) |
|
315
|
}) |
|
316
|
|
|
317
|
it("should delete entirely an order, shipping method and items. Including taxes and adjustments associated with them and credit lines", async function () { |
|
318
|
const inpCopy = JSON.parse(JSON.stringify(input)) as CreateOrderDTO |
|
319
|
inpCopy.transactions!.push({ |
|
320
|
amount: 10, |
|
321
|
currency_code: "USD", |
|
322
|
}) |
|
323
|
const created = await service.createOrders(inpCopy) |
|
324
|
|
|
325
|
await service.createOrderCreditLines([ |
|
326
|
{ |
|
327
|
order_id: created.id, |
|
328
|
amount: 10, |
|
329
|
reference_id: "credit_line_1", |
|
330
|
}, |
|
331
|
]) |
|
332
|
|
|
333
|
await service.addOrderTransactions([ |
|
334
|
{ |
|
335
|
order_id: created.id, |
|
336
|
amount: -20, |
|
337
|
currency_code: "USD", |
|
338
|
}, |
|
339
|
]) |
|
340
|
|
|
341
|
async function findAllOrderRelatedEntities(orderId: string) { |
|
342
|
const orderIdClause = { order_id: orderId } |
|
343
|
const manager = MikroOrmWrapper.forkManager() |
|
344
|
const [ |
|
345
|
order, |
|
346
|
orderItems, |
|
347
|
orderSummary, |
|
348
|
orderShippingMethods, |
|
349
|
orderTransactions, |
|
350
|
orderCreditLines, |
|
351
|
orderShippingAddress, |
|
352
|
orderBillingAddress, |
|
353
|
orderChange, |
|
354
|
orderChangeAction, |
|
355
|
] = await promiseAll([ |
|
356
|
manager.findOne("Order", { |
|
357
|
id: created.id, |
|
358
|
}), |
|
359
|
manager.findOne("OrderItem", orderIdClause), |
|
360
|
manager.findOne("OrderSummary", orderIdClause), |
|
361
|
manager.findOne("OrderShipping", orderIdClause), |
|
362
|
manager.findOne("OrderTransaction", orderIdClause), |
|
363
|
manager.findOne("OrderCreditLine", orderIdClause), |
|
364
|
manager.findOne("OrderAddress", { |
|
365
|
first_name: "Test", |
|
366
|
last_name: "Test", |
|
367
|
address_1: "Test", |
|
368
|
city: "Test", |
|
369
|
country_code: "US", |
|
370
|
postal_code: "12345", |
|
371
|
phone: "12345", |
|
372
|
}), |
|
373
|
manager.findOne("OrderAddress", { |
|
374
|
first_name: "Test", |
|
375
|
last_name: "Test", |
|
376
|
address_1: "Test", |
|
377
|
city: "Test", |
|
378
|
country_code: "US", |
|
379
|
postal_code: "12345", |
|
380
|
}), |
|
381
|
]) |
|
382
|
|
|
383
|
return { |
|
384
|
order, |
|
385
|
orderItems, |
|
386
|
orderSummary, |
|
387
|
orderShippingMethods, |
|
388
|
orderTransactions, |
|
389
|
orderCreditLines, |
|
390
|
orderShippingAddress, |
|
391
|
orderBillingAddress, |
|
392
|
} |
|
393
|
} |
|
394
|
|
|
395
|
let orderRelatedEntities = await findAllOrderRelatedEntities(created.id) |
|
396
|
expect(orderRelatedEntities.order).not.toBeNull() |
|
397
|
expect(orderRelatedEntities.orderItems).not.toBeNull() |
|
398
|
expect(orderRelatedEntities.orderSummary).not.toBeNull() |
|
399
|
expect(orderRelatedEntities.orderShippingMethods).not.toBeNull() |
|
400
|
expect(orderRelatedEntities.orderTransactions).not.toBeNull() |
|
401
|
expect(orderRelatedEntities.orderCreditLines).not.toBeNull() |
|
402
|
expect(orderRelatedEntities.orderShippingAddress).not.toBeNull() |
|
403
|
expect(orderRelatedEntities.orderBillingAddress).not.toBeNull() |
|
404
|
|
|
405
|
await service.deleteOrders([created.id]) |
|
406
|
|
|
407
|
orderRelatedEntities = await findAllOrderRelatedEntities(created.id) |
|
408
|
expect(orderRelatedEntities.order).toBeNull() |
|
409
|
expect(orderRelatedEntities.orderItems).toBeNull() |
|
410
|
expect(orderRelatedEntities.orderSummary).toBeNull() |
|
411
|
expect(orderRelatedEntities.orderShippingMethods).toBeNull() |
|
412
|
expect(orderRelatedEntities.orderTransactions).toBeNull() |
|
413
|
expect(orderRelatedEntities.orderCreditLines).toBeNull() |
|
414
|
expect(orderRelatedEntities.orderShippingAddress).toBeNull() |
|
415
|
expect(orderRelatedEntities.orderBillingAddress).toBeNull() |
|
416
|
}) |
|
417
|
|
|
418
|
it("should transform requested fields and relations to match the db schema and return the order", async function () { |
|
419
|
const createdOrder = await service.createOrders(input) |
|
420
|
const getOrder = await service.retrieveOrder(createdOrder.id, { |
|
421
|
select: [ |
|
422
|
"id", |
|
423
|
"display_id", |
|
424
|
"version", |
|
425
|
"custom_display_id", |
|
426
|
"items.id", |
|
427
|
"summary", |
|
428
|
"items.quantity", |
|
429
|
"items.detail.id", |
|
430
|
"items.detail.version", |
|
431
|
"items.detail.quantity", |
|
432
|
"items.detail.shipped_quantity", |
|
433
|
"items.detail.fulfilled_quantity", |
|
434
|
"items.tax_lines.id", |
|
435
|
"items.adjustments.id", |
|
436
|
"shipping_address.id", |
|
437
|
"billing_address.id", |
|
438
|
"shipping_methods.id", |
|
439
|
"shipping_methods.tax_lines.id", |
|
440
|
"shipping_methods.adjustments.id", |
|
441
|
], |
|
442
|
relations: [ |
|
443
|
"shipping_address", |
|
444
|
"billing_address", |
|
445
|
"items", |
|
446
|
"items.detail", |
|
447
|
"items.tax_lines", |
|
448
|
"items.adjustments", |
|
449
|
"shipping_methods", |
|
450
|
"shipping_methods.tax_lines", |
|
451
|
"shipping_methods.adjustments", |
|
452
|
], |
|
453
|
}) |
|
454
|
|
|
455
|
const serializedOrder = JSON.parse(JSON.stringify(getOrder)) |
|
456
|
expect(serializedOrder).toEqual(expectation) |
|
457
|
}) |
|
458
|
|
|
459
|
it("should return the item quantity when only explicit item fields are selected", async function () { |
|
460
|
const createdOrder = await service.createOrders(input) |
|
461
|
|
|
462
|
const expectedItems = expect.arrayContaining([ |
|
463
|
expect.objectContaining({ title: "Item 1", quantity: 1 }), |
|
464
|
expect.objectContaining({ title: "Item 2", quantity: 2 }), |
|
465
|
expect.objectContaining({ title: "Item 3", quantity: 1 }), |
|
466
|
]) |
|
467
|
|
|
468
|
const retrieved = await service.retrieveOrder(createdOrder.id, { |
|
469
|
select: ["id", "items.id", "items.title", "items.quantity"], |
|
470
|
relations: ["items"], |
|
471
|
}) |
|
472
|
expect(normalizeBigNumbers(retrieved).items).toEqual(expectedItems) |
|
473
|
|
|
474
|
const [listed] = await service.listOrders( |
|
475
|
{ id: createdOrder.id }, |
|
476
|
{ |
|
477
|
select: ["id", "items.id", "items.title", "items.quantity"], |
|
478
|
relations: ["items"], |
|
479
|
} |
|
480
|
) |
|
481
|
expect(normalizeBigNumbers(listed).items).toEqual(expectedItems) |
|
482
|
|
|
483
|
const [[paginated]] = await service.listAndCountOrders( |
|
484
|
{ id: createdOrder.id }, |
|
485
|
{ |
|
486
|
select: ["id", "items.id", "items.title", "items.quantity"], |
|
487
|
relations: ["items"], |
|
488
|
take: 10, |
|
489
|
} |
|
490
|
) |
|
491
|
expect(normalizeBigNumbers(paginated).items).toEqual(expectedItems) |
|
492
|
}) |
|
493
|
|
|
494
|
it("should return order transactions", async function () { |
|
495
|
const createdOrder = await service.createOrders(input) |
|
496
|
const getOrder = await service.retrieveOrder(createdOrder.id, { |
|
497
|
select: [ |
|
498
|
"id", |
|
499
|
"transactions.amount", |
|
500
|
"transactions.reference", |
|
501
|
"transactions.reference_id", |
|
502
|
], |
|
503
|
relations: ["transactions"], |
|
504
|
}) |
|
505
|
|
|
506
|
const serializedOrder = JSON.parse(JSON.stringify(getOrder)) |
|
507
|
expect(serializedOrder).toEqual( |
|
508
|
expect.objectContaining({ |
|
509
|
id: createdOrder.id, |
|
510
|
transactions: [ |
|
511
|
expect.objectContaining({ |
|
512
|
amount: 48.9, |
|
513
|
reference: "payment", |
|
514
|
reference_id: "pay_123", |
|
515
|
}), |
|
516
|
], |
|
517
|
}) |
|
518
|
) |
|
519
|
}) |
|
520
|
|
|
521
|
it("should transform where clause to match the db schema and return the order", async function () { |
|
522
|
await service.createOrders(input) |
|
523
|
const orders = await service.listOrders( |
|
524
|
{ |
|
525
|
items: { |
|
526
|
quantity: 2, |
|
527
|
}, |
|
528
|
}, |
|
529
|
{ |
|
530
|
select: ["id"], |
|
531
|
relations: ["items"], |
|
532
|
} |
|
533
|
) |
|
534
|
expect(orders.length).toEqual(1) |
|
535
|
|
|
536
|
const orders2 = await service.listOrders( |
|
537
|
{ |
|
538
|
items: { |
|
539
|
quantity: 5, |
|
540
|
}, |
|
541
|
}, |
|
542
|
{ |
|
543
|
select: ["items.quantity"], |
|
544
|
relations: ["items"], |
|
545
|
} |
|
546
|
) |
|
547
|
expect(orders2.length).toEqual(0) |
|
548
|
|
|
549
|
const orders3 = await service.listOrders( |
|
550
|
{ |
|
551
|
items: { |
|
552
|
detail: { |
|
553
|
shipped_quantity: 0, |
|
554
|
}, |
|
555
|
}, |
|
556
|
}, |
|
557
|
{ |
|
558
|
select: ["id"], |
|
559
|
relations: ["items.detail"], |
|
560
|
} |
|
561
|
) |
|
562
|
expect(orders3.length).toEqual(1) |
|
563
|
|
|
564
|
const orders4 = await service.listOrders( |
|
565
|
{ |
|
566
|
items: { |
|
567
|
detail: { |
|
568
|
shipped_quantity: 1, |
|
569
|
}, |
|
570
|
}, |
|
571
|
}, |
|
572
|
{ |
|
573
|
select: ["id"], |
|
574
|
relations: ["items.detail"], |
|
575
|
} |
|
576
|
) |
|
577
|
expect(orders4.length).toEqual(0) |
|
578
|
}) |
|
579
|
|
|
580
|
it("should list orders with totals without selecting shipping method fields", async function () { |
|
581
|
const createdOrder = await service.createOrders(input) |
|
582
|
|
|
583
|
const [orders, count] = await service.listAndCountOrders( |
|
584
|
{ id: createdOrder.id }, |
|
585
|
{ |
|
586
|
select: ["id", "total"], |
|
587
|
} |
|
588
|
) |
|
589
|
|
|
590
|
expect(count).toEqual(1) |
|
591
|
expect(orders[0].id).toEqual(createdOrder.id) |
|
592
|
expect(Number(orders[0].total)).toEqual(58.9) |
|
593
|
}) |
|
594
|
}) |
|
595
|
}, |
|
596
|
}) |