refactor: also return employee count of customer

This commit is contained in:
Methapon Metanipat 2024-08-30 09:43:34 +07:00
parent 23717bacaf
commit e367fca56e

View file

@ -147,7 +147,8 @@ 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([
prisma.customer.findFirst({
include: { include: {
branch: { branch: {
include: { include: {
@ -161,12 +162,14 @@ export class CustomerController extends Controller {
updatedBy: true, updatedBy: true,
}, },
where: { id: customerId }, where: { id: customerId },
}); }),
prisma.employee.count({ where: { customerBranch: { 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()