Adds fulfillment option endpoint

This commit is contained in:
Sebastian Rindom
2020-07-06 15:49:49 +02:00
parent 9c91ae77d2
commit ca6dd7e257
4 changed files with 46 additions and 2 deletions

View File

@@ -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
}
}

View File

@@ -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",

View File

@@ -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 })

View File

@@ -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
*/