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 { ProfileDisciplineHistory } from "../entities/ProfileDisciplineHistory"; import { RequestWithUser } from "../middlewares/user"; import { Profile } from "../entities/Profile"; import { CreateProfileDiscipline, ProfileDiscipline, UpdateProfileDiscipline, } from "../entities/ProfileDiscipline"; @Route("api/v1/org/profile/discipline") @Tags("ProfileDiscipline") @Security("bearerAuth") export class ProfileDisciplineController extends Controller { private profileRepository = AppDataSource.getRepository(Profile); private disciplineRepository = AppDataSource.getRepository(ProfileDiscipline); private disciplineHistoryRepository = AppDataSource.getRepository(ProfileDisciplineHistory); @Get("user") public async getDisciplineUser(@Request() request: { user: Record }) { const profile = await this.profileRepository.findOneBy({ keycloak: request.user.sub }); if (!profile) { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } const lists = await this.disciplineRepository.find({ where: { profileId: profile.id }, select: [ "id", "date", "level", "detail", "unStigma", "refCommandNo", "refCommandDate", "lastUpdateFullName", "lastUpdatedAt", ], }); return new HttpSuccess(lists); } @Get("{profileId}") public async getDiscipline(@Path() profileId: string) { const lists = await this.disciplineRepository.find({ where: { profileId: profileId }, select: [ "id", "date", "level", "detail", "unStigma", "refCommandNo", "refCommandDate", "lastUpdateFullName", "lastUpdatedAt", ], }); return new HttpSuccess(lists); } @Get("history/{disciplineId}") @Example({ status: 200, message: "สำเร็จ", result: [ { id: "debfa8a7-83fb-4801-a940-8ae74e7638d3", date: "2024-03-12T10:09:47.000Z", level: "string", detail: "string", unStigma: "string", refCommandNo: "string", refCommandDate: "2024-03-12T10:09:47.000Z", }, { id: "ba0e2f82-014e-46c6-8b82-a7c28eb5325f", date: "2024-03-12T10:09:47.000Z", level: "string", detail: "string", unStigma: "string", refCommandNo: "string", refCommandDate: "2024-03-12T10:09:47.000Z", }, ], }) public async disciplineHistory(@Path() disciplineId: string) { const record = await this.disciplineHistoryRepository.find({ where: { profileDisciplineId: disciplineId }, select: [ "id", "date", "level", "detail", "unStigma", "refCommandNo", "refCommandDate", "lastUpdateFullName", "lastUpdatedAt", ], order: { createdAt: "DESC" }, }); return new HttpSuccess(record); } @Post() public async newDiscipline( @Request() req: RequestWithUser, @Body() body: CreateProfileDiscipline, ) { if (!body.profileId) { throw new HttpError(HttpStatus.BAD_REQUEST, "กรุณากรอก profileId"); } const profile = await this.profileRepository.findOneBy({ id: body.profileId }); if (!profile) { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } const data = new ProfileDiscipline(); 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.disciplineRepository.save(data); return new HttpSuccess(); } @Patch("{disciplineId}") public async editDiscipline( @Request() req: RequestWithUser, @Body() body: UpdateProfileDiscipline, @Path() disciplineId: string, ) { const record = await this.disciplineRepository.findOneBy({ id: disciplineId }); if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); const history = new ProfileDisciplineHistory(); Object.assign(history, { ...record, id: undefined }); Object.assign(record, body); history.profileDisciplineId = disciplineId; record.lastUpdateFullName = req.user.name; history.lastUpdateFullName = req.user.name; await Promise.all([ this.disciplineRepository.save(record), this.disciplineHistoryRepository.save(history), ]); return new HttpSuccess(); } @Delete("{disciplineId}") public async deleteDiscipline(@Path() disciplineId: string) { await this.disciplineHistoryRepository.delete({ profileDisciplineId: disciplineId, }); const result = await this.disciplineRepository.delete({ id: disciplineId }); if (result.affected == undefined || result.affected <= 0) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); } return new HttpSuccess(); } }