1 import { IFulfillmentModuleService } from "@medusajs/types"
2 import { Modules } from "@medusajs/utils"
3 import {
4 adminHeaders,
5 createAdminUser,
6 } from "../../../../helpers/create-admin-user"
7
8 const { medusaIntegrationTestRunner } = require("@medusajs/test-utils")
9
10 jest.setTimeout(30000)
11
12 medusaIntegrationTestRunner({
13 testSuite: ({ dbConnection, getContainer, api, dbUtils }) => {
14 let fulfillmentSet1
15
16 beforeAll(async () => {
17 await createAdminUser(dbConnection, adminHeaders, getContainer())
18
19 // fulfillmentSet1 = (
20 // await api.post(
21 // "/admin/fulfillment-sets",
22 // {
23 // name: "Test fulfillment set",
24 // type: "pickup",
25 // },
26 // adminHeaders
27 // )
28 // ).data.fulfillment_set
29
30 // TODO: Add support for creating fulfillment sets through HTTP
31 const fulfillmentModule: IFulfillmentModuleService =
32 getContainer().resolve(Modules.FULFILLMENT)
33
34 fulfillmentSet1 = await fulfillmentModule.createFulfillmentSets({
35 name: "Test fulfillment set",
36 type: "pickup",
37 })
38
39 await dbUtils.snapshot()
40 })
41
42 describe("POST /admin/fulfillment-sets/:id", () => {
43 it("should delete a fulfillment set", async () => {
44 const deleteResponse = await api.delete(
45 `/admin/fulfillment-sets/${fulfillmentSet1.id}`,
46 adminHeaders
47 )
48
49 expect(deleteResponse.data).toEqual({
50 id: fulfillmentSet1.id,
51 object: "fulfillment_set",
52 deleted: true,
53 })
54 })
55 })
56
57 describe("POST /admin/fulfillment-sets/:id/service-zones", () => {
58 it("should create, update, and delete a service zone for a fulfillment set", async () => {
59 const stockLocationResponse = await api.post(
60 `/admin/stock-locations`,
61 {
62 name: "test location",
63 },
64 adminHeaders
65 )
66
67 const stockLocationId = stockLocationResponse.data.stock_location.id
68
69 const locationWithFSetResponse = await api.post(
70 `/admin/stock-locations/${stockLocationId}/fulfillment-sets?fields=id,*fulfillment_sets`,
71 {
72 name: "Fulfillment Set",
73 type: "shipping",
74 },
75 adminHeaders
76 )
77
78 const fulfillmentSetId =
79 locationWithFSetResponse.data.stock_location.fulfillment_sets[0].id
80
81 const response = await api.post(
82 `/admin/fulfillment-sets/${fulfillmentSetId}/service-zones`,
83 {
84 name: "Test Zone",
85 geo_zones: [
86 {
87 country_code: "dk",
88 type: "country",
89 },
90 {
91 country_code: "fr",
92 type: "province",
93 province_code: "fr-idf",
94 },
95 {
96 country_code: "it",
97 type: "city",
98 city: "some city",
99 province_code: "some-province",
100 },
101 {
102 country_code: "it",
103 type: "zip",
104 city: "some city",
105 province_code: "some-province",
106 postal_expression: { type: "regex", exp: "00*" },
107 },
108 ],
109 },
110 adminHeaders
111 )
112
113 const fset = response.data.fulfillment_set
114
115 expect(response.status).toEqual(200)
116 expect(fset).toEqual(
117 expect.objectContaining({
118 name: "Fulfillment Set",
119 type: "shipping",
120 service_zones: expect.arrayContaining([
121 expect.objectContaining({
122 name: "Test Zone",
123 fulfillment_set_id: fulfillmentSetId,
124 geo_zones: expect.arrayContaining([
125 expect.objectContaining({
126 country_code: "dk",
127 type: "country",
128 }),
129 expect.objectContaining({
130 country_code: "fr",
131 type: "province",
132 province_code: "fr-idf",
133 }),
134 expect.objectContaining({
135 country_code: "it",
136 type: "city",
137 city: "some city",
138 province_code: "some-province",
139 }),
140 expect.objectContaining({
141 country_code: "it",
142 type: "zip",
143 city: "some city",
144 province_code: "some-province",
145 postal_expression: { type: "regex", exp: "00*" },
146 }),
147 ]),
148 }),
149 ]),
150 })
151 )
152
153 const serviceZoneId = fset.service_zones[0].id
154
155 const countryGeoZone = fset.service_zones[0].geo_zones.find(
156 (z) => z.type === "country"
157 )
158
159 // Updates an existing and creates a new one
160 const updateResponse = await api.post(
161 `/admin/fulfillment-sets/${fulfillmentSetId}/service-zones/${serviceZoneId}`,
162 {
163 name: "Test Zone Updated",
164 geo_zones: [
165 {
166 id: countryGeoZone.id,
167 country_code: "us",
168 type: "country",
169 },
170 {
171 country_code: "ca",
172 type: "country",
173 },
174 ],
175 },
176 adminHeaders
177 )
178
179 const updatedFset = updateResponse.data.fulfillment_set
180
181 expect(updateResponse.status).toEqual(200)
182 expect(updatedFset).toEqual(
183 expect.objectContaining({
184 name: "Fulfillment Set",
185 type: "shipping",
186 service_zones: expect.arrayContaining([
187 expect.objectContaining({
188 id: serviceZoneId,
189 name: "Test Zone Updated",
190 fulfillment_set_id: updatedFset.id,
191 geo_zones: expect.arrayContaining([
192 expect.objectContaining({
193 id: countryGeoZone.id,
194 country_code: "us",
195 type: "country",
196 }),
197 expect.objectContaining({
198 country_code: "ca",
199 type: "country",
200 }),
201 ]),
202 }),
203 ]),
204 })
205 )
206
207 const deleteResponse = await api.delete(
208 `/admin/fulfillment-sets/${fulfillmentSetId}/service-zones/${serviceZoneId}`,
209 adminHeaders
210 )
211
212 expect(deleteResponse.status).toEqual(200)
213 expect(deleteResponse.data).toEqual(
214 expect.objectContaining({
215 id: serviceZoneId,
216 object: "service_zone",
217 deleted: true,
218 parent: expect.objectContaining({
219 id: fulfillmentSetId,
220 }),
221 })
222 )
223
224 const serviceZoneResponse = await api
225 .get(
226 `/admin/fulfillment-sets/${fulfillmentSetId}/service-zones/${serviceZoneId}`,
227 adminHeaders
228 )
229 .catch((err) => err.response)
230
231 expect(serviceZoneResponse.status).toEqual(404)
232 expect(serviceZoneResponse.data.message).toEqual(
233 `Service zone with id: ${serviceZoneId} not found`
234 )
235 })
236
237 it("should throw if invalid type is passed", async () => {
238 const stockLocationResponse = await api.post(
239 `/admin/stock-locations`,
240 {
241 name: "test location",
242 },
243 adminHeaders
244 )
245
246 const stockLocationId = stockLocationResponse.data.stock_location.id
247
248 const locationWithFSetResponse = await api.post(
249 `/admin/stock-locations/${stockLocationId}/fulfillment-sets?fields=id,*fulfillment_sets`,
250 {
251 name: "Fulfillment Set",
252 type: "shipping",
253 },
254 adminHeaders
255 )
256
257 const fulfillmentSetId =
258 locationWithFSetResponse.data.stock_location.fulfillment_sets[0].id
259
260 const errorResponse = await api
261 .post(
262 `/admin/fulfillment-sets/${fulfillmentSetId}/service-zones`,
263 {
264 name: "Test Zone",
265 geo_zones: [
266 {
267 country_code: "dk",
268 type: "country",
269 },
270 {
271 country_code: "fr",
272 type: "province",
273 province_code: "fr-idf",
274 },
275 {
276 country_code: "it",
277 type: "region",
278 city: "some region",
279 province_code: "some-province",
280 },
281 {
282 country_code: "it",
283 type: "zip",
284 city: "some city",
285 province_code: "some-province",
286 postal_expression: {},
287 },
288 ],
289 },
290 adminHeaders
291 )
292 .catch((err) => err.response)
293
294 expect(errorResponse.status).toEqual(400)
295 expect(errorResponse.data.message).toContain(
296 `Invalid request: Field 'geo_zones, 2, type' is required`
297 )
298 })
299
300 describe("POST /admin/fulfillment-sets/:id/service-zones", () => {
301 it("should throw when fulfillment set doesn't exist", async () => {
302 const deleteResponse = await api
303 .delete(
304 `/admin/fulfillment-sets/foo/service-zones/bar`,
305 adminHeaders
306 )
307 .catch((e) => e.response)
308
309 expect(deleteResponse.status).toEqual(404)
310 expect(deleteResponse.data.message).toEqual(
311 "FulfillmentSet with id: foo was not found"
312 )
313 })
314
315 it("should throw when fulfillment set doesn't have service zone", async () => {
316 const stockLocationResponse = await api.post(
317 `/admin/stock-locations`,
318 {
319 name: "test location",
320 },
321 adminHeaders
322 )
323
324 const stockLocationId = stockLocationResponse.data.stock_location.id
325
326 const locationWithFSetResponse = await api.post(
327 `/admin/stock-locations/${stockLocationId}/fulfillment-sets?fields=id,*fulfillment_sets`,
328 {
329 name: "Fulfillment Set",
330 type: "shipping",
331 },
332 adminHeaders
333 )
334
335 const fulfillmentSetId =
336 locationWithFSetResponse.data.stock_location.fulfillment_sets[0].id
337
338 const deleteResponse = await api
339 .delete(
340 `/admin/fulfillment-sets/${fulfillmentSetId}/service-zones/foo`,
341 adminHeaders
342 )
343 .catch((e) => e.response)
344
345 expect(deleteResponse.status).toEqual(404)
346 expect(deleteResponse.data.message).toEqual(
347 "Service zone with id: foo not found on fulfillment set"
348 )
349 })
350 })
351 })
352 },
353 })