feat: add extend param for customer groups
This commit is contained in:
@@ -120,6 +120,31 @@ describe("/admin/customer-groups", () => {
|
|||||||
name: "vip-customers",
|
name: "vip-customers",
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
expect(response.data.customerGroup).not.toHaveProperty("customers:")
|
||||||
|
})
|
||||||
|
|
||||||
|
it("gets customer group with `customers` prop", async () => {
|
||||||
|
const api = useApi()
|
||||||
|
|
||||||
|
const id = "customer-group-1"
|
||||||
|
|
||||||
|
const response = await api.get(
|
||||||
|
`/admin/customer-groups/${id}?expand=customers`,
|
||||||
|
{
|
||||||
|
headers: {
|
||||||
|
Authorization: "Bearer test_token",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
expect(response.status).toEqual(200)
|
||||||
|
expect(response.data.customerGroup).toEqual(
|
||||||
|
expect.objectContaining({
|
||||||
|
id: "customer-group-1",
|
||||||
|
name: "vip-customers",
|
||||||
|
})
|
||||||
|
)
|
||||||
|
expect(response.data.customerGroup.customers).toEqual([])
|
||||||
})
|
})
|
||||||
|
|
||||||
it("throws error when a customer group doesn't exist", async () => {
|
it("throws error when a customer group doesn't exist", async () => {
|
||||||
|
|||||||
@@ -1,4 +1,7 @@
|
|||||||
import { CustomerGroupService } from "../../../../services"
|
import { CustomerGroupService } from "../../../../services"
|
||||||
|
import { FindParams } from "../../../../types/common"
|
||||||
|
import { validator } from "../../../../utils/validator"
|
||||||
|
import { defaultAdminCustomerGroupsRelations } from "."
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @oas [get] /customer-group/{id}
|
* @oas [get] /customer-group/{id}
|
||||||
@@ -22,11 +25,25 @@ import { CustomerGroupService } from "../../../../services"
|
|||||||
*/
|
*/
|
||||||
export default async (req, res) => {
|
export default async (req, res) => {
|
||||||
const { id } = req.params
|
const { id } = req.params
|
||||||
|
|
||||||
|
const validated = await validator(FindParams, req.query)
|
||||||
|
|
||||||
const customerGroupService: CustomerGroupService = req.scope.resolve(
|
const customerGroupService: CustomerGroupService = req.scope.resolve(
|
||||||
"customerGroupService"
|
"customerGroupService"
|
||||||
)
|
)
|
||||||
|
|
||||||
const customerGroup = await customerGroupService.retrieve(id)
|
let expandFields: string[] = []
|
||||||
|
if (validated.expand) {
|
||||||
|
expandFields = validated.expand.split(",")
|
||||||
|
}
|
||||||
|
|
||||||
|
const findConfig = {
|
||||||
|
relations: expandFields.length
|
||||||
|
? expandFields
|
||||||
|
: defaultAdminCustomerGroupsRelations,
|
||||||
|
}
|
||||||
|
|
||||||
|
const customerGroup = await customerGroupService.retrieve(id, findConfig)
|
||||||
|
|
||||||
res.json({ customerGroup })
|
res.json({ customerGroup })
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,4 +23,6 @@ export type AdminCustomerGroupsListRes = PaginatedResponse & {
|
|||||||
customer_groups: CustomerGroup[]
|
customer_groups: CustomerGroup[]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const defaultAdminCustomerGroupsRelations = []
|
||||||
|
|
||||||
export * from "./create-customer-group"
|
export * from "./create-customer-group"
|
||||||
|
|||||||
Reference in New Issue
Block a user