import { Body, Controller, Delete, Example, Get, Patch, Path, Post, Request, Route, Security, Tags, } from "tsoa"; import { AppDataSource } from "../database/data-source"; import { CreateProfileEmployeeTraining, ProfileTraining, UpdateProfileTraining, } from "../entities/ProfileTraining"; import HttpSuccess from "../interfaces/http-success"; import HttpStatus from "../interfaces/http-status"; import HttpError from "../interfaces/http-error"; import { ProfileTrainingHistory } from "../entities/ProfileTrainingHistory"; import { RequestWithUser } from "../middlewares/user"; import { ProfileEmployee } from "../entities/ProfileEmployee"; @Route("api/v1/org/profile-employee/training") @Tags("ProfileEmployeeTraining") @Security("bearerAuth") export class ProfileTrainingEmployeeController extends Controller { private profileEmployeeRepo = AppDataSource.getRepository(ProfileEmployee); private trainingRepo = AppDataSource.getRepository(ProfileTraining); private trainingHistoryRepo = AppDataSource.getRepository(ProfileTrainingHistory); @Get("user") public async getTrainingUser(@Request() request: { user: Record }) { const profile = await this.profileEmployeeRepo.findOneBy({ keycloak: request.user.sub }); if (!profile) { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } const record = await this.trainingRepo.find({ where: { profileEmployeeId: profile.id }, }); return new HttpSuccess(record); } @Get("{profileEmployeeId}") @Example({ status: 200, message: "สำเร็จ", result: [ { id: "3cf02fb7-2f0c-471b-b641-51d557375c0a", createdAt: "2024-03-12T02:55:56.915Z", createdUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0", lastUpdatedAt: "2024-03-12T02:55:56.915Z", lastUpdateUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0", createdFullName: "สาวิตรี ศรีสมัย", lastUpdateFullName: "สาวิตรี ศรีสมัย", profileEmployeeId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201", isActive: true, startDate: "2024-03-12T09:55:23.000Z", endDate: "2024-03-12T09:55:23.000Z", numberOrder: "string", topic: "string", place: "string", dateOrder: "2024-03-12T09:55:23.000Z", department: "string", duration: "string", name: "string", yearly: 0, isDate: true, }, ], }) public async getTraining(@Path() profileEmployeeId: string) { const record = await this.trainingRepo.findBy({ profileEmployeeId }); return new HttpSuccess(record); } @Get("history/{trainingId}") @Example({ status: 200, message: "สำเร็จ", result: [ { id: "6d4e9dbe-8697-4546-9651-0a1c3ea0a82d", createdAt: "2024-03-12T02:55:56.971Z", createdUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0", lastUpdatedAt: "2024-03-12T02:55:56.971Z", lastUpdateUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0", createdFullName: "สาวิตรี ศรีสมัย", lastUpdateFullName: "สาวิตรี ศรีสมัย", isActive: true, startDate: "2024-03-12T09:55:23.000Z", endDate: "2024-03-12T09:55:23.000Z", numberOrder: "string", topic: "string", place: "string", dateOrder: "2024-03-12T09:55:23.000Z", department: "string", duration: "string", name: "string", yearly: 0, isDate: true, profileTrainingId: "3cf02fb7-2f0c-471b-b641-51d557375c0a", }, { id: "a251c176-3dac-4d09-9813-38c8db1127e3", createdAt: "2024-03-12T02:58:17.917Z", createdUserId: "00000000-0000-0000-0000-000000000000", lastUpdatedAt: "2024-03-12T02:58:17.917Z", lastUpdateUserId: "00000000-0000-0000-0000-000000000000", createdFullName: "string", lastUpdateFullName: "สาวิตรี ศรีสมัย", isActive: true, startDate: "2024-03-12T09:57:44.000Z", endDate: "2024-03-12T09:57:44.000Z", numberOrder: "string", topic: "topic", place: "place", dateOrder: "2024-03-12T09:57:44.000Z", department: "department", duration: "string", name: "name", yearly: 0, isDate: true, profileTrainingId: "3cf02fb7-2f0c-471b-b641-51d557375c0a", }, ], }) public async trainingHistory(@Path() trainingId: string) { const record = await this.trainingHistoryRepo.findBy({ profileTrainingId: trainingId, }); return new HttpSuccess(record); } @Post() public async newTraining(@Request() req: RequestWithUser, @Body() body: CreateProfileEmployeeTraining) { 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 ProfileTraining(); 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.trainingRepo.save(data); return new HttpSuccess(); } @Patch("{trainingId}") public async editTraining( @Request() req: RequestWithUser, @Body() body: UpdateProfileTraining, @Path() trainingId: string, ) { const record = await this.trainingRepo.findOneBy({ id: trainingId }); if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); const history = new ProfileTrainingHistory(); Object.assign(history, { ...record, id: undefined }); Object.assign(record, body); history.profileTrainingId = trainingId; record.lastUpdateFullName = req.user.name; history.lastUpdateFullName = req.user.name; await Promise.all([this.trainingRepo.save(record), this.trainingHistoryRepo.save(history)]); return new HttpSuccess(); } @Delete("{trainingId}") public async deleteTraining(@Path() trainingId: string) { await this.trainingHistoryRepo.delete({ profileTrainingId: trainingId, }); const result = await this.trainingRepo.delete({ id: trainingId }); if (result.affected == undefined || result.affected <= 0) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); } return new HttpSuccess(); } }