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