|
1
|
import { ITaxModuleService } from "@medusajs/framework/types" |
|
2
|
import { Module, Modules, toMikroORMEntity } from "@medusajs/framework/utils" |
|
3
|
import { moduleIntegrationTestRunner } from "@medusajs/test-utils" |
|
4
|
import { TaxModuleService } from "@services" |
|
5
|
import { setupTaxStructure } from "../utils/setup-tax-structure" |
|
6
|
|
|
7
|
jest.setTimeout(30000) |
|
8
|
|
|
9
|
moduleIntegrationTestRunner<ITaxModuleService>({ |
|
10
|
moduleName: Modules.TAX, |
|
11
|
testSuite: ({ service }) => { |
|
12
|
describe("TaxModuleService", function () { |
|
13
|
it(`should export the appropriate linkable configuration`, () => { |
|
14
|
const linkable = Module(toMikroORMEntity(Modules.TAX), { |
|
15
|
service: TaxModuleService, |
|
16
|
}).linkable |
|
17
|
|
|
18
|
expect(Object.keys(linkable)).toEqual([ |
|
19
|
"taxRate", |
|
20
|
"taxRegion", |
|
21
|
"taxRateRule", |
|
22
|
"taxProvider", |
|
23
|
]) |
|
24
|
|
|
25
|
Object.keys(linkable).forEach((key) => { |
|
26
|
delete linkable[key].toJSON |
|
27
|
}) |
|
28
|
|
|
29
|
expect(linkable).toEqual({ |
|
30
|
taxRate: { |
|
31
|
id: { |
|
32
|
linkable: "tax_rate_id", |
|
33
|
entity: "TaxRate", |
|
34
|
primaryKey: "id", |
|
35
|
serviceName: "tax", |
|
36
|
field: "taxRate", |
|
37
|
}, |
|
38
|
}, |
|
39
|
taxRegion: { |
|
40
|
id: { |
|
41
|
linkable: "tax_region_id", |
|
42
|
entity: "TaxRegion", |
|
43
|
primaryKey: "id", |
|
44
|
serviceName: "tax", |
|
45
|
field: "taxRegion", |
|
46
|
}, |
|
47
|
}, |
|
48
|
taxRateRule: { |
|
49
|
id: { |
|
50
|
linkable: "tax_rate_rule_id", |
|
51
|
entity: "TaxRateRule", |
|
52
|
primaryKey: "id", |
|
53
|
serviceName: "tax", |
|
54
|
field: "taxRateRule", |
|
55
|
}, |
|
56
|
}, |
|
57
|
taxProvider: { |
|
58
|
id: { |
|
59
|
linkable: "tax_provider_id", |
|
60
|
entity: "TaxProvider", |
|
61
|
primaryKey: "id", |
|
62
|
serviceName: "tax", |
|
63
|
field: "taxProvider", |
|
64
|
}, |
|
65
|
}, |
|
66
|
}) |
|
67
|
}) |
|
68
|
|
|
69
|
it("should create tax region", async () => { |
|
70
|
const region = await service.createTaxRegions({ |
|
71
|
country_code: "US", |
|
72
|
default_tax_rate: { |
|
73
|
name: "Test Rate", |
|
74
|
rate: 0.2, |
|
75
|
code: "TEST", |
|
76
|
}, |
|
77
|
}) |
|
78
|
|
|
79
|
expect(region).toEqual( |
|
80
|
expect.objectContaining({ |
|
81
|
id: region.id, |
|
82
|
country_code: "us", |
|
83
|
}) |
|
84
|
) |
|
85
|
}) |
|
86
|
|
|
87
|
it("should create two tax regions with the same country code but different province", async () => { |
|
88
|
const regionOne = await service.createTaxRegions({ |
|
89
|
country_code: "US", |
|
90
|
province_code: "CA", |
|
91
|
default_tax_rate: { |
|
92
|
name: "Test Rate", |
|
93
|
rate: 0.2, |
|
94
|
code: "TEST", |
|
95
|
}, |
|
96
|
}) |
|
97
|
|
|
98
|
const regionTwo = await service.createTaxRegions({ |
|
99
|
country_code: "US", |
|
100
|
province_code: "NY", |
|
101
|
default_tax_rate: { |
|
102
|
name: "Test Rate", |
|
103
|
rate: 0.2, |
|
104
|
code: "TEST", |
|
105
|
}, |
|
106
|
}) |
|
107
|
|
|
108
|
expect(regionOne).toEqual( |
|
109
|
expect.objectContaining({ |
|
110
|
id: regionOne.id, |
|
111
|
country_code: "us", |
|
112
|
province_code: "ca", |
|
113
|
}) |
|
114
|
) |
|
115
|
|
|
116
|
expect(regionTwo).toEqual( |
|
117
|
expect.objectContaining({ |
|
118
|
id: regionTwo.id, |
|
119
|
country_code: "us", |
|
120
|
province_code: "ny", |
|
121
|
}) |
|
122
|
) |
|
123
|
}) |
|
124
|
|
|
125
|
it("should create two tax regions in a child-parent-like relationship", async () => { |
|
126
|
const regionOne = await service.createTaxRegions({ |
|
127
|
country_code: "US", |
|
128
|
default_tax_rate: { |
|
129
|
name: "Test Rate", |
|
130
|
rate: 0.2, |
|
131
|
code: "TEST", |
|
132
|
}, |
|
133
|
}) |
|
134
|
|
|
135
|
const regionTwo = await service.createTaxRegions({ |
|
136
|
country_code: "US", |
|
137
|
parent_id: regionOne.id, |
|
138
|
province_code: "NY", |
|
139
|
default_tax_rate: { |
|
140
|
name: "Test Rate", |
|
141
|
rate: 0.2, |
|
142
|
code: "TEST", |
|
143
|
}, |
|
144
|
}) |
|
145
|
|
|
146
|
expect(regionOne).toEqual( |
|
147
|
expect.objectContaining({ |
|
148
|
id: regionOne.id, |
|
149
|
country_code: "us", |
|
150
|
province_code: null, |
|
151
|
}) |
|
152
|
) |
|
153
|
|
|
154
|
expect(regionTwo).toEqual( |
|
155
|
expect.objectContaining({ |
|
156
|
id: regionTwo.id, |
|
157
|
country_code: "us", |
|
158
|
province_code: "ny", |
|
159
|
}) |
|
160
|
) |
|
161
|
}) |
|
162
|
|
|
163
|
it("should create three tax regions in a child-parent-like relationship", async () => { |
|
164
|
const regionOne = await service.createTaxRegions({ |
|
165
|
country_code: "US", |
|
166
|
default_tax_rate: { |
|
167
|
name: "Test Rate", |
|
168
|
rate: 0.2, |
|
169
|
code: "TEST", |
|
170
|
}, |
|
171
|
}) |
|
172
|
|
|
173
|
const regionTwo = await service.createTaxRegions({ |
|
174
|
country_code: "US", |
|
175
|
parent_id: regionOne.id, |
|
176
|
province_code: "NY", |
|
177
|
default_tax_rate: { |
|
178
|
name: "Test Rate", |
|
179
|
rate: 0.2, |
|
180
|
code: "TEST", |
|
181
|
}, |
|
182
|
}) |
|
183
|
|
|
184
|
const regionThree = await service.createTaxRegions({ |
|
185
|
country_code: "US", |
|
186
|
parent_id: regionOne.id, |
|
187
|
province_code: "NE", |
|
188
|
default_tax_rate: { |
|
189
|
name: "Test Rate", |
|
190
|
rate: 0.2, |
|
191
|
code: "TEST", |
|
192
|
}, |
|
193
|
}) |
|
194
|
|
|
195
|
expect(regionOne).toEqual( |
|
196
|
expect.objectContaining({ |
|
197
|
id: regionOne.id, |
|
198
|
country_code: "us", |
|
199
|
province_code: null, |
|
200
|
}) |
|
201
|
) |
|
202
|
|
|
203
|
expect(regionTwo).toEqual( |
|
204
|
expect.objectContaining({ |
|
205
|
id: regionTwo.id, |
|
206
|
country_code: "us", |
|
207
|
province_code: "ny", |
|
208
|
}) |
|
209
|
) |
|
210
|
|
|
211
|
expect(regionThree).toEqual( |
|
212
|
expect.objectContaining({ |
|
213
|
id: regionThree.id, |
|
214
|
country_code: "us", |
|
215
|
province_code: "ne", |
|
216
|
}) |
|
217
|
) |
|
218
|
}) |
|
219
|
|
|
220
|
it("should throw when creating a tax region with a country code of an existing region", async () => { |
|
221
|
await service.createTaxRegions({ |
|
222
|
country_code: "US", |
|
223
|
default_tax_rate: { |
|
224
|
name: "Test Rate", |
|
225
|
rate: 0.2, |
|
226
|
code: "TEST", |
|
227
|
}, |
|
228
|
}) |
|
229
|
|
|
230
|
const error = await service |
|
231
|
.createTaxRegions({ |
|
232
|
country_code: "US", |
|
233
|
default_tax_rate: { |
|
234
|
name: "Test Rate", |
|
235
|
rate: 0.2, |
|
236
|
code: "TEST", |
|
237
|
}, |
|
238
|
}) |
|
239
|
.catch((e) => e) |
|
240
|
|
|
241
|
expect(error.message).toEqual( |
|
242
|
"Tax region with country_code: us, already exists." |
|
243
|
) |
|
244
|
}) |
|
245
|
|
|
246
|
it("should throw when creating a tax region with a country code and province code of an existing region", async () => { |
|
247
|
await service.createTaxRegions({ |
|
248
|
country_code: "US", |
|
249
|
province_code: "CA", |
|
250
|
default_tax_rate: { |
|
251
|
name: "Test Rate", |
|
252
|
rate: 0.2, |
|
253
|
code: "TEST", |
|
254
|
}, |
|
255
|
}) |
|
256
|
|
|
257
|
const error = await service |
|
258
|
.createTaxRegions({ |
|
259
|
country_code: "US", |
|
260
|
province_code: "CA", |
|
261
|
default_tax_rate: { |
|
262
|
name: "Test Rate", |
|
263
|
rate: 0.2, |
|
264
|
code: "TEST", |
|
265
|
}, |
|
266
|
}) |
|
267
|
.catch((e) => e) |
|
268
|
|
|
269
|
expect(error.message).toEqual( |
|
270
|
"Tax region with country_code: us, province_code: ca, already exists." |
|
271
|
) |
|
272
|
}) |
|
273
|
|
|
274
|
it("should create tax rates and update them", async () => { |
|
275
|
const region = await service.createTaxRegions({ |
|
276
|
country_code: "US", |
|
277
|
default_tax_rate: { |
|
278
|
name: "Test Rate", |
|
279
|
rate: 0.2, |
|
280
|
code: "TEST-CODE", |
|
281
|
}, |
|
282
|
}) |
|
283
|
|
|
284
|
const rate = await service.createTaxRates({ |
|
285
|
tax_region_id: region.id, |
|
286
|
name: "Shipping Rate", |
|
287
|
code: "test", |
|
288
|
rate: 8.23, |
|
289
|
}) |
|
290
|
|
|
291
|
const updatedRate = await service.updateTaxRates(rate.id, { |
|
292
|
name: "Updated Rate", |
|
293
|
code: "TEST", |
|
294
|
rate: 8.25, |
|
295
|
}) |
|
296
|
|
|
297
|
expect(updatedRate).toEqual( |
|
298
|
expect.objectContaining({ |
|
299
|
tax_region_id: region.id, |
|
300
|
rate: 8.25, |
|
301
|
name: "Updated Rate", |
|
302
|
code: "TEST", |
|
303
|
is_default: false, |
|
304
|
}) |
|
305
|
) |
|
306
|
|
|
307
|
const updatedDefaultRate = await service.updateTaxRates( |
|
308
|
{ tax_region_id: region.id, is_default: true }, |
|
309
|
{ rate: 2 } |
|
310
|
) |
|
311
|
|
|
312
|
expect(updatedDefaultRate).toEqual([ |
|
313
|
expect.objectContaining({ |
|
314
|
tax_region_id: region.id, |
|
315
|
rate: 2, |
|
316
|
name: "Test Rate", |
|
317
|
code: "TEST-CODE", |
|
318
|
is_default: true, |
|
319
|
}), |
|
320
|
]) |
|
321
|
|
|
322
|
const rates = await service.listTaxRates() |
|
323
|
expect(rates).toEqual( |
|
324
|
expect.arrayContaining([ |
|
325
|
expect.objectContaining({ |
|
326
|
tax_region_id: region.id, |
|
327
|
rate: 2, |
|
328
|
name: "Test Rate", |
|
329
|
code: "TEST-CODE", |
|
330
|
is_default: true, |
|
331
|
}), |
|
332
|
expect.objectContaining({ |
|
333
|
tax_region_id: region.id, |
|
334
|
rate: 8.25, |
|
335
|
name: "Updated Rate", |
|
336
|
code: "TEST", |
|
337
|
is_default: false, |
|
338
|
}), |
|
339
|
]) |
|
340
|
) |
|
341
|
}) |
|
342
|
|
|
343
|
it("should update tax rates with rules", async () => { |
|
344
|
const region = await service.createTaxRegions({ |
|
345
|
country_code: "US", |
|
346
|
default_tax_rate: { |
|
347
|
name: "Test Rate", |
|
348
|
rate: 0.2, |
|
349
|
code: "TEST-CODE", |
|
350
|
}, |
|
351
|
}) |
|
352
|
|
|
353
|
const rate = await service.createTaxRates({ |
|
354
|
tax_region_id: region.id, |
|
355
|
name: "Shipping Rate", |
|
356
|
code: "test", |
|
357
|
rate: 8.23, |
|
358
|
}) |
|
359
|
|
|
360
|
await service.updateTaxRates(rate.id, { |
|
361
|
name: "Updated Rate", |
|
362
|
code: "TEST", |
|
363
|
rate: 8.25, |
|
364
|
rules: [ |
|
365
|
{ reference: "product", reference_id: "product_id_1" }, |
|
366
|
{ reference: "product_type", reference_id: "product_type_id" }, |
|
367
|
], |
|
368
|
}) |
|
369
|
|
|
370
|
const rules = await service.listTaxRateRules({ tax_rate_id: rate.id }) |
|
371
|
|
|
372
|
expect(rules).toEqual( |
|
373
|
expect.arrayContaining([ |
|
374
|
expect.objectContaining({ |
|
375
|
reference: "product", |
|
376
|
reference_id: "product_id_1", |
|
377
|
}), |
|
378
|
expect.objectContaining({ |
|
379
|
reference: "product_type", |
|
380
|
reference_id: "product_type_id", |
|
381
|
}), |
|
382
|
]) |
|
383
|
) |
|
384
|
|
|
385
|
await service.updateTaxRates(rate.id, { |
|
386
|
rules: [ |
|
387
|
{ reference: "product", reference_id: "product_id_1" }, |
|
388
|
{ reference: "product", reference_id: "product_id_2" }, |
|
389
|
{ reference: "product_type", reference_id: "product_type_id_2" }, |
|
390
|
{ reference: "product_type", reference_id: "product_type_id_3" }, |
|
391
|
], |
|
392
|
}) |
|
393
|
|
|
394
|
const rulesWithDeletes = await service.listTaxRateRules( |
|
395
|
{ tax_rate_id: rate.id }, |
|
396
|
{ withDeleted: true } |
|
397
|
) |
|
398
|
|
|
399
|
expect(rulesWithDeletes).toEqual( |
|
400
|
expect.arrayContaining([ |
|
401
|
expect.objectContaining({ |
|
402
|
reference: "product", |
|
403
|
reference_id: "product_id_2", |
|
404
|
}), |
|
405
|
expect.objectContaining({ |
|
406
|
reference: "product_type", |
|
407
|
reference_id: "product_type_id_2", |
|
408
|
}), |
|
409
|
expect.objectContaining({ |
|
410
|
reference: "product_type", |
|
411
|
reference_id: "product_type_id_3", |
|
412
|
}), |
|
413
|
expect.objectContaining({ |
|
414
|
reference: "product", |
|
415
|
reference_id: "product_id_1", |
|
416
|
deleted_at: expect.any(Date), |
|
417
|
}), |
|
418
|
expect.objectContaining({ |
|
419
|
reference: "product_type", |
|
420
|
reference_id: "product_type_id", |
|
421
|
deleted_at: expect.any(Date), |
|
422
|
}), |
|
423
|
]) |
|
424
|
) |
|
425
|
}) |
|
426
|
|
|
427
|
it("should create a tax region", async () => { |
|
428
|
const region = await service.createTaxRegions({ |
|
429
|
country_code: "US", |
|
430
|
default_tax_rate: { |
|
431
|
code: "TEST", |
|
432
|
name: "Test Rate", |
|
433
|
rate: 0.2, |
|
434
|
}, |
|
435
|
}) |
|
436
|
|
|
437
|
const [provinceRegion] = await service.createTaxRegions([ |
|
438
|
{ |
|
439
|
country_code: "US", |
|
440
|
province_code: "CA", |
|
441
|
parent_id: region.id, |
|
442
|
default_tax_rate: { |
|
443
|
code: "TEST", |
|
444
|
name: "CA Rate", |
|
445
|
rate: 8.25, |
|
446
|
}, |
|
447
|
}, |
|
448
|
]) |
|
449
|
|
|
450
|
const listedRegions = await service.listTaxRegions() |
|
451
|
expect(listedRegions).toEqual( |
|
452
|
expect.arrayContaining([ |
|
453
|
expect.objectContaining({ |
|
454
|
id: region.id, |
|
455
|
country_code: "us", |
|
456
|
province_code: null, |
|
457
|
parent_id: null, |
|
458
|
}), |
|
459
|
expect.objectContaining({ |
|
460
|
id: provinceRegion.id, |
|
461
|
country_code: "us", |
|
462
|
province_code: "ca", |
|
463
|
parent_id: region.id, |
|
464
|
}), |
|
465
|
]) |
|
466
|
) |
|
467
|
|
|
468
|
const rates = await service.listTaxRates() |
|
469
|
expect(rates).toEqual( |
|
470
|
expect.arrayContaining([ |
|
471
|
expect.objectContaining({ |
|
472
|
tax_region_id: region.id, |
|
473
|
rate: 0.2, |
|
474
|
name: "Test Rate", |
|
475
|
is_default: true, |
|
476
|
}), |
|
477
|
expect.objectContaining({ |
|
478
|
tax_region_id: provinceRegion.id, |
|
479
|
rate: 8.25, |
|
480
|
name: "CA Rate", |
|
481
|
is_default: true, |
|
482
|
}), |
|
483
|
]) |
|
484
|
) |
|
485
|
}) |
|
486
|
|
|
487
|
it("should create a tax rate rule", async () => { |
|
488
|
const [region] = await service.createTaxRegions([ |
|
489
|
{ |
|
490
|
country_code: "US", |
|
491
|
default_tax_rate: { |
|
492
|
code: "TEST", |
|
493
|
name: "Test Rate", |
|
494
|
rate: 0.2, |
|
495
|
}, |
|
496
|
}, |
|
497
|
]) |
|
498
|
|
|
499
|
const rate = await service.createTaxRates({ |
|
500
|
tax_region_id: region.id, |
|
501
|
name: "Shipping Rate", |
|
502
|
rate: 8.23, |
|
503
|
code: "TEST-CODE", |
|
504
|
}) |
|
505
|
|
|
506
|
await service.createTaxRateRules([ |
|
507
|
{ |
|
508
|
tax_rate_id: rate.id, |
|
509
|
reference: "product", |
|
510
|
reference_id: "prod_1234", |
|
511
|
}, |
|
512
|
]) |
|
513
|
|
|
514
|
const listedRules = await service.listTaxRateRules( |
|
515
|
{}, |
|
516
|
{ |
|
517
|
relations: ["tax_rate"], |
|
518
|
} |
|
519
|
) |
|
520
|
expect(listedRules).toEqual( |
|
521
|
expect.arrayContaining([ |
|
522
|
expect.objectContaining({ |
|
523
|
reference: "product", |
|
524
|
reference_id: "prod_1234", |
|
525
|
tax_rate: expect.objectContaining({ |
|
526
|
tax_region_id: region.id, |
|
527
|
name: "Shipping Rate", |
|
528
|
rate: 8.23, |
|
529
|
}), |
|
530
|
}), |
|
531
|
]) |
|
532
|
) |
|
533
|
}) |
|
534
|
|
|
535
|
it("applies specific product rules at the province level", async () => { |
|
536
|
await setupTaxStructure(service) |
|
537
|
const item = { |
|
538
|
id: "item_test", |
|
539
|
product_id: "product_id_1", // Matching the specific product rate for CA province |
|
540
|
quantity: 1, |
|
541
|
} |
|
542
|
const calculationContext = { |
|
543
|
address: { |
|
544
|
country_code: "US", |
|
545
|
province_code: "CA", |
|
546
|
}, |
|
547
|
} |
|
548
|
|
|
549
|
const taxLines = await service.getTaxLines([item], calculationContext) |
|
550
|
|
|
551
|
expect(taxLines).toEqual([ |
|
552
|
expect.objectContaining({ |
|
553
|
rate_id: expect.any(String), |
|
554
|
rate: 3, // Expecting the reduced rate for specific products in CA |
|
555
|
code: "CAREDUCE_PROD", |
|
556
|
name: "CA Reduced Rate for Products", |
|
557
|
}), |
|
558
|
]) |
|
559
|
}) |
|
560
|
|
|
561
|
it("applies specific product type rules at the province level", async () => { |
|
562
|
await setupTaxStructure(service) |
|
563
|
const item = { |
|
564
|
id: "item_test", |
|
565
|
product_id: "product_id_unknown", // This product does not have a specific rule |
|
566
|
product_type_id: "product_type_id_1", // Matching the specific product type rate for CA province |
|
567
|
quantity: 1, |
|
568
|
} |
|
569
|
const calculationContext = { |
|
570
|
address: { |
|
571
|
country_code: "US", |
|
572
|
province_code: "CA", |
|
573
|
}, |
|
574
|
} |
|
575
|
|
|
576
|
const taxLines = await service.getTaxLines([item], calculationContext) |
|
577
|
|
|
578
|
expect(taxLines).toEqual([ |
|
579
|
expect.objectContaining({ |
|
580
|
rate_id: expect.any(String), |
|
581
|
rate: 1, // Expecting the reduced rate for specific product types in CA |
|
582
|
code: "CAREDUCE_TYPE", |
|
583
|
name: "CA Reduced Rate for Product Type", |
|
584
|
}), |
|
585
|
]) |
|
586
|
}) |
|
587
|
|
|
588
|
it("applies specific product type rules at the province level", async () => { |
|
589
|
await setupTaxStructure(service) |
|
590
|
const item = { |
|
591
|
id: "item_test", |
|
592
|
product_id: "product_id_unknown", // This product does not have a specific rule |
|
593
|
product_type_id: "product_type_id_1", // Matching the specific product type rate for CA province |
|
594
|
quantity: 1, |
|
595
|
} |
|
596
|
const calculationContext = { |
|
597
|
address: { |
|
598
|
country_code: "US", |
|
599
|
province_code: "CA", |
|
600
|
}, |
|
601
|
} |
|
602
|
|
|
603
|
const taxLines = await service.getTaxLines([item], calculationContext) |
|
604
|
|
|
605
|
expect(taxLines).toEqual([ |
|
606
|
expect.objectContaining({ |
|
607
|
rate_id: expect.any(String), |
|
608
|
rate: 1, // Expecting the reduced rate for specific product types in CA |
|
609
|
code: "CAREDUCE_TYPE", |
|
610
|
name: "CA Reduced Rate for Product Type", |
|
611
|
}), |
|
612
|
]) |
|
613
|
}) |
|
614
|
|
|
615
|
it("applies default province rules when no specific product or product type rule matches", async () => { |
|
616
|
await setupTaxStructure(service) |
|
617
|
const item = { |
|
618
|
id: "item_test", |
|
619
|
product_id: "product_id_unknown", |
|
620
|
quantity: 1, |
|
621
|
} |
|
622
|
const calculationContext = { |
|
623
|
address: { |
|
624
|
country_code: "US", |
|
625
|
province_code: "NY", // Testing with NY to apply the default provincial rate |
|
626
|
}, |
|
627
|
} |
|
628
|
|
|
629
|
const taxLines = await service.getTaxLines([item], calculationContext) |
|
630
|
|
|
631
|
expect(taxLines).toEqual([ |
|
632
|
expect.objectContaining({ |
|
633
|
rate_id: expect.any(String), |
|
634
|
rate: 6, // Expecting the default rate for NY province |
|
635
|
code: "NYDEFAULT", |
|
636
|
name: "NY Default Rate", |
|
637
|
}), |
|
638
|
]) |
|
639
|
}) |
|
640
|
|
|
641
|
it("applies specific product rules at the country level when no province rate applies", async () => { |
|
642
|
await setupTaxStructure(service) |
|
643
|
const item = { |
|
644
|
id: "item_test", |
|
645
|
product_id: "product_id_4", // Assuming this ID now has a specific rule at the country level for Canada |
|
646
|
quantity: 1, |
|
647
|
} |
|
648
|
const calculationContext = { |
|
649
|
address: { |
|
650
|
country_code: "CA", |
|
651
|
province_code: "ON", // This province does not have a specific rule |
|
652
|
}, |
|
653
|
} |
|
654
|
|
|
655
|
const taxLines = await service.getTaxLines([item], calculationContext) |
|
656
|
|
|
657
|
expect(taxLines).toEqual([ |
|
658
|
expect.objectContaining({ |
|
659
|
rate_id: expect.any(String), |
|
660
|
rate: 3, // Expecting the reduced rate for specific products in Canada |
|
661
|
code: "CAREDUCE_PROD_CA", |
|
662
|
name: "Canada Reduced Rate for Product", |
|
663
|
}), |
|
664
|
]) |
|
665
|
}) |
|
666
|
|
|
667
|
it("applies default country rules when no specific product or product type rule matches", async () => { |
|
668
|
await setupTaxStructure(service) |
|
669
|
const item = { |
|
670
|
id: "item_test", |
|
671
|
product_id: "product_id_unknown", |
|
672
|
quantity: 1, |
|
673
|
} |
|
674
|
const calculationContext = { |
|
675
|
address: { |
|
676
|
country_code: "DE", // Testing with Germany to apply the default country rate |
|
677
|
}, |
|
678
|
} |
|
679
|
|
|
680
|
const taxLines = await service.getTaxLines([item], calculationContext) |
|
681
|
|
|
682
|
expect(taxLines).toEqual([ |
|
683
|
expect.objectContaining({ |
|
684
|
rate_id: expect.any(String), |
|
685
|
rate: 19, |
|
686
|
code: "DE19", |
|
687
|
name: "Germany Default Rate", |
|
688
|
}), |
|
689
|
]) |
|
690
|
}) |
|
691
|
|
|
692
|
it("prioritizes specific product rules over product type rules", async () => { |
|
693
|
await setupTaxStructure(service) |
|
694
|
|
|
695
|
const item = { |
|
696
|
id: "item_test", |
|
697
|
product_id: "product_id_1", // This product has a specific rule for product type and product |
|
698
|
product_type_id: "product_type_id_1", // This product type has a specific rule for product type |
|
699
|
quantity: 1, |
|
700
|
} |
|
701
|
const calculationContext = { |
|
702
|
address: { |
|
703
|
country_code: "US", |
|
704
|
province_code: "CA", |
|
705
|
}, |
|
706
|
} |
|
707
|
|
|
708
|
const taxLines = await service.getTaxLines([item], calculationContext) |
|
709
|
|
|
710
|
expect(taxLines).toEqual([ |
|
711
|
expect.objectContaining({ |
|
712
|
rate_id: expect.any(String), |
|
713
|
rate: 3, // Expecting the reduced rate for specific products in CA |
|
714
|
code: "CAREDUCE_PROD", |
|
715
|
name: "CA Reduced Rate for Products", |
|
716
|
}), |
|
717
|
]) |
|
718
|
}) |
|
719
|
|
|
720
|
it("prioritizes specific shipping rate over default rate", async () => { |
|
721
|
const region = await service.createTaxRegions({ |
|
722
|
country_code: "US", |
|
723
|
provider_id: "tp_system", |
|
724
|
default_tax_rate: { |
|
725
|
code: "TEST", |
|
726
|
name: "Test Rate", |
|
727
|
rate: 5, |
|
728
|
}, |
|
729
|
}) |
|
730
|
|
|
731
|
const rate = await service.createTaxRates({ |
|
732
|
tax_region_id: region.id, |
|
733
|
name: "Shipping Rate", |
|
734
|
code: "SHIPPING_TEST", |
|
735
|
rate: 2, |
|
736
|
}) |
|
737
|
|
|
738
|
const item = { |
|
739
|
id: "shipping_test", |
|
740
|
shipping_option_id: "so_1234", |
|
741
|
quantity: 1, |
|
742
|
} |
|
743
|
|
|
744
|
const calculationContext = { |
|
745
|
address: { |
|
746
|
country_code: "US", |
|
747
|
}, |
|
748
|
} |
|
749
|
|
|
750
|
let taxLines = await service.getTaxLines([item], calculationContext) |
|
751
|
|
|
752
|
expect(taxLines).toEqual([ |
|
753
|
expect.objectContaining({ |
|
754
|
rate_id: expect.any(String), |
|
755
|
rate: 5, |
|
756
|
code: "TEST", |
|
757
|
name: "Test Rate", |
|
758
|
}), |
|
759
|
]) |
|
760
|
|
|
761
|
await service.updateTaxRates(rate.id, { |
|
762
|
rules: [ |
|
763
|
{ |
|
764
|
reference: "shipping_option", |
|
765
|
reference_id: "so_1234", |
|
766
|
}, |
|
767
|
], |
|
768
|
}) |
|
769
|
|
|
770
|
taxLines = await service.getTaxLines([item], calculationContext) |
|
771
|
|
|
772
|
expect(taxLines).toEqual([ |
|
773
|
expect.objectContaining({ |
|
774
|
rate_id: expect.any(String), |
|
775
|
rate: 2, |
|
776
|
code: "SHIPPING_TEST", |
|
777
|
name: "Shipping Rate", |
|
778
|
}), |
|
779
|
]) |
|
780
|
}) |
|
781
|
|
|
782
|
it("should delete tax rate", async () => { |
|
783
|
const region = await service.createTaxRegions({ |
|
784
|
country_code: "US", |
|
785
|
}) |
|
786
|
|
|
787
|
const taxRate = await service.createTaxRates({ |
|
788
|
tax_region_id: region.id, |
|
789
|
value: 10, |
|
790
|
code: "test", |
|
791
|
name: "test", |
|
792
|
}) |
|
793
|
|
|
794
|
await service.deleteTaxRates(taxRate.id) |
|
795
|
|
|
796
|
const rates = await service.listTaxRates({ tax_region_id: region.id }) |
|
797
|
|
|
798
|
expect(rates).toEqual([]) |
|
799
|
}) |
|
800
|
|
|
801
|
it("should soft delete tax rate", async () => { |
|
802
|
const region = await service.createTaxRegions({ |
|
803
|
country_code: "US", |
|
804
|
}) |
|
805
|
|
|
806
|
const taxRate = await service.createTaxRates({ |
|
807
|
tax_region_id: region.id, |
|
808
|
value: 10, |
|
809
|
code: "test", |
|
810
|
name: "test", |
|
811
|
}) |
|
812
|
|
|
813
|
await service.softDeleteTaxRates([taxRate.id]) |
|
814
|
|
|
815
|
const rates = await service.listTaxRates( |
|
816
|
{ tax_region_id: region.id }, |
|
817
|
{ withDeleted: true } |
|
818
|
) |
|
819
|
|
|
820
|
expect(rates).toEqual([ |
|
821
|
expect.objectContaining({ |
|
822
|
id: taxRate.id, |
|
823
|
deleted_at: expect.any(Date), |
|
824
|
}), |
|
825
|
]) |
|
826
|
}) |
|
827
|
|
|
828
|
it("should delete a tax region and its rates", async () => { |
|
829
|
const region = await service.createTaxRegions({ |
|
830
|
country_code: "US", |
|
831
|
default_tax_rate: { |
|
832
|
value: 2, |
|
833
|
code: "test", |
|
834
|
name: "default test", |
|
835
|
}, |
|
836
|
}) |
|
837
|
|
|
838
|
await service.createTaxRates({ |
|
839
|
tax_region_id: region.id, |
|
840
|
value: 10, |
|
841
|
code: "test", |
|
842
|
name: "test", |
|
843
|
}) |
|
844
|
|
|
845
|
await service.deleteTaxRegions(region.id) |
|
846
|
|
|
847
|
const taxRegions = await service.listTaxRegions() |
|
848
|
const rates = await service.listTaxRates() |
|
849
|
|
|
850
|
expect(taxRegions).toEqual([]) |
|
851
|
expect(rates).toEqual([]) |
|
852
|
}) |
|
853
|
|
|
854
|
it("should soft delete a tax region and its rates", async () => { |
|
855
|
const region = await service.createTaxRegions({ |
|
856
|
country_code: "US", |
|
857
|
default_tax_rate: { |
|
858
|
value: 2, |
|
859
|
code: "test", |
|
860
|
name: "default test", |
|
861
|
}, |
|
862
|
}) |
|
863
|
|
|
864
|
await service.createTaxRates({ |
|
865
|
tax_region_id: region.id, |
|
866
|
value: 10, |
|
867
|
code: "test", |
|
868
|
name: "test", |
|
869
|
}) |
|
870
|
|
|
871
|
await service.softDeleteTaxRegions([region.id]) |
|
872
|
|
|
873
|
const taxRegions = await service.listTaxRegions( |
|
874
|
{}, |
|
875
|
{ withDeleted: true } |
|
876
|
) |
|
877
|
const rates = await service.listTaxRates({}, { withDeleted: true }) |
|
878
|
|
|
879
|
expect(taxRegions).toEqual([ |
|
880
|
expect.objectContaining({ |
|
881
|
id: region.id, |
|
882
|
country_code: "us", |
|
883
|
deleted_at: expect.any(Date), |
|
884
|
}), |
|
885
|
]) |
|
886
|
expect(rates).toEqual([ |
|
887
|
expect.objectContaining({ |
|
888
|
deleted_at: expect.any(Date), |
|
889
|
}), |
|
890
|
expect.objectContaining({ |
|
891
|
deleted_at: expect.any(Date), |
|
892
|
}), |
|
893
|
]) |
|
894
|
}) |
|
895
|
|
|
896
|
it("should delete a tax rate and its rules", async () => { |
|
897
|
const region = await service.createTaxRegions({ |
|
898
|
country_code: "US", |
|
899
|
}) |
|
900
|
|
|
901
|
const rate = await service.createTaxRates({ |
|
902
|
tax_region_id: region.id, |
|
903
|
value: 10, |
|
904
|
code: "test", |
|
905
|
name: "test", |
|
906
|
rules: [ |
|
907
|
{ reference: "product", reference_id: "product_id_1" }, |
|
908
|
{ reference: "product_type", reference_id: "product_type_id" }, |
|
909
|
], |
|
910
|
}) |
|
911
|
|
|
912
|
await service.deleteTaxRates(rate.id) |
|
913
|
|
|
914
|
const taxRegions = await service.listTaxRegions() |
|
915
|
const rates = await service.listTaxRates() |
|
916
|
const rules = await service.listTaxRateRules() |
|
917
|
|
|
918
|
expect(taxRegions).toEqual([expect.objectContaining({ id: region.id })]) |
|
919
|
expect(rates).toEqual([]) |
|
920
|
expect(rules).toEqual([]) |
|
921
|
}) |
|
922
|
|
|
923
|
it("should soft delete a tax rate and its rules", async () => { |
|
924
|
const region = await service.createTaxRegions({ |
|
925
|
country_code: "US", |
|
926
|
}) |
|
927
|
|
|
928
|
const rate = await service.createTaxRates({ |
|
929
|
tax_region_id: region.id, |
|
930
|
value: 10, |
|
931
|
code: "test", |
|
932
|
name: "test", |
|
933
|
rules: [ |
|
934
|
{ reference: "product", reference_id: "product_id_1" }, |
|
935
|
{ reference: "product_type", reference_id: "product_type_id" }, |
|
936
|
], |
|
937
|
}) |
|
938
|
|
|
939
|
await service.softDeleteTaxRates(rate.id) |
|
940
|
|
|
941
|
const taxRegions = await service.listTaxRegions( |
|
942
|
{}, |
|
943
|
{ withDeleted: true } |
|
944
|
) |
|
945
|
const rates = await service.listTaxRates({}, { withDeleted: true }) |
|
946
|
const rules = await service.listTaxRateRules({}, { withDeleted: true }) |
|
947
|
|
|
948
|
expect(taxRegions).toEqual([ |
|
949
|
expect.objectContaining({ id: region.id, deleted_at: null }), |
|
950
|
]) |
|
951
|
expect(rates).toEqual([ |
|
952
|
expect.objectContaining({ |
|
953
|
id: rate.id, |
|
954
|
deleted_at: expect.any(Date), |
|
955
|
}), |
|
956
|
]) |
|
957
|
expect(rules).toEqual([ |
|
958
|
expect.objectContaining({ |
|
959
|
tax_rate_id: rate.id, |
|
960
|
deleted_at: expect.any(Date), |
|
961
|
}), |
|
962
|
expect.objectContaining({ |
|
963
|
tax_rate_id: rate.id, |
|
964
|
deleted_at: expect.any(Date), |
|
965
|
}), |
|
966
|
]) |
|
967
|
}) |
|
968
|
|
|
969
|
it("should soft delete a tax rule", async () => { |
|
970
|
const region = await service.createTaxRegions({ |
|
971
|
country_code: "US", |
|
972
|
}) |
|
973
|
|
|
974
|
const rate = await service.createTaxRates({ |
|
975
|
tax_region_id: region.id, |
|
976
|
value: 10, |
|
977
|
code: "test", |
|
978
|
name: "test", |
|
979
|
}) |
|
980
|
|
|
981
|
const [ruleOne, ruleTwo] = await service.createTaxRateRules([ |
|
982
|
{ |
|
983
|
tax_rate_id: rate.id, |
|
984
|
reference: "product", |
|
985
|
reference_id: "product_id_1", |
|
986
|
}, |
|
987
|
{ |
|
988
|
tax_rate_id: rate.id, |
|
989
|
reference: "product_type", |
|
990
|
reference_id: "product_type_id", |
|
991
|
}, |
|
992
|
]) |
|
993
|
|
|
994
|
await service.softDeleteTaxRateRules([ruleOne.id]) |
|
995
|
|
|
996
|
const rules = await service.listTaxRateRules({}, { withDeleted: true }) |
|
997
|
expect(rules).toEqual( |
|
998
|
expect.arrayContaining([ |
|
999
|
expect.objectContaining({ |
|
1000
|
id: ruleOne.id, |
|
1001
|
deleted_at: expect.any(Date), |
|
1002
|
}), |
|
1003
|
expect.objectContaining({ |
|
1004
|
id: ruleTwo.id, |
|
1005
|
deleted_at: null, |
|
1006
|
}), |
|
1007
|
]) |
|
1008
|
) |
|
1009
|
|
|
1010
|
const rateWithRules = await service.retrieveTaxRate(rate.id, { |
|
1011
|
relations: ["rules"], |
|
1012
|
}) |
|
1013
|
expect(rateWithRules.rules.length).toBe(1) |
|
1014
|
|
|
1015
|
// should be possible to add the rule back again |
|
1016
|
await service.createTaxRateRules({ |
|
1017
|
tax_rate_id: rate.id, |
|
1018
|
reference: ruleOne.reference, |
|
1019
|
reference_id: ruleOne.reference_id, |
|
1020
|
}) |
|
1021
|
|
|
1022
|
const rateWithRulesAfterReAdd = await service.retrieveTaxRate(rate.id, { |
|
1023
|
relations: ["rules"], |
|
1024
|
}) |
|
1025
|
expect(rateWithRulesAfterReAdd.rules.length).toBe(2) |
|
1026
|
}) |
|
1027
|
|
|
1028
|
it("should fail on duplicate rules", async () => { |
|
1029
|
const region = await service.createTaxRegions({ |
|
1030
|
country_code: "US", |
|
1031
|
}) |
|
1032
|
|
|
1033
|
await expect( |
|
1034
|
service.createTaxRates({ |
|
1035
|
tax_region_id: region.id, |
|
1036
|
value: 10, |
|
1037
|
code: "test", |
|
1038
|
name: "test", |
|
1039
|
rules: [ |
|
1040
|
{ reference: "product", reference_id: "product_id_1" }, |
|
1041
|
{ reference: "product", reference_id: "product_id_1" }, |
|
1042
|
], |
|
1043
|
}) |
|
1044
|
).rejects.toThrowError( |
|
1045
|
/Tax rate rule with tax_rate_id: .*?, reference_id: product_id_1, already exists./ |
|
1046
|
) |
|
1047
|
|
|
1048
|
const rate = await service.createTaxRates({ |
|
1049
|
tax_region_id: region.id, |
|
1050
|
value: 10, |
|
1051
|
code: "test", |
|
1052
|
name: "test", |
|
1053
|
rules: [{ reference: "product", reference_id: "product_id_1" }], |
|
1054
|
}) |
|
1055
|
|
|
1056
|
await expect( |
|
1057
|
service.createTaxRateRules({ |
|
1058
|
tax_rate_id: rate.id, |
|
1059
|
reference: "product", |
|
1060
|
reference_id: "product_id_1", |
|
1061
|
}) |
|
1062
|
).rejects.toThrowError( |
|
1063
|
/Tax rate rule with tax_rate_id: .*?, reference_id: product_id_1, already exists./ |
|
1064
|
) |
|
1065
|
}) |
|
1066
|
|
|
1067
|
it("should fail to create province region belonging to a parent with non-matching country", async () => { |
|
1068
|
const caRegion = await service.createTaxRegions({ |
|
1069
|
country_code: "CA", |
|
1070
|
}) |
|
1071
|
await expect( |
|
1072
|
service.createTaxRegions({ |
|
1073
|
country_code: "US", // This should be CA |
|
1074
|
parent_id: caRegion.id, |
|
1075
|
province_code: "QC", |
|
1076
|
}) |
|
1077
|
).rejects.toThrowError( |
|
1078
|
`Province region must belong to a parent region with the same country code. You are trying to create a province region with (country: us, province: qc) but parent expects (country: ca)` |
|
1079
|
) |
|
1080
|
}) |
|
1081
|
|
|
1082
|
it("should fail duplicate regions", async () => { |
|
1083
|
await service.createTaxRegions({ |
|
1084
|
country_code: "CA", |
|
1085
|
}) |
|
1086
|
|
|
1087
|
await service.createTaxRegions({ |
|
1088
|
country_code: "CA", |
|
1089
|
province_code: "QC", |
|
1090
|
}) |
|
1091
|
|
|
1092
|
await expect( |
|
1093
|
service.createTaxRegions({ |
|
1094
|
country_code: "CA", |
|
1095
|
province_code: "QC", |
|
1096
|
}) |
|
1097
|
).rejects.toThrowError( |
|
1098
|
"Tax region with country_code: ca, province_code: qc, already exists." |
|
1099
|
) |
|
1100
|
}) |
|
1101
|
|
|
1102
|
it("should fail to create region with non-existing parent", async () => { |
|
1103
|
await expect( |
|
1104
|
service.createTaxRegions({ |
|
1105
|
parent_id: "something random", |
|
1106
|
country_code: "CA", |
|
1107
|
province_code: "QC", |
|
1108
|
}) |
|
1109
|
).rejects.toThrowError( |
|
1110
|
`Province region must belong to a parent region. You are trying to create a province region with (country: ca, province: qc) but parent does not exist` |
|
1111
|
) |
|
1112
|
}) |
|
1113
|
|
|
1114
|
it("should fail to create two default tax rates", async () => { |
|
1115
|
const rate = await service.createTaxRegions({ |
|
1116
|
country_code: "CA", |
|
1117
|
default_tax_rate: { |
|
1118
|
name: "Test Rate", |
|
1119
|
rate: 0.2, |
|
1120
|
code: "TEST-CODE", |
|
1121
|
}, |
|
1122
|
}) |
|
1123
|
|
|
1124
|
await expect( |
|
1125
|
service.createTaxRates({ |
|
1126
|
tax_region_id: rate.id, |
|
1127
|
name: "Shipping Rate", |
|
1128
|
rate: 8.23, |
|
1129
|
is_default: true, |
|
1130
|
code: "TEST-CODE-2", |
|
1131
|
}) |
|
1132
|
).rejects.toThrowError( |
|
1133
|
/Tax rate with tax_region_id: .*?, already exists./ |
|
1134
|
) |
|
1135
|
}) |
|
1136
|
|
|
1137
|
it("should delete all child regions when parent region is deleted", async () => { |
|
1138
|
const region = await service.createTaxRegions({ |
|
1139
|
country_code: "CA", |
|
1140
|
}) |
|
1141
|
const provinceRegion = await service.createTaxRegions({ |
|
1142
|
parent_id: region.id, |
|
1143
|
country_code: "CA", |
|
1144
|
province_code: "QC", |
|
1145
|
}) |
|
1146
|
|
|
1147
|
await service.deleteTaxRegions(region.id) |
|
1148
|
|
|
1149
|
const taxRegions = await service.listTaxRegions({ |
|
1150
|
id: provinceRegion.id, |
|
1151
|
}) |
|
1152
|
|
|
1153
|
expect(taxRegions).toEqual([]) |
|
1154
|
}) |
|
1155
|
|
|
1156
|
it("should soft delete all child regions when parent region is deleted", async () => { |
|
1157
|
const region = await service.createTaxRegions({ |
|
1158
|
country_code: "CA", |
|
1159
|
}) |
|
1160
|
const provinceRegion = await service.createTaxRegions({ |
|
1161
|
parent_id: region.id, |
|
1162
|
country_code: "CA", |
|
1163
|
province_code: "QC", |
|
1164
|
}) |
|
1165
|
|
|
1166
|
await service.softDeleteTaxRegions([region.id]) |
|
1167
|
|
|
1168
|
const taxRegions = await service.listTaxRegions( |
|
1169
|
{ id: provinceRegion.id }, |
|
1170
|
{ withDeleted: true } |
|
1171
|
) |
|
1172
|
|
|
1173
|
expect(taxRegions).toEqual([ |
|
1174
|
expect.objectContaining({ |
|
1175
|
id: provinceRegion.id, |
|
1176
|
deleted_at: expect.any(Date), |
|
1177
|
}), |
|
1178
|
]) |
|
1179
|
}) |
|
1180
|
|
|
1181
|
it("should retrieve a tax module provider's service", async () => { |
|
1182
|
const providerService = service.getProvider("tp_system") |
|
1183
|
expect(providerService).toBeDefined() |
|
1184
|
expect(providerService.getIdentifier()).toEqual("system") |
|
1185
|
}) |
|
1186
|
}) |
|
1187
|
}, |
|
1188
|
}) |