import { Body, Controller, Delete, Example, Get, Patch, Path, Post, Request, Route, Security, Tags, } from "tsoa"; import { AppDataSource } from "../database/data-source"; import HttpSuccess from "../interfaces/http-success"; import HttpStatus from "../interfaces/http-status"; import HttpError from "../interfaces/http-error"; import { ProfileChangeNameHistory } from "../entities/ProfileChangeNameHistory"; import { RequestWithUser } from "../middlewares/user"; import { Profile } from "../entities/Profile"; import { CreateProfileChangeName, CreateProfileChangeNameEmployee, ProfileChangeName, UpdateProfileChangeName, } from "../entities/ProfileChangeName"; import { ProfileEmployee } from "../entities/ProfileEmployee"; import permission from "../interfaces/permission"; @Route("api/v1/org/profile-temp/changeName") @Tags("ProfileChangeNameEmployee") @Security("bearerAuth") export class ProfileChangeNameEmployeeTempController extends Controller { private profileEmployeeRepo = AppDataSource.getRepository(ProfileEmployee); private changeNameRepository = AppDataSource.getRepository(ProfileChangeName); private changeNameHistoryRepository = AppDataSource.getRepository(ProfileChangeNameHistory); @Get("user") public async getChangeNameUser(@Request() request: { user: Record }) { const profile = await this.profileEmployeeRepo.findOneBy({ keycloak: request.user.sub }); if (!profile) { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } const lists = await this.changeNameRepository.find({ where: { profileEmployeeId: profile.id }, select: ["id", "prefix", "firstName", "lastName", "status"], order: { createdAt: "ASC" }, }); return new HttpSuccess(lists); } @Get("{profileEmployeeId}") @Example({ status: 200, message: "สำเร็จ", result: [ { id: "debfa8a7-83fb-4801-a940-8ae74e7638d3", prefix: "string", firstName: "string", lastName: "string", status: "string", }, ], }) public async getChangeName(@Path() profileEmployeeId: string) { const lists = await this.changeNameRepository.find({ where: { profileEmployeeId: profileEmployeeId }, select: ["id", "prefix", "firstName", "lastName", "status"], order: { createdAt: "ASC" }, }); return new HttpSuccess(lists); } @Get("history/{changeNameId}") @Example({ status: 200, message: "สำเร็จ", result: [ { id: "debfa8a7-83fb-4801-a940-8ae74e7638d3", prefix: "string", firstName: "string", lastName: "string", status: "string", createdFullName: "string", createdAt: "string", }, { id: "debfa8a7-83fb-4801-a940-8ae74e7638d3", prefix: "string", firstName: "string", lastName: "string", status: "string", createdFullName: "string", createdAt: "string", }, ], }) public async changeNameHistory(@Path() changeNameId: string) { const record = await this.changeNameHistoryRepository.find({ where: { profileChangeNameId: changeNameId }, select: [ "id", "prefix", "firstName", "lastName", "status", "lastUpdateFullName", "lastUpdatedAt", ], order: { createdAt: "DESC" }, }); return new HttpSuccess(record); } @Post() public async newChangeName( @Request() req: RequestWithUser, @Body() body: CreateProfileChangeNameEmployee, ) { await new permission().PermissionCreate(req, "SYS_REGISTRY_TEMP"); if (!body.profileEmployeeId) { throw new HttpError(HttpStatus.BAD_REQUEST, "กรุณากรอก profileEmployeeId"); } const profile = await this.profileEmployeeRepo.findOneBy({ id: body.profileEmployeeId }); if (!profile) { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } const data = new ProfileChangeName(); const meta = { createdUserId: req.user.sub, createdFullName: req.user.name, lastUpdateUserId: req.user.sub, lastUpdateFullName: req.user.name, }; Object.assign(data, { ...body, ...meta }); await this.changeNameRepository.save(data); profile.firstName = body.firstName ?? profile.firstName; profile.lastName = body.lastName ?? profile.lastName; profile.prefix = body.prefix ?? profile.prefix; await this.profileEmployeeRepo.save(profile); return new HttpSuccess(data.id); } @Patch("{changeNameId}") public async editChangeName( @Request() req: RequestWithUser, @Body() body: UpdateProfileChangeName, @Path() changeNameId: string, ) { await new permission().PermissionUpdate(req, "SYS_REGISTRY_TEMP"); const record = await this.changeNameRepository.findOneBy({ id: changeNameId }); if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); const history = new ProfileChangeNameHistory(); Object.assign(history, { ...record, id: undefined }); Object.assign(record, body); history.profileChangeNameId = changeNameId; record.lastUpdateFullName = req.user.name; history.lastUpdateFullName = req.user.name; await Promise.all([ this.changeNameRepository.save(record), this.changeNameHistoryRepository.save(history), ]); const chkLastRecord = await this.changeNameRepository.findOne({ where: { profileEmployeeId: record.profileEmployeeId, }, order: { createdAt: "DESC", }, }); if (!chkLastRecord) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); const profile = await this.profileEmployeeRepo.findOneBy({ id: record.profileEmployeeId }); if (profile && chkLastRecord.id === record.id) { profile.firstName = body.firstName ?? profile.firstName; profile.lastName = body.lastName ?? profile.lastName; profile.prefix = body.prefix ?? profile.prefix; await this.profileEmployeeRepo.save(profile); } return new HttpSuccess(); } @Delete("{changeNameId}") public async deleteTraning(@Path() changeNameId: string, @Request() req: RequestWithUser) { await new permission().PermissionDelete(req, "SYS_REGISTRY_TEMP"); await this.changeNameHistoryRepository.delete({ profileChangeNameId: changeNameId, }); const result = await this.changeNameRepository.delete({ id: changeNameId }); if (result.affected == undefined || result.affected <= 0) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); } return new HttpSuccess(); } }