1 import {
2 CreatePriceRuleDTO,
3 CreatePriceSetDTO,
4 IPricingModuleService,
5 PricingTypes,
6 } from "@medusajs/framework/types"
7 import {
8 Modules,
9 PriceListStatus,
10 PriceListType,
11 } from "@medusajs/framework/utils"
12 import { moduleIntegrationTestRunner } from "@medusajs/test-utils"
13 import { withOperator } from "../../../__fixtures__/price-rule"
14 import { seedPriceData } from "../../../__fixtures__/seed-price-data"
15
16 jest.setTimeout(30000)
17
18 const defaultRules = {
19 customer_group_id: ["vip-customer-group-id", "another-vip-customer-group-id"],
20 region_id: ["DE", "DK"],
21 }
22
23 const defaultPriceListPrices: PricingTypes.CreatePriceListPriceDTO[] = [
24 {
25 amount: 232,
26 currency_code: "pln",
27 price_set_id: "price-set-PLN",
28 },
29 {
30 amount: 455,
31 currency_code: "eur",
32 price_set_id: "price-set-EUR",
33 },
34 ]
35 const createPriceLists = async (
36 service,
37 priceListOverride: Partial<
38 Omit<PricingTypes.CreatePriceListDTO, "rules" | "prices">
39 > = {},
40 rules: object = defaultRules,
41 prices = defaultPriceListPrices
42 ) => {
43 return await service.createPriceLists([
44 {
45 title: "Test Price List",
46 description: "test description",
47 type: PriceListType.SALE,
48 status: PriceListStatus.ACTIVE,
49 rules,
50 prices,
51 ...priceListOverride,
52 },
53 ])
54 }
55
56 moduleIntegrationTestRunner<IPricingModuleService>({
57 moduleName: Modules.PRICING,
58 testSuite: ({ MikroOrmWrapper, service }) => {
59 describe("PricingModule Service - Calculate Price", () => {
60 describe("calculatePrices", () => {
61 beforeEach(async () => {
62 const priceSetsData = [
63 { id: "price-set-EUR" },
64 { id: "price-set-PLN" },
65 { id: "price-set-ETH" },
66 ] as unknown as CreatePriceSetDTO[]
67
68 const pricesData = [
69 {
70 id: "price-PLN",
71 title: "price PLN",
72 price_set_id: "price-set-PLN",
73 currency_code: "pln",
74 amount: 1000,
75 min_quantity: 1,
76 max_quantity: 10,
77 rules_count: 0,
78 },
79 {
80 id: "price-PLN-min-quantity-only",
81 title: "price PLN - min quantity only",
82 price_set_id: "price-set-PLN",
83 currency_code: "pln",
84 amount: 1250,
85 min_quantity: 20,
86 max_quantity: null,
87 rules_count: 0,
88 },
89 {
90 id: "price-ETH",
91 title: "price ETH",
92 price_set_id: "price-set-ETH",
93 currency_code: "eth",
94 amount: {
95 value: "12345678988754.00000010000000085",
96 },
97 min_quantity: 1,
98 max_quantity: 10,
99 rules_count: 0,
100 },
101 {
102 id: "price-company_id-EUR",
103 title: "price EUR - company_id",
104 price_set_id: "price-set-EUR",
105 currency_code: "eur",
106 amount: 500,
107 min_quantity: 1,
108 max_quantity: 10,
109 rules_count: 1,
110 },
111 {
112 id: "price-company_id-PLN",
113 title: "price PLN - company_id",
114 price_set_id: "price-set-PLN",
115 currency_code: "pln",
116 amount: 400,
117 min_quantity: 1,
118 max_quantity: 5,
119 rules_count: 1,
120 },
121 {
122 id: "price-region_id-PLN",
123 title: "price PLN - region_id",
124 price_set_id: "price-set-PLN",
125 currency_code: "pln",
126 amount: 300,
127 min_quantity: 1,
128 max_quantity: 4,
129 rules_count: 1,
130 },
131 {
132 id: "price-region_id+company_id-PLN",
133 title: "price region_id + company_id",
134 price_set_id: "price-set-PLN",
135 currency_code: "pln",
136 amount: 999,
137 min_quantity: 1,
138 max_quantity: 10,
139 rules_count: 2,
140 },
141 {
142 id: "price-region_id-PLN-5-qty",
143 title: "price PLN - region_id 5 qty",
144 price_set_id: "price-set-PLN",
145 currency_code: "pln",
146 amount: 250,
147 min_quantity: 4,
148 max_quantity: 10,
149 rules_count: 1,
150 },
151 {
152 id: "price-region_id_company_id-PL-EUR",
153 title: "price PLN - region_id PL with EUR currency",
154 price_set_id: "price-set-PLN",
155 currency_code: "eur",
156 amount: 200,
157 min_quantity: 1,
158 max_quantity: 3,
159 rules_count: 2,
160 },
161 {
162 id: "price-region_id_company_id-PL-EUR-4-qty",
163 title:
164 "price PLN - region_id PL with EUR currency for quantity 4",
165 price_set_id: "price-set-PLN",
166 currency_code: "eur",
167 amount: 50,
168 min_quantity: 4,
169 max_quantity: 10,
170 rules_count: 2,
171 },
172 {
173 id: "price-region_id_company_id-PL-EUR-customer-group",
174 title:
175 "price PLN - region_id PL with EUR currency for customer group",
176 price_set_id: "price-set-PLN",
177 currency_code: "eur",
178 amount: 100,
179 min_quantity: 1,
180 max_quantity: 3,
181 rules_count: 3,
182 },
183 ]
184
185 const priceRuleData = [
186 {
187 id: "price-rule-company_id-EUR",
188 price_set_id: "price-set-EUR",
189 attribute: "company_id",
190 value: "EUR",
191 price_list_id: "test",
192 price_id: "price-company_id-EUR",
193 },
194 {
195 id: "price-rule-company_id-PLN",
196 price_set_id: "price-set-PLN",
197 attribute: "company_id",
198 value: "medusa-company-id",
199 price_list_id: "test",
200 price_id: "price-company_id-PLN",
201 },
202 {
203 id: "price-rule-region_id-PLN",
204 price_set_id: "price-set-PLN",
205 attribute: "region_id",
206 value: "PL",
207 price_list_id: "test",
208 price_id: "price-region_id-PLN",
209 },
210 {
211 id: "price-rule-region_id+company_id-PL",
212 price_set_id: "price-set-PLN",
213 attribute: "region_id",
214 value: "PL",
215 price_list_id: "test",
216 price_id: "price-region_id+company_id-PLN",
217 },
218 {
219 id: "price-rule-region_id+company_id-medusa-company-id",
220 price_set_id: "price-set-PLN",
221 attribute: "company_id",
222 value: "medusa-company-id",
223 price_list_id: "test",
224 price_id: "price-region_id+company_id-PLN",
225 },
226 {
227 id: "price-rule-region_id-PLN-5-qty",
228 price_set_id: "price-set-PLN",
229 attribute: "region_id",
230 value: "PL",
231 price_list_id: "test",
232 price_id: "price-region_id-PLN-5-qty",
233 },
234 {
235 id: "price-rule-region_id-company_id-PL",
236 price_set_id: "price-set-PLN",
237 attribute: "region_id",
238 value: "PL",
239 price_list_id: "test",
240 price_id: "price-region_id_company_id-PL-EUR",
241 },
242 {
243 id: "price-rule-region_id-company_id-PLN",
244 price_set_id: "price-set-PLN",
245 attribute: "company_id",
246 value: "medusa-company-id",
247 price_list_id: "test",
248 price_id: "price-region_id_company_id-PL-EUR",
249 },
250 {
251 id: "price-rule-region_id-company_id-PL-4-qty",
252 price_set_id: "price-set-PLN",
253 attribute: "region_id",
254 value: "PL",
255 price_list_id: "test",
256 price_id: "price-region_id_company_id-PL-EUR-4-qty",
257 },
258 {
259 id: "price-rule-region_id-company_id-PLN-4-qty",
260 price_set_id: "price-set-PLN",
261 attribute: "company_id",
262 value: "medusa-company-id",
263 price_list_id: "test",
264 price_id: "price-region_id_company_id-PL-EUR-4-qty",
265 },
266 {
267 id: "price-rule-region_id-currency_customer_group_code-PL",
268 price_set_id: "price-set-PLN",
269 attribute: "region_id",
270 value: "PL",
271 price_list_id: "test",
272 price_id: "price-region_id_company_id-PL-EUR-customer-group",
273 },
274 {
275 id: "price-rule-region_id-currency_customer_group_code-PLN",
276 price_set_id: "price-set-PLN",
277 attribute: "company_id",
278 value: "medusa-company-id",
279 price_list_id: "test",
280 price_id: "price-region_id_company_id-PL-EUR-customer-group",
281 },
282 {
283 id: "price-rule-region_id-currency_customer_group_code-test_customer_group",
284 price_set_id: "price-set-PLN",
285 attribute: "customer_group_id",
286 value: "test-customer-group",
287 price_list_id: "test",
288 price_id: "price-region_id_company_id-PL-EUR-customer-group",
289 },
290 ] as unknown as CreatePriceRuleDTO[]
291
292 await seedPriceData(MikroOrmWrapper.forkManager(), {
293 priceSetsData,
294 pricesData,
295 priceRuleData,
296 })
297 })
298
299 it("should successfully calculate prices with complex context", async () => {
300 const context = {
301 id: "cart_01JRDH08QD8CZ0KJDVE410KM1J",
302 currency_code: "pln",
303 email: "tony@stark-industries.com",
304 region_id: "reg_01JRDH08ENY3276P6133BVXGWJ",
305 created_at: "2025-04-09T14:59:24.526Z",
306 updated_at: "2025-04-09T14:59:24.526Z",
307 completed_at: null,
308 total: 1500,
309 subtotal: 1428.5714285714287,
310 tax_total: 71.42857142857143,
311 discount_total: 0,
312 discount_subtotal: 0,
313 discount_tax_total: 0,
314 original_total: 1500,
315 original_tax_total: 71.42857142857143,
316 item_total: 1500,
317 item_subtotal: 1428.5714285714287,
318 item_tax_total: 71.42857142857143,
319 original_item_total: 1500,
320 original_item_subtotal: 1428.5714285714287,
321 original_item_tax_total: 71.42857142857143,
322 shipping_total: 0,
323 shipping_subtotal: 0,
324 shipping_tax_total: 0,
325 original_shipping_tax_total: 0,
326 original_shipping_subtotal: 0,
327 original_shipping_total: 0,
328 credit_line_subtotal: 0,
329 credit_line_tax_total: 0,
330 credit_line_total: 0,
331 metadata: null,
332 sales_channel_id: "sc_01JRDH08KWX1AR5SB0A3THWWQQ",
333 shipping_address_id: "caaddr_01JRDH08QDXHV9SJXKHT04TXK0",
334 customer_id: "cus_01JRDH08ATYB5AMFEZDTWCQWNK",
335 items: [
336 {
337 id: "cali_01JRDH08QDQH3CB1DE4S79HREC",
338 thumbnail: null,
339 variant_id: "variant_01JRDH08GJCZQB4GZCDDTYMD1V",
340 product_id: "prod_01JRDH08FPZ6QBZQ096B310RM7",
341 product_type_id: null,
342 product_title: "Medusa T-Shirt",
343 product_description: null,
344 product_subtitle: null,
345 product_type: null,
346 product_collection: null,
347 product_handle: "t-shirt",
348 variant_sku: "SHIRT-S-BLACK",
349 variant_barcode: null,
350 variant_title: "S / Black",
351 requires_shipping: true,
352 metadata: {},
353 created_at: "2025-04-09T14:59:24.526Z",
354 updated_at: "2025-04-09T14:59:24.526Z",
355 title: "S / Black",
356 quantity: 1,
357 unit_price: 1500,
358 compare_at_unit_price: null,
359 is_tax_inclusive: true,
360 tax_lines: [
361 {
362 id: "calitxl_01JRDH08RJEQ4WXXDTJYWV7B4M",
363 description: "CA Default Rate",
364 code: "CADEFAULT",
365 rate: 5,
366 provider_id: "system",
367 },
368 ],
369 adjustments: [],
370 product: {
371 id: "prod_01JRDH08FPZ6QBZQ096B310RM7",
372 collection_id: null,
373 type_id: null,
374 categories: [],
375 tags: [],
376 },
377 },
378 ],
379 shipping_methods: [],
380 shipping_address: {
381 id: "caaddr_01JRDH08QDXHV9SJXKHT04TXK0",
382 first_name: null,
383 last_name: null,
384 company: null,
385 address_1: "test address 1",
386 address_2: "test address 2",
387 city: "SF",
388 postal_code: "94016",
389 country_code: "US",
390 province: "CA",
391 phone: null,
392 },
393 billing_address: null,
394 credit_lines: [],
395 customer: {
396 id: "cus_01JRDH08ATYB5AMFEZDTWCQWNK",
397 email: "tony@stark-industries.com",
398 groups: [],
399 },
400 region: {
401 id: "reg_01JRDH08ENY3276P6133BVXGWJ",
402 name: "US",
403 currency_code: "usd",
404 automatic_taxes: true,
405 countries: [
406 {
407 iso_2: "us",
408 iso_3: "usa",
409 num_code: "840",
410 name: "UNITED STATES",
411 display_name: "United States",
412 region_id: "reg_01JRDH08ENY3276P6133BVXGWJ",
413 metadata: null,
414 created_at: "2025-04-09T14:59:20.275Z",
415 updated_at: "2025-04-09T14:59:24.250Z",
416 deleted_at: null,
417 },
418 ],
419 },
420 promotions: [],
421 }
422
423 const calculatedPrice = await service.calculatePrices(
424 { id: ["price-set-EUR", "price-set-PLN"] },
425 { context: context as any }
426 )
427
428 expect(calculatedPrice).toEqual([
429 {
430 id: "price-set-PLN",
431 is_calculated_price_price_list: false,
432 is_calculated_price_tax_inclusive: false,
433 calculated_amount: 1000,
434 raw_calculated_amount: {
435 value: "1000",
436 precision: 20,
437 },
438 is_original_price_price_list: false,
439 is_original_price_tax_inclusive: false,
440 original_amount: 1000,
441 raw_original_amount: {
442 value: "1000",
443 precision: 20,
444 },
445 currency_code: "pln",
446 calculated_price: {
447 id: "price-PLN",
448 price_list_id: null,
449 price_list_type: null,
450 min_quantity: 1,
451 max_quantity: 10,
452 },
453 original_price: {
454 id: "price-PLN",
455 price_list_id: null,
456 price_list_type: null,
457 min_quantity: 1,
458 max_quantity: 10,
459 },
460 },
461 ])
462 })
463
464 it("should successfully calculate prices where only min quantity is set", async () => {
465 const context = {
466 currency_code: "pln",
467 region_id: "PL",
468 quantity: 255,
469 }
470
471 const calculatedPrice = await service.calculatePrices(
472 { id: ["price-set-EUR", "price-set-PLN"] },
473 { context }
474 )
475
476 expect(calculatedPrice).toEqual([
477 {
478 id: "price-set-PLN",
479 is_calculated_price_price_list: false,
480 is_calculated_price_tax_inclusive: false,
481 calculated_amount: 1250,
482 raw_calculated_amount: {
483 value: "1250",
484 precision: 20,
485 },
486 is_original_price_price_list: false,
487 is_original_price_tax_inclusive: false,
488 original_amount: 1250,
489 raw_original_amount: {
490 value: "1250",
491 precision: 20,
492 },
493 currency_code: "pln",
494 calculated_price: {
495 id: "price-PLN-min-quantity-only",
496 price_list_id: null,
497 price_list_type: null,
498 min_quantity: 20,
499 max_quantity: null,
500 },
501 original_price: {
502 id: "price-PLN-min-quantity-only",
503 price_list_id: null,
504 price_list_type: null,
505 min_quantity: 20,
506 max_quantity: null,
507 },
508 },
509 ])
510 })
511
512 it("should throw an error when currency code is not set", async () => {
513 let result = service.calculatePrices(
514 { id: ["price-set-EUR", "price-set-PLN"] },
515 {}
516 )
517
518 await expect(result).rejects.toThrow(
519 "Method calculatePrices requires currency_code in the pricing context"
520 )
521
522 result = service.calculatePrices(
523 { id: ["price-set-EUR", "price-set-PLN"] },
524 { context: { region_id: "DE" } }
525 )
526
527 await expect(result).rejects.toThrow(
528 "Method calculatePrices requires currency_code in the pricing context"
529 )
530 })
531
532 it("calculating prices when listing price sets should return null when there are no prices", async () => {
533 const [newSet] = await service.createPriceSets([{}])
534 const calculatePricesResult = await service.calculatePrices(
535 { id: [newSet.id] },
536 { context: { currency_code: "pln" } }
537 )
538
539 const priceSetsResult = await service.listPriceSets(
540 {
541 id: [newSet.id],
542 context: { currency_code: "pln" },
543 },
544 { relations: ["calculated_price"] }
545 )
546
547 expect(calculatePricesResult).toEqual([])
548 expect(priceSetsResult[0].calculated_price).toEqual(null)
549 })
550
551 it("should return filled prices when 1 context is present and price is setup for PLN", async () => {
552 const priceSetsResult = await service.calculatePrices(
553 { id: ["price-set-EUR", "price-set-PLN"] },
554 {
555 context: { currency_code: "pln" },
556 }
557 )
558
559 expect(priceSetsResult).toEqual([
560 {
561 id: "price-set-PLN",
562 is_calculated_price_price_list: false,
563 is_calculated_price_tax_inclusive: false,
564 calculated_amount: 1000,
565 raw_calculated_amount: {
566 value: "1000",
567 precision: 20,
568 },
569 is_original_price_price_list: false,
570 is_original_price_tax_inclusive: false,
571 original_amount: 1000,
572 raw_original_amount: {
573 value: "1000",
574 precision: 20,
575 },
576 currency_code: "pln",
577 calculated_price: {
578 id: "price-PLN",
579 price_list_id: null,
580 price_list_type: null,
581 min_quantity: 1,
582 max_quantity: 10,
583 },
584 original_price: {
585 id: "price-PLN",
586 price_list_id: null,
587 price_list_type: null,
588 min_quantity: 1,
589 max_quantity: 10,
590 },
591 },
592 ])
593 })
594
595 it("should return filled prices when 1 context is present and price is setup for ETH", async () => {
596 const priceSetsResult = await service.calculatePrices(
597 { id: ["price-set-ETH"] },
598 {
599 context: { currency_code: "eth" },
600 }
601 )
602
603 expect(priceSetsResult).toEqual([
604 {
605 id: "price-set-ETH",
606 is_calculated_price_price_list: false,
607 is_calculated_price_tax_inclusive: false,
608 calculated_amount: 12345678988754.000000100000001,
609 raw_calculated_amount: {
610 value: "12345678988754.00000010000000085",
611 precision: 20,
612 },
613 is_original_price_price_list: false,
614 is_original_price_tax_inclusive: false,
615 original_amount: 12345678988754.000000100000001,
616 raw_original_amount: {
617 value: "12345678988754.00000010000000085",
618 precision: 20,
619 },
620 currency_code: "eth",
621 calculated_price: {
622 id: "price-ETH",
623 price_list_id: null,
624 price_list_type: null,
625 min_quantity: 1,
626 max_quantity: 10,
627 },
628 original_price: {
629 id: "price-ETH",
630 price_list_id: null,
631 price_list_type: null,
632 min_quantity: 1,
633 max_quantity: 10,
634 },
635 },
636 ])
637 })
638
639 it("should return filled prices when 1 context is present and price is setup for PLN region_id", async () => {
640 const priceSetsResult = await service.calculatePrices(
641 { id: ["price-set-EUR", "price-set-PLN"] },
642 {
643 context: { currency_code: "pln", region_id: "PL" },
644 }
645 )
646
647 expect(priceSetsResult).toEqual([
648 {
649 id: "price-set-PLN",
650 is_calculated_price_price_list: false,
651 is_calculated_price_tax_inclusive: false,
652 calculated_amount: 300,
653 raw_calculated_amount: {
654 value: "300",
655 precision: 20,
656 },
657 is_original_price_price_list: false,
658 is_original_price_tax_inclusive: false,
659 original_amount: 300,
660 raw_original_amount: {
661 value: "300",
662 precision: 20,
663 },
664 currency_code: "pln",
665 calculated_price: {
666 id: "price-region_id-PLN",
667 price_list_id: null,
668 price_list_type: null,
669 min_quantity: 1,
670 max_quantity: 4,
671 },
672 original_price: {
673 id: "price-region_id-PLN",
674 price_list_id: null,
675 price_list_type: null,
676 min_quantity: 1,
677 max_quantity: 4,
678 },
679 },
680 ])
681 })
682
683 it("should return filled prices when 1 context is present and price is setup for PLN currency_code", async () => {
684 const priceSetsResult = await service.calculatePrices(
685 { id: ["price-set-EUR", "price-set-PLN"] },
686 {
687 context: { currency_code: "pln" },
688 }
689 )
690
691 expect(priceSetsResult).toEqual([
692 {
693 id: "price-set-PLN",
694 is_calculated_price_price_list: false,
695 is_calculated_price_tax_inclusive: false,
696 calculated_amount: 1000,
697 raw_calculated_amount: {
698 value: "1000",
699 precision: 20,
700 },
701 is_original_price_price_list: false,
702 is_original_price_tax_inclusive: false,
703 original_amount: 1000,
704 raw_original_amount: {
705 value: "1000",
706 precision: 20,
707 },
708 currency_code: "pln",
709 calculated_price: {
710 id: "price-PLN",
711 price_list_id: null,
712 price_list_type: null,
713 min_quantity: 1,
714 max_quantity: 10,
715 },
716 original_price: {
717 id: "price-PLN",
718 price_list_id: null,
719 price_list_type: null,
720 min_quantity: 1,
721 max_quantity: 10,
722 },
723 },
724 ])
725 })
726
727 it("should return null prices when 1 context is present and price is NOT setup", async () => {
728 const priceSetsResult = await service.calculatePrices(
729 { id: ["price-set-EUR", "price-set-PLN"] },
730 {
731 context: { currency_code: "eur", does_not_exist: "EUR" },
732 }
733 )
734
735 expect(priceSetsResult).toEqual([])
736 })
737
738 it("should return filled prices when 2 contexts are present and price is setup", async () => {
739 const priceSetsResult = await service.calculatePrices(
740 { id: ["price-set-EUR", "price-set-PLN"] },
741 {
742 context: { currency_code: "pln", region_id: "PL" },
743 }
744 )
745
746 expect(priceSetsResult).toEqual([
747 {
748 id: "price-set-PLN",
749 is_calculated_price_price_list: false,
750 is_calculated_price_tax_inclusive: false,
751 calculated_amount: 300,
752 raw_calculated_amount: {
753 value: "300",
754 precision: 20,
755 },
756 is_original_price_price_list: false,
757 is_original_price_tax_inclusive: false,
758 original_amount: 300,
759 raw_original_amount: {
760 value: "300",
761 precision: 20,
762 },
763 currency_code: "pln",
764 calculated_price: {
765 id: "price-region_id-PLN",
766 price_list_id: null,
767 price_list_type: null,
768 min_quantity: 1,
769 max_quantity: 4,
770 },
771 original_price: {
772 id: "price-region_id-PLN",
773 price_list_id: null,
774 price_list_type: null,
775 min_quantity: 1,
776 max_quantity: 4,
777 },
778 },
779 ])
780 })
781
782 it("should return filled prices when 2 contexts are present and price is not setup", async () => {
783 const priceSetsResult = await service.calculatePrices(
784 { id: ["price-set-EUR", "price-set-PLN"] },
785 {
786 context: { currency_code: "pln", company_id: "doesnotexist" },
787 }
788 )
789
790 expect(priceSetsResult).toEqual([
791 {
792 id: "price-set-PLN",
793 is_calculated_price_price_list: false,
794 is_calculated_price_tax_inclusive: false,
795 calculated_amount: 1000,
796 raw_calculated_amount: {
797 value: "1000",
798 precision: 20,
799 },
800 is_original_price_price_list: false,
801 is_original_price_tax_inclusive: false,
802 original_amount: 1000,
803 raw_original_amount: {
804 value: "1000",
805 precision: 20,
806 },
807 currency_code: "pln",
808 calculated_price: {
809 id: "price-PLN",
810 price_list_id: null,
811 price_list_type: null,
812 min_quantity: 1,
813 max_quantity: 10,
814 },
815 original_price: {
816 id: "price-PLN",
817 price_list_id: null,
818 price_list_type: null,
819 min_quantity: 1,
820 max_quantity: 10,
821 },
822 },
823 ])
824 })
825
826 it("should return filled prices when 2 contexts are present and price is setup along with declaring quantity", async () => {
827 const priceSetsResult = await service.calculatePrices(
828 { id: ["price-set-PLN"] },
829 {
830 context: { currency_code: "pln", region_id: "PL", quantity: 5 },
831 }
832 )
833
834 expect(priceSetsResult).toEqual([
835 {
836 id: "price-set-PLN",
837 is_calculated_price_price_list: false,
838 is_calculated_price_tax_inclusive: false,
839 calculated_amount: 250,
840 raw_calculated_amount: {
841 value: "250",
842 precision: 20,
843 },
844 is_original_price_price_list: false,
845 is_original_price_tax_inclusive: false,
846 original_amount: 250,
847 raw_original_amount: {
848 value: "250",
849 precision: 20,
850 },
851 currency_code: "pln",
852 calculated_price: {
853 id: "price-region_id-PLN-5-qty",
854 price_list_id: null,
855 price_list_type: null,
856 min_quantity: 4,
857 max_quantity: 10,
858 },
859 original_price: {
860 id: "price-region_id-PLN-5-qty",
861 price_list_id: null,
862 price_list_type: null,
863 min_quantity: 4,
864 max_quantity: 10,
865 },
866 },
867 ])
868 })
869
870 it("should return filled prices when 3 contexts are present and price is partially setup for 2", async () => {
871 const priceSetsResult = await service.calculatePrices(
872 { id: ["price-set-EUR", "price-set-PLN"] },
873 {
874 context: {
875 currency_code: "pln",
876 region_id: "PL",
877 customer_group_id: "test-customer-group",
878 company_id: "does-not-exist",
879 },
880 }
881 )
882
883 expect(priceSetsResult).toEqual([
884 {
885 id: "price-set-PLN",
886 is_calculated_price_price_list: false,
887 is_calculated_price_tax_inclusive: false,
888 calculated_amount: 300,
889 raw_calculated_amount: {
890 value: "300",
891 precision: 20,
892 },
893 is_original_price_price_list: false,
894 is_original_price_tax_inclusive: false,
895 original_amount: 300,
896 raw_original_amount: {
897 value: "300",
898 precision: 20,
899 },
900 currency_code: "pln",
901 calculated_price: {
902 id: "price-region_id-PLN",
903 price_list_id: null,
904 price_list_type: null,
905 min_quantity: 1,
906 max_quantity: 4,
907 },
908 original_price: {
909 id: "price-region_id-PLN",
910 price_list_id: null,
911 price_list_type: null,
912 min_quantity: 1,
913 max_quantity: 4,
914 },
915 },
916 ])
917 })
918
919 it("should return filled prices when 3 contexts are present and price is setup for 3", async () => {
920 const priceSetsResult = await service.calculatePrices(
921 { id: ["price-set-EUR", "price-set-PLN"] },
922 {
923 context: {
924 currency_code: "eur",
925 region_id: "PL",
926 customer_group_id: "test-customer-group",
927 company_id: "medusa-company-id",
928 },
929 }
930 )
931
932 expect(priceSetsResult).toEqual([
933 // Currency Code + Region value + customer group id
934 {
935 id: "price-set-PLN",
936 is_calculated_price_price_list: false,
937 is_calculated_price_tax_inclusive: false,
938 calculated_amount: 100,
939 raw_calculated_amount: {
940 value: "100",
941 precision: 20,
942 },
943 is_original_price_price_list: false,
944 is_original_price_tax_inclusive: false,
945 original_amount: 100,
946 raw_original_amount: {
947 value: "100",
948 precision: 20,
949 },
950 currency_code: "eur",
951 calculated_price: {
952 id: "price-region_id_company_id-PL-EUR-customer-group",
953 price_list_id: null,
954 price_list_type: null,
955 min_quantity: 1,
956 max_quantity: 3,
957 },
958 original_price: {
959 id: "price-region_id_company_id-PL-EUR-customer-group",
960 price_list_id: null,
961 price_list_type: null,
962 min_quantity: 1,
963 max_quantity: 3,
964 },
965 },
966 ])
967 })
968
969 it("should return filled prices when 3 contexts are present and price is setup for 3, but value is incorrect for 2. It falls back to 1 rule context when 1 rule is not setup", async () => {
970 const priceSetsResult = await service.calculatePrices(
971 { id: ["price-set-EUR", "price-set-PLN"] },
972 {
973 context: {
974 currency_code: "pln",
975 region_id: "PL",
976 customer_group_id: "does-not-exist",
977 company_id: "does-not-exist",
978 },
979 }
980 )
981
982 expect(priceSetsResult).toEqual([
983 // PLN price set is not setup for EUR currency_code so it will default to a null set
984 {
985 id: "price-set-PLN",
986 is_calculated_price_price_list: false,
987 is_calculated_price_tax_inclusive: false,
988 calculated_amount: 300,
989 raw_calculated_amount: {
990 value: "300",
991 precision: 20,
992 },
993 is_original_price_price_list: false,
994 is_original_price_tax_inclusive: false,
995 original_amount: 300,
996 raw_original_amount: {
997 value: "300",
998 precision: 20,
999 },
1000 currency_code: "pln",
1001 calculated_price: {
1002 id: "price-region_id-PLN",
1003 price_list_id: null,
1004 price_list_type: null,
1005 min_quantity: 1,
1006 max_quantity: 4,
1007 },
1008 original_price: {
1009 id: "price-region_id-PLN",
1010 price_list_id: null,
1011 price_list_type: null,
1012 min_quantity: 1,
1013 max_quantity: 4,
1014 },
1015 },
1016 ])
1017 })
1018
1019 it("should return filled prices when 3 contexts are present and price is setup for 3, but value is incorrect for 2. It falls back to default value", async () => {
1020 const priceSetsResult = await service.calculatePrices(
1021 { id: ["price-set-EUR", "price-set-PLN"] },
1022 {
1023 context: {
1024 currency_code: "pln",
1025 region_id: "does-not-exist",
1026 customer_group_id: "does-not-exist",
1027 company_id: "does-not-exist",
1028 },
1029 }
1030 )
1031
1032 expect(priceSetsResult).toEqual([
1033 // PLN price set is not setup for EUR currency_code so it will default to a null set
1034 {
1035 id: "price-set-PLN",
1036 is_calculated_price_price_list: false,
1037 is_calculated_price_tax_inclusive: false,
1038 calculated_amount: 1000,
1039 raw_calculated_amount: {
1040 value: "1000",
1041 precision: 20,
1042 },
1043 is_original_price_price_list: false,
1044 is_original_price_tax_inclusive: false,
1045 original_amount: 1000,
1046 raw_original_amount: {
1047 value: "1000",
1048 precision: 20,
1049 },
1050 currency_code: "pln",
1051 calculated_price: {
1052 id: "price-PLN",
1053 price_list_id: null,
1054 price_list_type: null,
1055 min_quantity: 1,
1056 max_quantity: 10,
1057 },
1058 original_price: {
1059 id: "price-PLN",
1060 price_list_id: null,
1061 price_list_type: null,
1062 min_quantity: 1,
1063 max_quantity: 10,
1064 },
1065 },
1066 ])
1067 })
1068
1069 it("should return null prices when 2 context is present and prices are NOT setup", async () => {
1070 const priceSetsResult = await service.calculatePrices(
1071 { id: ["price-set-EUR", "price-set-PLN"] },
1072 {
1073 context: { currency_code: "eur", does_not_exist_2: "Berlin" },
1074 }
1075 )
1076
1077 expect(priceSetsResult).toEqual([])
1078 })
1079
1080 it("should return filled prices when 2 context is present and prices are setup, but only for one of the contexts", async () => {
1081 const priceSetsResult = await service.calculatePrices(
1082 { id: ["price-set-EUR", "price-set-PLN"] },
1083 {
1084 context: {
1085 currency_code: "pln",
1086 region_id: "PL",
1087 city: "Berlin",
1088 },
1089 }
1090 )
1091
1092 expect(priceSetsResult).toEqual([
1093 {
1094 id: "price-set-PLN",
1095 is_calculated_price_price_list: false,
1096 is_calculated_price_tax_inclusive: false,
1097 calculated_amount: 300,
1098 raw_calculated_amount: {
1099 value: "300",
1100 precision: 20,
1101 },
1102 is_original_price_price_list: false,
1103 is_original_price_tax_inclusive: false,
1104 original_amount: 300,
1105 raw_original_amount: {
1106 value: "300",
1107 precision: 20,
1108 },
1109 currency_code: "pln",
1110 calculated_price: {
1111 id: "price-region_id-PLN",
1112 price_list_id: null,
1113 price_list_type: null,
1114 min_quantity: 1,
1115 max_quantity: 4,
1116 },
1117 original_price: {
1118 id: "price-region_id-PLN",
1119 price_list_id: null,
1120 price_list_type: null,
1121 min_quantity: 1,
1122 max_quantity: 4,
1123 },
1124 },
1125 ])
1126 })
1127
1128 it("should return a price of 0", async () => {
1129 const priceSet = await service.createPriceSets({
1130 prices: [
1131 {
1132 amount: 0,
1133 currency_code: "usd",
1134 },
1135 ],
1136 })
1137
1138 const priceSetsResult = await service.calculatePrices(
1139 { id: [priceSet.id] },
1140 {
1141 context: {
1142 currency_code: "usd",
1143 },
1144 }
1145 )
1146
1147 expect(priceSetsResult).toEqual([
1148 {
1149 id: priceSet.id,
1150 is_calculated_price_price_list: false,
1151 is_calculated_price_tax_inclusive: false,
1152 calculated_amount: 0,
1153 raw_calculated_amount: {
1154 value: "0",
1155 precision: 20,
1156 },
1157 is_original_price_price_list: false,
1158 is_original_price_tax_inclusive: false,
1159 original_amount: 0,
1160 raw_original_amount: {
1161 value: "0",
1162 precision: 20,
1163 },
1164 currency_code: "usd",
1165 calculated_price: {
1166 id: expect.any(String),
1167 price_list_id: null,
1168 price_list_type: null,
1169 min_quantity: null,
1170 max_quantity: null,
1171 },
1172 original_price: {
1173 id: expect.any(String),
1174 price_list_id: null,
1175 price_list_type: null,
1176 min_quantity: null,
1177 max_quantity: null,
1178 },
1179 },
1180 ])
1181 })
1182
1183 describe("Price Lists", () => {
1184 it("should return price list prices when price list conditions match", async () => {
1185 await createPriceLists(service)
1186
1187 const priceSetsResult = await service.calculatePrices(
1188 { id: ["price-set-EUR", "price-set-PLN"] },
1189 {
1190 context: {
1191 currency_code: "pln",
1192 region_id: "DE",
1193 customer_group_id: "vip-customer-group-id",
1194 company_id: "medusa-company-id",
1195 },
1196 }
1197 )
1198
1199 expect(priceSetsResult).toEqual([
1200 {
1201 id: "price-set-PLN",
1202 is_calculated_price_price_list: true,
1203 is_calculated_price_tax_inclusive: false,
1204 calculated_amount: 232,
1205 raw_calculated_amount: {
1206 value: "232",
1207 precision: 20,
1208 },
1209 is_original_price_price_list: false,
1210 is_original_price_tax_inclusive: false,
1211 original_amount: 400,
1212 raw_original_amount: {
1213 value: "400",
1214 precision: 20,
1215 },
1216 currency_code: "pln",
1217 calculated_price: {
1218 id: expect.any(String),
1219 price_list_id: expect.any(String),
1220 price_list_type: "sale",
1221 min_quantity: null,
1222 max_quantity: null,
1223 },
1224 original_price: {
1225 id: expect.any(String),
1226 price_list_id: null,
1227 price_list_type: null,
1228 min_quantity: 1,
1229 max_quantity: 5,
1230 },
1231 },
1232 ])
1233 })
1234
1235 it("should return cheapest price list price first when price list conditions match", async () => {
1236 await createPriceLists(
1237 service,
1238 {
1239 title: "Test Price List One",
1240 description: "test description",
1241 type: PriceListType.OVERRIDE,
1242 status: PriceListStatus.ACTIVE,
1243 },
1244 {},
1245 defaultPriceListPrices
1246 )
1247
1248 await createPriceLists(
1249 service,
1250 {
1251 title: "Test Price List Two",
1252 description: "test description",
1253 type: PriceListType.OVERRIDE,
1254 status: PriceListStatus.ACTIVE,
1255 },
1256 {},
1257 defaultPriceListPrices.map((price) => {
1258 return { ...price, amount: price.amount / 2 }
1259 })
1260 )
1261
1262 const priceSetsResult = await service.calculatePrices(
1263 { id: ["price-set-PLN"] },
1264 {
1265 context: {
1266 currency_code: "pln",
1267 region_id: "DE",
1268 customer_group_id: "vip-customer-group-id",
1269 company_id: "medusa-company-id",
1270 },
1271 }
1272 )
1273
1274 expect(priceSetsResult).toEqual([
1275 {
1276 id: "price-set-PLN",
1277 is_calculated_price_price_list: true,
1278 is_calculated_price_tax_inclusive: false,
1279 calculated_amount: 116,
1280 raw_calculated_amount: {
1281 value: "116",
1282 precision: 20,
1283 },
1284 is_original_price_price_list: true,
1285 is_original_price_tax_inclusive: false,
1286 original_amount: 116,
1287 raw_original_amount: {
1288 value: "116",
1289 precision: 20,
1290 },
1291 currency_code: "pln",
1292 calculated_price: {
1293 id: expect.any(String),
1294 price_list_id: expect.any(String),
1295 price_list_type: "override",
1296 min_quantity: null,
1297 max_quantity: null,
1298 },
1299 original_price: {
1300 id: expect.any(String),
1301 price_list_id: expect.any(String),
1302 price_list_type: "override",
1303 min_quantity: null,
1304 max_quantity: null,
1305 },
1306 },
1307 ])
1308 })
1309
1310 it("should return the override as original_amount when a sale is stacked on an override for the same customer group", async () => {
1311 // Customer's real price comes from an OVERRIDE price list (500),
1312 // and a SALE price list (300) is stacked on top for the same group.
1313 await createPriceLists(
1314 service,
1315 {
1316 title: "Group Override",
1317 description: "test description",
1318 type: PriceListType.OVERRIDE,
1319 status: PriceListStatus.ACTIVE,
1320 },
1321 defaultRules,
1322 [
1323 {
1324 amount: 500,
1325 currency_code: "pln",
1326 price_set_id: "price-set-PLN",
1327 },
1328 ]
1329 )
1330
1331 await createPriceLists(
1332 service,
1333 {
1334 title: "Group Sale",
1335 description: "test description",
1336 type: PriceListType.SALE,
1337 status: PriceListStatus.ACTIVE,
1338 },
1339 defaultRules,
1340 [
1341 {
1342 amount: 300,
1343 currency_code: "pln",
1344 price_set_id: "price-set-PLN",
1345 },
1346 ]
1347 )
1348
1349 const priceSetsResult = await service.calculatePrices(
1350 { id: ["price-set-PLN"] },
1351 {
1352 context: {
1353 currency_code: "pln",
1354 region_id: "DE",
1355 customer_group_id: "vip-customer-group-id",
1356 },
1357 }
1358 )
1359
1360 expect(priceSetsResult).toEqual([
1361 {
1362 id: "price-set-PLN",
1363 is_calculated_price_price_list: true,
1364 is_calculated_price_tax_inclusive: false,
1365 calculated_amount: 300,
1366 raw_calculated_amount: {
1367 value: "300",
1368 precision: 20,
1369 },
1370 is_original_price_price_list: true,
1371 is_original_price_tax_inclusive: false,
1372 original_amount: 500,
1373 raw_original_amount: {
1374 value: "500",
1375 precision: 20,
1376 },
1377 currency_code: "pln",
1378 calculated_price: {
1379 id: expect.any(String),
1380 price_list_id: expect.any(String),
1381 price_list_type: "sale",
1382 min_quantity: null,
1383 max_quantity: null,
1384 },
1385 original_price: {
1386 id: expect.any(String),
1387 price_list_id: expect.any(String),
1388 price_list_type: "override",
1389 min_quantity: null,
1390 max_quantity: null,
1391 },
1392 },
1393 ])
1394 })
1395
1396 it("should return price list prices when price list dont have rules, but context is loaded", async () => {
1397 await createPriceLists(service, {}, {})
1398
1399 const priceSetsResult = await service.calculatePrices(
1400 { id: ["price-set-EUR", "price-set-PLN"] },
1401 {
1402 context: {
1403 currency_code: "pln",
1404 region_id: "DE",
1405 customer_group_id: "vip-customer-group-id",
1406 company_id: "medusa-company-id",
1407 },
1408 }
1409 )
1410
1411 expect(priceSetsResult).toEqual([
1412 {
1413 id: "price-set-PLN",
1414 is_calculated_price_price_list: true,
1415 is_calculated_price_tax_inclusive: false,
1416 calculated_amount: 232,
1417 raw_calculated_amount: {
1418 value: "232",
1419 precision: 20,
1420 },
1421 is_original_price_price_list: false,
1422 is_original_price_tax_inclusive: false,
1423 original_amount: 400,
1424 raw_original_amount: {
1425 value: "400",
1426 precision: 20,
1427 },
1428 currency_code: "pln",
1429 calculated_price: {
1430 id: expect.any(String),
1431 price_list_id: expect.any(String),
1432 price_list_type: "sale",
1433 min_quantity: null,
1434 max_quantity: null,
1435 },
1436 original_price: {
1437 id: expect.any(String),
1438 price_list_id: null,
1439 price_list_type: null,
1440 min_quantity: 1,
1441 max_quantity: 5,
1442 },
1443 },
1444 ])
1445 })
1446
1447 it("should not return price list prices when price list have rules but no context is loaded", async() => {
1448 await createPriceLists(service)
1449
1450 const priceSetsResult = await service.calculatePrices(
1451 { id: ["price-set-EUR", "price-set-PLN"] },
1452 {
1453 context: {
1454 currency_code: "pln",
1455 },
1456 }
1457 )
1458
1459 expect(priceSetsResult).toEqual([
1460 {
1461 id: "price-set-PLN",
1462 is_calculated_price_price_list: false,
1463 is_calculated_price_tax_inclusive: false,
1464 calculated_amount: 1000,
1465 raw_calculated_amount: {
1466 value: "1000",
1467 precision: 20,
1468 },
1469 is_original_price_price_list: false,
1470 is_original_price_tax_inclusive: false,
1471 original_amount: 1000,
1472 raw_original_amount: {
1473 value: "1000",
1474 precision: 20,
1475 },
1476 currency_code: "pln",
1477 calculated_price: {
1478 id: "price-PLN",
1479 price_list_id: null,
1480 price_list_type: null,
1481 min_quantity: 1,
1482 max_quantity: 10,
1483 },
1484 original_price: {
1485 id: "price-PLN",
1486 price_list_id: null,
1487 price_list_type: null,
1488 min_quantity: 1,
1489 max_quantity: 10,
1490 },
1491 }
1492 ])
1493 })
1494
1495 it("should return price list prices when price list dont have any rules", async () => {
1496 await createPriceLists(service, {}, {})
1497
1498 const priceSetsResult = await service.calculatePrices(
1499 { id: ["price-set-EUR", "price-set-PLN"] },
1500 {
1501 context: {
1502 currency_code: "pln",
1503 },
1504 }
1505 )
1506
1507 expect(priceSetsResult).toEqual([
1508 {
1509 id: "price-set-PLN",
1510 is_calculated_price_price_list: true,
1511 is_calculated_price_tax_inclusive: false,
1512 calculated_amount: 232,
1513 raw_calculated_amount: {
1514 value: "232",
1515 precision: 20,
1516 },
1517 is_original_price_price_list: false,
1518 is_original_price_tax_inclusive: false,
1519 original_amount: 1000,
1520 raw_original_amount: {
1521 value: "1000",
1522 precision: 20,
1523 },
1524 currency_code: "pln",
1525 calculated_price: {
1526 id: expect.any(String),
1527 price_list_id: expect.any(String),
1528 price_list_type: "sale",
1529 min_quantity: null,
1530 max_quantity: null,
1531 },
1532 original_price: {
1533 id: expect.any(String),
1534 price_list_id: null,
1535 price_list_type: null,
1536 min_quantity: 1,
1537 max_quantity: 10,
1538 },
1539 },
1540 ])
1541 })
1542
1543 it("should return default prices when the price list price is higher than the default price when the price list is of type SALE", async () => {
1544 await createPriceLists(service, undefined, undefined, [
1545 {
1546 amount: 2500,
1547 currency_code: "pln",
1548 price_set_id: "price-set-PLN",
1549 },
1550 {
1551 amount: 2500,
1552 currency_code: "eur",
1553 price_set_id: "price-set-EUR",
1554 },
1555 ])
1556
1557 const priceSetsResult = await service.calculatePrices(
1558 { id: ["price-set-EUR", "price-set-PLN"] },
1559 {
1560 context: {
1561 currency_code: "pln",
1562 },
1563 }
1564 )
1565
1566 expect(priceSetsResult).toEqual([
1567 {
1568 id: "price-set-PLN",
1569 is_calculated_price_price_list: false,
1570 is_calculated_price_tax_inclusive: false,
1571 calculated_amount: 1000,
1572 raw_calculated_amount: {
1573 value: "1000",
1574 precision: 20,
1575 },
1576 is_original_price_price_list: false,
1577 is_original_price_tax_inclusive: false,
1578 original_amount: 1000,
1579 raw_original_amount: {
1580 value: "1000",
1581 precision: 20,
1582 },
1583 currency_code: "pln",
1584 calculated_price: {
1585 id: expect.any(String),
1586 price_list_id: null,
1587 price_list_type: null,
1588 min_quantity: 1,
1589 max_quantity: 10,
1590 },
1591 original_price: {
1592 id: expect.any(String),
1593 price_list_id: null,
1594 price_list_type: null,
1595 min_quantity: 1,
1596 max_quantity: 10,
1597 },
1598 },
1599 ])
1600 })
1601
1602 it("should return price list prices even if the price list price is higher than the default price when the price list is of type OVERRIDE", async () => {
1603 await createPriceLists(
1604 service,
1605 { type: PriceListType.OVERRIDE },
1606 {},
1607 [
1608 {
1609 amount: 2500,
1610 currency_code: "pln",
1611 price_set_id: "price-set-PLN",
1612 },
1613 {
1614 amount: 2500,
1615 currency_code: "eur",
1616 price_set_id: "price-set-EUR",
1617 },
1618 ]
1619 )
1620
1621 const priceSetsResult = await service.calculatePrices(
1622 { id: ["price-set-EUR", "price-set-PLN"] },
1623 {
1624 context: {
1625 currency_code: "pln",
1626 },
1627 }
1628 )
1629
1630 expect(priceSetsResult).toEqual([
1631 {
1632 id: "price-set-PLN",
1633 is_calculated_price_price_list: true,
1634 is_calculated_price_tax_inclusive: false,
1635 calculated_amount: 2500,
1636 raw_calculated_amount: {
1637 value: "2500",
1638 precision: 20,
1639 },
1640 is_original_price_price_list: true,
1641 is_original_price_tax_inclusive: false,
1642 original_amount: 2500,
1643 raw_original_amount: {
1644 value: "2500",
1645 precision: 20,
1646 },
1647 currency_code: "pln",
1648 calculated_price: {
1649 id: expect.any(String),
1650 price_list_id: expect.any(String),
1651 price_list_type: "override",
1652 min_quantity: null,
1653 max_quantity: null,
1654 },
1655 original_price: {
1656 id: expect.any(String),
1657 price_list_id: expect.any(String),
1658 price_list_type: "override",
1659 min_quantity: null,
1660 max_quantity: null,
1661 },
1662 },
1663 ])
1664 })
1665
1666 it("should return price list prices when price list conditions match for override", async () => {
1667 await createPriceLists(service, { type: PriceListType.OVERRIDE })
1668
1669 const priceSetsResult = await service.calculatePrices(
1670 { id: ["price-set-EUR", "price-set-PLN"] },
1671 {
1672 context: {
1673 currency_code: "pln",
1674 region_id: "DE",
1675 customer_group_id: "vip-customer-group-id",
1676 company_id: "medusa-company-id",
1677 },
1678 }
1679 )
1680
1681 expect(priceSetsResult).toEqual([
1682 {
1683 id: "price-set-PLN",
1684 is_calculated_price_price_list: true,
1685 is_calculated_price_tax_inclusive: false,
1686 calculated_amount: 232,
1687 raw_calculated_amount: {
1688 value: "232",
1689 precision: 20,
1690 },
1691 is_original_price_price_list: true,
1692 is_original_price_tax_inclusive: false,
1693 original_amount: 232,
1694 raw_original_amount: {
1695 value: "232",
1696 precision: 20,
1697 },
1698 currency_code: "pln",
1699 calculated_price: {
1700 id: expect.any(String),
1701 price_list_id: expect.any(String),
1702 price_list_type: "override",
1703 min_quantity: null,
1704 max_quantity: null,
1705 },
1706 original_price: {
1707 id: expect.any(String),
1708 price_list_id: expect.any(String),
1709 price_list_type: "override",
1710 min_quantity: null,
1711 max_quantity: null,
1712 },
1713 },
1714 ])
1715 })
1716
1717 it("should not return price list prices when price list conditions only partially match", async () => {
1718 await createPriceLists(service)
1719 const priceSetsResult = await service.calculatePrices(
1720 { id: ["price-set-EUR", "price-set-PLN"] },
1721 {
1722 context: {
1723 currency_code: "pln",
1724 region_id: "PL",
1725 customer_group_id: "vip-customer-group-id",
1726 company_id: "does-not-exist",
1727 },
1728 }
1729 )
1730
1731 expect(priceSetsResult).toEqual([
1732 {
1733 id: "price-set-PLN",
1734 is_calculated_price_price_list: false,
1735 is_calculated_price_tax_inclusive: false,
1736 calculated_amount: 300,
1737 raw_calculated_amount: {
1738 value: "300",
1739 precision: 20,
1740 },
1741 is_original_price_price_list: false,
1742 is_original_price_tax_inclusive: false,
1743 original_amount: 300,
1744 raw_original_amount: {
1745 value: "300",
1746 precision: 20,
1747 },
1748 currency_code: "pln",
1749 calculated_price: {
1750 id: "price-region_id-PLN",
1751 price_list_id: null,
1752 price_list_type: null,
1753 min_quantity: 1,
1754 max_quantity: 4,
1755 },
1756 original_price: {
1757 id: "price-region_id-PLN",
1758 price_list_id: null,
1759 price_list_type: null,
1760 min_quantity: 1,
1761 max_quantity: 4,
1762 },
1763 },
1764 ])
1765 })
1766
1767 it("should not return price list prices when price list conditions dont match", async () => {
1768 await createPriceLists(service)
1769 const priceSetsResult = await service.calculatePrices(
1770 { id: ["price-set-EUR", "price-set-PLN"] },
1771 {
1772 context: {
1773 currency_code: "pln",
1774 region_id: "PL",
1775 customer_group_id: "does-not-exist",
1776 company_id: "does-not-exist",
1777 },
1778 }
1779 )
1780
1781 expect(priceSetsResult).toEqual([
1782 {
1783 id: "price-set-PLN",
1784 is_calculated_price_price_list: false,
1785 is_calculated_price_tax_inclusive: false,
1786 calculated_amount: 300,
1787 raw_calculated_amount: {
1788 value: "300",
1789 precision: 20,
1790 },
1791 is_original_price_price_list: false,
1792 is_original_price_tax_inclusive: false,
1793 original_amount: 300,
1794 raw_original_amount: {
1795 value: "300",
1796 precision: 20,
1797 },
1798 currency_code: "pln",
1799 calculated_price: {
1800 id: expect.any(String),
1801 price_list_id: null,
1802 price_list_type: null,
1803 min_quantity: 1,
1804 max_quantity: 4,
1805 },
1806 original_price: {
1807 id: expect.any(String),
1808 price_list_id: null,
1809 price_list_type: null,
1810 min_quantity: 1,
1811 max_quantity: 4,
1812 },
1813 },
1814 ])
1815 })
1816
1817 it("should return price list prices for pricelist with valid pricing interval", async () => {
1818 const yesterday = ((today) =>
1819 new Date(today.setDate(today.getDate() - 1)))(new Date())
1820 const tomorrow = ((today) =>
1821 new Date(today.setDate(today.getDate() + 1)))(new Date())
1822
1823 await createPriceLists(
1824 service,
1825 {
1826 starts_at: yesterday,
1827 ends_at: tomorrow,
1828 },
1829 {}
1830 )
1831
1832 const priceSetsResult = await service.calculatePrices(
1833 { id: ["price-set-EUR", "price-set-PLN"] },
1834 {
1835 context: {
1836 currency_code: "pln",
1837 region_id: "DE",
1838 customer_group_id: "vip-customer-group-id",
1839 company_id: "medusa-company-id",
1840 },
1841 }
1842 )
1843
1844 expect(priceSetsResult).toEqual([
1845 {
1846 id: "price-set-PLN",
1847 is_calculated_price_price_list: true,
1848 is_calculated_price_tax_inclusive: false,
1849 calculated_amount: 232,
1850 raw_calculated_amount: {
1851 value: "232",
1852 precision: 20,
1853 },
1854 is_original_price_price_list: false,
1855 is_original_price_tax_inclusive: false,
1856 original_amount: 400,
1857 raw_original_amount: {
1858 value: "400",
1859 precision: 20,
1860 },
1861 currency_code: "pln",
1862 calculated_price: {
1863 id: expect.any(String),
1864 price_list_id: expect.any(String),
1865 price_list_type: "sale",
1866 min_quantity: null,
1867 max_quantity: null,
1868 },
1869 original_price: {
1870 id: expect.any(String),
1871 price_list_id: null,
1872 price_list_type: null,
1873 min_quantity: 1,
1874 max_quantity: 5,
1875 },
1876 },
1877 ])
1878 })
1879
1880 it("should not return price list prices for upcoming pricelist", async () => {
1881 const tomorrow = ((today) =>
1882 new Date(today.setDate(today.getDate() + 1)))(new Date())
1883
1884 const tenDaysFromToday = ((today) =>
1885 new Date(today.setDate(today.getDate() + 10)))(new Date())
1886
1887 await createPriceLists(
1888 service,
1889 {
1890 starts_at: tomorrow,
1891 ends_at: tenDaysFromToday,
1892 },
1893 {}
1894 )
1895
1896 const priceSetsResult = await service.calculatePrices(
1897 { id: ["price-set-EUR", "price-set-PLN"] },
1898 {
1899 context: {
1900 currency_code: "pln",
1901 region_id: "DE",
1902 customer_group_id: "vip-customer-group-id",
1903 company_id: "medusa-company-id",
1904 },
1905 }
1906 )
1907
1908 expect(priceSetsResult).toEqual([
1909 {
1910 id: "price-set-PLN",
1911 is_calculated_price_price_list: false,
1912 is_calculated_price_tax_inclusive: false,
1913 calculated_amount: 400,
1914 raw_calculated_amount: {
1915 value: "400",
1916 precision: 20,
1917 },
1918 is_original_price_price_list: false,
1919 is_original_price_tax_inclusive: false,
1920 original_amount: 400,
1921 raw_original_amount: {
1922 value: "400",
1923 precision: 20,
1924 },
1925 currency_code: "pln",
1926 calculated_price: {
1927 id: "price-company_id-PLN",
1928 price_list_id: null,
1929 price_list_type: null,
1930 min_quantity: 1,
1931 max_quantity: 5,
1932 },
1933 original_price: {
1934 id: "price-company_id-PLN",
1935 price_list_id: null,
1936 price_list_type: null,
1937 min_quantity: 1,
1938 max_quantity: 5,
1939 },
1940 },
1941 ])
1942 })
1943
1944 it("should not return price list prices for expired pricelist", async () => {
1945 const yesterday = ((today) =>
1946 new Date(today.setDate(today.getDate() - 1)))(new Date())
1947 const tenDaysAgo = ((today) =>
1948 new Date(today.setDate(today.getDate() - 10)))(new Date())
1949
1950 await createPriceLists(
1951 service,
1952 {
1953 starts_at: tenDaysAgo,
1954 ends_at: yesterday,
1955 },
1956 {}
1957 )
1958
1959 const priceSetsResult = await service.calculatePrices(
1960 { id: ["price-set-EUR", "price-set-PLN"] },
1961 {
1962 context: {
1963 currency_code: "pln",
1964 region_id: "DE",
1965 customer_group_id: "vip-customer-group-id",
1966 company_id: "medusa-company-id",
1967 },
1968 }
1969 )
1970
1971 expect(priceSetsResult).toEqual([
1972 {
1973 id: "price-set-PLN",
1974 is_calculated_price_price_list: false,
1975 is_calculated_price_tax_inclusive: false,
1976 calculated_amount: 400,
1977 raw_calculated_amount: {
1978 value: "400",
1979 precision: 20,
1980 },
1981 is_original_price_price_list: false,
1982 is_original_price_tax_inclusive: false,
1983 original_amount: 400,
1984 raw_original_amount: {
1985 value: "400",
1986 precision: 20,
1987 },
1988 currency_code: "pln",
1989 calculated_price: {
1990 id: "price-company_id-PLN",
1991 price_list_id: null,
1992 price_list_type: null,
1993 min_quantity: 1,
1994 max_quantity: 5,
1995 },
1996 original_price: {
1997 id: "price-company_id-PLN",
1998 price_list_id: null,
1999 price_list_type: null,
2000 min_quantity: 1,
2001 max_quantity: 5,
2002 },
2003 },
2004 ])
2005 })
2006
2007 it("should return price list prices for price list with customer groups", async () => {
2008 const [{ id }] = await createPriceLists(
2009 service,
2010 {},
2011 {
2012 customer_group_id: [
2013 "vip-customer-group-id",
2014 "vip-customer-group-id-1",
2015 ],
2016 },
2017 [
2018 {
2019 amount: 200,
2020 currency_code: "eur",
2021 price_set_id: "price-set-EUR",
2022 },
2023 ]
2024 )
2025
2026 const priceSetsResult = await service.calculatePrices(
2027 { id: ["price-set-EUR"] },
2028 {
2029 context: {
2030 currency_code: "eur",
2031 customer_group_id: "vip-customer-group-id",
2032 },
2033 }
2034 )
2035
2036 expect(priceSetsResult).toEqual([
2037 {
2038 id: "price-set-EUR",
2039 is_calculated_price_price_list: true,
2040 is_calculated_price_tax_inclusive: false,
2041 calculated_amount: 200,
2042 raw_calculated_amount: {
2043 value: "200",
2044 precision: 20,
2045 },
2046 is_original_price_price_list: false,
2047 is_original_price_tax_inclusive: false,
2048 original_amount: null,
2049 raw_original_amount: null,
2050 currency_code: "eur",
2051 calculated_price: {
2052 id: expect.any(String),
2053 price_list_id: id,
2054 price_list_type: "sale",
2055 min_quantity: null,
2056 max_quantity: null,
2057 },
2058 original_price: {
2059 id: null,
2060 price_list_id: null,
2061 price_list_type: null,
2062 min_quantity: null,
2063 max_quantity: null,
2064 },
2065 },
2066 ])
2067 })
2068
2069 it("should return price list prices when price list conditions match within prices", async () => {
2070 await createPriceLists(service, {}, { region_id: ["DE", "PL"] }, [
2071 ...defaultPriceListPrices,
2072 {
2073 amount: 111,
2074 currency_code: "pln",
2075 price_set_id: "price-set-PLN",
2076 rules: {
2077 region_id: "DE",
2078 },
2079 },
2080 ])
2081
2082 const priceSetsResult = await service.calculatePrices(
2083 { id: ["price-set-EUR", "price-set-PLN"] },
2084 {
2085 context: {
2086 currency_code: "pln",
2087 region_id: "DE",
2088 customer_group_id: "vip-customer-group-id",
2089 company_id: "medusa-company-id",
2090 },
2091 }
2092 )
2093
2094 expect(priceSetsResult).toEqual([
2095 {
2096 id: "price-set-PLN",
2097 is_calculated_price_price_list: true,
2098 is_calculated_price_tax_inclusive: false,
2099 calculated_amount: 111,
2100 raw_calculated_amount: {
2101 value: "111",
2102 precision: 20,
2103 },
2104 is_original_price_price_list: false,
2105 is_original_price_tax_inclusive: false,
2106 original_amount: 400,
2107 raw_original_amount: {
2108 value: "400",
2109 precision: 20,
2110 },
2111 currency_code: "pln",
2112 calculated_price: {
2113 id: expect.any(String),
2114 price_list_id: expect.any(String),
2115 price_list_type: "sale",
2116 min_quantity: null,
2117 max_quantity: null,
2118 },
2119 original_price: {
2120 id: expect.any(String),
2121 price_list_id: null,
2122 price_list_type: null,
2123 min_quantity: 1,
2124 max_quantity: 5,
2125 },
2126 },
2127 ])
2128 })
2129
2130 it("should return price list prices for multiple price lists with customer groups", async () => {
2131 const [{ id }] = await createPriceLists(
2132 service,
2133 { type: "override" },
2134 {
2135 ["customer.groups.id"]: ["vip-customer-group-id"],
2136 },
2137 [
2138 {
2139 amount: 600,
2140 currency_code: "eur",
2141 price_set_id: "price-set-EUR",
2142 },
2143 ]
2144 )
2145
2146 const [{ id: idTwo }] = await createPriceLists(
2147 service,
2148 { type: "override" },
2149 {
2150 ["customer.groups.id"]: ["vip-customer-group-id-1"],
2151 },
2152 [
2153 {
2154 amount: 400,
2155 currency_code: "eur",
2156 price_set_id: "price-set-EUR",
2157 },
2158 ]
2159 )
2160
2161 const priceSetsResult = await service.calculatePrices(
2162 { id: ["price-set-EUR"] },
2163 {
2164 context: {
2165 currency_code: "eur",
2166 // @ts-ignore
2167 customer: {
2168 groups: {
2169 id: ["vip-customer-group-id", "vip-customer-group-id-1"],
2170 },
2171 },
2172 },
2173 }
2174 )
2175
2176 expect(priceSetsResult).toEqual([
2177 {
2178 id: "price-set-EUR",
2179 is_calculated_price_price_list: true,
2180 is_calculated_price_tax_inclusive: false,
2181 calculated_amount: 400,
2182 raw_calculated_amount: {
2183 value: "400",
2184 precision: 20,
2185 },
2186 is_original_price_price_list: true,
2187 is_original_price_tax_inclusive: false,
2188 original_amount: 400,
2189 raw_original_amount: {
2190 value: "400",
2191 precision: 20,
2192 },
2193 currency_code: "eur",
2194 calculated_price: {
2195 id: expect.any(String),
2196 price_list_id: idTwo,
2197 price_list_type: "override",
2198 min_quantity: null,
2199 max_quantity: null,
2200 },
2201 original_price: {
2202 id: expect.any(String),
2203 price_list_id: idTwo,
2204 price_list_type: "override",
2205 min_quantity: null,
2206 max_quantity: null,
2207 },
2208 },
2209 ])
2210 })
2211
2212 it("should return price list prices when price list conditions match within prices", async () => {
2213 await createPriceLists(service, {}, { region_id: ["DE", "PL"] }, [
2214 ...defaultPriceListPrices,
2215 {
2216 amount: 111,
2217 currency_code: "pln",
2218 price_set_id: "price-set-PLN",
2219 rules: {
2220 region_id: "DE",
2221 },
2222 },
2223 ])
2224
2225 const priceSetsResult = await service.calculatePrices(
2226 { id: ["price-set-EUR", "price-set-PLN"] },
2227 {
2228 context: {
2229 currency_code: "pln",
2230 region_id: "DE",
2231 customer_group_id: "vip-customer-group-id",
2232 company_id: "medusa-company-id",
2233 },
2234 }
2235 )
2236
2237 expect(priceSetsResult).toEqual([
2238 {
2239 id: "price-set-PLN",
2240 is_calculated_price_price_list: true,
2241 is_calculated_price_tax_inclusive: false,
2242 calculated_amount: 111,
2243 raw_calculated_amount: {
2244 value: "111",
2245 precision: 20,
2246 },
2247 is_original_price_price_list: false,
2248 is_original_price_tax_inclusive: false,
2249 original_amount: 400,
2250 raw_original_amount: {
2251 value: "400",
2252 precision: 20,
2253 },
2254 currency_code: "pln",
2255 calculated_price: {
2256 id: expect.any(String),
2257 price_list_id: expect.any(String),
2258 price_list_type: "sale",
2259 min_quantity: null,
2260 max_quantity: null,
2261 },
2262 original_price: {
2263 id: expect.any(String),
2264 price_list_id: null,
2265 price_list_type: null,
2266 min_quantity: 1,
2267 max_quantity: 5,
2268 },
2269 },
2270 ])
2271 })
2272
2273 it("should not return price list prices when price list conditions are met but price rules are not", async () => {
2274 await createPriceLists(service, {}, { region_id: ["DE", "PL"] }, [
2275 ...defaultPriceListPrices,
2276 {
2277 amount: 111,
2278 currency_code: "pln",
2279 price_set_id: "price-set-PLN",
2280 rules: {
2281 region_id: "PL",
2282 },
2283 },
2284 ])
2285
2286 const priceSetsResult = await service.calculatePrices(
2287 { id: ["price-set-EUR", "price-set-PLN"] },
2288 {
2289 context: {
2290 currency_code: "pln",
2291 region_id: "DE",
2292 customer_group_id: "vip-customer-group-id",
2293 company_id: "medusa-company-id",
2294 },
2295 }
2296 )
2297
2298 expect(priceSetsResult).toEqual([
2299 {
2300 id: "price-set-PLN",
2301 is_calculated_price_price_list: true,
2302 is_calculated_price_tax_inclusive: false,
2303 calculated_amount: 232,
2304 raw_calculated_amount: {
2305 value: "232",
2306 precision: 20,
2307 },
2308 is_original_price_price_list: false,
2309 is_original_price_tax_inclusive: false,
2310 original_amount: 400,
2311 raw_original_amount: {
2312 value: "400",
2313 precision: 20,
2314 },
2315 currency_code: "pln",
2316 calculated_price: {
2317 id: expect.any(String),
2318 price_list_id: expect.any(String),
2319 price_list_type: "sale",
2320 min_quantity: null,
2321 max_quantity: null,
2322 },
2323 original_price: {
2324 id: expect.any(String),
2325 price_list_id: null,
2326 price_list_type: null,
2327 min_quantity: 1,
2328 max_quantity: 5,
2329 },
2330 },
2331 ])
2332 })
2333
2334 it("should not return price list prices when price is deleted", async () => {
2335 const [priceList] = await createPriceLists(
2336 service,
2337 {},
2338 { region_id: ["DE", "PL"] },
2339 [
2340 {
2341 amount: 111,
2342 currency_code: "pln",
2343 price_set_id: "price-set-PLN",
2344 rules: {
2345 region_id: "DE",
2346 },
2347 },
2348 ]
2349 )
2350
2351 const priceSetsResult1 = await service.calculatePrices(
2352 { id: ["price-set-EUR", "price-set-PLN"] },
2353 {
2354 context: {
2355 currency_code: "pln",
2356 region_id: "DE",
2357 customer_group_id: "vip-customer-group-id",
2358 company_id: "medusa-company-id",
2359 },
2360 }
2361 )
2362
2363 expect(priceSetsResult1).toEqual([
2364 expect.objectContaining({
2365 id: "price-set-PLN",
2366 is_calculated_price_price_list: true,
2367 calculated_amount: 111,
2368 is_original_price_price_list: false,
2369 original_amount: 400,
2370 }),
2371 ])
2372
2373 await service.softDeletePrices(priceList.prices.map((p) => p.id))
2374
2375 const priceSetsResult2 = await service.calculatePrices(
2376 { id: ["price-set-EUR", "price-set-PLN"] },
2377 {
2378 context: {
2379 currency_code: "pln",
2380 region_id: "DE",
2381 customer_group_id: "vip-customer-group-id",
2382 company_id: "medusa-company-id",
2383 },
2384 }
2385 )
2386
2387 expect(priceSetsResult2).toEqual([
2388 expect.objectContaining({
2389 id: "price-set-PLN",
2390 is_calculated_price_price_list: false,
2391 calculated_amount: 400,
2392 is_original_price_price_list: false,
2393 original_amount: 400,
2394 }),
2395 ])
2396 })
2397
2398 it("should only return the matching price list price when many price lists with rules exist", async () => {
2399 // B2B pattern: one price list per customer group, one price per
2400 // variant. Non-matching lists are cheaper on purpose: if any of
2401 // them leaked into the results, the cheapest-first ordering would
2402 // surface it and the assertion would fail.
2403 await service.createPriceLists(
2404 Array.from({ length: 49 }, (_, i) => ({
2405 title: `Non matching price list ${i}`,
2406 description: "test description",
2407 type: PriceListType.SALE,
2408 status: PriceListStatus.ACTIVE,
2409 rules: { ["customer.groups.id"]: [`other-group-${i}`] },
2410 prices: [
2411 {
2412 amount: 50,
2413 currency_code: "eur",
2414 price_set_id: "price-set-EUR",
2415 },
2416 ],
2417 }))
2418 )
2419
2420 const [{ id: matchingPriceListId }] = await createPriceLists(
2421 service,
2422 { title: "Matching price list" },
2423 { ["customer.groups.id"]: ["target-group"] },
2424 [
2425 {
2426 amount: 125,
2427 currency_code: "eur",
2428 price_set_id: "price-set-EUR",
2429 },
2430 ]
2431 )
2432
2433 const priceSetsResult = await service.calculatePrices(
2434 { id: ["price-set-EUR"] },
2435 {
2436 context: {
2437 currency_code: "eur",
2438 // @ts-ignore
2439 customer: { groups: { id: ["target-group"] } },
2440 },
2441 }
2442 )
2443
2444 expect(priceSetsResult).toEqual([
2445 {
2446 id: "price-set-EUR",
2447 is_calculated_price_price_list: true,
2448 is_calculated_price_tax_inclusive: false,
2449 calculated_amount: 125,
2450 raw_calculated_amount: {
2451 value: "125",
2452 precision: 20,
2453 },
2454 is_original_price_price_list: false,
2455 is_original_price_tax_inclusive: false,
2456 original_amount: null,
2457 raw_original_amount: null,
2458 currency_code: "eur",
2459 calculated_price: {
2460 id: expect.any(String),
2461 price_list_id: matchingPriceListId,
2462 price_list_type: "sale",
2463 min_quantity: null,
2464 max_quantity: null,
2465 },
2466 original_price: {
2467 id: null,
2468 price_list_id: null,
2469 price_list_type: null,
2470 min_quantity: null,
2471 max_quantity: null,
2472 },
2473 },
2474 ])
2475 })
2476
2477 it("should not return price list prices when only some of the price list rules match", async () => {
2478 await createPriceLists(
2479 service,
2480 {},
2481 {
2482 customer_group_id: ["vip-customer-group-id"],
2483 region_id: ["DE"],
2484 },
2485 [
2486 {
2487 amount: 232,
2488 currency_code: "pln",
2489 price_set_id: "price-set-PLN",
2490 },
2491 ]
2492 )
2493
2494 const priceSetsResult = await service.calculatePrices(
2495 { id: ["price-set-PLN"] },
2496 {
2497 context: {
2498 currency_code: "pln",
2499 customer_group_id: "vip-customer-group-id",
2500 region_id: "FR",
2501 },
2502 }
2503 )
2504
2505 expect(priceSetsResult).toEqual([
2506 expect.objectContaining({
2507 id: "price-set-PLN",
2508 is_calculated_price_price_list: false,
2509 calculated_amount: 1000,
2510 is_original_price_price_list: false,
2511 original_amount: 1000,
2512 calculated_price: expect.objectContaining({
2513 id: "price-PLN",
2514 price_list_id: null,
2515 }),
2516 }),
2517 ])
2518 })
2519
2520 it("should not return price list prices for expired or inactive price lists even when their rules match", async () => {
2521 const yesterday = ((today) =>
2522 new Date(today.setDate(today.getDate() - 1)))(new Date())
2523 const tenDaysAgo = ((today) =>
2524 new Date(today.setDate(today.getDate() - 10)))(new Date())
2525
2526 await createPriceLists(
2527 service,
2528 {
2529 title: "Expired price list",
2530 starts_at: tenDaysAgo.toISOString(),
2531 ends_at: yesterday.toISOString(),
2532 },
2533 { customer_group_id: ["vip-customer-group-id"] },
2534 [
2535 {
2536 amount: 111,
2537 currency_code: "pln",
2538 price_set_id: "price-set-PLN",
2539 },
2540 ]
2541 )
2542
2543 await createPriceLists(
2544 service,
2545 {
2546 title: "Draft price list",
2547 status: PriceListStatus.DRAFT,
2548 },
2549 { customer_group_id: ["vip-customer-group-id"] },
2550 [
2551 {
2552 amount: 222,
2553 currency_code: "pln",
2554 price_set_id: "price-set-PLN",
2555 },
2556 ]
2557 )
2558
2559 const priceSetsResult = await service.calculatePrices(
2560 { id: ["price-set-PLN"] },
2561 {
2562 context: {
2563 currency_code: "pln",
2564 customer_group_id: "vip-customer-group-id",
2565 },
2566 }
2567 )
2568
2569 expect(priceSetsResult).toEqual([
2570 expect.objectContaining({
2571 id: "price-set-PLN",
2572 is_calculated_price_price_list: false,
2573 calculated_amount: 1000,
2574 is_original_price_price_list: false,
2575 original_amount: 1000,
2576 calculated_price: expect.objectContaining({
2577 id: "price-PLN",
2578 price_list_id: null,
2579 }),
2580 }),
2581 ])
2582 })
2583
2584 it("should return price list prices for price lists without rules regardless of the context", async () => {
2585 await createPriceLists(service, {}, {}, [
2586 {
2587 amount: 232,
2588 currency_code: "pln",
2589 price_set_id: "price-set-PLN",
2590 },
2591 ])
2592
2593 const priceSetsResult = await service.calculatePrices(
2594 { id: ["price-set-PLN"] },
2595 {
2596 context: {
2597 currency_code: "pln",
2598 region_id: "does-not-exist",
2599 customer_group_id: "unknown-customer-group",
2600 },
2601 }
2602 )
2603
2604 expect(priceSetsResult).toEqual([
2605 expect.objectContaining({
2606 id: "price-set-PLN",
2607 is_calculated_price_price_list: true,
2608 calculated_amount: 232,
2609 is_original_price_price_list: false,
2610 original_amount: 1000,
2611 calculated_price: expect.objectContaining({
2612 price_list_id: expect.any(String),
2613 price_list_type: "sale",
2614 }),
2615 original_price: expect.objectContaining({
2616 id: "price-PLN",
2617 price_list_id: null,
2618 }),
2619 }),
2620 ])
2621 })
2622
2623 it("should return price list prices when multiple context attributes match different rules of the same price list", async () => {
2624 await createPriceLists(
2625 service,
2626 {},
2627 {
2628 customer_group_id: ["vip-customer-group-id"],
2629 region_id: ["DE"],
2630 },
2631 [
2632 {
2633 amount: 232,
2634 currency_code: "pln",
2635 price_set_id: "price-set-PLN",
2636 },
2637 ]
2638 )
2639
2640 const priceSetsResult = await service.calculatePrices(
2641 { id: ["price-set-PLN"] },
2642 {
2643 context: {
2644 currency_code: "pln",
2645 customer_group_id: "vip-customer-group-id",
2646 region_id: "DE",
2647 },
2648 }
2649 )
2650
2651 expect(priceSetsResult).toEqual([
2652 expect.objectContaining({
2653 id: "price-set-PLN",
2654 is_calculated_price_price_list: true,
2655 calculated_amount: 232,
2656 is_original_price_price_list: false,
2657 original_amount: 1000,
2658 calculated_price: expect.objectContaining({
2659 price_list_id: expect.any(String),
2660 price_list_type: "sale",
2661 }),
2662 original_price: expect.objectContaining({
2663 id: "price-PLN",
2664 price_list_id: null,
2665 }),
2666 }),
2667 ])
2668 })
2669 })
2670
2671 describe("Tax inclusivity", () => {
2672 it("should return the currency tax inclusivity for the selected price when it is not region-based", async () => {
2673 await (service as any).createPricePreferences([
2674 {
2675 attribute: "currency_code",
2676 value: "pln",
2677 is_tax_inclusive: true,
2678 },
2679 ])
2680
2681 const priceSetsResult = await service.calculatePrices(
2682 { id: ["price-set-PLN"] },
2683 {
2684 context: { currency_code: "pln" },
2685 }
2686 )
2687
2688 expect(priceSetsResult).toEqual([
2689 expect.objectContaining({
2690 id: "price-set-PLN",
2691 is_calculated_price_tax_inclusive: true,
2692 calculated_amount: 1000,
2693 raw_calculated_amount: {
2694 value: "1000",
2695 precision: 20,
2696 },
2697 is_original_price_tax_inclusive: true,
2698 original_amount: 1000,
2699 raw_original_amount: {
2700 value: "1000",
2701 precision: 20,
2702 },
2703 currency_code: "pln",
2704 }),
2705 ])
2706 })
2707
2708 it("should return the region tax inclusivity for the selected price when it is region-based", async () => {
2709 await (service as any).createPricePreferences([
2710 {
2711 attribute: "currency_code",
2712 value: "pln",
2713 is_tax_inclusive: false,
2714 },
2715 {
2716 attribute: "region_id",
2717 value: "PL",
2718 is_tax_inclusive: true,
2719 },
2720 ])
2721
2722 const priceSetsResult = await service.calculatePrices(
2723 { id: ["price-set-PLN"] },
2724 {
2725 context: { currency_code: "pln", region_id: "PL" },
2726 }
2727 )
2728
2729 expect(priceSetsResult).toEqual([
2730 expect.objectContaining({
2731 id: "price-set-PLN",
2732 is_calculated_price_tax_inclusive: true,
2733 calculated_amount: 300,
2734 raw_calculated_amount: {
2735 value: "300",
2736 precision: 20,
2737 },
2738 is_original_price_tax_inclusive: true,
2739 original_amount: 300,
2740 raw_original_amount: {
2741 value: "300",
2742 precision: 20,
2743 },
2744 currency_code: "pln",
2745 }),
2746 ])
2747 })
2748
2749 it("should return the region tax inclusivity for the selected price when there are multiple region preferences", async () => {
2750 await (service as any).createPricePreferences([
2751 {
2752 attribute: "region_id",
2753 value: "DE",
2754 is_tax_inclusive: false,
2755 },
2756 {
2757 attribute: "region_id",
2758 value: "PL",
2759 is_tax_inclusive: true,
2760 },
2761 ])
2762
2763 const priceSetsResult = await service.calculatePrices(
2764 { id: ["price-set-PLN"] },
2765 {
2766 context: { currency_code: "pln", region_id: "PL" },
2767 }
2768 )
2769
2770 expect(priceSetsResult).toEqual([
2771 expect.objectContaining({
2772 id: "price-set-PLN",
2773 is_calculated_price_tax_inclusive: true,
2774 calculated_amount: 300,
2775 raw_calculated_amount: {
2776 value: "300",
2777 precision: 20,
2778 },
2779 is_original_price_tax_inclusive: true,
2780 original_amount: 300,
2781 raw_original_amount: {
2782 value: "300",
2783 precision: 20,
2784 },
2785 currency_code: "pln",
2786 }),
2787 ])
2788 })
2789
2790 it("should return the appropriate tax inclusive setting for each calculated and original price", async () => {
2791 await createPriceLists(service, {}, {})
2792 await (service as any).createPricePreferences([
2793 {
2794 attribute: "currency_code",
2795 value: "pln",
2796 is_tax_inclusive: false,
2797 },
2798 {
2799 attribute: "region_id",
2800 value: "PL",
2801 is_tax_inclusive: true,
2802 },
2803 ])
2804
2805 const priceSetsResult = await service.calculatePrices(
2806 { id: ["price-set-PLN"] },
2807 {
2808 context: {
2809 currency_code: "pln",
2810 region_id: "PL",
2811 },
2812 }
2813 )
2814
2815 expect(priceSetsResult).toEqual([
2816 expect.objectContaining({
2817 id: "price-set-PLN",
2818 is_calculated_price_tax_inclusive: false,
2819 calculated_amount: 232,
2820 raw_calculated_amount: {
2821 value: "232",
2822 precision: 20,
2823 },
2824 is_original_price_tax_inclusive: true,
2825 original_amount: 300,
2826 raw_original_amount: {
2827 value: "300",
2828 precision: 20,
2829 },
2830 currency_code: "pln",
2831 }),
2832 ])
2833 })
2834 })
2835 })
2836
2837 describe("calculatePrices", () => {
2838 let priceSet1
2839
2840 it("should return accurate prices when using custom price rule operators", async () => {
2841 priceSet1 = await service.createPriceSets({
2842 prices: [
2843 {
2844 amount: 50,
2845 currency_code: "usd",
2846 rules: {
2847 region_id: "de",
2848 total: withOperator("between", 300, 400),
2849 },
2850 },
2851 {
2852 amount: 100,
2853 currency_code: "usd",
2854 rules: {
2855 region_id: "de",
2856 total: withOperator("betweenEquals", 400, 500),
2857 },
2858 },
2859 {
2860 amount: 150,
2861 currency_code: "usd",
2862 rules: {
2863 region_id: "de",
2864 total: withOperator("excludingMin", 500, 600),
2865 },
2866 },
2867 {
2868 amount: 200,
2869 currency_code: "usd",
2870 rules: {
2871 region_id: "de",
2872 total: withOperator("excludingMax", 600, 700),
2873 },
2874 },
2875 ],
2876 })
2877
2878 let priceSetsResult = await service.calculatePrices(
2879 { id: [priceSet1.id] },
2880 {
2881 context: {
2882 currency_code: "usd",
2883 region_id: "de",
2884 total: 350,
2885 },
2886 }
2887 )
2888
2889 expect(priceSetsResult).toEqual([
2890 expect.objectContaining({
2891 is_calculated_price_price_list: false,
2892 is_calculated_price_tax_inclusive: false,
2893 calculated_amount: 50,
2894 raw_calculated_amount: {
2895 value: "50",
2896 precision: 20,
2897 },
2898 is_original_price_price_list: false,
2899 is_original_price_tax_inclusive: false,
2900 original_amount: 50,
2901 raw_original_amount: {
2902 value: "50",
2903 precision: 20,
2904 },
2905 currency_code: "usd",
2906 calculated_price: expect.objectContaining({
2907 id: expect.any(String),
2908 price_list_id: null,
2909 price_list_type: null,
2910 min_quantity: null,
2911 max_quantity: null,
2912 }),
2913 original_price: {
2914 id: expect.any(String),
2915 price_list_id: null,
2916 price_list_type: null,
2917 min_quantity: null,
2918 max_quantity: null,
2919 },
2920 }),
2921 ])
2922
2923 priceSetsResult = await service.calculatePrices(
2924 { id: [priceSet1.id] },
2925 {
2926 context: {
2927 currency_code: "usd",
2928 region_id: "de",
2929 total: 300,
2930 },
2931 }
2932 )
2933
2934 expect(priceSetsResult).toEqual([])
2935
2936 priceSetsResult = await service.calculatePrices(
2937 { id: [priceSet1.id] },
2938 {
2939 context: {
2940 currency_code: "usd",
2941 region_id: "de",
2942 total: 400,
2943 },
2944 }
2945 )
2946
2947 expect(priceSetsResult).toEqual([
2948 expect.objectContaining({ calculated_amount: 100 }),
2949 ])
2950
2951 priceSetsResult = await service.calculatePrices(
2952 { id: [priceSet1.id] },
2953 {
2954 context: {
2955 currency_code: "usd",
2956 region_id: "de",
2957 total: 500,
2958 },
2959 }
2960 )
2961
2962 expect(priceSetsResult).toEqual([
2963 expect.objectContaining({ calculated_amount: 100 }),
2964 ])
2965
2966 priceSetsResult = await service.calculatePrices(
2967 { id: [priceSet1.id] },
2968 {
2969 context: {
2970 currency_code: "usd",
2971 region_id: "de",
2972 total: 501,
2973 },
2974 }
2975 )
2976
2977 expect(priceSetsResult).toEqual([
2978 expect.objectContaining({ calculated_amount: 150 }),
2979 ])
2980
2981 priceSetsResult = await service.calculatePrices(
2982 { id: [priceSet1.id] },
2983 {
2984 context: {
2985 currency_code: "usd",
2986 region_id: "de",
2987 total: 601,
2988 },
2989 }
2990 )
2991
2992 expect(priceSetsResult).toEqual([
2993 expect.objectContaining({ calculated_amount: 200 }),
2994 ])
2995
2996 priceSetsResult = await service.calculatePrices(
2997 { id: [priceSet1.id] },
2998 {
2999 context: {
3000 currency_code: "usd",
3001 region_id: "de",
3002 total: 900,
3003 },
3004 }
3005 )
3006
3007 expect(priceSetsResult).toEqual([])
3008 })
3009 })
3010 })
3011 },
3012 })