Adds decoration for admin endpoints

This commit is contained in:
olivermrbl
2020-08-25 13:24:35 +02:00
parent 00a2ff0015
commit ee00bd27e4
4 changed files with 21 additions and 9 deletions
@@ -3,11 +3,12 @@ export default async (req, res) => {
try { try {
const customerService = req.scope.resolve("customerService") const customerService = req.scope.resolve("customerService")
let customer = await customerService.retrieve(id) let customer = await customerService.retrieve(id)
customer = customerService.decorate(customer, [ customer = await customerService.decorate(
"email", customer,
"first_name", ["email", "payment_methods", "has_account", "shipping_addresses"],
"last_name", ["orders"]
]) )
res.json({ customer }) res.json({ customer })
} catch (err) { } catch (err) {
throw err throw err
@@ -3,9 +3,14 @@ export default async (req, res) => {
try { try {
const orderService = req.scope.resolve("orderService") const orderService = req.scope.resolve("orderService")
const customerService = req.scope.resolve("customerService")
let order = await orderService.retrieve(id) let order = await orderService.retrieve(id)
order = await orderService.decorate(order, [], ["region"]) order = await orderService.decorate(order, [], ["region", "customer"])
if (order.customer_id) {
order.customer = await customerService.retrieve(order.customer_id)
}
res.json({ order }) res.json({ order })
} catch (error) { } catch (error) {
@@ -5,7 +5,13 @@ export default async (req, res) => {
const data = await profileService.retrieve(profile_id) const data = await profileService.retrieve(profile_id)
res.status(200).json({ shipping_profile: data }) const profile = await profileService.decorate(
data,
["name"],
["products", "shipping_options"]
)
res.status(200).json({ shipping_profile: profile })
} catch (err) { } catch (err) {
throw err throw err
} }
@@ -333,13 +333,13 @@ class ShippingProfileService extends BaseService {
const requiredFields = ["_id", "metadata"] const requiredFields = ["_id", "metadata"]
let decorated = _.pick(profile, fields.concat(requiredFields)) let decorated = _.pick(profile, fields.concat(requiredFields))
if (expandFields.includes("products")) { if (expandFields.includes("products") && profile.products) {
decorated.products = await Promise.all( decorated.products = await Promise.all(
profile.products.map(pId => this.productService_.retrieve(pId)) profile.products.map(pId => this.productService_.retrieve(pId))
) )
} }
if (expandFields.includes("shipping_options")) { if (expandFields.includes("shipping_options") && profile.shipping_options) {
decorated.shipping_options = await Promise.all( decorated.shipping_options = await Promise.all(
profile.shipping_options.map(oId => profile.shipping_options.map(oId =>
this.shippingOptionService_.retrieve(oId) this.shippingOptionService_.retrieve(oId)