1 import { ITaxModuleService } from "@medusajs/types"
2
3 import {
4 createTaxRateRulesStepId,
5 updateTaxRatesWorkflow,
6 } from "@medusajs/core-flows"
7 import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
8 import { Modules } from "@medusajs/utils"
9 import { createAdminUser } from "../../../../helpers/create-admin-user"
10
11 jest.setTimeout(50000)
12
13 const env = {}
14 const adminHeaders = {
15 headers: { "x-medusa-access-token": "test_token" },
16 }
17
18 medusaIntegrationTestRunner({
19 env,
20 testSuite: ({ dbConnection, getContainer, api, dbUtils }) => {
21 describe("Taxes - Workflow", () => {
22 let appContainer
23 let service: ITaxModuleService
24
25 beforeAll(async () => {
26 appContainer = getContainer()
27 service = appContainer.resolve(Modules.TAX)
28 })
29
30 beforeAll(async () => {
31 await createAdminUser(dbConnection, adminHeaders, appContainer)
32
33 await dbUtils.snapshot()
34 })
35
36 it("compensates rules correctly", async () => {
37 const taxRegion = await service.createTaxRegions({
38 country_code: "us",
39 })
40
41 const [rateOne, rateTwo] = await service.createTaxRates([
42 {
43 tax_region_id: taxRegion.id,
44 rate: 10,
45 code: "standard",
46 name: "Standard",
47 rules: [
48 { reference: "shipping", reference_id: "shipping_12354" },
49 { reference: "shipping", reference_id: "shipping_11111" },
50 { reference: "shipping", reference_id: "shipping_22222" },
51 ],
52 },
53 {
54 tax_region_id: taxRegion.id,
55 rate: 2,
56 code: "reduced",
57 name: "Reduced",
58 rules: [
59 { reference: "product", reference_id: "product_12354" },
60 { reference: "product", reference_id: "product_11111" },
61 { reference: "product", reference_id: "product_22222" },
62 ],
63 },
64 ])
65
66 const workflow = updateTaxRatesWorkflow(appContainer)
67
68 workflow.appendAction("throw", createTaxRateRulesStepId, {
69 invoke: async function failStep() {
70 throw new Error(`Failed to update`)
71 },
72 })
73
74 await workflow.run({
75 input: {
76 selector: { tax_region_id: taxRegion.id },
77 update: {
78 rate: 2,
79 rules: [
80 { reference: "product", reference_id: "product_12354" },
81 { reference: "shipping", reference_id: "shipping_12354" },
82 ],
83 },
84 },
85 throwOnError: false,
86 })
87
88 const taxRateRules = await service.listTaxRateRules({
89 tax_rate: { tax_region_id: taxRegion.id },
90 })
91
92 expect(taxRateRules.length).toEqual(6)
93 expect(taxRateRules).toEqual(
94 expect.arrayContaining([
95 expect.objectContaining({
96 tax_rate_id: rateOne.id,
97 reference_id: "shipping_12354",
98 }),
99 expect.objectContaining({
100 tax_rate_id: rateOne.id,
101 reference_id: "shipping_11111",
102 }),
103 expect.objectContaining({
104 tax_rate_id: rateOne.id,
105 reference_id: "shipping_22222",
106 }),
107 expect.objectContaining({
108 tax_rate_id: rateTwo.id,
109 reference_id: "product_12354",
110 }),
111 expect.objectContaining({
112 tax_rate_id: rateTwo.id,
113 reference_id: "product_11111",
114 }),
115 expect.objectContaining({
116 tax_rate_id: rateTwo.id,
117 reference_id: "product_22222",
118 }),
119 ])
120 )
121 })
122
123 it("creates rules correctly", async () => {
124 const taxRegion = await service.createTaxRegions({
125 country_code: "us",
126 })
127
128 const [rateOne, rateTwo] = await service.createTaxRates([
129 {
130 tax_region_id: taxRegion.id,
131 rate: 10,
132 code: "standard",
133 name: "Standard",
134 rules: [
135 { reference: "shipping", reference_id: "shipping_12354" },
136 { reference: "shipping", reference_id: "shipping_11111" },
137 { reference: "shipping", reference_id: "shipping_22222" },
138 ],
139 },
140 {
141 tax_region_id: taxRegion.id,
142 rate: 2,
143 code: "reduced",
144 name: "Reduced",
145 rules: [
146 { reference: "product", reference_id: "product_12354" },
147 { reference: "product", reference_id: "product_11111" },
148 { reference: "product", reference_id: "product_22222" },
149 ],
150 },
151 ])
152
153 await updateTaxRatesWorkflow(appContainer).run({
154 input: {
155 selector: { tax_region_id: taxRegion.id },
156 update: {
157 rate: 2,
158 rules: [
159 { reference: "product", reference_id: "product_12354" },
160 { reference: "shipping", reference_id: "shipping_12354" },
161 ],
162 },
163 },
164 })
165
166 const taxRateRules = await service.listTaxRateRules({
167 tax_rate: { tax_region_id: taxRegion.id },
168 })
169
170 expect(taxRateRules.length).toEqual(4)
171 expect(taxRateRules).toEqual(
172 expect.arrayContaining([
173 expect.objectContaining({
174 tax_rate_id: rateOne.id,
175 reference_id: "shipping_12354",
176 }),
177 expect.objectContaining({
178 tax_rate_id: rateTwo.id,
179 reference_id: "shipping_12354",
180 }),
181 expect.objectContaining({
182 tax_rate_id: rateOne.id,
183 reference_id: "product_12354",
184 }),
185 expect.objectContaining({
186 tax_rate_id: rateTwo.id,
187 reference_id: "product_12354",
188 }),
189 ])
190 )
191 })
192 })
193 },
194 })