Adds fulfillment option endpoint
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
import { MedusaError, Validator } from "medusa-core-utils"
|
||||
|
||||
export default async (req, res) => {
|
||||
const { region_id } = req.params
|
||||
try {
|
||||
const fulfillmentProviderService = req.scope.resolve(
|
||||
"fulfillmentProviderService"
|
||||
)
|
||||
const regionService = req.scope.resolve("regionService")
|
||||
const region = await regionService.retrieve(region_id)
|
||||
|
||||
const options = await fulfillmentProviderService.listFulfillmentOptions(
|
||||
region.fulfillment_providers || []
|
||||
)
|
||||
|
||||
res.status(200).json({
|
||||
fulfillment_options: options,
|
||||
})
|
||||
} catch (err) {
|
||||
throw err
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,11 @@ export default app => {
|
||||
route.get("/", middlewares.wrap(require("./list-regions").default))
|
||||
route.get("/:region_id", middlewares.wrap(require("./get-region").default))
|
||||
|
||||
route.get(
|
||||
"/:region_id/fulfillment-options",
|
||||
middlewares.wrap(require("./get-fulfillment-options").default)
|
||||
)
|
||||
|
||||
route.post("/", middlewares.wrap(require("./create-region").default))
|
||||
route.post(
|
||||
"/:region_id",
|
||||
|
||||
@@ -32,10 +32,13 @@ export default async (req, res) => {
|
||||
const optionService = req.scope.resolve("shippingOptionService")
|
||||
const shippingProfileService = req.scope.resolve("shippingProfileService")
|
||||
|
||||
const data = await optionService.create(value)
|
||||
|
||||
// Add to default shipping profile
|
||||
const { _id } = await shippingProfileService.retrieveDefault()
|
||||
|
||||
const data = await optionService.create({
|
||||
...value,
|
||||
profile_id: _id,
|
||||
})
|
||||
await shippingProfileService.addShippingOption(_id, data._id)
|
||||
|
||||
res.status(200).json({ shipping_option: data })
|
||||
|
||||
@@ -9,6 +9,20 @@ class FulfillmentProviderService {
|
||||
this.container_ = container
|
||||
}
|
||||
|
||||
async listFulfillmentOptions(providers) {
|
||||
const result = await Promise.all(
|
||||
providers.map(async p => {
|
||||
const provider = await this.retrieveProvider(p)
|
||||
return {
|
||||
provider_id: p,
|
||||
options: await provider.getFulfillmentOptions(),
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns {FulfillmentService} the payment fulfillment provider
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user