1 import {
2 createLinksWorkflow,
3 createLinksWorkflowId,
4 dismissLinksWorkflow,
5 dismissLinksWorkflowId,
6 updateLinksWorkflow,
7 updateLinksWorkflowId,
8 } from "@medusajs/core-flows"
9 import { Modules } from "@medusajs/utils"
10 import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
11 import {
12 adminHeaders,
13 createAdminUser,
14 } from "../../../helpers/create-admin-user"
15
16 jest.setTimeout(50000)
17
18 medusaIntegrationTestRunner({
19 env: {},
20 testSuite: ({ getContainer, api, dbConnection, dbUtils }) => {
21 describe("Workflows: Common", () => {
22 let appContainer
23 let product
24 let variant
25
26 beforeAll(async () => {
27 appContainer = getContainer()
28 })
29
30 beforeAll(async () => {
31 await createAdminUser(dbConnection, adminHeaders, getContainer())
32
33 const shippingProfile = (
34 await api.post(
35 `/admin/shipping-profiles`,
36 { name: "Test", type: "default" },
37 adminHeaders
38 )
39 ).data.shipping_profile
40
41 product = (
42 await api.post(
43 "/admin/products",
44 {
45 title: "product 1",
46 shipping_profile_id: shippingProfile.id,
47 options: [{ title: "size", values: ["x", "l"] }],
48 variants: [
49 {
50 title: "variant 1",
51 options: { size: "x" },
52 prices: [{ currency_code: "usd", amount: 100 }],
53 },
54 ],
55 },
56 adminHeaders
57 )
58 ).data.product
59
60 variant = product.variants[0]
61
62 await dbUtils.snapshot()
63 })
64
65 describe("createLinksWorkflow", () => {
66 describe("compensation", () => {
67 it("should dismiss links when step throws an error", async () => {
68 const workflow = createLinksWorkflow(appContainer)
69 const workflowId = createLinksWorkflowId
70 const inventoryItem = (
71 await api.post(
72 `/admin/inventory-items`,
73 { sku: "12345" },
74 adminHeaders
75 )
76 ).data.inventory_item
77
78 workflow.appendAction("throw", workflowId, {
79 invoke: async function failStep() {
80 throw new Error(`Fail`)
81 },
82 })
83
84 const { errors } = await workflow.run({
85 input: [
86 {
87 [Modules.PRODUCT]: { variant_id: variant.id },
88 [Modules.INVENTORY]: { inventory_item_id: inventoryItem.id },
89 data: { required_quantity: 10 },
90 },
91 ],
92 throwOnError: false,
93 })
94
95 expect(errors).toEqual([
96 {
97 action: "throw",
98 handlerType: "invoke",
99 error: expect.objectContaining({
100 message: `Fail`,
101 }),
102 },
103 ])
104
105 const updatedVariant = (
106 await api.get(
107 `/admin/products/${product.id}/variants/${variant.id}?fields=inventory_items.inventory.*,inventory_items.*`,
108 adminHeaders
109 )
110 ).data.variant
111
112 // The default inventory item remains that was created as a part of create product
113 expect(updatedVariant.inventory_items).toHaveLength(1)
114 })
115 })
116 })
117
118 describe("updateLinksWorkflow", () => {
119 describe("compensation", () => {
120 it("should revert link data when step throws an error", async () => {
121 const workflow = updateLinksWorkflow(appContainer)
122 const workflowId = updateLinksWorkflowId
123 const originalQuantity = 5
124 const newQuantity = 10
125 const inventoryItem = (
126 await api.post(
127 `/admin/inventory-items`,
128 { sku: "12345" },
129 adminHeaders
130 )
131 ).data.inventory_item
132
133 await api.post(
134 `/admin/products/${product.id}/variants/${variant.id}/inventory-items`,
135 {
136 inventory_item_id: inventoryItem.id,
137 required_quantity: originalQuantity,
138 },
139 adminHeaders
140 )
141
142 workflow.appendAction("throw", workflowId, {
143 invoke: async function failStep() {
144 throw new Error(`Fail`)
145 },
146 })
147
148 const { errors } = await workflow.run({
149 input: [
150 {
151 [Modules.PRODUCT]: { variant_id: variant.id },
152 [Modules.INVENTORY]: { inventory_item_id: inventoryItem.id },
153 data: { required_quantity: newQuantity },
154 },
155 ],
156 throwOnError: false,
157 })
158
159 expect(errors).toEqual([
160 {
161 action: "throw",
162 handlerType: "invoke",
163 error: expect.objectContaining({
164 message: `Fail`,
165 }),
166 },
167 ])
168
169 const updatedVariant = (
170 await api.get(
171 `/admin/products/${product.id}/variants/${variant.id}?fields=inventory_items.inventory.*,inventory_items.*`,
172 adminHeaders
173 )
174 ).data.variant
175
176 expect(updatedVariant.inventory_items).toHaveLength(2)
177 expect(updatedVariant.inventory_items).toEqual(
178 expect.arrayContaining([
179 expect.objectContaining({
180 required_quantity: originalQuantity,
181 }),
182 ])
183 )
184 })
185
186 it("should throw an error when a link is not found", async () => {
187 const workflow = updateLinksWorkflow(appContainer)
188 const workflowId = updateLinksWorkflowId
189
190 workflow.appendAction("throw", workflowId, {
191 invoke: async function failStep() {
192 throw new Error(`Fail`)
193 },
194 })
195
196 const { errors } = await workflow.run({
197 input: [
198 {
199 [Modules.PRODUCT]: { variant_id: variant.id },
200 [Modules.INVENTORY]: {
201 inventory_item_id: "does-not-exist-id",
202 },
203 data: { required_quantity: 10 },
204 },
205 ],
206 throwOnError: false,
207 })
208
209 expect(errors).toEqual([
210 {
211 action: "update-remote-links-step",
212 handlerType: "invoke",
213 error: expect.objectContaining({
214 message: `Could not find all existing links from data`,
215 }),
216 },
217 ])
218 })
219 })
220 })
221
222 describe("dismissLinksWorkflow", () => {
223 describe("compensation", () => {
224 it("should recreate dismissed links when step throws an error", async () => {
225 const originalQuantity = 10
226 const workflow = dismissLinksWorkflow(appContainer)
227 const workflowId = dismissLinksWorkflowId
228 const inventoryItem = (
229 await api.post(
230 `/admin/inventory-items`,
231 { sku: "12345" },
232 adminHeaders
233 )
234 ).data.inventory_item
235
236 await api.post(
237 `/admin/products/${product.id}/variants/${variant.id}/inventory-items`,
238 {
239 inventory_item_id: inventoryItem.id,
240 required_quantity: originalQuantity,
241 },
242 adminHeaders
243 )
244
245 workflow.appendAction("throw", workflowId, {
246 invoke: async function failStep() {
247 throw new Error(`Fail`)
248 },
249 })
250
251 const { errors } = await workflow.run({
252 input: [
253 {
254 [Modules.PRODUCT]: { variant_id: variant.id },
255 [Modules.INVENTORY]: { inventory_item_id: inventoryItem.id },
256 },
257 ],
258 throwOnError: false,
259 })
260
261 expect(errors).toEqual([
262 {
263 action: "throw",
264 handlerType: "invoke",
265 error: expect.objectContaining({
266 message: `Fail`,
267 }),
268 },
269 ])
270
271 const updatedVariant = (
272 await api.get(
273 `/admin/products/${product.id}/variants/${variant.id}?fields=inventory_items.inventory.*,inventory_items.*`,
274 adminHeaders
275 )
276 ).data.variant
277
278 expect(updatedVariant.inventory_items).toHaveLength(2)
279 expect(updatedVariant.inventory_items).toEqual(
280 expect.arrayContaining([
281 expect.objectContaining({
282 required_quantity: originalQuantity,
283 }),
284 ])
285 )
286 })
287
288 it("should pass dismiss step if link not found if next step throws error", async () => {
289 const workflow = dismissLinksWorkflow(appContainer)
290 const workflowId = dismissLinksWorkflowId
291
292 workflow.appendAction("throw", workflowId, {
293 invoke: async function failStep() {
294 throw new Error(`Fail`)
295 },
296 })
297
298 const { errors } = await workflow.run({
299 input: [
300 {
301 [Modules.PRODUCT]: { variant_id: variant.id },
302 [Modules.INVENTORY]: {
303 inventory_item_id: "does-not-exist-id",
304 },
305 },
306 ],
307 throwOnError: false,
308 })
309
310 expect(errors).toEqual([
311 {
312 action: "throw",
313 handlerType: "invoke",
314 error: expect.objectContaining({
315 message: `Fail`,
316 }),
317 },
318 ])
319
320 const updatedVariant = (
321 await api.get(
322 `/admin/products/${product.id}/variants/${variant.id}?fields=inventory_items.inventory.*,inventory_items.*`,
323 adminHeaders
324 )
325 ).data.variant
326
327 expect(updatedVariant.inventory_items).toHaveLength(1)
328 })
329 })
330 })
331 })
332 },
333 })