refactor: also return employee count of customer
This commit is contained in:
parent
23717bacaf
commit
e367fca56e
1 changed files with 17 additions and 14 deletions
|
|
@ -147,26 +147,29 @@ export class CustomerController extends Controller {
|
||||||
@Get("{customerId}")
|
@Get("{customerId}")
|
||||||
@Security("keycloak")
|
@Security("keycloak")
|
||||||
async getById(@Path() customerId: string) {
|
async getById(@Path() customerId: string) {
|
||||||
const record = await prisma.customer.findFirst({
|
const [record, countEmployee] = await prisma.$transaction([
|
||||||
include: {
|
prisma.customer.findFirst({
|
||||||
branch: {
|
include: {
|
||||||
include: {
|
branch: {
|
||||||
province: true,
|
include: {
|
||||||
district: true,
|
province: true,
|
||||||
subDistrict: true,
|
district: true,
|
||||||
|
subDistrict: true,
|
||||||
|
},
|
||||||
|
orderBy: { createdAt: "asc" },
|
||||||
},
|
},
|
||||||
orderBy: { createdAt: "asc" },
|
createdBy: true,
|
||||||
|
updatedBy: true,
|
||||||
},
|
},
|
||||||
createdBy: true,
|
where: { id: customerId },
|
||||||
updatedBy: true,
|
}),
|
||||||
},
|
prisma.employee.count({ where: { customerBranch: { customerId } } }),
|
||||||
where: { id: customerId },
|
]);
|
||||||
});
|
|
||||||
|
|
||||||
if (!record) {
|
if (!record) {
|
||||||
throw new HttpError(HttpStatus.NOT_FOUND, "Customer cannot be found.", "customerNotFound");
|
throw new HttpError(HttpStatus.NOT_FOUND, "Customer cannot be found.", "customerNotFound");
|
||||||
}
|
}
|
||||||
return record;
|
return Object.assign(record, { _count: { employee: countEmployee } });
|
||||||
}
|
}
|
||||||
|
|
||||||
@Post()
|
@Post()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue