From b7ae2e35a45b560e014c92a4a7b2e1c37a1af0c3 Mon Sep 17 00:00:00 2001 From: Bright Date: Thu, 10 Oct 2024 17:58:34 +0700 Subject: [PATCH] fix #656, #657 --- .../DevelopmentRequestController.ts | 22 +++---- .../ProfileDevelopmentController.ts | 60 ++++++++++++++++--- 2 files changed, 64 insertions(+), 18 deletions(-) diff --git a/src/controllers/DevelopmentRequestController.ts b/src/controllers/DevelopmentRequestController.ts index e9745e79..10ec4363 100644 --- a/src/controllers/DevelopmentRequestController.ts +++ b/src/controllers/DevelopmentRequestController.ts @@ -301,17 +301,8 @@ export class DevelopmentRequestController extends Controller { }); if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); await new permission().PermissionUpdate(req, "SYS_REGISTRY_OFFICER"); - const before = structuredClone(record); - requestBody.status = requestBody.status.trim().toUpperCase(); - Object.assign(record, requestBody); - - record.lastUpdateUserId = req.user.sub; - record.lastUpdateFullName = req.user.name; - record.lastUpdatedAt = new Date(); - - await this.developmentRequestRepository.save(record, { data: req }); - setLogDataDiff(req, { before, after: record }); - if (requestBody.status == "APPROVE") { + + if (requestBody.status == "APPROVE" && record.status == "PENDING") { let profileDevelopment = new ProfileDevelopment(); const meta = { createdUserId: req.user.sub, @@ -368,7 +359,16 @@ export class DevelopmentRequestController extends Controller { ); } } + const before = structuredClone(record); + requestBody.status = requestBody.status.trim().toUpperCase(); + Object.assign(record, requestBody); + record.lastUpdateUserId = req.user.sub; + record.lastUpdateFullName = req.user.name; + record.lastUpdatedAt = new Date(); + + await this.developmentRequestRepository.save(record, { data: req }); + setLogDataDiff(req, { before, after: record }); return new HttpSuccess(record.id); } diff --git a/src/controllers/ProfileDevelopmentController.ts b/src/controllers/ProfileDevelopmentController.ts index 76aab0fa..08948b7c 100644 --- a/src/controllers/ProfileDevelopmentController.ts +++ b/src/controllers/ProfileDevelopmentController.ts @@ -10,6 +10,7 @@ import { Route, Security, Tags, + Query } from "tsoa"; import { AppDataSource } from "../database/data-source"; import HttpSuccess from "../interfaces/http-success"; @@ -25,7 +26,7 @@ import { } from "../entities/ProfileDevelopment"; import permission from "../interfaces/permission"; import { DevelopmentProject } from "../entities/DevelopmentProject"; -import { In } from "typeorm"; +import { In, Brackets } from "typeorm"; @Route("api/v1/org/profile/development") @Tags("ProfileDevelopment") @Security("bearerAuth") @@ -49,13 +50,58 @@ export class ProfileDevelopmentController extends Controller { } @Get("{profileId}") - public async getDevelopment(@Path() profileId: string, @Request() req: RequestWithUser) { + public async getDevelopment( + @Path() profileId: string, + @Request() req: RequestWithUser, + @Query("page") page: number = 1, + @Query("pageSize") pageSize: number = 10, + @Query() searchKeyword: string = "", + ) { await new permission().PermissionOrgUserGet(req, "SYS_REGISTRY_OFFICER", profileId); - const lists = await this.developmentRepository.find({ - where: { profileId: profileId }, - order: { createdAt: "ASC" }, - }); - return new HttpSuccess(lists); + const [profileDevelopment, total] = await AppDataSource.getRepository(ProfileDevelopment) + .createQueryBuilder("profileDevelopment") + .where({ profileId: profileId }) + .andWhere( + new Brackets((qb) => { + qb.where( + searchKeyword != undefined && searchKeyword != null && searchKeyword != "" + ? "profileDevelopment.name LIKE :keyword" + : "1=1", + { + keyword: `%${searchKeyword}%`, + }, + ) + .orWhere( + searchKeyword != undefined && searchKeyword != null && searchKeyword != "" + ? "profileDevelopment.developmentTarget LIKE :keyword" + : "1=1", + { + keyword: `%${searchKeyword}%`, + }, + ) + .orWhere( + searchKeyword != undefined && searchKeyword != null && searchKeyword != "" + ? "profileDevelopment.developmentResults LIKE :keyword" + : "1=1", + { + keyword: `%${searchKeyword}%`, + }, + ) + .orWhere( + searchKeyword != undefined && searchKeyword != null && searchKeyword != "" + ? "profileDevelopment.developmentReport LIKE :keyword" + : "1=1", + { + keyword: `%${searchKeyword}%`, + }, + ); + }), + ) + .orderBy("profileDevelopment.createdAt", "ASC") + .skip((page - 1) * pageSize) + .take(pageSize) + .getManyAndCount(); + return new HttpSuccess({ data: profileDevelopment, total }); } @Get("history/{developmentId}")