From 114f008934fd9f18116a10dbb381bca57129c4f2 Mon Sep 17 00:00:00 2001 From: Methapon Metanipat Date: Tue, 10 Sep 2024 13:01:43 +0700 Subject: [PATCH] refactor: where conditions --- .../03-customer-branch-controller.ts | 14 ++- src/controllers/03-employee-controller.ts | 99 ++++++++----------- 2 files changed, 53 insertions(+), 60 deletions(-) diff --git a/src/controllers/03-customer-branch-controller.ts b/src/controllers/03-customer-branch-controller.ts index 0d6504f..d398ee2 100644 --- a/src/controllers/03-customer-branch-controller.ts +++ b/src/controllers/03-customer-branch-controller.ts @@ -184,12 +184,24 @@ export class CustomerBranchController extends Controller { : { registeredBranch: { OR: [ - { user: { some: { userId: req.user.sub } } }, + { + user: { some: { userId: req.user.sub } }, + }, { branch: globalAllow(req.user) ? { some: { user: { some: { userId: req.user.sub } } } } : undefined, }, + { + headOffice: globalAllow(req.user) + ? { branch: { some: { user: { some: { userId: req.user.sub } } } } } + : undefined, + }, + { + headOffice: globalAllow(req.user) + ? { user: { some: { userId: req.user.sub } } } + : undefined, + }, ], }, }, diff --git a/src/controllers/03-employee-controller.ts b/src/controllers/03-employee-controller.ts index 4bd08bb..307249a 100644 --- a/src/controllers/03-employee-controller.ts +++ b/src/controllers/03-employee-controller.ts @@ -19,6 +19,7 @@ import HttpStatus from "../interfaces/http-status"; import HttpError from "../interfaces/http-error"; import minio, { presignedGetObjectIfExist } from "../services/minio"; import { isSystem } from "../utils/keycloak"; +import { filterStatus } from "../services/prisma"; if (!process.env.MINIO_BUCKET) { throw Error("Require MinIO bucket."); @@ -234,29 +235,52 @@ export class EmployeeController extends Controller { @Get("stats/gender") @Security("keycloak") async getEmployeeStatsGender( + @Request() req: RequestWithUser, @Query() customerBranchId?: string, @Query() status?: Status, @Query() query: string = "", ) { - const filterStatus = (val?: Status) => { - if (!val) return {}; - - return val !== Status.CREATED && val !== Status.ACTIVE - ? { status: val } - : { OR: [{ status: Status.CREATED }, { status: Status.ACTIVE }] }; - }; - return await prisma.employee .groupBy({ _count: true, by: ["gender"], where: { OR: [ - { firstName: { contains: query }, customerBranchId, ...filterStatus(status) }, - { firstNameEN: { contains: query }, customerBranchId, ...filterStatus(status) }, - { lastName: { contains: query }, customerBranchId, ...filterStatus(status) }, - { lastNameEN: { contains: query }, customerBranchId, ...filterStatus(status) }, + { firstName: { contains: query } }, + { firstNameEN: { contains: query } }, + { lastName: { contains: query } }, + { lastNameEN: { contains: query } }, ], + AND: { + ...filterStatus(status), + customerBranchId, + customerBranch: { + customer: isSystem(req.user) + ? undefined + : { + registeredBranch: { + OR: [ + { + user: { some: { userId: req.user.sub } }, + }, + { + branch: { some: { user: { some: { userId: req.user.sub } } } }, + }, + { + headOffice: globalAllow(req.user) + ? { branch: { some: { user: { some: { userId: req.user.sub } } } } } + : undefined, + }, + { + headOffice: globalAllow(req.user) + ? { user: { some: { userId: req.user.sub } } } + : undefined, + }, + ], + }, + }, + }, + }, }, }) .then((res) => @@ -279,14 +303,6 @@ export class EmployeeController extends Controller { @Query() page: number = 1, @Query() pageSize: number = 30, ) { - const filterStatus = (val?: Status) => { - if (!val) return {}; - - return val !== Status.CREATED && val !== Status.ACTIVE - ? { status: val } - : { OR: [{ status: Status.CREATED }, { status: Status.ACTIVE }] }; - }; - const where = { OR: [ { firstName: { contains: query } }, @@ -350,16 +366,7 @@ export class EmployeeController extends Controller { ]); return { - result: await Promise.all( - result.map(async (v) => ({ - ...v, - profileImageUrl: await presignedGetObjectIfExist( - MINIO_BUCKET, - imageLocation(v.id), - 12 * 60 * 60, - ), - })), - ), + result, page, pageSize, total, @@ -580,18 +587,7 @@ export class EmployeeController extends Controller { this.setStatus(HttpStatus.CREATED); - return Object.assign(record, { - profileImageUrl: await presignedGetObjectIfExist( - MINIO_BUCKET, - imageLocation(record.id), - 12 * 60 * 60, - ), - profileImageUploadUrl: await minio.presignedPutObject( - MINIO_BUCKET, - imageLocation(record.id), - 12 * 60 * 60, - ), - }); + return record; } @Put("{employeeId}") @@ -896,18 +892,7 @@ export class EmployeeController extends Controller { })), }); - return Object.assign(record, { - profileImageUrl: await presignedGetObjectIfExist( - MINIO_BUCKET, - imageLocation(record.id), - 12 * 60 * 60, - ), - profileImageUploadUrl: await minio.presignedPutObject( - MINIO_BUCKET, - imageLocation(record.id), - 12 * 60 * 60, - ), - }); + return record; } @Delete("{employeeId}") @@ -998,11 +983,7 @@ export class EmployeeAttachmentController extends Controller { } @Get("{filename}") - async getAttachment( - @Request() req: RequestWithUser, - @Path() employeeId: string, - @Path() filename: string, - ) { + async getAttachment(@Path() employeeId: string, @Path() filename: string) { const record = await prisma.employee.findFirst({ where: { id: employeeId }, });