diff --git a/integration-tests/api/__tests__/store/cart.js b/integration-tests/api/__tests__/store/cart.js index 9b5ceeb22b..a4e493f116 100644 --- a/integration-tests/api/__tests__/store/cart.js +++ b/integration-tests/api/__tests__/store/cart.js @@ -206,6 +206,65 @@ describe("/store/carts", () => { }); }); + describe("POST /store/carts/:id/shipping-methods", () => { + beforeEach(async () => { + await cartSeeder(dbConnection); + }); + + afterEach(async () => { + const manager = dbConnection.manager; + await doAfterEach(manager); + }); + + it("adds a shipping method to cart", async () => { + const api = useApi(); + + const cartWithShippingMethod = await api.post( + "/store/carts/test-cart/shipping-methods", + { + option_id: "test-option", + }, + { withCredentials: true } + ); + + expect(cartWithShippingMethod.data.cart.shipping_methods).toContainEqual( + expect.objectContaining({ shipping_option_id: "test-option" }) + ); + expect(cartWithShippingMethod.status).toEqual(200); + }); + + it("adds no more than 1 shipping method per shipping profile", async () => { + const api = useApi(); + const addShippingMethod = async (option_id) => { + return await api.post( + "/store/carts/test-cart/shipping-methods", + { + option_id, + }, + { withCredentials: true } + ); + }; + + await addShippingMethod("test-option"); + const cartWithAnotherShippingMethod = await addShippingMethod( + "test-option-2" + ); + + expect( + cartWithAnotherShippingMethod.data.cart.shipping_methods.length + ).toEqual(1); + expect( + cartWithAnotherShippingMethod.data.cart.shipping_methods + ).toContainEqual( + expect.objectContaining({ + shipping_option_id: "test-option-2", + price: 500, + }) + ); + expect(cartWithAnotherShippingMethod.status).toEqual(200); + }); + }); + describe("DELETE /store/carts/:id/discounts/:code", () => { beforeEach(async () => { try { diff --git a/integration-tests/api/helpers/cart-seeder.js b/integration-tests/api/helpers/cart-seeder.js index dfbf4d4ffe..8156a35d1d 100644 --- a/integration-tests/api/helpers/cart-seeder.js +++ b/integration-tests/api/helpers/cart-seeder.js @@ -100,6 +100,17 @@ module.exports = async (connection, data = {}) => { data: {}, }); + await manager.insert(ShippingOption, { + id: "test-option-2", + name: "test-option-2", + provider_id: "test-ful", + region_id: "test-region", + profile_id: defaultProfile.id, + price_type: "flat_rate", + amount: 500, + data: {}, + }); + const cart = manager.create(Cart, { id: "test-cart", customer_id: "some-customer", diff --git a/packages/medusa/src/services/cart.js b/packages/medusa/src/services/cart.js index 6c2bd03792..a52bae8065 100644 --- a/packages/medusa/src/services/cart.js +++ b/packages/medusa/src/services/cart.js @@ -1365,7 +1365,7 @@ class CartService extends BaseService { .withTransaction(manager) .emit(CartService.Events.UPDATED, result) return result - }) + }, "SERIALIZABLE") } /**