From 6b3d1dc67b940bb9b847c9ebfb302cee846cab94 Mon Sep 17 00:00:00 2001 From: harid Date: Thu, 12 Mar 2026 18:01:15 +0700 Subject: [PATCH] =?UTF-8?q?API=20=E0=B8=A5=E0=B8=9A=E0=B8=82=E0=B9=89?= =?UTF-8?q?=E0=B8=AD=E0=B8=A1=E0=B8=B9=E0=B8=A5=E0=B8=81=E0=B8=B2=E0=B8=A3?= =?UTF-8?q?=E0=B8=9D=E0=B8=B6=E0=B8=81=E0=B8=AD=E0=B8=9A=E0=B8=A3=E0=B8=A1?= =?UTF-8?q?/=E0=B8=94=E0=B8=B9=E0=B8=87=E0=B8=B2=E0=B8=99=20=E0=B9=81?= =?UTF-8?q?=E0=B8=A5=E0=B8=B0=20=E0=B8=81=E0=B8=B2=E0=B8=A3=E0=B8=9E?= =?UTF-8?q?=E0=B8=B1=E0=B8=92=E0=B8=99=E0=B8=B2=E0=B8=A3=E0=B8=B2=E0=B8=A2?= =?UTF-8?q?=E0=B8=9A=E0=B8=B8=E0=B8=84=E0=B8=84=E0=B8=A5=20IDP=20=E0=B8=A3?= =?UTF-8?q?=E0=B8=B2=E0=B8=A2=E0=B8=9A=E0=B8=B8=E0=B8=84=E0=B8=84=E0=B8=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ProfileDevelopmentEmployeeController.ts | 39 +++++++++ src/controllers/ProfileTrainingController.ts | 82 +++++++++++++++++++ 2 files changed, 121 insertions(+) diff --git a/src/controllers/ProfileDevelopmentEmployeeController.ts b/src/controllers/ProfileDevelopmentEmployeeController.ts index 2f5a7715..d56ae484 100644 --- a/src/controllers/ProfileDevelopmentEmployeeController.ts +++ b/src/controllers/ProfileDevelopmentEmployeeController.ts @@ -27,6 +27,7 @@ import { import permission from "../interfaces/permission"; import { DevelopmentProject } from "../entities/DevelopmentProject"; import { In, Brackets } from "typeorm"; +import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/org/profile-employee/development") @Tags("ProfileDevelopment") @Security("bearerAuth") @@ -273,6 +274,44 @@ export class ProfileDevelopmentEmployeeController extends Controller { return new HttpSuccess(); } + /** + * API ลบข้อมูลการพัฒนารายบุคคล IDP + * @summary API ลบข้อมูลการพัฒนารายบุคคล IDP + * @param developmentId คีย์การพัฒนารายบุคคล IDP + */ + @Patch("update-delete/{developmentId}") + public async updateIsDeletedTraining( + @Request() req: RequestWithUser, + @Path() developmentId: string, + ) { + const record = await this.developmentRepository.findOneBy({ id: developmentId }); + if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + if (record.isDeleted === true) { + return new HttpSuccess(); + } + await new permission().PermissionOrgUserDelete(req, "SYS_REGISTRY_EMP", record.profileEmployeeId); + const before = structuredClone(record); + const history = new ProfileDevelopmentHistory(); + const now = new Date(); + record.isDeleted = true; + record.lastUpdateUserId = req.user.sub; + record.lastUpdateFullName = req.user.name; + record.lastUpdatedAt = now; + + Object.assign(history, { ...record, id: undefined }); + history.createdUserId = req.user.sub; + history.createdFullName = req.user.name; + history.createdAt = now; + + await Promise.all([ + this.developmentRepository.save(record, { data: req }), + setLogDataDiff(req, { before, after: record }), + this.developmentHistoryRepository.save(history, { data: req }), + ]); + + return new HttpSuccess(); + } + @Delete("{developmentId}") public async deleteDevelopment(@Path() developmentId: string, @Request() req: RequestWithUser) { const _record = await this.developmentRepository.findOneBy({ id: developmentId }); diff --git a/src/controllers/ProfileTrainingController.ts b/src/controllers/ProfileTrainingController.ts index 0df7594f..a286d26a 100644 --- a/src/controllers/ProfileTrainingController.ts +++ b/src/controllers/ProfileTrainingController.ts @@ -23,6 +23,7 @@ import HttpError from "../interfaces/http-error"; import { ProfileTrainingHistory } from "../entities/ProfileTrainingHistory"; import { RequestWithUser } from "../middlewares/user"; import { Profile } from "../entities/Profile"; +import { ProfileEmployee } from "../entities/ProfileEmployee"; import permission from "../interfaces/permission"; import { setLogDataDiff } from "../interfaces/utils"; import { ProfileDevelopment } from "../entities/ProfileDevelopment"; @@ -33,11 +34,13 @@ import { In } from "typeorm"; @Security("bearerAuth") export class ProfileTrainingController extends Controller { private profileRepo = AppDataSource.getRepository(Profile); + private profileEmployeeRepo = AppDataSource.getRepository(ProfileEmployee); private trainingRepo = AppDataSource.getRepository(ProfileTraining); private trainingHistoryRepo = AppDataSource.getRepository(ProfileTrainingHistory); private developmentRepo = AppDataSource.getRepository(ProfileDevelopment); private developmentHistoryRepo = AppDataSource.getRepository(ProfileDevelopmentHistory); + @Get("user") public async getTrainingUser(@Request() request: { user: Record }) { const profile = await this.profileRepo.findOneBy({ keycloak: request.user.sub }); @@ -256,4 +259,83 @@ export class ProfileTrainingController extends Controller { return new HttpSuccess(); } + /** + * API ลบข้อมูลการฝึกอบรม/ดูงาน และ การพัฒนารายบุคคล IDP รายบุคคล + * @summary API ลบข้อมูลการฝึกอบรม/ดูงาน และ การพัฒนารายบุคคล IDP รายบุคคล + */ + @Post("delete-byId") + public async deleteById( + @Body() reqBody: { + type: string; + profileId: string; + developmentId: string; + }, + @Request() req: RequestWithUser + ) { + + const type = reqBody.type?.trim().toUpperCase(); + // 1. validate profile + if (type === "OFFICER") { + const profile = await this.profileRepo.findOneBy({ id: reqBody.profileId }); + if (!profile) { + throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); + } + } else { + const profile = await this.profileEmployeeRepo.findOneBy({ id: reqBody.profileId }); + if (!profile) { + throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); + } + } + + const profileField = type === "OFFICER" ? "profileId" : "profileEmployeeId"; + + // 2. หา ProfileTraining + const trainings = await this.trainingRepo.find({ + select: { id: true }, + where: { + developmentId: reqBody.developmentId, + [profileField]: reqBody.profileId, + }, + }); + + if (trainings.length > 0) { + const trainingIds = trainings.map(x => x.id); + + // 3. ลบ TrainingHistory + await this.trainingHistoryRepo.delete({ + profileTrainingId: In(trainingIds), + }); + + // 4. ลบ ProfileTraining + await this.trainingRepo.delete({ + id: In(trainingIds), + }); + } + + // 5. หา ProfileDevelopment + const developments = await this.developmentRepo.find({ + select: { id: true }, + where: { + kpiDevelopmentId: reqBody.developmentId, + [profileField]: reqBody.profileId, + }, + }); + + if (developments.length > 0) { + const devIds = developments.map(x => x.id); + + // 6. ลบ DevelopmentHistory + await this.developmentHistoryRepo.delete({ + kpiDevelopmentId: In(devIds), + }); + + // 7. ลบ ProfileDevelopment + await this.developmentRepo.delete({ + id: In(devIds), + }); + } + + return new HttpSuccess(); + } + }