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 () => {
|
beforeAll(async () => {
|
||||||
const cwd = path.resolve(path.join(__dirname, "..", ".."))
|
const cwd = path.resolve(path.join(__dirname, "..", ".."))
|
||||||
dbConnection = await initDb({ cwd })
|
dbConnection = await initDb({ cwd })
|
||||||
medusaProcess = await setupServer({ cwd })
|
medusaProcess = await setupServer({ cwd, verbose: true })
|
||||||
})
|
})
|
||||||
|
|
||||||
afterAll(async () => {
|
afterAll(async () => {
|
||||||
|
|||||||
@@ -120,6 +120,18 @@ module.exports = async (connection, data = {}) => {
|
|||||||
|
|
||||||
await manager.save(cartWithCustomSo)
|
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, {
|
manager.insert(CustomShippingOption, {
|
||||||
id: "cso-test",
|
id: "cso-test",
|
||||||
cart_id: cartWithCustomSo.id,
|
cart_id: cartWithCustomSo.id,
|
||||||
|
|||||||
@@ -176,8 +176,17 @@ describe("ShippingProfileService", () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
const shippingOptionService = {
|
const shippingOptionService = {
|
||||||
list: jest.fn().mockImplementation(() =>
|
list: jest.fn().mockImplementation(({ id }) => {
|
||||||
Promise.resolve([
|
if (id && id.includes("test-option")) {
|
||||||
|
return Promise.resolve([
|
||||||
|
{
|
||||||
|
id: "test-option",
|
||||||
|
amount: 1000,
|
||||||
|
name: "Test option",
|
||||||
|
},
|
||||||
|
])
|
||||||
|
}
|
||||||
|
return Promise.resolve([
|
||||||
{
|
{
|
||||||
id: "ship_1",
|
id: "ship_1",
|
||||||
},
|
},
|
||||||
@@ -185,7 +194,7 @@ describe("ShippingProfileService", () => {
|
|||||||
id: "ship_2",
|
id: "ship_2",
|
||||||
},
|
},
|
||||||
])
|
])
|
||||||
),
|
}),
|
||||||
validateCartOption: jest.fn().mockImplementation(s => s),
|
validateCartOption: jest.fn().mockImplementation(s => s),
|
||||||
withTransaction: function() {
|
withTransaction: function() {
|
||||||
return this
|
return this
|
||||||
@@ -199,11 +208,7 @@ describe("ShippingProfileService", () => {
|
|||||||
{
|
{
|
||||||
id: "cso_1",
|
id: "cso_1",
|
||||||
cart_id: "cso-cart",
|
cart_id: "cso-cart",
|
||||||
shipping_option: {
|
shipping_option_id: "test-option",
|
||||||
id: "test-option",
|
|
||||||
amount: 200,
|
|
||||||
name: "Test option",
|
|
||||||
},
|
|
||||||
price: 0,
|
price: 0,
|
||||||
},
|
},
|
||||||
])
|
])
|
||||||
@@ -226,6 +231,24 @@ describe("ShippingProfileService", () => {
|
|||||||
it("given a cart with custom shipping options, should return correct custom shipping options ", async () => {
|
it("given a cart with custom shipping options, should return correct custom shipping options ", async () => {
|
||||||
const cart = {
|
const cart = {
|
||||||
id: "cso-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",
|
type: "swap",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -418,29 +418,43 @@ class ShippingProfileService extends BaseService {
|
|||||||
* @return {[ShippingOption]} a list of the available shipping options
|
* @return {[ShippingOption]} a list of the available shipping options
|
||||||
*/
|
*/
|
||||||
async fetchCartOptions(cart) {
|
async fetchCartOptions(cart) {
|
||||||
|
const profileIds = this.getProfilesInCart_(cart)
|
||||||
|
|
||||||
|
const selector = {
|
||||||
|
profile_id: profileIds,
|
||||||
|
admin_only: false,
|
||||||
|
}
|
||||||
|
|
||||||
const customShippingOptions = await this.customShippingOptionService_.list(
|
const customShippingOptions = await this.customShippingOptionService_.list(
|
||||||
{
|
{
|
||||||
cart_id: cart.id,
|
cart_id: cart.id,
|
||||||
},
|
},
|
||||||
{ relations: ["shipping_option"] }
|
{ select: ["id", "shipping_option_id", "price"] }
|
||||||
)
|
)
|
||||||
|
|
||||||
if (customShippingOptions?.length) {
|
const hasCustomShippingOptions = customShippingOptions?.length
|
||||||
return customShippingOptions.map(cso => ({
|
// if there are custom shipping options associated with the cart, use those
|
||||||
...cso.shipping_option,
|
if (hasCustomShippingOptions) {
|
||||||
amount: cso.price,
|
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(
|
// if there are custom shipping options associated with the cart, return cart shipping options with custom price
|
||||||
{
|
if (hasCustomShippingOptions) {
|
||||||
profile_id: profileIds,
|
return rawOpts.map(so => {
|
||||||
admin_only: false,
|
const customOption = customShippingOptions.find(
|
||||||
},
|
cso => cso.shipping_option_id === so.id
|
||||||
{ relations: ["requirements", "profile"] }
|
)
|
||||||
)
|
|
||||||
|
return {
|
||||||
|
...so,
|
||||||
|
amount: customOption?.price,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
const options = []
|
const options = []
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user