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