Add expand to /admin/customers/:id (#1070)

* fix: 1055 add expand to /admin/customers/:id

* fix tests

* fix tests
This commit is contained in:
Oliver Windall Juhl
2022-02-16 23:41:30 +01:00
committed by GitHub
parent 66294038f0
commit 9f4a9f7db4
6 changed files with 189 additions and 14 deletions
@@ -1,4 +1,7 @@
import { defaultAdminCustomersRelations } from "."
import CustomerService from "../../../../services/customer"
import { FindParams } from "../../../../types/common"
import { validator } from "../../../../utils/validator"
/**
* @oas [get] /customers/{id}
@@ -22,10 +25,23 @@ import CustomerService from "../../../../services/customer"
*/
export default async (req, res) => {
const { id } = req.params
const validated = await validator(FindParams, req.query)
const customerService: CustomerService = req.scope.resolve("customerService")
const customer = await customerService.retrieve(id, {
relations: ["orders", "shipping_addresses"],
})
let expandFields: string[] = []
if (validated.expand) {
expandFields = validated.expand.split(",")
}
const findConfig = {
relations: expandFields.length
? expandFields
: defaultAdminCustomersRelations,
}
const customer = await customerService.retrieve(id, findConfig)
res.json({ customer })
}
@@ -26,6 +26,8 @@ export type AdminCustomersListRes = PaginatedResponse & {
customers: Customer[]
}
export const defaultAdminCustomersRelations = ["orders", "shipping_addresses"]
export * from "./create-customer"
export * from "./get-customer"
export * from "./list-customers"