From f36594266c928f84126d6f38b69161a0588a92b6 Mon Sep 17 00:00:00 2001 From: waruneeauy Date: Fri, 14 Mar 2025 18:14:43 +0700 Subject: [PATCH] fixing --- src/controllers/EvaluationController.ts | 172 +++++++++++++----------- src/database/data-source.ts | 2 +- 2 files changed, 91 insertions(+), 83 deletions(-) diff --git a/src/controllers/EvaluationController.ts b/src/controllers/EvaluationController.ts index 393fc0d..16ca3a3 100644 --- a/src/controllers/EvaluationController.ts +++ b/src/controllers/EvaluationController.ts @@ -88,89 +88,97 @@ export class EvaluationController { status?: string[]; }, ) { - // await new permission().PermissionList(request, "SYS_EVA_REQ"); - let _data = await new permission().PermissionOrgList(request, "SYS_EVA_REQ"); - const [evaluation, total] = await AppDataSource.getRepository(Evaluation) - .createQueryBuilder("evaluation") - .andWhere( - _data.root != undefined && _data.root != null - ? _data.root[0] != null - ? `evaluation.rootId IN (:...root)` - : `evaluation.rootId is null` - : "1=1", - { - root: _data.root, - }, - ) - .andWhere( - _data.child1 != undefined && _data.child1 != null - ? _data.child1[0] != null - ? `evaluation.child1Id IN (:...child1)` - : `evaluation.child1Id is null` - : "1=1", - { - child1: _data.child1, - }, - ) - .andWhere( - _data.child2 != undefined && _data.child2 != null - ? _data.child2[0] != null - ? `evaluation.child2Id IN (:...child2)` - : `evaluation.child2Id is null` - : "1=1", - { - child2: _data.child2, - }, - ) - .andWhere( - _data.child3 != undefined && _data.child3 != null - ? _data.child3[0] != null - ? `evaluation.child3Id IN (:...child3)` - : `evaluation.child3Id is null` - : "1=1", - { - child3: _data.child3, - }, - ) - .andWhere( - _data.child4 != undefined && _data.child4 != null - ? _data.child4[0] != null - ? `evaluation.child4Id IN (:...child4)` - : `evaluation.child4Id is null` - : "1=1", - { - child4: _data.child4, - }, - ) - .andWhere( - new Brackets((qb) => { - qb.andWhere( - body.status == undefined || body.status == null || body.status.every((s) => s === "ALL") - ? "1=1" - : `evaluation.step In (:status)`, - { - status: body.status, - }, - ).andWhere( - new Brackets((qb) => { - qb.orWhere("evaluation.fullName LIKE :keyword", { keyword: `%${body.keyword}%` }) - .orWhere("evaluation.citizenId LIKE :keyword", { keyword: `%${body.keyword}%` }) - .orWhere("evaluation.position LIKE :keyword", { keyword: `%${body.keyword}%` }) - .orWhere("evaluation.posNo LIKE :keyword", { keyword: `%${body.keyword}%` }) - .orWhere("evaluation.oc LIKE :keyword", { keyword: `%${body.keyword}%` }) - .orWhere("evaluation.type IN (:...type)", { - type: body.keyword == null ? [""] : ConvertThaiToType(body.keyword), - }); - }), - ); - }), - ) - .orderBy("evaluation.lastUpdatedAt", "DESC") - .skip((body.page - 1) * body.pageSize) - .take(body.pageSize) - .getManyAndCount(); + try { + // await new permission().PermissionList(request, "SYS_EVA_REQ"); + let _data = await new permission().PermissionOrgList(request, "SYS_EVA_REQ"); + const [evaluation, total] = await AppDataSource.getRepository(Evaluation) + .createQueryBuilder("evaluation") + .andWhere( + _data.root != undefined && _data.root != null + ? _data.root[0] != null + ? `evaluation.rootId IN (:...root)` + : `evaluation.rootId is null` + : "1=1", + { + root: _data.root, + }, + ) + .andWhere( + _data.child1 != undefined && _data.child1 != null + ? _data.child1[0] != null + ? `evaluation.child1Id IN (:...child1)` + : `evaluation.child1Id is null` + : "1=1", + { + child1: _data.child1, + }, + ) + .andWhere( + _data.child2 != undefined && _data.child2 != null + ? _data.child2[0] != null + ? `evaluation.child2Id IN (:...child2)` + : `evaluation.child2Id is null` + : "1=1", + { + child2: _data.child2, + }, + ) + .andWhere( + _data.child3 != undefined && _data.child3 != null + ? _data.child3[0] != null + ? `evaluation.child3Id IN (:...child3)` + : `evaluation.child3Id is null` + : "1=1", + { + child3: _data.child3, + }, + ) + .andWhere( + _data.child4 != undefined && _data.child4 != null + ? _data.child4[0] != null + ? `evaluation.child4Id IN (:...child4)` + : `evaluation.child4Id is null` + : "1=1", + { + child4: _data.child4, + }, + ) + .andWhere( + new Brackets((qb) => { + qb.andWhere( + body.status == undefined || + body.status == null || + body.status.every((s) => s === "ALL") + ? "1=1" + : `evaluation.step In (:status)`, + { + status: body.status, + }, + ).andWhere( + new Brackets((qb) => { + qb.orWhere("evaluation.fullName LIKE :keyword", { keyword: `%${body.keyword}%` }) + .orWhere("evaluation.citizenId LIKE :keyword", { keyword: `%${body.keyword}%` }) + .orWhere("evaluation.position LIKE :keyword", { keyword: `%${body.keyword}%` }) + .orWhere("evaluation.posNo LIKE :keyword", { keyword: `%${body.keyword}%` }) + .orWhere("evaluation.oc LIKE :keyword", { keyword: `%${body.keyword}%` }) + .orWhere("evaluation.type IN (:...type)", { + type: body.keyword == null ? [""] : ConvertThaiToType(body.keyword), + }); + }), + ); + }), + ) + .orderBy("evaluation.lastUpdatedAt", "DESC") + .skip((body.page - 1) * body.pageSize) + .take(body.pageSize) + .getManyAndCount(); - return new HttpSuccess({ data: evaluation, total }); + return new HttpSuccess({ data: evaluation, total }); + } catch (error: any) { + if (error instanceof HttpError) { + throw error; + } else throw new HttpError(HttpStatusCode.INTERNAL_SERVER_ERROR, error); + } } /** diff --git a/src/database/data-source.ts b/src/database/data-source.ts index 1cf5b09..be19ec2 100644 --- a/src/database/data-source.ts +++ b/src/database/data-source.ts @@ -51,7 +51,7 @@ export const AppDataSource = new DataSource({ password: process.env.DB_PASSWORD, connectorPackage: "mysql2", synchronize: false, - timezone: "Z", // "Z" = UTC + // timezone: "Z", // "Z" = UTC logging: ["query", "error"], entities: process.env.NODE_ENV !== "production"