1 import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
2 import {
3 createAdminUser,
4 generatePublishableKey,
5 generateStoreHeaders,
6 } from "../../../../helpers/create-admin-user"
7 import { ProductStatus } from "@medusajs/utils"
8
9 jest.setTimeout(50000)
10
11 const env = {}
12 const adminHeaders = { headers: { "x-medusa-access-token": "test_token" } }
13 const adminTemplateName = "shipping-option-admin-template"
14
15 medusaIntegrationTestRunner({
16 env,
17 testSuite: ({ dbConnection, getContainer, api, dbUtils }) => {
18 beforeAll(async () => {
19 await createAdminUser(dbConnection, adminHeaders, getContainer())
20 await dbUtils.snapshot({ templateName: adminTemplateName })
21 })
22
23 describe("Store: Shipping Option API", () => {
24 let appContainer
25 let salesChannel
26 let region
27 let regionTwo
28 let product
29 let stockLocation
30 let shippingProfile
31 let fulfillmentSet
32 let cart
33 let shippingOption
34 let storeHeaders
35
36 beforeAll(async () => {
37 appContainer = getContainer()
38
39 const publishableKey = await generatePublishableKey(appContainer)
40 storeHeaders = generateStoreHeaders({ publishableKey })
41
42 region = (
43 await api.post(
44 "/admin/regions",
45 { name: "US", currency_code: "usd", countries: ["US"] },
46 adminHeaders
47 )
48 ).data.region
49
50 regionTwo = (
51 await api.post(
52 "/admin/regions",
53 {
54 name: "Test region two",
55 currency_code: "dkk",
56 countries: ["DK"],
57 is_tax_inclusive: true,
58 },
59 adminHeaders
60 )
61 ).data.region
62
63 shippingProfile = (
64 await api.post(
65 `/admin/shipping-profiles`,
66 { name: "Test", type: "default" },
67 adminHeaders
68 )
69 ).data.shipping_profile
70
71 salesChannel = (
72 await api.post(
73 "/admin/sales-channels",
74 { name: "first channel", description: "channel" },
75 adminHeaders
76 )
77 ).data.sales_channel
78
79 product = (
80 await api.post(
81 "/admin/products",
82 {
83 title: "Test fixture",
84 status: ProductStatus.PUBLISHED,
85 options: [
86 { title: "size", values: ["large", "small"] },
87 { title: "color", values: ["green"] },
88 ],
89 shipping_profile_id: shippingProfile.id,
90 variants: [
91 {
92 title: "Test variant",
93 manage_inventory: false,
94 prices: [
95 {
96 currency_code: "usd",
97 amount: 100,
98 },
99 {
100 currency_code: "dkk",
101 amount: 100,
102 },
103 ],
104 options: {
105 size: "large",
106 color: "green",
107 },
108 },
109 ],
110 },
111 adminHeaders
112 )
113 ).data.product
114
115 stockLocation = (
116 await api.post(
117 `/admin/stock-locations`,
118 { name: "test location" },
119 adminHeaders
120 )
121 ).data.stock_location
122
123 await api.post(
124 `/admin/stock-locations/${stockLocation.id}/sales-channels`,
125 { add: [salesChannel.id] },
126 adminHeaders
127 )
128
129 const fulfillmentSets = (
130 await api.post(
131 `/admin/stock-locations/${stockLocation.id}/fulfillment-sets?fields=*fulfillment_sets`,
132 {
133 name: "Test",
134 type: "test-type",
135 },
136 adminHeaders
137 )
138 ).data.stock_location.fulfillment_sets
139
140 fulfillmentSet = (
141 await api.post(
142 `/admin/fulfillment-sets/${fulfillmentSets[0].id}/service-zones`,
143 {
144 name: "Test",
145 geo_zones: [
146 { type: "country", country_code: "us" },
147 { type: "country", country_code: "dk" },
148 ],
149 },
150 adminHeaders
151 )
152 ).data.fulfillment_set
153
154 await api.post(
155 `/admin/stock-locations/${stockLocation.id}/fulfillment-providers`,
156 { add: ["manual_test-provider"] },
157 adminHeaders
158 )
159
160 shippingOption = (
161 await api.post(
162 `/admin/shipping-options`,
163 {
164 name: "Test shipping option",
165 service_zone_id: fulfillmentSet.service_zones[0].id,
166 shipping_profile_id: shippingProfile.id,
167 provider_id: "manual_test-provider",
168 price_type: "flat",
169 type: {
170 label: "Test type",
171 description: "Test description",
172 code: "test-code",
173 },
174 prices: [
175 {
176 currency_code: "usd",
177 amount: 1000,
178 },
179 {
180 region_id: region.id,
181 amount: 1100,
182 },
183 {
184 region_id: region.id,
185 amount: 0,
186 rules: [
187 {
188 operator: "gt",
189 attribute: "item_total",
190 value: 2000,
191 },
192 ],
193 },
194 {
195 region_id: regionTwo.id,
196 amount: 500,
197 },
198 ],
199 rules: [],
200 },
201 adminHeaders
202 )
203 ).data.shipping_option
204
205 await dbUtils.snapshot()
206 })
207
208 describe("GET /store/shipping-options?cart_id=", () => {
209 it("should get shipping options for a cart successfully", async () => {
210 cart = (
211 await api.post(
212 `/store/carts`,
213 {
214 region_id: region.id,
215 sales_channel_id: salesChannel.id,
216 currency_code: "usd",
217 email: "test@admin.com",
218 items: [
219 {
220 variant_id: product.variants[0].id,
221 quantity: 1,
222 },
223 ],
224 },
225 storeHeaders
226 )
227 ).data.cart
228
229 const resp = await api.get(
230 `/store/shipping-options?cart_id=${cart.id}`,
231 storeHeaders
232 )
233
234 const shippingOptions = resp.data.shipping_options
235
236 expect(shippingOptions).toHaveLength(1)
237 expect(shippingOptions[0]).toEqual(
238 expect.objectContaining({
239 id: shippingOption.id,
240 name: "Test shipping option",
241 price_type: "flat",
242 amount: 1100,
243 calculated_price: expect.objectContaining({
244 calculated_amount: 1100,
245 }),
246 })
247 )
248
249 cart = (
250 await api.post(
251 `/store/carts/${cart.id}`,
252 {
253 region_id: regionTwo.id,
254 },
255 storeHeaders
256 )
257 ).data.cart
258
259 const secondResp = await api.get(
260 `/store/shipping-options?cart_id=${cart.id}`,
261 storeHeaders
262 )
263
264 expect(secondResp.data.shipping_options).toHaveLength(1)
265 expect(secondResp.data.shipping_options[0]).toEqual(
266 expect.objectContaining({
267 id: shippingOption.id,
268 name: "Test shipping option",
269 amount: 500,
270 is_tax_inclusive: true,
271 calculated_price: expect.objectContaining({
272 calculated_amount: 500,
273 is_calculated_price_tax_inclusive: true,
274 }),
275 price_type: "flat",
276 })
277 )
278 })
279
280 it("should return prices based on cart total", async () => {
281 cart = (
282 await api.post(
283 `/store/carts`,
284 {
285 region_id: region.id,
286 sales_channel_id: salesChannel.id,
287 currency_code: "usd",
288 email: "test@admin.com",
289 items: [
290 {
291 variant_id: product.variants[0].id,
292 // Adding a quantity of 100 to emulate total being greater than 2000
293 quantity: 100,
294 },
295 ],
296 },
297 storeHeaders
298 )
299 ).data.cart
300
301 const resp = await api.get(
302 `/store/shipping-options?cart_id=${cart.id}`,
303 storeHeaders
304 )
305
306 const shippingOptions = resp.data.shipping_options
307
308 expect(shippingOptions).toHaveLength(1)
309 expect(shippingOptions[0]).toEqual(
310 expect.objectContaining({
311 id: shippingOption.id,
312 name: "Test shipping option",
313 // Free shipping due to cart total being greater than 2000
314 amount: 0,
315 calculated_price: expect.objectContaining({
316 calculated_amount: 0,
317 }),
318 price_type: "flat",
319 })
320 )
321 })
322
323 it("should throw error when cart_id is not passed as a parameter", async () => {
324 const { response } = await api
325 .get(`/store/shipping-options`, storeHeaders)
326 .catch((e) => e)
327
328 expect(response.data).toEqual({
329 type: "invalid_data",
330 message: "Invalid request: Field 'cart_id' is required",
331 })
332 })
333 })
334 })
335
336 describe("with insufficient inventory", () => {
337 let appContainer
338 let salesChannel
339 let region
340 let product
341 let stockLocation1
342 let stockLocation2
343 let stockLocation3
344 let shippingProfile
345 let fulfillmentSet
346 let shippingOption1
347 let shippingOption2
348 let shippingOption3
349 let storeHeaders
350
351 beforeAll(async () => {
352 await dbUtils.restore({ templateName: adminTemplateName })
353 appContainer = getContainer()
354
355 const publishableKey = await generatePublishableKey(appContainer)
356 storeHeaders = generateStoreHeaders({ publishableKey })
357
358 region = (
359 await api.post(
360 "/admin/regions",
361 { name: "US", currency_code: "usd", countries: ["us"] },
362 adminHeaders
363 )
364 ).data.region
365
366 shippingProfile = (
367 await api.post(
368 `/admin/shipping-profiles`,
369 { name: "Test", type: "default" },
370 adminHeaders
371 )
372 ).data.shipping_profile
373
374 salesChannel = (
375 await api.post(
376 "/admin/sales-channels",
377 { name: "first channel", description: "channel" },
378 adminHeaders
379 )
380 ).data.sales_channel
381
382 stockLocation1 = (
383 await api.post(
384 `/admin/stock-locations`,
385 { name: "test location" },
386 adminHeaders
387 )
388 ).data.stock_location
389
390 const fulfillmentSets1 = (
391 await api.post(
392 `/admin/stock-locations/${stockLocation1.id}/fulfillment-sets?fields=*fulfillment_sets`,
393 {
394 name: "Test 1",
395 type: "pickup",
396 },
397 adminHeaders
398 )
399 ).data.stock_location.fulfillment_sets
400
401 fulfillmentSet = (
402 await api.post(
403 `/admin/fulfillment-sets/${fulfillmentSets1[0].id}/service-zones`,
404 {
405 name: "Test 1",
406 geo_zones: [
407 { type: "country", country_code: "us" },
408 { type: "country", country_code: "dk" },
409 ],
410 },
411 adminHeaders
412 )
413 ).data.fulfillment_set
414
415 await api.post(
416 `/admin/stock-locations/${stockLocation1.id}/fulfillment-providers`,
417 { add: ["manual_test-provider"] },
418 adminHeaders
419 )
420
421 shippingOption1 = (
422 await api.post(
423 `/admin/shipping-options`,
424 {
425 name: "Pickup option 1",
426 service_zone_id: fulfillmentSet.service_zones[0].id,
427 shipping_profile_id: shippingProfile.id,
428 provider_id: "manual_test-provider",
429 price_type: "flat",
430 type: {
431 label: "Test type",
432 description: "Test description",
433 code: "test-code",
434 },
435 prices: [
436 {
437 currency_code: "usd",
438 amount: 0,
439 },
440 {
441 region_id: region.id,
442 amount: 0,
443 },
444 ],
445 rules: [],
446 },
447 adminHeaders
448 )
449 ).data.shipping_option
450
451 stockLocation2 = (
452 await api.post(
453 `/admin/stock-locations`,
454 { name: "test location" },
455 adminHeaders
456 )
457 ).data.stock_location
458
459 const fulfillmentSets2 = (
460 await api.post(
461 `/admin/stock-locations/${stockLocation2.id}/fulfillment-sets?fields=*fulfillment_sets`,
462 {
463 name: "Test 2",
464 type: "pickup",
465 },
466 adminHeaders
467 )
468 ).data.stock_location.fulfillment_sets
469
470 fulfillmentSet = (
471 await api.post(
472 `/admin/fulfillment-sets/${fulfillmentSets2[0].id}/service-zones`,
473 {
474 name: "Test 2",
475 geo_zones: [
476 { type: "country", country_code: "us" },
477 { type: "country", country_code: "dk" },
478 ],
479 },
480 adminHeaders
481 )
482 ).data.fulfillment_set
483
484 await api.post(
485 `/admin/stock-locations/${stockLocation2.id}/fulfillment-providers`,
486 { add: ["manual_test-provider"] },
487 adminHeaders
488 )
489
490 shippingOption2 = (
491 await api.post(
492 `/admin/shipping-options`,
493 {
494 name: "Pickup option 2",
495 service_zone_id: fulfillmentSet.service_zones[0].id,
496 shipping_profile_id: shippingProfile.id,
497 provider_id: "manual_test-provider",
498 price_type: "flat",
499 type: {
500 label: "Test type",
501 description: "Test description",
502 code: "test-code",
503 },
504 prices: [
505 {
506 currency_code: "usd",
507 amount: 0,
508 },
509 {
510 region_id: region.id,
511 amount: 0,
512 },
513 ],
514 rules: [],
515 },
516 adminHeaders
517 )
518 ).data.shipping_option
519
520 stockLocation3 = (
521 await api.post(
522 `/admin/stock-locations`,
523 { name: "test location" },
524 adminHeaders
525 )
526 ).data.stock_location
527
528 const fulfillmentSets3 = (
529 await api.post(
530 `/admin/stock-locations/${stockLocation3.id}/fulfillment-sets?fields=*fulfillment_sets`,
531 {
532 name: "Test 3",
533 type: "pickup",
534 },
535 adminHeaders
536 )
537 ).data.stock_location.fulfillment_sets
538
539 fulfillmentSet = (
540 await api.post(
541 `/admin/fulfillment-sets/${fulfillmentSets3[0].id}/service-zones`,
542 {
543 name: "Test 3",
544 geo_zones: [
545 { type: "country", country_code: "us" },
546 { type: "country", country_code: "dk" },
547 ],
548 },
549 adminHeaders
550 )
551 ).data.fulfillment_set
552
553 await api.post(
554 `/admin/stock-locations/${stockLocation3.id}/fulfillment-providers`,
555 { add: ["manual_test-provider"] },
556 adminHeaders
557 )
558
559 shippingOption3 = (
560 await api.post(
561 `/admin/shipping-options`,
562 {
563 name: "Pickup option 3",
564 service_zone_id: fulfillmentSet.service_zones[0].id,
565 shipping_profile_id: shippingProfile.id,
566 provider_id: "manual_test-provider",
567 price_type: "flat",
568 type: {
569 label: "Test type",
570 description: "Test description",
571 code: "test-code",
572 },
573 prices: [
574 {
575 currency_code: "usd",
576 amount: 0,
577 },
578 {
579 region_id: region.id,
580 amount: 0,
581 },
582 ],
583 rules: [],
584 },
585 adminHeaders
586 )
587 ).data.shipping_option
588
589 const inventoryItem = (
590 await api.post(
591 `/admin/inventory-items`,
592 { sku: "inventory-item" },
593 adminHeaders
594 )
595 ).data.inventory_item
596
597 await api.post(
598 `/admin/inventory-items/${inventoryItem.id}/location-levels`,
599 {
600 location_id: stockLocation1.id,
601 stocked_quantity: 10,
602 },
603 adminHeaders
604 )
605
606 await api.post(
607 `/admin/inventory-items/${inventoryItem.id}/location-levels`,
608 {
609 location_id: stockLocation2.id,
610 stocked_quantity: 5,
611 },
612 adminHeaders
613 )
614
615 // STOCK LOCATION 3 doesn't have any inventory for that item
616
617 await api.post(
618 `/admin/stock-locations/${stockLocation1.id}/sales-channels`,
619 { add: [salesChannel.id] },
620 adminHeaders
621 )
622
623 await api.post(
624 `/admin/stock-locations/${stockLocation2.id}/sales-channels`,
625 { add: [salesChannel.id] },
626 adminHeaders
627 )
628
629 await api.post(
630 `/admin/stock-locations/${stockLocation3.id}/sales-channels`,
631 { add: [salesChannel.id] },
632 adminHeaders
633 )
634
635 product = (
636 await api.post(
637 "/admin/products",
638 {
639 title: "Test prod",
640 status: ProductStatus.PUBLISHED,
641 options: [
642 { title: "size", values: ["large", "small"] },
643 { title: "color", values: ["green"] },
644 ],
645 shipping_profile_id: shippingProfile.id,
646 variants: [
647 {
648 title: "Test variant",
649 manage_inventory: true,
650 inventory_items: [
651 {
652 inventory_item_id: inventoryItem.id,
653 required_quantity: 1,
654 },
655 ],
656 prices: [
657 {
658 currency_code: "usd",
659 amount: 100,
660 },
661 {
662 currency_code: "dkk",
663 amount: 100,
664 },
665 ],
666 options: {
667 size: "large",
668 color: "green",
669 },
670 },
671 ],
672 },
673 adminHeaders
674 )
675 ).data.product
676
677 await dbUtils.snapshot()
678 })
679
680 it("should get shipping options for a cart with insufficient inventory flag set correctly", async () => {
681 const cart = (
682 await api.post(
683 `/store/carts`,
684 {
685 region_id: region.id,
686 sales_channel_id: salesChannel.id,
687 currency_code: "usd",
688 email: "test@admin.com",
689 items: [
690 {
691 variant_id: product.variants[0].id,
692 quantity: 8,
693 },
694 ],
695 },
696 storeHeaders
697 )
698 ).data.cart
699
700 const resp = await api.get(
701 `/store/shipping-options?cart_id=${cart.id}`,
702 storeHeaders
703 )
704
705 const shippingOptions = resp.data.shipping_options
706
707 expect(shippingOptions.length).toBe(3)
708 expect(shippingOptions).toEqual(
709 expect.arrayContaining([
710 expect.objectContaining({
711 id: shippingOption1.id,
712 name: "Pickup option 1",
713 amount: 0,
714 insufficient_inventory: false, // sufficient inventory at location
715 }),
716 expect.objectContaining({
717 id: shippingOption2.id,
718 name: "Pickup option 2",
719 amount: 0,
720 insufficient_inventory: true, // inventory item is at location 2 but not enough quantity
721 }),
722 expect.objectContaining({
723 id: shippingOption3.id,
724 name: "Pickup option 3",
725 amount: 0,
726 insufficient_inventory: true, // inventory item is not at location 3
727 }),
728 ])
729 )
730 })
731 })
732 },
733 })