fix: include shipping profile and requirement relations when fetching custom shipping options
This commit is contained in:
@@ -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 () => {
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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",
|
||||
}
|
||||
|
||||
|
||||
@@ -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 = []
|
||||
|
||||
|
||||
Reference in New Issue
Block a user