1 import {
2 batchShippingOptionRulesWorkflow,
3 createShippingOptionsWorkflow,
4 } from "@medusajs/core-flows"
5 import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
6 import {
7 BatchWorkflowInput,
8 CreateShippingOptionRuleDTO,
9 FulfillmentSetDTO,
10 FulfillmentWorkflow,
11 IFulfillmentModuleService,
12 IRegionModuleService,
13 ServiceZoneDTO,
14 ShippingProfileDTO,
15 UpdateShippingOptionRuleDTO,
16 } from "@medusajs/types"
17 import {
18 ContainerRegistrationKeys,
19 Modules,
20 RuleOperator,
21 remoteQueryObjectFromString,
22 } from "@medusajs/utils"
23
24 jest.setTimeout(100000)
25
26 const env = {}
27 const provider_id = "manual_test-provider"
28
29 async function createShippingOptionFixture({
30 container,
31 serviceZone,
32 shippingProfile,
33 }) {
34 const regionService = container.resolve(
35 Modules.REGION
36 ) as IRegionModuleService
37
38 const [region] = await regionService.createRegions([
39 {
40 name: "Test region",
41 currency_code: "eur",
42 countries: ["fr"],
43 },
44 ])
45
46 const shippingOptionData: FulfillmentWorkflow.CreateShippingOptionsWorkflowInput =
47 {
48 name: "Test shipping option",
49 price_type: "flat",
50 service_zone_id: serviceZone.id,
51 shipping_profile_id: shippingProfile.id,
52 provider_id,
53 type: {
54 code: "manual-type",
55 label: "Manual Type",
56 description: "Manual Type Description",
57 },
58 prices: [
59 {
60 currency_code: "usd",
61 amount: 10,
62 },
63 {
64 region_id: region.id,
65 amount: 100,
66 },
67 ],
68 rules: [
69 {
70 attribute: "total",
71 operator: RuleOperator.EQ,
72 value: "100",
73 },
74 {
75 attribute: "is_store",
76 operator: RuleOperator.EQ,
77 value: "true",
78 },
79 ],
80 }
81
82 const { result } = await createShippingOptionsWorkflow(container).run({
83 input: [shippingOptionData],
84 })
85
86 const remoteQuery = container.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
87
88 const remoteQueryObject = remoteQueryObjectFromString({
89 entryPoint: "shipping_option",
90 variables: {
91 id: result[0].id,
92 },
93 fields: [
94 "id",
95 "name",
96 "price_type",
97 "service_zone_id",
98 "shipping_profile_id",
99 "provider_id",
100 "data",
101 "metadata",
102 "type.*",
103 "created_at",
104 "updated_at",
105 "deleted_at",
106 "shipping_option_type_id",
107 "prices.*",
108 "rules.*",
109 ],
110 })
111
112 const [createdShippingOption] = await remoteQuery(remoteQueryObject)
113
114 return createdShippingOption
115 }
116
117 medusaIntegrationTestRunner({
118 env,
119 testSuite: ({ getContainer }) => {
120 let service: IFulfillmentModuleService
121 let container
122
123 beforeAll(() => {
124 container = getContainer()
125 service = container.resolve(Modules.FULFILLMENT)
126 })
127
128 describe("Fulfillment workflows", () => {
129 let fulfillmentSet: FulfillmentSetDTO
130 let serviceZone: ServiceZoneDTO
131 let shippingProfile: ShippingProfileDTO
132
133 beforeEach(async () => {
134 shippingProfile = await service.createShippingProfiles({
135 name: "test",
136 type: "default",
137 })
138
139 fulfillmentSet = await service.createFulfillmentSets({
140 name: "Test fulfillment set",
141 type: "manual_test",
142 })
143
144 serviceZone = await service.createServiceZones({
145 name: "Test service zone",
146 fulfillment_set_id: fulfillmentSet.id,
147 geo_zones: [
148 {
149 type: "country",
150 country_code: "US",
151 },
152 ],
153 })
154
155 const stockLocationModule = container.resolve(Modules.STOCK_LOCATION)
156
157 const location = await stockLocationModule.createStockLocations({
158 name: "Europe",
159 })
160
161 const remoteLink = container.resolve(
162 ContainerRegistrationKeys.REMOTE_LINK
163 )
164
165 await remoteLink.create([
166 {
167 [Modules.STOCK_LOCATION]: {
168 stock_location_id: location.id,
169 },
170 [Modules.FULFILLMENT]: {
171 fulfillment_set_id: fulfillmentSet.id,
172 },
173 },
174 {
175 [Modules.STOCK_LOCATION]: {
176 stock_location_id: location.id,
177 },
178 [Modules.FULFILLMENT]: {
179 fulfillment_provider_id: "manual_test-provider",
180 },
181 },
182 ])
183 })
184
185 it("should create, update and delete rules in batch", async () => {
186 const shippingOption = await createShippingOptionFixture({
187 container,
188 serviceZone,
189 shippingProfile,
190 })
191
192 expect(shippingOption.rules).toHaveLength(2)
193
194 const ruleToUpdate = shippingOption.rules.find((rule) => {
195 return rule.attribute === "is_store"
196 })
197 const ruleToDelete = shippingOption.rules.find((rule) => {
198 return rule.attribute === "total"
199 })
200
201 const workflowInput: BatchWorkflowInput<
202 CreateShippingOptionRuleDTO,
203 UpdateShippingOptionRuleDTO
204 > = {
205 create: [
206 {
207 shipping_option_id: shippingOption.id,
208 attribute: "new_attribute",
209 operator: RuleOperator.EQ,
210 value: "100",
211 },
212 ],
213 update: [
214 {
215 ...ruleToUpdate,
216 value: "false",
217 },
218 ],
219 delete: [ruleToDelete.id],
220 }
221
222 await batchShippingOptionRulesWorkflow(container).run({
223 input: workflowInput,
224 })
225
226 const remoteQuery = container.resolve(
227 ContainerRegistrationKeys.REMOTE_QUERY
228 )
229
230 const remoteQueryObject = remoteQueryObjectFromString({
231 entryPoint: "shipping_option",
232 variables: {
233 id: shippingOption.id,
234 },
235 fields: ["id", "rules.*"],
236 })
237
238 const [updatedShippingOption] = await remoteQuery(remoteQueryObject)
239
240 const newAttrRule = updatedShippingOption.rules.find((rule) => {
241 return rule.attribute === "new_attribute"
242 })
243 const updatedRule = updatedShippingOption.rules.find((rule) => {
244 return rule.attribute === "is_store"
245 })
246
247 expect(updatedShippingOption.rules).toHaveLength(2)
248 expect(newAttrRule).toEqual(
249 expect.objectContaining({
250 attribute: "new_attribute",
251 operator: "eq",
252 value: "100",
253 })
254 )
255 expect(updatedRule).toEqual(
256 expect.objectContaining({
257 value: "false",
258 })
259 )
260 })
261
262 it("should revert the shipping options rules batch actions", async () => {
263 const shippingOption = await createShippingOptionFixture({
264 container,
265 serviceZone,
266 shippingProfile,
267 })
268
269 expect(shippingOption.rules).toHaveLength(2)
270
271 const ruleToUpdate = shippingOption.rules.find((rule) => {
272 return rule.attribute === "is_store"
273 })
274 const ruleToDelete = shippingOption.rules.find((rule) => {
275 return rule.attribute === "total"
276 })
277
278 const workflowInput: BatchWorkflowInput<
279 CreateShippingOptionRuleDTO,
280 UpdateShippingOptionRuleDTO
281 > = {
282 create: [
283 {
284 shipping_option_id: shippingOption.id,
285 attribute: "new_attribute",
286 operator: RuleOperator.EQ,
287 value: "100",
288 },
289 ],
290 update: [
291 {
292 ...ruleToUpdate,
293 value: "false",
294 },
295 ],
296 delete: [ruleToDelete.id],
297 }
298
299 const workflow = batchShippingOptionRulesWorkflow(container)
300
301 workflow.addAction(
302 "throw",
303 {
304 invoke: async function failStep() {
305 throw new Error(`Failed to update shipping option rules`)
306 },
307 },
308 {
309 noCompensation: true,
310 }
311 )
312
313 const { errors } = await workflow.run({
314 input: workflowInput,
315 throwOnError: false,
316 })
317
318 expect(errors).toHaveLength(1)
319 expect(errors[0].error.message).toEqual(
320 `Failed to update shipping option rules`
321 )
322
323 const remoteQuery = container.resolve(
324 ContainerRegistrationKeys.REMOTE_QUERY
325 )
326
327 const remoteQueryObject = remoteQueryObjectFromString({
328 entryPoint: "shipping_option",
329 variables: {
330 id: shippingOption.id,
331 },
332 fields: ["id", "rules.*"],
333 })
334
335 const [updatedShippingOption] = await remoteQuery(remoteQueryObject)
336
337 expect(updatedShippingOption.rules).toHaveLength(2)
338 expect(updatedShippingOption.rules).toEqual(
339 expect.arrayContaining([
340 expect.objectContaining({
341 attribute: "is_store",
342 operator: "eq",
343 value: "true",
344 }),
345 expect.objectContaining({
346 attribute: "total",
347 operator: "eq",
348 value: "100",
349 }),
350 ])
351 )
352 })
353 })
354 },
355 })