From 0b69570738cfba21d3a1feaed52db0e534a66d80 Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Wed, 20 Mar 2024 16:24:36 +0700 Subject: [PATCH] feat: profile gov endpoints (incomplete) --- .../ProfileGovernmentController.ts | 126 ++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 src/controllers/ProfileGovernmentController.ts diff --git a/src/controllers/ProfileGovernmentController.ts b/src/controllers/ProfileGovernmentController.ts new file mode 100644 index 00000000..ff40422c --- /dev/null +++ b/src/controllers/ProfileGovernmentController.ts @@ -0,0 +1,126 @@ +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 { RequestWithUser } from "../middlewares/user"; +import { Profile } from "../entities/Profile"; +import { + CreateProfileGovernment, + ProfileGovernment, + UpdateProfileGovernment, +} from "../entities/ProfileGovernment"; + +@Route("api/v1/org/profile/familyHistory") +@Tags("ProfileFamilyHistory") +@Security("bearerAuth") +export class ProfileFamilyHistoryController extends Controller { + private profileRepo = AppDataSource.getRepository(Profile); + private govRepo = AppDataSource.getRepository(ProfileGovernment); + + @Get("{profileId}") + @Example({}) + public async getGovHistory(@Path() profileId: string) { + const record = await this.govRepo.find({ + take: 1, + order: { createdAt: "DESC" }, + where: { profileId }, + }); + return new HttpSuccess({ ...record[0] }); + } + + @Get("history/{profileId}") + @Example({}) + public async govHistory(@Path() profileId: string) { + const record = await this.govRepo.find({ + order: { lastUpdatedAt: "DESC" }, + where: { profileId }, + }); + + record.pop(); + + return new HttpSuccess(record); + } + + @Post() + public async newFamilyHistory( + @Request() req: RequestWithUser, + @Body() body: CreateProfileGovernment, + ) { + if (!body.profileId) { + throw new HttpError(HttpStatus.BAD_REQUEST, "กรุณากรอก profileId"); + } + + const profile = await this.profileRepo.findOneBy({ id: body.profileId }); + + if (!profile) { + throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); + } + + const data = new ProfileGovernment(); + + 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.govRepo.save(data); + + return new HttpSuccess(); + } + + @Patch("{profileId}") + public async editFamilyHistory( + @Request() req: RequestWithUser, + @Body() body: UpdateProfileGovernment, + @Path() profileId: string, + ) { + const record = ( + await this.govRepo.find({ + take: 1, + order: { lastUpdatedAt: "DESC" }, + where: { profileId }, + }) + )[0]; + + if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + + const historyData = new ProfileGovernment(); + + Object.assign(historyData, { ...record, ...body, id: undefined }); + historyData.lastUpdateFullName = req.user.name; + historyData.lastUpdateFullName = req.user.name; + + await this.govRepo.save(historyData); + + return new HttpSuccess(); + } + + @Delete("{profileId}") + public async deleteGovHistory(@Path() profileId: string) { + const result = await this.govRepo.delete({ profileId: profileId }); + + if (result.affected && result.affected <= 0) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + } + + return new HttpSuccess(); + } +}