Add expand to /admin/customers/:id (#1070)
* fix: 1055 add expand to /admin/customers/:id * fix tests * fix tests
This commit is contained in:
committed by
olivermrbl
parent
1e4cc2fc80
commit
7c31acb064
@@ -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"
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
import { Transform } from "class-transformer"
|
||||
import { transformDate } from "../utils/validators/date-transform"
|
||||
import { Type } from "class-transformer"
|
||||
import { Transform, Type } from "class-transformer"
|
||||
import { IsDate, IsNumber, IsOptional, IsString } from "class-validator"
|
||||
import "reflect-metadata"
|
||||
import { transformDate } from "../utils/validators/date-transform"
|
||||
|
||||
export type PartialPick<T, K extends keyof T> = {
|
||||
[P in K]?: T[P]
|
||||
@@ -169,3 +168,13 @@ export class AddressCreatePayload {
|
||||
@IsString()
|
||||
postal_code: string
|
||||
}
|
||||
|
||||
export class FindParams {
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
expand?: string
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
fields?: string
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user