fix: adds field selection to list order endpoint (#133)
This commit is contained in:
@@ -18,8 +18,13 @@ export default async (req, res) => {
|
|||||||
|
|
||||||
let orders = await orderService.list(query, offset, limit)
|
let orders = await orderService.list(query, offset, limit)
|
||||||
|
|
||||||
|
let includeFields = []
|
||||||
|
if ("fields" in req.query) {
|
||||||
|
includeFields = req.query.fields.split(",")
|
||||||
|
}
|
||||||
|
|
||||||
orders = await Promise.all(
|
orders = await Promise.all(
|
||||||
orders.map(order => orderService.decorate(order))
|
orders.map(order => orderService.decorate(order, includeFields))
|
||||||
)
|
)
|
||||||
|
|
||||||
let numOrders = await orderService.count()
|
let numOrders = await orderService.count()
|
||||||
|
|||||||
@@ -29,7 +29,6 @@ class OrderModel extends BaseModel {
|
|||||||
items: { type: [LineItemSchema], required: true },
|
items: { type: [LineItemSchema], required: true },
|
||||||
currency_code: { type: String, required: true },
|
currency_code: { type: String, required: true },
|
||||||
tax_rate: { type: Number, required: true },
|
tax_rate: { type: Number, required: true },
|
||||||
shipments: { type: [ShipmentSchema], default: [] },
|
|
||||||
fulfillments: { type: [FulfillmentSchema], default: [] },
|
fulfillments: { type: [FulfillmentSchema], default: [] },
|
||||||
returns: { type: [ReturnSchema], default: [] },
|
returns: { type: [ReturnSchema], default: [] },
|
||||||
refunds: { type: [RefundSchema], default: [] },
|
refunds: { type: [RefundSchema], default: [] },
|
||||||
|
|||||||
@@ -1242,22 +1242,84 @@ class OrderService extends BaseService {
|
|||||||
* @param {string[]} expandFields - fields to expand.
|
* @param {string[]} expandFields - fields to expand.
|
||||||
* @return {Order} return the decorated order.
|
* @return {Order} return the decorated order.
|
||||||
*/
|
*/
|
||||||
async decorate(order, fields, expandFields = []) {
|
async decorate(order, fields = [], expandFields = []) {
|
||||||
const o = order
|
if (fields.length === 0) {
|
||||||
|
// Default to include all fields
|
||||||
|
fields = [
|
||||||
|
"_id",
|
||||||
|
"display_id",
|
||||||
|
"status",
|
||||||
|
"fulfillment_status",
|
||||||
|
"payment_status",
|
||||||
|
"email",
|
||||||
|
"cart_id",
|
||||||
|
"billing_address",
|
||||||
|
"shipping_address",
|
||||||
|
"items",
|
||||||
|
"currency_code",
|
||||||
|
"tax_rate",
|
||||||
|
"fulfillments",
|
||||||
|
"returns",
|
||||||
|
"refunds",
|
||||||
|
"region_id",
|
||||||
|
"discounts",
|
||||||
|
"customer_id",
|
||||||
|
"payment_method",
|
||||||
|
"shipping_methods",
|
||||||
|
"documents",
|
||||||
|
"created",
|
||||||
|
"metadata",
|
||||||
|
"shipping_total",
|
||||||
|
"discount_total",
|
||||||
|
"tax_total",
|
||||||
|
"subtotal",
|
||||||
|
"total",
|
||||||
|
"refunded_total",
|
||||||
|
"refundable_amount",
|
||||||
|
]
|
||||||
|
}
|
||||||
|
const requiredFields = [
|
||||||
|
"_id",
|
||||||
|
"display_id",
|
||||||
|
"fulfillment_status",
|
||||||
|
"payment_status",
|
||||||
|
"status",
|
||||||
|
"currency_code",
|
||||||
|
"region_id",
|
||||||
|
"metadata",
|
||||||
|
]
|
||||||
|
const o = _.pick(order, fields.concat(requiredFields))
|
||||||
|
|
||||||
|
if (fields.includes("shipping_total")) {
|
||||||
o.shipping_total = await this.totalsService_.getShippingTotal(order)
|
o.shipping_total = await this.totalsService_.getShippingTotal(order)
|
||||||
|
}
|
||||||
|
if (fields.includes("discount_total")) {
|
||||||
o.discount_total = await this.totalsService_.getDiscountTotal(order)
|
o.discount_total = await this.totalsService_.getDiscountTotal(order)
|
||||||
|
}
|
||||||
|
if (fields.includes("tax_total")) {
|
||||||
o.tax_total = await this.totalsService_.getTaxTotal(order)
|
o.tax_total = await this.totalsService_.getTaxTotal(order)
|
||||||
|
}
|
||||||
|
if (fields.includes("subtotal")) {
|
||||||
o.subtotal = await this.totalsService_.getSubtotal(order)
|
o.subtotal = await this.totalsService_.getSubtotal(order)
|
||||||
|
}
|
||||||
|
if (fields.includes("total")) {
|
||||||
o.total = await this.totalsService_.getTotal(order)
|
o.total = await this.totalsService_.getTotal(order)
|
||||||
|
}
|
||||||
|
if (fields.includes("refunded_total")) {
|
||||||
o.refunded_total = await this.totalsService_.getRefundedTotal(order)
|
o.refunded_total = await this.totalsService_.getRefundedTotal(order)
|
||||||
|
}
|
||||||
|
if (fields.includes("refundable_amount")) {
|
||||||
o.refundable_amount = o.total - o.refunded_total
|
o.refundable_amount = o.total - o.refunded_total
|
||||||
|
}
|
||||||
|
|
||||||
o.created = order._id.getTimestamp()
|
o.created = order._id.getTimestamp()
|
||||||
|
|
||||||
if (expandFields.includes("region")) {
|
if (expandFields.includes("region")) {
|
||||||
o.region = await this.regionService_.retrieve(order.region_id)
|
o.region = await this.regionService_.retrieve(order.region_id)
|
||||||
}
|
}
|
||||||
|
|
||||||
o.items = o.items.map(i => {
|
if (fields.includes("items")) {
|
||||||
|
o.items = order.items.map(i => {
|
||||||
return {
|
return {
|
||||||
...i,
|
...i,
|
||||||
refundable: this.totalsService_.getLineItemRefund(o, {
|
refundable: this.totalsService_.getLineItemRefund(o, {
|
||||||
@@ -1266,9 +1328,10 @@ class OrderService extends BaseService {
|
|||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
|
||||||
const final = await this.runDecorators_(o)
|
const data = await this.runDecorators_(o)
|
||||||
return final
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user