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 { ProfileFamilyFather, CreateProfileFamilyFather, UpdateProfileFamilyFather, } from "../entities/ProfileFamilyFather"; import { ProfileFamilyFatherHistory } from "../entities/ProfileFamilyFatherHistory"; import Extension from "../interfaces/extension"; @Route("api/v1/org/profile/family/father") @Tags("ProfileFamilyFather") @Security("bearerAuth") export class ProfileFamilyFatherController extends Controller { private profileRepo = AppDataSource.getRepository(Profile); private ProfileFamilyFather = AppDataSource.getRepository(ProfileFamilyFather); private ProfileFamilyFatherHistory = AppDataSource.getRepository(ProfileFamilyFatherHistory); @Get("user") public async getFamilyFatherUser(@Request() request: { user: Record }) { const profile = await this.profileRepo.findOneBy({ keycloak: request.user.sub }); if (!profile) { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } const familyFather = await this.ProfileFamilyFather.findOne({ select: [ "id", "fatherPrefix", "fatherFirstName", "fatherLastName", "fatherCareer", "fatherCitizenId", "fatherLive", "profileId", ], where: { id: profile.id }, order: { lastUpdatedAt: "DESC" }, }); return new HttpSuccess(familyFather); } @Get("{profileId}") @Example({ status: 200, message: "สำเร็จ", result: { id: "6207ae29-05ef-4abb-9a37-a887265d671e", fatherPrefix: "string", fatherFirstName: "string", fatherLastName: "string", fatherCareer: "string", fatherCitizenId: "string", fatherLive: true, profileId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201", }, }) public async getFamilyFather(@Path() profileId: string) { const profile = await this.profileRepo.findOne({ where: { id: profileId }, }); if (!profile) { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } const familyFather = await this.ProfileFamilyFather.findOne({ select: [ "id", "fatherPrefix", "fatherFirstName", "fatherLastName", "fatherCareer", "fatherCitizenId", "fatherLive", "profileId", ], where: { profileId }, order: { lastUpdatedAt: "DESC" }, }); return new HttpSuccess(familyFather); } @Get("history/{profileId}") @Example({ status: 200, message: "สำเร็จ", result: [ { id: "6207ae29-05ef-4abb-9a37-a887265d671e", createdAt: "2024-03-19T11:00:29.769Z", createdUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0", lastUpdatedAt: "2024-03-19T11:00:29.769Z", lastUpdateUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0", createdFullName: "สาวิตรี ศรีสมัย", lastUpdateFullName: "สาวิตรี ศรีสมัย", fatherPrefix: "string", fatherFirstName: "string", fatherLastName: "string", fatherCareer: "string", fatherCitizenId: "string", fatherLive: true, profileFamilyFatherId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201", profileId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201", }, ], }) public async familyFatherHistory(@Path() profileId: string) { const profile = await this.profileRepo.findOne({ where: { id: profileId }, }); if (!profile) { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } const familyFather = await this.ProfileFamilyFather.find({ relations: ["histories"], order: { lastUpdatedAt: "DESC" }, where: { profileId: profileId }, }); const mapData = familyFather .flatMap((x) => x.histories) .map((y) => ({ id: y.id, createdAt: y.createdAt, createdUserId: y.createdUserId, lastUpdatedAt: y.lastUpdatedAt, lastUpdateUserId: y.lastUpdateUserId, createdFullName: y.createdFullName, lastUpdateFullName: y.lastUpdateFullName, fatherPrefix: y.fatherPrefix, fatherFirstName: y.fatherFirstName, fatherLastName: y.fatherLastName, fatherCareer: y.fatherCareer, fatherCitizenId: y.fatherCitizenId, fatherLive: y.fatherLive, profileFamilyFatherId: y.profileFamilyFatherId, profileId: profileId, })); return new HttpSuccess(mapData); } @Post() public async FamilyFather( @Request() req: RequestWithUser, @Body() body: CreateProfileFamilyFather, ) { const familyFather = Object.assign(new ProfileFamilyFather(), body); if (!familyFather) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); } const profile = await this.profileRepo.findOneBy({ id: body.profileId }); if (!profile) { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } familyFather.fatherCitizenId = Extension.CheckCitizen(String(body.fatherCitizenId)); familyFather.createdUserId = req.user.sub; familyFather.createdFullName = req.user.name; familyFather.lastUpdateUserId = req.user.sub; familyFather.lastUpdateFullName = req.user.name; await this.ProfileFamilyFather.save(familyFather); if (familyFather) { const history: ProfileFamilyFatherHistory = Object.assign(new ProfileFamilyFatherHistory(), { profileFamilyFatherId: familyFather.id, fatherPrefix: familyFather.fatherPrefix, fatherFirstName: familyFather.fatherFirstName, fatherLastName: familyFather.fatherLastName, fatherCareer: familyFather.fatherCareer, fatherCitizenId: familyFather.fatherCitizenId, fatherLive: familyFather.fatherLive, createdUserId: req.user.sub, createdFullName: req.user.name, lastUpdateUserId: req.user.sub, lastUpdateFullName: req.user.name, }); await this.ProfileFamilyFatherHistory.save(history); } return new HttpSuccess(familyFather.id); } @Patch("{profileId}") public async editFamilyFather( @Request() req: RequestWithUser, @Body() body: UpdateProfileFamilyFather, @Path() profileId: string, ) { const familyFather = await this.ProfileFamilyFather.findOneBy({ profileId: profileId }); if (!familyFather) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); const history = new ProfileFamilyFatherHistory(); Object.assign(history, { ...familyFather, id: undefined }); Object.assign(familyFather, body); (familyFather.lastUpdateUserId = req.user.sub), (familyFather.lastUpdateFullName = req.user.name); familyFather.fatherCitizenId = Extension.CheckCitizen(String(body.fatherCitizenId)); history.profileFamilyFatherId = familyFather.id; //profileFamilyFatherId (history.fatherPrefix = familyFather.fatherPrefix), (history.fatherFirstName = familyFather.fatherFirstName), (history.fatherLastName = familyFather.fatherLastName), (history.fatherCareer = familyFather.fatherCareer), (history.fatherCitizenId = familyFather.fatherCitizenId), (history.fatherLive = familyFather.fatherLive), (history.lastUpdateUserId = req.user.sub), (history.lastUpdateFullName = req.user.name); await Promise.all([ this.ProfileFamilyFather.save(familyFather), this.ProfileFamilyFatherHistory.save(history), ]); return new HttpSuccess(); } // @Delete("{profileId}") // public async deleteFamilyFather(@Path() profileId: string) { // const result = await this.ProfileFamilyFather.delete({ profileId: profileId }); // if (result.affected == undefined || result.affected <= 0) { // throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); // } // return new HttpSuccess(); // } }