1 import {
2 addToCartWorkflow,
3 createCartCreditLinesWorkflow,
4 updateCartsStep,
5 updateCartPromotionsWorkflow,
6 createProductsWorkflow,
7 } from "@medusajs/core-flows"
8 import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
9 import {
10 Modules,
11 PaymentSessionStatus,
12 PriceListStatus,
13 PriceListType,
14 ProductStatus,
15 PromotionActions,
16 PromotionRuleOperator,
17 PromotionStatus,
18 PromotionType,
19 } from "@medusajs/utils"
20 import { setTimeout } from "timers/promises"
21 import {
22 createAdminUser,
23 generatePublishableKey,
24 generateStoreHeaders,
25 } from "../../../../helpers/create-admin-user"
26 import { setupTaxStructure } from "../../../../modules/__tests__/fixtures"
27 import { createAuthenticatedCustomer } from "../../../../modules/helpers/create-authenticated-customer"
28 import { medusaTshirtProduct } from "../../../__fixtures__/product"
29 import {
30 createWorkflow,
31 WorkflowResponse,
32 } from "@medusajs/framework/workflows-sdk"
33
34 jest.setTimeout(100000)
35
36 const env = {}
37 const adminHeaders = { headers: { "x-medusa-access-token": "test_token" } }
38
39 const shippingAddressData = {
40 address_1: "test address 1",
41 address_2: "test address 2",
42 city: "SF",
43 country_code: "US",
44 province: "CA",
45 postal_code: "94016",
46 }
47
48 medusaIntegrationTestRunner({
49 env,
50 testSuite: ({ dbConnection, getContainer, api, dbUtils }) => {
51 let appContainer
52
53 beforeAll(async () => {
54 appContainer = getContainer()
55 })
56
57 describe("Store Carts API", () => {
58 let storeHeaders
59 let storeHeadersWithCustomer
60 let region
61 let noAutomaticRegion
62 let product
63 let salesChannel
64 let cart
65 let customer
66 let promotion
67 let shippingProfile
68 let taxSeedData
69
70 beforeAll(async () => {
71 await createAdminUser(dbConnection, adminHeaders, appContainer)
72 const publishableKey = await generatePublishableKey(appContainer)
73 storeHeaders = generateStoreHeaders({ publishableKey })
74
75 const result = await createAuthenticatedCustomer(api, storeHeaders, {
76 first_name: "tony",
77 last_name: "stark",
78 email: "tony@stark-industries.com",
79 })
80
81 customer = result.customer
82 storeHeadersWithCustomer = {
83 headers: {
84 ...storeHeaders.headers,
85 authorization: `Bearer ${result.jwt}`,
86 },
87 }
88
89 shippingProfile = (
90 await api.post(
91 `/admin/shipping-profiles`,
92 { name: "default", type: "default" },
93 adminHeaders
94 )
95 ).data.shipping_profile
96
97 taxSeedData = await setupTaxStructure(appContainer.resolve(Modules.TAX))
98
99 region = (
100 await api.post(
101 "/admin/regions",
102 {
103 payment_providers: ["pp_system_default"],
104 name: "US",
105 currency_code: "usd",
106 countries: ["us"],
107 },
108 adminHeaders
109 )
110 ).data.region
111
112 noAutomaticRegion = (
113 await api.post(
114 "/admin/regions",
115 {
116 payment_providers: ["pp_system_default"],
117 name: "EUR",
118 currency_code: "eur",
119 automatic_taxes: false,
120 },
121 adminHeaders
122 )
123 ).data.region
124
125 product = (
126 await api.post(
127 "/admin/products",
128 { ...medusaTshirtProduct, shipping_profile_id: shippingProfile.id },
129 adminHeaders
130 )
131 ).data.product
132
133 salesChannel = (
134 await api.post(
135 "/admin/sales-channels",
136 { name: "Webshop", description: "channel" },
137 adminHeaders
138 )
139 ).data.sales_channel
140
141 await api.post(
142 "/admin/price-preferences",
143 {
144 attribute: "currency_code",
145 value: "usd",
146 is_tax_inclusive: true,
147 },
148 adminHeaders
149 )
150
151 promotion = (
152 await api.post(
153 `/admin/promotions`,
154 {
155 code: "PROMOTION_APPLIED",
156 type: PromotionType.STANDARD,
157 status: PromotionStatus.ACTIVE,
158 application_method: {
159 type: "fixed",
160 target_type: "items",
161 allocation: "each",
162 value: 100,
163 max_quantity: 1,
164 currency_code: "usd",
165 target_rules: [
166 {
167 attribute: "items.product_id",
168 operator: "in",
169 values: [product.id],
170 },
171 ],
172 },
173 },
174 adminHeaders
175 )
176 ).data.promotion
177
178 await dbUtils.snapshot()
179 })
180
181 describe("GET /store/carts/[id]", () => {
182 it("should return 404 when trying to fetch a cart that does not exist", async () => {
183 const response = await api
184 .get(`/store/carts/fake`, storeHeadersWithCustomer)
185 .catch((e) => e)
186
187 expect(response.response.status).toEqual(404)
188 })
189 })
190
191 describe("POST /store/carts", () => {
192 it("should successfully create a cart", async () => {
193 const response = await api.post(
194 `/store/carts`,
195 {
196 currency_code: "usd",
197 sales_channel_id: salesChannel.id,
198 region_id: region.id,
199 shipping_address: shippingAddressData,
200 items: [{ variant_id: product.variants[0].id, quantity: 1 }],
201 },
202 storeHeadersWithCustomer
203 )
204
205 expect(response.status).toEqual(200)
206 expect(response.data.cart).toEqual(
207 expect.objectContaining({
208 currency_code: "usd",
209 items: expect.arrayContaining([
210 expect.objectContaining({
211 unit_price: 1500,
212 compare_at_unit_price: null,
213 is_tax_inclusive: true,
214 quantity: 1,
215 tax_lines: [
216 expect.objectContaining({
217 description: "CA Default Rate",
218 code: "CADEFAULT",
219 rate: 5,
220 provider_id: "system",
221 }),
222 ],
223 adjustments: [],
224 }),
225 ]),
226 })
227 )
228 })
229
230 it("should reject a currency_code that does not match the region's currency", async () => {
231 const multiCurrencyProduct = (
232 await api.post(
233 `/admin/products`,
234 {
235 title: "Multi-currency product",
236 status: ProductStatus.PUBLISHED,
237 shipping_profile_id: shippingProfile.id,
238 options: [{ title: "Size", values: ["S"] }],
239 variants: [
240 {
241 title: "S",
242 sku: "MULTI-CURRENCY-S",
243 options: { Size: "S" },
244 manage_inventory: false,
245 prices: [
246 { amount: 1500, currency_code: "usd" },
247 { amount: 1000, currency_code: "eur" },
248 ],
249 },
250 ],
251 },
252 adminHeaders
253 )
254 ).data.product
255
256 const response = await api
257 .post(
258 `/store/carts`,
259 {
260 currency_code: "eur",
261 sales_channel_id: salesChannel.id,
262 region_id: region.id, // US region -> usd
263 items: [
264 {
265 variant_id: multiCurrencyProduct.variants[0].id,
266 quantity: 1,
267 },
268 ],
269 },
270 storeHeaders
271 )
272 .catch((e) => e)
273
274 expect(response.response.status).toEqual(400)
275 expect(response.response.data.message).toContain(
276 "does not match the region's currency code"
277 )
278 })
279
280 it("should successfully create a cart with a line item with quantity and calculate prices based on the correct quantity", async () => {
281 const productData = {
282 title: "Medusa T-Shirt based quantity",
283 handle: "t-shirt-with-quantity-prices",
284 status: ProductStatus.PUBLISHED,
285 options: [
286 {
287 title: "Size",
288 values: ["S"],
289 },
290 ],
291 variants: [
292 {
293 title: "S",
294 sku: "SHIRT-S-BLACK-w-quantity-prices",
295 options: {
296 Size: "S",
297 },
298 manage_inventory: false,
299 prices: [
300 {
301 amount: 1500,
302 currency_code: "usd",
303 min_quantity: 1,
304 max_quantity: 4,
305 },
306 {
307 amount: 1000,
308 currency_code: "usd",
309 min_quantity: 5,
310 max_quantity: 10,
311 },
312 ],
313 },
314 ],
315 }
316
317 const newProduct = await api.post(
318 `/admin/products`,
319 productData,
320 adminHeaders
321 )
322
323 const variantId = newProduct.data.product.variants[0].id
324
325 const newCart = (
326 await api.post(
327 `/store/carts`,
328 {
329 currency_code: "usd",
330 sales_channel_id: salesChannel.id,
331 region_id: region.id,
332 shipping_address: shippingAddressData,
333 items: [{ variant_id: variantId, quantity: 6 }],
334 },
335 storeHeaders
336 )
337 ).data.cart
338
339 expect(newCart).toEqual(
340 expect.objectContaining({
341 item_subtotal: 5714.285714285715,
342 item_tax_total: 285.7142857142857,
343 item_total: 6000,
344 items: [
345 expect.objectContaining({
346 quantity: 6,
347 title: "Medusa T-Shirt based quantity",
348 unit_price: 1000,
349 updated_at: expect.any(String),
350 variant_barcode: null,
351 variant_id: expect.any(String),
352 variant_sku: "SHIRT-S-BLACK-w-quantity-prices",
353 variant_title: "S",
354 }),
355 ],
356 original_item_subtotal: 5714.285714285715,
357 original_item_tax_total: 285.7142857142857,
358 original_item_total: 6000,
359 original_shipping_subtotal: 0,
360 original_shipping_tax_total: 0,
361 original_shipping_total: 0,
362 original_tax_total: 285.7142857142857,
363 original_total: 6000,
364 shipping_subtotal: 0,
365 shipping_tax_total: 0,
366 shipping_total: 0,
367 subtotal: 5714.285714285715,
368 tax_total: 285.7142857142857,
369 total: 6000,
370 })
371 )
372 })
373
374 it("should successfully create a cart with a line items for the same variant with different quantities and calculate prices based on the correct quantity", async () => {
375 const productData = {
376 title: "Medusa T-Shirt based quantity",
377 handle: "t-shirt-with-quantity-prices",
378 status: ProductStatus.PUBLISHED,
379 options: [
380 {
381 title: "Size",
382 values: ["S"],
383 },
384 ],
385 variants: [
386 {
387 title: "S",
388 sku: "SHIRT-S-BLACK-w-quantity-prices",
389 options: {
390 Size: "S",
391 },
392 manage_inventory: false,
393 prices: [
394 {
395 amount: 1500,
396 currency_code: "usd",
397 min_quantity: 1,
398 max_quantity: 4,
399 },
400 {
401 amount: 1000,
402 currency_code: "usd",
403 min_quantity: 5,
404 max_quantity: 10,
405 },
406 ],
407 },
408 ],
409 }
410
411 const newProduct = await api.post(
412 `/admin/products`,
413 productData,
414 adminHeaders
415 )
416
417 const variantId = newProduct.data.product.variants[0].id
418
419 const newCart = (
420 await api.post(
421 `/store/carts`,
422 {
423 currency_code: "usd",
424 sales_channel_id: salesChannel.id,
425 region_id: region.id,
426 shipping_address: shippingAddressData,
427 items: [{ variant_id: variantId, quantity: 6 }],
428 },
429 storeHeaders
430 )
431 ).data.cart
432
433 expect(newCart).toEqual(
434 expect.objectContaining({
435 item_subtotal: 5714.285714285715,
436 item_tax_total: 285.7142857142857,
437 item_total: 6000,
438 items: [
439 expect.objectContaining({
440 quantity: 6,
441 title: "Medusa T-Shirt based quantity",
442 unit_price: 1000,
443 updated_at: expect.any(String),
444 variant_barcode: null,
445 variant_id: expect.any(String),
446 variant_sku: "SHIRT-S-BLACK-w-quantity-prices",
447 variant_title: "S",
448 }),
449 ],
450 original_item_subtotal: 5714.285714285715,
451 original_item_tax_total: 285.7142857142857,
452 original_item_total: 6000,
453 original_shipping_subtotal: 0,
454 original_shipping_tax_total: 0,
455 original_shipping_total: 0,
456 original_tax_total: 285.7142857142857,
457 original_total: 6000,
458 shipping_subtotal: 0,
459 shipping_tax_total: 0,
460 shipping_total: 0,
461 subtotal: 5714.285714285715,
462 tax_total: 285.7142857142857,
463 total: 6000,
464 })
465 )
466
467 const updatedCart = (
468 await api.post(
469 `/store/carts/${newCart.id}/line-items`,
470 {
471 variant_id: variantId,
472 quantity: 1,
473 metadata: { custom: true },
474 },
475 storeHeaders
476 )
477 ).data.cart
478
479 expect(updatedCart).toEqual(
480 expect.objectContaining({
481 item_subtotal: 7142.857142857143,
482 item_tax_total: 357.14285714285717,
483 item_total: 7500,
484 items: expect.arrayContaining([
485 expect.objectContaining({
486 quantity: 6,
487 title: "Medusa T-Shirt based quantity",
488 unit_price: 1000,
489 updated_at: expect.any(String),
490 variant_barcode: null,
491 variant_id: expect.any(String),
492 variant_sku: "SHIRT-S-BLACK-w-quantity-prices",
493 variant_title: "S",
494 }),
495 expect.objectContaining({
496 quantity: 1,
497 title: "Medusa T-Shirt based quantity",
498 unit_price: 1500,
499 updated_at: expect.any(String),
500 variant_barcode: null,
501 variant_id: expect.any(String),
502 variant_sku: "SHIRT-S-BLACK-w-quantity-prices",
503 variant_title: "S",
504 }),
505 ]),
506 original_item_subtotal: 7142.857142857143,
507 original_item_tax_total: 357.14285714285717,
508 original_item_total: 7500,
509 original_shipping_subtotal: 0,
510 original_shipping_tax_total: 0,
511 original_shipping_total: 0,
512 original_tax_total: 357.14285714285717,
513 original_total: 7500,
514 shipping_subtotal: 0,
515 shipping_tax_total: 0,
516 shipping_total: 0,
517 subtotal: 7142.857142857143,
518 tax_total: 357.14285714285717,
519 total: 7500,
520 })
521 )
522 })
523
524 describe("with sale price lists", () => {
525 let priceList
526
527 beforeEach(async () => {
528 priceList = (
529 await api.post(
530 `/admin/price-lists`,
531 {
532 title: "test price list",
533 description: "test",
534 status: PriceListStatus.ACTIVE,
535 type: PriceListType.SALE,
536 prices: [
537 {
538 amount: 350,
539 currency_code: "usd",
540 variant_id: product.variants[0].id,
541 },
542 ],
543 },
544 adminHeaders
545 )
546 ).data.price_list
547 })
548
549 it("should successfully create cart with price from price list", async () => {
550 const response = await api.post(
551 `/store/carts`,
552 {
553 currency_code: "usd",
554 sales_channel_id: salesChannel.id,
555 region_id: region.id,
556 shipping_address: shippingAddressData,
557 items: [{ variant_id: product.variants[0].id, quantity: 1 }],
558 },
559 storeHeaders
560 )
561
562 expect(response.status).toEqual(200)
563 expect(response.data.cart).toEqual(
564 expect.objectContaining({
565 currency_code: "usd",
566 items: expect.arrayContaining([
567 expect.objectContaining({
568 unit_price: 350,
569 compare_at_unit_price: 1500,
570 is_tax_inclusive: true,
571 quantity: 1,
572 tax_lines: [
573 expect.objectContaining({
574 description: "CA Default Rate",
575 code: "CADEFAULT",
576 rate: 5,
577 provider_id: "system",
578 }),
579 ],
580 adjustments: [],
581 }),
582 ]),
583 })
584 )
585 })
586 })
587 })
588
589 describe("POST /store/carts/:id/line-items", () => {
590 let shippingOption
591 let shippingOptionExpensive
592 let stockLocation
593
594 beforeEach(async () => {
595 stockLocation = (
596 await api.post(
597 `/admin/stock-locations`,
598 { name: "test location" },
599 adminHeaders
600 )
601 ).data.stock_location
602
603 await api.post(
604 `/admin/stock-locations/${stockLocation.id}/sales-channels`,
605 { add: [salesChannel.id] },
606 adminHeaders
607 )
608
609 const shippingProfile = (
610 await api.post(
611 `/admin/shipping-profiles`,
612 { name: `test-${stockLocation.id}`, type: "default" },
613 adminHeaders
614 )
615 ).data.shipping_profile
616
617 const fulfillmentSets = (
618 await api.post(
619 `/admin/stock-locations/${stockLocation.id}/fulfillment-sets?fields=*fulfillment_sets`,
620 {
621 name: `Test-${shippingProfile.id}`,
622 type: "test-type",
623 },
624 adminHeaders
625 )
626 ).data.stock_location.fulfillment_sets
627
628 const fulfillmentSet = (
629 await api.post(
630 `/admin/fulfillment-sets/${fulfillmentSets[0].id}/service-zones`,
631 {
632 name: `Test-${shippingProfile.id}`,
633 geo_zones: [
634 { type: "country", country_code: "it" },
635 { type: "country", country_code: "us" },
636 ],
637 },
638 adminHeaders
639 )
640 ).data.fulfillment_set
641
642 await api.post(
643 `/admin/stock-locations/${stockLocation.id}/fulfillment-providers`,
644 { add: ["manual_test-provider"] },
645 adminHeaders
646 )
647
648 const shippingOptionPayload = {
649 name: `Shipping`,
650 service_zone_id: fulfillmentSet.service_zones[0].id,
651 shipping_profile_id: shippingProfile.id,
652 provider_id: "manual_test-provider",
653 price_type: "flat",
654 type: {
655 label: "Test type",
656 description: "Test description",
657 code: "test-code",
658 },
659 prices: [
660 { currency_code: "usd", amount: 1000 },
661 {
662 currency_code: "usd",
663 amount: 0,
664 rules: [
665 {
666 attribute: "item_total",
667 operator: "gt",
668 value: 5000,
669 },
670 ],
671 },
672 ],
673 rules: [
674 {
675 attribute: "enabled_in_store",
676 value: "true",
677 operator: "eq",
678 },
679 {
680 attribute: "is_return",
681 value: "false",
682 operator: "eq",
683 },
684 ],
685 }
686
687 shippingOption = (
688 await api.post(
689 `/admin/shipping-options`,
690 shippingOptionPayload,
691 adminHeaders
692 )
693 ).data.shipping_option
694
695 shippingOptionExpensive = (
696 await api.post(
697 `/admin/shipping-options`,
698 {
699 ...shippingOptionPayload,
700 prices: [
701 { currency_code: "usd", amount: 10000 },
702 {
703 currency_code: "usd",
704 amount: 5000,
705 rules: [
706 {
707 attribute: "item_total",
708 operator: "gt",
709 value: 5000,
710 },
711 ],
712 },
713 ],
714 },
715 adminHeaders
716 )
717 ).data.shipping_option
718
719 cart = (
720 await api.post(
721 `/store/carts`,
722 {
723 currency_code: "usd",
724 sales_channel_id: salesChannel.id,
725 region_id: region.id,
726 shipping_address: shippingAddressData,
727 items: [{ variant_id: product.variants[0].id, quantity: 1 }],
728 promo_codes: [promotion.code],
729 },
730 storeHeaders
731 )
732 ).data.cart
733 })
734
735 it("should add item to cart", async () => {
736 const response = await api.post(
737 `/store/carts/${cart.id}/line-items`,
738 {
739 variant_id: product.variants[0].id,
740 quantity: 1,
741 },
742 storeHeaders
743 )
744
745 expect(response.status).toEqual(200)
746 expect(response.data.cart).toEqual(
747 expect.objectContaining({
748 id: cart.id,
749 currency_code: "usd",
750 items: expect.arrayContaining([
751 expect.objectContaining({
752 unit_price: 1500,
753 compare_at_unit_price: null,
754 is_tax_inclusive: true,
755 quantity: 2,
756 tax_lines: expect.arrayContaining([
757 expect.objectContaining({
758 description: "CA Default Rate",
759 code: "CADEFAULT",
760 rate: 5,
761 provider_id: "system",
762 }),
763 ]),
764 adjustments: expect.arrayContaining([
765 expect.objectContaining({
766 id: expect.any(String),
767 code: "PROMOTION_APPLIED",
768 promotion_id: promotion.id,
769 amount: 100,
770 }),
771 ]),
772 }),
773 ]),
774 })
775 )
776 })
777
778 it("should return a 400 instead of a 500 when adding a variant with no price", async () => {
779 const productData = {
780 title: "Medusa T-Shirt with no price",
781 handle: "t-shirt-with-no-price",
782 status: ProductStatus.PUBLISHED,
783 options: [
784 {
785 title: "Size",
786 values: ["S"],
787 },
788 ],
789 variants: [
790 {
791 title: "S",
792 sku: "SHIRT-S-BLACK-NO-PRICE",
793 options: {
794 Size: "S",
795 },
796 manage_inventory: false,
797 },
798 ],
799 }
800
801 const {
802 result: [newProduct],
803 } = await createProductsWorkflow(appContainer).run({
804 input: {
805 products: [productData],
806 },
807 })
808
809 const variantId = newProduct.variants[0].id
810
811 const error = await api
812 .post(
813 `/store/carts/${cart.id}/line-items`,
814 {
815 variant_id: variantId,
816 quantity: 1,
817 },
818 storeHeaders
819 )
820 .catch((e) => e)
821
822 expect(error.response.status).toEqual(400)
823 expect(error.response.data.message).toEqual(
824 `Variants with IDs ${variantId} do not have a price`
825 )
826 })
827
828 it("should add item to cart and calculate prices based on item quantity", async () => {
829 const productData = {
830 title: "Medusa T-Shirt based quantity",
831 handle: "t-shirt-with-quantity-prices",
832 status: ProductStatus.PUBLISHED,
833 options: [
834 {
835 title: "Size",
836 values: ["S"],
837 },
838 ],
839 variants: [
840 {
841 title: "S",
842 sku: "SHIRT-S-BLACK-w-quantity-prices",
843 options: {
844 Size: "S",
845 },
846 manage_inventory: false,
847 prices: [
848 {
849 amount: 1500,
850 currency_code: "usd",
851 min_quantity: 1,
852 max_quantity: 4,
853 },
854 {
855 amount: 1000,
856 currency_code: "usd",
857 min_quantity: 5,
858 max_quantity: 10,
859 },
860 ],
861 },
862 ],
863 }
864
865 const newProduct = await api.post(
866 `/admin/products`,
867 productData,
868 adminHeaders
869 )
870
871 const variantId = newProduct.data.product.variants[0].id
872
873 const newCart = (
874 await api.post(
875 `/store/carts`,
876 {
877 currency_code: "usd",
878 sales_channel_id: salesChannel.id,
879 region_id: region.id,
880 shipping_address: shippingAddressData,
881 },
882 storeHeaders
883 )
884 ).data.cart
885
886 /**
887 * Add item to cart with quantity 1
888 * in order to have the price calculated based on the price rule
889 * with min_quantity 1 and max_quantity 4
890 */
891
892 let response = await api.post(
893 `/store/carts/${newCart.id}/line-items`,
894 {
895 variant_id: variantId,
896 quantity: 1,
897 },
898 storeHeaders
899 )
900
901 expect(response.status).toEqual(200)
902 expect(response.data.cart).toEqual(
903 expect.objectContaining({
904 billing_address: null,
905 completed_at: null,
906 created_at: expect.any(String),
907 credit_line_subtotal: 0,
908 credit_line_tax_total: 0,
909 credit_line_total: 0,
910 credit_lines: [],
911 currency_code: "usd",
912 customer_id: null,
913 discount_subtotal: 0,
914 discount_tax_total: 0,
915 discount_total: 0,
916 email: null,
917 id: newCart.id,
918 item_subtotal: 1428.5714285714287,
919 item_tax_total: 71.42857142857143,
920 item_total: 1500,
921 items: [
922 expect.objectContaining({
923 compare_at_unit_price: null,
924 created_at: expect.any(String),
925 id: expect.any(String),
926 is_tax_inclusive: true,
927 metadata: {},
928 product: expect.objectContaining({
929 categories: [],
930 collection_id: null,
931 id: expect.any(String),
932 tags: [],
933 type_id: null,
934 }),
935 product_collection: null,
936 product_description: null,
937 product_handle: "t-shirt-with-quantity-prices",
938 product_id: expect.any(String),
939 product_subtitle: null,
940 product_title: "Medusa T-Shirt based quantity",
941 product_type: null,
942 product_type_id: null,
943 quantity: 1,
944 requires_shipping: false,
945 tax_lines: [
946 {
947 code: "CADEFAULT",
948 data: null,
949 description: "CA Default Rate",
950 id: expect.any(String),
951 metadata: null,
952 provider_id: "system",
953 rate: 5,
954 },
955 ],
956 thumbnail: null,
957 title: "Medusa T-Shirt based quantity",
958 unit_price: 1500,
959 updated_at: expect.any(String),
960 variant_barcode: null,
961 variant_id: expect.any(String),
962 variant_sku: "SHIRT-S-BLACK-w-quantity-prices",
963 variant_title: "S",
964 }),
965 ],
966 metadata: null,
967 original_item_subtotal: 1428.5714285714287,
968 original_item_tax_total: 71.42857142857143,
969 original_item_total: 1500,
970 original_shipping_subtotal: 0,
971 original_shipping_tax_total: 0,
972 original_shipping_total: 0,
973 original_tax_total: 71.42857142857143,
974 original_total: 1500,
975 region: expect.objectContaining({
976 automatic_taxes: true,
977 countries: expect.any(Array),
978 currency_code: "usd",
979 id: expect.any(String),
980 name: "US",
981 }),
982 region_id: expect.any(String),
983 sales_channel_id: expect.any(String),
984 shipping_address: expect.objectContaining({
985 address_1: "test address 1",
986 address_2: "test address 2",
987 city: "SF",
988 company: null,
989 country_code: "US",
990 first_name: null,
991 id: expect.any(String),
992 last_name: null,
993 phone: null,
994 postal_code: "94016",
995 province: "CA",
996 }),
997 shipping_address_id: expect.any(String),
998 shipping_methods: [],
999 shipping_subtotal: 0,
1000 shipping_tax_total: 0,
1001 shipping_total: 0,
1002 subtotal: 1428.5714285714287,
1003 tax_total: 71.42857142857143,
1004 total: 1500,
1005 updated_at: expect.any(String),
1006 })
1007 )
1008
1009 /**
1010 * Add item to cart with quantity 5
1011 * in order to have the price calculated based on the price rule
1012 * with min_quantity 5 and max_quantity 10
1013 */
1014
1015 response = await api.post(
1016 `/store/carts/${newCart.id}/line-items`,
1017 {
1018 variant_id: variantId,
1019 quantity: 5,
1020 },
1021 storeHeaders
1022 )
1023
1024 expect(response.status).toEqual(200)
1025 expect(response.data.cart).toEqual(
1026 expect.objectContaining({
1027 billing_address: null,
1028 completed_at: null,
1029 created_at: expect.any(String),
1030 credit_line_subtotal: 0,
1031 credit_line_tax_total: 0,
1032 credit_line_total: 0,
1033 credit_lines: [],
1034 currency_code: "usd",
1035 customer_id: null,
1036 discount_subtotal: 0,
1037 discount_tax_total: 0,
1038 discount_total: 0,
1039 email: null,
1040 id: newCart.id,
1041 item_subtotal: 5714.285714285715,
1042 item_tax_total: 285.7142857142857,
1043 item_total: 6000,
1044 items: [
1045 expect.objectContaining({
1046 adjustments: [],
1047 compare_at_unit_price: null,
1048 created_at: expect.any(String),
1049 id: expect.any(String),
1050 is_tax_inclusive: true,
1051 metadata: {},
1052 product: {
1053 categories: [],
1054 collection_id: null,
1055 id: expect.any(String),
1056 tags: [],
1057 type_id: null,
1058 },
1059 product_collection: null,
1060 product_description: null,
1061 product_handle: "t-shirt-with-quantity-prices",
1062 product_id: expect.any(String),
1063 product_subtitle: null,
1064 product_title: "Medusa T-Shirt based quantity",
1065 product_type: null,
1066 product_type_id: null,
1067 quantity: 6,
1068 requires_shipping: false,
1069 tax_lines: [
1070 {
1071 code: "CADEFAULT",
1072 data: null,
1073 description: "CA Default Rate",
1074 id: expect.any(String),
1075 metadata: null,
1076 provider_id: "system",
1077 rate: 5,
1078 },
1079 ],
1080 thumbnail: null,
1081 title: "Medusa T-Shirt based quantity",
1082 unit_price: 1000,
1083 updated_at: expect.any(String),
1084 variant_barcode: null,
1085 variant_id: expect.any(String),
1086 variant_sku: "SHIRT-S-BLACK-w-quantity-prices",
1087 variant_title: "S",
1088 }),
1089 ],
1090 metadata: null,
1091 original_item_subtotal: 5714.285714285715,
1092 original_item_tax_total: 285.7142857142857,
1093 original_item_total: 6000,
1094 original_shipping_subtotal: 0,
1095 original_shipping_tax_total: 0,
1096 original_shipping_total: 0,
1097 original_tax_total: 285.7142857142857,
1098 original_total: 6000,
1099 promotions: [],
1100 region: {
1101 automatic_taxes: true,
1102 countries: expect.any(Array),
1103 currency_code: "usd",
1104 id: expect.any(String),
1105 name: "US",
1106 },
1107 region_id: expect.any(String),
1108 sales_channel_id: expect.any(String),
1109 shipping_address: {
1110 address_1: "test address 1",
1111 address_2: "test address 2",
1112 city: "SF",
1113 company: null,
1114 country_code: "US",
1115 first_name: null,
1116 id: expect.any(String),
1117 last_name: null,
1118 phone: null,
1119 postal_code: "94016",
1120 province: "CA",
1121 },
1122 shipping_address_id: expect.any(String),
1123 shipping_methods: [],
1124 shipping_subtotal: 0,
1125 shipping_tax_total: 0,
1126 shipping_total: 0,
1127 subtotal: 5714.285714285715,
1128 tax_total: 285.7142857142857,
1129 total: 6000,
1130 updated_at: expect.any(String),
1131 })
1132 )
1133 })
1134
1135 it("should update a cart line item quantity and calculate prices based the new item quantity", async () => {
1136 const productData = {
1137 title: "Medusa T-Shirt based quantity",
1138 handle: "t-shirt-with-quantity-prices",
1139 status: ProductStatus.PUBLISHED,
1140 options: [
1141 {
1142 title: "Size",
1143 values: ["S"],
1144 },
1145 ],
1146 variants: [
1147 {
1148 title: "S",
1149 sku: "SHIRT-S-BLACK-w-quantity-prices",
1150 options: {
1151 Size: "S",
1152 },
1153 manage_inventory: false,
1154 prices: [
1155 {
1156 amount: 1500,
1157 currency_code: "usd",
1158 min_quantity: 1,
1159 max_quantity: 4,
1160 },
1161 {
1162 amount: 1000,
1163 currency_code: "usd",
1164 min_quantity: 5,
1165 max_quantity: 10,
1166 },
1167 ],
1168 },
1169 ],
1170 }
1171
1172 const newProduct = await api.post(
1173 `/admin/products`,
1174 productData,
1175 adminHeaders
1176 )
1177
1178 const variantId = newProduct.data.product.variants[0].id
1179
1180 const newCart = (
1181 await api.post(
1182 `/store/carts`,
1183 {
1184 currency_code: "usd",
1185 sales_channel_id: salesChannel.id,
1186 region_id: region.id,
1187 shipping_address: shippingAddressData,
1188 },
1189 storeHeaders
1190 )
1191 ).data.cart
1192
1193 /**
1194 * Add item to cart with quantity 1
1195 * in order to have the price calculated based on the price rule
1196 * with min_quantity 1 and max_quantity 4
1197 */
1198
1199 let response = await api.post(
1200 `/store/carts/${newCart.id}/line-items`,
1201 {
1202 variant_id: variantId,
1203 quantity: 1,
1204 },
1205 storeHeaders
1206 )
1207
1208 expect(response.status).toEqual(200)
1209 expect(response.data.cart).toEqual(
1210 expect.objectContaining({
1211 item_subtotal: 1428.5714285714287,
1212 item_tax_total: 71.42857142857143,
1213 item_total: 1500,
1214 items: [
1215 expect.objectContaining({
1216 quantity: 1,
1217 title: "Medusa T-Shirt based quantity",
1218 unit_price: 1500,
1219 updated_at: expect.any(String),
1220 variant_barcode: null,
1221 variant_id: expect.any(String),
1222 variant_sku: "SHIRT-S-BLACK-w-quantity-prices",
1223 variant_title: "S",
1224 }),
1225 ],
1226 original_item_subtotal: 1428.5714285714287,
1227 original_item_tax_total: 71.42857142857143,
1228 original_item_total: 1500,
1229 original_shipping_subtotal: 0,
1230 original_shipping_tax_total: 0,
1231 original_shipping_total: 0,
1232 original_tax_total: 71.42857142857143,
1233 original_total: 1500,
1234 shipping_subtotal: 0,
1235 shipping_tax_total: 0,
1236 shipping_total: 0,
1237 subtotal: 1428.5714285714287,
1238 tax_total: 71.42857142857143,
1239 total: 1500,
1240 })
1241 )
1242
1243 /**
1244 * update item quantity to 5
1245 * in order to have the price calculated based on the price rule
1246 * with min_quantity 5 and max_quantity 10
1247 */
1248
1249 const itemId = response.data.cart.items[0].id
1250 response = await api
1251 .post(
1252 `/store/carts/${newCart.id}/line-items/${itemId}`,
1253 {
1254 quantity: 6,
1255 },
1256 storeHeaders
1257 )
1258 .catch((e) => {
1259 console.log(e.response.data)
1260 throw e
1261 })
1262
1263 expect(response.status).toEqual(200)
1264 expect(response.data.cart).toEqual(
1265 expect.objectContaining({
1266 item_subtotal: 5714.285714285715,
1267 item_tax_total: 285.7142857142857,
1268 item_total: 6000,
1269 items: [
1270 expect.objectContaining({
1271 quantity: 6,
1272 title: "Medusa T-Shirt based quantity",
1273 unit_price: 1000,
1274 updated_at: expect.any(String),
1275 variant_barcode: null,
1276 variant_id: expect.any(String),
1277 variant_sku: "SHIRT-S-BLACK-w-quantity-prices",
1278 variant_title: "S",
1279 }),
1280 ],
1281 original_item_subtotal: 5714.285714285715,
1282 original_item_tax_total: 285.7142857142857,
1283 original_item_total: 6000,
1284 original_shipping_subtotal: 0,
1285 original_shipping_tax_total: 0,
1286 original_shipping_total: 0,
1287 original_tax_total: 285.7142857142857,
1288 original_total: 6000,
1289 shipping_subtotal: 0,
1290 shipping_tax_total: 0,
1291 shipping_total: 0,
1292 subtotal: 5714.285714285715,
1293 tax_total: 285.7142857142857,
1294 total: 6000,
1295 })
1296 )
1297 })
1298
1299 it("should remove promotions when promotion is no longer in active state", async () => {
1300 const responseBeforePromotionUpdate = await api.post(
1301 `/store/carts/${cart.id}/line-items`,
1302 {
1303 variant_id: product.variants[0].id,
1304 quantity: 1,
1305 },
1306 storeHeaders
1307 )
1308
1309 expect(responseBeforePromotionUpdate.data.cart).toEqual(
1310 expect.objectContaining({
1311 id: cart.id,
1312 items: expect.arrayContaining([
1313 expect.objectContaining({
1314 adjustments: expect.arrayContaining([
1315 expect.objectContaining({
1316 id: expect.any(String),
1317 code: "PROMOTION_APPLIED",
1318 promotion_id: promotion.id,
1319 amount: 100,
1320 }),
1321 ]),
1322 }),
1323 ]),
1324 })
1325 )
1326
1327 await api.post(
1328 `/admin/promotions/${promotion.id}`,
1329 { status: PromotionStatus.INACTIVE },
1330 adminHeaders
1331 )
1332
1333 const responseAfterPromotionUpdate = await api.post(
1334 `/store/carts/${cart.id}/line-items`,
1335 {
1336 variant_id: product.variants[0].id,
1337 quantity: 1,
1338 },
1339 storeHeaders
1340 )
1341
1342 expect(responseAfterPromotionUpdate.status).toEqual(200)
1343 expect(responseAfterPromotionUpdate.data.cart).toEqual(
1344 expect.objectContaining({
1345 id: cart.id,
1346 items: expect.arrayContaining([
1347 expect.objectContaining({
1348 adjustments: [],
1349 }),
1350 expect.objectContaining({
1351 adjustments: [],
1352 }),
1353 ]),
1354 })
1355 )
1356 })
1357
1358 describe("with custom shipping options prices", () => {
1359 beforeEach(async () => {
1360 cart = (
1361 await api.post(
1362 `/store/carts`,
1363 {
1364 currency_code: "usd",
1365 sales_channel_id: salesChannel.id,
1366 region_id: region.id,
1367 items: [{ variant_id: product.variants[0].id, quantity: 1 }],
1368 promo_codes: [promotion.code],
1369 },
1370 storeHeadersWithCustomer
1371 )
1372 ).data.cart
1373 })
1374
1375 it("should update shipping method amount when cart totals change", async () => {
1376 let response = await api.post(
1377 `/store/carts/${cart.id}/shipping-methods`,
1378 { option_id: shippingOption.id },
1379 storeHeaders
1380 )
1381
1382 expect(response.data.cart).toEqual(
1383 expect.objectContaining({
1384 id: cart.id,
1385 shipping_methods: expect.arrayContaining([
1386 expect.objectContaining({
1387 shipping_option_id: shippingOption.id,
1388 amount: 1000,
1389 is_tax_inclusive: true,
1390 }),
1391 ]),
1392 })
1393 )
1394
1395 response = await api.post(
1396 `/store/carts/${cart.id}/line-items`,
1397 {
1398 variant_id: product.variants[0].id,
1399 quantity: 100,
1400 },
1401 storeHeaders
1402 )
1403
1404 expect(response.data.cart).toEqual(
1405 expect.objectContaining({
1406 id: cart.id,
1407 shipping_methods: expect.arrayContaining([
1408 expect.objectContaining({
1409 shipping_option_id: shippingOption.id,
1410 amount: 0,
1411 is_tax_inclusive: true,
1412 }),
1413 ]),
1414 })
1415 )
1416 })
1417
1418 it("should remove shipping methods when they are no longer valid for the cart", async () => {
1419 let response = await api.post(
1420 `/store/carts/${cart.id}/shipping-methods`,
1421 { option_id: shippingOption.id },
1422 storeHeaders
1423 )
1424
1425 expect(response.data.cart).toEqual(
1426 expect.objectContaining({
1427 id: cart.id,
1428 shipping_methods: expect.arrayContaining([
1429 expect.objectContaining({
1430 shipping_option_id: shippingOption.id,
1431 amount: 1000,
1432 is_tax_inclusive: true,
1433 }),
1434 ]),
1435 })
1436 )
1437
1438 response = await api.post(
1439 `/store/carts/${cart.id}`,
1440 { region_id: noAutomaticRegion.id },
1441 storeHeaders
1442 )
1443
1444 expect(response.data.cart).toEqual(
1445 expect.objectContaining({
1446 id: cart.id,
1447 shipping_methods: expect.arrayContaining([]),
1448 })
1449 )
1450 })
1451
1452 it("should update payment collection upon changing shipping option", async () => {
1453 await api.post(
1454 `/store/carts/${cart.id}/shipping-methods`,
1455 { option_id: shippingOption.id },
1456 storeHeaders
1457 )
1458
1459 await api.post(
1460 `/store/payment-collections`,
1461 { cart_id: cart.id },
1462 storeHeaders
1463 )
1464
1465 const cartAfterCollection = (
1466 await api.get(`/store/carts/${cart.id}`, storeHeaders)
1467 ).data.cart
1468
1469 expect(cartAfterCollection).toEqual(
1470 expect.objectContaining({
1471 id: cart.id,
1472 shipping_methods: expect.arrayContaining([
1473 expect.objectContaining({
1474 shipping_option_id: shippingOption.id,
1475 }),
1476 ]),
1477 payment_collection: expect.objectContaining({
1478 amount: 2398,
1479 }),
1480 })
1481 )
1482
1483 await api.post(
1484 `/store/carts/${cart.id}/line-items`,
1485 {
1486 variant_id: product.variants[0].id,
1487 quantity: 100,
1488 },
1489 storeHeaders
1490 )
1491
1492 const cartAfterExpensiveShipping = (
1493 await api.post(
1494 `/store/carts/${cart.id}/shipping-methods`,
1495 { option_id: shippingOptionExpensive.id },
1496 storeHeaders
1497 )
1498 ).data.cart
1499
1500 expect(cartAfterExpensiveShipping).toEqual(
1501 expect.objectContaining({
1502 id: cartAfterExpensiveShipping.id,
1503 shipping_methods: expect.arrayContaining([
1504 expect.objectContaining({
1505 shipping_option_id: shippingOptionExpensive.id,
1506 amount: 5000,
1507 }),
1508 ]),
1509 payment_collection: expect.objectContaining({
1510 amount: 156398,
1511 }),
1512 })
1513 )
1514 })
1515 })
1516
1517 it("should add item to cart with tax lines multiple times", async () => {
1518 let response = await api.post(
1519 `/store/carts/${cart.id}/line-items`,
1520 {
1521 variant_id: product.variants[0].id,
1522 quantity: 1,
1523 },
1524 storeHeaders
1525 )
1526
1527 expect(response.status).toEqual(200)
1528 expect(response.data.cart).toEqual(
1529 expect.objectContaining({
1530 id: cart.id,
1531 currency_code: "usd",
1532 items: expect.arrayContaining([
1533 expect.objectContaining({
1534 unit_price: 1500,
1535 compare_at_unit_price: null,
1536 is_tax_inclusive: true,
1537 title: "Medusa T-Shirt",
1538 quantity: 2,
1539 tax_lines: [
1540 expect.objectContaining({
1541 description: "CA Default Rate",
1542 code: "CADEFAULT",
1543 rate: 5,
1544 provider_id: "system",
1545 }),
1546 ],
1547 }),
1548 ]),
1549 })
1550 )
1551
1552 response = await api.post(
1553 `/store/carts/${cart.id}/line-items`,
1554 {
1555 variant_id: product.variants[1].id,
1556 quantity: 1,
1557 },
1558 storeHeaders
1559 )
1560
1561 expect(response.status).toEqual(200)
1562 expect(response.data.cart).toEqual(
1563 expect.objectContaining({
1564 id: cart.id,
1565 currency_code: "usd",
1566 items: expect.arrayContaining([
1567 expect.objectContaining({
1568 unit_price: 1500,
1569 compare_at_unit_price: null,
1570 is_tax_inclusive: true,
1571 quantity: 2,
1572 title: "Medusa T-Shirt",
1573 tax_lines: [
1574 expect.objectContaining({
1575 description: "CA Default Rate",
1576 code: "CADEFAULT",
1577 rate: 5,
1578 provider_id: "system",
1579 }),
1580 ],
1581 }),
1582 expect.objectContaining({
1583 unit_price: 1500,
1584 compare_at_unit_price: null,
1585 is_tax_inclusive: true,
1586 quantity: 1,
1587 title: "Medusa T-Shirt",
1588 tax_lines: [
1589 expect.objectContaining({
1590 description: "CA Default Rate",
1591 code: "CADEFAULT",
1592 rate: 5,
1593 provider_id: "system",
1594 }),
1595 ],
1596 }),
1597 ]),
1598 })
1599 )
1600 })
1601
1602 it("should update the correct line item when multiple items exist for the same variant with different metadata", async () => {
1603 // Add same variant with different metadata to create two separate line items
1604 await api.post(
1605 `/store/carts/${cart.id}/line-items`,
1606 {
1607 variant_id: product.variants[0].id,
1608 quantity: 1,
1609 metadata: { engraving: "Hello" },
1610 },
1611 storeHeaders
1612 )
1613
1614 let response = await api.get(`/store/carts/${cart.id}`, storeHeaders)
1615
1616 // Cart should have 2 items: original (qty 1, no metadata) and new (qty 1, with metadata)
1617 expect(response.data.cart.items).toHaveLength(2)
1618
1619 // Now add the same variant again with the engraving metadata — should update that specific item
1620 response = await api.post(
1621 `/store/carts/${cart.id}/line-items`,
1622 {
1623 variant_id: product.variants[0].id,
1624 quantity: 2,
1625 metadata: { engraving: "Hello" },
1626 },
1627 storeHeaders
1628 )
1629
1630 expect(response.status).toEqual(200)
1631 // Should still be 2 items, not 3
1632 expect(response.data.cart.items).toHaveLength(2)
1633 expect(response.data.cart.items).toEqual(
1634 expect.arrayContaining([
1635 // Original item without metadata should remain qty 1
1636 expect.objectContaining({
1637 variant_id: product.variants[0].id,
1638 quantity: 1,
1639 }),
1640 // Metadata item should be updated to qty 3 (1 + 2)
1641 expect.objectContaining({
1642 variant_id: product.variants[0].id,
1643 quantity: 3,
1644 metadata: { engraving: "Hello" },
1645 }),
1646 ])
1647 )
1648 })
1649
1650 it("should create a new line item when adding same variant with custom price that differs from existing custom price item", async () => {
1651 const variantId = product.variants[0].id
1652
1653 // Use the workflow directly to add an item with a custom unit_price
1654 await addToCartWorkflow(appContainer).run({
1655 input: {
1656 cart_id: cart.id,
1657 items: [
1658 {
1659 variant_id: variantId,
1660 quantity: 1,
1661 unit_price: 999,
1662 },
1663 ],
1664 },
1665 })
1666
1667 let response = await api.get(`/store/carts/${cart.id}`, storeHeaders)
1668
1669 // Should have 2 items: original (variant price) + custom price item
1670 expect(response.data.cart.items).toHaveLength(2)
1671
1672 // Add another item with the same variant but a different custom price
1673 await addToCartWorkflow(appContainer).run({
1674 input: {
1675 cart_id: cart.id,
1676 items: [
1677 {
1678 variant_id: variantId,
1679 quantity: 1,
1680 unit_price: 500,
1681 },
1682 ],
1683 },
1684 })
1685
1686 response = await api.get(`/store/carts/${cart.id}`, storeHeaders)
1687
1688 // Should now have 3 items: original + custom price 999 + custom price 500
1689 expect(response.data.cart.items).toHaveLength(3)
1690 expect(response.data.cart.items).toEqual(
1691 expect.arrayContaining([
1692 expect.objectContaining({
1693 variant_id: variantId,
1694 unit_price: 999,
1695 quantity: 1,
1696 }),
1697 expect.objectContaining({
1698 variant_id: variantId,
1699 unit_price: 500,
1700 quantity: 1,
1701 }),
1702 ])
1703 )
1704 })
1705
1706 it("should update quantity when adding same variant with matching custom price", async () => {
1707 const variantId = product.variants[0].id
1708
1709 // Add an item with a custom unit_price
1710 await addToCartWorkflow(appContainer).run({
1711 input: {
1712 cart_id: cart.id,
1713 items: [
1714 {
1715 variant_id: variantId,
1716 quantity: 1,
1717 unit_price: 999,
1718 },
1719 ],
1720 },
1721 })
1722
1723 let response = await api.get(`/store/carts/${cart.id}`, storeHeaders)
1724
1725 expect(response.data.cart.items).toHaveLength(2)
1726
1727 // Add same variant with the same custom price — should update quantity
1728 await addToCartWorkflow(appContainer).run({
1729 input: {
1730 cart_id: cart.id,
1731 items: [
1732 {
1733 variant_id: variantId,
1734 quantity: 2,
1735 unit_price: 999,
1736 },
1737 ],
1738 },
1739 })
1740
1741 response = await api.get(`/store/carts/${cart.id}`, storeHeaders)
1742
1743 // Should still be 2 items, custom price item quantity updated to 3
1744 expect(response.data.cart.items).toHaveLength(2)
1745 expect(response.data.cart.items).toEqual(
1746 expect.arrayContaining([
1747 expect.objectContaining({
1748 variant_id: variantId,
1749 unit_price: 999,
1750 quantity: 3,
1751 }),
1752 ])
1753 )
1754 })
1755
1756 it("should create a new line item when adding same variant with same metadata but different custom price", async () => {
1757 const variantId = product.variants[0].id
1758 const metadata = { note: "gift" }
1759
1760 // Add item with custom price and metadata
1761 await addToCartWorkflow(appContainer).run({
1762 input: {
1763 cart_id: cart.id,
1764 items: [
1765 {
1766 variant_id: variantId,
1767 quantity: 1,
1768 unit_price: 999,
1769 metadata,
1770 },
1771 ],
1772 },
1773 })
1774
1775 let response = await api.get(`/store/carts/${cart.id}`, storeHeaders)
1776
1777 expect(response.data.cart.items).toHaveLength(2)
1778
1779 // Add same variant with same metadata but different custom price
1780 // Should create a new line item, not update the existing one
1781 await addToCartWorkflow(appContainer).run({
1782 input: {
1783 cart_id: cart.id,
1784 items: [
1785 {
1786 variant_id: variantId,
1787 quantity: 1,
1788 unit_price: 500,
1789 metadata,
1790 },
1791 ],
1792 },
1793 })
1794
1795 response = await api.get(`/store/carts/${cart.id}`, storeHeaders)
1796
1797 // Should have 3 items: original + custom price 999 + custom price 500
1798 expect(response.data.cart.items).toHaveLength(3)
1799 expect(response.data.cart.items).toEqual(
1800 expect.arrayContaining([
1801 expect.objectContaining({
1802 variant_id: variantId,
1803 unit_price: 999,
1804 quantity: 1,
1805 metadata: { note: "gift" },
1806 }),
1807 expect.objectContaining({
1808 variant_id: variantId,
1809 unit_price: 500,
1810 quantity: 1,
1811 metadata: { note: "gift" },
1812 }),
1813 ])
1814 )
1815 })
1816
1817 it("should accumulate quantity when adding same variant with same metadata and same custom price", async () => {
1818 const variantId = product.variants[0].id
1819 const metadata = { note: "gift" }
1820
1821 // Add item with custom price and metadata
1822 await addToCartWorkflow(appContainer).run({
1823 input: {
1824 cart_id: cart.id,
1825 items: [
1826 {
1827 variant_id: variantId,
1828 quantity: 1,
1829 unit_price: 999,
1830 metadata,
1831 },
1832 ],
1833 },
1834 })
1835
1836 let response = await api.get(`/store/carts/${cart.id}`, storeHeaders)
1837
1838 // Should have 2 items: original + custom price with metadata
1839 expect(response.data.cart.items).toHaveLength(2)
1840
1841 // Add same variant with same metadata and same custom price at the same time
1842 // Should accumulate quantity on the existing line item, not create a duplicate
1843 await addToCartWorkflow(appContainer).run({
1844 input: {
1845 cart_id: cart.id,
1846 items: [
1847 {
1848 variant_id: variantId,
1849 quantity: 2,
1850 unit_price: 999,
1851 metadata,
1852 },
1853 {
1854 variant_id: variantId,
1855 quantity: 1,
1856 unit_price: 999,
1857 metadata,
1858 },
1859 ],
1860 },
1861 })
1862
1863 response = await api.get(`/store/carts/${cart.id}`, storeHeaders)
1864
1865 // Should still have 2 items, with the custom price item's quantity accumulated
1866 expect(response.data.cart.items).toHaveLength(2)
1867 expect(response.data.cart.items).toEqual(
1868 expect.arrayContaining([
1869 expect.objectContaining({
1870 variant_id: variantId,
1871 unit_price: 999,
1872 quantity: 4,
1873 metadata: { note: "gift" },
1874 }),
1875 ])
1876 )
1877 })
1878
1879 describe("with sale price lists", () => {
1880 beforeEach(async () => {
1881 await api.post(
1882 `/admin/price-lists`,
1883 {
1884 title: "test price list",
1885 description: "test",
1886 status: PriceListStatus.ACTIVE,
1887 type: PriceListType.SALE,
1888 prices: [
1889 {
1890 amount: 350,
1891 currency_code: "usd",
1892 variant_id: product.variants[0].id,
1893 },
1894 ],
1895 },
1896 adminHeaders
1897 )
1898
1899 const customerGroup = (
1900 await api.post(
1901 "/admin/customer-groups",
1902 { name: "VIP" },
1903 adminHeaders
1904 )
1905 ).data.customer_group
1906
1907 await api.post(
1908 `/admin/customer-groups/${customerGroup.id}/customers`,
1909 {
1910 add: [customer.id],
1911 },
1912 adminHeaders
1913 )
1914
1915 await api.post(
1916 `/admin/price-lists`,
1917 {
1918 title: "test price list",
1919 description: "test",
1920 status: PriceListStatus.ACTIVE,
1921 type: PriceListType.SALE,
1922 prices: [
1923 {
1924 amount: 200,
1925 currency_code: "usd",
1926 variant_id: product.variants[0].id,
1927 },
1928 ],
1929 rules: {
1930 "customer.groups.id": [customerGroup.id],
1931 },
1932 },
1933 adminHeaders
1934 )
1935 })
1936
1937 it("should add price from price list and set compare_at_unit_price", async () => {
1938 const response = await api.post(
1939 `/store/carts/${cart.id}/line-items`,
1940 {
1941 variant_id: product.variants[0].id,
1942 quantity: 1,
1943 },
1944 storeHeaders
1945 )
1946
1947 expect(response.status).toEqual(200)
1948 expect(response.data.cart).toEqual(
1949 expect.objectContaining({
1950 id: cart.id,
1951 currency_code: "usd",
1952 items: expect.arrayContaining([
1953 expect.objectContaining({
1954 unit_price: 350,
1955 compare_at_unit_price: 1500,
1956 is_tax_inclusive: true,
1957 quantity: 2,
1958 tax_lines: expect.arrayContaining([
1959 expect.objectContaining({
1960 description: "CA Default Rate",
1961 code: "CADEFAULT",
1962 rate: 5,
1963 provider_id: "system",
1964 }),
1965 ]),
1966 adjustments: expect.arrayContaining([
1967 expect.objectContaining({
1968 id: expect.any(String),
1969 code: "PROMOTION_APPLIED",
1970 promotion_id: promotion.id,
1971 amount: 100,
1972 }),
1973 ]),
1974 }),
1975 ]),
1976 })
1977 )
1978 })
1979
1980 it("should add price from price list associated to a customer group when customer rules match", async () => {
1981 const transferredCart = (
1982 await api.post(
1983 `/store/carts/${cart.id}/customer`,
1984 {},
1985 storeHeadersWithCustomer
1986 )
1987 ).data.cart
1988
1989 expect(transferredCart).toEqual(
1990 expect.objectContaining({
1991 id: cart.id,
1992 items: expect.arrayContaining([
1993 expect.objectContaining({
1994 unit_price: 200,
1995 compare_at_unit_price: 1500,
1996 is_tax_inclusive: true,
1997 quantity: 1,
1998 }),
1999 ]),
2000 })
2001 )
2002
2003 const response = await api.post(
2004 `/store/carts/${cart.id}/line-items`,
2005 {
2006 variant_id: product.variants[0].id,
2007 quantity: 1,
2008 },
2009 storeHeadersWithCustomer
2010 )
2011
2012 expect(response.status).toEqual(200)
2013 expect(response.data.cart).toEqual(
2014 expect.objectContaining({
2015 id: cart.id,
2016 currency_code: "usd",
2017 items: expect.arrayContaining([
2018 expect.objectContaining({
2019 unit_price: 200,
2020 compare_at_unit_price: 1500,
2021 is_tax_inclusive: true,
2022 quantity: 2,
2023 }),
2024 ]),
2025 })
2026 )
2027 })
2028 })
2029
2030 describe("with quantity based sale price lists", () => {
2031 let tieredVariantId
2032 let tieredCart
2033
2034 beforeEach(async () => {
2035 const tieredProduct = (
2036 await api.post(
2037 `/admin/products`,
2038 {
2039 title: "Medusa T-Shirt with tiered sale prices",
2040 handle: "t-shirt-with-tiered-sale-prices",
2041 status: ProductStatus.PUBLISHED,
2042 options: [{ title: "Size", values: ["S"] }],
2043 variants: [
2044 {
2045 title: "S",
2046 sku: "SHIRT-S-BLACK-w-tiered-sale-prices",
2047 options: { Size: "S" },
2048 manage_inventory: false,
2049 prices: [{ amount: 4000, currency_code: "usd" }],
2050 },
2051 ],
2052 },
2053 adminHeaders
2054 )
2055 ).data.product
2056
2057 tieredVariantId = tieredProduct.variants[0].id
2058
2059 await api.post(
2060 `/admin/price-lists`,
2061 {
2062 title: "tiered sale price list",
2063 description: "test",
2064 status: PriceListStatus.ACTIVE,
2065 type: PriceListType.SALE,
2066 prices: [
2067 {
2068 amount: 3799,
2069 currency_code: "usd",
2070 variant_id: tieredVariantId,
2071 min_quantity: 3,
2072 max_quantity: 4,
2073 },
2074 {
2075 amount: 3599,
2076 currency_code: "usd",
2077 variant_id: tieredVariantId,
2078 min_quantity: 5,
2079 },
2080 ],
2081 },
2082 adminHeaders
2083 )
2084
2085 tieredCart = (
2086 await api.post(
2087 `/store/carts`,
2088 {
2089 currency_code: "usd",
2090 sales_channel_id: salesChannel.id,
2091 region_id: region.id,
2092 shipping_address: shippingAddressData,
2093 },
2094 storeHeaders
2095 )
2096 ).data.cart
2097 })
2098
2099 it("should re-calculate the price of a merged line item based on the resulting quantity", async () => {
2100 let response
2101
2102 for (let i = 0; i < 3; i++) {
2103 response = await api.post(
2104 `/store/carts/${tieredCart.id}/line-items`,
2105 {
2106 variant_id: tieredVariantId,
2107 quantity: 1,
2108 },
2109 storeHeaders
2110 )
2111
2112 expect(response.status).toEqual(200)
2113 }
2114
2115 expect(response.data.cart.items).toHaveLength(1)
2116 expect(response.data.cart.items[0]).toEqual(
2117 expect.objectContaining({
2118 variant_id: tieredVariantId,
2119 quantity: 3,
2120 unit_price: 3799,
2121 compare_at_unit_price: 4000,
2122 })
2123 )
2124 })
2125
2126 it("should set compare_at_unit_price when a line item's quantity is updated", async () => {
2127 const itemId = (
2128 await api.post(
2129 `/store/carts/${tieredCart.id}/line-items`,
2130 {
2131 variant_id: tieredVariantId,
2132 quantity: 1,
2133 },
2134 storeHeaders
2135 )
2136 ).data.cart.items[0].id
2137
2138 let response = await api.post(
2139 `/store/carts/${tieredCart.id}/line-items/${itemId}`,
2140 { quantity: 3 },
2141 storeHeaders
2142 )
2143
2144 expect(response.status).toEqual(200)
2145 expect(response.data.cart.items[0]).toEqual(
2146 expect.objectContaining({
2147 quantity: 3,
2148 unit_price: 3799,
2149 compare_at_unit_price: 4000,
2150 })
2151 )
2152
2153 // Dropping back below the first tier resets the compare-at price
2154 response = await api.post(
2155 `/store/carts/${tieredCart.id}/line-items/${itemId}`,
2156 { quantity: 1 },
2157 storeHeaders
2158 )
2159
2160 expect(response.status).toEqual(200)
2161 expect(response.data.cart.items[0]).toEqual(
2162 expect.objectContaining({
2163 quantity: 1,
2164 unit_price: 4000,
2165 compare_at_unit_price: null,
2166 })
2167 )
2168 })
2169 })
2170
2171 describe("with manage_inventory true", () => {
2172 let inventoryItem
2173 beforeEach(async () => {
2174 await api.post(
2175 `/admin/products/${product.id}/variants/${product.variants[0].id}`,
2176 { manage_inventory: true },
2177 adminHeaders
2178 )
2179
2180 inventoryItem = (
2181 await api.post(
2182 `/admin/inventory-items`,
2183 { sku: "bottle" },
2184 adminHeaders
2185 )
2186 ).data.inventory_item
2187 })
2188
2189 describe("with allow_backorder true", () => {
2190 beforeEach(async () => {
2191 await api.post(
2192 `/admin/products/${product.id}/variants/${product.variants[0].id}`,
2193 { allow_backorder: true },
2194 adminHeaders
2195 )
2196 })
2197
2198 it("should add item to cart even if no inventory locations", async () => {
2199 const response = await api.post(
2200 `/store/carts/${cart.id}/line-items`,
2201 {
2202 variant_id: product.variants[0].id,
2203 quantity: 1,
2204 },
2205 storeHeaders
2206 )
2207
2208 expect(response.status).toEqual(200)
2209 })
2210
2211 it("should add item to cart even if inventory is empty", async () => {
2212 await api.post(
2213 `/admin/inventory-items/${inventoryItem.id}/location-levels/batch`,
2214 { create: [{ location_id: stockLocation.id }] },
2215 adminHeaders
2216 )
2217
2218 const response = await api.post(
2219 `/store/carts/${cart.id}/line-items`,
2220 {
2221 variant_id: product.variants[0].id,
2222 quantity: 1,
2223 },
2224 storeHeaders
2225 )
2226
2227 expect(response.status).toEqual(200)
2228 })
2229 })
2230 })
2231 })
2232
2233 describe("POST /store/carts/:id/line-items/:id", () => {
2234 let item
2235 let customerGroup
2236
2237 beforeEach(async () => {
2238 cart = (
2239 await api.post(
2240 `/store/carts`,
2241 {
2242 currency_code: "usd",
2243 sales_channel_id: salesChannel.id,
2244 region_id: region.id,
2245 shipping_address: shippingAddressData,
2246 items: [{ variant_id: product.variants[0].id, quantity: 1 }],
2247 },
2248 storeHeadersWithCustomer
2249 )
2250 ).data.cart
2251
2252 item = cart.items[0]
2253
2254 customerGroup = (
2255 await api.post(
2256 "/admin/customer-groups",
2257 { name: "VIP" },
2258 adminHeaders
2259 )
2260 ).data.customer_group
2261
2262 await api.post(
2263 `/admin/customer-groups/${customerGroup.id}/customers`,
2264 {
2265 add: [customer.id],
2266 },
2267 adminHeaders
2268 )
2269 })
2270
2271 it("should update cart's line item", async () => {
2272 let response = await api.post(
2273 `/store/carts/${cart.id}/line-items/${item.id}`,
2274 {
2275 quantity: 2,
2276 },
2277 storeHeaders
2278 )
2279
2280 expect(response.status).toEqual(200)
2281 expect(response.data.cart).toEqual(
2282 expect.objectContaining({
2283 id: cart.id,
2284 currency_code: "usd",
2285 items: expect.arrayContaining([
2286 expect.objectContaining({
2287 unit_price: 1500,
2288 quantity: 2,
2289 }),
2290 ]),
2291 })
2292 )
2293
2294 await api.post(
2295 `/admin/price-lists`,
2296 {
2297 title: "test price list",
2298 description: "test",
2299 status: PriceListStatus.ACTIVE,
2300 type: PriceListType.SALE,
2301 prices: [
2302 {
2303 amount: 200,
2304 currency_code: "usd",
2305 variant_id: product.variants[0].id,
2306 },
2307 ],
2308 rules: {
2309 "customer.groups.id": [customerGroup.id],
2310 },
2311 },
2312 adminHeaders
2313 )
2314
2315 response = await api.post(
2316 `/store/carts/${cart.id}/line-items/${item.id}`,
2317 { quantity: 3 },
2318 storeHeaders
2319 )
2320
2321 expect(response.status).toEqual(200)
2322 expect(response.data.cart).toEqual(
2323 expect.objectContaining({
2324 id: cart.id,
2325 currency_code: "usd",
2326 items: expect.arrayContaining([
2327 expect.objectContaining({
2328 unit_price: 200,
2329 quantity: 3,
2330 }),
2331 ]),
2332 })
2333 )
2334 })
2335 })
2336
2337 describe("POST /store/carts/:id/complete", () => {
2338 describe("should successfully complete cart", () => {
2339 beforeEach(async () => {
2340 const stockLocation = (
2341 await api.post(
2342 `/admin/stock-locations`,
2343 { name: "test location" },
2344 adminHeaders
2345 )
2346 ).data.stock_location
2347
2348 await api.post(
2349 `/admin/stock-locations/${stockLocation.id}/sales-channels`,
2350 { add: [salesChannel.id] },
2351 adminHeaders
2352 )
2353
2354 const fulfillmentSets = (
2355 await api.post(
2356 `/admin/stock-locations/${stockLocation.id}/fulfillment-sets?fields=*fulfillment_sets`,
2357 {
2358 name: `Test-${shippingProfile.id}`,
2359 type: "test-type",
2360 },
2361 adminHeaders
2362 )
2363 ).data.stock_location.fulfillment_sets
2364
2365 const fulfillmentSet = (
2366 await api.post(
2367 `/admin/fulfillment-sets/${fulfillmentSets[0].id}/service-zones`,
2368 {
2369 name: `Test-${shippingProfile.id}`,
2370 geo_zones: [{ type: "country", country_code: "US" }],
2371 },
2372 adminHeaders
2373 )
2374 ).data.fulfillment_set
2375
2376 await api.post(
2377 `/admin/stock-locations/${stockLocation.id}/fulfillment-providers`,
2378 { add: ["manual_test-provider"] },
2379 adminHeaders
2380 )
2381
2382 const shippingOption = (
2383 await api.post(
2384 `/admin/shipping-options`,
2385 {
2386 name: `Test shipping option ${fulfillmentSet.id}`,
2387 service_zone_id: fulfillmentSet.service_zones[0].id,
2388 shipping_profile_id: shippingProfile.id,
2389 provider_id: "manual_test-provider",
2390 price_type: "flat",
2391 type: {
2392 label: "Test type",
2393 description: "Test description",
2394 code: "test-code",
2395 },
2396 prices: [{ currency_code: "usd", amount: 1000 }],
2397 rules: [],
2398 },
2399 adminHeaders
2400 )
2401 ).data.shipping_option
2402
2403 cart = (
2404 await api.post(
2405 `/store/carts`,
2406 {
2407 currency_code: "usd",
2408 sales_channel_id: salesChannel.id,
2409 region_id: region.id,
2410 shipping_address: shippingAddressData,
2411 items: [{ variant_id: product.variants[0].id, quantity: 1 }],
2412 promo_codes: [promotion.code],
2413 },
2414 storeHeadersWithCustomer
2415 )
2416 ).data.cart
2417
2418 await api.post(
2419 `/store/carts/${cart.id}/shipping-methods`,
2420 { option_id: shippingOption.id },
2421 storeHeaders
2422 )
2423 })
2424
2425 it("should successfully complete cart and fail on concurrent complete", async () => {
2426 const paymentCollection = (
2427 await api.post(
2428 `/store/payment-collections`,
2429 { cart_id: cart.id },
2430 storeHeaders
2431 )
2432 ).data.payment_collection
2433
2434 await api.post(
2435 `/store/payment-collections/${paymentCollection.id}/payment-sessions`,
2436 { provider_id: "pp_system_default" },
2437 storeHeaders
2438 )
2439
2440 await createCartCreditLinesWorkflow.run({
2441 input: [
2442 {
2443 cart_id: cart.id,
2444 amount: 100,
2445 currency_code: "usd",
2446 reference: "test",
2447 reference_id: "test",
2448 },
2449 ],
2450 container: appContainer,
2451 })
2452
2453 // Concurrently complete the cart
2454 const completedCart: any[] = []
2455 for (let i = 0; i < 5; i++) {
2456 completedCart.push(
2457 api
2458 .post(`/store/carts/${cart.id}/complete`, {}, storeHeaders)
2459 .catch((e) => e)
2460 )
2461
2462 await setTimeout(25)
2463 }
2464
2465 const all = await Promise.all(completedCart)
2466
2467 const success = all.filter((res) => res.status === 200)
2468 const failure = all.filter((res) => res.status !== 200)
2469
2470 const successData = success[0].data.order
2471 for (const res of success) {
2472 expect(res.data.order).toEqual(successData)
2473 }
2474
2475 expect(failure.length).toBe(0)
2476
2477 expect(successData).toEqual(
2478 expect.objectContaining({
2479 id: expect.any(String),
2480 currency_code: "usd",
2481 credit_lines: [
2482 expect.objectContaining({
2483 amount: 100,
2484 reference: "test",
2485 reference_id: "test",
2486 }),
2487 ],
2488 items: expect.arrayContaining([
2489 expect.objectContaining({
2490 unit_price: 1500,
2491 compare_at_unit_price: null,
2492 quantity: 1,
2493 }),
2494 ]),
2495 })
2496 )
2497 })
2498
2499 it("should successfully complete cart", async () => {
2500 const paymentCollection = (
2501 await api.post(
2502 `/store/payment-collections`,
2503 { cart_id: cart.id },
2504 storeHeaders
2505 )
2506 ).data.payment_collection
2507
2508 await api.post(
2509 `/store/payment-collections/${paymentCollection.id}/payment-sessions`,
2510 { provider_id: "pp_system_default" },
2511 storeHeaders
2512 )
2513
2514 await createCartCreditLinesWorkflow.run({
2515 input: [
2516 {
2517 cart_id: cart.id,
2518 amount: 100,
2519 currency_code: "usd",
2520 reference: "test",
2521 reference_id: "test",
2522 },
2523 ],
2524 container: appContainer,
2525 })
2526
2527 const response = await api.post(
2528 `/store/carts/${cart.id}/complete`,
2529 {},
2530 storeHeaders
2531 )
2532
2533 expect(response.status).toEqual(200)
2534 expect(response.data.order).toEqual(
2535 expect.objectContaining({
2536 id: expect.any(String),
2537 currency_code: "usd",
2538 credit_lines: [
2539 expect.objectContaining({
2540 amount: 100,
2541 reference: "test",
2542 reference_id: "test",
2543 }),
2544 ],
2545 items: expect.arrayContaining([
2546 expect.objectContaining({
2547 unit_price: 1500,
2548 compare_at_unit_price: null,
2549 quantity: 1,
2550 }),
2551 ]),
2552 })
2553 )
2554 })
2555
2556 it("should successfully complete cart with pre existing captured payment session", async () => {
2557 const paymentModule = appContainer.resolve(Modules.PAYMENT)
2558
2559 const paymentCollection = (
2560 await api.post(
2561 `/store/payment-collections`,
2562 { cart_id: cart.id },
2563 storeHeaders
2564 )
2565 ).data.payment_collection
2566
2567 const paymentSession = await api
2568 .post(
2569 `/store/payment-collections/${paymentCollection.id}/payment-sessions`,
2570 { provider_id: "pp_system_default" },
2571 storeHeaders
2572 )
2573 .then((res) => res.data.payment_collection.payment_sessions[0])
2574
2575 // Authorize the payment session (creates a payment)
2576 const payment = await paymentModule.authorizePaymentSession(
2577 paymentSession.id,
2578 {}
2579 )
2580
2581 // Capture the payment
2582 await paymentModule.capturePayment({
2583 payment_id: payment.id,
2584 })
2585
2586 const updatedPaymentSession =
2587 await paymentModule.retrievePaymentSession(paymentSession.id, {
2588 relations: ["payment", "payment.captures"],
2589 })
2590 expect(updatedPaymentSession.payment.captures).toHaveLength(1)
2591 expect(updatedPaymentSession.status).toBe(
2592 PaymentSessionStatus.AUTHORIZED
2593 )
2594
2595 // Complete the cart
2596 const response = await api.post(
2597 `/store/carts/${cart.id}/complete`,
2598 {},
2599 storeHeaders
2600 )
2601
2602 expect(response.status).toEqual(200)
2603 expect(response.data.order).toEqual(
2604 expect.objectContaining({
2605 id: expect.any(String),
2606 currency_code: "usd",
2607 items: expect.arrayContaining([
2608 expect.objectContaining({
2609 unit_price: 1500,
2610 compare_at_unit_price: null,
2611 quantity: 1,
2612 }),
2613 ]),
2614 })
2615 )
2616 })
2617
2618 it("should successfully complete cart with credit lines alone", async () => {
2619 const oldCart = (
2620 await api.get(`/store/carts/${cart.id}`, storeHeaders)
2621 ).data.cart
2622
2623 createCartCreditLinesWorkflow.run({
2624 input: [
2625 {
2626 cart_id: oldCart.id,
2627 amount: oldCart.total,
2628 currency_code: "usd",
2629 reference: "test",
2630 reference_id: "test",
2631 },
2632 ],
2633 container: appContainer,
2634 })
2635
2636 const response = await api.post(
2637 `/store/carts/${cart.id}/complete`,
2638 {},
2639 storeHeaders
2640 )
2641
2642 expect(response.status).toEqual(200)
2643 expect(response.data.order).toEqual(
2644 expect.objectContaining({
2645 id: expect.any(String),
2646 currency_code: "usd",
2647 credit_line_total: 2395,
2648 discount_total: 105,
2649 credit_lines: [
2650 expect.objectContaining({
2651 amount: 2395,
2652 }),
2653 ],
2654 items: expect.arrayContaining([
2655 expect.objectContaining({
2656 unit_price: 1500,
2657 compare_at_unit_price: null,
2658 quantity: 1,
2659 }),
2660 ]),
2661 })
2662 )
2663 })
2664
2665 it("should successfully complete cart with promotions", async () => {
2666 const oldCart = (
2667 await api.get(`/store/carts/${cart.id}`, storeHeaders)
2668 ).data.cart
2669
2670 createCartCreditLinesWorkflow.run({
2671 input: [
2672 {
2673 cart_id: oldCart.id,
2674 amount: oldCart.total,
2675 currency_code: "usd",
2676 reference: "test",
2677 reference_id: "test",
2678 },
2679 ],
2680 container: appContainer,
2681 })
2682
2683 const cartResponse = await api.post(
2684 `/store/carts/${cart.id}/complete`,
2685 {},
2686 storeHeaders
2687 )
2688
2689 const orderResponse = await api.get(
2690 `/store/orders/${cartResponse.data.order.id}?fields=+promotions.*`,
2691 storeHeaders
2692 )
2693
2694 expect(cartResponse.status).toEqual(200)
2695 expect(orderResponse.data.order).toEqual(
2696 expect.objectContaining({
2697 promotions: [
2698 expect.objectContaining({
2699 code: promotion.code,
2700 }),
2701 ],
2702 items: expect.arrayContaining([
2703 expect.objectContaining({
2704 unit_price: 1500,
2705 compare_at_unit_price: null,
2706 quantity: 1,
2707 adjustments: expect.arrayContaining([
2708 expect.objectContaining({
2709 amount: 100,
2710 code: promotion.code,
2711 }),
2712 ]),
2713 }),
2714 ]),
2715 })
2716 )
2717 })
2718
2719 it("should fail to complete a cart if that would exceed the promotion limit", async () => {
2720 const product = (
2721 await api.post(
2722 `/admin/products`,
2723 {
2724 status: ProductStatus.PUBLISHED,
2725 title: "Product for camapign",
2726 description: "test",
2727 options: [
2728 {
2729 title: "Type",
2730 values: ["L"],
2731 },
2732 ],
2733 variants: [
2734 {
2735 title: "L",
2736 sku: "campaign-product-l",
2737 options: {
2738 Type: "L",
2739 },
2740 manage_inventory: false,
2741 prices: [
2742 {
2743 amount: 300,
2744 currency_code: "usd",
2745 },
2746 ],
2747 },
2748 ],
2749 },
2750 adminHeaders
2751 )
2752 ).data.product
2753
2754 const campaign = (
2755 await api.post(
2756 `/admin/campaigns`,
2757 {
2758 name: "TEST-1",
2759 budget: {
2760 type: "spend",
2761 currency_code: "usd",
2762 limit: 100, // -> promotions value can't exceed 100$
2763 },
2764 campaign_identifier: "PROMO_CAMPAIGN",
2765 },
2766 adminHeaders
2767 )
2768 ).data.campaign
2769
2770 const promotion = (
2771 await api
2772 .post(
2773 `/admin/promotions`,
2774 {
2775 code: "TEST_PROMO",
2776 type: PromotionType.STANDARD,
2777 status: PromotionStatus.ACTIVE,
2778 is_automatic: false,
2779 is_tax_inclusive: true,
2780 application_method: {
2781 target_type: "items",
2782 type: "fixed",
2783 allocation: "across",
2784 currency_code: "usd",
2785 value: 100, // -> promotion applies 100$ fixed discount on the entire order
2786 },
2787 campaign_id: campaign.id,
2788 },
2789 adminHeaders
2790 )
2791 .catch((e) => console.log(e))
2792 ).data.promotion
2793
2794 const cart1 = (
2795 await api.post(
2796 `/store/carts`,
2797 {
2798 currency_code: "usd",
2799 sales_channel_id: salesChannel.id,
2800 region_id: region.id,
2801 shipping_address: shippingAddressData,
2802 items: [{ variant_id: product.variants[0].id, quantity: 1 }],
2803 promo_codes: [promotion.code],
2804 },
2805 storeHeadersWithCustomer
2806 )
2807 ).data.cart
2808
2809 expect(cart1).toEqual(
2810 expect.objectContaining({
2811 promotions: [
2812 expect.objectContaining({
2813 code: promotion.code,
2814 }),
2815 ],
2816 })
2817 )
2818
2819 const cart2 = (
2820 await api.post(
2821 `/store/carts`,
2822 {
2823 currency_code: "usd",
2824 sales_channel_id: salesChannel.id,
2825 region_id: region.id,
2826 shipping_address: shippingAddressData,
2827 items: [{ variant_id: product.variants[0].id, quantity: 1 }],
2828 promo_codes: [promotion.code],
2829 },
2830 storeHeadersWithCustomer
2831 )
2832 ).data.cart
2833
2834 expect(cart2).toEqual(
2835 expect.objectContaining({
2836 promotions: [
2837 expect.objectContaining({
2838 code: promotion.code,
2839 }),
2840 ],
2841 })
2842 )
2843
2844 /**
2845 * At this point both carts have the same promotion applied successfully
2846 */
2847
2848 const paymentCollection1 = (
2849 await api.post(
2850 `/store/payment-collections`,
2851 { cart_id: cart1.id },
2852 storeHeaders
2853 )
2854 ).data.payment_collection
2855
2856 await api.post(
2857 `/store/payment-collections/${paymentCollection1.id}/payment-sessions`,
2858 { provider_id: "pp_system_default" },
2859 storeHeaders
2860 )
2861
2862 const order1 = (
2863 await api.post(
2864 `/store/carts/${cart1.id}/complete`,
2865 {},
2866 storeHeaders
2867 )
2868 ).data.order
2869
2870 expect(order1).toEqual(
2871 expect.objectContaining({ discount_total: 100 })
2872 )
2873
2874 let campaignAfter = (
2875 await api.get(
2876 `/admin/campaigns/${campaign.id}?fields=budget.*`,
2877 adminHeaders
2878 )
2879 ).data.campaign
2880
2881 expect(campaignAfter).toEqual(
2882 expect.objectContaining({
2883 budget: expect.objectContaining({
2884 used: 100,
2885 limit: 100,
2886 }),
2887 })
2888 )
2889
2890 const paymentCollection2 = (
2891 await api.post(
2892 `/store/payment-collections`,
2893 { cart_id: cart2.id },
2894 storeHeaders
2895 )
2896 ).data.payment_collection
2897
2898 await api.post(
2899 `/store/payment-collections/${paymentCollection2.id}/payment-sessions`,
2900 { provider_id: "pp_system_default" },
2901 storeHeaders
2902 )
2903
2904 const response2 = await api
2905 .post(`/store/carts/${cart2.id}/complete`, {}, storeHeaders)
2906 .catch((e) => e)
2907
2908 expect(response2.response.status).toEqual(400)
2909 expect(response2.response.data).toEqual(
2910 expect.objectContaining({
2911 type: "not_allowed",
2912 message: "Promotion usage exceeds the budget limit.",
2913 })
2914 )
2915
2916 campaignAfter = (
2917 await api.get(
2918 `/admin/campaigns/${campaign.id}?fields=budget.*`,
2919 adminHeaders
2920 )
2921 ).data.campaign
2922
2923 expect(campaignAfter).toEqual(
2924 expect.objectContaining({
2925 budget: expect.objectContaining({
2926 used: 100,
2927 limit: 100,
2928 }),
2929 })
2930 )
2931 })
2932
2933 it("should successfully complete cart without shipping for digital products", async () => {
2934 /**
2935 * Product has a shipping profile so cart item should not require shipping
2936 */
2937 const product = (
2938 await api.post(
2939 `/admin/products`,
2940 {
2941 title: "Product without inventory management",
2942 description: "test",
2943 status: ProductStatus.PUBLISHED,
2944 options: [
2945 {
2946 title: "Size",
2947 values: ["S", "M", "L", "XL"],
2948 },
2949 ],
2950 variants: [
2951 {
2952 title: "S / Black",
2953 sku: "special-shirt",
2954 options: {
2955 Size: "S",
2956 },
2957 manage_inventory: false,
2958 prices: [
2959 {
2960 amount: 1500,
2961 currency_code: "usd",
2962 },
2963 ],
2964 },
2965 ],
2966 },
2967 adminHeaders
2968 )
2969 ).data.product
2970
2971 cart = (
2972 await api.post(
2973 `/store/carts`,
2974 {
2975 currency_code: "usd",
2976 sales_channel_id: salesChannel.id,
2977 region_id: region.id,
2978 shipping_address: shippingAddressData,
2979 },
2980 storeHeadersWithCustomer
2981 )
2982 ).data.cart
2983
2984 cart = (
2985 await api.post(
2986 `/store/carts/${cart.id}/line-items`,
2987 {
2988 variant_id: product.variants[0].id,
2989 quantity: 1,
2990 },
2991 storeHeaders
2992 )
2993 ).data.cart
2994
2995 const paymentCollection = (
2996 await api.post(
2997 `/store/payment-collections`,
2998 { cart_id: cart.id },
2999 storeHeaders
3000 )
3001 ).data.payment_collection
3002
3003 await api.post(
3004 `/store/payment-collections/${paymentCollection.id}/payment-sessions`,
3005 { provider_id: "pp_system_default" },
3006 storeHeaders
3007 )
3008
3009 expect(cart.items[0].requires_shipping).toEqual(false)
3010
3011 const response = await api.post(
3012 `/store/carts/${cart.id}/complete`,
3013 {},
3014 storeHeaders
3015 )
3016
3017 expect(response.status).toEqual(200)
3018 expect(response.data.order).toEqual(
3019 expect.objectContaining({
3020 shipping_methods: [],
3021 items: expect.arrayContaining([
3022 expect.objectContaining({
3023 requires_shipping: false,
3024 }),
3025 ]),
3026 })
3027 )
3028 })
3029
3030 describe("with sale price lists", () => {
3031 let priceList
3032
3033 beforeEach(async () => {
3034 priceList = (
3035 await api.post(
3036 `/admin/price-lists`,
3037 {
3038 title: "test price list",
3039 description: "test",
3040 status: PriceListStatus.ACTIVE,
3041 type: PriceListType.SALE,
3042 prices: [
3043 {
3044 amount: 350,
3045 currency_code: "usd",
3046 variant_id: product.variants[0].id,
3047 },
3048 ],
3049 },
3050 adminHeaders
3051 )
3052 ).data.price_list
3053
3054 await api.post(
3055 `/store/carts/${cart.id}/line-items`,
3056 { variant_id: product.variants[0].id, quantity: 1 },
3057 storeHeaders
3058 )
3059
3060 const paymentCollection = (
3061 await api.post(
3062 `/store/payment-collections`,
3063 { cart_id: cart.id },
3064 storeHeaders
3065 )
3066 ).data.payment_collection
3067
3068 await api.post(
3069 `/store/payment-collections/${paymentCollection.id}/payment-sessions`,
3070 { provider_id: "pp_system_default" },
3071 storeHeaders
3072 )
3073 })
3074
3075 it("should add price from price list and set compare_at_unit_price for order item", async () => {
3076 const response = await api.post(
3077 `/store/carts/${cart.id}/complete`,
3078 {},
3079 storeHeaders
3080 )
3081
3082 expect(response.status).toEqual(200)
3083 expect(response.data.order).toEqual(
3084 expect.objectContaining({
3085 items: expect.arrayContaining([
3086 expect.objectContaining({
3087 unit_price: 350,
3088 compare_at_unit_price: 1500,
3089 is_tax_inclusive: true,
3090 quantity: 2,
3091 }),
3092 ]),
3093 })
3094 )
3095 })
3096 })
3097
3098 describe("with inventory kit", () => {
3099 let stockLocation
3100 let inventoryItem
3101 let product
3102 let cart
3103 beforeEach(async () => {
3104 stockLocation = (
3105 await api.post(
3106 `/admin/stock-locations`,
3107 { name: "test location" },
3108 adminHeaders
3109 )
3110 ).data.stock_location
3111
3112 inventoryItem = (
3113 await api.post(
3114 `/admin/inventory-items`,
3115 { sku: "bottle" },
3116 adminHeaders
3117 )
3118 ).data.inventory_item
3119
3120 await api.post(
3121 `/admin/inventory-items/${inventoryItem.id}/location-levels`,
3122 {
3123 location_id: stockLocation.id,
3124 stocked_quantity: 10,
3125 },
3126 adminHeaders
3127 )
3128
3129 await api.post(
3130 `/admin/stock-locations/${stockLocation.id}/sales-channels`,
3131 { add: [salesChannel.id] },
3132 adminHeaders
3133 )
3134
3135 product = (
3136 await api.post(
3137 "/admin/products",
3138 {
3139 title: `Test fixture ${shippingProfile.id}`,
3140 status: ProductStatus.PUBLISHED,
3141 shipping_profile_id: shippingProfile.id,
3142 options: [
3143 { title: "pack", values: ["1-pack", "2-pack", "3-pack"] },
3144 ],
3145 variants: [
3146 {
3147 title: "2-pack",
3148 sku: "2-pack",
3149 inventory_items: [
3150 {
3151 inventory_item_id: inventoryItem.id,
3152 required_quantity: 2,
3153 },
3154 ],
3155 prices: [
3156 {
3157 currency_code: "usd",
3158 amount: 100,
3159 },
3160 ],
3161 options: {
3162 pack: "2-pack",
3163 },
3164 },
3165 {
3166 title: "3-pack",
3167 sku: "3-pack",
3168 inventory_items: [
3169 {
3170 inventory_item_id: inventoryItem.id,
3171 required_quantity: 3,
3172 },
3173 ],
3174 prices: [
3175 {
3176 currency_code: "usd",
3177 amount: 140,
3178 },
3179 ],
3180 options: {
3181 pack: "3-pack",
3182 },
3183 },
3184 ],
3185 },
3186 adminHeaders
3187 )
3188 ).data.product
3189
3190 cart = (
3191 await api.post(
3192 `/store/carts`,
3193 {
3194 currency_code: "usd",
3195 sales_channel_id: salesChannel.id,
3196 region_id: region.id,
3197 shipping_address: shippingAddressData,
3198 items: [
3199 { variant_id: product.variants[0].id, quantity: 1 },
3200 { variant_id: product.variants[1].id, quantity: 1 },
3201 ],
3202 },
3203 storeHeadersWithCustomer
3204 )
3205 ).data.cart
3206
3207 const fulfillmentSets = (
3208 await api.post(
3209 `/admin/stock-locations/${stockLocation.id}/fulfillment-sets?fields=*fulfillment_sets`,
3210 {
3211 name: `Test-inventory`,
3212 type: "test-type",
3213 },
3214 adminHeaders
3215 )
3216 ).data.stock_location.fulfillment_sets
3217
3218 const fulfillmentSet = (
3219 await api.post(
3220 `/admin/fulfillment-sets/${fulfillmentSets[0].id}/service-zones`,
3221 {
3222 name: `Test-inventory`,
3223 geo_zones: [{ type: "country", country_code: "US" }],
3224 },
3225 adminHeaders
3226 )
3227 ).data.fulfillment_set
3228
3229 await api.post(
3230 `/admin/stock-locations/${stockLocation.id}/fulfillment-providers`,
3231 { add: ["manual_test-provider"] },
3232 adminHeaders
3233 )
3234
3235 const shippingOption = (
3236 await api.post(
3237 `/admin/shipping-options`,
3238 {
3239 name: `Test shipping option ${fulfillmentSet.id}`,
3240 service_zone_id: fulfillmentSet.service_zones[0].id,
3241 shipping_profile_id: shippingProfile.id,
3242 provider_id: "manual_test-provider",
3243 price_type: "flat",
3244 type: {
3245 label: "Test type",
3246 description: "Test description",
3247 code: "test-code",
3248 },
3249 prices: [{ currency_code: "usd", amount: 1000 }],
3250 rules: [],
3251 },
3252 adminHeaders
3253 )
3254 ).data.shipping_option
3255
3256 await api.post(
3257 `/store/carts/${cart.id}/shipping-methods`,
3258 { option_id: shippingOption.id },
3259 storeHeaders
3260 )
3261
3262 const paymentCollection = (
3263 await api.post(
3264 `/store/payment-collections`,
3265 { cart_id: cart.id },
3266 storeHeaders
3267 )
3268 ).data.payment_collection
3269
3270 await api.post(
3271 `/store/payment-collections/${paymentCollection.id}/payment-sessions`,
3272 { provider_id: "pp_system_default" },
3273 storeHeaders
3274 )
3275 })
3276
3277 it("should complete a cart with inventory item shared between variants", async () => {
3278 const response = await api.post(
3279 `/store/carts/${cart.id}/complete`,
3280 {},
3281 storeHeaders
3282 )
3283
3284 expect(response.status).toEqual(200)
3285 expect(response.data.order).toEqual(
3286 expect.objectContaining({
3287 items: expect.arrayContaining([
3288 expect.objectContaining({
3289 subtitle: "2-pack",
3290 quantity: 1,
3291 }),
3292 expect.objectContaining({
3293 subtitle: "3-pack",
3294 quantity: 1,
3295 }),
3296 ]),
3297 })
3298 )
3299
3300 const reservations = (
3301 await api.get(`/admin/reservations`, adminHeaders)
3302 ).data.reservations
3303
3304 expect(reservations).toEqual(
3305 expect.arrayContaining([
3306 expect.objectContaining({
3307 location_id: stockLocation.id,
3308 inventory_item_id: inventoryItem.id,
3309 quantity: 2, // 2-pack
3310 inventory_item: expect.objectContaining({
3311 id: inventoryItem.id,
3312 sku: "bottle",
3313 reserved_quantity: 5,
3314 stocked_quantity: 10,
3315 }),
3316 }),
3317 expect.objectContaining({
3318 location_id: stockLocation.id,
3319 inventory_item_id: inventoryItem.id,
3320 quantity: 3, // 3-pack
3321 inventory_item: expect.objectContaining({
3322 id: inventoryItem.id,
3323 sku: "bottle",
3324 reserved_quantity: 5,
3325 stocked_quantity: 10,
3326 }),
3327 }),
3328 ])
3329 )
3330 })
3331 })
3332
3333 describe("with fractional inventory kit", () => {
3334 let stockLocation
3335 let inventoryItem
3336 let product
3337 let cart
3338 let quarterPound
3339 let halfPound
3340 let pound
3341
3342 beforeEach(async () => {
3343 stockLocation = (
3344 await api.post(
3345 `/admin/stock-locations`,
3346 { name: "test location" },
3347 adminHeaders
3348 )
3349 ).data.stock_location
3350
3351 inventoryItem = (
3352 await api.post(
3353 `/admin/inventory-items`,
3354 { sku: "strain", unit_of_measure: "lb" },
3355 adminHeaders
3356 )
3357 ).data.inventory_item
3358
3359 await api.post(
3360 `/admin/inventory-items/${inventoryItem.id}/location-levels`,
3361 {
3362 location_id: stockLocation.id,
3363 stocked_quantity: 100,
3364 },
3365 adminHeaders
3366 )
3367
3368 await api.post(
3369 `/admin/stock-locations/${stockLocation.id}/sales-channels`,
3370 { add: [salesChannel.id] },
3371 adminHeaders
3372 )
3373
3374 // Three sizes sold out of a single shared pound pool
3375 product = (
3376 await api.post(
3377 "/admin/products",
3378 {
3379 title: `Test fractional fixture ${shippingProfile.id}`,
3380 status: ProductStatus.PUBLISHED,
3381 shipping_profile_id: shippingProfile.id,
3382 options: [
3383 { title: "size", values: ["quarter", "half", "pound"] },
3384 ],
3385 variants: [
3386 {
3387 title: "quarter-pound",
3388 sku: "quarter-pound",
3389 inventory_items: [
3390 {
3391 inventory_item_id: inventoryItem.id,
3392 required_quantity: 0.25,
3393 },
3394 ],
3395 prices: [{ currency_code: "usd", amount: 100 }],
3396 options: { size: "quarter" },
3397 },
3398 {
3399 title: "half-pound",
3400 sku: "half-pound",
3401 inventory_items: [
3402 {
3403 inventory_item_id: inventoryItem.id,
3404 required_quantity: 0.5,
3405 },
3406 ],
3407 prices: [{ currency_code: "usd", amount: 180 }],
3408 options: { size: "half" },
3409 },
3410 {
3411 title: "pound",
3412 sku: "pound",
3413 inventory_items: [
3414 {
3415 inventory_item_id: inventoryItem.id,
3416 required_quantity: 1,
3417 },
3418 ],
3419 prices: [{ currency_code: "usd", amount: 340 }],
3420 options: { size: "pound" },
3421 },
3422 ],
3423 },
3424 adminHeaders
3425 )
3426 ).data.product
3427
3428 quarterPound = product.variants.find(
3429 (v) => v.sku === "quarter-pound"
3430 )
3431 halfPound = product.variants.find((v) => v.sku === "half-pound")
3432 pound = product.variants.find((v) => v.sku === "pound")
3433
3434 cart = (
3435 await api.post(
3436 `/store/carts`,
3437 {
3438 currency_code: "usd",
3439 sales_channel_id: salesChannel.id,
3440 region_id: region.id,
3441 shipping_address: shippingAddressData,
3442 items: [
3443 { variant_id: quarterPound.id, quantity: 1 },
3444 { variant_id: halfPound.id, quantity: 1 },
3445 { variant_id: pound.id, quantity: 1 },
3446 ],
3447 },
3448 storeHeadersWithCustomer
3449 )
3450 ).data.cart
3451
3452 const fulfillmentSets = (
3453 await api.post(
3454 `/admin/stock-locations/${stockLocation.id}/fulfillment-sets?fields=*fulfillment_sets`,
3455 {
3456 name: `Test-fractional-inventory`,
3457 type: "test-type",
3458 },
3459 adminHeaders
3460 )
3461 ).data.stock_location.fulfillment_sets
3462
3463 const fulfillmentSet = (
3464 await api.post(
3465 `/admin/fulfillment-sets/${fulfillmentSets[0].id}/service-zones`,
3466 {
3467 name: `Test-fractional-inventory`,
3468 geo_zones: [{ type: "country", country_code: "US" }],
3469 },
3470 adminHeaders
3471 )
3472 ).data.fulfillment_set
3473
3474 await api.post(
3475 `/admin/stock-locations/${stockLocation.id}/fulfillment-providers`,
3476 { add: ["manual_test-provider"] },
3477 adminHeaders
3478 )
3479
3480 const shippingOption = (
3481 await api.post(
3482 `/admin/shipping-options`,
3483 {
3484 name: `Test shipping option ${fulfillmentSet.id}`,
3485 service_zone_id: fulfillmentSet.service_zones[0].id,
3486 shipping_profile_id: shippingProfile.id,
3487 provider_id: "manual_test-provider",
3488 price_type: "flat",
3489 type: {
3490 label: "Test type",
3491 description: "Test description",
3492 code: "test-code",
3493 },
3494 prices: [{ currency_code: "usd", amount: 1000 }],
3495 rules: [],
3496 },
3497 adminHeaders
3498 )
3499 ).data.shipping_option
3500
3501 await api.post(
3502 `/store/carts/${cart.id}/shipping-methods`,
3503 { option_id: shippingOption.id },
3504 storeHeaders
3505 )
3506
3507 const paymentCollection = (
3508 await api.post(
3509 `/store/payment-collections`,
3510 { cart_id: cart.id },
3511 storeHeaders
3512 )
3513 ).data.payment_collection
3514
3515 await api.post(
3516 `/store/payment-collections/${paymentCollection.id}/payment-sessions`,
3517 { provider_id: "pp_system_default" },
3518 storeHeaders
3519 )
3520 })
3521
3522 it("should respect fractional availability when adding line items", async () => {
3523 // Drop the pool down to a single pound
3524 await api.post(
3525 `/admin/inventory-items/${inventoryItem.id}/location-levels/${stockLocation.id}`,
3526 { stocked_quantity: 1 },
3527 adminHeaders
3528 )
3529
3530 const emptyCart = (
3531 await api.post(
3532 `/store/carts`,
3533 {
3534 currency_code: "usd",
3535 sales_channel_id: salesChannel.id,
3536 region_id: region.id,
3537 shipping_address: shippingAddressData,
3538 },
3539 storeHeadersWithCustomer
3540 )
3541 ).data.cart
3542
3543 // 4 x 0.25 lb consumes exactly the 1 lb available
3544 const response = await api.post(
3545 `/store/carts/${emptyCart.id}/line-items`,
3546 { variant_id: quarterPound.id, quantity: 4 },
3547 storeHeaders
3548 )
3549
3550 expect(response.status).toEqual(200)
3551
3552 // a fifth quarter pound would need 1.25 lb
3553 const { response: errorResponse } = await api
3554 .post(
3555 `/store/carts/${emptyCart.id}/line-items`,
3556 { variant_id: quarterPound.id, quantity: 1 },
3557 storeHeaders
3558 )
3559 .catch((e) => e)
3560
3561 expect(errorResponse.status).toEqual(400)
3562 expect(errorResponse.data.message).toEqual(
3563 "Some variant does not have the required inventory"
3564 )
3565 })
3566
3567 it("should complete a cart consuming fractional quantities of a shared inventory item", async () => {
3568 const response = await api.post(
3569 `/store/carts/${cart.id}/complete`,
3570 {},
3571 storeHeaders
3572 )
3573
3574 expect(response.status).toEqual(200)
3575 expect(response.data.order).toEqual(
3576 expect.objectContaining({
3577 items: expect.arrayContaining([
3578 expect.objectContaining({
3579 subtitle: "quarter-pound",
3580 quantity: 1,
3581 }),
3582 expect.objectContaining({
3583 subtitle: "half-pound",
3584 quantity: 1,
3585 }),
3586 expect.objectContaining({
3587 subtitle: "pound",
3588 quantity: 1,
3589 }),
3590 ]),
3591 })
3592 )
3593
3594 const reservations = (
3595 await api.get(`/admin/reservations`, adminHeaders)
3596 ).data.reservations
3597
3598 // 0.25 + 0.5 + 1 = 1.75 lb reserved against a 100 lb pool
3599 const reservedRollup = expect.objectContaining({
3600 id: inventoryItem.id,
3601 sku: "strain",
3602 unit_of_measure: "lb",
3603 reserved_quantity: 1.75,
3604 stocked_quantity: 100,
3605 })
3606
3607 expect(reservations).toEqual(
3608 expect.arrayContaining([
3609 expect.objectContaining({
3610 location_id: stockLocation.id,
3611 inventory_item_id: inventoryItem.id,
3612 quantity: 0.25, // quarter-pound
3613 inventory_item: reservedRollup,
3614 }),
3615 expect.objectContaining({
3616 location_id: stockLocation.id,
3617 inventory_item_id: inventoryItem.id,
3618 quantity: 0.5, // half-pound
3619 inventory_item: reservedRollup,
3620 }),
3621 expect.objectContaining({
3622 location_id: stockLocation.id,
3623 inventory_item_id: inventoryItem.id,
3624 quantity: 1, // pound
3625 inventory_item: reservedRollup,
3626 }),
3627 ])
3628 )
3629
3630 const levels = (
3631 await api.get(
3632 `/admin/inventory-items/${inventoryItem.id}/location-levels`,
3633 adminHeaders
3634 )
3635 ).data.inventory_levels
3636
3637 expect(levels).toEqual([
3638 expect.objectContaining({
3639 location_id: stockLocation.id,
3640 stocked_quantity: 100,
3641 reserved_quantity: 1.75,
3642 available_quantity: 98.25,
3643 }),
3644 ])
3645 })
3646 })
3647 })
3648
3649 it("should preserve tax line data field on order after cart completion", async () => {
3650 const taxService = appContainer.resolve(Modules.TAX)
3651 await taxService.createTaxRegions([
3652 {
3653 country_code: "GB",
3654 provider_id: "tp_tax-data-provider_data-provider",
3655 default_tax_rate: {
3656 name: "GB Standard Rate",
3657 rate: 20,
3658 code: "GB_STD",
3659 },
3660 },
3661 ])
3662
3663 const gbRegion = (
3664 await api.post(
3665 "/admin/regions",
3666 {
3667 payment_providers: ["pp_system_default"],
3668 name: "GB Tax Data",
3669 currency_code: "gbp",
3670 countries: ["gb"],
3671 },
3672 adminHeaders
3673 )
3674 ).data.region
3675
3676 const gbProduct = (
3677 await api.post(
3678 "/admin/products",
3679 {
3680 title: "GB Tax Test Product",
3681 status: ProductStatus.PUBLISHED,
3682 options: [{ title: "Size", values: ["S"] }],
3683 variants: [
3684 {
3685 title: "S",
3686 manage_inventory: false,
3687 options: { Size: "S" },
3688 prices: [{ amount: 1000, currency_code: "gbp" }],
3689 },
3690 ],
3691 },
3692 adminHeaders
3693 )
3694 ).data.product
3695
3696 const gbCart = (
3697 await api.post(
3698 "/store/carts",
3699 {
3700 currency_code: "gbp",
3701 region_id: gbRegion.id,
3702 sales_channel_id: salesChannel.id,
3703 shipping_address: {
3704 address_1: "1 Oxford Street",
3705 city: "London",
3706 country_code: "GB",
3707 postal_code: "W1D 1AN",
3708 },
3709 items: [{ variant_id: gbProduct.variants[0].id, quantity: 1 }],
3710 },
3711 storeHeaders
3712 )
3713 ).data.cart
3714
3715 const paymentCollection = (
3716 await api.post(
3717 "/store/payment-collections",
3718 { cart_id: gbCart.id },
3719 storeHeaders
3720 )
3721 ).data.payment_collection
3722
3723 await api.post(
3724 `/store/payment-collections/${paymentCollection.id}/payment-sessions`,
3725 { provider_id: "pp_system_default" },
3726 storeHeaders
3727 )
3728
3729 const response = await api.post(
3730 `/store/carts/${gbCart.id}/complete`,
3731 {},
3732 storeHeaders
3733 )
3734
3735 expect(response.status).toEqual(200)
3736
3737 const order = (
3738 await api.get(
3739 `/admin/orders/${response.data.order.id}?fields=items.tax_lines.*`,
3740 adminHeaders
3741 )
3742 ).data.order
3743
3744 expect(order.items[0].tax_lines).toEqual(
3745 expect.arrayContaining([
3746 expect.objectContaining({
3747 code: "GB_STD",
3748 provider_id: "tax-data-provider",
3749 data: { state_rate: 4.0, county_rate: 3.0, city_rate: 1.9 },
3750 }),
3751 ])
3752 )
3753 })
3754
3755 describe("empty cart validation", () => {
3756 it("should fail to complete an empty cart via the store endpoint", async () => {
3757 const emptyCart = (
3758 await api.post(
3759 `/store/carts`,
3760 {
3761 currency_code: "usd",
3762 sales_channel_id: salesChannel.id,
3763 region_id: region.id,
3764 shipping_address: shippingAddressData,
3765 },
3766 storeHeadersWithCustomer
3767 )
3768 ).data.cart
3769
3770 const paymentCollection = (
3771 await api.post(
3772 `/store/payment-collections`,
3773 { cart_id: emptyCart.id },
3774 storeHeaders
3775 )
3776 ).data.payment_collection
3777
3778 await api.post(
3779 `/store/payment-collections/${paymentCollection.id}/payment-sessions`,
3780 { provider_id: "pp_system_default" },
3781 storeHeaders
3782 )
3783
3784 const error = await api
3785 .post(`/store/carts/${emptyCart.id}/complete`, {}, storeHeaders)
3786 .catch((e) => e)
3787
3788 expect(error.response.status).toEqual(400)
3789 expect(error.response.data.message).toEqual(
3790 "Cannot complete a cart with no items"
3791 )
3792 })
3793
3794 it("should successfully complete a cart with items (regression guard)", async () => {
3795 const cartWithItems = (
3796 await api.post(
3797 `/store/carts`,
3798 {
3799 currency_code: "usd",
3800 sales_channel_id: salesChannel.id,
3801 region_id: region.id,
3802 shipping_address: shippingAddressData,
3803 items: [{ variant_id: product.variants[0].id, quantity: 1 }],
3804 },
3805 storeHeadersWithCustomer
3806 )
3807 ).data.cart
3808
3809 const stockLocation = (
3810 await api.post(
3811 `/admin/stock-locations`,
3812 { name: "test location regression" },
3813 adminHeaders
3814 )
3815 ).data.stock_location
3816
3817 await api.post(
3818 `/admin/stock-locations/${stockLocation.id}/sales-channels`,
3819 { add: [salesChannel.id] },
3820 adminHeaders
3821 )
3822
3823 const fulfillmentSets = (
3824 await api.post(
3825 `/admin/stock-locations/${stockLocation.id}/fulfillment-sets?fields=*fulfillment_sets`,
3826 {
3827 name: `Test-regression-${shippingProfile.id}`,
3828 type: "test-type",
3829 },
3830 adminHeaders
3831 )
3832 ).data.stock_location.fulfillment_sets
3833
3834 const fulfillmentSet = (
3835 await api.post(
3836 `/admin/fulfillment-sets/${fulfillmentSets[0].id}/service-zones`,
3837 {
3838 name: `Test-regression-${shippingProfile.id}`,
3839 geo_zones: [{ type: "country", country_code: "US" }],
3840 },
3841 adminHeaders
3842 )
3843 ).data.fulfillment_set
3844
3845 await api.post(
3846 `/admin/stock-locations/${stockLocation.id}/fulfillment-providers`,
3847 { add: ["manual_test-provider"] },
3848 adminHeaders
3849 )
3850
3851 const shippingOption = (
3852 await api.post(
3853 `/admin/shipping-options`,
3854 {
3855 name: `Test shipping regression ${fulfillmentSet.id}`,
3856 service_zone_id: fulfillmentSet.service_zones[0].id,
3857 shipping_profile_id: shippingProfile.id,
3858 provider_id: "manual_test-provider",
3859 price_type: "flat",
3860 type: {
3861 label: "Test type",
3862 description: "Test description",
3863 code: "test-code",
3864 },
3865 prices: [{ currency_code: "usd", amount: 1000 }],
3866 rules: [],
3867 },
3868 adminHeaders
3869 )
3870 ).data.shipping_option
3871
3872 await api.post(
3873 `/store/carts/${cartWithItems.id}/shipping-methods`,
3874 { option_id: shippingOption.id },
3875 storeHeaders
3876 )
3877
3878 const paymentCollection = (
3879 await api.post(
3880 `/store/payment-collections`,
3881 { cart_id: cartWithItems.id },
3882 storeHeaders
3883 )
3884 ).data.payment_collection
3885
3886 await api.post(
3887 `/store/payment-collections/${paymentCollection.id}/payment-sessions`,
3888 { provider_id: "pp_system_default" },
3889 storeHeaders
3890 )
3891
3892 const response = await api.post(
3893 `/store/carts/${cartWithItems.id}/complete`,
3894 {},
3895 storeHeaders
3896 )
3897
3898 expect(response.status).toEqual(200)
3899 expect(response.data.type).toEqual("order")
3900 expect(response.data.order).toEqual(
3901 expect.objectContaining({
3902 id: expect.any(String),
3903 currency_code: "usd",
3904 items: expect.arrayContaining([
3905 expect.objectContaining({
3906 quantity: 1,
3907 }),
3908 ]),
3909 })
3910 )
3911 })
3912 })
3913
3914 describe("shipping validation", () => {
3915 it("should fail to complete the cart if no shipping method is selected and items require shipping", async () => {
3916 const cart = (
3917 await api.post(
3918 `/store/carts`,
3919 {
3920 currency_code: "usd",
3921 sales_channel_id: salesChannel.id,
3922 region_id: region.id,
3923 shipping_address: shippingAddressData,
3924 items: [{ variant_id: product.variants[0].id, quantity: 1 }],
3925 promo_codes: [promotion.code],
3926 },
3927 storeHeadersWithCustomer
3928 )
3929 ).data.cart
3930
3931 const paymentCollection = (
3932 await api.post(
3933 `/store/payment-collections`,
3934 { cart_id: cart.id },
3935 storeHeaders
3936 )
3937 ).data.payment_collection
3938
3939 await api.post(
3940 `/store/payment-collections/${paymentCollection.id}/payment-sessions`,
3941 { provider_id: "pp_system_default" },
3942 storeHeaders
3943 )
3944
3945 const response = await api
3946 .post(`/store/carts/${cart.id}/complete`, {}, storeHeaders)
3947 .catch((e) => e)
3948
3949 expect(response.response.status).toEqual(400)
3950 expect(response.response.data.message).toEqual(
3951 "No shipping method selected but the cart contains items that require shipping."
3952 )
3953 })
3954
3955 it("should fail to complete the cart if the shipping profile of a product is not supported by the shipping method", async () => {
3956 const stockLocation = (
3957 await api.post(
3958 `/admin/stock-locations`,
3959 { name: "test location" },
3960 adminHeaders
3961 )
3962 ).data.stock_location
3963
3964 await api.post(
3965 `/admin/stock-locations/${stockLocation.id}/sales-channels`,
3966 { add: [salesChannel.id] },
3967 adminHeaders
3968 )
3969
3970 const shippingProfile = (
3971 await api.post(
3972 `/admin/shipping-profiles`,
3973 { name: `test-${stockLocation.id}`, type: "default" },
3974 adminHeaders
3975 )
3976 ).data.shipping_profile
3977
3978 const fulfillmentSets = (
3979 await api.post(
3980 `/admin/stock-locations/${stockLocation.id}/fulfillment-sets?fields=*fulfillment_sets`,
3981 {
3982 name: `Test-${shippingProfile.id}`,
3983 type: "test-type",
3984 },
3985 adminHeaders
3986 )
3987 ).data.stock_location.fulfillment_sets
3988
3989 const fulfillmentSet = (
3990 await api.post(
3991 `/admin/fulfillment-sets/${fulfillmentSets[0].id}/service-zones`,
3992 {
3993 name: `Test-${shippingProfile.id}`,
3994 geo_zones: [{ type: "country", country_code: "US" }],
3995 },
3996 adminHeaders
3997 )
3998 ).data.fulfillment_set
3999
4000 await api.post(
4001 `/admin/stock-locations/${stockLocation.id}/fulfillment-providers`,
4002 { add: ["manual_test-provider"] },
4003 adminHeaders
4004 )
4005
4006 const shippingOption = (
4007 await api.post(
4008 `/admin/shipping-options`,
4009 {
4010 name: `Test shipping option ${fulfillmentSet.id}`,
4011 service_zone_id: fulfillmentSet.service_zones[0].id,
4012 shipping_profile_id: shippingProfile.id,
4013 provider_id: "manual_test-provider",
4014 price_type: "flat",
4015 type: {
4016 label: "Test type",
4017 description: "Test description",
4018 code: "test-code",
4019 },
4020 prices: [{ currency_code: "usd", amount: 1000 }],
4021 rules: [],
4022 },
4023 adminHeaders
4024 )
4025 ).data.shipping_option
4026
4027 const specialShippingProfile = (
4028 await api.post(
4029 `/admin/shipping-profiles`,
4030 { name: "special-shipping-profile", type: "special" },
4031 adminHeaders
4032 )
4033 ).data.shipping_profile
4034
4035 const product = (
4036 await api.post(
4037 `/admin/products`,
4038 {
4039 title: "test product",
4040 status: ProductStatus.PUBLISHED,
4041 description: "test",
4042 options: [
4043 {
4044 title: "Size",
4045 values: ["S", "M", "L", "XL"],
4046 },
4047 ],
4048 variants: [
4049 {
4050 title: "S / Black",
4051 sku: "special-shirt",
4052 options: {
4053 Size: "S",
4054 },
4055 manage_inventory: false,
4056 prices: [
4057 {
4058 amount: 1500,
4059 currency_code: "usd",
4060 },
4061 ],
4062 },
4063 ],
4064 shipping_profile_id: specialShippingProfile.id, // --> product has a different shipping profile than
4065 },
4066 adminHeaders
4067 )
4068 ).data.product
4069
4070 const cart = (
4071 await api.post(
4072 `/store/carts`,
4073 {
4074 currency_code: "usd",
4075 sales_channel_id: salesChannel.id,
4076 region_id: region.id,
4077 shipping_address: shippingAddressData,
4078 items: [{ variant_id: product.variants[0].id, quantity: 1 }],
4079 promo_codes: [promotion.code],
4080 },
4081 storeHeadersWithCustomer
4082 )
4083 ).data.cart
4084
4085 await api.post(
4086 `/store/carts/${cart.id}/shipping-methods`,
4087 { option_id: shippingOption.id },
4088 storeHeaders
4089 )
4090
4091 const paymentCollection = (
4092 await api.post(
4093 `/store/payment-collections`,
4094 { cart_id: cart.id },
4095 storeHeaders
4096 )
4097 ).data.payment_collection
4098
4099 await api.post(
4100 `/store/payment-collections/${paymentCollection.id}/payment-sessions`,
4101 { provider_id: "pp_system_default" },
4102 storeHeaders
4103 )
4104
4105 const response = await api
4106 .post(`/store/carts/${cart.id}/complete`, {}, storeHeaders)
4107 .catch((e) => e)
4108
4109 expect(response.response.status).toEqual(400)
4110 expect(response.response.data.message).toEqual(
4111 "The cart items require shipping profiles that are not satisfied by the current shipping methods"
4112 )
4113 })
4114
4115 it("should complete a cart that spans two shipping profiles when each profile has a shipping method", async () => {
4116 // --- Profile A setup ---
4117 const stockLocationA = (
4118 await api.post(
4119 `/admin/stock-locations`,
4120 { name: "location-profile-a" },
4121 adminHeaders
4122 )
4123 ).data.stock_location
4124
4125 await api.post(
4126 `/admin/stock-locations/${stockLocationA.id}/sales-channels`,
4127 { add: [salesChannel.id] },
4128 adminHeaders
4129 )
4130
4131 const shippingProfileA = (
4132 await api.post(
4133 `/admin/shipping-profiles`,
4134 { name: "profile-a", type: "default" },
4135 adminHeaders
4136 )
4137 ).data.shipping_profile
4138
4139 const fulfillmentSetA = (
4140 await api.post(
4141 `/admin/stock-locations/${stockLocationA.id}/fulfillment-sets?fields=*fulfillment_sets`,
4142 { name: "fs-profile-a", type: "test-type" },
4143 adminHeaders
4144 )
4145 ).data.stock_location.fulfillment_sets[0]
4146
4147 const serviceZoneA = (
4148 await api.post(
4149 `/admin/fulfillment-sets/${fulfillmentSetA.id}/service-zones`,
4150 {
4151 name: "sz-profile-a",
4152 geo_zones: [{ type: "country", country_code: "US" }],
4153 },
4154 adminHeaders
4155 )
4156 ).data.fulfillment_set.service_zones[0]
4157
4158 await api.post(
4159 `/admin/stock-locations/${stockLocationA.id}/fulfillment-providers`,
4160 { add: ["manual_test-provider"] },
4161 adminHeaders
4162 )
4163
4164 const shippingOptionA = (
4165 await api.post(
4166 `/admin/shipping-options`,
4167 {
4168 name: "shipping-option-a",
4169 service_zone_id: serviceZoneA.id,
4170 shipping_profile_id: shippingProfileA.id,
4171 provider_id: "manual_test-provider",
4172 price_type: "flat",
4173 type: {
4174 label: "Standard",
4175 description: "Standard",
4176 code: "standard",
4177 },
4178 prices: [{ currency_code: "usd", amount: 500 }],
4179 rules: [],
4180 },
4181 adminHeaders
4182 )
4183 ).data.shipping_option
4184
4185 const productA = (
4186 await api.post(
4187 `/admin/products`,
4188 {
4189 title: "Product Profile A",
4190 status: ProductStatus.PUBLISHED,
4191 options: [{ title: "Size", values: ["One"] }],
4192 variants: [
4193 {
4194 title: "One",
4195 sku: "product-a-sku",
4196 options: { Size: "One" },
4197 manage_inventory: false,
4198 prices: [{ amount: 1000, currency_code: "usd" }],
4199 },
4200 ],
4201 shipping_profile_id: shippingProfileA.id,
4202 },
4203 adminHeaders
4204 )
4205 ).data.product
4206
4207 // --- Profile B setup ---
4208 const stockLocationB = (
4209 await api.post(
4210 `/admin/stock-locations`,
4211 { name: "location-profile-b" },
4212 adminHeaders
4213 )
4214 ).data.stock_location
4215
4216 await api.post(
4217 `/admin/stock-locations/${stockLocationB.id}/sales-channels`,
4218 { add: [salesChannel.id] },
4219 adminHeaders
4220 )
4221
4222 const shippingProfileB = (
4223 await api.post(
4224 `/admin/shipping-profiles`,
4225 { name: "profile-b", type: "default" },
4226 adminHeaders
4227 )
4228 ).data.shipping_profile
4229
4230 const fulfillmentSetB = (
4231 await api.post(
4232 `/admin/stock-locations/${stockLocationB.id}/fulfillment-sets?fields=*fulfillment_sets`,
4233 { name: "fs-profile-b", type: "test-type" },
4234 adminHeaders
4235 )
4236 ).data.stock_location.fulfillment_sets[0]
4237
4238 const serviceZoneB = (
4239 await api.post(
4240 `/admin/fulfillment-sets/${fulfillmentSetB.id}/service-zones`,
4241 {
4242 name: "sz-profile-b",
4243 geo_zones: [{ type: "country", country_code: "US" }],
4244 },
4245 adminHeaders
4246 )
4247 ).data.fulfillment_set.service_zones[0]
4248
4249 await api.post(
4250 `/admin/stock-locations/${stockLocationB.id}/fulfillment-providers`,
4251 { add: ["manual_test-provider"] },
4252 adminHeaders
4253 )
4254
4255 const shippingOptionB = (
4256 await api.post(
4257 `/admin/shipping-options`,
4258 {
4259 name: "shipping-option-b",
4260 service_zone_id: serviceZoneB.id,
4261 shipping_profile_id: shippingProfileB.id,
4262 provider_id: "manual_test-provider",
4263 price_type: "flat",
4264 type: {
4265 label: "Express",
4266 description: "Express",
4267 code: "express",
4268 },
4269 prices: [{ currency_code: "usd", amount: 1000 }],
4270 rules: [],
4271 },
4272 adminHeaders
4273 )
4274 ).data.shipping_option
4275
4276 const productB = (
4277 await api.post(
4278 `/admin/products`,
4279 {
4280 title: "Product Profile B",
4281 status: ProductStatus.PUBLISHED,
4282 options: [{ title: "Size", values: ["One"] }],
4283 variants: [
4284 {
4285 title: "One",
4286 sku: "product-b-sku",
4287 options: { Size: "One" },
4288 manage_inventory: false,
4289 prices: [{ amount: 2000, currency_code: "usd" }],
4290 },
4291 ],
4292 shipping_profile_id: shippingProfileB.id,
4293 },
4294 adminHeaders
4295 )
4296 ).data.product
4297
4298 // --- Cart with items from both profiles ---
4299 const cart = (
4300 await api.post(
4301 `/store/carts`,
4302 {
4303 currency_code: "usd",
4304 sales_channel_id: salesChannel.id,
4305 region_id: region.id,
4306 shipping_address: shippingAddressData,
4307 items: [
4308 { variant_id: productA.variants[0].id, quantity: 1 },
4309 { variant_id: productB.variants[0].id, quantity: 1 },
4310 ],
4311 },
4312 storeHeadersWithCustomer
4313 )
4314 ).data.cart
4315
4316 // Add shipping method for profile A
4317 await api.post(
4318 `/store/carts/${cart.id}/shipping-methods`,
4319 { option_id: shippingOptionA.id },
4320 storeHeaders
4321 )
4322
4323 // Add shipping method for profile B — with the fix this should NOT remove profile A's method
4324 const cartAfterBothMethods = (
4325 await api.post(
4326 `/store/carts/${cart.id}/shipping-methods`,
4327 { option_id: shippingOptionB.id },
4328 storeHeaders
4329 )
4330 ).data.cart
4331
4332 // Both methods must be present and shipping total must reflect both (500 + 1000 = 1500)
4333 expect(cartAfterBothMethods.shipping_methods).toHaveLength(2)
4334 expect(
4335 cartAfterBothMethods.shipping_methods
4336 .map((sm) => sm.shipping_option_id)
4337 .sort()
4338 ).toEqual([shippingOptionA.id, shippingOptionB.id].sort())
4339 expect(cartAfterBothMethods.shipping_total).toEqual(1500)
4340
4341 const paymentCollection = (
4342 await api.post(
4343 `/store/payment-collections`,
4344 { cart_id: cart.id },
4345 storeHeaders
4346 )
4347 ).data.payment_collection
4348
4349 await api.post(
4350 `/store/payment-collections/${paymentCollection.id}/payment-sessions`,
4351 { provider_id: "pp_system_default" },
4352 storeHeaders
4353 )
4354
4355 const response = await api.post(
4356 `/store/carts/${cart.id}/complete`,
4357 {},
4358 storeHeaders
4359 )
4360
4361 expect(response.status).toEqual(200)
4362 expect(response.data.type).toEqual("order")
4363 expect(response.data.order).toEqual(
4364 expect.objectContaining({
4365 id: expect.any(String),
4366 shipping_total: 1500,
4367 })
4368 )
4369 })
4370
4371 it("should auto-remove a shipping method when the last item for its profile is removed from the cart", async () => {
4372 // --- Profile A setup ---
4373 const stockLocationA = (
4374 await api.post(
4375 `/admin/stock-locations`,
4376 { name: "location-orphan-a" },
4377 adminHeaders
4378 )
4379 ).data.stock_location
4380
4381 await api.post(
4382 `/admin/stock-locations/${stockLocationA.id}/sales-channels`,
4383 { add: [salesChannel.id] },
4384 adminHeaders
4385 )
4386
4387 const shippingProfileA = (
4388 await api.post(
4389 `/admin/shipping-profiles`,
4390 { name: "profile-orphan-a", type: "default" },
4391 adminHeaders
4392 )
4393 ).data.shipping_profile
4394
4395 const fulfillmentSetA = (
4396 await api.post(
4397 `/admin/stock-locations/${stockLocationA.id}/fulfillment-sets?fields=*fulfillment_sets`,
4398 { name: "fs-orphan-a", type: "test-type" },
4399 adminHeaders
4400 )
4401 ).data.stock_location.fulfillment_sets[0]
4402
4403 const serviceZoneA = (
4404 await api.post(
4405 `/admin/fulfillment-sets/${fulfillmentSetA.id}/service-zones`,
4406 {
4407 name: "sz-orphan-a",
4408 geo_zones: [{ type: "country", country_code: "US" }],
4409 },
4410 adminHeaders
4411 )
4412 ).data.fulfillment_set.service_zones[0]
4413
4414 await api.post(
4415 `/admin/stock-locations/${stockLocationA.id}/fulfillment-providers`,
4416 { add: ["manual_test-provider"] },
4417 adminHeaders
4418 )
4419
4420 const shippingOptionA = (
4421 await api.post(
4422 `/admin/shipping-options`,
4423 {
4424 name: "shipping-option-orphan-a",
4425 service_zone_id: serviceZoneA.id,
4426 shipping_profile_id: shippingProfileA.id,
4427 provider_id: "manual_test-provider",
4428 price_type: "flat",
4429 type: {
4430 label: "Standard",
4431 description: "Standard",
4432 code: "standard",
4433 },
4434 prices: [{ currency_code: "usd", amount: 500 }],
4435 rules: [],
4436 },
4437 adminHeaders
4438 )
4439 ).data.shipping_option
4440
4441 const productA = (
4442 await api.post(
4443 `/admin/products`,
4444 {
4445 title: "Product Orphan Profile A",
4446 status: ProductStatus.PUBLISHED,
4447 options: [{ title: "Size", values: ["One"] }],
4448 variants: [
4449 {
4450 title: "One",
4451 sku: "product-orphan-a-sku",
4452 options: { Size: "One" },
4453 manage_inventory: false,
4454 prices: [{ amount: 1000, currency_code: "usd" }],
4455 },
4456 ],
4457 shipping_profile_id: shippingProfileA.id,
4458 },
4459 adminHeaders
4460 )
4461 ).data.product
4462
4463 // --- Profile B setup ---
4464 const stockLocationB = (
4465 await api.post(
4466 `/admin/stock-locations`,
4467 { name: "location-orphan-b" },
4468 adminHeaders
4469 )
4470 ).data.stock_location
4471
4472 await api.post(
4473 `/admin/stock-locations/${stockLocationB.id}/sales-channels`,
4474 { add: [salesChannel.id] },
4475 adminHeaders
4476 )
4477
4478 const shippingProfileB = (
4479 await api.post(
4480 `/admin/shipping-profiles`,
4481 { name: "profile-orphan-b", type: "default" },
4482 adminHeaders
4483 )
4484 ).data.shipping_profile
4485
4486 const fulfillmentSetB = (
4487 await api.post(
4488 `/admin/stock-locations/${stockLocationB.id}/fulfillment-sets?fields=*fulfillment_sets`,
4489 { name: "fs-orphan-b", type: "test-type" },
4490 adminHeaders
4491 )
4492 ).data.stock_location.fulfillment_sets[0]
4493
4494 const serviceZoneB = (
4495 await api.post(
4496 `/admin/fulfillment-sets/${fulfillmentSetB.id}/service-zones`,
4497 {
4498 name: "sz-orphan-b",
4499 geo_zones: [{ type: "country", country_code: "US" }],
4500 },
4501 adminHeaders
4502 )
4503 ).data.fulfillment_set.service_zones[0]
4504
4505 await api.post(
4506 `/admin/stock-locations/${stockLocationB.id}/fulfillment-providers`,
4507 { add: ["manual_test-provider"] },
4508 adminHeaders
4509 )
4510
4511 const shippingOptionB = (
4512 await api.post(
4513 `/admin/shipping-options`,
4514 {
4515 name: "shipping-option-orphan-b",
4516 service_zone_id: serviceZoneB.id,
4517 shipping_profile_id: shippingProfileB.id,
4518 provider_id: "manual_test-provider",
4519 price_type: "flat",
4520 type: {
4521 label: "Express",
4522 description: "Express",
4523 code: "express",
4524 },
4525 prices: [{ currency_code: "usd", amount: 1000 }],
4526 rules: [],
4527 },
4528 adminHeaders
4529 )
4530 ).data.shipping_option
4531
4532 const productB = (
4533 await api.post(
4534 `/admin/products`,
4535 {
4536 title: "Product Orphan Profile B",
4537 status: ProductStatus.PUBLISHED,
4538 options: [{ title: "Size", values: ["One"] }],
4539 variants: [
4540 {
4541 title: "One",
4542 sku: "product-orphan-b-sku",
4543 options: { Size: "One" },
4544 manage_inventory: false,
4545 prices: [{ amount: 2000, currency_code: "usd" }],
4546 },
4547 ],
4548 shipping_profile_id: shippingProfileB.id,
4549 },
4550 adminHeaders
4551 )
4552 ).data.product
4553
4554 // --- Cart with items from both profiles ---
4555 const cart = (
4556 await api.post(
4557 `/store/carts`,
4558 {
4559 currency_code: "usd",
4560 sales_channel_id: salesChannel.id,
4561 region_id: region.id,
4562 shipping_address: shippingAddressData,
4563 items: [
4564 { variant_id: productA.variants[0].id, quantity: 1 },
4565 { variant_id: productB.variants[0].id, quantity: 1 },
4566 ],
4567 },
4568 storeHeadersWithCustomer
4569 )
4570 ).data.cart
4571
4572 // Add shipping methods for both profiles
4573 await api.post(
4574 `/store/carts/${cart.id}/shipping-methods`,
4575 [
4576 { option_id: shippingOptionA.id },
4577 { option_id: shippingOptionB.id },
4578 ],
4579 storeHeaders
4580 )
4581
4582 // Confirm both shipping methods are present
4583 const cartWithBothMethods = (
4584 await api.get(`/store/carts/${cart.id}`, storeHeaders)
4585 ).data.cart
4586
4587 expect(cartWithBothMethods.shipping_methods).toHaveLength(2)
4588
4589 // Find the line item for profile A's product
4590 const lineItemA = cartWithBothMethods.items.find(
4591 (item) => item.variant_id === productA.variants[0].id
4592 )
4593
4594 // Remove the last item belonging to profile A (quantity 0 triggers removal)
4595 await api.post(
4596 `/store/carts/${cart.id}/line-items/${lineItemA.id}`,
4597 { quantity: 0 },
4598 storeHeaders
4599 )
4600
4601 // Profile A's shipping method should have been auto-removed
4602 const cartAfterRemoval = (
4603 await api.get(`/store/carts/${cart.id}`, storeHeaders)
4604 ).data.cart
4605
4606 expect(cartAfterRemoval.shipping_methods).toHaveLength(1)
4607 expect(
4608 cartAfterRemoval.shipping_methods[0].shipping_option_id
4609 ).toEqual(shippingOptionB.id)
4610 })
4611 })
4612 })
4613
4614 describe("POST /store/carts/:id", () => {
4615 let otherRegion
4616
4617 beforeEach(async () => {
4618 const cartData = {
4619 currency_code: "usd",
4620 sales_channel_id: salesChannel.id,
4621 region_id: region.id,
4622 shipping_address: shippingAddressData,
4623 items: [{ variant_id: product.variants[0].id, quantity: 1 }],
4624 promo_codes: [promotion.code],
4625 }
4626
4627 cart = (await api.post(`/store/carts`, cartData, storeHeaders)).data
4628 .cart
4629
4630 otherRegion = (
4631 await api.post(
4632 "/admin/regions",
4633 {
4634 payment_providers: ["pp_system_default"],
4635 name: "dk",
4636 currency_code: "dkk",
4637 countries: ["dk"],
4638 },
4639 adminHeaders
4640 )
4641 ).data.region
4642 })
4643
4644 it("should update prices when region is changed", async () => {
4645 let updated = await api.post(
4646 `/store/carts/${cart.id}/line-items`,
4647 { variant_id: product.variants[0].id, quantity: 1 },
4648 storeHeaders
4649 )
4650
4651 expect(updated.status).toEqual(200)
4652 expect(updated.data.cart).toEqual(
4653 expect.objectContaining({
4654 id: cart.id,
4655 currency_code: "usd",
4656 items: [
4657 expect.objectContaining({
4658 unit_price: 1500,
4659 quantity: 2,
4660 }),
4661 ],
4662 })
4663 )
4664
4665 updated = await api.post(
4666 `/store/carts/${cart.id}`,
4667 { region_id: otherRegion.id },
4668 storeHeaders
4669 )
4670
4671 expect(updated.status).toEqual(200)
4672 expect(updated.data.cart).toEqual(
4673 expect.objectContaining({
4674 id: cart.id,
4675 currency_code: "dkk",
4676 items: [
4677 expect.objectContaining({
4678 unit_price: 1300,
4679 quantity: 2,
4680 }),
4681 ],
4682 })
4683 )
4684
4685 updated = await api.post(
4686 `/store/carts/${cart.id}/line-items`,
4687 { variant_id: product.variants[0].id, quantity: 1 },
4688 storeHeaders
4689 )
4690
4691 expect(updated.status).toEqual(200)
4692 expect(updated.data.cart).toEqual(
4693 expect.objectContaining({
4694 id: cart.id,
4695 currency_code: "dkk",
4696 items: [
4697 expect.objectContaining({
4698 unit_price: 1300,
4699 quantity: 3,
4700 }),
4701 ],
4702 })
4703 )
4704 })
4705
4706 it("should update a cart with promo codes with a replace action", async () => {
4707 const newPromotion = (
4708 await api.post(
4709 `/admin/promotions`,
4710 {
4711 code: "PROMOTION_TEST",
4712 type: PromotionType.STANDARD,
4713 status: PromotionStatus.ACTIVE,
4714 application_method: {
4715 type: "fixed",
4716 target_type: "items",
4717 allocation: "across",
4718 currency_code: "usd",
4719 value: 1000,
4720 apply_to_quantity: 1,
4721 target_rules: [
4722 {
4723 attribute: "items.product_id",
4724 operator: PromotionRuleOperator.IN,
4725 values: [product.id],
4726 },
4727 ],
4728 },
4729 },
4730 adminHeaders
4731 )
4732 ).data.promotion
4733
4734 await api.post(
4735 `/store/carts/${cart.id}/line-items`,
4736 {
4737 variant_id: product.variants[0].id,
4738 quantity: 1,
4739 },
4740 storeHeaders
4741 )
4742
4743 // Should remove earlier adjustments from other promocodes
4744 let updated = await api.post(
4745 `/store/carts/${cart.id}`,
4746 { promo_codes: [newPromotion.code] },
4747 storeHeaders
4748 )
4749
4750 expect(updated.status).toEqual(200)
4751 expect(updated.data.cart).toEqual(
4752 expect.objectContaining({
4753 id: cart.id,
4754 items: [
4755 expect.objectContaining({
4756 adjustments: [
4757 expect.objectContaining({
4758 code: newPromotion.code,
4759 }),
4760 ],
4761 }),
4762 ],
4763 })
4764 )
4765
4766 // Should remove all adjustments from other promo codes
4767 updated = await api.post(
4768 `/store/carts/${cart.id}`,
4769 { promo_codes: [] },
4770 storeHeaders
4771 )
4772
4773 expect(updated.status).toEqual(200)
4774 expect(updated.data.cart).toEqual(
4775 expect.objectContaining({
4776 id: cart.id,
4777 items: [
4778 expect.objectContaining({
4779 adjustments: [],
4780 }),
4781 ],
4782 })
4783 )
4784 })
4785
4786 it("should not add a promotion that belongs to a different currency than the cart", async () => {
4787 const newPromotion = (
4788 await api.post(
4789 `/admin/promotions`,
4790 {
4791 code: "PROMOTION_TEST",
4792 type: PromotionType.STANDARD,
4793 status: PromotionStatus.ACTIVE,
4794 application_method: {
4795 type: "fixed",
4796 target_type: "items",
4797 allocation: "across",
4798 // Set for EUR currency, different from USD Currency of the cart
4799 currency_code: "eur",
4800 value: 1000,
4801 apply_to_quantity: 1,
4802 },
4803 },
4804 adminHeaders
4805 )
4806 ).data.promotion
4807
4808 await api.post(
4809 `/store/carts/${cart.id}/line-items`,
4810 {
4811 variant_id: product.variants[0].id,
4812 quantity: 1,
4813 },
4814 storeHeaders
4815 )
4816
4817 const updated = await api.post(
4818 `/store/carts/${cart.id}`,
4819 { promo_codes: [newPromotion.code] },
4820 storeHeaders
4821 )
4822
4823 expect(updated.status).toEqual(200)
4824 expect(updated.data.cart).toEqual(
4825 expect.objectContaining({
4826 id: cart.id,
4827 items: [
4828 expect.objectContaining({
4829 adjustments: [],
4830 }),
4831 ],
4832 })
4833 )
4834 })
4835
4836 it("should not generate tax lines if automatic taxes is false", async () => {
4837 let updated = await api.post(
4838 `/store/carts/${cart.id}`,
4839 {},
4840 storeHeaders
4841 )
4842
4843 expect(updated.status).toEqual(200)
4844 expect(updated.data.cart).toEqual(
4845 expect.objectContaining({
4846 id: cart.id,
4847 items: [
4848 expect.objectContaining({
4849 tax_lines: [
4850 expect.objectContaining({
4851 description: "CA Default Rate",
4852 code: "CADEFAULT",
4853 rate: 5,
4854 provider_id: "system",
4855 }),
4856 ],
4857 }),
4858 ],
4859 })
4860 )
4861
4862 updated = await api.post(
4863 `/store/carts/${cart.id}`,
4864 { region_id: noAutomaticRegion.id },
4865 storeHeaders
4866 )
4867
4868 expect(updated.status).toEqual(200)
4869 expect(updated.data.cart).toEqual(
4870 expect.objectContaining({
4871 id: cart.id,
4872 items: [
4873 expect.objectContaining({
4874 tax_lines: [],
4875 }),
4876 ],
4877 })
4878 )
4879 })
4880
4881 it("should refresh tax lines when the shipping address province changes", async () => {
4882 let updated = await api.post(
4883 `/store/carts/${cart.id}`,
4884 {},
4885 storeHeaders
4886 )
4887
4888 expect(updated.data.cart).toEqual(
4889 expect.objectContaining({
4890 id: cart.id,
4891 items: [
4892 expect.objectContaining({
4893 tax_lines: [
4894 expect.objectContaining({
4895 description: "CA Default Rate",
4896 code: "CADEFAULT",
4897 rate: 5,
4898 }),
4899 ],
4900 }),
4901 ],
4902 })
4903 )
4904
4905 updated = await api.post(
4906 `/store/carts/${cart.id}`,
4907 {
4908 shipping_address: {
4909 ...shippingAddressData,
4910 country_code: "us",
4911 province: "NY",
4912 },
4913 },
4914 storeHeaders
4915 )
4916
4917 expect(updated.status).toEqual(200)
4918 expect(updated.data.cart).toEqual(
4919 expect.objectContaining({
4920 id: cart.id,
4921 shipping_address: expect.objectContaining({ province: "NY" }),
4922 items: [
4923 expect.objectContaining({
4924 tax_lines: [
4925 expect.objectContaining({
4926 description: "NY Default Rate",
4927 code: "NYDEFAULT",
4928 rate: 6,
4929 }),
4930 ],
4931 }),
4932 ],
4933 })
4934 )
4935 })
4936
4937 it("should refresh tax lines when a country-only shipping address is set on a multi-country region cart", async () => {
4938 const euRegion = (
4939 await api.post(
4940 "/admin/regions",
4941 {
4942 payment_providers: ["pp_system_default"],
4943 name: "Europe",
4944 currency_code: "eur",
4945 countries: ["de", "ca"],
4946 },
4947 adminHeaders
4948 )
4949 ).data.region
4950
4951 // A multi-country region doesn't auto-assign a country, so the item
4952 // is added before the cart has a shipping country.
4953 const euCart = (
4954 await api.post(
4955 `/store/carts`,
4956 {
4957 currency_code: "eur",
4958 sales_channel_id: salesChannel.id,
4959 region_id: euRegion.id,
4960 items: [{ variant_id: product.variants[0].id, quantity: 2 }],
4961 },
4962 storeHeaders
4963 )
4964 ).data.cart
4965
4966 // Without a country on the cart, items carry no tax lines.
4967 expect(euCart.items).toEqual([
4968 expect.objectContaining({ tax_lines: [] }),
4969 ])
4970
4971 // Setting a country-only address (no province) must refresh taxes.
4972 const updated = await api.post(
4973 `/store/carts/${euCart.id}`,
4974 { shipping_address: { country_code: "de" } },
4975 storeHeaders
4976 )
4977
4978 expect(updated.status).toEqual(200)
4979 expect(updated.data.cart).toEqual(
4980 expect.objectContaining({
4981 id: euCart.id,
4982 shipping_address: expect.objectContaining({ country_code: "de" }),
4983 items: [
4984 expect.objectContaining({
4985 tax_lines: [
4986 expect.objectContaining({
4987 description: "Germany Default Rate",
4988 code: "DE19",
4989 rate: 19,
4990 }),
4991 ],
4992 }),
4993 ],
4994 })
4995 )
4996 })
4997
4998 it("should refresh tax lines when the shipping country changes within a multi-country region", async () => {
4999 const euRegion = (
5000 await api.post(
Showing first 5,000 of 8,098 lines. View raw