import { Body, Controller, Delete, Get, Patch, Path, Post, Request, Route, Security, Tags, } from "tsoa"; import { AppDataSource } from "../database/data-source"; import { CreateProfileCertificate, ProfileCertificate, UpdateProfileCertificate, } from "../entities/ProfileCertificate"; import HttpSuccess from "../interfaces/http-success"; import HttpStatus from "../interfaces/http-status"; import HttpError from "../interfaces/http-error"; import { ProfileCertificateHistory } from "../entities/ProfileCertificateHistory"; import { RequestWithUser } from "../middlewares/user"; @Route("api/v1/org/profile/certificate") @Tags("ProfileCertificate") @Security("bearerAuth") export class ProfileCertificateController extends Controller { private certificateRepo = AppDataSource.getRepository(ProfileCertificate); private certificateHistoryRepo = AppDataSource.getRepository(ProfileCertificateHistory); @Get("{profileId}") public async getCertificate(@Path() profileId: string) { const record = await this.certificateRepo.findBy({ profileId }); return new HttpSuccess(record); } @Get("history/{certificateId}") public async certificateHistory(@Path() certificateId: string) { const record = await this.certificateHistoryRepo.findBy({ profileCertificateId: certificateId, }); return new HttpSuccess(record); } @Post() public async newCertificate( @Request() req: RequestWithUser, @Body() body: CreateProfileCertificate, ) { const data = new ProfileCertificate(); const history = new ProfileCertificateHistory(); const meta = { createdUserId: req.user.sub, createdFullName: req.user.name, lastUpdateUserId: req.user.sub, lastUpdateFullName: req.user.name, }; Object.assign(data, { ...body, ...meta }); Object.assign(history, { ...body, ...meta }); const result = await this.certificateRepo.save(data); history.profileCertificateId = result.id; await this.certificateHistoryRepo.save(history); return new HttpSuccess(); } @Patch("{certificateId}") public async editCertificate( @Request() req: RequestWithUser, @Body() body: UpdateProfileCertificate, @Path() certificateId: string, ) { const record = await this.certificateRepo.findOneBy({ id: certificateId }); if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); const history = new ProfileCertificateHistory(); Object.assign(record, body); Object.assign(history, { ...body, id: undefined }); history.profileCertificateId = certificateId; record.lastUpdateFullName = req.user.name; history.lastUpdateFullName = req.user.name; await Promise.all([ this.certificateRepo.save(record), this.certificateHistoryRepo.save(history), ]); return new HttpSuccess(); } @Delete("{certificateId}") public async deleteCertificate(@Path() certificateId: string) { await this.certificateHistoryRepo.delete({ profileCertificateId: certificateId, }); const certificateResult = await this.certificateRepo.delete({ id: certificateId, }); if (certificateResult.affected && certificateResult.affected <= 0) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); } return new HttpSuccess(); } }