1 import {
2 CreatePriceSetDTO,
3 IPricingModuleService,
4 } from "@medusajs/framework/types"
5 import {
6 CommonEvents,
7 composeMessage,
8 Modules,
9 PricingEvents,
10 } from "@medusajs/framework/utils"
11 import {
12 MockEventBusService,
13 moduleIntegrationTestRunner,
14 } from "@medusajs/test-utils"
15 import { seedPriceData } from "../../../__fixtures__/seed-price-data"
16
17 jest.setTimeout(30000)
18
19 moduleIntegrationTestRunner<IPricingModuleService>({
20 moduleName: Modules.PRICING,
21 testSuite: ({ MikroOrmWrapper, service }) => {
22 let eventBusEmitSpy
23
24 beforeEach(() => {
25 eventBusEmitSpy = jest.spyOn(MockEventBusService.prototype, "emit")
26 })
27
28 afterEach(() => {
29 jest.clearAllMocks()
30 })
31
32 describe("PricingModule Service - PriceSet", () => {
33 beforeEach(async () => {
34 const testManager = await MikroOrmWrapper.forkManager()
35 await seedPriceData(testManager)
36 })
37
38 describe("list", () => {
39 it("list priceSets", async () => {
40 const priceSetsResult = await service.listPriceSets()
41
42 expect(priceSetsResult).toEqual([
43 expect.objectContaining({
44 id: "price-set-1",
45 }),
46 expect.objectContaining({
47 id: "price-set-2",
48 }),
49 expect.objectContaining({
50 id: "price-set-3",
51 }),
52 ])
53 })
54
55 it("list priceSets by id", async () => {
56 const priceSetsResult = await service.listPriceSets({
57 id: ["price-set-1"],
58 })
59
60 expect(priceSetsResult).toEqual([
61 expect.objectContaining({
62 id: "price-set-1",
63 }),
64 ])
65 })
66
67 it("list priceSets with relations and selects", async () => {
68 const priceSetsResult = await service.listPriceSets(
69 {
70 id: ["price-set-1"],
71 },
72 {
73 select: ["id", "prices.id", "prices.amount"],
74 relations: ["prices"],
75 }
76 )
77
78 const serialized = JSON.parse(JSON.stringify(priceSetsResult))
79
80 expect(serialized).toEqual([
81 {
82 id: "price-set-1",
83 prices: [
84 expect.objectContaining({
85 id: "price-set-money-amount-USD",
86 amount: 500,
87 }),
88 ],
89 },
90 ])
91 })
92
93 it("list priceSets should return only prices from a price set (and not the ones from a price list)", async () => {
94 const [priceList] = await service.createPriceLists([
95 {
96 title: "test",
97 description: "test",
98 prices: [
99 {
100 amount: 400,
101 currency_code:"eur",
102 price_set_id: "price-set-1",
103 rules: {
104 region_id: "DE",
105 },
106 },
107 {
108 amount: 600,
109 currency_code:"eur",
110 price_set_id: "price-set-1",
111 },
112 ],
113 },
114 ])
115
116 const priceSetsResult = await service.listPriceSets(
117 {
118 id: ["price-set-1"],
119 },
120 {
121 relations: ["prices"],
122 }
123 )
124
125 expect(priceSetsResult).toHaveLength(1)
126 expect(priceSetsResult).toEqual([
127 expect.objectContaining({
128 id: "price-set-1",
129 prices: [
130 expect.objectContaining({
131 id: "price-set-money-amount-USD",
132 amount: 500,
133 }),
134 ],
135 }),
136 ])
137 })
138 })
139
140 describe("listAndCount", () => {
141 it("should return priceSets and count", async () => {
142 const [priceSetsResult, count] = await service.listAndCountPriceSets()
143
144 expect(count).toEqual(3)
145 expect(priceSetsResult).toEqual([
146 expect.objectContaining({
147 id: "price-set-1",
148 }),
149 expect.objectContaining({
150 id: "price-set-2",
151 }),
152 expect.objectContaining({
153 id: "price-set-3",
154 }),
155 ])
156 })
157
158 it("should return priceSets and count when filtered", async () => {
159 const [priceSetsResult, count] = await service.listAndCountPriceSets({
160 id: ["price-set-1"],
161 })
162
163 expect(count).toEqual(1)
164 expect(priceSetsResult).toEqual([
165 expect.objectContaining({
166 id: "price-set-1",
167 }),
168 ])
169 })
170
171 it("list priceSets with relations and selects", async () => {
172 const [priceSetsResult, count] = await service.listAndCountPriceSets(
173 {
174 id: ["price-set-1"],
175 },
176 {
177 select: ["id", "prices.amount", "prices.id"],
178 relations: ["prices"],
179 }
180 )
181
182 const serialized = JSON.parse(JSON.stringify(priceSetsResult))
183
184 expect(count).toEqual(1)
185 expect(serialized).toEqual([
186 {
187 id: "price-set-1",
188 prices: [
189 expect.objectContaining({
190 id: "price-set-money-amount-USD",
191 amount: 500,
192 }),
193 ],
194 },
195 ])
196 })
197
198 it("should return priceSets and count when using skip and take", async () => {
199 const [priceSetsResult, count] = await service.listAndCountPriceSets(
200 {},
201 { skip: 1, take: 1 }
202 )
203
204 expect(count).toEqual(3)
205 expect(priceSetsResult).toEqual([
206 expect.objectContaining({
207 id: "price-set-2",
208 }),
209 ])
210 })
211
212 it("should return requested fields", async () => {
213 const [priceSetsResult, count] = await service.listAndCountPriceSets(
214 {},
215 {
216 take: 1,
217 select: ["id"],
218 }
219 )
220
221 const serialized = JSON.parse(JSON.stringify(priceSetsResult))
222
223 expect(count).toEqual(3)
224 expect(serialized).toEqual([
225 {
226 id: "price-set-1",
227 },
228 ])
229 })
230 })
231
232 describe("retrieve", () => {
233 const id = "price-set-1"
234
235 it("should return priceSet for the given id", async () => {
236 const priceSet = await service.retrievePriceSet(id)
237
238 expect(priceSet).toEqual(
239 expect.objectContaining({
240 id,
241 })
242 )
243 })
244
245 it("should throw an error when priceSet with id does not exist", async () => {
246 let error
247
248 try {
249 await service.retrievePriceSet("does-not-exist")
250 } catch (e) {
251 error = e
252 }
253
254 expect(error.message).toEqual(
255 "PriceSet with id: does-not-exist was not found"
256 )
257 })
258
259 it("should throw an error when a id is not provided", async () => {
260 let error
261
262 try {
263 await service.retrievePriceSet(undefined as unknown as string)
264 } catch (e) {
265 error = e
266 }
267
268 expect(error.message).toEqual("priceSet - id must be defined")
269 })
270
271 it("should return priceSet based on config select param", async () => {
272 const priceSet = await service.retrievePriceSet(id, {
273 select: ["id"],
274 })
275
276 const serialized = JSON.parse(JSON.stringify(priceSet))
277
278 expect(serialized).toEqual({
279 id,
280 })
281 })
282
283 it("should return priceSet with only its own prices", async () => {
284 const [priceList] = await service.createPriceLists([
285 {
286 title: "test",
287 description: "test",
288 prices: [
289 {
290 amount: 400,
291 currency_code:"eur",
292 price_set_id: id,
293 rules: {
294 region_id: "DE",
295 },
296 },
297 {
298 amount: 600,
299 currency_code:"eur",
300 price_set_id: id,
301 },
302 ],
303 },
304 ])
305
306 const priceSetResult = await service.retrievePriceSet(id, {
307 relations: ["prices"],
308 })
309
310 expect(priceSetResult).toEqual(
311 expect.objectContaining({
312 id: "price-set-1",
313 prices: [
314 expect.objectContaining({
315 id: "price-set-money-amount-USD",
316 amount: 500,
317 }),
318 ],
319 })
320 )
321 })
322 })
323
324 describe("delete", () => {
325 const id = "price-set-1"
326
327 it("should delete the priceSets given an id successfully", async () => {
328 await service.deletePriceSets([id])
329
330 const priceSets = await service.listPriceSets({
331 id: [id],
332 })
333
334 expect(priceSets).toHaveLength(0)
335 })
336 })
337
338 describe("update", () => {
339 const id = "price-set-1"
340
341 it("should throw an error when an id does not exist", async () => {
342 let error = await service
343 .updatePriceSets("does-not-exist", {})
344 .catch((e) => e.message)
345
346 expect(error).toEqual(
347 "PriceSet with id: does-not-exist was not found"
348 )
349 })
350
351 it("should create, update, and delete prices to a price set", async () => {
352 const priceSetBefore = await service.retrievePriceSet(id, {
353 relations: ["prices"],
354 })
355
356 const updateResponse = await service.updatePriceSets(
357 priceSetBefore.id,
358 {
359 prices: [
360 { amount: 100, currency_code:"usd" },
361 { amount: 200, currency_code:"eur" },
362 ],
363 }
364 )
365
366 const priceSetAfter = await service.retrievePriceSet(id, {
367 relations: ["prices"],
368 })
369 expect(priceSetBefore.prices).toHaveLength(1)
370 expect(priceSetBefore.prices?.[0]).toEqual(
371 expect.objectContaining({
372 amount: 500,
373 currency_code:"usd",
374 })
375 )
376
377 expect(priceSetAfter.prices).toHaveLength(2)
378 expect(priceSetAfter.prices).toEqual(
379 expect.arrayContaining([
380 expect.objectContaining({
381 amount: 100,
382 currency_code:"usd",
383 }),
384 expect.objectContaining({
385 amount: 200,
386 currency_code:"eur",
387 }),
388 ])
389 )
390 expect(updateResponse.prices).toHaveLength(2)
391 expect(updateResponse.prices).toEqual(
392 expect.arrayContaining([
393 expect.objectContaining({
394 amount: 100,
395 currency_code:"usd",
396 }),
397 expect.objectContaining({
398 amount: 200,
399 currency_code:"eur",
400 }),
401 ])
402 )
403 })
404
405 it("should update price set prices and preserve price list prices", async () => {
406 const priceSetBefore = await service.retrievePriceSet(id, {
407 relations: ["prices"],
408 })
409
410 const [pl] = await service.createPriceLists([
411 {
412 title: "test",
413 description: "test",
414
415 prices: [
416 {
417 amount: 400,
418 currency_code:"eur",
419 price_set_id: priceSetBefore.id,
420 },
421 ],
422 },
423 ])
424
425 const updateResponse = await service.updatePriceSets(
426 priceSetBefore.id,
427 {
428 prices: [
429 { amount: 100, currency_code:"usd" },
430 { amount: 200, currency_code:"eur" },
431 ],
432 }
433 )
434
435 const priceSetAfter = await service.retrievePriceSet(id, {
436 relations: ["prices"],
437 })
438
439 expect(priceSetBefore.prices).toHaveLength(1)
440 expect(priceSetBefore.prices?.[0]).toEqual(
441 expect.objectContaining({
442 amount: 500,
443 currency_code:"usd",
444 })
445 )
446
447 // Price list prices are not present in this response
448 expect(priceSetAfter.prices).toHaveLength(2)
449 expect(priceSetAfter.prices).toEqual(
450 expect.arrayContaining([
451 expect.objectContaining({
452 amount: 100,
453 currency_code:"usd",
454 }),
455 expect.objectContaining({
456 amount: 200,
457 currency_code:"eur",
458 }),
459 ])
460 )
461 expect(updateResponse.prices).toHaveLength(2)
462 expect(updateResponse.prices).toEqual(
463 expect.arrayContaining([
464 expect.objectContaining({
465 amount: 100,
466 currency_code:"usd",
467 }),
468 expect.objectContaining({
469 amount: 200,
470 currency_code:"eur",
471 }),
472 ])
473 )
474
475 const plAfter = await service.retrievePriceList(pl.id, {
476 relations: ["prices"],
477 })
478
479 // Price list prices are preserved
480 expect(plAfter.prices).toHaveLength(1)
481 expect(plAfter.prices).toEqual(
482 expect.arrayContaining([
483 expect.objectContaining({
484 amount: 400,
485 currency_code:"eur",
486 price_set_id: priceSetBefore.id,
487 }),
488 ])
489 )
490 })
491
492 it("should upsert the later price when setting a price set with existing equivalent rules", async () => {
493 await service.updatePriceSets(id, {
494 prices: [
495 {
496 amount: 100,
497 currency_code:"usd",
498 rules: { region_id: "1234" },
499 },
500 {
501 amount: 200,
502 currency_code:"usd",
503 rules: { region_id: "1234" },
504 },
505 ],
506 })
507
508 const priceSet = await service.retrievePriceSet(id, {
509 relations: ["prices", "prices.price_rules"],
510 })
511
512 expect(priceSet.prices).toEqual([
513 expect.objectContaining({
514 amount: 200,
515 currency_code:"usd",
516 price_rules: [
517 expect.objectContaining({
518 attribute: "region_id",
519 value: "1234",
520 }),
521 ],
522 }),
523 ])
524 })
525 })
526
527 describe("create", () => {
528 it("should create a price set with rule types and money amounts", async () => {
529 const [priceSet] = await service.createPriceSets([
530 {
531 prices: [
532 {
533 amount: 100,
534 currency_code:"usd",
535 rules: {
536 region_id: "1",
537 },
538 },
539 ],
540 },
541 ])
542
543 expect(priceSet).toEqual(
544 expect.objectContaining({
545 prices: [
546 expect.objectContaining({
547 amount: 100,
548 currency_code:"usd",
549 rules_count: 1,
550 }),
551 ],
552 })
553 )
554
555 const events = eventBusEmitSpy.mock.calls[0][0]
556 expect(events).toHaveLength(3)
557 expect(events[0]).toEqual(
558 composeMessage(PricingEvents.PRICE_SET_CREATED, {
559 source: Modules.PRICING,
560 action: CommonEvents.CREATED,
561 object: "price_set",
562 data: { id: priceSet.id },
563 })
564 )
565
566 expect(events[1]).toEqual(
567 composeMessage(PricingEvents.PRICE_CREATED, {
568 source: Modules.PRICING,
569 action: CommonEvents.CREATED,
570 object: "price",
571 data: { id: priceSet.prices![0].id },
572 })
573 )
574
575 expect(events[2]).toEqual(
576 composeMessage(PricingEvents.PRICE_RULE_CREATED, {
577 source: Modules.PRICING,
578 action: CommonEvents.CREATED,
579 object: "price_rule",
580 data: {
581 id: (priceSet as any).prices![0].price_rules[0].id,
582 },
583 })
584 )
585 })
586
587 it("should create a price set with prices", async () => {
588 const [priceSet] = await service.createPriceSets([
589 {
590 prices: [
591 {
592 amount: 100,
593 currency_code:"usd",
594 rules: {
595 region_id: "1",
596 },
597 },
598 {
599 amount: 150,
600 currency_code:"usd",
601 },
602 ],
603 },
604 ])
605
606 expect(priceSet).toEqual(
607 expect.objectContaining({
608 prices: expect.arrayContaining([
609 expect.objectContaining({
610 amount: 100,
611 currency_code:"usd",
612 }),
613 expect.objectContaining({
614 amount: 150,
615 currency_code:"usd",
616 }),
617 ]),
618 })
619 )
620 })
621
622 it("should create price set with prices including rule operators", async () => {
623 const [priceSet] = await service.createPriceSets([
624 {
625 prices: [
626 {
627 amount: 100,
628 currency_code:"usd",
629 rules: {
630 region_id: "1",
631 custom_rule: [
632 {
633 operator: "gt",
634 value: 500,
635 },
636 {
637 operator: "lt",
638 value: 1000,
639 },
640 ],
641 },
642 },
643 {
644 amount: 150,
645 currency_code:"usd",
646 },
647 ],
648 },
649 ])
650
651 expect(priceSet.prices).toHaveLength(2)
652 expect(priceSet).toEqual(
653 expect.objectContaining({
654 prices: expect.arrayContaining([
655 expect.objectContaining({
656 amount: 100,
657 currency_code:"usd",
658 price_rules: expect.arrayContaining([
659 expect.objectContaining({
660 attribute: "region_id",
661 operator: "eq",
662 value: "1",
663 }),
664 expect.objectContaining({
665 attribute: "custom_rule",
666 operator: "gt",
667 value: "500",
668 }),
669 expect.objectContaining({
670 attribute: "custom_rule",
671 operator: "lt",
672 value: "1000",
673 }),
674 ]),
675 }),
676 expect.objectContaining({
677 amount: 150,
678 currency_code:"usd",
679 }),
680 ]),
681 })
682 )
683 })
684
685 it("should throw error when creating price with invalid rules", async () => {
686 let error = await service
687 .createPriceSets([
688 {
689 prices: [
690 {
691 amount: 100,
692 currency_code:"usd",
693 rules: {
694 custom_rule: [
695 {
696 operator: "unknown" as any,
697 value: 500,
698 },
699 ],
700 },
701 },
702 ],
703 },
704 ])
705 .catch((e) => e)
706
707 expect(error.message).toBe(
708 "operator should be one of gte, lte, gt, lt, eq"
709 )
710
711 error = await service
712 .createPriceSets([
713 {
714 prices: [
715 {
716 amount: 100,
717 currency_code:"usd",
718 rules: {
719 custom_rule: [
720 {
721 operator: "gt",
722 value: "string" as any,
723 },
724 ],
725 },
726 },
727 ],
728 },
729 ])
730 .catch((e) => e)
731
732 expect(error.message).toBe("value should be a number")
733 })
734
735 it("should create a priceSet successfully", async () => {
736 await service.createPriceSets([
737 {
738 id: "price-set-new",
739 } as unknown as CreatePriceSetDTO,
740 ])
741
742 const [priceSet] = await service.listPriceSets({
743 id: ["price-set-new"],
744 })
745
746 expect(priceSet).toEqual(
747 expect.objectContaining({
748 id: "price-set-new",
749 })
750 )
751 })
752
753 it("should take the later price when passing two prices with equivalent rules", async () => {
754 await service.createPriceSets([
755 {
756 id: "price-set-new",
757 prices: [
758 {
759 amount: 100,
760 currency_code:"usd",
761 rules: { region_id: "1234" },
762 },
763 {
764 amount: 200,
765 currency_code:"usd",
766 rules: { region_id: "1234" },
767 },
768 ],
769 } as unknown as CreatePriceSetDTO,
770 ])
771
772 const priceSet = await service.retrievePriceSet("price-set-new", {
773 relations: ["prices", "prices.price_rules"],
774 })
775
776 expect(priceSet.prices).toEqual([
777 expect.objectContaining({
778 amount: 200,
779 currency_code:"usd",
780 price_rules: [
781 expect.objectContaining({
782 attribute: "region_id",
783 value: "1234",
784 }),
785 ],
786 }),
787 ])
788 })
789 })
790
791 describe("addPrices", () => {
792 it("should add prices to existing price set", async () => {
793 await service.addPrices([
794 {
795 priceSetId: "price-set-1",
796 prices: [
797 {
798 amount: 100,
799 currency_code:"usd",
800 rules: { region_id: "1234" },
801 },
802 ],
803 },
804 ])
805
806 const [priceSet] = await service.listPriceSets(
807 { id: ["price-set-1"] },
808 { relations: ["prices", "prices.price_rules"] }
809 )
810
811 expect(priceSet).toEqual(
812 expect.objectContaining({
813 id: "price-set-1",
814 prices: expect.arrayContaining([
815 expect.objectContaining({
816 amount: 100,
817 currency_code:"usd",
818 }),
819 ]),
820 })
821 )
822
823 const events = eventBusEmitSpy.mock.calls[0][0]
824 expect(events).toHaveLength(2)
825 expect(events[0]).toEqual(
826 composeMessage(PricingEvents.PRICE_CREATED, {
827 source: Modules.PRICING,
828 action: CommonEvents.CREATED,
829 object: "price",
830 data: { id: priceSet.prices![1].id },
831 })
832 )
833 expect(events[1]).toEqual(
834 composeMessage(PricingEvents.PRICE_RULE_CREATED, {
835 source: Modules.PRICING,
836 action: CommonEvents.CREATED,
837 object: "price_rule",
838 data: { id: priceSet.prices![1].price_rules[0].id },
839 })
840 )
841 })
842
843 it("should add prices to multiple existing price set", async () => {
844 await service.addPrices([
845 {
846 priceSetId: "price-set-1",
847 prices: [
848 {
849 amount: 100,
850 currency_code:"usd",
851 rules: { region_id: "region-1" },
852 },
853 ],
854 },
855 {
856 priceSetId: "price-set-2",
857 prices: [
858 {
859 amount: 150,
860 currency_code:"eur",
861 rules: { region_id: "region-2" },
862 },
863 ],
864 },
865 ])
866
867 const priceSets = await service.listPriceSets(
868 { id: ["price-set-1", "price-set-2"] },
869 { relations: ["prices"] }
870 )
871
872 expect(priceSets).toEqual([
873 expect.objectContaining({
874 id: "price-set-1",
875 prices: expect.arrayContaining([
876 expect.objectContaining({
877 amount: 100,
878 currency_code:"usd",
879 }),
880 ]),
881 }),
882 expect.objectContaining({
883 id: "price-set-2",
884 prices: expect.arrayContaining([
885 expect.objectContaining({
886 amount: 150,
887 currency_code:"eur",
888 }),
889 ]),
890 }),
891 ])
892 })
893
894 it("should do an update if a price exists with the equivalent rules", async () => {
895 await service.addPrices([
896 {
897 priceSetId: "price-set-1",
898 prices: [
899 {
900 amount: 100,
901 currency_code:"usd",
902 rules: {
903 region_id: "123",
904 },
905 },
906 ],
907 },
908 ])
909
910 await service.addPrices([
911 {
912 priceSetId: "price-set-1",
913 prices: [
914 {
915 amount: 200,
916 currency_code:"usd",
917 rules: {
918 region_id: "123",
919 test: [{ value: 500, operator: "gte" }],
920 },
921 },
922 ],
923 },
924 ])
925
926 const priceSet = await service.retrievePriceSet("price-set-1", {
927 relations: ["prices", "prices.price_rules"],
928 })
929
930 expect(
931 priceSet.prices?.sort((a: any, b: any) => a.amount - b.amount)
932 ).toEqual([
933 expect.objectContaining({
934 amount: 100,
935 currency_code:"usd",
936 }),
937 expect.objectContaining({
938 amount: 200,
939 currency_code:"usd",
940 price_rules: [
941 expect.objectContaining({
942 attribute: "region_id",
943 value: "123",
944 }),
945 expect.objectContaining({
946 attribute: "test",
947 operator: "gte",
948 value: "500",
949 }),
950 ],
951 }),
952 expect.objectContaining({
953 amount: 500,
954 currency_code:"usd",
955 }),
956 ])
957 })
958 })
959 })
960 },
961 })