Raw
1 import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
2 import { RuleOperator } from "@medusajs/utils"
3 import {
4 adminHeaders,
5 createAdminUser,
6 } from "../../../../helpers/create-admin-user"
7
8 jest.setTimeout(50000)
9
10 // BREAKING: Shipping setup has significantly changed from v1, exact migration needs more investigation
11 medusaIntegrationTestRunner({
12 testSuite: ({ dbConnection, getContainer, api, dbUtils }) => {
13 describe("Admin: Shipping Option API", () => {
14 let shippingProfile
15 let fulfillmentSet
16 let region
17 let appContainer
18 let location
19 let location2
20 let type
21
22 const shippingOptionRule = {
23 operator: RuleOperator.EQ,
24 attribute: "old_attr",
25 value: "old value",
26 }
27
28 beforeAll(async () => {
29 appContainer = getContainer()
30
31 await createAdminUser(dbConnection, adminHeaders, appContainer)
32
33 shippingProfile = (
34 await api.post(
35 `/admin/shipping-profiles`,
36 {
37 name: "Test",
38 type: "default",
39 },
40 adminHeaders
41 )
42 ).data.shipping_profile
43
44 location = (
45 await api.post(
46 `/admin/stock-locations`,
47 { name: "Test location" },
48 adminHeaders
49 )
50 ).data.stock_location
51
52 location = (
53 await api.post(
54 `/admin/stock-locations/${location.id}/fulfillment-sets?fields=*fulfillment_sets`,
55 { name: "Test", type: "test-type" },
56 adminHeaders
57 )
58 ).data.stock_location
59
60 location2 = (
61 await api.post(
62 `/admin/stock-locations`,
63 { name: "Test location 2" },
64 adminHeaders
65 )
66 ).data.stock_location
67
68 await api.post(
69 `/admin/stock-locations/${location.id}/fulfillment-providers`,
70 { add: ["manual_test-provider"] },
71 adminHeaders
72 )
73
74 fulfillmentSet = (
75 await api.post(
76 `/admin/fulfillment-sets/${location.fulfillment_sets[0].id}/service-zones`,
77 {
78 name: "Test",
79 geo_zones: [{ type: "country", country_code: "us" }],
80 },
81 adminHeaders
82 )
83 ).data.fulfillment_set
84
85 region = (
86 await api.post(
87 `/admin/regions`,
88 {
89 name: "Test region",
90 countries: ["FR"],
91 currency_code: "eur",
92 },
93 adminHeaders
94 )
95 ).data.region
96
97 type = (
98 await api.post(
99 `/admin/shipping-option-types`,
100 {
101 label: "Test",
102 code: "test",
103 },
104 adminHeaders
105 )
106 ).data.shipping_option_type
107
108 await dbUtils.snapshot()
109 })
110
111 describe("GET /admin/shipping-options", () => {
112 it("should filters options by stock_location_id", async () => {
113 const shippingOptionPayload = {
114 name: "Test shipping option",
115 service_zone_id: fulfillmentSet.service_zones[0].id,
116 shipping_profile_id: shippingProfile.id,
117 provider_id: "manual_test-provider",
118 price_type: "flat",
119 type: {
120 label: "Test type",
121 description: "Test description",
122 code: "test-code",
123 },
124 prices: [{ currency_code: "usd", amount: 1000 }],
125 }
126
127 await api.post(
128 `/admin/shipping-options`,
129 shippingOptionPayload,
130 adminHeaders
131 )
132
133 const shippingOptions = await api.get(
134 `/admin/shipping-options?stock_location_id=${location.id}`,
135 adminHeaders
136 )
137
138 expect(shippingOptions.data.shipping_options).toHaveLength(1)
139
140 const shippingOptions2 = await api.get(
141 `/admin/shipping-options?stock_location_id=${location2.id}`,
142 adminHeaders
143 )
144
145 expect(shippingOptions2.data.shipping_options).toHaveLength(0)
146 })
147 })
148
149 describe("GET /admin/shipping-options/:id", () => {
150 it("should filters options by stock_location_id", async () => {
151 const shippingOptionPayload = {
152 name: "Test shipping option",
153 service_zone_id: fulfillmentSet.service_zones[0].id,
154 shipping_profile_id: shippingProfile.id,
155 provider_id: "manual_test-provider",
156 price_type: "flat",
157 type: {
158 label: "Test type",
159 description: "Test description",
160 code: "test-code",
161 },
162 prices: [
163 { currency_code: "usd", amount: 1000 },
164 {
165 currency_code: "usd",
166 amount: 500,
167 rules: [
168 {
169 attribute: "item_total",
170 operator: "gte",
171 value: 100,
172 },
173 {
174 attribute: "item_total",
175 operator: "lte",
176 value: 200,
177 },
178 ],
179 },
180 ],
181 }
182
183 const {
184 data: { shipping_option: shippingOption },
185 } = await api.post(
186 `/admin/shipping-options`,
187 shippingOptionPayload,
188 adminHeaders
189 )
190
191 const shippingOptionRes = await api.get(
192 `/admin/shipping-options/${shippingOption.id}`,
193 adminHeaders
194 )
195
196 expect(shippingOptionRes.data.shipping_option).toEqual({
197 id: expect.any(String),
198 name: "Test shipping option",
199 price_type: "flat",
200 prices: expect.arrayContaining([
201 expect.objectContaining({
202 id: expect.any(String),
203 amount: 1000,
204 currency_code: "usd",
205 max_quantity: null,
206 min_quantity: null,
207 price_list: null,
208 price_list_id: null,
209 price_set_id: expect.any(String),
210 raw_amount: {
211 precision: 20,
212 value: "1000",
213 },
214 rules_count: 0,
215 title: null,
216 created_at: expect.any(String),
217 updated_at: expect.any(String),
218 deleted_at: null,
219 price_rules: [],
220 }),
221 expect.objectContaining({
222 id: expect.any(String),
223 amount: 500,
224 currency_code: "usd",
225 max_quantity: null,
226 min_quantity: null,
227 price_list: null,
228 price_list_id: null,
229 price_set_id: expect.any(String),
230 raw_amount: {
231 precision: 20,
232 value: "500",
233 },
234 rules_count: 2,
235 price_rules: expect.arrayContaining([
236 expect.objectContaining({
237 attribute: "item_total",
238 operator: "gte",
239 value: "100",
240 }),
241 expect.objectContaining({
242 attribute: "item_total",
243 operator: "lte",
244 value: "200",
245 }),
246 ]),
247 title: null,
248 created_at: expect.any(String),
249 updated_at: expect.any(String),
250 deleted_at: null,
251 }),
252 ]),
253 provider_id: "manual_test-provider",
254 provider: expect.objectContaining({
255 id: "manual_test-provider",
256 is_enabled: true,
257 }),
258 rules: [],
259 service_zone_id: expect.any(String),
260 service_zone: {
261 id: expect.any(String),
262 name: "Test",
263 fulfillment_set: {
264 id: expect.any(String),
265 },
266 fulfillment_set_id: expect.any(String),
267 metadata: null,
268 created_at: expect.any(String),
269 updated_at: expect.any(String),
270 deleted_at: null,
271 },
272 shipping_profile_id: expect.any(String),
273 shipping_profile: {
274 id: expect.any(String),
275 metadata: null,
276 name: "Test",
277 type: "default",
278 created_at: expect.any(String),
279 updated_at: expect.any(String),
280 deleted_at: null,
281 },
282 type: {
283 code: "test-code",
284 description: "Test description",
285 id: expect.any(String),
286 label: "Test type",
287 created_at: expect.any(String),
288 updated_at: expect.any(String),
289 deleted_at: null,
290 },
291 created_at: expect.any(String),
292 updated_at: expect.any(String),
293 data: null,
294 metadata: null,
295 })
296 })
297
298 it("should retrieve a shipping option with metadata successfully", async () => {
299 const shippingOptionPayload = {
300 name: "Test shipping option with metadata",
301 service_zone_id: fulfillmentSet.service_zones[0].id,
302 shipping_profile_id: shippingProfile.id,
303 provider_id: "manual_test-provider",
304 price_type: "flat",
305 type: {
306 label: "Test type",
307 description: "Test description",
308 code: "test-code",
309 },
310 prices: [{ currency_code: "usd", amount: 1000 }],
311 metadata: {
312 priority: "high",
313 tracking: "enabled",
314 carrier: "express",
315 specialHandling: true,
316 },
317 }
318
319 const {
320 data: { shipping_option: shippingOption },
321 } = await api.post(
322 `/admin/shipping-options`,
323 shippingOptionPayload,
324 adminHeaders
325 )
326
327 const shippingOptionRes = await api.get(
328 `/admin/shipping-options/${shippingOption.id}`,
329 adminHeaders
330 )
331
332 expect(shippingOptionRes.data.shipping_option).toEqual(
333 expect.objectContaining({
334 id: expect.any(String),
335 name: "Test shipping option with metadata",
336 metadata: {
337 priority: "high",
338 tracking: "enabled",
339 carrier: "express",
340 specialHandling: true,
341 },
342 })
343 )
344 })
345 })
346
347 describe("POST /admin/shipping-options", () => {
348 it("should throw error when required params are missing", async () => {
349 const shippingOptionPayload = {
350 name: "Test shipping option",
351 }
352
353 let err = await api
354 .post(
355 `/admin/shipping-options`,
356 shippingOptionPayload,
357 adminHeaders
358 )
359 .catch((e) => e.response)
360
361 expect(err.status).toEqual(400)
362 expect(err.data).toEqual({
363 type: "invalid_data",
364 message: `Invalid request: Field 'service_zone_id' is required; Field 'shipping_profile_id' is required; Field 'price_type' is required`,
365 })
366 })
367
368 it("should create a shipping option successfully", async () => {
369 const shippingOptionPayload = {
370 name: "Test shipping option",
371 service_zone_id: fulfillmentSet.service_zones[0].id,
372 shipping_profile_id: shippingProfile.id,
373 provider_id: "manual_test-provider",
374 price_type: "flat",
375 type: {
376 label: "Test type",
377 description: "Test description",
378 code: "test-code",
379 },
380 prices: [
381 {
382 currency_code: "usd",
383 amount: 1000,
384 },
385 {
386 region_id: region.id,
387 amount: 1000,
388 },
389 {
390 region_id: region.id,
391 amount: 500,
392 rules: [
393 {
394 attribute: "item_total",
395 operator: "gt",
396 value: 200,
397 },
398 ],
399 },
400 ],
401 rules: [shippingOptionRule],
402 }
403
404 const response = await api.post(
405 `/admin/shipping-options`,
406 shippingOptionPayload,
407 adminHeaders
408 )
409
410 expect(response.status).toEqual(200)
411 expect(response.data.shipping_option).toEqual(
412 expect.objectContaining({
413 id: expect.any(String),
414 name: shippingOptionPayload.name,
415 provider: expect.objectContaining({
416 id: shippingOptionPayload.provider_id,
417 }),
418 price_type: shippingOptionPayload.price_type,
419 type: expect.objectContaining({
420 id: expect.any(String),
421 label: shippingOptionPayload.type.label,
422 description: shippingOptionPayload.type.description,
423 code: shippingOptionPayload.type.code,
424 }),
425 service_zone_id: fulfillmentSet.service_zones[0].id,
426 shipping_profile_id: shippingProfile.id,
427 prices: expect.arrayContaining([
428 expect.objectContaining({
429 id: expect.any(String),
430 currency_code: "usd",
431 amount: 1000,
432 }),
433 expect.objectContaining({
434 id: expect.any(String),
435 currency_code: "eur",
436 amount: 1000,
437 }),
438 expect.objectContaining({
439 id: expect.any(String),
440 currency_code: "eur",
441 amount: 500,
442 rules_count: 2,
443 price_rules: expect.arrayContaining([
444 expect.objectContaining({
445 attribute: "item_total",
446 operator: "gt",
447 value: "200",
448 }),
449 expect.objectContaining({
450 attribute: "region_id",
451 operator: "eq",
452 value: region.id,
453 }),
454 ]),
455 }),
456 ]),
457 rules: expect.arrayContaining([
458 expect.objectContaining({
459 id: expect.any(String),
460 operator: "eq",
461 attribute: "old_attr",
462 value: "old value",
463 }),
464 ]),
465 })
466 )
467 })
468
469 it("should create a shipping option successfully with the provided shipping option type", async () => {
470 const shippingOptionPayload = {
471 name: "Test shipping option",
472 service_zone_id: fulfillmentSet.service_zones[0].id,
473 shipping_profile_id: shippingProfile.id,
474 provider_id: "manual_test-provider",
475 price_type: "flat",
476 type_id: type.id,
477 prices: [
478 {
479 currency_code: "usd",
480 amount: 1000,
481 },
482 {
483 region_id: region.id,
484 amount: 1000,
485 },
486 {
487 region_id: region.id,
488 amount: 500,
489 rules: [
490 {
491 attribute: "item_total",
492 operator: "gt",
493 value: 200,
494 },
495 ],
496 },
497 ],
498 rules: [shippingOptionRule],
499 }
500
501 const response = await api.post(
502 `/admin/shipping-options`,
503 shippingOptionPayload,
504 adminHeaders
505 )
506
507 expect(response.status).toEqual(200)
508 expect(response.data.shipping_option).toEqual(
509 expect.objectContaining({
510 id: expect.any(String),
511 name: shippingOptionPayload.name,
512 provider: expect.objectContaining({
513 id: shippingOptionPayload.provider_id,
514 }),
515 price_type: shippingOptionPayload.price_type,
516 type: expect.objectContaining({
517 id: type.id,
518 label: type.label,
519 description: type.description,
520 code: type.code,
521 }),
522 service_zone_id: fulfillmentSet.service_zones[0].id,
523 shipping_profile_id: shippingProfile.id,
524 prices: expect.arrayContaining([
525 expect.objectContaining({
526 id: expect.any(String),
527 currency_code: "usd",
528 amount: 1000,
529 }),
530 expect.objectContaining({
531 id: expect.any(String),
532 currency_code: "eur",
533 amount: 1000,
534 }),
535 expect.objectContaining({
536 id: expect.any(String),
537 currency_code: "eur",
538 amount: 500,
539 rules_count: 2,
540 price_rules: expect.arrayContaining([
541 expect.objectContaining({
542 attribute: "item_total",
543 operator: "gt",
544 value: "200",
545 }),
546 expect.objectContaining({
547 attribute: "region_id",
548 operator: "eq",
549 value: region.id,
550 }),
551 ]),
552 }),
553 ]),
554 rules: expect.arrayContaining([
555 expect.objectContaining({
556 id: expect.any(String),
557 operator: "eq",
558 attribute: "old_attr",
559 value: "old value",
560 }),
561 ]),
562 })
563 )
564 })
565
566 it("should throw error when creating a price rule with a non white listed attribute", async () => {
567 const shippingOptionPayload = {
568 name: "Test shipping option",
569 service_zone_id: fulfillmentSet.service_zones[0].id,
570 shipping_profile_id: shippingProfile.id,
571 provider_id: "manual_test-provider",
572 price_type: "flat",
573 type: {
574 label: "Test type",
575 description: "Test description",
576 code: "test-code",
577 },
578 prices: [
579 {
580 currency_code: "usd",
581 amount: 500,
582 rules: [
583 {
584 attribute: "not_whitelisted",
585 operator: "gte",
586 value: 100,
587 },
588 ],
589 },
590 ],
591 }
592
593 const error = await api
594 .post(
595 `/admin/shipping-options`,
596 shippingOptionPayload,
597 adminHeaders
598 )
599 .catch((e) => e)
600
601 expect(error.response.status).toEqual(400)
602 })
603
604 it("should throw error when creating a price rule with a non white listed operator", async () => {
605 const shippingOptionPayload = {
606 name: "Test shipping option",
607 service_zone_id: fulfillmentSet.service_zones[0].id,
608 shipping_profile_id: shippingProfile.id,
609 provider_id: "manual_test-provider",
610 price_type: "flat",
611 type: {
612 label: "Test type",
613 description: "Test description",
614 code: "test-code",
615 },
616 prices: [
617 {
618 currency_code: "usd",
619 amount: 500,
620 rules: [
621 {
622 attribute: "item_total",
623 operator: "not_whitelisted",
624 value: 100,
625 },
626 ],
627 },
628 ],
629 }
630
631 const error = await api
632 .post(
633 `/admin/shipping-options`,
634 shippingOptionPayload,
635 adminHeaders
636 )
637 .catch((e) => e)
638
639 expect(error.response.status).toEqual(400)
640 })
641
642 it("should throw error when creating a price rule with a string value", async () => {
643 const shippingOptionPayload = {
644 name: "Test shipping option",
645 service_zone_id: fulfillmentSet.service_zones[0].id,
646 shipping_profile_id: shippingProfile.id,
647 provider_id: "manual_test-provider",
648 price_type: "flat",
649 type: {
650 label: "Test type",
651 description: "Test description",
652 code: "test-code",
653 },
654 prices: [
655 {
656 currency_code: "usd",
657 amount: 500,
658 rules: [
659 {
660 attribute: "item_total",
661 operator: "gt",
662 value: "string",
663 },
664 ],
665 },
666 ],
667 }
668
669 const error = await api
670 .post(
671 `/admin/shipping-options`,
672 shippingOptionPayload,
673 adminHeaders
674 )
675 .catch((e) => e)
676
677 expect(error.response.status).toEqual(400)
678 })
679
680 it("should throw error when provider does not exist on a location", async () => {
681 const shippingOptionPayload = {
682 name: "Test shipping option",
683 service_zone_id: fulfillmentSet.service_zones[0].id,
684 shipping_profile_id: shippingProfile.id,
685 provider_id: "does-not-exist",
686 price_type: "flat",
687 type: {
688 label: "Test type",
689 description: "Test description",
690 code: "test-code",
691 },
692 prices: [
693 {
694 currency_code: "usd",
695 amount: 1000,
696 },
697 ],
698 rules: [shippingOptionRule],
699 }
700
701 const error = await api
702 .post(
703 `/admin/shipping-options`,
704 shippingOptionPayload,
705 adminHeaders
706 )
707 .catch((e) => e)
708
709 expect(error.response.status).toEqual(400)
710 expect(error.response.data.message).toEqual(
711 "Providers (does-not-exist) are not enabled for the service location"
712 )
713 })
714
715 it("should throw error if both type and type_id are missing", async () => {
716 const shippingOptionPayload = {
717 name: "Test shipping option",
718 service_zone_id: fulfillmentSet.service_zones[0].id,
719 shipping_profile_id: shippingProfile.id,
720 provider_id: "manual_test-provider",
721 price_type: "flat",
722 prices: [
723 {
724 currency_code: "usd",
725 amount: 1000,
726 },
727 ],
728 rules: [shippingOptionRule],
729 }
730
731 const error = await api
732 .post(
733 `/admin/shipping-options`,
734 shippingOptionPayload,
735 adminHeaders
736 )
737 .catch((e) => e)
738
739 expect(error.response.status).toEqual(400)
740 expect(error.response.data.message).toEqual(
741 "Invalid request: Exactly one of 'type' or 'type_id' must be provided, but not both"
742 )
743 })
744
745 it("should throw error if both type and type_id are defined", async () => {
746 const shippingOptionPayload = {
747 name: "Test shipping option",
748 service_zone_id: fulfillmentSet.service_zones[0].id,
749 shipping_profile_id: shippingProfile.id,
750 provider_id: "manual_test-provider",
751 type: {
752 label: "Test type",
753 description: "Test description",
754 code: "test-code",
755 },
756 type_id: "test_type_id",
757 price_type: "flat",
758 prices: [
759 {
760 currency_code: "usd",
761 amount: 1000,
762 },
763 ],
764 rules: [shippingOptionRule],
765 }
766
767 const error = await api
768 .post(
769 `/admin/shipping-options`,
770 shippingOptionPayload,
771 adminHeaders
772 )
773 .catch((e) => e)
774
775 expect(error.response.status).toEqual(400)
776 expect(error.response.data.message).toEqual(
777 "Invalid request: Exactly one of 'type' or 'type_id' must be provided, but not both"
778 )
779 })
780 })
781
782 describe("POST /admin/shipping-options/:id", () => {
783 it("should update a shipping option successfully", async () => {
784 const shippingOptionPayload = {
785 name: "Test shipping option",
786 service_zone_id: fulfillmentSet.service_zones[0].id,
787 shipping_profile_id: shippingProfile.id,
788 provider_id: "manual_test-provider",
789 price_type: "flat",
790 type: {
791 label: "Test type",
792 description: "Test description",
793 code: "test-code",
794 },
795 prices: [
796 {
797 currency_code: "usd",
798 amount: 1000,
799 },
800 {
801 region_id: region.id,
802 amount: 1000,
803 },
804 ],
805 rules: [
806 {
807 operator: RuleOperator.EQ,
808 attribute: "old_attr",
809 value: "old value",
810 },
811 {
812 operator: RuleOperator.EQ,
813 attribute: "old_attr_2",
814 value: "true",
815 },
816 ],
817 }
818
819 const response = await api.post(
820 `/admin/shipping-options`,
821 shippingOptionPayload,
822 adminHeaders
823 )
824
825 const shippingOptionId = response.data.shipping_option.id
826
827 const eurPrice = response.data.shipping_option.prices.find(
828 (p) => p.currency_code === "eur"
829 )
830
831 const oldAttrRule = response.data.shipping_option.rules.find(
832 (r) => r.attribute === "old_attr"
833 )
834 const oldAttr2Rule = response.data.shipping_option.rules.find(
835 (r) => r.attribute === "old_attr_2"
836 )
837
838 const updateShippingOptionPayload = {
839 name: "Updated shipping option",
840 provider_id: "manual_test-provider",
841 price_type: "flat",
842 prices: [
843 {
844 currency_code: "dkk",
845 amount: 10,
846 },
847 {
848 id: eurPrice.id,
849 amount: 10000,
850 },
851 {
852 currency_code: "dkk",
853 amount: 5,
854 rules: [
855 {
856 attribute: "item_total",
857 operator: "gt",
858 value: 200,
859 },
860 ],
861 },
862 ],
863 rules: [
864 {
865 // Untouched
866 id: oldAttrRule.id,
867 operator: RuleOperator.EQ,
868 attribute: "old_attr",
869 value: "old value",
870 },
871 {
872 // Updated
873 id: oldAttr2Rule.id,
874 operator: RuleOperator.EQ,
875 attribute: "old_attr_2",
876 value: "false",
877 },
878 {
879 // Created
880 operator: RuleOperator.EQ,
881 attribute: "new_attr",
882 value: "true",
883 },
884 ],
885 }
886
887 const updateResponse = await api.post(
888 `/admin/shipping-options/${shippingOptionId}`,
889 updateShippingOptionPayload,
890 adminHeaders
891 )
892
893 expect(updateResponse.status).toEqual(200)
894 expect(updateResponse.data.shipping_option.prices).toHaveLength(3)
895 expect(updateResponse.data.shipping_option.rules).toHaveLength(3)
896 expect(updateResponse.data.shipping_option).toEqual(
897 expect.objectContaining({
898 id: expect.any(String),
899 name: updateShippingOptionPayload.name,
900 provider: expect.objectContaining({
901 id: shippingOptionPayload.provider_id,
902 }),
903 price_type: updateShippingOptionPayload.price_type,
904 type: expect.objectContaining({
905 id: expect.any(String),
906 label: shippingOptionPayload.type.label,
907 description: shippingOptionPayload.type.description,
908 code: shippingOptionPayload.type.code,
909 }),
910 service_zone_id: fulfillmentSet.service_zones[0].id,
911 shipping_profile_id: shippingProfile.id,
912 prices: expect.arrayContaining([
913 expect.objectContaining({
914 id: expect.any(String),
915 currency_code: "dkk",
916 rules_count: 0,
917 amount: 10,
918 }),
919 expect.objectContaining({
920 id: expect.any(String),
921 currency_code: "eur",
922 rules_count: 1,
923 amount: 10000,
924 }),
925 expect.objectContaining({
926 id: expect.any(String),
927 currency_code: "dkk",
928 rules_count: 1,
929 amount: 5,
930 price_rules: [
931 expect.objectContaining({
932 attribute: "item_total",
933 operator: "gt",
934 value: "200",
935 }),
936 ],
937 }),
938 ]),
939 rules: expect.arrayContaining([
940 expect.objectContaining({
941 id: expect.any(String),
942 operator: "eq",
943 attribute: "old_attr",
944 value: "old value",
945 }),
946 expect.objectContaining({
947 id: expect.any(String),
948 operator: "eq",
949 attribute: "old_attr_2",
950 value: "false",
951 }),
952 expect.objectContaining({
953 id: expect.any(String),
954 operator: "eq",
955 attribute: "new_attr",
956 value: "true",
957 }),
958 ]),
959 })
960 )
961 })
962
963 it("should update a shipping option with a provided shipping option type successfully", async () => {
964 const shippingOptionPayload = {
965 name: "Test shipping option",
966 service_zone_id: fulfillmentSet.service_zones[0].id,
967 shipping_profile_id: shippingProfile.id,
968 provider_id: "manual_test-provider",
969 price_type: "flat",
970 type: {
971 label: "Test type",
972 description: "Test description",
973 code: "test-code",
974 },
975 prices: [
976 {
977 currency_code: "usd",
978 amount: 1000,
979 },
980 {
981 region_id: region.id,
982 amount: 1000,
983 },
984 ],
985 rules: [
986 {
987 operator: RuleOperator.EQ,
988 attribute: "old_attr",
989 value: "old value",
990 },
991 {
992 operator: RuleOperator.EQ,
993 attribute: "old_attr_2",
994 value: "true",
995 },
996 ],
997 }
998
999 const response = await api.post(
1000 `/admin/shipping-options`,
1001 shippingOptionPayload,
1002 adminHeaders
1003 )
1004
1005 const shippingOptionId = response.data.shipping_option.id
1006
1007 const updateResponse = await api.post(
1008 `/admin/shipping-options/${shippingOptionId}`,
1009 {
1010 name: "Updated shipping option",
1011 type_id: type.id,
1012 },
1013 adminHeaders
1014 )
1015
1016 expect(updateResponse.status).toEqual(200)
1017 expect(updateResponse.data.shipping_option).toEqual(
1018 expect.objectContaining({
1019 id: expect.any(String),
1020 name: "Updated shipping option",
1021 type: expect.objectContaining({
1022 id: type.id,
1023 label: type.label,
1024 description: type.description,
1025 code: type.code,
1026 }),
1027 })
1028 )
1029 })
1030
1031 it("should update a shipping option without providing shipping option type successfully", async () => {
1032 const shippingOptionPayload = {
1033 name: "Test shipping option",
1034 service_zone_id: fulfillmentSet.service_zones[0].id,
1035 shipping_profile_id: shippingProfile.id,
1036 provider_id: "manual_test-provider",
1037 price_type: "flat",
1038 type: {
1039 label: "Test type",
1040 description: "Test description",
1041 code: "test-code",
1042 },
1043 prices: [
1044 {
1045 currency_code: "usd",
1046 amount: 1000,
1047 },
1048 {
1049 region_id: region.id,
1050 amount: 1000,
1051 },
1052 ],
1053 rules: [
1054 {
1055 operator: RuleOperator.EQ,
1056 attribute: "old_attr",
1057 value: "old value",
1058 },
1059 {
1060 operator: RuleOperator.EQ,
1061 attribute: "old_attr_2",
1062 value: "true",
1063 },
1064 ],
1065 }
1066
1067 const response = await api.post(
1068 `/admin/shipping-options`,
1069 shippingOptionPayload,
1070 adminHeaders
1071 )
1072
1073 const shippingOptionId = response.data.shipping_option.id
1074
1075 const updateResponse = await api.post(
1076 `/admin/shipping-options/${shippingOptionId}`,
1077 {
1078 name: "Updated shipping option",
1079 },
1080 adminHeaders
1081 )
1082
1083 expect(updateResponse.status).toEqual(200)
1084 expect(updateResponse.data.shipping_option).toEqual(
1085 expect.objectContaining({
1086 id: expect.any(String),
1087 name: "Updated shipping option",
1088 })
1089 )
1090 })
1091
1092 it("should throw an error when provider does not belong to service location", async () => {
1093 const shippingOptionPayload = {
1094 name: "Test shipping option",
1095 service_zone_id: fulfillmentSet.service_zones[0].id,
1096 shipping_profile_id: shippingProfile.id,
1097 provider_id: "manual_test-provider",
1098 price_type: "flat",
1099 type: {
1100 label: "Test type",
1101 description: "Test description",
1102 code: "test-code",
1103 },
1104 prices: [
1105 {
1106 currency_code: "usd",
1107 amount: 1000,
1108 },
1109 {
1110 region_id: region.id,
1111 amount: 1000,
1112 },
1113 ],
1114 rules: [shippingOptionRule],
1115 }
1116
1117 const response = await api.post(
1118 `/admin/shipping-options`,
1119 shippingOptionPayload,
1120 adminHeaders
1121 )
1122
1123 const shippingOptionId = response.data.shipping_option.id
1124
1125 const updateShippingOptionPayload = {
1126 provider_id: "another_test-provider",
1127 }
1128
1129 const error = await api
1130 .post(
1131 `/admin/shipping-options/${shippingOptionId}`,
1132 updateShippingOptionPayload,
1133 adminHeaders
1134 )
1135 .catch((e) => e)
1136
1137 expect(error.response.status).toEqual(400)
1138 expect(error.response.data.message).toEqual(
1139 "Providers (another_test-provider) are not enabled for the service location"
1140 )
1141 })
1142
1143 it("should throw an error when type and type_id are both defined", async () => {
1144 const shippingOptionPayload = {
1145 name: "Test shipping option",
1146 service_zone_id: fulfillmentSet.service_zones[0].id,
1147 shipping_profile_id: shippingProfile.id,
1148 provider_id: "manual_test-provider",
1149 price_type: "flat",
1150 type: {
1151 label: "Test type",
1152 description: "Test description",
1153 code: "test-code",
1154 },
1155 prices: [
1156 {
1157 currency_code: "usd",
1158 amount: 1000,
1159 },
1160 {
1161 region_id: region.id,
1162 amount: 1000,
1163 },
1164 ],
1165 rules: [shippingOptionRule],
1166 }
1167
1168 const response = await api.post(
1169 `/admin/shipping-options`,
1170 shippingOptionPayload,
1171 adminHeaders
1172 )
1173
1174 const shippingOptionId = response.data.shipping_option.id
1175
1176 const updateShippingOptionPayload = {
1177 type: {
1178 label: "Test type",
1179 description: "Test description",
1180 code: "test-code",
1181 },
1182 type_id: "test_type_id",
1183 }
1184
1185 const error = await api
1186 .post(
1187 `/admin/shipping-options/${shippingOptionId}`,
1188 updateShippingOptionPayload,
1189 adminHeaders
1190 )
1191 .catch((e) => e)
1192
1193 expect(error.response.status).toEqual(400)
1194 expect(error.response.data.message).toEqual(
1195 "Invalid request: Only one of 'type' or 'type_id' can be provided"
1196 )
1197 })
1198 })
1199
1200 describe("DELETE /admin/shipping-options/:id", () => {
1201 it("should delete a shipping option successfully", async () => {
1202 const shippingOptionPayload = {
1203 name: "Test shipping option",
1204 service_zone_id: fulfillmentSet.service_zones[0].id,
1205 shipping_profile_id: shippingProfile.id,
1206 provider_id: "manual_test-provider",
1207 price_type: "flat",
1208 type: {
1209 label: "Test type",
1210 description: "Test description",
1211 code: "test-code",
1212 },
1213 prices: [
1214 {
1215 currency_code: "usd",
1216 amount: 1000,
1217 },
1218 {
1219 region_id: region.id,
1220 amount: 1000,
1221 },
1222 ],
1223 rules: [shippingOptionRule],
1224 }
1225
1226 const response = await api.post(
1227 `/admin/shipping-options`,
1228 shippingOptionPayload,
1229 adminHeaders
1230 )
1231
1232 const shippingOptionId = response.data.shipping_option.id
1233
1234 await api.delete(
1235 `/admin/shipping-options/${shippingOptionId}`,
1236 adminHeaders
1237 )
1238
1239 const shippingOptions = await api.get(
1240 `/admin/shipping-options`,
1241 adminHeaders
1242 )
1243
1244 expect(shippingOptions.data.shipping_options).toHaveLength(0)
1245 })
1246 })
1247
1248 describe("POST /admin/shipping-options/:id/rules/batch", () => {
1249 it.skip("should throw error when required params are missing", async () => {
1250 const shippingOptionPayload = {
1251 name: "Test shipping option",
1252 service_zone_id: fulfillmentSet.service_zones[0].id,
1253 shipping_profile_id: shippingProfile.id,
1254 provider_id: "manual_test-provider",
1255 price_type: "flat",
1256 type: {
1257 label: "Test type",
1258 description: "Test description",
1259 code: "test-code",
1260 },
1261 prices: [
1262 {
1263 currency_code: "usd",
1264 amount: 1000,
1265 },
1266 {
1267 region_id: region.id,
1268 amount: 1000,
1269 },
1270 ],
1271 rules: [shippingOptionRule],
1272 }
1273
1274 const shippingOption = (
1275 await api.post(
1276 `/admin/shipping-options`,
1277 shippingOptionPayload,
1278 adminHeaders
1279 )
1280 ).data.shipping_option
1281
1282 const { response } = await api
1283 .post(
1284 `/admin/shipping-options/${shippingOption.id}/rules/batch`,
1285 {
1286 create: [{ operator: RuleOperator.EQ, value: "new_value" }],
1287 },
1288 adminHeaders
1289 )
1290 .catch((e) => e)
1291
1292 expect(response.status).toEqual(400)
1293 expect(response.data).toEqual({
1294 type: "invalid_data",
1295 message:
1296 "attribute must be a string, attribute should not be empty",
1297 })
1298 })
1299
1300 it("should throw error when shipping option does not exist", async () => {
1301 const { response } = await api
1302 .post(
1303 `/admin/shipping-options/does-not-exist/rules/batch`,
1304 {
1305 create: [
1306 { attribute: "new_attr", operator: "eq", value: "new value" },
1307 ],
1308 },
1309 adminHeaders
1310 )
1311 .catch((e) => e)
1312
1313 expect(response.status).toEqual(404)
1314 expect(response.data).toEqual({
1315 type: "not_found",
1316 message:
1317 "You tried to set relationship shipping_option_id: does-not-exist, but such entity does not exist",
1318 })
1319 })
1320
1321 it.skip("should add rules to a shipping option successfully", async () => {
1322 const shippingOptionPayload = {
1323 name: "Test shipping option",
1324 service_zone_id: fulfillmentSet.service_zones[0].id,
1325 shipping_profile_id: shippingProfile.id,
1326 provider_id: "manual_test-provider",
1327 price_type: "flat",
1328 type: {
1329 label: "Test type",
1330 description: "Test description",
1331 code: "test-code",
1332 },
1333 prices: [
1334 {
1335 currency_code: "usd",
1336 amount: 1000,
1337 },
1338 {
1339 region_id: region.id,
1340 amount: 1000,
1341 },
1342 ],
1343 rules: [shippingOptionRule],
1344 }
1345
1346 const shippingOption = (
1347 await api.post(
1348 `/admin/shipping-options`,
1349 shippingOptionPayload,
1350 adminHeaders
1351 )
1352 ).data.shipping_option
1353
1354 const response = await api.post(
1355 `/admin/shipping-options/${shippingOption.id}/rules/batch`,
1356 {
1357 create: [
1358 { operator: "eq", attribute: "new_attr", value: "new value" },
1359 ],
1360 },
1361 adminHeaders
1362 )
1363
1364 expect(response.status).toEqual(200)
1365
1366 const updatedShippingOption = (
1367 await api.get(
1368 `/admin/shipping-options/${shippingOption.id}`,
1369 adminHeaders
1370 )
1371 ).data.shipping_option
1372 expect(updatedShippingOption).toEqual(
1373 expect.objectContaining({
1374 id: shippingOption.id,
1375 rules: expect.arrayContaining([
1376 expect.objectContaining({
1377 id: expect.any(String),
1378 operator: "eq",
1379 attribute: "old_attr",
1380 value: "old value",
1381 shipping_option_id: shippingOption.id,
1382 }),
1383 expect.objectContaining({
1384 id: expect.any(String),
1385 operator: "eq",
1386 attribute: "new_attr",
1387 value: "new value",
1388 shipping_option_id: shippingOption.id,
1389 }),
1390 ]),
1391 })
1392 )
1393 })
1394 })
1395 })
1396 },
1397 })