Raw
1 import {
2 CreateShippingOptionDTO,
3 IFulfillmentModuleService,
4 UpdateShippingOptionDTO,
5 } from "@medusajs/framework/types"
6 import {
7 FulfillmentEvents,
8 GeoZoneType,
9 Modules,
10 } from "@medusajs/framework/utils"
11 import {
12 MockEventBusService,
13 moduleIntegrationTestRunner,
14 } from "@medusajs/test-utils"
15 import { FulfillmentProviderService } from "@services"
16 import { resolve } from "path"
17 import {
18 buildExpectedEventMessageShape,
19 generateCreateShippingOptionsData,
20 } from "../../__fixtures__"
21 import { FulfillmentProviderServiceFixtures } from "../../__fixtures__/providers"
22
23 jest.setTimeout(1000000)
24
25 const moduleOptions = {
26 providers: [
27 {
28 resolve: resolve(
29 process.cwd() +
30 "/integration-tests/__fixtures__/providers/default-provider"
31 ),
32 id: "test-provider",
33 },
34 ],
35 }
36
37 const providerId = FulfillmentProviderService.getRegistrationIdentifier(
38 FulfillmentProviderServiceFixtures,
39 "test-provider"
40 )
41
42 moduleIntegrationTestRunner<IFulfillmentModuleService>({
43 moduleName: Modules.FULFILLMENT,
44 moduleOptions,
45 testSuite: ({ service }) => {
46 let eventBusEmitSpy
47
48 beforeEach(() => {
49 eventBusEmitSpy = jest.spyOn(MockEventBusService.prototype, "emit")
50 })
51
52 afterEach(() => {
53 jest.clearAllMocks()
54 })
55
56 describe("Fulfillment Module Service", () => {
57 describe("read", () => {
58 it("should list shipping options with a filter", async function () {
59 const fulfillmentSet = await service.createFulfillmentSets({
60 name: "test",
61 type: "test-type",
62 service_zones: [
63 {
64 name: "test",
65 },
66 ],
67 })
68
69 const shippingProfile = await service.createShippingProfiles({
70 name: "test",
71 type: "default",
72 })
73
74 const [shippingOption1] = await service.createShippingOptions([
75 generateCreateShippingOptionsData({
76 service_zone_id: fulfillmentSet.service_zones[0].id,
77 shipping_profile_id: shippingProfile.id,
78 provider_id: providerId,
79 rules: [
80 {
81 attribute: "test-attribute",
82 operator: "in",
83 value: ["test"],
84 },
85 ],
86 }),
87 generateCreateShippingOptionsData({
88 service_zone_id: fulfillmentSet.service_zones[0].id,
89 shipping_profile_id: shippingProfile.id,
90 provider_id: providerId,
91 rules: [
92 {
93 attribute: "test-attribute",
94 operator: "eq",
95 value: "test",
96 },
97 {
98 attribute: "test-attribute2.options",
99 operator: "in",
100 value: ["test", "test2"],
101 },
102 ],
103 }),
104 ])
105
106 const listedOptions = await service.listShippingOptions({
107 name: shippingOption1.name,
108 })
109
110 expect(listedOptions).toHaveLength(1)
111 expect(listedOptions[0].id).toEqual(shippingOption1.id)
112 })
113
114 it("should list shipping options with a context", async function () {
115 const fulfillmentSet = await service.createFulfillmentSets({
116 name: "test",
117 type: "test-type",
118 service_zones: [
119 {
120 name: "test",
121 },
122 ],
123 })
124
125 const shippingProfile = await service.createShippingProfiles({
126 name: "test",
127 type: "default",
128 })
129
130 const [shippingOption1, , shippingOption3] =
131 await service.createShippingOptions([
132 generateCreateShippingOptionsData({
133 service_zone_id: fulfillmentSet.service_zones[0].id,
134 shipping_profile_id: shippingProfile.id,
135 provider_id: providerId,
136 rules: [
137 {
138 attribute: "test-attribute",
139 operator: "in",
140 value: ["true"],
141 },
142 ],
143 }),
144 generateCreateShippingOptionsData({
145 service_zone_id: fulfillmentSet.service_zones[0].id,
146 shipping_profile_id: shippingProfile.id,
147 provider_id: providerId,
148 rules: [
149 {
150 attribute: "test-attribute",
151 operator: "in",
152 value: ["test-test"],
153 },
154 ],
155 }),
156 generateCreateShippingOptionsData({
157 service_zone_id: fulfillmentSet.service_zones[0].id,
158 shipping_profile_id: shippingProfile.id,
159 provider_id: providerId,
160 rules: [
161 {
162 attribute: "test-attribute",
163 operator: "eq",
164 value: "true",
165 },
166 {
167 attribute: "test-attribute2.options",
168 operator: "in",
169 value: ["test", "test2"],
170 },
171 ],
172 }),
173 ])
174
175 let listedOptions = await service.listShippingOptionsForContext({
176 context: {
177 "test-attribute": "true",
178 "test-attribute2": {
179 options: "test2",
180 },
181 },
182 })
183
184 expect(listedOptions).toHaveLength(2)
185 expect(listedOptions).toEqual(
186 expect.arrayContaining([
187 expect.objectContaining({ id: shippingOption1.id }),
188 expect.objectContaining({ id: shippingOption3.id }),
189 ])
190 )
191
192 listedOptions = await service.listShippingOptionsForContext({
193 service_zone: {
194 fulfillment_set: {
195 id: { $ne: fulfillmentSet.id },
196 },
197 },
198 context: {
199 "test-attribute": "test",
200 "test-attribute2": {
201 options: "test2",
202 },
203 },
204 })
205
206 expect(listedOptions).toHaveLength(0)
207
208 listedOptions = await service.listShippingOptionsForContext({
209 service_zone: {
210 fulfillment_set: {
211 type: "non-existing-type",
212 },
213 },
214 context: {
215 "test-attribute": "test",
216 "test-attribute2": {
217 options: "test2",
218 },
219 },
220 })
221
222 expect(listedOptions).toHaveLength(0)
223 })
224
225 it(`should list the shipping options for a context with a specific address`, async function () {
226 const fulfillmentSet = await service.createFulfillmentSets({
227 name: "test",
228 type: "test-type",
229 service_zones: [
230 {
231 name: "test",
232 geo_zones: [
233 {
234 type: GeoZoneType.COUNTRY,
235 country_code: "fr",
236 },
237 {
238 type: GeoZoneType.ZIP,
239 country_code: "fr",
240 province_code: "rhone",
241 city: "paris",
242 postal_expression: "75006",
243 },
244 ],
245 },
246 ],
247 })
248
249 const shippingProfile = await service.createShippingProfiles({
250 name: "test",
251 type: "default",
252 })
253
254 const [shippingOption1, , shippingOption3] =
255 await service.createShippingOptions([
256 generateCreateShippingOptionsData({
257 service_zone_id: fulfillmentSet.service_zones[0].id,
258 shipping_profile_id: shippingProfile.id,
259 provider_id: providerId,
260 rules: [
261 {
262 attribute: "test-attribute",
263 operator: "in",
264 value: ["test"],
265 },
266 ],
267 }),
268 generateCreateShippingOptionsData({
269 service_zone_id: fulfillmentSet.service_zones[0].id,
270 shipping_profile_id: shippingProfile.id,
271 provider_id: providerId,
272 rules: [
273 {
274 attribute: "test-attribute",
275 operator: "in",
276 value: ["test-test"],
277 },
278 ],
279 }),
280 generateCreateShippingOptionsData({
281 service_zone_id: fulfillmentSet.service_zones[0].id,
282 shipping_profile_id: shippingProfile.id,
283 provider_id: providerId,
284 rules: [
285 {
286 attribute: "test-attribute",
287 operator: "eq",
288 value: "test",
289 },
290 {
291 attribute: "test-attribute2.options",
292 operator: "in",
293 value: ["test", "test2"],
294 },
295 ],
296 }),
297 ])
298
299 let shippingOptions = await service.listShippingOptionsForContext({
300 address: {
301 country_code: "fr",
302 province_code: "rhone",
303 city: "paris",
304 postal_expression: "75006",
305 },
306 })
307
308 expect(shippingOptions).toHaveLength(3)
309
310 shippingOptions = await service.listShippingOptionsForContext({
311 address: {
312 country_code: "fr",
313 province_code: "rhone",
314 city: "paris",
315 },
316 })
317
318 expect(shippingOptions).toHaveLength(3)
319
320 shippingOptions = await service.listShippingOptionsForContext({
321 address: {
322 country_code: "fr",
323 province_code: "rhone",
324 },
325 })
326
327 expect(shippingOptions).toHaveLength(3)
328
329 shippingOptions = await service.listShippingOptionsForContext({
330 address: {
331 country_code: "fr",
332 },
333 })
334
335 expect(shippingOptions).toHaveLength(3)
336
337 shippingOptions = await service.listShippingOptionsForContext({
338 address: {
339 country_code: "fr",
340 postal_expression: "75006",
341 },
342 })
343
344 expect(shippingOptions).toHaveLength(3)
345
346 shippingOptions = await service.listShippingOptionsForContext({
347 address: {
348 country_code: "us",
349 province_code: "rhone",
350 city: "paris",
351 postal_expression: "75001",
352 },
353 })
354
355 expect(shippingOptions).toHaveLength(0)
356
357 shippingOptions = await service.listShippingOptionsForContext({
358 address: {
359 country_code: "fr",
360 province_code: "rhone",
361 city: "paris",
362 postal_expression: "75006",
363 },
364 context: {
365 "test-attribute": "test",
366 "test-attribute2": {
367 options: "test2",
368 },
369 },
370 })
371
372 expect(shippingOptions).toHaveLength(2)
373 expect(shippingOptions).toEqual(
374 expect.arrayContaining([
375 expect.objectContaining({ id: shippingOption1.id }),
376 expect.objectContaining({ id: shippingOption3.id }),
377 ])
378 )
379 })
380 })
381
382 it("should validate if a shipping option is applicable to a context", async function () {
383 const fulfillmentSet = await service.createFulfillmentSets({
384 name: "test",
385 type: "test-type",
386 service_zones: [
387 {
388 name: "test",
389 },
390 ],
391 })
392
393 const shippingProfile = await service.createShippingProfiles({
394 name: "test",
395 type: "default",
396 })
397
398 const [shippingOption1, shippingOption2, shippingOption3] =
399 await service.createShippingOptions([
400 generateCreateShippingOptionsData({
401 service_zone_id: fulfillmentSet.service_zones[0].id,
402 shipping_profile_id: shippingProfile.id,
403 provider_id: providerId,
404 rules: [
405 {
406 attribute: "test-attribute",
407 operator: "in",
408 value: ["test"],
409 },
410 ],
411 }),
412 generateCreateShippingOptionsData({
413 service_zone_id: fulfillmentSet.service_zones[0].id,
414 shipping_profile_id: shippingProfile.id,
415 provider_id: providerId,
416 rules: [
417 {
418 attribute: "test-attribute",
419 operator: "in",
420 value: ["test-test"],
421 },
422 ],
423 }),
424 generateCreateShippingOptionsData({
425 service_zone_id: fulfillmentSet.service_zones[0].id,
426 shipping_profile_id: shippingProfile.id,
427 provider_id: providerId,
428 rules: [
429 {
430 attribute: "test-attribute",
431 operator: "eq",
432 value: "test",
433 },
434 {
435 attribute: "test-attribute2.options",
436 operator: "in",
437 value: ["test", "test2"],
438 },
439 ],
440 }),
441 ])
442
443 let listedOptions = await service.listShippingOptions()
444
445 expect(listedOptions).toHaveLength(3)
446
447 const context = {
448 "test-attribute": "test",
449 "test-attribute2": {
450 options: "test2",
451 },
452 }
453
454 const isShippingOption1Applicable =
455 await service.validateShippingOption(shippingOption1.id, context)
456 expect(isShippingOption1Applicable).toBeTruthy()
457
458 const isShippingOption2Applicable =
459 await service.validateShippingOption(shippingOption2.id, context)
460 expect(isShippingOption2Applicable).toBeFalsy()
461
462 const isShippingOption3Applicable =
463 await service.validateShippingOption(shippingOption3.id, context)
464 expect(isShippingOption3Applicable).toBeTruthy()
465 })
466
467 describe("mutations", () => {
468 describe("on create", () => {
469 it("should create a new shipping option", async function () {
470 const shippingProfile = await service.createShippingProfiles({
471 name: "test",
472 type: "default",
473 })
474 const fulfillmentSet = await service.createFulfillmentSets({
475 name: "test",
476 type: "test-type",
477 })
478 const serviceZone = await service.createServiceZones({
479 name: "test",
480 fulfillment_set_id: fulfillmentSet.id,
481 })
482
483 const createData: CreateShippingOptionDTO =
484 generateCreateShippingOptionsData({
485 service_zone_id: serviceZone.id,
486 shipping_profile_id: shippingProfile.id,
487 provider_id: providerId,
488 })
489
490 jest.clearAllMocks()
491
492 const createdShippingOption = await service.createShippingOptions(
493 createData
494 )
495
496 expect(createdShippingOption).toEqual(
497 expect.objectContaining({
498 id: expect.any(String),
499 name: createData.name,
500 price_type: createData.price_type,
501 service_zone_id: createData.service_zone_id,
502 shipping_profile_id: createData.shipping_profile_id,
503 provider_id: createData.provider_id,
504 shipping_option_type_id: expect.any(String),
505 type: expect.objectContaining({
506 id: expect.any(String),
507 code: createData.type.code,
508 description: createData.type.description,
509 label: createData.type.label,
510 }),
511 data: createData.data,
512 rules: expect.arrayContaining([
513 expect.objectContaining({
514 id: expect.any(String),
515 attribute: createData.rules![0].attribute,
516 operator: createData.rules![0].operator,
517 value: createData.rules![0].value,
518 }),
519 ]),
520 })
521 )
522
523 expect(eventBusEmitSpy.mock.calls[0][0]).toHaveLength(3)
524 expect(eventBusEmitSpy).toHaveBeenCalledWith(
525 expect.arrayContaining([
526 buildExpectedEventMessageShape({
527 eventName: FulfillmentEvents.SHIPPING_OPTION_CREATED,
528 action: "created",
529 object: "shipping_option",
530 data: { id: createdShippingOption.id },
531 }),
532 buildExpectedEventMessageShape({
533 eventName: FulfillmentEvents.SHIPPING_OPTION_TYPE_CREATED,
534 action: "created",
535 object: "shipping_option_type",
536 data: { id: createdShippingOption.type.id },
537 }),
538 buildExpectedEventMessageShape({
539 eventName: FulfillmentEvents.SHIPPING_OPTION_RULE_CREATED,
540 action: "created",
541 object: "shipping_option_rule",
542 data: { id: createdShippingOption.rules[0].id },
543 }),
544 ]),
545 {
546 internal: true,
547 }
548 )
549 })
550
551 it("should create multiple new shipping options", async function () {
552 const shippingProfile = await service.createShippingProfiles({
553 name: "test",
554 type: "default",
555 })
556 const fulfillmentSet = await service.createFulfillmentSets({
557 name: "test",
558 type: "test-type",
559 })
560 const serviceZone = await service.createServiceZones({
561 name: "test",
562 fulfillment_set_id: fulfillmentSet.id,
563 })
564
565 const createData: CreateShippingOptionDTO[] = [
566 generateCreateShippingOptionsData({
567 service_zone_id: serviceZone.id,
568 shipping_profile_id: shippingProfile.id,
569 provider_id: providerId,
570 }),
571 generateCreateShippingOptionsData({
572 service_zone_id: serviceZone.id,
573 shipping_profile_id: shippingProfile.id,
574 provider_id: providerId,
575 }),
576 ]
577
578 jest.clearAllMocks()
579
580 const createdShippingOptions = await service.createShippingOptions(
581 createData
582 )
583
584 expect(createdShippingOptions).toHaveLength(2)
585 expect(eventBusEmitSpy.mock.calls[0][0]).toHaveLength(6)
586
587 let i = 0
588 for (const data_ of createData) {
589 expect(createdShippingOptions[i]).toEqual(
590 expect.objectContaining({
591 id: expect.any(String),
592 name: data_.name,
593 price_type: data_.price_type,
594 service_zone_id: data_.service_zone_id,
595 shipping_profile_id: data_.shipping_profile_id,
596 provider_id: data_.provider_id,
597 shipping_option_type_id: expect.any(String),
598 type: expect.objectContaining({
599 id: expect.any(String),
600 code: data_.type.code,
601 description: data_.type.description,
602 label: data_.type.label,
603 }),
604 data: data_.data,
605 rules: expect.arrayContaining([
606 expect.objectContaining({
607 id: expect.any(String),
608 attribute: data_.rules![0].attribute,
609 operator: data_.rules![0].operator,
610 value: data_.rules![0].value,
611 }),
612 ]),
613 })
614 )
615
616 expect(eventBusEmitSpy).toHaveBeenCalledWith(
617 expect.arrayContaining([
618 buildExpectedEventMessageShape({
619 eventName: FulfillmentEvents.SHIPPING_OPTION_CREATED,
620 action: "created",
621 object: "shipping_option",
622 data: {
623 id: createdShippingOptions[i].id,
624 },
625 }),
626 buildExpectedEventMessageShape({
627 eventName: FulfillmentEvents.SHIPPING_OPTION_TYPE_CREATED,
628 action: "created",
629 object: "shipping_option_type",
630 data: {
631 id: createdShippingOptions[i].type.id,
632 },
633 }),
634 buildExpectedEventMessageShape({
635 eventName: FulfillmentEvents.SHIPPING_OPTION_RULE_CREATED,
636 action: "created",
637 object: "shipping_option_rule",
638 data: {
639 id: createdShippingOptions[i].rules[0].id,
640 },
641 }),
642 ]),
643 {
644 internal: true,
645 }
646 )
647
648 ++i
649 }
650 })
651
652 it("should fail to create a new shipping option with invalid rules", async function () {
653 const shippingProfile = await service.createShippingProfiles({
654 name: "test",
655 type: "default",
656 })
657 const fulfillmentSet = await service.createFulfillmentSets({
658 name: "test",
659 type: "test-type",
660 })
661 const serviceZone = await service.createServiceZones({
662 name: "test",
663 fulfillment_set_id: fulfillmentSet.id,
664 })
665
666 const createData: CreateShippingOptionDTO =
667 generateCreateShippingOptionsData({
668 service_zone_id: serviceZone.id,
669 shipping_profile_id: shippingProfile.id,
670 provider_id: providerId,
671 rules: [
672 {
673 attribute: "test-attribute",
674 operator: "invalid" as any,
675 value: "test-value",
676 },
677 ],
678 })
679
680 const err = await service
681 .createShippingOptions(createData)
682 .catch((e) => e)
683
684 expect(err).toBeDefined()
685 expect(err.message).toBe(
686 "Rule operator invalid is not supported. Must be one of in, eq, ne, gt, gte, lt, lte, nin"
687 )
688 })
689 })
690
691 describe("on update", () => {
692 it("should update a shipping option", async () => {
693 const fulfillmentSet = await service.createFulfillmentSets({
694 name: "test",
695 type: "test-type",
696 })
697 const serviceZone = await service.createServiceZones({
698 name: "test",
699 fulfillment_set_id: fulfillmentSet.id,
700 })
701 const shippingProfile = await service.createShippingProfiles({
702 name: "test",
703 type: "default",
704 })
705
706 const shippingOptionData = generateCreateShippingOptionsData({
707 service_zone_id: serviceZone.id,
708 shipping_profile_id: shippingProfile.id,
709 provider_id: providerId,
710 })
711
712 const shippingOption = await service.createShippingOptions(
713 shippingOptionData
714 )
715
716 const existingRule = shippingOption.rules[0]!
717
718 const updateData: UpdateShippingOptionDTO & {
719 type: {
720 code: string
721 description: string
722 label: string
723 }
724 rules: {
725 attribute: string
726 operator: string
727 value: string
728 }[]
729 } = {
730 id: shippingOption.id,
731 name: "updated-test",
732 price_type: "calculated",
733 service_zone_id: serviceZone.id,
734 shipping_profile_id: shippingProfile.id,
735 provider_id: providerId,
736 type: {
737 code: "updated-test",
738 description: "updated-test",
739 label: "updated-test",
740 },
741 data: {
742 amount: 2000,
743 },
744 rules: [
745 {
746 ...existingRule,
747 value: "false",
748 },
749 {
750 attribute: "new-test",
751 operator: "eq",
752 value: "new-test",
753 },
754 ],
755 }
756
757 jest.clearAllMocks()
758
759 const updatedShippingOption = await service.updateShippingOptions(
760 updateData.id!,
761 updateData
762 )
763
764 expect(updatedShippingOption).toEqual(
765 expect.objectContaining({
766 id: updateData.id,
767 name: updateData.name,
768 price_type: updateData.price_type,
769 service_zone_id: updateData.service_zone_id,
770 shipping_profile_id: updateData.shipping_profile_id,
771 provider_id: updateData.provider_id,
772 shipping_option_type_id: expect.any(String),
773 type: expect.objectContaining({
774 id: expect.any(String),
775 code: updateData.type.code,
776 description: updateData.type.description,
777 label: updateData.type.label,
778 }),
779 data: updateData.data,
780 rules: expect.arrayContaining([
781 expect.objectContaining({
782 id: existingRule.id,
783 value: "false",
784 }),
785 expect.objectContaining({
786 id: expect.any(String),
787 attribute: updateData.rules![1].attribute,
788 operator: updateData.rules![1].operator,
789 value: updateData.rules![1].value,
790 }),
791 ]),
792 })
793 )
794
795 const rules = await service.listShippingOptionRules()
796 expect(rules).toHaveLength(2)
797 expect(rules).toEqual(
798 expect.arrayContaining([
799 expect.objectContaining({
800 id: updatedShippingOption.rules[0].id,
801 }),
802 expect.objectContaining({
803 id: updatedShippingOption.rules[1].id,
804 }),
805 ])
806 )
807
808 const types = await service.listShippingOptionTypes()
809 expect(types).toHaveLength(2)
810 expect(types).toEqual(
811 expect.arrayContaining([
812 expect.objectContaining({
813 code: updateData.type.code,
814 description: updateData.type.description,
815 label: updateData.type.label,
816 }),
817 ])
818 )
819
820 expect(eventBusEmitSpy.mock.calls[0][0]).toHaveLength(4)
821 expect(eventBusEmitSpy).toHaveBeenCalledWith(
822 expect.arrayContaining([
823 buildExpectedEventMessageShape({
824 eventName: FulfillmentEvents.SHIPPING_OPTION_UPDATED,
825 action: "updated",
826 object: "shipping_option",
827 data: { id: updatedShippingOption.id },
828 }),
829 buildExpectedEventMessageShape({
830 eventName: FulfillmentEvents.SHIPPING_OPTION_TYPE_CREATED,
831 action: "created",
832 object: "shipping_option_type",
833 data: { id: updatedShippingOption.type.id },
834 }),
835 buildExpectedEventMessageShape({
836 eventName: FulfillmentEvents.SHIPPING_OPTION_RULE_CREATED,
837 action: "created",
838 object: "shipping_option_rule",
839 data: { id: updatedShippingOption.rules[1].id },
840 }),
841 buildExpectedEventMessageShape({
842 eventName: FulfillmentEvents.SHIPPING_OPTION_RULE_UPDATED,
843 action: "updated",
844 object: "shipping_option_rule",
845 data: { id: updatedShippingOption.rules[0].id },
846 }),
847 ]),
848 {
849 internal: true,
850 }
851 )
852 })
853
854 it("should update a shipping option without updating the rules or the type", async () => {
855 const fulfillmentSet = await service.createFulfillmentSets({
856 name: "test",
857 type: "test-type",
858 })
859 const serviceZone = await service.createServiceZones({
860 name: "test",
861 fulfillment_set_id: fulfillmentSet.id,
862 })
863 const shippingProfile = await service.createShippingProfiles({
864 name: "test",
865 type: "default",
866 })
867
868 const shippingOptionData = generateCreateShippingOptionsData({
869 service_zone_id: serviceZone.id,
870 shipping_profile_id: shippingProfile.id,
871 provider_id: providerId,
872 })
873
874 const shippingOption = await service.createShippingOptions(
875 shippingOptionData
876 )
877
878 const updateData: Partial<UpdateShippingOptionDTO> = {
879 id: shippingOption.id,
880 name: "updated-test",
881 price_type: "calculated",
882 service_zone_id: serviceZone.id,
883 shipping_profile_id: shippingProfile.id,
884 provider_id: providerId,
885 data: {
886 amount: 2000,
887 },
888 }
889
890 await service.updateShippingOptions(updateData.id!, updateData)
891
892 const updatedShippingOption = await service.retrieveShippingOption(
893 shippingOption.id,
894 {
895 relations: ["rules", "type"],
896 }
897 )
898
899 expect(updatedShippingOption).toEqual(
900 expect.objectContaining({
901 id: updateData.id,
902 name: updateData.name,
903 price_type: updateData.price_type,
904 service_zone_id: updateData.service_zone_id,
905 shipping_profile_id: updateData.shipping_profile_id,
906 provider_id: updateData.provider_id,
907 shipping_option_type_id: expect.any(String),
908 type: expect.objectContaining({
909 id: expect.any(String),
910 code: shippingOptionData.type.code,
911 description: shippingOptionData.type.description,
912 label: shippingOptionData.type.label,
913 }),
914 data: updateData.data,
915 rules: expect.arrayContaining([
916 expect.objectContaining({
917 id: expect.any(String),
918 attribute: shippingOptionData.rules[0].attribute,
919 operator: shippingOptionData.rules[0].operator,
920 value: shippingOptionData.rules[0].value,
921 }),
922 ]),
923 })
924 )
925
926 const rules = await service.listShippingOptionRules()
927 expect(rules).toHaveLength(1)
928 expect(rules[0]).toEqual(
929 expect.objectContaining({
930 id: updatedShippingOption.rules[0].id,
931 })
932 )
933
934 const types = await service.listShippingOptionTypes()
935 expect(types).toHaveLength(1)
936 expect(types[0]).toEqual(
937 expect.objectContaining({
938 code: shippingOptionData.type.code,
939 description: shippingOptionData.type.description,
940 label: shippingOptionData.type.label,
941 })
942 )
943 })
944
945 it("should update a collection of shipping options", async () => {
946 const fulfillmentSet = await service.createFulfillmentSets({
947 name: "test",
948 type: "test-type",
949 })
950 const serviceZone = await service.createServiceZones({
951 name: "test",
952 fulfillment_set_id: fulfillmentSet.id,
953 })
954 const shippingProfile = await service.createShippingProfiles({
955 name: "test",
956 type: "default",
957 })
958
959 const shippingOptionData = [
960 generateCreateShippingOptionsData({
961 service_zone_id: serviceZone.id,
962 shipping_profile_id: shippingProfile.id,
963 provider_id: providerId,
964 }),
965 generateCreateShippingOptionsData({
966 service_zone_id: serviceZone.id,
967 shipping_profile_id: shippingProfile.id,
968 provider_id: providerId,
969 }),
970 ]
971
972 const shippingOptions = await service.createShippingOptions(
973 shippingOptionData
974 )
975
976 const updateData = [
977 {
978 id: shippingOptions[0].id,
979 name: "updated-test",
980 price_type: "calculated",
981 service_zone_id: serviceZone.id,
982 shipping_profile_id: shippingProfile.id,
983 provider_id: providerId,
984 type: {
985 code: "updated-test",
986 description: "updated-test",
987 label: "updated-test",
988 },
989 data: {
990 amount: 2000,
991 },
992 rules: [
993 {
994 attribute: "new-test",
995 operator: "eq",
996 value: "new-test",
997 },
998 ],
999 },
1000 {
1001 id: shippingOptions[1].id,
1002 name: "updated-test",
1003 price_type: "calculated",
1004 service_zone_id: serviceZone.id,
1005 shipping_profile_id: shippingProfile.id,
1006 provider_id: providerId,
1007 type: {
1008 code: "updated-test",
1009 description: "updated-test",
1010 label: "updated-test",
1011 },
1012 data: {
1013 amount: 2000,
1014 },
1015 rules: [
1016 {
1017 attribute: "new-test",
1018 operator: "eq",
1019 value: "new-test",
1020 },
1021 ],
1022 },
1023 ]
1024
1025 const updatedShippingOption = await service.upsertShippingOptions(
1026 updateData
1027 )
1028
1029 for (const data_ of updateData) {
1030 const expectedShippingOption = updatedShippingOption.find(
1031 (shippingOption) => shippingOption.id === data_.id
1032 )
1033 expect(expectedShippingOption).toEqual(
1034 expect.objectContaining({
1035 id: data_.id,
1036 name: data_.name,
1037 price_type: data_.price_type,
1038 service_zone_id: data_.service_zone_id,
1039 shipping_profile_id: data_.shipping_profile_id,
1040 provider_id: data_.provider_id,
1041 shipping_option_type_id: expect.any(String),
1042 type: expect.objectContaining({
1043 id: expect.any(String),
1044 code: data_.type.code,
1045 description: data_.type.description,
1046 label: data_.type.label,
1047 }),
1048 data: data_.data,
1049 rules: expect.arrayContaining([
1050 expect.objectContaining({
1051 id: expect.any(String),
1052 attribute: data_.rules[0].attribute,
1053 operator: data_.rules[0].operator,
1054 value: data_.rules[0].value,
1055 }),
1056 ]),
1057 })
1058 )
1059 }
1060
1061 const rules = await service.listShippingOptionRules()
1062 expect(rules).toHaveLength(2)
1063 expect(rules).toEqual(
1064 expect.arrayContaining([
1065 expect.objectContaining({
1066 id: updatedShippingOption[0].rules[0].id,
1067 }),
1068 expect.objectContaining({
1069 id: updatedShippingOption[1].rules[0].id,
1070 }),
1071 ])
1072 )
1073
1074 const types = await service.listShippingOptionTypes()
1075 expect(types).toHaveLength(4)
1076 expect(types).toEqual(
1077 expect.arrayContaining([
1078 expect.objectContaining({
1079 code: updateData[0].type.code,
1080 description: updateData[0].type.description,
1081 label: updateData[0].type.label,
1082 }),
1083 expect.objectContaining({
1084 code: updateData[1].type.code,
1085 description: updateData[1].type.description,
1086 label: updateData[1].type.label,
1087 }),
1088 ])
1089 )
1090 })
1091
1092 it("should fail to update a non-existent shipping option", async () => {
1093 const fulfillmentSet = await service.createFulfillmentSets({
1094 name: "test",
1095 type: "test-type",
1096 })
1097 const serviceZone = await service.createServiceZones({
1098 name: "test",
1099 fulfillment_set_id: fulfillmentSet.id,
1100 })
1101 const shippingProfile = await service.createShippingProfiles({
1102 name: "test",
1103 type: "default",
1104 })
1105
1106 const shippingOptionData: UpdateShippingOptionDTO = {
1107 id: "sp_jdafwfleiwuonl",
1108 name: "test",
1109 price_type: "flat",
1110 service_zone_id: serviceZone.id,
1111 shipping_profile_id: shippingProfile.id,
1112 provider_id: providerId,
1113 type: {
1114 code: "test",
1115 description: "test",
1116 label: "test",
1117 },
1118 data: {
1119 amount: 1000,
1120 },
1121 rules: [
1122 {
1123 attribute: "test",
1124 operator: "eq",
1125 value: "test",
1126 },
1127 ],
1128 }
1129
1130 const err = await service
1131 .updateShippingOptions(shippingOptionData.id!, shippingOptionData)
1132 .catch((e) => e)
1133
1134 expect(err).toBeDefined()
1135 expect(err.message).toBe(
1136 `The following shipping options do not exist: ${shippingOptionData.id}`
1137 )
1138 })
1139
1140 it("should fail to update a shipping option when adding non existing rules", async () => {
1141 const fulfillmentSet = await service.createFulfillmentSets({
1142 name: "test",
1143 type: "test-type",
1144 })
1145 const serviceZone = await service.createServiceZones({
1146 name: "test",
1147 fulfillment_set_id: fulfillmentSet.id,
1148 })
1149 const shippingProfile = await service.createShippingProfiles({
1150 name: "test",
1151 type: "default",
1152 })
1153
1154 const shippingOptionData = generateCreateShippingOptionsData({
1155 service_zone_id: serviceZone.id,
1156 shipping_profile_id: shippingProfile.id,
1157 provider_id: providerId,
1158 })
1159
1160 const shippingOption = await service.createShippingOptions(
1161 shippingOptionData
1162 )
1163
1164 const updateData = [
1165 {
1166 id: shippingOption.id,
1167 rules: [
1168 {
1169 id: "sp_jdafwfleiwuonl",
1170 },
1171 ],
1172 },
1173 ]
1174
1175 const err = await service
1176 .updateShippingOptions(updateData[0].id!, updateData[0])
1177 .catch((e) => e)
1178
1179 expect(err).toBeDefined()
1180 expect(err.message).toBe(
1181 `The following rules does not exists: ${updateData[0].rules[0].id} on shipping option ${shippingOption.id}`
1182 )
1183 })
1184
1185 it("should fail to update a shipping option when adding invalid rules", async () => {
1186 const fulfillmentSet = await service.createFulfillmentSets({
1187 name: "test",
1188 type: "test-type",
1189 })
1190 const serviceZone = await service.createServiceZones({
1191 name: "test",
1192 fulfillment_set_id: fulfillmentSet.id,
1193 })
1194 const shippingProfile = await service.createShippingProfiles({
1195 name: "test",
1196 type: "default",
1197 })
1198
1199 const shippingOptionData = generateCreateShippingOptionsData({
1200 service_zone_id: serviceZone.id,
1201 shipping_profile_id: shippingProfile.id,
1202 provider_id: providerId,
1203 })
1204
1205 const shippingOption = await service.createShippingOptions(
1206 shippingOptionData
1207 )
1208
1209 const updateData = [
1210 {
1211 id: shippingOption.id,
1212 rules: [
1213 {
1214 attribute: "test",
1215 operator: "invalid",
1216 value: "test",
1217 },
1218 ],
1219 },
1220 ]
1221
1222 const err = await service
1223 .updateShippingOptions(updateData[0].id!, updateData[0])
1224 .catch((e) => e)
1225
1226 expect(err).toBeDefined()
1227 expect(err.message).toBe(
1228 `Rule operator invalid is not supported. Must be one of in, eq, ne, gt, gte, lt, lte, nin`
1229 )
1230 })
1231 })
1232
1233 describe("on create shipping option rules", () => {
1234 it("should create a new rule", async () => {
1235 const shippingProfile = await service.createShippingProfiles({
1236 name: "test",
1237 type: "default",
1238 })
1239 const fulfillmentSet = await service.createFulfillmentSets({
1240 name: "test",
1241 type: "test-type",
1242 })
1243 const serviceZone = await service.createServiceZones({
1244 name: "test",
1245 fulfillment_set_id: fulfillmentSet.id,
1246 })
1247
1248 const shippingOption = await service.createShippingOptions(
1249 generateCreateShippingOptionsData({
1250 service_zone_id: serviceZone.id,
1251 shipping_profile_id: shippingProfile.id,
1252 provider_id: providerId,
1253 })
1254 )
1255
1256 const ruleData = {
1257 attribute: "test-attribute",
1258 operator: "eq",
1259 value: "test-value",
1260 shipping_option_id: shippingOption.id,
1261 }
1262
1263 jest.clearAllMocks()
1264
1265 const rule = await service.createShippingOptionRules(ruleData)
1266
1267 expect(rule).toEqual(
1268 expect.objectContaining({
1269 id: expect.any(String),
1270 attribute: ruleData.attribute,
1271 operator: ruleData.operator,
1272 value: ruleData.value,
1273 shipping_option_id: ruleData.shipping_option_id,
1274 })
1275 )
1276
1277 expect(eventBusEmitSpy.mock.calls[0][0]).toHaveLength(1)
1278 expect(eventBusEmitSpy).toHaveBeenCalledWith(
1279 [
1280 buildExpectedEventMessageShape({
1281 eventName: FulfillmentEvents.SHIPPING_OPTION_RULE_CREATED,
1282 action: "created",
1283 object: "shipping_option_rule",
1284 data: { id: rule.id },
1285 }),
1286 ],
1287 {
1288 internal: true,
1289 }
1290 )
1291
1292 const rules = await service.listShippingOptionRules()
1293 expect(rules).toHaveLength(2)
1294 expect(rules).toEqual(
1295 expect.arrayContaining([
1296 expect.objectContaining({
1297 id: rule.id,
1298 attribute: ruleData.attribute,
1299 operator: ruleData.operator,
1300 value: ruleData.value,
1301 shipping_option_id: shippingOption.id,
1302 }),
1303 expect.objectContaining({
1304 id: shippingOption.rules[0].id,
1305 attribute: shippingOption.rules[0].attribute,
1306 operator: shippingOption.rules[0].operator,
1307 value: shippingOption.rules[0].value,
1308 shipping_option_id: shippingOption.id,
1309 }),
1310 ])
1311 )
1312 })
1313 })
1314
1315 describe("buildGeoZoneConstraintsFromAddress", () => {
1316 it("should build correct constraints for full address with postal expression", async () => {
1317 const fulfillmentSet = await service.createFulfillmentSets({
1318 name: "test",
1319 type: "test-type",
1320 service_zones: [
1321 {
1322 name: "test",
1323 geo_zones: [
1324 {
1325 type: GeoZoneType.ZIP,
1326 country_code: "US",
1327 province_code: "CA",
1328 city: "Los Angeles",
1329 postal_expression: "90210",
1330 },
1331 {
1332 type: GeoZoneType.CITY,
1333 country_code: "US",
1334 province_code: "CA",
1335 city: "San Francisco",
1336 },
1337 {
1338 type: GeoZoneType.PROVINCE,
1339 country_code: "US",
1340 province_code: "NY",
1341 },
1342 {
1343 type: GeoZoneType.COUNTRY,
1344 country_code: "CA",
1345 },
1346 ],
1347 },
1348 ],
1349 })
1350
1351 const shippingProfile = await service.createShippingProfiles({
1352 name: "test",
1353 type: "default",
1354 })
1355
1356 await service.createShippingOptions([
1357 generateCreateShippingOptionsData({
1358 service_zone_id: fulfillmentSet.service_zones[0].id,
1359 shipping_profile_id: shippingProfile.id,
1360 provider_id: providerId,
1361 }),
1362 ])
1363
1364 // Test with full address including postal expression
1365 const shippingOptions = await service.listShippingOptionsForContext(
1366 {
1367 address: {
1368 country_code: "US",
1369 province_code: "CA",
1370 city: "Los Angeles",
1371 postal_expression: "90210",
1372 },
1373 }
1374 )
1375
1376 expect(shippingOptions).toHaveLength(1)
1377 })
1378
1379 it("should build correct constraints for address with city but no postal expression", async () => {
1380 const fulfillmentSet = await service.createFulfillmentSets({
1381 name: "test",
1382 type: "test-type",
1383 service_zones: [
1384 {
1385 name: "test",
1386 geo_zones: [
1387 {
1388 type: GeoZoneType.CITY,
1389 country_code: "US",
1390 province_code: "CA",
1391 city: "San Francisco",
1392 },
1393 ],
1394 },
1395 ],
1396 })
1397
1398 const shippingProfile = await service.createShippingProfiles({
1399 name: "test",
1400 type: "default",
1401 })
1402
1403 await service.createShippingOptions([
1404 generateCreateShippingOptionsData({
1405 service_zone_id: fulfillmentSet.service_zones[0].id,
1406 shipping_profile_id: shippingProfile.id,
1407 provider_id: providerId,
1408 }),
1409 ])
1410
1411 // Test with city but no postal expression
1412 const shippingOptions = await service.listShippingOptionsForContext(
1413 {
1414 address: {
1415 country_code: "US",
1416 province_code: "CA",
1417 city: "San Francisco",
1418 },
1419 }
1420 )
1421
1422 expect(shippingOptions).toHaveLength(1)
1423 })
1424
1425 it("should build correct constraints for address with province but no city", async () => {
1426 const fulfillmentSet = await service.createFulfillmentSets({
1427 name: "test",
1428 type: "test-type",
1429 service_zones: [
1430 {
1431 name: "test",
1432 geo_zones: [
1433 {
1434 type: GeoZoneType.PROVINCE,
1435 country_code: "US",
1436 province_code: "NY",
1437 },
1438 ],
1439 },
1440 ],
1441 })
1442
1443 const shippingProfile = await service.createShippingProfiles({
1444 name: "test",
1445 type: "default",
1446 })
1447
1448 await service.createShippingOptions([
1449 generateCreateShippingOptionsData({
1450 service_zone_id: fulfillmentSet.service_zones[0].id,
1451 shipping_profile_id: shippingProfile.id,
1452 provider_id: providerId,
1453 }),
1454 ])
1455
1456 // Test with province but no city
1457 const shippingOptions = await service.listShippingOptionsForContext(
1458 {
1459 address: {
1460 country_code: "US",
1461 province_code: "NY",
1462 },
1463 }
1464 )
1465
1466 expect(shippingOptions).toHaveLength(1)
1467 })
1468
1469 it("should build correct constraints for address with only country", async () => {
1470 const fulfillmentSet = await service.createFulfillmentSets({
1471 name: "test",
1472 type: "test-type",
1473 service_zones: [
1474 {
1475 name: "test",
1476 geo_zones: [
1477 {
1478 type: GeoZoneType.COUNTRY,
1479 country_code: "CA",
1480 },
1481 ],
1482 },
1483 ],
1484 })
1485
1486 const shippingProfile = await service.createShippingProfiles({
1487 name: "test",
1488 type: "default",
1489 })
1490
1491 await service.createShippingOptions([
1492 generateCreateShippingOptionsData({
1493 service_zone_id: fulfillmentSet.service_zones[0].id,
1494 shipping_profile_id: shippingProfile.id,
1495 provider_id: providerId,
1496 }),
1497 ])
1498
1499 // Test with only country
1500 const shippingOptions = await service.listShippingOptionsForContext(
1501 {
1502 address: {
1503 country_code: "CA",
1504 },
1505 }
1506 )
1507
1508 expect(shippingOptions).toHaveLength(1)
1509 })
1510
1511 it("should handle hierarchical geo zone matching correctly", async () => {
1512 const fulfillmentSet = await service.createFulfillmentSets({
1513 name: "test",
1514 type: "test-type",
1515 service_zones: [
1516 {
1517 name: "test",
1518 geo_zones: [
1519 {
1520 type: GeoZoneType.ZIP,
1521 country_code: "US",
1522 province_code: "CA",
1523 city: "Los Angeles",
1524 postal_expression: "90210",
1525 },
1526 {
1527 type: GeoZoneType.CITY,
1528 country_code: "US",
1529 province_code: "CA",
1530 city: "Los Angeles",
1531 },
1532 {
1533 type: GeoZoneType.PROVINCE,
1534 country_code: "US",
1535 province_code: "CA",
1536 },
1537 {
1538 type: GeoZoneType.COUNTRY,
1539 country_code: "US",
1540 },
1541 ],
1542 },
1543 ],
1544 })
1545
1546 const shippingProfile = await service.createShippingProfiles({
1547 name: "test",
1548 type: "default",
1549 })
1550
1551 await service.createShippingOptions([
1552 generateCreateShippingOptionsData({
1553 service_zone_id: fulfillmentSet.service_zones[0].id,
1554 shipping_profile_id: shippingProfile.id,
1555 provider_id: providerId,
1556 }),
1557 ])
1558
1559 // Test with full address - should match all levels
1560 let shippingOptions = await service.listShippingOptionsForContext({
1561 address: {
1562 country_code: "US",
1563 province_code: "CA",
1564 city: "Los Angeles",
1565 postal_expression: "90210",
1566 },
1567 })
1568 expect(shippingOptions).toHaveLength(1)
1569
1570 // Test with partial address - should still match broader zones
1571 shippingOptions = await service.listShippingOptionsForContext({
1572 address: {
1573 country_code: "US",
1574 province_code: "CA",
1575 },
1576 })
1577 expect(shippingOptions).toHaveLength(1)
1578
1579 // Test with only country - should match country zone
1580 shippingOptions = await service.listShippingOptionsForContext({
1581 address: {
1582 country_code: "US",
1583 },
1584 })
1585 expect(shippingOptions).toHaveLength(1)
1586 })
1587
1588 it("should not match zones when address doesn't satisfy requirements", async () => {
1589 const fulfillmentSet = await service.createFulfillmentSets({
1590 name: "test",
1591 type: "test-type",
1592 service_zones: [
1593 {
1594 name: "test",
1595 geo_zones: [
1596 {
1597 type: GeoZoneType.ZIP,
1598 country_code: "US",
1599 province_code: "CA",
1600 city: "Los Angeles",
1601 postal_expression: "90210",
1602 },
1603 ],
1604 },
1605 ],
1606 })
1607
1608 const shippingProfile = await service.createShippingProfiles({
1609 name: "test",
1610 type: "default",
1611 })
1612
1613 await service.createShippingOptions([
1614 generateCreateShippingOptionsData({
1615 service_zone_id: fulfillmentSet.service_zones[0].id,
1616 shipping_profile_id: shippingProfile.id,
1617 provider_id: providerId,
1618 }),
1619 ])
1620
1621 // Wrong postal code - should not match
1622 let shippingOptions = await service.listShippingOptionsForContext({
1623 address: {
1624 country_code: "US",
1625 province_code: "CA",
1626 city: "Los Angeles",
1627 postal_expression: "90211",
1628 },
1629 })
1630 expect(shippingOptions).toHaveLength(0)
1631
1632 // Wrong city - should not match
1633 shippingOptions = await service.listShippingOptionsForContext({
1634 address: {
1635 country_code: "US",
1636 province_code: "CA",
1637 city: "San Francisco",
1638 postal_expression: "90210",
1639 },
1640 })
1641 expect(shippingOptions).toHaveLength(0)
1642
1643 // Wrong province - should not match
1644 shippingOptions = await service.listShippingOptionsForContext({
1645 address: {
1646 country_code: "US",
1647 province_code: "NY",
1648 city: "Los Angeles",
1649 postal_expression: "90210",
1650 },
1651 })
1652 expect(shippingOptions).toHaveLength(0)
1653
1654 // Wrong country - should not match
1655 shippingOptions = await service.listShippingOptionsForContext({
1656 address: {
1657 country_code: "CA",
1658 province_code: "CA",
1659 city: "Los Angeles",
1660 postal_expression: "90210",
1661 },
1662 })
1663 expect(shippingOptions).toHaveLength(0)
1664 })
1665
1666 it("should handle partial address matching correctly", async () => {
1667 const fulfillmentSet = await service.createFulfillmentSets({
1668 name: "test",
1669 type: "test-type",
1670 service_zones: [
1671 {
1672 name: "test",
1673 geo_zones: [
1674 {
1675 type: GeoZoneType.PROVINCE,
1676 country_code: "US",
1677 province_code: "CA",
1678 },
1679 ],
1680 },
1681 ],
1682 })
1683
1684 const shippingProfile = await service.createShippingProfiles({
1685 name: "test",
1686 type: "default",
1687 })
1688
1689 await service.createShippingOptions([
1690 generateCreateShippingOptionsData({
1691 service_zone_id: fulfillmentSet.service_zones[0].id,
1692 shipping_profile_id: shippingProfile.id,
1693 provider_id: providerId,
1694 }),
1695 ])
1696
1697 // Full address with matching province should match
1698 let shippingOptions = await service.listShippingOptionsForContext({
1699 address: {
1700 country_code: "US",
1701 province_code: "CA",
1702 city: "Any City",
1703 postal_expression: "12345",
1704 },
1705 })
1706 expect(shippingOptions).toHaveLength(1)
1707
1708 // Minimal matching address should match
1709 shippingOptions = await service.listShippingOptionsForContext({
1710 address: {
1711 country_code: "US",
1712 province_code: "CA",
1713 },
1714 })
1715 expect(shippingOptions).toHaveLength(1)
1716
1717 // Address with only country should not match province-level zone
1718 shippingOptions = await service.listShippingOptionsForContext({
1719 address: {
1720 country_code: "US",
1721 },
1722 })
1723 expect(shippingOptions).toHaveLength(0)
1724 })
1725
1726 it("should handle empty or null address fields correctly", async () => {
1727 const fulfillmentSet = await service.createFulfillmentSets({
1728 name: "test",
1729 type: "test-type",
1730 service_zones: [
1731 {
1732 name: "test",
1733 geo_zones: [
1734 {
1735 type: GeoZoneType.COUNTRY,
1736 country_code: "US",
1737 },
1738 ],
1739 },
1740 ],
1741 })
1742
1743 const shippingProfile = await service.createShippingProfiles({
1744 name: "test",
1745 type: "default",
1746 })
1747
1748 await service.createShippingOptions([
1749 generateCreateShippingOptionsData({
1750 service_zone_id: fulfillmentSet.service_zones[0].id,
1751 shipping_profile_id: shippingProfile.id,
1752 provider_id: providerId,
1753 }),
1754 ])
1755
1756 // Address with undefined fields should still match if country matches
1757 const shippingOptions = await service.listShippingOptionsForContext(
1758 {
1759 address: {
1760 country_code: "US",
1761 province_code: undefined,
1762 city: undefined,
1763 postal_expression: undefined,
1764 },
1765 }
1766 )
1767 expect(shippingOptions).toHaveLength(1)
1768 })
1769
1770 it("should correctly match addresses across multiple service zones", async () => {
1771 const fulfillmentSet = await service.createFulfillmentSets({
1772 name: "test",
1773 type: "test-type",
1774 service_zones: [
1775 {
1776 name: "US Zones",
1777 geo_zones: [
1778 {
1779 type: GeoZoneType.ZIP,
1780 country_code: "US",
1781 province_code: "CA",
1782 city: "Los Angeles",
1783 postal_expression: "90210",
1784 },
1785 {
1786 type: GeoZoneType.CITY,
1787 country_code: "US",
1788 province_code: "NY",
1789 city: "New York",
1790 },
1791 ],
1792 },
1793 {
1794 name: "Europe Zones",
1795 geo_zones: [
1796 {
1797 type: GeoZoneType.COUNTRY,
1798 country_code: "FR",
1799 },
1800 {
1801 type: GeoZoneType.PROVINCE,
1802 country_code: "DE",
1803 province_code: "BY",
1804 },
1805 ],
1806 },
1807 {
1808 name: "Canada Zones",
1809 geo_zones: [
1810 {
1811 type: GeoZoneType.PROVINCE,
1812 country_code: "CA",
1813 province_code: "ON",
1814 },
1815 {
1816 type: GeoZoneType.CITY,
1817 country_code: "CA",
1818 province_code: "BC",
1819 city: "Vancouver",
1820 },
1821 ],
1822 },
1823 ],
1824 })
1825
1826 const shippingProfile = await service.createShippingProfiles({
1827 name: "test",
1828 type: "default",
1829 })
1830
1831 // Create shipping options for each zone
1832 const [usOption, europeOption, canadaOption] =
1833 await service.createShippingOptions([
1834 generateCreateShippingOptionsData({
1835 name: "US Shipping",
1836 service_zone_id: fulfillmentSet.service_zones[0].id,
1837 shipping_profile_id: shippingProfile.id,
1838 provider_id: providerId,
1839 }),
1840 generateCreateShippingOptionsData({
1841 name: "Europe Shipping",
1842 service_zone_id: fulfillmentSet.service_zones[1].id,
1843 shipping_profile_id: shippingProfile.id,
1844 provider_id: providerId,
1845 }),
1846 generateCreateShippingOptionsData({
1847 name: "Canada Shipping",
1848 service_zone_id: fulfillmentSet.service_zones[2].id,
1849 shipping_profile_id: shippingProfile.id,
1850 provider_id: providerId,
1851 }),
1852 ])
1853
1854 // Test US ZIP code - should only match US zone
1855 let shippingOptions = await service.listShippingOptionsForContext({
1856 address: {
1857 country_code: "US",
1858 province_code: "CA",
1859 city: "Los Angeles",
1860 postal_expression: "90210",
1861 },
1862 })
1863 expect(shippingOptions).toHaveLength(1)
1864 expect(shippingOptions[0].id).toBe(usOption.id)
1865
1866 // Test New York city - should only match US zone
1867 shippingOptions = await service.listShippingOptionsForContext({
1868 address: {
1869 country_code: "US",
1870 province_code: "NY",
1871 city: "New York",
1872 },
1873 })
1874 expect(shippingOptions).toHaveLength(1)
1875 expect(shippingOptions[0].id).toBe(usOption.id)
1876
1877 // Test France - should only match Europe zone
1878 shippingOptions = await service.listShippingOptionsForContext({
1879 address: {
1880 country_code: "FR",
1881 },
1882 })
1883 expect(shippingOptions).toHaveLength(1)
1884 expect(shippingOptions[0].id).toBe(europeOption.id)
1885
1886 // Test German province - should only match Europe zone
1887 shippingOptions = await service.listShippingOptionsForContext({
1888 address: {
1889 country_code: "DE",
1890 province_code: "BY",
1891 },
1892 })
1893 expect(shippingOptions).toHaveLength(1)
1894 expect(shippingOptions[0].id).toBe(europeOption.id)
1895
1896 // Test Canadian province - should only match Canada zone
1897 shippingOptions = await service.listShippingOptionsForContext({
1898 address: {
1899 country_code: "CA",
1900 province_code: "ON",
1901 },
1902 })
1903 expect(shippingOptions).toHaveLength(1)
1904 expect(shippingOptions[0].id).toBe(canadaOption.id)
1905
1906 // Test Vancouver - should only match Canada zone
1907 shippingOptions = await service.listShippingOptionsForContext({
1908 address: {
1909 country_code: "CA",
1910 province_code: "BC",
1911 city: "Vancouver",
1912 },
1913 })
1914 expect(shippingOptions).toHaveLength(1)
1915 expect(shippingOptions[0].id).toBe(canadaOption.id)
1916
1917 // Test non-matching address - should return no options
1918 shippingOptions = await service.listShippingOptionsForContext({
1919 address: {
1920 country_code: "JP",
1921 city: "Tokyo",
1922 },
1923 })
1924 expect(shippingOptions).toHaveLength(0)
1925 })
1926
1927 it("should handle overlapping zones across multiple service zones", async () => {
1928 const fulfillmentSet = await service.createFulfillmentSets({
1929 name: "test",
1930 type: "test-type",
1931 service_zones: [
1932 {
1933 name: "Broad US Zone",
1934 geo_zones: [
1935 {
1936 type: GeoZoneType.COUNTRY,
1937 country_code: "US",
1938 },
1939 ],
1940 },
1941 {
1942 name: "Specific California Zone",
1943 geo_zones: [
1944 {
1945 type: GeoZoneType.PROVINCE,
1946 country_code: "US",
1947 province_code: "CA",
1948 },
1949 ],
1950 },
1951 {
1952 name: "Specific LA Zone",
1953 geo_zones: [
1954 {
1955 type: GeoZoneType.ZIP,
1956 country_code: "US",
1957 province_code: "CA",
1958 city: "Los Angeles",
1959 postal_expression: "90210",
1960 },
1961 ],
1962 },
1963 ],
1964 })
1965
1966 const shippingProfile = await service.createShippingProfiles({
1967 name: "test",
1968 type: "default",
1969 })
1970
1971 // Create shipping options with different prices for each zone
1972 const [broadOption, californiaOption, laOption] =
1973 await service.createShippingOptions([
1974 generateCreateShippingOptionsData({
1975 name: "Standard US Shipping",
1976 service_zone_id: fulfillmentSet.service_zones[0].id,
1977 shipping_profile_id: shippingProfile.id,
1978 provider_id: providerId,
1979 }),
1980 generateCreateShippingOptionsData({
1981 name: "California Express",
1982 service_zone_id: fulfillmentSet.service_zones[1].id,
1983 shipping_profile_id: shippingProfile.id,
1984 provider_id: providerId,
1985 }),
1986 generateCreateShippingOptionsData({
1987 name: "LA Premium",
1988 service_zone_id: fulfillmentSet.service_zones[2].id,
1989 shipping_profile_id: shippingProfile.id,
1990 provider_id: providerId,
1991 }),
1992 ])
1993
1994 // Test LA ZIP code - should match all three zones
1995 let shippingOptions = await service.listShippingOptionsForContext({
1996 address: {
1997 country_code: "US",
1998 province_code: "CA",
1999 city: "Los Angeles",
2000 postal_expression: "90210",
2001 },
2002 })
2003 expect(shippingOptions).toHaveLength(3)
2004 const laIds = shippingOptions.map((opt) => opt.id)
2005 expect(laIds).toContain(broadOption.id)
2006 expect(laIds).toContain(californiaOption.id)
2007 expect(laIds).toContain(laOption.id)
2008
2009 // Test California (not LA) - should match broad US and California zones
2010 shippingOptions = await service.listShippingOptionsForContext({
2011 address: {
2012 country_code: "US",
2013 province_code: "CA",
2014 city: "San Francisco",
2015 },
2016 })
2017 expect(shippingOptions).toHaveLength(2)
2018 const caIds = shippingOptions.map((opt) => opt.id)
2019 expect(caIds).toContain(broadOption.id)
2020 expect(caIds).toContain(californiaOption.id)
2021 expect(caIds).not.toContain(laOption.id)
2022
2023 // Test US (not California) - should only match broad US zone
2024 shippingOptions = await service.listShippingOptionsForContext({
2025 address: {
2026 country_code: "US",
2027 province_code: "NY",
2028 },
2029 })
2030 expect(shippingOptions).toHaveLength(1)
2031 expect(shippingOptions[0].id).toBe(broadOption.id)
2032
2033 // Test non-US address - should match nothing
2034 shippingOptions = await service.listShippingOptionsForContext({
2035 address: {
2036 country_code: "CA",
2037 },
2038 })
2039 expect(shippingOptions).toHaveLength(0)
2040 })
2041
2042 it("should handle mixed granularity zones across service zones", async () => {
2043 const fulfillmentSet = await service.createFulfillmentSets({
2044 name: "test",
2045 type: "test-type",
2046 service_zones: [
2047 {
2048 name: "Mixed Zone 1",
2049 geo_zones: [
2050 {
2051 type: GeoZoneType.ZIP,
2052 country_code: "US",
2053 province_code: "CA",
2054 city: "Los Angeles",
2055 postal_expression: "90210",
2056 },
2057 {
2058 type: GeoZoneType.COUNTRY,
2059 country_code: "FR",
2060 },
2061 ],
2062 },
2063 {
2064 name: "Mixed Zone 2",
2065 geo_zones: [
2066 {
2067 type: GeoZoneType.CITY,
2068 country_code: "US",
2069 province_code: "NY",
2070 city: "New York",
2071 },
2072 {
2073 type: GeoZoneType.PROVINCE,
2074 country_code: "CA",
2075 province_code: "ON",
2076 },
2077 ],
2078 },
2079 ],
2080 })
2081
2082 const shippingProfile = await service.createShippingProfiles({
2083 name: "test",
2084 type: "default",
2085 })
2086
2087 const [option1, option2] = await service.createShippingOptions([
2088 generateCreateShippingOptionsData({
2089 name: "Mixed Option 1",
2090 service_zone_id: fulfillmentSet.service_zones[0].id,
2091 shipping_profile_id: shippingProfile.id,
2092 provider_id: providerId,
2093 }),
2094 generateCreateShippingOptionsData({
2095 name: "Mixed Option 2",
2096 service_zone_id: fulfillmentSet.service_zones[1].id,
2097 shipping_profile_id: shippingProfile.id,
2098 provider_id: providerId,
2099 }),
2100 ])
2101
2102 // Test LA ZIP - should match zone 1
2103 let shippingOptions = await service.listShippingOptionsForContext({
2104 address: {
2105 country_code: "US",
2106 province_code: "CA",
2107 city: "Los Angeles",
2108 postal_expression: "90210",
2109 },
2110 })
2111 expect(shippingOptions).toHaveLength(1)
2112 expect(shippingOptions[0].id).toBe(option1.id)
2113
2114 // Test France - should match zone 1
2115 shippingOptions = await service.listShippingOptionsForContext({
2116 address: {
2117 country_code: "FR",
2118 city: "Paris",
2119 },
2120 })
2121 expect(shippingOptions).toHaveLength(1)
2122 expect(shippingOptions[0].id).toBe(option1.id)
2123
2124 // Test New York - should match zone 2
2125 shippingOptions = await service.listShippingOptionsForContext({
2126 address: {
2127 country_code: "US",
2128 province_code: "NY",
2129 city: "New York",
2130 },
2131 })
2132 expect(shippingOptions).toHaveLength(1)
2133 expect(shippingOptions[0].id).toBe(option2.id)
2134
2135 // Test Ontario, Canada - should match zone 2
2136 shippingOptions = await service.listShippingOptionsForContext({
2137 address: {
2138 country_code: "CA",
2139 province_code: "ON",
2140 city: "Toronto",
2141 },
2142 })
2143 expect(shippingOptions).toHaveLength(1)
2144 expect(shippingOptions[0].id).toBe(option2.id)
2145
2146 // Test unmatched location
2147 shippingOptions = await service.listShippingOptionsForContext({
2148 address: {
2149 country_code: "UK",
2150 },
2151 })
2152 expect(shippingOptions).toHaveLength(0)
2153 })
2154 })
2155
2156 describe("on update shipping option rules", () => {
2157 it("should update a shipping option rule", async () => {
2158 const shippingProfile = await service.createShippingProfiles({
2159 name: "test",
2160 type: "default",
2161 })
2162 const fulfillmentSet = await service.createFulfillmentSets({
2163 name: "test",
2164 type: "test-type",
2165 })
2166 const serviceZone = await service.createServiceZones({
2167 name: "test",
2168 fulfillment_set_id: fulfillmentSet.id,
2169 })
2170
2171 const shippingOption = await service.createShippingOptions(
2172 generateCreateShippingOptionsData({
2173 service_zone_id: serviceZone.id,
2174 shipping_profile_id: shippingProfile.id,
2175 provider_id: providerId,
2176 })
2177 )
2178
2179 const updateData = {
2180 id: shippingOption.rules[0].id,
2181 attribute: "updated-test",
2182 operator: "eq",
2183 value: "updated-test",
2184 }
2185
2186 jest.clearAllMocks()
2187
2188 const updatedRule = await service.updateShippingOptionRules(
2189 updateData
2190 )
2191
2192 expect(updatedRule).toEqual(
2193 expect.objectContaining({
2194 id: updateData.id,
2195 attribute: updateData.attribute,
2196 operator: updateData.operator,
2197 value: updateData.value,
2198 })
2199 )
2200
2201 expect(eventBusEmitSpy).toHaveBeenCalledWith(
2202 [
2203 buildExpectedEventMessageShape({
2204 eventName: FulfillmentEvents.SHIPPING_OPTION_RULE_UPDATED,
2205 action: "updated",
2206 object: "shipping_option_rule",
2207 data: { id: updatedRule.id },
2208 }),
2209 ],
2210 {
2211 internal: true,
2212 }
2213 )
2214 })
2215
2216 it("should fail to update a non-existent shipping option rule", async () => {
2217 const updateData = {
2218 id: "sp_jdafwfleiwuonl",
2219 attribute: "updated-test",
2220 operator: "eq",
2221 value: "updated-test",
2222 }
2223
2224 const err = await service
2225 .updateShippingOptionRules(updateData)
2226 .catch((e) => e)
2227
2228 expect(err).toBeDefined()
2229 expect(err.message).toBe(
2230 `ShippingOptionRule with id "${updateData.id}" not found`
2231 )
2232 })
2233 })
2234 })
2235 })
2236 },
2237 })