From 113e1643e9a52c3f34b3733aa3cb20b2bd0a6ac6 Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Mon, 13 May 2024 11:13:02 +0700 Subject: [PATCH] API change-status , req-edit --- .../KpiUserEvaluationController.ts | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/src/controllers/KpiUserEvaluationController.ts b/src/controllers/KpiUserEvaluationController.ts index 2ba0892..58d5739 100644 --- a/src/controllers/KpiUserEvaluationController.ts +++ b/src/controllers/KpiUserEvaluationController.ts @@ -500,4 +500,82 @@ export class KpiUserEvaluationController extends Controller { await this.kpiUserEvalutionRepository.remove(kpiUserEvaluation); return new HttpSuccess(); } + + /** + * API แก้สถานะแบบประเมินตามรายการที่เลือก + * + * @summary แก้สถานะแบบประเมินตามรายการที่เลือก + * + * + */ + @Post("admin/change-status") + async ChangeStatus( + @Request() request: { user: Record }, + @Body() + requestBody: { + status: string; + id: string[]; + }, + ) { + const list = await this.kpiUserEvalutionRepository.find({ + where: { id: In(requestBody.id) }, + }); + if (!list || list.length === 0) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + } + + const hasAllData = requestBody.id.every((id) => list.some((item) => item.id === id)); + + if (!hasAllData) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "พบบางไอดีผู้ประเมินที่ไม่มีข้อมูล"); + } + + list.forEach(async (item) => { + item.evaluationStatus = requestBody.status.trim().toUpperCase(); + item.lastUpdateUserId = request.user.sub; + item.lastUpdateFullName = request.user.name; + await this.kpiUserEvalutionRepository.save(item); + }); + + return new HttpSuccess(); + } + + /** + * API อนุมัติการขอแก้ไขแบบประเมินตามรายการที่เลือก + * + * @summary อนุมัติการขอแก้ไขแบบประเมินตามรายการที่เลือก + * + * + */ + @Post("admin/req-edit") + async RequestEdit( + @Request() request: { user: Record }, + @Body() + requestBody: { + status: string; + id: string[]; + }, + ) { + const list = await this.kpiUserEvalutionRepository.find({ + where: { id: In(requestBody.id) }, + }); + if (!list || list.length === 0) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + } + + const hasAllData = requestBody.id.every((id) => list.some((item) => item.id === id)); + + if (!hasAllData) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "พบบางไอดีผู้ประเมินที่ไม่มีข้อมูล"); + } + + list.forEach(async (item) => { + item.evaluationReqEdit = requestBody.status.trim().toUpperCase(); + item.lastUpdateUserId = request.user.sub; + item.lastUpdateFullName = request.user.name; + await this.kpiUserEvalutionRepository.save(item); + }); + + return new HttpSuccess(); + } }