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 { ProfileFamilyCouple, CreateProfileFamilyCouple, UpdateProfileFamilyCouple, } from "../entities/ProfileFamilyCouple"; import { ProfileFamilyCoupleHistory } from "../entities/ProfileFamilyCoupleHistory"; import Extension from "../interfaces/extension"; @Route("api/v1/org/profile/family/couple") @Tags("ProfileFamilyCouple") @Security("bearerAuth") export class ProfileFamilyCoupleController extends Controller { private profileRepo = AppDataSource.getRepository(Profile); private ProfileFamilyCouple = AppDataSource.getRepository(ProfileFamilyCouple); private ProfileFamilyCoupleHistory = AppDataSource.getRepository(ProfileFamilyCoupleHistory); @Get("user") public async getFamilyCoupleUser(@Request() request: { user: Record }) { const profile = await this.profileRepo.findOneBy({ keycloak: request.user.sub }); if (!profile) { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } const familyCouple = await this.ProfileFamilyCouple.findOne({ select: [ "id", "couplePrefix", "coupleFirstName", "coupleLastName", "coupleLastNameOld", "coupleCareer", "coupleCitizenId", "coupleLive", "relationship", "profileId", ], where: { profileId: profile.id }, order: { lastUpdatedAt: "DESC" }, }); return new HttpSuccess(familyCouple); } @Get("{profileId}") @Example({ status: 200, message: "สำเร็จ", result: { id: "6207ae29-05ef-4abb-9a37-a887265d671e", couplePrefix: "string", coupleFirstName: "string", coupleLastName: "string", coupleLastNameOld: "string", coupleCareer: "string", coupleCitizenId: "string", coupleLive: true, relationship: "string", profileId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201", }, }) public async getFamilyCouple(@Path() profileId: string) { const profile = await this.profileRepo.findOne({ where: { id: profileId }, }); if (!profile) { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } const familyCouple = await this.ProfileFamilyCouple.findOne({ select: [ "id", "couplePrefix", "coupleFirstName", "coupleLastName", "coupleLastNameOld", "coupleCareer", "coupleCitizenId", "coupleLive", "relationship", "profileId", ], where: { profileId }, order: { lastUpdatedAt: "DESC" }, }); return new HttpSuccess(familyCouple); } /** * * @summary ประวัติแก้ไขครอบครัว by keycloak * */ @Get("history/user") public async familyCoupleHistoryUser(@Request() request: RequestWithUser) { const profile = await this.profileRepo.findOneBy({ keycloak: request.user.sub }); if (!profile) { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } const familyCouple = await this.ProfileFamilyCouple.find({ relations: ["histories"], order: { lastUpdatedAt: "DESC" }, where: { profileId: profile.id, }, }); const mapData = familyCouple .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, couplePrefix: y.couplePrefix, coupleFirstName: y.coupleFirstName, coupleLastName: y.coupleLastName, coupleLastNameOld: y.coupleLastNameOld, coupleCareer: y.coupleCareer, coupleCitizenId: y.coupleCitizenId, coupleLive: y.coupleLive, relationship: y.relationship, profileFamilyCoupleId: y.profileFamilyCoupleId, profileId: profile.id, })); return new HttpSuccess(mapData); } @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: "สาวิตรี ศรีสมัย", couplePrefix: "string", coupleFirstName: "string", coupleLastName: "string", coupleLastNameOld: "string", coupleCareer: "string", coupleCitizenId: "string", coupleLive: true, relationship: "string", profileFamilyCoupleId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201", profileId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201", }, ], }) public async familyCoupleHistory(@Path() profileId: string) { const profile = await this.profileRepo.findOne({ where: { id: profileId }, }); if (!profile) { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } const familyCouple = await this.ProfileFamilyCouple.find({ relations: ["histories"], order: { lastUpdatedAt: "DESC" }, where: { profileId: profileId }, }); const mapData = familyCouple .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, couplePrefix: y.couplePrefix, coupleFirstName: y.coupleFirstName, coupleLastName: y.coupleLastName, coupleLastNameOld: y.coupleLastNameOld, coupleCareer: y.coupleCareer, coupleCitizenId: y.coupleCitizenId, coupleLive: y.coupleLive, relationship: y.relationship, profileFamilyCoupleId: y.profileFamilyCoupleId, profileId: profileId, })); return new HttpSuccess(mapData); } @Post() public async FamilyCouple( @Request() req: RequestWithUser, @Body() body: CreateProfileFamilyCouple, ) { const familyCouple = Object.assign(new ProfileFamilyCouple(), body); if (!familyCouple) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); } const profile = await this.profileRepo.findOneBy({ id: body.profileId }); if (!profile) { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } familyCouple.coupleCitizenId = Extension.CheckCitizen(String(body.coupleCitizenId)); familyCouple.createdUserId = req.user.sub; familyCouple.createdFullName = req.user.name; familyCouple.lastUpdateUserId = req.user.sub; familyCouple.lastUpdateFullName = req.user.name; await this.ProfileFamilyCouple.save(familyCouple); if (familyCouple) { profile.relationship = familyCouple.relationship; //update profile.relationship const history: ProfileFamilyCoupleHistory = Object.assign(new ProfileFamilyCoupleHistory(), { profileFamilyCoupleId: familyCouple.id, couplePrefix: familyCouple.couplePrefix, coupleFirstName: familyCouple.coupleFirstName, coupleLastName: familyCouple.coupleLastName, coupleLastNameOld: familyCouple.coupleLastNameOld, coupleCareer: familyCouple.coupleCareer, coupleCitizenId: familyCouple.coupleCitizenId, coupleLive: familyCouple.coupleLive, relationship: familyCouple.relationship, createdUserId: req.user.sub, createdFullName: req.user.name, lastUpdateUserId: req.user.sub, lastUpdateFullName: req.user.name, }); await Promise.all([ this.profileRepo.save(profile), this.ProfileFamilyCoupleHistory.save(history), ]); } return new HttpSuccess(familyCouple.id); } @Patch("{profileId}") public async editFamilyCouple( @Request() req: RequestWithUser, @Body() body: UpdateProfileFamilyCouple, @Path() profileId: string, ) { const familyCouple = await this.ProfileFamilyCouple.findOneBy({ profileId: profileId }); if (!familyCouple) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); const history = new ProfileFamilyCoupleHistory(); Object.assign(history, { ...familyCouple, id: undefined }); Object.assign(familyCouple, body); (familyCouple.lastUpdateUserId = req.user.sub), (familyCouple.lastUpdateFullName = req.user.name); familyCouple.coupleCitizenId = Extension.CheckCitizen(String(body.coupleCitizenId)); history.profileFamilyCoupleId = familyCouple.id; (history.couplePrefix = familyCouple.couplePrefix), (history.coupleFirstName = familyCouple.coupleFirstName), (history.coupleLastName = familyCouple.coupleLastName), (history.coupleLastNameOld = familyCouple.coupleLastNameOld), (history.coupleCareer = familyCouple.coupleCareer), (history.coupleCitizenId = familyCouple.coupleCitizenId), (history.coupleLive = familyCouple.coupleLive), (history.relationship = familyCouple.relationship), (history.lastUpdateUserId = req.user.sub), (history.lastUpdateFullName = req.user.name); await Promise.all([ this.ProfileFamilyCouple.save(familyCouple), this.ProfileFamilyCoupleHistory.save(history), ]); return new HttpSuccess(); } }