|
1
|
import { |
|
2
|
CreateOrderLineItemTaxLineDTO, |
|
3
|
IOrderModuleService, |
|
4
|
} from "@medusajs/framework/types" |
|
5
|
import { Modules, OrderStatus } from "@medusajs/framework/utils" |
|
6
|
import { moduleIntegrationTestRunner } from "@medusajs/test-utils" |
|
7
|
|
|
8
|
jest.setTimeout(100000) |
|
9
|
|
|
10
|
moduleIntegrationTestRunner<IOrderModuleService>({ |
|
11
|
moduleName: Modules.ORDER, |
|
12
|
testSuite: ({ service }) => { |
|
13
|
describe("Order - Items and Shipping methods", () => { |
|
14
|
describe("create", () => { |
|
15
|
it("should throw an error when required params are not passed", async () => { |
|
16
|
const error = await service |
|
17
|
.createOrders([ |
|
18
|
{ |
|
19
|
email: "test@email.com", |
|
20
|
} as any, |
|
21
|
]) |
|
22
|
.catch((e) => e) |
|
23
|
|
|
24
|
expect(error.message).toContain( |
|
25
|
"Value for Order.currency_code is required, 'undefined' found" |
|
26
|
) |
|
27
|
}) |
|
28
|
|
|
29
|
it("should create an order successfully", async () => { |
|
30
|
const [createdOrder] = await service.createOrders([ |
|
31
|
{ |
|
32
|
currency_code: "eur", |
|
33
|
items: [ |
|
34
|
{ |
|
35
|
title: "test", |
|
36
|
quantity: 1, |
|
37
|
unit_price: 100, |
|
38
|
}, |
|
39
|
], |
|
40
|
}, |
|
41
|
]) |
|
42
|
|
|
43
|
const [order] = await service.listOrders({ id: [createdOrder.id] }) |
|
44
|
|
|
45
|
expect(order).toEqual( |
|
46
|
expect.objectContaining({ |
|
47
|
id: createdOrder.id, |
|
48
|
currency_code: "eur", |
|
49
|
}) |
|
50
|
) |
|
51
|
}) |
|
52
|
|
|
53
|
it("should create an order with billing + shipping address successfully", async () => { |
|
54
|
const [createdOrder] = await service.createOrders([ |
|
55
|
{ |
|
56
|
currency_code: "eur", |
|
57
|
billing_address: { |
|
58
|
first_name: "John", |
|
59
|
last_name: "Doe", |
|
60
|
}, |
|
61
|
shipping_address: { |
|
62
|
first_name: "John", |
|
63
|
last_name: "Doe", |
|
64
|
}, |
|
65
|
}, |
|
66
|
]) |
|
67
|
|
|
68
|
const [order] = await service.listOrders( |
|
69
|
{ id: [createdOrder.id] }, |
|
70
|
{ relations: ["billing_address", "shipping_address"] } |
|
71
|
) |
|
72
|
|
|
73
|
expect(order).toEqual( |
|
74
|
expect.objectContaining({ |
|
75
|
id: createdOrder.id, |
|
76
|
currency_code: "eur", |
|
77
|
billing_address: expect.objectContaining({ |
|
78
|
first_name: "John", |
|
79
|
last_name: "Doe", |
|
80
|
}), |
|
81
|
shipping_address: expect.objectContaining({ |
|
82
|
first_name: "John", |
|
83
|
last_name: "Doe", |
|
84
|
}), |
|
85
|
}) |
|
86
|
) |
|
87
|
}) |
|
88
|
|
|
89
|
it("should create an order with billing id + shipping id successfully", async () => { |
|
90
|
const [createdAddress] = await service.createOrderAddresses([ |
|
91
|
{ |
|
92
|
first_name: "John", |
|
93
|
last_name: "Doe", |
|
94
|
}, |
|
95
|
]) |
|
96
|
|
|
97
|
const [createdOrder] = await service.createOrders([ |
|
98
|
{ |
|
99
|
currency_code: "eur", |
|
100
|
billing_address_id: createdAddress.id, |
|
101
|
shipping_address_id: createdAddress.id, |
|
102
|
}, |
|
103
|
]) |
|
104
|
|
|
105
|
expect(createdOrder).toEqual( |
|
106
|
expect.objectContaining({ |
|
107
|
id: createdOrder.id, |
|
108
|
currency_code: "eur", |
|
109
|
billing_address: expect.objectContaining({ |
|
110
|
id: createdAddress.id, |
|
111
|
first_name: "John", |
|
112
|
last_name: "Doe", |
|
113
|
}), |
|
114
|
shipping_address: expect.objectContaining({ |
|
115
|
id: createdAddress.id, |
|
116
|
first_name: "John", |
|
117
|
last_name: "Doe", |
|
118
|
}), |
|
119
|
}) |
|
120
|
) |
|
121
|
}) |
|
122
|
|
|
123
|
it("should create an order with items", async () => { |
|
124
|
const createdOrder = await service.createOrders({ |
|
125
|
currency_code: "eur", |
|
126
|
items: [ |
|
127
|
{ |
|
128
|
title: "test", |
|
129
|
quantity: 1, |
|
130
|
unit_price: 100, |
|
131
|
}, |
|
132
|
], |
|
133
|
}) |
|
134
|
|
|
135
|
const order = await service.retrieveOrder(createdOrder.id, { |
|
136
|
relations: ["items.item"], |
|
137
|
}) |
|
138
|
|
|
139
|
const serialized = JSON.parse(JSON.stringify(order)) |
|
140
|
expect(serialized).toEqual( |
|
141
|
expect.objectContaining({ |
|
142
|
id: createdOrder.id, |
|
143
|
currency_code: "eur", |
|
144
|
items: expect.arrayContaining([ |
|
145
|
expect.objectContaining({ |
|
146
|
title: "test", |
|
147
|
unit_price: 100, |
|
148
|
}), |
|
149
|
]), |
|
150
|
}) |
|
151
|
) |
|
152
|
}) |
|
153
|
|
|
154
|
it("should create multiple orders with items", async () => { |
|
155
|
const createdOrders = await service.createOrders([ |
|
156
|
{ |
|
157
|
currency_code: "eur", |
|
158
|
items: [ |
|
159
|
{ |
|
160
|
title: "test", |
|
161
|
quantity: 1, |
|
162
|
unit_price: 100, |
|
163
|
}, |
|
164
|
], |
|
165
|
}, |
|
166
|
{ |
|
167
|
currency_code: "usd", |
|
168
|
items: [ |
|
169
|
{ |
|
170
|
title: "test-2", |
|
171
|
quantity: 2, |
|
172
|
unit_price: 200, |
|
173
|
}, |
|
174
|
], |
|
175
|
}, |
|
176
|
]) |
|
177
|
|
|
178
|
const orders = await service.listOrders( |
|
179
|
{ id: createdOrders.map((c) => c.id) }, |
|
180
|
{ |
|
181
|
relations: ["items.item"], |
|
182
|
} |
|
183
|
) |
|
184
|
|
|
185
|
const serialized = JSON.parse(JSON.stringify(orders)) |
|
186
|
expect(serialized).toEqual( |
|
187
|
expect.arrayContaining([ |
|
188
|
expect.objectContaining({ |
|
189
|
currency_code: "eur", |
|
190
|
items: expect.arrayContaining([ |
|
191
|
expect.objectContaining({ |
|
192
|
title: "test", |
|
193
|
unit_price: 100, |
|
194
|
detail: expect.objectContaining({ |
|
195
|
quantity: 1, |
|
196
|
}), |
|
197
|
}), |
|
198
|
]), |
|
199
|
}), |
|
200
|
expect.objectContaining({ |
|
201
|
currency_code: "usd", |
|
202
|
items: expect.arrayContaining([ |
|
203
|
expect.objectContaining({ |
|
204
|
title: "test-2", |
|
205
|
unit_price: 200, |
|
206
|
quantity: 2, |
|
207
|
raw_quantity: expect.objectContaining({ |
|
208
|
value: "2", |
|
209
|
}), |
|
210
|
detail: expect.objectContaining({ |
|
211
|
quantity: 2, |
|
212
|
}), |
|
213
|
}), |
|
214
|
]), |
|
215
|
}), |
|
216
|
]) |
|
217
|
) |
|
218
|
}) |
|
219
|
}) |
|
220
|
|
|
221
|
describe("update", () => { |
|
222
|
it("should throw an error if order does not exist", async () => { |
|
223
|
const error = await service |
|
224
|
.updateOrders([ |
|
225
|
{ |
|
226
|
id: "none-existing", |
|
227
|
}, |
|
228
|
]) |
|
229
|
.catch((e) => e) |
|
230
|
|
|
231
|
expect(error.message).toContain( |
|
232
|
'Order with id "none-existing" not found' |
|
233
|
) |
|
234
|
}) |
|
235
|
|
|
236
|
it("should update an order successfully", async () => { |
|
237
|
const [createdOrder] = await service.createOrders([ |
|
238
|
{ |
|
239
|
currency_code: "eur", |
|
240
|
}, |
|
241
|
]) |
|
242
|
|
|
243
|
const [updatedOrder] = await service.updateOrders([ |
|
244
|
{ |
|
245
|
id: createdOrder.id, |
|
246
|
email: "test@email.com", |
|
247
|
}, |
|
248
|
]) |
|
249
|
|
|
250
|
const [order] = await service.listOrders({ id: [createdOrder.id] }) |
|
251
|
|
|
252
|
expect(order).toEqual( |
|
253
|
expect.objectContaining({ |
|
254
|
id: createdOrder.id, |
|
255
|
currency_code: "eur", |
|
256
|
email: updatedOrder.email, |
|
257
|
}) |
|
258
|
) |
|
259
|
}) |
|
260
|
|
|
261
|
it("should update an order with selector successfully", async () => { |
|
262
|
const createdOrder = await service.createOrders({ |
|
263
|
currency_code: "eur", |
|
264
|
}) |
|
265
|
|
|
266
|
const [updatedOrder] = await service.updateOrders( |
|
267
|
{ id: createdOrder.id }, |
|
268
|
{ |
|
269
|
email: "test@email.com", |
|
270
|
status: OrderStatus.DRAFT, |
|
271
|
} |
|
272
|
) |
|
273
|
|
|
274
|
const [order] = await service.listOrders({ id: [createdOrder.id] }) |
|
275
|
|
|
276
|
expect(order).toEqual( |
|
277
|
expect.objectContaining({ |
|
278
|
id: createdOrder.id, |
|
279
|
status: "draft", |
|
280
|
currency_code: "eur", |
|
281
|
email: updatedOrder.email, |
|
282
|
}) |
|
283
|
) |
|
284
|
}) |
|
285
|
|
|
286
|
it("should update an order with id successfully", async () => { |
|
287
|
const [createdOrder] = await service.createOrders([ |
|
288
|
{ |
|
289
|
currency_code: "eur", |
|
290
|
}, |
|
291
|
]) |
|
292
|
|
|
293
|
const updatedOrder = await service.updateOrders(createdOrder.id, { |
|
294
|
email: "test@email.com", |
|
295
|
}) |
|
296
|
|
|
297
|
const [order] = await service.listOrders({ id: [createdOrder.id] }) |
|
298
|
|
|
299
|
expect(order).toEqual( |
|
300
|
expect.objectContaining({ |
|
301
|
id: createdOrder.id, |
|
302
|
currency_code: "eur", |
|
303
|
email: updatedOrder.email, |
|
304
|
}) |
|
305
|
) |
|
306
|
}) |
|
307
|
}) |
|
308
|
|
|
309
|
describe("delete", () => { |
|
310
|
it("should delete an order successfully", async () => { |
|
311
|
const [createdOrder] = await service.createOrders([ |
|
312
|
{ |
|
313
|
currency_code: "eur", |
|
314
|
}, |
|
315
|
]) |
|
316
|
|
|
317
|
await service.deleteOrders([createdOrder.id]) |
|
318
|
|
|
319
|
const orders = await service.listOrders({ id: [createdOrder.id] }) |
|
320
|
|
|
321
|
expect(orders.length).toEqual(0) |
|
322
|
}) |
|
323
|
}) |
|
324
|
|
|
325
|
describe("createAddresses", () => { |
|
326
|
it("should create an address successfully", async () => { |
|
327
|
const [createdAddress] = await service.createOrderAddresses([ |
|
328
|
{ |
|
329
|
first_name: "John", |
|
330
|
}, |
|
331
|
]) |
|
332
|
|
|
333
|
const [address] = await service.listOrderAddresses({ |
|
334
|
id: [createdAddress.id!], |
|
335
|
}) |
|
336
|
|
|
337
|
expect(address).toEqual( |
|
338
|
expect.objectContaining({ |
|
339
|
id: createdAddress.id, |
|
340
|
first_name: "John", |
|
341
|
}) |
|
342
|
) |
|
343
|
}) |
|
344
|
}) |
|
345
|
|
|
346
|
describe("updateAddresses", () => { |
|
347
|
it("should update an address successfully", async () => { |
|
348
|
const [createdAddress] = await service.createOrderAddresses([ |
|
349
|
{ |
|
350
|
first_name: "John", |
|
351
|
}, |
|
352
|
]) |
|
353
|
|
|
354
|
const [updatedAddress] = await service.updateOrderAddresses([ |
|
355
|
{ id: createdAddress.id!, first_name: "Jane" }, |
|
356
|
]) |
|
357
|
|
|
358
|
expect(updatedAddress).toEqual( |
|
359
|
expect.objectContaining({ |
|
360
|
id: createdAddress.id, |
|
361
|
first_name: "Jane", |
|
362
|
}) |
|
363
|
) |
|
364
|
}) |
|
365
|
}) |
|
366
|
|
|
367
|
describe("deleteAddresses", () => { |
|
368
|
it("should delete an address successfully", async () => { |
|
369
|
const [createdAddress] = await service.createOrderAddresses([ |
|
370
|
{ |
|
371
|
first_name: "John", |
|
372
|
}, |
|
373
|
]) |
|
374
|
|
|
375
|
await service.deleteOrderAddresses([createdAddress.id!]) |
|
376
|
|
|
377
|
const [address] = await service.listOrderAddresses({ |
|
378
|
id: [createdAddress.id!], |
|
379
|
}) |
|
380
|
|
|
381
|
expect(address).toBe(undefined) |
|
382
|
}) |
|
383
|
}) |
|
384
|
|
|
385
|
describe("createLineItems", () => { |
|
386
|
it("should add a line item to order succesfully", async () => { |
|
387
|
const [createdOrder] = await service.createOrders([ |
|
388
|
{ |
|
389
|
currency_code: "eur", |
|
390
|
}, |
|
391
|
]) |
|
392
|
|
|
393
|
await service.createOrderLineItems(createdOrder.id, [ |
|
394
|
{ |
|
395
|
quantity: 1, |
|
396
|
unit_price: 100, |
|
397
|
title: "test", |
|
398
|
}, |
|
399
|
]) |
|
400
|
|
|
401
|
const order = await service.retrieveOrder(createdOrder.id, { |
|
402
|
relations: ["items.item"], |
|
403
|
}) |
|
404
|
|
|
405
|
const serialized = JSON.parse(JSON.stringify(order)) |
|
406
|
expect(serialized.items[0]).toEqual( |
|
407
|
expect.objectContaining({ |
|
408
|
quantity: 1, |
|
409
|
title: "test", |
|
410
|
unit_price: 100, |
|
411
|
}) |
|
412
|
) |
|
413
|
expect(order.items?.length).toBe(1) |
|
414
|
}) |
|
415
|
|
|
416
|
it("should add multiple line items to order succesfully", async () => { |
|
417
|
const [createdOrder] = await service.createOrders([ |
|
418
|
{ |
|
419
|
currency_code: "eur", |
|
420
|
}, |
|
421
|
]) |
|
422
|
|
|
423
|
await service.createOrderLineItems([ |
|
424
|
{ |
|
425
|
quantity: 1, |
|
426
|
unit_price: 100, |
|
427
|
title: "test", |
|
428
|
version: 1, |
|
429
|
order_id: createdOrder.id, |
|
430
|
}, |
|
431
|
{ |
|
432
|
quantity: 2, |
|
433
|
unit_price: 200, |
|
434
|
title: "test-2", |
|
435
|
version: 1, |
|
436
|
order_id: createdOrder.id, |
|
437
|
}, |
|
438
|
]) |
|
439
|
|
|
440
|
const order = await service.retrieveOrder(createdOrder.id, { |
|
441
|
relations: ["items.item"], |
|
442
|
}) |
|
443
|
|
|
444
|
const serialized = JSON.parse(JSON.stringify(order)) |
|
445
|
expect(serialized.items).toEqual( |
|
446
|
expect.arrayContaining([ |
|
447
|
expect.objectContaining({ |
|
448
|
title: "test", |
|
449
|
unit_price: 100, |
|
450
|
quantity: 1, |
|
451
|
}), |
|
452
|
expect.objectContaining({ |
|
453
|
title: "test-2", |
|
454
|
unit_price: 200, |
|
455
|
quantity: 2, |
|
456
|
}), |
|
457
|
]) |
|
458
|
) |
|
459
|
|
|
460
|
expect(order.items?.length).toBe(2) |
|
461
|
}) |
|
462
|
|
|
463
|
it("should add multiple line items to multiple orders succesfully", async () => { |
|
464
|
let [eurOrder, usdOrder] = await service.createOrders([ |
|
465
|
{ |
|
466
|
currency_code: "eur", |
|
467
|
status: OrderStatus.DRAFT, |
|
468
|
}, |
|
469
|
{ |
|
470
|
currency_code: "usd", |
|
471
|
status: OrderStatus.DRAFT, |
|
472
|
}, |
|
473
|
]) |
|
474
|
|
|
475
|
const items = await service.createOrderLineItems([ |
|
476
|
{ |
|
477
|
order_id: eurOrder.id, |
|
478
|
quantity: 1, |
|
479
|
unit_price: 100, |
|
480
|
title: "test", |
|
481
|
}, |
|
482
|
{ |
|
483
|
order_id: usdOrder.id, |
|
484
|
quantity: 1, |
|
485
|
unit_price: 100, |
|
486
|
title: "test", |
|
487
|
}, |
|
488
|
]) |
|
489
|
|
|
490
|
const orders = await service.listOrders( |
|
491
|
{ id: [eurOrder.id, usdOrder.id] }, |
|
492
|
{ relations: ["items.item"] } |
|
493
|
) |
|
494
|
|
|
495
|
eurOrder = orders.find((c) => c.currency_code === "eur")! |
|
496
|
usdOrder = orders.find((c) => c.currency_code === "usd")! |
|
497
|
|
|
498
|
const eurItems = orders.filter((i) => i.id === eurOrder.id)[0].items |
|
499
|
|
|
500
|
const usdItems = orders.filter((i) => i.id === usdOrder.id)[0].items |
|
501
|
|
|
502
|
expect(eurOrder.items[0].id).toBe(eurItems[0].id) |
|
503
|
expect(usdOrder.items[0].id).toBe(usdItems[0].id) |
|
504
|
|
|
505
|
expect(eurOrder.items?.length).toBe(1) |
|
506
|
expect(usdOrder.items?.length).toBe(1) |
|
507
|
}) |
|
508
|
|
|
509
|
it("should throw if order does not exist", async () => { |
|
510
|
const error = await service |
|
511
|
.createOrderLineItems("foo", [ |
|
512
|
{ |
|
513
|
quantity: 1, |
|
514
|
unit_price: 100, |
|
515
|
title: "test", |
|
516
|
tax_lines: [], |
|
517
|
}, |
|
518
|
]) |
|
519
|
.catch((e) => e) |
|
520
|
|
|
521
|
expect(error.message).toContain("Order with id: foo was not found") |
|
522
|
}) |
|
523
|
|
|
524
|
it("should throw an error when required params are not passed adding to a single order", async () => { |
|
525
|
const [createdOrder] = await service.createOrders([ |
|
526
|
{ |
|
527
|
currency_code: "eur", |
|
528
|
}, |
|
529
|
]) |
|
530
|
|
|
531
|
const error = await service |
|
532
|
.createOrderLineItems(createdOrder.id, [ |
|
533
|
{ |
|
534
|
unit_price: 10, |
|
535
|
title: "test", |
|
536
|
}, |
|
537
|
] as any) |
|
538
|
.catch((e) => e) |
|
539
|
|
|
540
|
expect(error.message).toContain( |
|
541
|
"Value for OrderItem.quantity is required, 'undefined' found" |
|
542
|
) |
|
543
|
}) |
|
544
|
|
|
545
|
it("should throw a generic error when required params are not passed using bulk add method", async () => { |
|
546
|
const [createdOrder] = await service.createOrders([ |
|
547
|
{ |
|
548
|
currency_code: "eur", |
|
549
|
}, |
|
550
|
]) |
|
551
|
|
|
552
|
const error = await service |
|
553
|
.createOrderLineItems([ |
|
554
|
{ |
|
555
|
order_id: createdOrder.id, |
|
556
|
unit_price: 10, |
|
557
|
title: "test", |
|
558
|
}, |
|
559
|
] as any) |
|
560
|
.catch((e) => e) |
|
561
|
|
|
562
|
expect(error.message).toContain( |
|
563
|
"Value for OrderItem.quantity is required, 'undefined' found" |
|
564
|
) |
|
565
|
}) |
|
566
|
}) |
|
567
|
|
|
568
|
describe("updateLineItems", () => { |
|
569
|
it("should update a line item in order succesfully with selector approach", async () => { |
|
570
|
const [createdOrder] = await service.createOrders([ |
|
571
|
{ |
|
572
|
currency_code: "eur", |
|
573
|
}, |
|
574
|
]) |
|
575
|
|
|
576
|
const [item] = await service.createOrderLineItems(createdOrder.id, [ |
|
577
|
{ |
|
578
|
quantity: 1, |
|
579
|
unit_price: 100, |
|
580
|
title: "test", |
|
581
|
tax_lines: [], |
|
582
|
}, |
|
583
|
]) |
|
584
|
|
|
585
|
expect(item.title).toBe("test") |
|
586
|
|
|
587
|
const [updatedItem] = await service.updateOrderLineItems( |
|
588
|
{ id: item.id }, |
|
589
|
{ |
|
590
|
title: "test2", |
|
591
|
} |
|
592
|
) |
|
593
|
|
|
594
|
expect(updatedItem.title).toBe("test2") |
|
595
|
}) |
|
596
|
|
|
597
|
it("should update a line item in order succesfully with id approach", async () => { |
|
598
|
const [createdOrder] = await service.createOrders([ |
|
599
|
{ |
|
600
|
currency_code: "eur", |
|
601
|
}, |
|
602
|
]) |
|
603
|
|
|
604
|
const [item] = await service.createOrderLineItems(createdOrder.id, [ |
|
605
|
{ |
|
606
|
quantity: 1, |
|
607
|
unit_price: 100, |
|
608
|
title: "test", |
|
609
|
tax_lines: [], |
|
610
|
}, |
|
611
|
]) |
|
612
|
|
|
613
|
expect(item.title).toBe("test") |
|
614
|
|
|
615
|
const updatedItem = await service.updateOrderLineItems(item.id, { |
|
616
|
title: "test2", |
|
617
|
}) |
|
618
|
|
|
619
|
expect(updatedItem.title).toBe("test2") |
|
620
|
}) |
|
621
|
|
|
622
|
it("should update line items in orders succesfully with multi-selector approach", async () => { |
|
623
|
const [createdOrder] = await service.createOrders([ |
|
624
|
{ |
|
625
|
currency_code: "eur", |
|
626
|
}, |
|
627
|
]) |
|
628
|
|
|
629
|
const items = await service.createOrderLineItems(createdOrder.id, [ |
|
630
|
{ |
|
631
|
quantity: 1, |
|
632
|
unit_price: 100, |
|
633
|
title: "test", |
|
634
|
}, |
|
635
|
{ |
|
636
|
quantity: 2, |
|
637
|
unit_price: 200, |
|
638
|
title: "other-test", |
|
639
|
}, |
|
640
|
]) |
|
641
|
|
|
642
|
expect(items).toEqual( |
|
643
|
expect.arrayContaining([ |
|
644
|
expect.objectContaining({ |
|
645
|
title: "test", |
|
646
|
unit_price: 100, |
|
647
|
}), |
|
648
|
expect.objectContaining({ |
|
649
|
title: "other-test", |
|
650
|
unit_price: 200, |
|
651
|
}), |
|
652
|
]) |
|
653
|
) |
|
654
|
|
|
655
|
const itemTwo = items.find((i) => i.title === "other-test") |
|
656
|
|
|
657
|
await service.updateOrderLineItems([ |
|
658
|
{ |
|
659
|
selector: { unit_price: 100 }, |
|
660
|
data: { |
|
661
|
title: "changed-test", |
|
662
|
quantity: 15, |
|
663
|
}, |
|
664
|
}, |
|
665
|
{ |
|
666
|
selector: { id: itemTwo!.id }, |
|
667
|
data: { |
|
668
|
title: "changed-other-test", |
|
669
|
quantity: 7, |
|
670
|
}, |
|
671
|
}, |
|
672
|
]) |
|
673
|
|
|
674
|
const order = await service.retrieveOrder(createdOrder.id, { |
|
675
|
relations: ["items.item"], |
|
676
|
}) |
|
677
|
|
|
678
|
const serialized = JSON.parse(JSON.stringify(order)) |
|
679
|
expect(serialized.items).toEqual( |
|
680
|
expect.arrayContaining([ |
|
681
|
expect.objectContaining({ |
|
682
|
title: "changed-test", |
|
683
|
unit_price: 100, |
|
684
|
quantity: 15, |
|
685
|
}), |
|
686
|
expect.objectContaining({ |
|
687
|
title: "changed-other-test", |
|
688
|
unit_price: 200, |
|
689
|
quantity: 7, |
|
690
|
}), |
|
691
|
]) |
|
692
|
) |
|
693
|
}) |
|
694
|
}) |
|
695
|
|
|
696
|
describe("deleteLineItems", () => { |
|
697
|
it("should delete a line item succesfully", async () => { |
|
698
|
const [createdOrder] = await service.createOrders([ |
|
699
|
{ |
|
700
|
currency_code: "eur", |
|
701
|
}, |
|
702
|
]) |
|
703
|
|
|
704
|
const [item] = await service.createOrderLineItems(createdOrder.id, [ |
|
705
|
{ |
|
706
|
quantity: 1, |
|
707
|
unit_price: 100, |
|
708
|
title: "test", |
|
709
|
tax_lines: [], |
|
710
|
}, |
|
711
|
]) |
|
712
|
|
|
713
|
expect(item.title).toBe("test") |
|
714
|
|
|
715
|
await service.deleteOrderLineItems([item.id]) |
|
716
|
|
|
717
|
const order = await service.retrieveOrder(createdOrder.id, { |
|
718
|
relations: ["items"], |
|
719
|
}) |
|
720
|
|
|
721
|
expect(order.items?.length).toBe(0) |
|
722
|
}) |
|
723
|
|
|
724
|
it("should delete multiple line items succesfully", async () => { |
|
725
|
const [createdOrder] = await service.createOrders([ |
|
726
|
{ |
|
727
|
currency_code: "eur", |
|
728
|
}, |
|
729
|
]) |
|
730
|
|
|
731
|
const [item, item2] = await service.createOrderLineItems( |
|
732
|
createdOrder.id, |
|
733
|
[ |
|
734
|
{ |
|
735
|
quantity: 1, |
|
736
|
unit_price: 100, |
|
737
|
title: "test", |
|
738
|
}, |
|
739
|
{ |
|
740
|
quantity: 1, |
|
741
|
unit_price: 100, |
|
742
|
title: "test-2", |
|
743
|
}, |
|
744
|
] |
|
745
|
) |
|
746
|
|
|
747
|
await service.deleteOrderLineItems([item.id, item2.id]) |
|
748
|
|
|
749
|
const order = await service.retrieveOrder(createdOrder.id, { |
|
750
|
relations: ["items"], |
|
751
|
}) |
|
752
|
|
|
753
|
expect(order.items?.length).toBe(0) |
|
754
|
}) |
|
755
|
}) |
|
756
|
|
|
757
|
describe("createShippingMethods", () => { |
|
758
|
it("should add a shipping method to order succesfully", async () => { |
|
759
|
const [createdOrder] = await service.createOrders([ |
|
760
|
{ |
|
761
|
currency_code: "eur", |
|
762
|
}, |
|
763
|
]) |
|
764
|
|
|
765
|
const [method] = await service.createOrderShippingMethods( |
|
766
|
createdOrder.id, |
|
767
|
[ |
|
768
|
{ |
|
769
|
amount: 100, |
|
770
|
name: "Test", |
|
771
|
}, |
|
772
|
] |
|
773
|
) |
|
774
|
|
|
775
|
const order = await service.retrieveOrder(createdOrder.id, { |
|
776
|
relations: ["shipping_methods"], |
|
777
|
}) |
|
778
|
|
|
779
|
expect(method.id).toBe(order.shipping_methods![0].id) |
|
780
|
}) |
|
781
|
|
|
782
|
it("should add multiple shipping methods to multiple orders succesfully", async () => { |
|
783
|
let [eurOrder] = await service.createOrders([ |
|
784
|
{ |
|
785
|
currency_code: "eur", |
|
786
|
}, |
|
787
|
]) |
|
788
|
|
|
789
|
let [usdOrder] = await service.createOrders([ |
|
790
|
{ |
|
791
|
currency_code: "usd", |
|
792
|
}, |
|
793
|
]) |
|
794
|
|
|
795
|
const [eurMethod, usdMethod] = |
|
796
|
await service.createOrderShippingMethods([ |
|
797
|
{ |
|
798
|
order_id: eurOrder.id, |
|
799
|
amount: 100, |
|
800
|
name: "Test One", |
|
801
|
}, |
|
802
|
{ |
|
803
|
order_id: usdOrder.id, |
|
804
|
amount: 100, |
|
805
|
name: "Test One", |
|
806
|
}, |
|
807
|
]) |
|
808
|
|
|
809
|
const orders = await service.listOrders( |
|
810
|
{ id: [eurOrder.id, usdOrder.id] }, |
|
811
|
{ relations: ["shipping_methods"] } |
|
812
|
) |
|
813
|
|
|
814
|
eurOrder = orders.find((c) => c.currency_code === "eur")! |
|
815
|
usdOrder = orders.find((c) => c.currency_code === "usd")! |
|
816
|
|
|
817
|
expect(eurOrder.shipping_methods![0].id).toBe(eurMethod.id) |
|
818
|
expect(usdOrder.shipping_methods![0].id).toBe(usdMethod.id) |
|
819
|
|
|
820
|
expect(eurOrder.shipping_methods?.length).toBe(1) |
|
821
|
expect(usdOrder.shipping_methods?.length).toBe(1) |
|
822
|
}) |
|
823
|
}) |
|
824
|
|
|
825
|
describe("setOrderLineItemAdjustments", () => { |
|
826
|
it("should set line item adjustments for an order", async () => { |
|
827
|
const [createdOrder] = await service.createOrders([ |
|
828
|
{ |
|
829
|
currency_code: "eur", |
|
830
|
}, |
|
831
|
]) |
|
832
|
|
|
833
|
const [itemOne] = await service.createOrderLineItems( |
|
834
|
createdOrder.id, |
|
835
|
[ |
|
836
|
{ |
|
837
|
quantity: 1, |
|
838
|
unit_price: 100, |
|
839
|
title: "test", |
|
840
|
}, |
|
841
|
] |
|
842
|
) |
|
843
|
|
|
844
|
const [itemTwo] = await service.createOrderLineItems( |
|
845
|
createdOrder.id, |
|
846
|
[ |
|
847
|
{ |
|
848
|
quantity: 2, |
|
849
|
unit_price: 200, |
|
850
|
title: "test-2", |
|
851
|
}, |
|
852
|
] |
|
853
|
) |
|
854
|
|
|
855
|
const adjustments = await service.setOrderLineItemAdjustments( |
|
856
|
createdOrder.id, |
|
857
|
[ |
|
858
|
{ |
|
859
|
item_id: itemOne.id, |
|
860
|
amount: 100, |
|
861
|
code: "FREE", |
|
862
|
}, |
|
863
|
{ |
|
864
|
item_id: itemTwo.id, |
|
865
|
amount: 200, |
|
866
|
code: "FREE-2", |
|
867
|
}, |
|
868
|
] |
|
869
|
) |
|
870
|
|
|
871
|
expect(adjustments).toEqual( |
|
872
|
expect.arrayContaining([ |
|
873
|
expect.objectContaining({ |
|
874
|
item_id: itemOne.id, |
|
875
|
amount: 100, |
|
876
|
code: "FREE", |
|
877
|
}), |
|
878
|
expect.objectContaining({ |
|
879
|
item_id: itemTwo.id, |
|
880
|
amount: 200, |
|
881
|
code: "FREE-2", |
|
882
|
}), |
|
883
|
]) |
|
884
|
) |
|
885
|
}) |
|
886
|
|
|
887
|
it("should replace line item adjustments for an order", async () => { |
|
888
|
const [createdOrder] = await service.createOrders([ |
|
889
|
{ |
|
890
|
currency_code: "eur", |
|
891
|
}, |
|
892
|
]) |
|
893
|
|
|
894
|
const [itemOne] = await service.createOrderLineItems( |
|
895
|
createdOrder.id, |
|
896
|
[ |
|
897
|
{ |
|
898
|
quantity: 1, |
|
899
|
unit_price: 100, |
|
900
|
title: "test", |
|
901
|
}, |
|
902
|
] |
|
903
|
) |
|
904
|
|
|
905
|
const adjustments = await service.setOrderLineItemAdjustments( |
|
906
|
createdOrder.id, |
|
907
|
[ |
|
908
|
{ |
|
909
|
item_id: itemOne.id, |
|
910
|
amount: 100, |
|
911
|
code: "FREE", |
|
912
|
}, |
|
913
|
] |
|
914
|
) |
|
915
|
|
|
916
|
expect(adjustments).toEqual( |
|
917
|
expect.arrayContaining([ |
|
918
|
expect.objectContaining({ |
|
919
|
item_id: itemOne.id, |
|
920
|
amount: 100, |
|
921
|
code: "FREE", |
|
922
|
}), |
|
923
|
]) |
|
924
|
) |
|
925
|
|
|
926
|
await service.setOrderLineItemAdjustments(createdOrder.id, [ |
|
927
|
{ |
|
928
|
item_id: itemOne.id, |
|
929
|
amount: 50, |
|
930
|
code: "50%", |
|
931
|
}, |
|
932
|
]) |
|
933
|
|
|
934
|
const order = await service.retrieveOrder(createdOrder.id, { |
|
935
|
relations: ["items.item.adjustments"], |
|
936
|
}) |
|
937
|
|
|
938
|
const serialized = JSON.parse(JSON.stringify(order)) |
|
939
|
expect(serialized.items).toEqual( |
|
940
|
expect.arrayContaining([ |
|
941
|
expect.objectContaining({ |
|
942
|
id: itemOne.id, |
|
943
|
adjustments: expect.arrayContaining([ |
|
944
|
expect.objectContaining({ |
|
945
|
item_id: itemOne.id, |
|
946
|
amount: 50, |
|
947
|
code: "50%", |
|
948
|
}), |
|
949
|
]), |
|
950
|
}), |
|
951
|
]) |
|
952
|
) |
|
953
|
|
|
954
|
expect(order.items.length).toBe(1) |
|
955
|
expect(order.items[0].adjustments?.length).toBe(1) |
|
956
|
}) |
|
957
|
|
|
958
|
it("should delete all line item adjustments for an order", async () => { |
|
959
|
const [createdOrder] = await service.createOrders([ |
|
960
|
{ |
|
961
|
currency_code: "eur", |
|
962
|
}, |
|
963
|
]) |
|
964
|
|
|
965
|
const [itemOne] = await service.createOrderLineItems( |
|
966
|
createdOrder.id, |
|
967
|
[ |
|
968
|
{ |
|
969
|
quantity: 1, |
|
970
|
unit_price: 100, |
|
971
|
title: "test", |
|
972
|
}, |
|
973
|
] |
|
974
|
) |
|
975
|
|
|
976
|
const adjustments = await service.setOrderLineItemAdjustments( |
|
977
|
createdOrder.id, |
|
978
|
[ |
|
979
|
{ |
|
980
|
item_id: itemOne.id, |
|
981
|
amount: 100, |
|
982
|
code: "FREE", |
|
983
|
}, |
|
984
|
] |
|
985
|
) |
|
986
|
|
|
987
|
expect(adjustments).toEqual( |
|
988
|
expect.arrayContaining([ |
|
989
|
expect.objectContaining({ |
|
990
|
item_id: itemOne.id, |
|
991
|
amount: 100, |
|
992
|
code: "FREE", |
|
993
|
}), |
|
994
|
]) |
|
995
|
) |
|
996
|
|
|
997
|
await service.setOrderLineItemAdjustments(createdOrder.id, []) |
|
998
|
|
|
999
|
const order = await service.retrieveOrder(createdOrder.id, { |
|
1000
|
relations: ["items.item.adjustments"], |
|
1001
|
}) |
|
1002
|
|
|
1003
|
expect(order.items).toEqual( |
|
1004
|
expect.arrayContaining([ |
|
1005
|
expect.objectContaining({ |
|
1006
|
id: itemOne.id, |
|
1007
|
adjustments: [], |
|
1008
|
}), |
|
1009
|
]) |
|
1010
|
) |
|
1011
|
|
|
1012
|
expect(order.items.length).toBe(1) |
|
1013
|
expect(order.items[0].adjustments.length).toBe(0) |
|
1014
|
}) |
|
1015
|
|
|
1016
|
it("should update line item adjustments for an order", async () => { |
|
1017
|
const [createdOrder] = await service.createOrders([ |
|
1018
|
{ |
|
1019
|
currency_code: "eur", |
|
1020
|
}, |
|
1021
|
]) |
|
1022
|
|
|
1023
|
const [itemOne] = await service.createOrderLineItems( |
|
1024
|
createdOrder.id, |
|
1025
|
[ |
|
1026
|
{ |
|
1027
|
quantity: 1, |
|
1028
|
unit_price: 100, |
|
1029
|
title: "test", |
|
1030
|
}, |
|
1031
|
] |
|
1032
|
) |
|
1033
|
|
|
1034
|
const adjustments = await service.setOrderLineItemAdjustments( |
|
1035
|
createdOrder.id, |
|
1036
|
[ |
|
1037
|
{ |
|
1038
|
item_id: itemOne.id, |
|
1039
|
amount: 100, |
|
1040
|
code: "FREE", |
|
1041
|
}, |
|
1042
|
] |
|
1043
|
) |
|
1044
|
|
|
1045
|
expect(adjustments).toEqual( |
|
1046
|
expect.arrayContaining([ |
|
1047
|
expect.objectContaining({ |
|
1048
|
item_id: itemOne.id, |
|
1049
|
amount: 100, |
|
1050
|
code: "FREE", |
|
1051
|
}), |
|
1052
|
]) |
|
1053
|
) |
|
1054
|
|
|
1055
|
await service.setOrderLineItemAdjustments(createdOrder.id, [ |
|
1056
|
{ |
|
1057
|
id: adjustments[0].id, |
|
1058
|
item_id: itemOne.id, |
|
1059
|
amount: 50, |
|
1060
|
code: "50%", |
|
1061
|
}, |
|
1062
|
]) |
|
1063
|
|
|
1064
|
const order = await service.retrieveOrder(createdOrder.id, { |
|
1065
|
relations: ["items.item.adjustments"], |
|
1066
|
}) |
|
1067
|
|
|
1068
|
const serialized = JSON.parse(JSON.stringify(order)) |
|
1069
|
expect(serialized.items).toEqual( |
|
1070
|
expect.arrayContaining([ |
|
1071
|
expect.objectContaining({ |
|
1072
|
id: itemOne.id, |
|
1073
|
adjustments: [ |
|
1074
|
expect.objectContaining({ |
|
1075
|
id: adjustments[0].id, |
|
1076
|
item_id: itemOne.id, |
|
1077
|
amount: 50, |
|
1078
|
code: "50%", |
|
1079
|
}), |
|
1080
|
], |
|
1081
|
}), |
|
1082
|
]) |
|
1083
|
) |
|
1084
|
|
|
1085
|
expect(order.items.length).toBe(1) |
|
1086
|
expect(order.items[0].adjustments.length).toBe(1) |
|
1087
|
}) |
|
1088
|
}) |
|
1089
|
|
|
1090
|
describe("createLineItemAdjustments", () => { |
|
1091
|
it("should add line item adjustments for items in an order", async () => { |
|
1092
|
const [createdOrder] = await service.createOrders([ |
|
1093
|
{ |
|
1094
|
currency_code: "eur", |
|
1095
|
}, |
|
1096
|
]) |
|
1097
|
|
|
1098
|
const [itemOne] = await service.createOrderLineItems( |
|
1099
|
createdOrder.id, |
|
1100
|
[ |
|
1101
|
{ |
|
1102
|
quantity: 1, |
|
1103
|
unit_price: 100, |
|
1104
|
title: "test", |
|
1105
|
}, |
|
1106
|
] |
|
1107
|
) |
|
1108
|
|
|
1109
|
const adjustments = await service.createOrderLineItemAdjustments( |
|
1110
|
createdOrder.id, |
|
1111
|
[ |
|
1112
|
{ |
|
1113
|
item_id: itemOne.id, |
|
1114
|
amount: 100, |
|
1115
|
code: "FREE", |
|
1116
|
}, |
|
1117
|
] |
|
1118
|
) |
|
1119
|
|
|
1120
|
expect(adjustments).toEqual( |
|
1121
|
expect.arrayContaining([ |
|
1122
|
expect.objectContaining({ |
|
1123
|
item_id: itemOne.id, |
|
1124
|
amount: 100, |
|
1125
|
code: "FREE", |
|
1126
|
}), |
|
1127
|
]) |
|
1128
|
) |
|
1129
|
}) |
|
1130
|
|
|
1131
|
it("should add multiple line item adjustments for multiple line items", async () => { |
|
1132
|
const [createdOrder] = await service.createOrders([ |
|
1133
|
{ |
|
1134
|
currency_code: "eur", |
|
1135
|
}, |
|
1136
|
]) |
|
1137
|
|
|
1138
|
const [itemOne] = await service.createOrderLineItems( |
|
1139
|
createdOrder.id, |
|
1140
|
[ |
|
1141
|
{ |
|
1142
|
quantity: 1, |
|
1143
|
unit_price: 100, |
|
1144
|
title: "test", |
|
1145
|
}, |
|
1146
|
] |
|
1147
|
) |
|
1148
|
const [itemTwo] = await service.createOrderLineItems( |
|
1149
|
createdOrder.id, |
|
1150
|
[ |
|
1151
|
{ |
|
1152
|
quantity: 2, |
|
1153
|
unit_price: 200, |
|
1154
|
title: "test-2", |
|
1155
|
}, |
|
1156
|
] |
|
1157
|
) |
|
1158
|
|
|
1159
|
const adjustments = await service.createOrderLineItemAdjustments( |
|
1160
|
createdOrder.id, |
|
1161
|
[ |
|
1162
|
{ |
|
1163
|
item_id: itemOne.id, |
|
1164
|
amount: 100, |
|
1165
|
code: "FREE", |
|
1166
|
}, |
|
1167
|
{ |
|
1168
|
item_id: itemTwo.id, |
|
1169
|
amount: 150, |
|
1170
|
code: "CODE-2", |
|
1171
|
}, |
|
1172
|
] |
|
1173
|
) |
|
1174
|
|
|
1175
|
expect(adjustments).toEqual( |
|
1176
|
expect.arrayContaining([ |
|
1177
|
expect.objectContaining({ |
|
1178
|
item_id: itemOne.id, |
|
1179
|
amount: 100, |
|
1180
|
code: "FREE", |
|
1181
|
}), |
|
1182
|
expect.objectContaining({ |
|
1183
|
item_id: itemTwo.id, |
|
1184
|
amount: 150, |
|
1185
|
code: "CODE-2", |
|
1186
|
}), |
|
1187
|
]) |
|
1188
|
) |
|
1189
|
}) |
|
1190
|
|
|
1191
|
it("should add line item adjustments for line items on multiple orders", async () => { |
|
1192
|
let [orderOne, orderTwo] = await service.createOrders([ |
|
1193
|
{ |
|
1194
|
currency_code: "eur", |
|
1195
|
}, |
|
1196
|
{ |
|
1197
|
currency_code: "usd", |
|
1198
|
}, |
|
1199
|
]) |
|
1200
|
|
|
1201
|
const [itemOne] = await service.createOrderLineItems(orderOne.id, [ |
|
1202
|
{ |
|
1203
|
quantity: 1, |
|
1204
|
unit_price: 100, |
|
1205
|
title: "test", |
|
1206
|
}, |
|
1207
|
]) |
|
1208
|
const [itemTwo] = await service.createOrderLineItems(orderTwo.id, [ |
|
1209
|
{ |
|
1210
|
quantity: 2, |
|
1211
|
unit_price: 200, |
|
1212
|
title: "test-2", |
|
1213
|
}, |
|
1214
|
]) |
|
1215
|
|
|
1216
|
await service.createOrderLineItemAdjustments([ |
|
1217
|
// item from order one |
|
1218
|
{ |
|
1219
|
item_id: itemOne.id, |
|
1220
|
amount: 125, |
|
1221
|
code: "FREE", |
|
1222
|
}, |
|
1223
|
// item from order two |
|
1224
|
{ |
|
1225
|
item_id: itemTwo.id, |
|
1226
|
amount: 150, |
|
1227
|
code: "CODE-2", |
|
1228
|
}, |
|
1229
|
]) |
|
1230
|
|
|
1231
|
const [checkOrderOne, checkOrderTwo] = JSON.parse( |
|
1232
|
JSON.stringify( |
|
1233
|
await service.listOrders( |
|
1234
|
{}, |
|
1235
|
{ relations: ["items.item.adjustments"] } |
|
1236
|
) |
|
1237
|
) |
|
1238
|
) |
|
1239
|
|
|
1240
|
expect(checkOrderOne.items).toEqual( |
|
1241
|
expect.arrayContaining([ |
|
1242
|
expect.objectContaining({ |
|
1243
|
quantity: 1, |
|
1244
|
adjustments: expect.arrayContaining([ |
|
1245
|
expect.objectContaining({ |
|
1246
|
item_id: itemOne.id, |
|
1247
|
amount: 125, |
|
1248
|
code: "FREE", |
|
1249
|
}), |
|
1250
|
]), |
|
1251
|
}), |
|
1252
|
]) |
|
1253
|
) |
|
1254
|
|
|
1255
|
expect(checkOrderTwo.items).toEqual( |
|
1256
|
expect.arrayContaining([ |
|
1257
|
expect.objectContaining({ |
|
1258
|
quantity: 2, |
|
1259
|
adjustments: expect.arrayContaining([ |
|
1260
|
expect.objectContaining({ |
|
1261
|
item_id: itemTwo.id, |
|
1262
|
amount: 150, |
|
1263
|
code: "CODE-2", |
|
1264
|
}), |
|
1265
|
]), |
|
1266
|
}), |
|
1267
|
]) |
|
1268
|
) |
|
1269
|
}) |
|
1270
|
}) |
|
1271
|
|
|
1272
|
describe("deleteLineItemAdjustments", () => { |
|
1273
|
it("should delete a line item succesfully", async () => { |
|
1274
|
const [createdOrder] = await service.createOrders([ |
|
1275
|
{ |
|
1276
|
currency_code: "eur", |
|
1277
|
}, |
|
1278
|
]) |
|
1279
|
|
|
1280
|
const [item] = await service.createOrderLineItems(createdOrder.id, [ |
|
1281
|
{ |
|
1282
|
quantity: 1, |
|
1283
|
unit_price: 100, |
|
1284
|
title: "test", |
|
1285
|
}, |
|
1286
|
]) |
|
1287
|
|
|
1288
|
const [adjustment] = await service.createOrderLineItemAdjustments( |
|
1289
|
createdOrder.id, |
|
1290
|
[ |
|
1291
|
{ |
|
1292
|
item_id: item.id, |
|
1293
|
amount: 50, |
|
1294
|
}, |
|
1295
|
] |
|
1296
|
) |
|
1297
|
|
|
1298
|
expect(adjustment.item_id).toBe(item.id) |
|
1299
|
|
|
1300
|
await service.deleteOrderLineItemAdjustments(adjustment.id) |
|
1301
|
|
|
1302
|
const adjustments = await service.listOrderLineItemAdjustments({ |
|
1303
|
item_id: item.id, |
|
1304
|
}) |
|
1305
|
|
|
1306
|
expect(adjustments?.length).toBe(0) |
|
1307
|
}) |
|
1308
|
|
|
1309
|
it("should delete a line item succesfully with selector", async () => { |
|
1310
|
const [createdOrder] = await service.createOrders([ |
|
1311
|
{ |
|
1312
|
currency_code: "eur", |
|
1313
|
}, |
|
1314
|
]) |
|
1315
|
|
|
1316
|
const [item] = await service.createOrderLineItems(createdOrder.id, [ |
|
1317
|
{ |
|
1318
|
quantity: 1, |
|
1319
|
unit_price: 100, |
|
1320
|
title: "test", |
|
1321
|
}, |
|
1322
|
]) |
|
1323
|
|
|
1324
|
const [adjustment] = await service.createOrderLineItemAdjustments( |
|
1325
|
createdOrder.id, |
|
1326
|
[ |
|
1327
|
{ |
|
1328
|
item_id: item.id, |
|
1329
|
amount: 50, |
|
1330
|
}, |
|
1331
|
] |
|
1332
|
) |
|
1333
|
|
|
1334
|
expect(adjustment.item_id).toBe(item.id) |
|
1335
|
|
|
1336
|
await service.deleteOrderLineItemAdjustments({ item_id: item.id }) |
|
1337
|
|
|
1338
|
const adjustments = await service.listOrderLineItemAdjustments({ |
|
1339
|
item_id: item.id, |
|
1340
|
}) |
|
1341
|
|
|
1342
|
expect(adjustments?.length).toBe(0) |
|
1343
|
}) |
|
1344
|
}) |
|
1345
|
|
|
1346
|
describe("setShippingMethodAdjustments", () => { |
|
1347
|
it("should set shipping method adjustments for an order", async () => { |
|
1348
|
const [createdOrder] = await service.createOrders([ |
|
1349
|
{ |
|
1350
|
currency_code: "eur", |
|
1351
|
}, |
|
1352
|
]) |
|
1353
|
|
|
1354
|
const [shippingMethodOne] = await service.createOrderShippingMethods( |
|
1355
|
createdOrder.id, |
|
1356
|
[ |
|
1357
|
{ |
|
1358
|
amount: 100, |
|
1359
|
name: "test", |
|
1360
|
}, |
|
1361
|
] |
|
1362
|
) |
|
1363
|
|
|
1364
|
const [shippingMethodTwo] = await service.createOrderShippingMethods( |
|
1365
|
createdOrder.id, |
|
1366
|
[ |
|
1367
|
{ |
|
1368
|
amount: 200, |
|
1369
|
name: "test-2", |
|
1370
|
}, |
|
1371
|
] |
|
1372
|
) |
|
1373
|
|
|
1374
|
const adjustments = await service.setOrderShippingMethodAdjustments( |
|
1375
|
createdOrder.id, |
|
1376
|
[ |
|
1377
|
{ |
|
1378
|
shipping_method_id: shippingMethodOne.id, |
|
1379
|
amount: 100, |
|
1380
|
code: "FREE", |
|
1381
|
}, |
|
1382
|
{ |
|
1383
|
shipping_method_id: shippingMethodTwo.id, |
|
1384
|
amount: 200, |
|
1385
|
code: "FREE-2", |
|
1386
|
}, |
|
1387
|
] |
|
1388
|
) |
|
1389
|
|
|
1390
|
expect(adjustments).toEqual( |
|
1391
|
expect.arrayContaining([ |
|
1392
|
expect.objectContaining({ |
|
1393
|
shipping_method_id: shippingMethodOne.id, |
|
1394
|
amount: 100, |
|
1395
|
code: "FREE", |
|
1396
|
}), |
|
1397
|
expect.objectContaining({ |
|
1398
|
shipping_method_id: shippingMethodTwo.id, |
|
1399
|
amount: 200, |
|
1400
|
code: "FREE-2", |
|
1401
|
}), |
|
1402
|
]) |
|
1403
|
) |
|
1404
|
}) |
|
1405
|
|
|
1406
|
it("should replace shipping method adjustments for an order", async () => { |
|
1407
|
const [createdOrder] = await service.createOrders([ |
|
1408
|
{ |
|
1409
|
currency_code: "eur", |
|
1410
|
}, |
|
1411
|
]) |
|
1412
|
|
|
1413
|
const [shippingMethodOne] = await service.createOrderShippingMethods( |
|
1414
|
createdOrder.id, |
|
1415
|
[ |
|
1416
|
{ |
|
1417
|
amount: 100, |
|
1418
|
name: "test", |
|
1419
|
}, |
|
1420
|
] |
|
1421
|
) |
|
1422
|
|
|
1423
|
const adjustments = await service.setOrderShippingMethodAdjustments( |
|
1424
|
createdOrder.id, |
|
1425
|
[ |
|
1426
|
{ |
|
1427
|
shipping_method_id: shippingMethodOne.id, |
|
1428
|
amount: 100, |
|
1429
|
code: "FREE", |
|
1430
|
}, |
|
1431
|
] |
|
1432
|
) |
|
1433
|
|
|
1434
|
expect(adjustments).toEqual( |
|
1435
|
expect.arrayContaining([ |
|
1436
|
expect.objectContaining({ |
|
1437
|
shipping_method_id: shippingMethodOne.id, |
|
1438
|
amount: 100, |
|
1439
|
code: "FREE", |
|
1440
|
}), |
|
1441
|
]) |
|
1442
|
) |
|
1443
|
|
|
1444
|
await service.setOrderShippingMethodAdjustments(createdOrder.id, [ |
|
1445
|
{ |
|
1446
|
shipping_method_id: shippingMethodOne.id, |
|
1447
|
amount: 50, |
|
1448
|
code: "50%", |
|
1449
|
}, |
|
1450
|
]) |
|
1451
|
|
|
1452
|
const order = await service.retrieveOrder(createdOrder.id, { |
|
1453
|
relations: ["shipping_methods.adjustments"], |
|
1454
|
}) |
|
1455
|
|
|
1456
|
const serialized = JSON.parse(JSON.stringify(order)) |
|
1457
|
|
|
1458
|
expect(serialized.shipping_methods).toEqual( |
|
1459
|
expect.arrayContaining([ |
|
1460
|
expect.objectContaining({ |
|
1461
|
id: shippingMethodOne.id, |
|
1462
|
order_id: createdOrder.id, |
|
1463
|
adjustments: expect.arrayContaining([ |
|
1464
|
expect.objectContaining({ |
|
1465
|
shipping_method_id: shippingMethodOne.id, |
|
1466
|
amount: 50, |
|
1467
|
code: "50%", |
|
1468
|
}), |
|
1469
|
]), |
|
1470
|
}), |
|
1471
|
]) |
|
1472
|
) |
|
1473
|
|
|
1474
|
expect(order.shipping_methods?.length).toBe(1) |
|
1475
|
expect(order.shipping_methods?.[0].adjustments?.length).toBe(1) |
|
1476
|
}) |
|
1477
|
|
|
1478
|
it("should delete all shipping method adjustments for an order", async () => { |
|
1479
|
const [createdOrder] = await service.createOrders([ |
|
1480
|
{ |
|
1481
|
currency_code: "eur", |
|
1482
|
}, |
|
1483
|
]) |
|
1484
|
|
|
1485
|
const [shippingMethodOne] = await service.createOrderShippingMethods( |
|
1486
|
createdOrder.id, |
|
1487
|
[ |
|
1488
|
{ |
|
1489
|
amount: 100, |
|
1490
|
name: "test", |
|
1491
|
}, |
|
1492
|
] |
|
1493
|
) |
|
1494
|
|
|
1495
|
const adjustments = await service.setOrderShippingMethodAdjustments( |
|
1496
|
createdOrder.id, |
|
1497
|
[ |
|
1498
|
{ |
|
1499
|
shipping_method_id: shippingMethodOne.id, |
|
1500
|
amount: 100, |
|
1501
|
code: "FREE", |
|
1502
|
}, |
|
1503
|
] |
|
1504
|
) |
|
1505
|
|
|
1506
|
expect(adjustments).toEqual( |
|
1507
|
expect.arrayContaining([ |
|
1508
|
expect.objectContaining({ |
|
1509
|
shipping_method_id: shippingMethodOne.id, |
|
1510
|
amount: 100, |
|
1511
|
code: "FREE", |
|
1512
|
}), |
|
1513
|
]) |
|
1514
|
) |
|
1515
|
|
|
1516
|
await service.setOrderShippingMethodAdjustments(createdOrder.id, []) |
|
1517
|
|
|
1518
|
const order = await service.retrieveOrder(createdOrder.id, { |
|
1519
|
relations: ["shipping_methods.adjustments"], |
|
1520
|
}) |
|
1521
|
|
|
1522
|
expect(order.shipping_methods).toEqual( |
|
1523
|
expect.arrayContaining([ |
|
1524
|
expect.objectContaining({ |
|
1525
|
id: shippingMethodOne.id, |
|
1526
|
adjustments: [], |
|
1527
|
}), |
|
1528
|
]) |
|
1529
|
) |
|
1530
|
|
|
1531
|
expect(order.shipping_methods?.length).toBe(1) |
|
1532
|
expect(order.shipping_methods?.[0].adjustments?.length).toBe(0) |
|
1533
|
}) |
|
1534
|
|
|
1535
|
it("should update shipping method adjustments for an order", async () => { |
|
1536
|
const [createdOrder] = await service.createOrders([ |
|
1537
|
{ |
|
1538
|
currency_code: "eur", |
|
1539
|
}, |
|
1540
|
]) |
|
1541
|
|
|
1542
|
const [shippingMethodOne] = await service.createOrderShippingMethods( |
|
1543
|
createdOrder.id, |
|
1544
|
[ |
|
1545
|
{ |
|
1546
|
amount: 100, |
|
1547
|
name: "test", |
|
1548
|
}, |
|
1549
|
] |
|
1550
|
) |
|
1551
|
|
|
1552
|
const adjustments = await service.setOrderShippingMethodAdjustments( |
|
1553
|
createdOrder.id, |
|
1554
|
[ |
|
1555
|
{ |
|
1556
|
shipping_method_id: shippingMethodOne.id, |
|
1557
|
amount: 100, |
|
1558
|
code: "FREE", |
|
1559
|
}, |
|
1560
|
] |
|
1561
|
) |
|
1562
|
|
|
1563
|
expect(adjustments).toEqual( |
|
1564
|
expect.arrayContaining([ |
|
1565
|
expect.objectContaining({ |
|
1566
|
shipping_method_id: shippingMethodOne.id, |
|
1567
|
amount: 100, |
|
1568
|
code: "FREE", |
|
1569
|
}), |
|
1570
|
]) |
|
1571
|
) |
|
1572
|
|
|
1573
|
await service.setOrderShippingMethodAdjustments(createdOrder.id, [ |
|
1574
|
{ |
|
1575
|
id: adjustments[0].id, |
|
1576
|
amount: 50, |
|
1577
|
code: "50%", |
|
1578
|
}, |
|
1579
|
]) |
|
1580
|
|
|
1581
|
const order = await service.retrieveOrder(createdOrder.id, { |
|
1582
|
relations: ["shipping_methods.adjustments"], |
|
1583
|
}) |
|
1584
|
|
|
1585
|
const serialized = JSON.parse(JSON.stringify(order)) |
|
1586
|
expect(serialized.shipping_methods).toEqual( |
|
1587
|
expect.arrayContaining([ |
|
1588
|
expect.objectContaining({ |
|
1589
|
id: shippingMethodOne.id, |
|
1590
|
adjustments: [ |
|
1591
|
expect.objectContaining({ |
|
1592
|
id: adjustments[0].id, |
|
1593
|
shipping_method_id: shippingMethodOne.id, |
|
1594
|
amount: 50, |
|
1595
|
code: "50%", |
|
1596
|
}), |
|
1597
|
], |
|
1598
|
}), |
|
1599
|
]) |
|
1600
|
) |
|
1601
|
|
|
1602
|
expect(order.shipping_methods?.length).toBe(1) |
|
1603
|
expect(order.shipping_methods?.[0].adjustments?.length).toBe(1) |
|
1604
|
}) |
|
1605
|
}) |
|
1606
|
|
|
1607
|
describe("createShippingMethodAdjustments", () => { |
|
1608
|
it("should add shipping method adjustments in an order", async () => { |
|
1609
|
const [createdOrder] = await service.createOrders([ |
|
1610
|
{ |
|
1611
|
currency_code: "eur", |
|
1612
|
}, |
|
1613
|
]) |
|
1614
|
|
|
1615
|
const [shippingMethodOne] = await service.createOrderShippingMethods( |
|
1616
|
createdOrder.id, |
|
1617
|
[ |
|
1618
|
{ |
|
1619
|
amount: 100, |
|
1620
|
name: "test", |
|
1621
|
}, |
|
1622
|
] |
|
1623
|
) |
|
1624
|
|
|
1625
|
const adjustments = |
|
1626
|
await service.createOrderShippingMethodAdjustments( |
|
1627
|
createdOrder.id, |
|
1628
|
[ |
|
1629
|
{ |
|
1630
|
shipping_method_id: shippingMethodOne.id, |
|
1631
|
amount: 100, |
|
1632
|
code: "FREE", |
|
1633
|
}, |
|
1634
|
] |
|
1635
|
) |
|
1636
|
|
|
1637
|
expect(adjustments).toEqual( |
|
1638
|
expect.arrayContaining([ |
|
1639
|
expect.objectContaining({ |
|
1640
|
shipping_method_id: shippingMethodOne.id, |
|
1641
|
amount: 100, |
|
1642
|
code: "FREE", |
|
1643
|
}), |
|
1644
|
]) |
|
1645
|
) |
|
1646
|
}) |
|
1647
|
|
|
1648
|
it("should add multiple shipping method adjustments for multiple shipping methods", async () => { |
|
1649
|
const [createdOrder] = await service.createOrders([ |
|
1650
|
{ |
|
1651
|
currency_code: "eur", |
|
1652
|
}, |
|
1653
|
]) |
|
1654
|
|
|
1655
|
const [shippingMethodOne] = await service.createOrderShippingMethods( |
|
1656
|
createdOrder.id, |
|
1657
|
[ |
|
1658
|
{ |
|
1659
|
amount: 100, |
|
1660
|
name: "test", |
|
1661
|
}, |
|
1662
|
] |
|
1663
|
) |
|
1664
|
const [shippingMethodTwo] = await service.createOrderShippingMethods( |
|
1665
|
createdOrder.id, |
|
1666
|
[ |
|
1667
|
{ |
|
1668
|
amount: 200, |
|
1669
|
name: "test-2", |
|
1670
|
}, |
|
1671
|
] |
|
1672
|
) |
|
1673
|
|
|
1674
|
const adjustments = |
|
1675
|
await service.createOrderShippingMethodAdjustments( |
|
1676
|
createdOrder.id, |
|
1677
|
[ |
|
1678
|
{ |
|
1679
|
shipping_method_id: shippingMethodOne.id, |
|
1680
|
amount: 100, |
|
1681
|
code: "FREE", |
|
1682
|
}, |
|
1683
|
{ |
|
1684
|
shipping_method_id: shippingMethodTwo.id, |
|
1685
|
amount: 150, |
|
1686
|
code: "CODE-2", |
|
1687
|
}, |
|
1688
|
] |
|
1689
|
) |
|
1690
|
|
|
1691
|
expect(adjustments).toEqual( |
|
1692
|
expect.arrayContaining([ |
|
1693
|
expect.objectContaining({ |
|
1694
|
shipping_method_id: shippingMethodOne.id, |
|
1695
|
amount: 100, |
|
1696
|
code: "FREE", |
|
1697
|
}), |
|
1698
|
expect.objectContaining({ |
|
1699
|
shipping_method_id: shippingMethodTwo.id, |
|
1700
|
amount: 150, |
|
1701
|
code: "CODE-2", |
|
1702
|
}), |
|
1703
|
]) |
|
1704
|
) |
|
1705
|
}) |
|
1706
|
|
|
1707
|
it("should add shipping method adjustments for shipping methods on multiple orders", async () => { |
|
1708
|
const [orderOne] = await service.createOrders([ |
|
1709
|
{ |
|
1710
|
currency_code: "eur", |
|
1711
|
}, |
|
1712
|
]) |
|
1713
|
const [orderTwo] = await service.createOrders([ |
|
1714
|
{ |
|
1715
|
currency_code: "usd", |
|
1716
|
}, |
|
1717
|
]) |
|
1718
|
|
|
1719
|
const [shippingMethodOne] = await service.createOrderShippingMethods( |
|
1720
|
orderOne.id, |
|
1721
|
[ |
|
1722
|
{ |
|
1723
|
amount: 100, |
|
1724
|
name: "test", |
|
1725
|
}, |
|
1726
|
] |
|
1727
|
) |
|
1728
|
const [shippingMethodTwo] = await service.createOrderShippingMethods( |
|
1729
|
orderTwo.id, |
|
1730
|
[ |
|
1731
|
{ |
|
1732
|
amount: 200, |
|
1733
|
name: "test-2", |
|
1734
|
}, |
|
1735
|
] |
|
1736
|
) |
|
1737
|
|
|
1738
|
await service.createOrderShippingMethodAdjustments([ |
|
1739
|
// item from order one |
|
1740
|
{ |
|
1741
|
shipping_method_id: shippingMethodOne.id, |
|
1742
|
amount: 100, |
|
1743
|
code: "FREE", |
|
1744
|
}, |
|
1745
|
// item from order two |
|
1746
|
{ |
|
1747
|
shipping_method_id: shippingMethodTwo.id, |
|
1748
|
amount: 150, |
|
1749
|
code: "CODE-2", |
|
1750
|
}, |
|
1751
|
]) |
|
1752
|
|
|
1753
|
const orderOneMethods = await (service as any).listOrderShippings( |
|
1754
|
{ order_id: orderOne.id }, |
|
1755
|
{ relations: ["shipping_method.adjustments"] } |
|
1756
|
) |
|
1757
|
|
|
1758
|
const orderTwoMethods = await (service as any).listOrderShippings( |
|
1759
|
{ order_id: orderTwo.id }, |
|
1760
|
{ relations: ["shipping_method.adjustments"] } |
|
1761
|
) |
|
1762
|
|
|
1763
|
expect(orderOneMethods).toEqual( |
|
1764
|
expect.arrayContaining([ |
|
1765
|
expect.objectContaining({ |
|
1766
|
shipping_method: expect.objectContaining({ |
|
1767
|
adjustments: expect.arrayContaining([ |
|
1768
|
expect.objectContaining({ |
|
1769
|
shipping_method_id: shippingMethodOne.id, |
|
1770
|
amount: 100, |
|
1771
|
code: "FREE", |
|
1772
|
}), |
|
1773
|
]), |
|
1774
|
}), |
|
1775
|
}), |
|
1776
|
]) |
|
1777
|
) |
|
1778
|
expect(orderTwoMethods).toEqual( |
|
1779
|
expect.arrayContaining([ |
|
1780
|
expect.objectContaining({ |
|
1781
|
shipping_method: expect.objectContaining({ |
|
1782
|
adjustments: expect.arrayContaining([ |
|
1783
|
expect.objectContaining({ |
|
1784
|
shipping_method_id: shippingMethodTwo.id, |
|
1785
|
amount: 150, |
|
1786
|
code: "CODE-2", |
|
1787
|
}), |
|
1788
|
]), |
|
1789
|
}), |
|
1790
|
}), |
|
1791
|
]) |
|
1792
|
) |
|
1793
|
}) |
|
1794
|
|
|
1795
|
it("should throw if shipping method is not associated with order", async () => { |
|
1796
|
const [orderOne] = await service.createOrders([ |
|
1797
|
{ |
|
1798
|
currency_code: "eur", |
|
1799
|
}, |
|
1800
|
]) |
|
1801
|
|
|
1802
|
const [orderTwo] = await service.createOrders([ |
|
1803
|
{ |
|
1804
|
currency_code: "eur", |
|
1805
|
}, |
|
1806
|
]) |
|
1807
|
|
|
1808
|
const [shippingMethodOne] = await service.createOrderShippingMethods( |
|
1809
|
orderOne.id, |
|
1810
|
[ |
|
1811
|
{ |
|
1812
|
amount: 100, |
|
1813
|
name: "test", |
|
1814
|
}, |
|
1815
|
] |
|
1816
|
) |
|
1817
|
|
|
1818
|
const error = await service |
|
1819
|
.createOrderShippingMethodAdjustments(orderTwo.id, [ |
|
1820
|
{ |
|
1821
|
shipping_method_id: shippingMethodOne.id, |
|
1822
|
amount: 100, |
|
1823
|
code: "FREE", |
|
1824
|
}, |
|
1825
|
]) |
|
1826
|
.catch((e) => e) |
|
1827
|
|
|
1828
|
expect(error.message).toBe( |
|
1829
|
`Shipping method with id ${shippingMethodOne.id} does not exist on order with id ${orderTwo.id}` |
|
1830
|
) |
|
1831
|
}) |
|
1832
|
}) |
|
1833
|
|
|
1834
|
describe("deleteShippingMethodAdjustments", () => { |
|
1835
|
it("should delete a shipping method adjustment succesfully", async () => { |
|
1836
|
const [createdOrder] = await service.createOrders([ |
|
1837
|
{ |
|
1838
|
currency_code: "eur", |
|
1839
|
}, |
|
1840
|
]) |
|
1841
|
|
|
1842
|
const [method] = await service.createOrderShippingMethods( |
|
1843
|
createdOrder.id, |
|
1844
|
[ |
|
1845
|
{ |
|
1846
|
amount: 100, |
|
1847
|
name: "test", |
|
1848
|
}, |
|
1849
|
] |
|
1850
|
) |
|
1851
|
|
|
1852
|
const [adjustment] = |
|
1853
|
await service.createOrderShippingMethodAdjustments( |
|
1854
|
createdOrder.id, |
|
1855
|
[ |
|
1856
|
{ |
|
1857
|
shipping_method_id: method.id, |
|
1858
|
amount: 50, |
|
1859
|
code: "50%", |
|
1860
|
}, |
|
1861
|
] |
|
1862
|
) |
|
1863
|
|
|
1864
|
expect(adjustment.shipping_method_id).toBe(method.id) |
|
1865
|
|
|
1866
|
await service.deleteOrderShippingMethodAdjustments(adjustment.id) |
|
1867
|
|
|
1868
|
const adjustments = await service.listOrderShippingMethodAdjustments({ |
|
1869
|
shipping_method_id: method.id, |
|
1870
|
}) |
|
1871
|
|
|
1872
|
expect(adjustments?.length).toBe(0) |
|
1873
|
}) |
|
1874
|
|
|
1875
|
it("should delete a shipping method succesfully with selector", async () => { |
|
1876
|
const [createdOrder] = await service.createOrders([ |
|
1877
|
{ |
|
1878
|
currency_code: "eur", |
|
1879
|
}, |
|
1880
|
]) |
|
1881
|
|
|
1882
|
const [shippingMethod] = await service.createOrderShippingMethods( |
|
1883
|
createdOrder.id, |
|
1884
|
[ |
|
1885
|
{ |
|
1886
|
amount: 100, |
|
1887
|
name: "test", |
|
1888
|
}, |
|
1889
|
] |
|
1890
|
) |
|
1891
|
|
|
1892
|
const [adjustment] = |
|
1893
|
await service.createOrderShippingMethodAdjustments( |
|
1894
|
createdOrder.id, |
|
1895
|
[ |
|
1896
|
{ |
|
1897
|
shipping_method_id: shippingMethod.id, |
|
1898
|
amount: 50, |
|
1899
|
code: "50%", |
|
1900
|
}, |
|
1901
|
] |
|
1902
|
) |
|
1903
|
|
|
1904
|
expect(adjustment.shipping_method_id).toBe(shippingMethod.id) |
|
1905
|
|
|
1906
|
await service.deleteOrderShippingMethodAdjustments({ |
|
1907
|
shipping_method_id: shippingMethod.id, |
|
1908
|
}) |
|
1909
|
|
|
1910
|
const adjustments = await service.listOrderShippingMethodAdjustments({ |
|
1911
|
shipping_method_id: shippingMethod.id, |
|
1912
|
}) |
|
1913
|
|
|
1914
|
expect(adjustments?.length).toBe(0) |
|
1915
|
}) |
|
1916
|
}) |
|
1917
|
|
|
1918
|
describe("setOrderLineItemTaxLines", () => { |
|
1919
|
it("should set line item tax lines for an order", async () => { |
|
1920
|
const [createdOrder] = await service.createOrders([ |
|
1921
|
{ |
|
1922
|
currency_code: "eur", |
|
1923
|
}, |
|
1924
|
]) |
|
1925
|
|
|
1926
|
const [itemOne] = await service.createOrderLineItems( |
|
1927
|
createdOrder.id, |
|
1928
|
[ |
|
1929
|
{ |
|
1930
|
quantity: 1, |
|
1931
|
unit_price: 100, |
|
1932
|
title: "test", |
|
1933
|
}, |
|
1934
|
] |
|
1935
|
) |
|
1936
|
|
|
1937
|
const [itemTwo] = await service.createOrderLineItems( |
|
1938
|
createdOrder.id, |
|
1939
|
[ |
|
1940
|
{ |
|
1941
|
quantity: 2, |
|
1942
|
unit_price: 200, |
|
1943
|
title: "test-2", |
|
1944
|
}, |
|
1945
|
] |
|
1946
|
) |
|
1947
|
|
|
1948
|
const taxLines = await service.setOrderLineItemTaxLines( |
|
1949
|
createdOrder.id, |
|
1950
|
[ |
|
1951
|
{ |
|
1952
|
item_id: itemOne.id, |
|
1953
|
rate: 20, |
|
1954
|
code: "TX", |
|
1955
|
}, |
|
1956
|
{ |
|
1957
|
item_id: itemTwo.id, |
|
1958
|
rate: 20, |
|
1959
|
code: "TX", |
|
1960
|
}, |
|
1961
|
] |
|
1962
|
) |
|
1963
|
|
|
1964
|
expect(taxLines).toEqual( |
|
1965
|
expect.arrayContaining([ |
|
1966
|
expect.objectContaining({ |
|
1967
|
item_id: itemOne.id, |
|
1968
|
rate: 20, |
|
1969
|
code: "TX", |
|
1970
|
}), |
|
1971
|
expect.objectContaining({ |
|
1972
|
item_id: itemTwo.id, |
|
1973
|
rate: 20, |
|
1974
|
code: "TX", |
|
1975
|
}), |
|
1976
|
]) |
|
1977
|
) |
|
1978
|
}) |
|
1979
|
|
|
1980
|
it("should replace line item tax lines for an order", async () => { |
|
1981
|
const [createdOrder] = await service.createOrders([ |
|
1982
|
{ |
|
1983
|
currency_code: "eur", |
|
1984
|
}, |
|
1985
|
]) |
|
1986
|
|
|
1987
|
const [itemOne] = await service.createOrderLineItems( |
|
1988
|
createdOrder.id, |
|
1989
|
[ |
|
1990
|
{ |
|
1991
|
quantity: 1, |
|
1992
|
unit_price: 100, |
|
1993
|
title: "test", |
|
1994
|
}, |
|
1995
|
] |
|
1996
|
) |
|
1997
|
|
|
1998
|
const taxLines = await service.setOrderLineItemTaxLines( |
|
1999
|
createdOrder.id, |
|
2000
|
[ |
|
2001
|
{ |
|
2002
|
item_id: itemOne.id, |
|
2003
|
rate: 20, |
|
2004
|
code: "TX", |
|
2005
|
}, |
|
2006
|
] |
|
2007
|
) |
|
2008
|
|
|
2009
|
expect(taxLines).toEqual( |
|
2010
|
expect.arrayContaining([ |
|
2011
|
expect.objectContaining({ |
|
2012
|
item_id: itemOne.id, |
|
2013
|
rate: 20, |
|
2014
|
code: "TX", |
|
2015
|
}), |
|
2016
|
]) |
|
2017
|
) |
|
2018
|
|
|
2019
|
await service.deleteOrderLineItemTaxLines(taxLines.map((o) => o.id)) |
|
2020
|
await service.setOrderLineItemTaxLines(createdOrder.id, [ |
|
2021
|
{ |
|
2022
|
item_id: itemOne.id, |
|
2023
|
rate: 25, |
|
2024
|
code: "TX-2", |
|
2025
|
}, |
|
2026
|
]) |
|
2027
|
|
|
2028
|
const order = await service.retrieveOrder(createdOrder.id, { |
|
2029
|
relations: ["items.item.tax_lines"], |
|
2030
|
}) |
|
2031
|
|
|
2032
|
const serialized = JSON.parse(JSON.stringify(order)) |
|
2033
|
expect(serialized.items).toEqual( |
|
2034
|
expect.arrayContaining([ |
|
2035
|
expect.objectContaining({ |
|
2036
|
id: itemOne.id, |
|
2037
|
tax_lines: expect.arrayContaining([ |
|
2038
|
expect.objectContaining({ |
|
2039
|
item_id: itemOne.id, |
|
2040
|
rate: 25, |
|
2041
|
code: "TX-2", |
|
2042
|
}), |
|
2043
|
]), |
|
2044
|
}), |
|
2045
|
]) |
|
2046
|
) |
|
2047
|
|
|
2048
|
expect(order.items.length).toBe(1) |
|
2049
|
expect(order.items[0].tax_lines.length).toBe(1) |
|
2050
|
}) |
|
2051
|
|
|
2052
|
it("should delete all line item tax lines for an order", async () => { |
|
2053
|
const [createdOrder] = await service.createOrders([ |
|
2054
|
{ |
|
2055
|
currency_code: "eur", |
|
2056
|
}, |
|
2057
|
]) |
|
2058
|
|
|
2059
|
const [itemOne] = await service.createOrderLineItems( |
|
2060
|
createdOrder.id, |
|
2061
|
[ |
|
2062
|
{ |
|
2063
|
quantity: 1, |
|
2064
|
unit_price: 100, |
|
2065
|
title: "test", |
|
2066
|
}, |
|
2067
|
] |
|
2068
|
) |
|
2069
|
|
|
2070
|
const taxLines = await service.setOrderLineItemTaxLines( |
|
2071
|
createdOrder.id, |
|
2072
|
[ |
|
2073
|
{ |
|
2074
|
item_id: itemOne.id, |
|
2075
|
rate: 20, |
|
2076
|
code: "TX", |
|
2077
|
}, |
|
2078
|
] |
|
2079
|
) |
|
2080
|
|
|
2081
|
expect(taxLines).toEqual( |
|
2082
|
expect.arrayContaining([ |
|
2083
|
expect.objectContaining({ |
|
2084
|
item_id: itemOne.id, |
|
2085
|
rate: 20, |
|
2086
|
code: "TX", |
|
2087
|
}), |
|
2088
|
]) |
|
2089
|
) |
|
2090
|
|
|
2091
|
await service.deleteOrderLineItemTaxLines(taxLines.map((o) => o.id)) |
|
2092
|
|
|
2093
|
const order = await service.retrieveOrder(createdOrder.id, { |
|
2094
|
relations: ["items.item.tax_lines"], |
|
2095
|
}) |
|
2096
|
|
|
2097
|
expect(order.items).toEqual( |
|
2098
|
expect.arrayContaining([ |
|
2099
|
expect.objectContaining({ |
|
2100
|
id: itemOne.id, |
|
2101
|
tax_lines: [], |
|
2102
|
}), |
|
2103
|
]) |
|
2104
|
) |
|
2105
|
|
|
2106
|
expect(order.items.length).toBe(1) |
|
2107
|
expect(order.items[0].tax_lines.length).toBe(0) |
|
2108
|
}) |
|
2109
|
|
|
2110
|
it("should update line item tax lines for an order", async () => { |
|
2111
|
const [createdOrder] = await service.createOrders([ |
|
2112
|
{ |
|
2113
|
currency_code: "eur", |
|
2114
|
}, |
|
2115
|
]) |
|
2116
|
|
|
2117
|
const [itemOne] = await service.createOrderLineItems( |
|
2118
|
createdOrder.id, |
|
2119
|
[ |
|
2120
|
{ |
|
2121
|
quantity: 1, |
|
2122
|
unit_price: 100, |
|
2123
|
title: "test", |
|
2124
|
}, |
|
2125
|
] |
|
2126
|
) |
|
2127
|
|
|
2128
|
const taxLines = await service.setOrderLineItemTaxLines( |
|
2129
|
createdOrder.id, |
|
2130
|
[ |
|
2131
|
{ |
|
2132
|
item_id: itemOne.id, |
|
2133
|
rate: 20, |
|
2134
|
code: "TX", |
|
2135
|
}, |
|
2136
|
] |
|
2137
|
) |
|
2138
|
|
|
2139
|
expect(taxLines).toEqual( |
|
2140
|
expect.arrayContaining([ |
|
2141
|
expect.objectContaining({ |
|
2142
|
item_id: itemOne.id, |
|
2143
|
rate: 20, |
|
2144
|
code: "TX", |
|
2145
|
}), |
|
2146
|
]) |
|
2147
|
) |
|
2148
|
|
|
2149
|
await service.setOrderLineItemTaxLines(createdOrder.id, [ |
|
2150
|
{ |
|
2151
|
id: taxLines[0].id, |
|
2152
|
item_id: itemOne.id, |
|
2153
|
rate: 25, |
|
2154
|
code: "TX", |
|
2155
|
}, |
|
2156
|
]) |
|
2157
|
|
|
2158
|
const order = await service.retrieveOrder(createdOrder.id, { |
|
2159
|
relations: ["items.item.tax_lines"], |
|
2160
|
}) |
|
2161
|
|
|
2162
|
const serialized = JSON.parse(JSON.stringify(order)) |
|
2163
|
expect(serialized.items).toEqual( |
|
2164
|
expect.arrayContaining([ |
|
2165
|
expect.objectContaining({ |
|
2166
|
id: itemOne.id, |
|
2167
|
tax_lines: [ |
|
2168
|
expect.objectContaining({ |
|
2169
|
id: taxLines[0].id, |
|
2170
|
item_id: itemOne.id, |
|
2171
|
rate: 25, |
|
2172
|
code: "TX", |
|
2173
|
}), |
|
2174
|
], |
|
2175
|
}), |
|
2176
|
]) |
|
2177
|
) |
|
2178
|
|
|
2179
|
expect(order.items.length).toBe(1) |
|
2180
|
expect(order.items[0].tax_lines.length).toBe(1) |
|
2181
|
}) |
|
2182
|
|
|
2183
|
it("should delete, update, and create line item tax lines for an order", async () => { |
|
2184
|
const [createdOrder] = await service.createOrders([ |
|
2185
|
{ |
|
2186
|
currency_code: "eur", |
|
2187
|
}, |
|
2188
|
]) |
|
2189
|
|
|
2190
|
const [itemOne] = await service.createOrderLineItems( |
|
2191
|
createdOrder.id, |
|
2192
|
[ |
|
2193
|
{ |
|
2194
|
quantity: 1, |
|
2195
|
unit_price: 100, |
|
2196
|
title: "test", |
|
2197
|
}, |
|
2198
|
] |
|
2199
|
) |
|
2200
|
|
|
2201
|
const taxLines = await service.setOrderLineItemTaxLines( |
|
2202
|
createdOrder.id, |
|
2203
|
[ |
|
2204
|
{ |
|
2205
|
item_id: itemOne.id, |
|
2206
|
rate: 20, |
|
2207
|
code: "TX", |
|
2208
|
}, |
|
2209
|
{ |
|
2210
|
item_id: itemOne.id, |
|
2211
|
rate: 25, |
|
2212
|
code: "TX", |
|
2213
|
}, |
|
2214
|
] |
|
2215
|
) |
|
2216
|
|
|
2217
|
expect(taxLines).toEqual( |
|
2218
|
expect.arrayContaining([ |
|
2219
|
expect.objectContaining({ |
|
2220
|
item_id: itemOne.id, |
|
2221
|
rate: 20, |
|
2222
|
code: "TX", |
|
2223
|
}), |
|
2224
|
expect.objectContaining({ |
|
2225
|
item_id: itemOne.id, |
|
2226
|
rate: 25, |
|
2227
|
code: "TX", |
|
2228
|
}), |
|
2229
|
]) |
|
2230
|
) |
|
2231
|
|
|
2232
|
const taxLine = taxLines.find((tx) => tx.item_id === itemOne.id)! |
|
2233
|
|
|
2234
|
await service.setOrderLineItemTaxLines(createdOrder.id, [ |
|
2235
|
// update |
|
2236
|
{ |
|
2237
|
id: taxLine.id, |
|
2238
|
rate: 40, |
|
2239
|
code: "TX", |
|
2240
|
}, |
|
2241
|
// create |
|
2242
|
{ |
|
2243
|
item_id: itemOne.id, |
|
2244
|
rate: 32, |
|
2245
|
code: "TX-2", |
|
2246
|
} as CreateOrderLineItemTaxLineDTO, |
|
2247
|
]) |
|
2248
|
|
|
2249
|
const order = await service.retrieveOrder(createdOrder.id, { |
|
2250
|
relations: ["items.tax_lines"], |
|
2251
|
}) |
|
2252
|
|
|
2253
|
const serialized = JSON.parse(JSON.stringify(order)) |
|
2254
|
|
|
2255
|
expect(serialized.items).toEqual( |
|
2256
|
expect.arrayContaining([ |
|
2257
|
expect.objectContaining({ |
|
2258
|
id: itemOne.id, |
|
2259
|
tax_lines: expect.arrayContaining([ |
|
2260
|
expect.objectContaining({ |
|
2261
|
id: taxLine!.id, |
|
2262
|
item_id: itemOne.id, |
|
2263
|
rate: 40, |
|
2264
|
code: "TX", |
|
2265
|
}), |
|
2266
|
expect.objectContaining({ |
|
2267
|
item_id: itemOne.id, |
|
2268
|
rate: 32, |
|
2269
|
code: "TX-2", |
|
2270
|
}), |
|
2271
|
]), |
|
2272
|
}), |
|
2273
|
]) |
|
2274
|
) |
|
2275
|
|
|
2276
|
expect(order.items.length).toBe(1) |
|
2277
|
expect(order.items[0].tax_lines.length).toBe(2) |
|
2278
|
}) |
|
2279
|
|
|
2280
|
it("should persist metadata and data on line item tax lines", async () => { |
|
2281
|
const [createdOrder] = await service.createOrders([ |
|
2282
|
{ |
|
2283
|
currency_code: "eur", |
|
2284
|
}, |
|
2285
|
]) |
|
2286
|
|
|
2287
|
const [item] = await service.createOrderLineItems(createdOrder.id, [ |
|
2288
|
{ |
|
2289
|
quantity: 1, |
|
2290
|
unit_price: 100, |
|
2291
|
title: "test", |
|
2292
|
}, |
|
2293
|
]) |
|
2294
|
|
|
2295
|
const taxLines = await service.setOrderLineItemTaxLines( |
|
2296
|
createdOrder.id, |
|
2297
|
[ |
|
2298
|
{ |
|
2299
|
item_id: item.id, |
|
2300
|
rate: 8.9, |
|
2301
|
code: "US-GA", |
|
2302
|
metadata: { custom_key: "custom_value" }, |
|
2303
|
data: { |
|
2304
|
state_rate: 4.0, |
|
2305
|
county_rate: 3.0, |
|
2306
|
city_rate: 1.9, |
|
2307
|
}, |
|
2308
|
}, |
|
2309
|
] |
|
2310
|
) |
|
2311
|
|
|
2312
|
expect(taxLines).toEqual( |
|
2313
|
expect.arrayContaining([ |
|
2314
|
expect.objectContaining({ |
|
2315
|
item_id: item.id, |
|
2316
|
rate: 8.9, |
|
2317
|
code: "US-GA", |
|
2318
|
metadata: { custom_key: "custom_value" }, |
|
2319
|
data: { |
|
2320
|
state_rate: 4.0, |
|
2321
|
county_rate: 3.0, |
|
2322
|
city_rate: 1.9, |
|
2323
|
}, |
|
2324
|
}), |
|
2325
|
]) |
|
2326
|
) |
|
2327
|
}) |
|
2328
|
}) |
|
2329
|
|
|
2330
|
describe("createLineItemAdjustments", () => { |
|
2331
|
it("should add line item tax lines for items in an order", async () => { |
|
2332
|
const [createdOrder] = await service.createOrders([ |
|
2333
|
{ |
|
2334
|
currency_code: "eur", |
|
2335
|
}, |
|
2336
|
]) |
|
2337
|
|
|
2338
|
const [itemOne] = await service.createOrderLineItems( |
|
2339
|
createdOrder.id, |
|
2340
|
[ |
|
2341
|
{ |
|
2342
|
quantity: 1, |
|
2343
|
unit_price: 100, |
|
2344
|
title: "test", |
|
2345
|
}, |
|
2346
|
] |
|
2347
|
) |
|
2348
|
|
|
2349
|
const taxLines = await service.createOrderLineItemTaxLines( |
|
2350
|
createdOrder.id, |
|
2351
|
[ |
|
2352
|
{ |
|
2353
|
item_id: itemOne.id, |
|
2354
|
rate: 20, |
|
2355
|
code: "TX", |
|
2356
|
}, |
|
2357
|
] |
|
2358
|
) |
|
2359
|
|
|
2360
|
expect(taxLines).toEqual( |
|
2361
|
expect.arrayContaining([ |
|
2362
|
expect.objectContaining({ |
|
2363
|
item_id: itemOne.id, |
|
2364
|
rate: 20, |
|
2365
|
code: "TX", |
|
2366
|
}), |
|
2367
|
]) |
|
2368
|
) |
|
2369
|
}) |
|
2370
|
|
|
2371
|
it("should add multiple line item tax lines for multiple line items", async () => { |
|
2372
|
const [createdOrder] = await service.createOrders([ |
|
2373
|
{ |
|
2374
|
currency_code: "eur", |
|
2375
|
}, |
|
2376
|
]) |
|
2377
|
|
|
2378
|
const [itemOne] = await service.createOrderLineItems( |
|
2379
|
createdOrder.id, |
|
2380
|
[ |
|
2381
|
{ |
|
2382
|
quantity: 1, |
|
2383
|
unit_price: 100, |
|
2384
|
title: "test", |
|
2385
|
}, |
|
2386
|
] |
|
2387
|
) |
|
2388
|
const [itemTwo] = await service.createOrderLineItems( |
|
2389
|
createdOrder.id, |
|
2390
|
[ |
|
2391
|
{ |
|
2392
|
quantity: 2, |
|
2393
|
unit_price: 200, |
|
2394
|
title: "test-2", |
|
2395
|
}, |
|
2396
|
] |
|
2397
|
) |
|
2398
|
|
|
2399
|
const taxLines = await service.createOrderLineItemTaxLines( |
|
2400
|
createdOrder.id, |
|
2401
|
[ |
|
2402
|
{ |
|
2403
|
item_id: itemOne.id, |
|
2404
|
rate: 20, |
|
2405
|
code: "TX", |
|
2406
|
}, |
|
2407
|
{ |
|
2408
|
item_id: itemTwo.id, |
|
2409
|
rate: 20, |
|
2410
|
code: "TX", |
|
2411
|
}, |
|
2412
|
] |
|
2413
|
) |
|
2414
|
|
|
2415
|
expect(taxLines).toEqual( |
|
2416
|
expect.arrayContaining([ |
|
2417
|
expect.objectContaining({ |
|
2418
|
item_id: itemOne.id, |
|
2419
|
rate: 20, |
|
2420
|
code: "TX", |
|
2421
|
}), |
|
2422
|
expect.objectContaining({ |
|
2423
|
item_id: itemTwo.id, |
|
2424
|
rate: 20, |
|
2425
|
code: "TX", |
|
2426
|
}), |
|
2427
|
]) |
|
2428
|
) |
|
2429
|
}) |
|
2430
|
|
|
2431
|
it("should add line item tax lines for line items on multiple orders", async () => { |
|
2432
|
const [orderOne] = await service.createOrders([ |
|
2433
|
{ |
|
2434
|
currency_code: "eur", |
|
2435
|
}, |
|
2436
|
]) |
|
2437
|
const [orderTwo] = await service.createOrders([ |
|
2438
|
{ |
|
2439
|
currency_code: "usd", |
|
2440
|
}, |
|
2441
|
]) |
|
2442
|
|
|
2443
|
const [itemOne] = await service.createOrderLineItems(orderOne.id, [ |
|
2444
|
{ |
|
2445
|
quantity: 1, |
|
2446
|
unit_price: 100, |
|
2447
|
title: "test", |
|
2448
|
}, |
|
2449
|
]) |
|
2450
|
const [itemTwo] = await service.createOrderLineItems(orderTwo.id, [ |
|
2451
|
{ |
|
2452
|
quantity: 2, |
|
2453
|
unit_price: 200, |
|
2454
|
title: "test-2", |
|
2455
|
}, |
|
2456
|
]) |
|
2457
|
|
|
2458
|
await service.createOrderLineItemTaxLines([ |
|
2459
|
// item from order one |
|
2460
|
{ |
|
2461
|
item_id: itemOne.id, |
|
2462
|
rate: 20, |
|
2463
|
code: "TX", |
|
2464
|
}, |
|
2465
|
// item from order two |
|
2466
|
{ |
|
2467
|
item_id: itemTwo.id, |
|
2468
|
rate: 25, |
|
2469
|
code: "TX-2", |
|
2470
|
}, |
|
2471
|
]) |
|
2472
|
|
|
2473
|
const [checkOrderOne, checkOrderTwo] = JSON.parse( |
|
2474
|
JSON.stringify( |
|
2475
|
await service.listOrders( |
|
2476
|
{}, |
|
2477
|
{ relations: ["items.item.tax_lines"] } |
|
2478
|
) |
|
2479
|
) |
|
2480
|
) |
|
2481
|
|
|
2482
|
expect(checkOrderOne.items).toEqual( |
|
2483
|
expect.arrayContaining([ |
|
2484
|
expect.objectContaining({ |
|
2485
|
quantity: 1, |
|
2486
|
tax_lines: expect.arrayContaining([ |
|
2487
|
expect.objectContaining({ |
|
2488
|
item_id: itemOne.id, |
|
2489
|
rate: 20, |
|
2490
|
code: "TX", |
|
2491
|
}), |
|
2492
|
]), |
|
2493
|
}), |
|
2494
|
]) |
|
2495
|
) |
|
2496
|
|
|
2497
|
expect(checkOrderTwo.items).toEqual( |
|
2498
|
expect.arrayContaining([ |
|
2499
|
expect.objectContaining({ |
|
2500
|
quantity: 2, |
|
2501
|
tax_lines: expect.arrayContaining([ |
|
2502
|
expect.objectContaining({ |
|
2503
|
item_id: itemTwo.id, |
|
2504
|
rate: 25, |
|
2505
|
code: "TX-2", |
|
2506
|
}), |
|
2507
|
]), |
|
2508
|
}), |
|
2509
|
]) |
|
2510
|
) |
|
2511
|
}) |
|
2512
|
}) |
|
2513
|
|
|
2514
|
describe("deleteLineItemAdjustments", () => { |
|
2515
|
it("should delete line item tax line succesfully", async () => { |
|
2516
|
const [createdOrder] = await service.createOrders([ |
|
2517
|
{ |
|
2518
|
currency_code: "eur", |
|
2519
|
}, |
|
2520
|
]) |
|
2521
|
|
|
2522
|
const [item] = await service.createOrderLineItems(createdOrder.id, [ |
|
2523
|
{ |
|
2524
|
quantity: 1, |
|
2525
|
unit_price: 100, |
|
2526
|
title: "test", |
|
2527
|
}, |
|
2528
|
]) |
|
2529
|
|
|
2530
|
const [taxLine] = await service.createOrderLineItemTaxLines( |
|
2531
|
createdOrder.id, |
|
2532
|
[ |
|
2533
|
{ |
|
2534
|
item_id: item.id, |
|
2535
|
rate: 20, |
|
2536
|
code: "TX", |
|
2537
|
}, |
|
2538
|
] |
|
2539
|
) |
|
2540
|
|
|
2541
|
expect(taxLine.item_id).toBe(item.id) |
|
2542
|
|
|
2543
|
await service.deleteOrderLineItemTaxLines(taxLine.id) |
|
2544
|
|
|
2545
|
const taxLines = await service.listOrderLineItemTaxLines({ |
|
2546
|
item_id: item.id, |
|
2547
|
}) |
|
2548
|
|
|
2549
|
expect(taxLines?.length).toBe(0) |
|
2550
|
}) |
|
2551
|
|
|
2552
|
it("should delete line item tax lines succesfully with selector", async () => { |
|
2553
|
const [createdOrder] = await service.createOrders([ |
|
2554
|
{ |
|
2555
|
currency_code: "eur", |
|
2556
|
}, |
|
2557
|
]) |
|
2558
|
|
|
2559
|
const [item] = await service.createOrderLineItems(createdOrder.id, [ |
|
2560
|
{ |
|
2561
|
quantity: 1, |
|
2562
|
unit_price: 100, |
|
2563
|
title: "test", |
|
2564
|
}, |
|
2565
|
]) |
|
2566
|
|
|
2567
|
const [taxLine] = await service.createOrderLineItemTaxLines( |
|
2568
|
createdOrder.id, |
|
2569
|
[ |
|
2570
|
{ |
|
2571
|
item_id: item.id, |
|
2572
|
rate: 20, |
|
2573
|
code: "TX", |
|
2574
|
}, |
|
2575
|
] |
|
2576
|
) |
|
2577
|
|
|
2578
|
expect(taxLine.item_id).toBe(item.id) |
|
2579
|
|
|
2580
|
await service.deleteOrderLineItemTaxLines({ item_id: item.id }) |
|
2581
|
|
|
2582
|
const taxLines = await service.listOrderLineItemTaxLines({ |
|
2583
|
item_id: item.id, |
|
2584
|
}) |
|
2585
|
|
|
2586
|
expect(taxLines?.length).toBe(0) |
|
2587
|
}) |
|
2588
|
}) |
|
2589
|
}) |
|
2590
|
}, |
|
2591
|
}) |