hotfix: list shipping options by product id
This commit is contained in:
1 parent
17a34ae30d
commit
d784e5b0fb
3 files changed
+55
-3
No files matched your search
@@ -6,6 +6,7 @@ const route = Router()
|
|||||||
export default app => {
|
export default app => {
|
||||||
app.use("/shipping-options", route)
|
app.use("/shipping-options", route)
|
||||||
|
|
||||||
|
route.get("/", middlewares.wrap(require("./list-options").default))
|
||||||
route.get(
|
route.get(
|
||||||
"/:cart_id",
|
"/:cart_id",
|
||||||
middlewares.wrap(require("./list-shipping-options").default)
|
middlewares.wrap(require("./list-shipping-options").default)
|
||||||
|
|||||||
@@ -0,0 +1,22 @@
|
|||||||
|
import { Validator, MedusaError } from "medusa-core-utils"
|
||||||
|
|
||||||
|
export default async (req, res) => {
|
||||||
|
const productIds =
|
||||||
|
(req.query.product_ids && req.query.product_ids.split(",")) || []
|
||||||
|
const regionId = req.query.region_id
|
||||||
|
|
||||||
|
try {
|
||||||
|
const shippingProfileService = req.scope.resolve("shippingProfileService")
|
||||||
|
|
||||||
|
const options = await shippingProfileService.fetchOptionsByProductIds(
|
||||||
|
productIds,
|
||||||
|
{
|
||||||
|
region_id: regionId,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
res.status(200).json({ shipping_options: options })
|
||||||
|
} catch (err) {
|
||||||
|
throw err
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,3 @@
|
|||||||
import mongoose from "mongoose"
|
|
||||||
import _ from "lodash"
|
import _ from "lodash"
|
||||||
import { Validator, MedusaError } from "medusa-core-utils"
|
import { Validator, MedusaError } from "medusa-core-utils"
|
||||||
import { BaseService } from "medusa-interfaces"
|
import { BaseService } from "medusa-interfaces"
|
||||||
@@ -52,6 +51,36 @@ class ShippingProfileService extends BaseService {
|
|||||||
return this.profileModel_.find(selector)
|
return this.profileModel_.find(selector)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fetchOptionsByProductIds(productIds, filter) {
|
||||||
|
const profiles = await this.list({ products: { $in: productIds } })
|
||||||
|
const optionIds = profiles.reduce(
|
||||||
|
(acc, next) => acc.concat(next.shipping_options),
|
||||||
|
[]
|
||||||
|
)
|
||||||
|
|
||||||
|
const options = await Promise.all(
|
||||||
|
optionIds.map(async oId => {
|
||||||
|
const option = await this.shippingOptionService_
|
||||||
|
.retrieve(oId)
|
||||||
|
.catch(_ => undefined)
|
||||||
|
|
||||||
|
if (!option) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
let canSend = true
|
||||||
|
if (filter.region_id) {
|
||||||
|
if (filter.region_id !== option.region_id) {
|
||||||
|
canSend = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return canSend ? option : null
|
||||||
|
})
|
||||||
|
)
|
||||||
|
|
||||||
|
return options.filter(o => !!o)
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets a profile by id.
|
* Gets a profile by id.
|
||||||
* Throws in case of DB Error and if profile was not found.
|
* Throws in case of DB Error and if profile was not found.
|
||||||
@@ -297,7 +326,7 @@ class ShippingProfileService extends BaseService {
|
|||||||
const profile = await this.retrieve(profileId)
|
const profile = await this.retrieve(profileId)
|
||||||
|
|
||||||
return this.profileModel_.updateOne(
|
return this.profileModel_.updateOne(
|
||||||
{ _id: profileId },
|
{ _id: profile._id },
|
||||||
{ $pull: { shipping_options: optionId } }
|
{ $pull: { shipping_options: optionId } }
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -391,7 +420,7 @@ class ShippingProfileService extends BaseService {
|
|||||||
optionIds.map(async oId => {
|
optionIds.map(async oId => {
|
||||||
const option = await this.shippingOptionService_
|
const option = await this.shippingOptionService_
|
||||||
.validateCartOption(oId, cart)
|
.validateCartOption(oId, cart)
|
||||||
.catch(err => {
|
.catch(_ => {
|
||||||
// If validation failed we skip the option
|
// If validation failed we skip the option
|
||||||
return null
|
return null
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in new issue
Block a user