diff --git a/integration-tests/api/__tests__/store/shipping-options.js b/integration-tests/api/__tests__/store/shipping-options.js index 06814a5cf6..3235e878e4 100644 --- a/integration-tests/api/__tests__/store/shipping-options.js +++ b/integration-tests/api/__tests__/store/shipping-options.js @@ -16,7 +16,7 @@ describe("/store/shipping-options", () => { beforeAll(async () => { const cwd = path.resolve(path.join(__dirname, "..", "..")) dbConnection = await initDb({ cwd }) - medusaProcess = await setupServer({ cwd }) + medusaProcess = await setupServer({ cwd, verbose: true }) }) afterAll(async () => { diff --git a/integration-tests/api/helpers/swap-seeder.js b/integration-tests/api/helpers/swap-seeder.js index f3f098a637..c35fcc08ef 100644 --- a/integration-tests/api/helpers/swap-seeder.js +++ b/integration-tests/api/helpers/swap-seeder.js @@ -120,6 +120,18 @@ module.exports = async (connection, data = {}) => { await manager.save(cartWithCustomSo) + const liRma = manager.create(LineItem, { + id: "test-item-rma", + title: "Line Item RMA", + description: "Line Item Desc", + thumbnail: "https://test.js/1234", + unit_price: 8000, + quantity: 1, + variant_id: "test-variant", + cart_id: "test-cart-rma", + }) + await manager.save(liRma) + manager.insert(CustomShippingOption, { id: "cso-test", cart_id: cartWithCustomSo.id, diff --git a/packages/medusa/src/services/__tests__/shipping-profile.js b/packages/medusa/src/services/__tests__/shipping-profile.js index b46737ebe6..e9078f26d7 100644 --- a/packages/medusa/src/services/__tests__/shipping-profile.js +++ b/packages/medusa/src/services/__tests__/shipping-profile.js @@ -176,8 +176,17 @@ describe("ShippingProfileService", () => { }) const shippingOptionService = { - list: jest.fn().mockImplementation(() => - Promise.resolve([ + list: jest.fn().mockImplementation(({ id }) => { + if (id && id.includes("test-option")) { + return Promise.resolve([ + { + id: "test-option", + amount: 1000, + name: "Test option", + }, + ]) + } + return Promise.resolve([ { id: "ship_1", }, @@ -185,7 +194,7 @@ describe("ShippingProfileService", () => { id: "ship_2", }, ]) - ), + }), validateCartOption: jest.fn().mockImplementation(s => s), withTransaction: function() { return this @@ -199,11 +208,7 @@ describe("ShippingProfileService", () => { { id: "cso_1", cart_id: "cso-cart", - shipping_option: { - id: "test-option", - amount: 200, - name: "Test option", - }, + shipping_option_id: "test-option", price: 0, }, ]) @@ -226,6 +231,24 @@ describe("ShippingProfileService", () => { it("given a cart with custom shipping options, should return correct custom shipping options ", async () => { const cart = { id: "cso-cart", + items: [ + { + variant: { + product: { + _id: IdMap.getId("product_1"), + profile_id: IdMap.getId("profile"), + }, + }, + }, + { + variant: { + product: { + _id: IdMap.getId("product_2"), + profile_id: IdMap.getId("profile"), + }, + }, + }, + ], type: "swap", } diff --git a/packages/medusa/src/services/shipping-profile.js b/packages/medusa/src/services/shipping-profile.js index 3f1f121ce9..7271f171c1 100644 --- a/packages/medusa/src/services/shipping-profile.js +++ b/packages/medusa/src/services/shipping-profile.js @@ -418,29 +418,43 @@ class ShippingProfileService extends BaseService { * @return {[ShippingOption]} a list of the available shipping options */ async fetchCartOptions(cart) { + const profileIds = this.getProfilesInCart_(cart) + + const selector = { + profile_id: profileIds, + admin_only: false, + } + const customShippingOptions = await this.customShippingOptionService_.list( { cart_id: cart.id, }, - { relations: ["shipping_option"] } + { select: ["id", "shipping_option_id", "price"] } ) - if (customShippingOptions?.length) { - return customShippingOptions.map(cso => ({ - ...cso.shipping_option, - amount: cso.price, - })) + const hasCustomShippingOptions = customShippingOptions?.length + // if there are custom shipping options associated with the cart, use those + if (hasCustomShippingOptions) { + selector.id = customShippingOptions.map(cso => cso.shipping_option_id) } - const profileIds = this.getProfilesInCart_(cart) + const rawOpts = await this.shippingOptionService_.list(selector, { + relations: ["requirements", "profile"], + }) - const rawOpts = await this.shippingOptionService_.list( - { - profile_id: profileIds, - admin_only: false, - }, - { relations: ["requirements", "profile"] } - ) + // if there are custom shipping options associated with the cart, return cart shipping options with custom price + if (hasCustomShippingOptions) { + return rawOpts.map(so => { + const customOption = customShippingOptions.find( + cso => cso.shipping_option_id === so.id + ) + + return { + ...so, + amount: customOption?.price, + } + }) + } const options = []