diff --git a/src/controllers/KpiUserEvaluationController.ts b/src/controllers/KpiUserEvaluationController.ts index d3e25d4..a27421b 100644 --- a/src/controllers/KpiUserEvaluationController.ts +++ b/src/controllers/KpiUserEvaluationController.ts @@ -10,11 +10,9 @@ import { Body, Path, Request, - Example, SuccessResponse, Response, Query, - ArrayValidator, } from "tsoa"; import { AppDataSource } from "../database/data-source"; import HttpSuccess from "../interfaces/http-success"; @@ -31,7 +29,7 @@ import { updateKpiUserReqEditEvaluation, updateKpiUserResultEvaluation, } from "../entities/kpiUserEvaluation"; -import { Like, In, Brackets, IsNull, Not } from "typeorm"; +import { In, Brackets, IsNull, Not } from "typeorm"; import CallAPI from "../interfaces/call-api"; import { KpiCapacity } from "../entities/kpiCapacity"; import { Position } from "../entities/position"; @@ -39,6 +37,7 @@ import { KpiLink } from "../entities/kpiLink"; import { KpiGroup } from "../entities/kpiGroup"; import { RequestWithUser } from "../middlewares/user"; import permission from "../interfaces/permission"; +import { addLogSequence, setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/kpi/user/evaluation") @Tags("kpiUserEvaluation") @@ -64,7 +63,7 @@ export class KpiUserEvaluationController extends Controller { */ @Post("admin") async listKpiAdminEvaluation( - @Request() request: { user: Record }, + @Request() request: RequestWithUser, @Body() requestBody: { page: number; @@ -216,6 +215,123 @@ export class KpiUserEvaluationController extends Controller { }, ) { await new permission().PermissionDelete(request, "SYS_RESULT"); + let conditionFullName = + "CONCAT(kpiUserEvaluation.prefix, kpiUserEvaluation.firstName, ' ', kpiUserEvaluation.lastName) LIKE :keyword"; + const [kpiUserEvaluation, total] = await AppDataSource.getRepository(KpiUserEvaluation) + .createQueryBuilder("kpiUserEvaluation") + .andWhere(requestBody.kpiPeriodId ? "kpiPeriodId LIKE :kpiPeriodId" : "1=1", { + kpiPeriodId: requestBody.kpiPeriodId, + }) + .andWhere( + requestBody.status != null && requestBody.status != undefined + ? "evaluationstatus LIKE :status" + : "1=1", + { + status: + requestBody.status == null || requestBody.status == undefined + ? null + : requestBody.status.trim().toUpperCase(), + }, + ) + .andWhere( + requestBody.results != null && requestBody.results != undefined + ? "evaluationResults LIKE :results" + : "1=1", + { + results: + requestBody.results == null || requestBody.results == undefined + ? null + : requestBody.results.trim().toUpperCase(), + }, + ) + .andWhere( + new Brackets((qb) => { + qb.orWhere("kpiUserEvaluation.prefix LIKE :keyword", { + keyword: `%${requestBody.keyword}%`, + }) + .orWhere("kpiUserEvaluation.firstName LIKE :keyword", { + keyword: `%${requestBody.keyword}%`, + }) + .orWhere("kpiUserEvaluation.lastName LIKE :keyword", { + keyword: `%${requestBody.keyword}%`, + }) + .orWhere("kpiUserEvaluation.org LIKE :keyword", { + keyword: `%${requestBody.keyword}%`, + }) + .orWhere("kpiUserEvaluation.position LIKE :keyword", { + keyword: `%${requestBody.keyword}%`, + }) + .orWhere("kpiUserEvaluation.posTypeName LIKE :keyword", { + keyword: `%${requestBody.keyword}%`, + }) + .orWhere("kpiUserEvaluation.posLevelName LIKE :keyword", { + keyword: `%${requestBody.keyword}%`, + }) + .orWhere(conditionFullName, { + keyword: `%${requestBody.keyword}%`, + }); + }), + ) + .orderBy("kpiUserEvaluation.createdAt", "ASC") + .skip((requestBody.page - 1) * requestBody.pageSize) + .take(requestBody.pageSize) + .getManyAndCount(); + + const mapData = kpiUserEvaluation.map((item) => { + const fullNameParts = [item.child4, item.child3, item.child2, item.child1, item.org]; + + const organization = fullNameParts + .filter((part) => part !== undefined && part !== null) + .join("/"); + + return { + id: item.id, + profileId: item.profileId, + prefix: item.prefix, + firstname: item.firstName, + lastname: item.lastName, + kpiPeriodId: item.kpiPeriodId, + evaluationStatus: item.evaluationStatus, + evaluationResults: item.evaluationResults, + createdAt: item.createdAt, + evaluatorId: item.evaluatorId, + commanderId: item.commanderId, + commanderHighId: item.commanderHighId, + root: item.org ? item.org : null, + rootId: item.orgId ? item.orgId : null, + position: item.position ? item.position : null, + // posTypeId: item.posTypeId, + posTypeName: item.posTypeName ? item.posTypeName : null, + // posLevelId: item.posLevelId, + posLevelName: item.posLevelName ? item.posLevelName : null, + organization: organization ? organization : null, + }; + }); + return new HttpSuccess({ data: mapData, total }); + } + + /** + * API + * + * @summary รายการประเมินผลการปฏิบัติราชการระดับบุคคลทั้งหมด + * + */ + @Post("list") + async listKpiListEvaluation( + @Request() request: RequestWithUser, + @Body() + requestBody: { + page: number; + pageSize: number; + kpiPeriodId?: string; + keyword?: string; + status?: string | null; + results?: string | null; + reqedit?: string | null; + evaluating?: boolean | null; + }, + ) { + await new permission().PermissionDelete(request, "SYS_KPI_LIST"); let conditionFullName = "CONCAT(kpiUserEvaluation.prefix, kpiUserEvaluation.firstName, ' ', kpiUserEvaluation.lastName) LIKE :keyword"; const [kpiUserEvaluation, total] = await AppDataSource.getRepository(KpiUserEvaluation) @@ -401,13 +517,15 @@ export class KpiUserEvaluationController extends Controller { kpiUserEvaluation.posTypeNameEvaluator = x.posTypeName; kpiUserEvaluation.orgEvaluator = x.root; }); + const before = null; kpiUserEvaluation.evaluationStatus = "NEW"; kpiUserEvaluation.evaluationResults = "PENDING"; kpiUserEvaluation.createdUserId = request.user.sub; kpiUserEvaluation.createdFullName = request.user.name; kpiUserEvaluation.lastUpdateUserId = request.user.sub; kpiUserEvaluation.lastUpdateFullName = request.user.name; - await this.kpiUserEvalutionRepository.save(kpiUserEvaluation); + await this.kpiUserEvalutionRepository.save(kpiUserEvaluation, { data: request }); + setLogDataDiff(request, { before, after: kpiUserEvaluation }); enum CapacityType { HEAD = "HEAD", @@ -533,7 +651,9 @@ export class KpiUserEvaluationController extends Controller { kpiUserEvaluation.weightPoint1 = 50; kpiUserEvaluation.weightPoint2 = 50; } - await this.kpiUserEvalutionRepository.save(kpiUserEvaluation); + + await this.kpiUserEvalutionRepository.save(kpiUserEvaluation, { data: request }); + setLogDataDiff(request, { before, after: kpiUserEvaluation }); return new HttpSuccess(kpiUserEvaluation.id); } @@ -549,7 +669,7 @@ export class KpiUserEvaluationController extends Controller { async updateKpiUserCheckEvaluation( @Path() id: string, @Body() requestBody: updateKpiUserCheckEvaluation, - @Request() request: { user: Record }, + @Request() request: RequestWithUser, ) { const kpiUserEvaluation = await this.kpiUserEvalutionRepository.findOne({ where: { id: id }, @@ -560,10 +680,14 @@ export class KpiUserEvaluationController extends Controller { "ไม่พบข้อมูลรายการประเมินผลการปฏิบัติราชการระดับบุคคลนี้", ); } + const before = structuredClone(kpiUserEvaluation); + kpiUserEvaluation.lastUpdateUserId = request.user.sub; kpiUserEvaluation.lastUpdateFullName = request.user.name; Object.assign(kpiUserEvaluation, requestBody); - await this.kpiUserEvalutionRepository.save(kpiUserEvaluation); + await this.kpiUserEvalutionRepository.save(kpiUserEvaluation, { data: request }); + setLogDataDiff(request, { before, after: kpiUserEvaluation }); + return new HttpSuccess(kpiUserEvaluation.id); } @@ -578,7 +702,7 @@ export class KpiUserEvaluationController extends Controller { async updateKpiUserPointEvaluation( @Path() id: string, @Body() requestBody: updateKpiUserPointEvaluation, - @Request() request: { user: Record }, + @Request() request: RequestWithUser, ) { const kpiUserEvaluation = await this.kpiUserEvalutionRepository.findOne({ where: { id: id }, @@ -605,10 +729,12 @@ export class KpiUserEvaluationController extends Controller { } else { kpiUserEvaluation.evaluationResults = "IMPROVEMENT"; } + const before = structuredClone(kpiUserEvaluation); kpiUserEvaluation.lastUpdateUserId = request.user.sub; kpiUserEvaluation.lastUpdateFullName = request.user.name; Object.assign(kpiUserEvaluation, requestBody); - await this.kpiUserEvalutionRepository.save(kpiUserEvaluation); + await this.kpiUserEvalutionRepository.save(kpiUserEvaluation, { data: request }); + setLogDataDiff(request, { before, after: kpiUserEvaluation }); return new HttpSuccess(kpiUserEvaluation.id); } @@ -623,7 +749,7 @@ export class KpiUserEvaluationController extends Controller { async updateKpiUserEvaluation( @Path() id: string, @Body() requestBody: updateKpiUserEvaluation, - @Request() request: { user: Record }, + @Request() request: RequestWithUser, ) { const kpiUserEvaluation = await this.kpiUserEvalutionRepository.findOne({ where: { id: id }, @@ -644,11 +770,12 @@ export class KpiUserEvaluationController extends Controller { "ไม่พบข้อมูลรอบการประเมินผลการปฏิบัติหน้าที่ราชการนี้", ); } - + const before = structuredClone(kpiUserEvaluation); kpiUserEvaluation.lastUpdateUserId = request.user.sub; kpiUserEvaluation.lastUpdateFullName = request.user.name; Object.assign(kpiUserEvaluation, requestBody); - await this.kpiUserEvalutionRepository.save(kpiUserEvaluation); + await this.kpiUserEvalutionRepository.save(kpiUserEvaluation, { data: request }); + setLogDataDiff(request, { before, after: kpiUserEvaluation }); return new HttpSuccess(kpiUserEvaluation.id); } @@ -663,7 +790,7 @@ export class KpiUserEvaluationController extends Controller { async updateKpiUserReqEditEvaluation( @Path() id: string, @Body() requestBody: updateKpiUserReqEditEvaluation, - @Request() request: { user: Record }, + @Request() request: RequestWithUser, ) { const kpiUserEvaluation = await this.kpiUserEvalutionRepository.findOne({ where: { id: id }, @@ -674,10 +801,12 @@ export class KpiUserEvaluationController extends Controller { "ไม่พบข้อมูลรายการประเมินผลการปฏิบัติราชการระดับบุคคลนี้", ); } + const before = structuredClone(kpiUserEvaluation); kpiUserEvaluation.evaluationReqEdit = requestBody.status.trim().toUpperCase(); kpiUserEvaluation.lastUpdateUserId = request.user.sub; kpiUserEvaluation.lastUpdateFullName = request.user.name; - await this.kpiUserEvalutionRepository.save(kpiUserEvaluation); + await this.kpiUserEvalutionRepository.save(kpiUserEvaluation, { data: request }); + setLogDataDiff(request, { before, after: kpiUserEvaluation }); return new HttpSuccess(kpiUserEvaluation.id); } @@ -692,7 +821,7 @@ export class KpiUserEvaluationController extends Controller { async updateKpiUserResultEvaluation( @Path() id: string, @Body() requestBody: updateKpiUserResultEvaluation, - @Request() request: { user: Record }, + @Request() request: RequestWithUser, ) { const kpiUserEvaluation = await this.kpiUserEvalutionRepository.findOne({ where: { id: id }, @@ -703,10 +832,12 @@ export class KpiUserEvaluationController extends Controller { "ไม่พบข้อมูลรายการประเมินผลการปฏิบัติราชการระดับบุคคลนี้", ); } + const before = structuredClone(kpiUserEvaluation); kpiUserEvaluation.evaluationResults = requestBody.status.trim().toUpperCase(); kpiUserEvaluation.lastUpdateUserId = request.user.sub; kpiUserEvaluation.lastUpdateFullName = request.user.name; - await this.kpiUserEvalutionRepository.save(kpiUserEvaluation); + await this.kpiUserEvalutionRepository.save(kpiUserEvaluation, { data: request }); + setLogDataDiff(request, { before, after: kpiUserEvaluation }); return new HttpSuccess(kpiUserEvaluation.id); } @@ -721,7 +852,7 @@ export class KpiUserEvaluationController extends Controller { async updateKpiUserStatusEvaluation( @Path() id: string, @Body() requestBody: updateKpiUserStatusEvaluation, - @Request() request: { user: Record }, + @Request() request: RequestWithUser, ) { const kpiUserEvaluation = await this.kpiUserEvalutionRepository.findOne({ where: { id: id }, @@ -759,10 +890,12 @@ export class KpiUserEvaluationController extends Controller { .then((x) => {}) .catch((x) => {}); } + const before = structuredClone(kpiUserEvaluation); kpiUserEvaluation.evaluationStatus = requestBody.status.trim().toUpperCase(); kpiUserEvaluation.lastUpdateUserId = request.user.sub; kpiUserEvaluation.lastUpdateFullName = request.user.name; - await this.kpiUserEvalutionRepository.save(kpiUserEvaluation); + await this.kpiUserEvalutionRepository.save(kpiUserEvaluation, { data: request }); + setLogDataDiff(request, { before, after: kpiUserEvaluation }); return new HttpSuccess(kpiUserEvaluation.id); } @@ -894,7 +1027,7 @@ export class KpiUserEvaluationController extends Controller { * @param {string} id Guid, *Id รายการประเมินผลการปฏิบัติราชการระดับบุคคล (USER) */ @Delete("{id}") - async deleteKpiUserEvaluation(@Path() id: string) { + async deleteKpiUserEvaluation(@Path() id: string, @Request() request: RequestWithUser) { const kpiUserEvaluation = await this.kpiUserEvalutionRepository.findOne({ where: { id: id }, }); @@ -904,7 +1037,7 @@ export class KpiUserEvaluationController extends Controller { "ไม่พบข้อมูลการประเมินผลการปฏิบัติราชการระดับบุคคลนี้", ); } - await this.kpiUserEvalutionRepository.remove(kpiUserEvaluation); + await this.kpiUserEvalutionRepository.remove(kpiUserEvaluation, { data: request }); return new HttpSuccess(); } @@ -917,7 +1050,7 @@ export class KpiUserEvaluationController extends Controller { */ @Post("admin/change-status") async ChangeStatus( - @Request() request: { user: Record }, + @Request() request: RequestWithUser, @Body() requestBody: { status: string; @@ -1001,9 +1134,11 @@ export class KpiUserEvaluationController extends Controller { } else { item.evaluationStatus = requestBody.status.trim().toUpperCase(); } + const before = null; item.lastUpdateUserId = request.user.sub; item.lastUpdateFullName = request.user.name; - await this.kpiUserEvalutionRepository.save(item); + await this.kpiUserEvalutionRepository.save(item, { data: request }); + setLogDataDiff(request, { before, after: item }); }), ); return new HttpSuccess(); @@ -1018,7 +1153,7 @@ export class KpiUserEvaluationController extends Controller { */ @Post("admin/req-edit") async RequestEdit( - @Request() request: { user: Record }, + @Request() request: RequestWithUser, @Body() requestBody: { status: string; @@ -1082,9 +1217,11 @@ export class KpiUserEvaluationController extends Controller { item.evaluationReqEdit = requestBody.status.trim().toUpperCase(); } + const before = null; item.lastUpdateUserId = request.user.sub; item.lastUpdateFullName = request.user.name; - await this.kpiUserEvalutionRepository.save(item); + await this.kpiUserEvalutionRepository.save(item, { data: request }); + setLogDataDiff(request, { before, after: item }); }); return new HttpSuccess(); @@ -1099,7 +1236,7 @@ export class KpiUserEvaluationController extends Controller { */ @Post("admin/result-status") async ResultStatus( - @Request() request: { user: Record }, + @Request() request: RequestWithUser, @Body() requestBody: { status: string; @@ -1160,9 +1297,11 @@ export class KpiUserEvaluationController extends Controller { // } else { // // item.evaluationStatus = requestBody.status.trim().toUpperCase(); // } + const before = null; item.lastUpdateUserId = request.user.sub; item.lastUpdateFullName = request.user.name; - await this.kpiUserEvalutionRepository.save(item); + await this.kpiUserEvalutionRepository.save(item, { data: request }); + setLogDataDiff(request, { before, after: item }); }); return new HttpSuccess(); @@ -1174,10 +1313,7 @@ export class KpiUserEvaluationController extends Controller { * @param {string} id Guid, *Id คนประเมิน (USER) */ @Get("open/{id}") - async openKpiUserEvaluation( - @Path() id: string, - @Request() request: { user: Record }, - ) { + async openKpiUserEvaluation(@Path() id: string, @Request() request: RequestWithUser) { const kpiUserEvaluation = await this.kpiUserEvalutionRepository.findOne({ where: { id: id }, }); @@ -1187,11 +1323,13 @@ export class KpiUserEvaluationController extends Controller { "ไม่พบข้อมูลรายการประเมินผลการปฏิบัติราชการระดับบุคคลนี้", ); } + const before = structuredClone(kpiUserEvaluation); kpiUserEvaluation.isOpen = true; kpiUserEvaluation.openDate = new Date(); kpiUserEvaluation.lastUpdateUserId = request.user.sub; kpiUserEvaluation.lastUpdateFullName = request.user.name; - await this.kpiUserEvalutionRepository.save(kpiUserEvaluation); + await this.kpiUserEvalutionRepository.save(kpiUserEvaluation, { data: request }); + setLogDataDiff(request, { before, after: kpiUserEvaluation }); return new HttpSuccess(kpiUserEvaluation.id); } @@ -1212,7 +1350,7 @@ export class KpiUserEvaluationController extends Controller { timeEvaluator?: string | null; reasonEvaluator?: string | null; }, - @Request() request: { user: Record }, + @Request() request: RequestWithUser, ) { const kpiUserEvaluation = await this.kpiUserEvalutionRepository.findOne({ where: { id: id }, @@ -1253,6 +1391,8 @@ export class KpiUserEvaluationController extends Controller { .catch((x) => {}); kpiUserEvaluation.evaluationStatus = "SUMMARY_COMMANDER"; } + const before = structuredClone(kpiUserEvaluation); + kpiUserEvaluation.topicEvaluator = requestBody.topicEvaluator == null ? _null : requestBody.topicEvaluator; kpiUserEvaluation.developEvaluator = @@ -1263,7 +1403,8 @@ export class KpiUserEvaluationController extends Controller { requestBody.reasonEvaluator == null ? _null : requestBody.reasonEvaluator; kpiUserEvaluation.lastUpdateUserId = request.user.sub; kpiUserEvaluation.lastUpdateFullName = request.user.name; - await this.kpiUserEvalutionRepository.save(kpiUserEvaluation); + await this.kpiUserEvalutionRepository.save(kpiUserEvaluation, { data: request }); + setLogDataDiff(request, { before, after: kpiUserEvaluation }); return new HttpSuccess(kpiUserEvaluation.id); } @@ -1282,7 +1423,7 @@ export class KpiUserEvaluationController extends Controller { isReason: boolean; reason?: string | null; }, - @Request() request: { user: Record }, + @Request() request: RequestWithUser, ) { const kpiUserEvaluation = await this.kpiUserEvalutionRepository.findOne({ where: { id: id }, @@ -1323,11 +1464,15 @@ export class KpiUserEvaluationController extends Controller { .catch((x) => {}); kpiUserEvaluation.evaluationStatus = "SUMMARY_COMMANDER_HIGH"; } + const before = structuredClone(kpiUserEvaluation); + kpiUserEvaluation.isReasonCommander = requestBody.isReason; kpiUserEvaluation.reasonCommander = requestBody.reason == null ? _null : requestBody.reason; kpiUserEvaluation.lastUpdateUserId = request.user.sub; kpiUserEvaluation.lastUpdateFullName = request.user.name; - await this.kpiUserEvalutionRepository.save(kpiUserEvaluation); + await this.kpiUserEvalutionRepository.save(kpiUserEvaluation, { data: request }); + setLogDataDiff(request, { before, after: kpiUserEvaluation }); + return new HttpSuccess(kpiUserEvaluation.id); } @@ -1346,7 +1491,7 @@ export class KpiUserEvaluationController extends Controller { isReason: boolean; reason?: string | null; }, - @Request() request: { user: Record }, + @Request() request: RequestWithUser, ) { const kpiUserEvaluation = await this.kpiUserEvalutionRepository.findOne({ where: { id: id }, @@ -1371,11 +1516,15 @@ export class KpiUserEvaluationController extends Controller { }) .then((x) => {}) .catch((x) => {}); + const before = structuredClone(kpiUserEvaluation); + kpiUserEvaluation.isReasonCommanderHigh = requestBody.isReason; kpiUserEvaluation.reasonCommanderHigh = requestBody.reason == null ? _null : requestBody.reason; kpiUserEvaluation.lastUpdateUserId = request.user.sub; kpiUserEvaluation.lastUpdateFullName = request.user.name; - await this.kpiUserEvalutionRepository.save(kpiUserEvaluation); + await this.kpiUserEvalutionRepository.save(kpiUserEvaluation, { data: request }); + setLogDataDiff(request, { before, after: kpiUserEvaluation }); + return new HttpSuccess(kpiUserEvaluation.id); } /** @@ -1426,9 +1575,11 @@ export class KpiUserEvaluationController extends Controller { }) .then((x) => {}) .catch((x) => {}); + const before = null; kpiUserEvaluation.lastUpdateUserId = request.user.sub; kpiUserEvaluation.lastUpdateFullName = request.user.name; - await this.kpiUserEvalutionRepository.save(kpiUserEvaluation); + await this.kpiUserEvalutionRepository.save(kpiUserEvaluation, { data: request }); + setLogDataDiff(request, { before, after: kpiUserEvaluation }); }), ); @@ -1495,10 +1646,7 @@ export class KpiUserEvaluationController extends Controller { * @param {string} id Guid, *Id คนประเมิน (USER) */ @Get("summary/{id}") - async getSummaryKpiEvaluation( - @Path() id: string, - @Request() request: { user: Record }, - ) { + async getSummaryKpiEvaluation(@Path() id: string, @Request() request: RequestWithUser) { const kpiUserEvaluation = await this.kpiUserEvalutionRepository.findOne({ where: { id: id }, select: ["id", "evaluationStatus", "lastUpdateUserId", "lastUpdateFullName"], @@ -1509,10 +1657,13 @@ export class KpiUserEvaluationController extends Controller { "ไม่พบข้อมูลรายการประเมินผลการปฏิบัติราชการระดับบุคคลนี้", ); } + const before = structuredClone(kpiUserEvaluation); kpiUserEvaluation.evaluationStatus = "SUMMARY"; kpiUserEvaluation.lastUpdateUserId = request.user.sub; kpiUserEvaluation.lastUpdateFullName = request.user.name; await this.kpiUserEvalutionRepository.save(kpiUserEvaluation, { data: request }); + setLogDataDiff(request, { before, after: kpiUserEvaluation }); + return new HttpSuccess(kpiUserEvaluation); } }