From 702ed615ea010ac7cddbffc5699ba174316c672b Mon Sep 17 00:00:00 2001 From: Bright Date: Wed, 15 May 2024 15:41:51 +0700 Subject: [PATCH 1/3] Family Controller --- .../ProfileFamilyCoupleController.ts | 207 ++++++++++++++++++ .../ProfileFamilyCoupleEmployeeController.ts | 207 ++++++++++++++++++ .../ProfileFamilyFatherController.ts | 206 +++++++++++++++++ .../ProfileFamilyFatherEmployeeController.ts | 196 +++++++++++++++++ .../ProfileFamilyMotherController.ts | 196 +++++++++++++++++ .../ProfileFamilyMotherEmployeeController.ts | 196 +++++++++++++++++ 6 files changed, 1208 insertions(+) create mode 100644 src/controllers/ProfileFamilyCoupleController.ts create mode 100644 src/controllers/ProfileFamilyCoupleEmployeeController.ts create mode 100644 src/controllers/ProfileFamilyFatherController.ts create mode 100644 src/controllers/ProfileFamilyFatherEmployeeController.ts create mode 100644 src/controllers/ProfileFamilyMotherController.ts create mode 100644 src/controllers/ProfileFamilyMotherEmployeeController.ts diff --git a/src/controllers/ProfileFamilyCoupleController.ts b/src/controllers/ProfileFamilyCoupleController.ts new file mode 100644 index 00000000..e79a5977 --- /dev/null +++ b/src/controllers/ProfileFamilyCoupleController.ts @@ -0,0 +1,207 @@ +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"; + +@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("{profileId}") + @Example({ + status: 200, + message: "สำเร็จ", + result: { + id: "6207ae29-05ef-4abb-9a37-a887265d671e", + couple: true, + 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", "couple", "couplePrefix", "coupleFirstName", "coupleLastName", "coupleLastNameOld", + "coupleCareer", "coupleCitizenId", "coupleLive", "relationship", "profileId", + ], + where: { profileId }, + }); + + return new HttpSuccess(familyCouple); + } + + @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: "สาวิตรี ศรีสมัย", + couple: true, + 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, + couple: y.couple, + couplePrefix: y.couplePrefix, + coupleFirstName: y.coupleFirstName, + coupleLastName: y.coupleLastName, + coupleLastNameOld: y.coupleLastNameOld, + coupleCareer: y.coupleCareer, + 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.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) { + const history: ProfileFamilyCoupleHistory = Object.assign(new ProfileFamilyCoupleHistory(), { + profileFamilyCoupleId: familyCouple.id, + couple: familyCouple.couple, + couplePrefix: familyCouple.couplePrefix, + coupleFirstName: familyCouple.coupleFirstName, + coupleLastName: familyCouple.coupleLastName, + coupleLastNameOld: familyCouple.coupleLastNameOld, + coupleCareer: familyCouple.coupleCareer, + coupleLive: familyCouple.coupleLive, + relationship: familyCouple.relationship, + createdUserId: req.user.sub, + createdFullName: req.user.name, + lastUpdateUserId: req.user.sub, + lastUpdateFullName: req.user.name, + }); + await 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; + history.profileFamilyCoupleId = familyCouple.id; + history.couple = familyCouple.couple, + history.couplePrefix = familyCouple.couplePrefix, + history.coupleFirstName = familyCouple.coupleFirstName, + history.coupleLastName = familyCouple.coupleLastName, + history.coupleLastNameOld = familyCouple.coupleLastNameOld, + history.coupleCareer = familyCouple.coupleCareer, + 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(); + } + +} diff --git a/src/controllers/ProfileFamilyCoupleEmployeeController.ts b/src/controllers/ProfileFamilyCoupleEmployeeController.ts new file mode 100644 index 00000000..8102a6fd --- /dev/null +++ b/src/controllers/ProfileFamilyCoupleEmployeeController.ts @@ -0,0 +1,207 @@ +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 { ProfileEmployee } from "../entities/ProfileEmployee"; +import { ProfileFamilyCouple, CreateProfileEmployeeFamilyCouple, UpdateProfileFamilyCouple } from "../entities/ProfileFamilyCouple"; +import { ProfileFamilyCoupleHistory } from "../entities/ProfileFamilyCoupleHistory"; + +@Route("api/v1/org/profile-employee/family/couple") +@Tags("ProfileEmployeeFamilyCouple") +@Security("bearerAuth") +export class ProfileFamilyCoupleEmployeeController extends Controller { + private profileRepo = AppDataSource.getRepository(ProfileEmployee); + private ProfileFamilyCouple = AppDataSource.getRepository(ProfileFamilyCouple); + private ProfileFamilyCoupleHistory = AppDataSource.getRepository(ProfileFamilyCoupleHistory); + + @Get("{profileEmployeeId}") + @Example({ + status: 200, + message: "สำเร็จ", + result: { + id: "6207ae29-05ef-4abb-9a37-a887265d671e", + couple: true, + couplePrefix: "string", + coupleFirstName: "string", + coupleLastName: "string", + coupleLastNameOld: "string", + coupleCareer: "string", + coupleCitizenId: "string", + coupleLive: true, + relationship: "string", + profileEmployeeId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201", + }, + }) + public async getFamilyCouple(@Path() profileEmployeeId: string) { + const profile = await this.profileRepo.findOne({ + where: { id: profileEmployeeId } + }) + + if (!profile) { + throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); + } + const familyCouple = await this.ProfileFamilyCouple.findOne({ + select: [ + "id", "couple", "couplePrefix", "coupleFirstName", "coupleLastName", "coupleLastNameOld", + "coupleCareer", "coupleCitizenId", "coupleLive", "relationship", "profileEmployeeId", + ], + where: { profileEmployeeId }, + }); + + return new HttpSuccess(familyCouple); + } + + @Get("history/{profileEmployeeId}") + @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: "สาวิตรี ศรีสมัย", + couple: true, + couplePrefix: "string", + coupleFirstName: "string", + coupleLastName: "string", + coupleLastNameOld: "string", + coupleCareer: "string", + coupleCitizenId: "string", + coupleLive: true, + relationship: "string", + profileFamilyCoupleId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201", + profileEmployeeId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201", + } + ], + }) + public async familyCoupleHistory(@Path() profileEmployeeId: string) { + const profile = await this.profileRepo.findOne({ + where: { id: profileEmployeeId } + }) + if (!profile) { + throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); + } + + const familyCouple = await this.ProfileFamilyCouple.find({ + relations: ["histories"], + order: { lastUpdatedAt: "DESC" }, + where: { profileEmployeeId: profileEmployeeId}, + }); + + 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, + couple: y.couple, + couplePrefix: y.couplePrefix, + coupleFirstName: y.coupleFirstName, + coupleLastName: y.coupleLastName, + coupleLastNameOld: y.coupleLastNameOld, + coupleCareer: y.coupleCareer, + coupleLive: y.coupleLive, + relationship: y.relationship, + profileFamilyCoupleId: y.profileFamilyCoupleId, + profileEmployeeId: profileEmployeeId, + })); + + return new HttpSuccess(mapData); + } + + @Post() + public async FamilyCouple( + @Request() req: RequestWithUser, + @Body() body: CreateProfileEmployeeFamilyCouple, + ) { + const familyCouple = Object.assign(new ProfileFamilyCouple(), body); + if (!familyCouple) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + } + const profile = await this.profileRepo.findOneBy({ id: body.profileEmployeeId }); + if (!profile) { + throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); + } + 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) { + const history: ProfileFamilyCoupleHistory = Object.assign(new ProfileFamilyCoupleHistory(), { + profileFamilyCoupleId: familyCouple.id, + couple: familyCouple.couple, + couplePrefix: familyCouple.couplePrefix, + coupleFirstName: familyCouple.coupleFirstName, + coupleLastName: familyCouple.coupleLastName, + coupleLastNameOld: familyCouple.coupleLastNameOld, + coupleCareer: familyCouple.coupleCareer, + coupleLive: familyCouple.coupleLive, + relationship: familyCouple.relationship, + createdUserId: req.user.sub, + createdFullName: req.user.name, + lastUpdateUserId: req.user.sub, + lastUpdateFullName: req.user.name, + }); + await this.ProfileFamilyCoupleHistory.save(history); + } + return new HttpSuccess(familyCouple.id); + } + + @Patch("{profileEmployeeId}") + public async editFamilyCouple( + @Request() req: RequestWithUser, + @Body() body: UpdateProfileFamilyCouple, + @Path() profileEmployeeId: string, + ) { + const familyCouple = await this.ProfileFamilyCouple.findOneBy({ profileEmployeeId: profileEmployeeId }); + 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; + history.profileFamilyCoupleId = familyCouple.id; + history.couple = familyCouple.couple, + history.couplePrefix = familyCouple.couplePrefix, + history.coupleFirstName = familyCouple.coupleFirstName, + history.coupleLastName = familyCouple.coupleLastName, + history.coupleLastNameOld = familyCouple.coupleLastNameOld, + history.coupleCareer = familyCouple.coupleCareer, + 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(); + } + +} diff --git a/src/controllers/ProfileFamilyFatherController.ts b/src/controllers/ProfileFamilyFatherController.ts new file mode 100644 index 00000000..4a55103a --- /dev/null +++ b/src/controllers/ProfileFamilyFatherController.ts @@ -0,0 +1,206 @@ +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"; + +@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("{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 }, + }); + + 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.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; + + 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(); + // } +} diff --git a/src/controllers/ProfileFamilyFatherEmployeeController.ts b/src/controllers/ProfileFamilyFatherEmployeeController.ts new file mode 100644 index 00000000..d494974a --- /dev/null +++ b/src/controllers/ProfileFamilyFatherEmployeeController.ts @@ -0,0 +1,196 @@ +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 { ProfileEmployee } from "../entities/ProfileEmployee"; +import { ProfileFamilyFather, CreateProfileEmployeeFamilyFather, UpdateProfileFamilyFather } from "../entities/ProfileFamilyFather"; +import { ProfileFamilyFatherHistory } from "../entities/ProfileFamilyFatherHistory"; + +@Route("api/v1/org/profile-employee/family/father") +@Tags("ProfileEmployeeFamilyFather") +@Security("bearerAuth") +export class ProfileFamilyFatherEmployeeController extends Controller { + private profileRepo = AppDataSource.getRepository(ProfileEmployee); + private ProfileFamilyFather = AppDataSource.getRepository(ProfileFamilyFather); + private ProfileFamilyFatherHistory = AppDataSource.getRepository(ProfileFamilyFatherHistory); + + @Get("{profileEmployeeId}") + @Example({ + status: 200, + message: "สำเร็จ", + result: { + id: "6207ae29-05ef-4abb-9a37-a887265d671e", + fatherPrefix: "string", + fatherFirstName: "string", + fatherLastName: "string", + fatherCareer: "string", + fatherCitizenId: "string", + fatherLive: true, + profileEmployeeId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201", + }, + }) + public async getFamilyFather(@Path() profileEmployeeId: string) { + const profile = await this.profileRepo.findOne({ + where: { id: profileEmployeeId } + }) + + if (!profile) { + throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); + } + const familyFather = await this.ProfileFamilyFather.findOne({ + select: [ + "id", "fatherPrefix", "fatherFirstName", "fatherLastName", + "fatherCareer", "fatherCitizenId", "fatherLive", "profileEmployeeId", + ], + where: { profileEmployeeId }, + }); + + return new HttpSuccess(familyFather); + } + + @Get("history/{profileEmployeeId}") + @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", + profileEmployeeId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201", + } + ], + }) + public async familyFatherHistory(@Path() profileEmployeeId: string) { + const profile = await this.profileRepo.findOne({ + where: { id: profileEmployeeId } + }) + if (!profile) { + throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); + } + + const familyFather = await this.ProfileFamilyFather.find({ + relations: ["histories"], + order: { lastUpdatedAt: "DESC" }, + where: { profileEmployeeId: profileEmployeeId}, + }); + + 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, + profileEmployeeId: profileEmployeeId, + })); + + return new HttpSuccess(mapData); + } + + @Post() + public async FamilyFather( + @Request() req: RequestWithUser, + @Body() body: CreateProfileEmployeeFamilyFather, + ) { + const familyFather = Object.assign(new ProfileFamilyFather(), body); + if (!familyFather) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + } + const profile = await this.profileRepo.findOneBy({ id: body.profileEmployeeId }); + if (!profile) { + throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); + } + 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("{profileEmployeeId}") + public async editFamilyFather( + @Request() req: RequestWithUser, + @Body() body: UpdateProfileFamilyFather, + @Path() profileEmployeeId: string, + ) { + const familyFather = await this.ProfileFamilyFather.findOneBy({ profileEmployeeId: profileEmployeeId }); + 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; + + history.profileFamilyFatherId = familyFather.id; + 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(); + } + +} diff --git a/src/controllers/ProfileFamilyMotherController.ts b/src/controllers/ProfileFamilyMotherController.ts new file mode 100644 index 00000000..c2e60642 --- /dev/null +++ b/src/controllers/ProfileFamilyMotherController.ts @@ -0,0 +1,196 @@ +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 { ProfileFamilyMother, CreateProfileFamilyMother, UpdateProfileFamilyMother } from "../entities/ProfileFamilyMother"; +import { ProfileFamilyMotherHistory } from "../entities/ProfileFamilyMotherHistory"; + +@Route("api/v1/org/profile/family/mother") +@Tags("ProfileFamilyMother") +@Security("bearerAuth") +export class ProfileFamilyMotherController extends Controller { + private profileRepo = AppDataSource.getRepository(Profile); + private ProfileFamilyMother = AppDataSource.getRepository(ProfileFamilyMother); + private ProfileFamilyMotherHistory = AppDataSource.getRepository(ProfileFamilyMotherHistory); + + @Get("{profileId}") + @Example({ + status: 200, + message: "สำเร็จ", + result: { + id: "6207ae29-05ef-4abb-9a37-a887265d671e", + motherPrefix: "string", + motherFirstName: "string", + motherLastName: "string", + motherCareer: "string", + motherCitizenId: "string", + motherLive: true, + profileId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201", + }, + }) + public async getFamilyMother(@Path() profileId: string) { + const profile = await this.profileRepo.findOne({ + where: { id: profileId } + }) + + if (!profile) { + throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); + } + const familyMother = await this.ProfileFamilyMother.findOne({ + select: [ + "id", "motherPrefix", "motherFirstName", "motherLastName", + "motherCareer", "motherCitizenId", "motherLive", "profileId", + ], + where: { profileId }, + }); + + return new HttpSuccess(familyMother); + } + + @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: "สาวิตรี ศรีสมัย", + motherPrefix: "string", + motherFirstName: "string", + motherLastName: "string", + motherCareer: "string", + motherCitizenId: "string", + motherLive: true, + profileFamilyMotherId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201", + profileId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201", + } + ], + }) + public async familyMotherHistory(@Path() profileId: string) { + const profile = await this.profileRepo.findOne({ + where: { id: profileId } + }) + if (!profile) { + throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); + } + + const familyMother = await this.ProfileFamilyMother.find({ + relations: ["histories"], + order: { lastUpdatedAt: "DESC" }, + where: { profileId: profileId}, + }); + + const mapData = familyMother.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, + motherPrefix: y.motherPrefix, + motherFirstName: y.motherFirstName, + motherLastName: y.motherLastName, + motherCareer: y.motherCareer, + motherCitizenId: y.motherCitizenId, + motherLive: y.motherLive, + profileFamilyMotherId: y.profileFamilyMotherId, + profileId: profileId, + })); + + return new HttpSuccess(mapData); + } + + @Post() + public async FamilyMother( + @Request() req: RequestWithUser, + @Body() body: CreateProfileFamilyMother, + ) { + const familyMother = Object.assign(new ProfileFamilyMother(), body); + if (!familyMother) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + } + const profile = await this.profileRepo.findOneBy({ id: body.profileId }); + if (!profile) { + throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); + } + familyMother.createdUserId = req.user.sub; + familyMother.createdFullName = req.user.name; + familyMother.lastUpdateUserId = req.user.sub; + familyMother.lastUpdateFullName = req.user.name; + await this.ProfileFamilyMother.save(familyMother); + + if (familyMother) { + const history: ProfileFamilyMotherHistory = Object.assign(new ProfileFamilyMotherHistory(), { + profileFamilyMotherId: familyMother.id, + motherPrefix: familyMother.motherPrefix, + motherFirstName: familyMother.motherFirstName, + motherLastName: familyMother.motherLastName, + motherCareer: familyMother.motherCareer, + motherCitizenId: familyMother.motherCitizenId, + motherLive: familyMother.motherLive, + createdUserId: req.user.sub, + createdFullName: req.user.name, + lastUpdateUserId: req.user.sub, + lastUpdateFullName: req.user.name, + }); + await this.ProfileFamilyMotherHistory.save(history); + } + return new HttpSuccess(familyMother.id); + } + + @Patch("{profileId}") + public async editFamilyMother( + @Request() req: RequestWithUser, + @Body() body: UpdateProfileFamilyMother, + @Path() profileId: string, + ) { + const familyMother = await this.ProfileFamilyMother.findOneBy({ profileId: profileId }); + if (!familyMother) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + + const history = new ProfileFamilyMotherHistory(); + Object.assign(history, { ...familyMother, id: undefined }); + Object.assign(familyMother, body); + familyMother.lastUpdateUserId = req.user.sub, + familyMother.lastUpdateFullName = req.user.name; + + history.profileFamilyMotherId = familyMother.id; + history.motherPrefix = familyMother.motherPrefix, + history.motherFirstName = familyMother.motherFirstName, + history.motherLastName = familyMother.motherLastName, + history.motherCareer = familyMother.motherCareer, + history.motherCitizenId = familyMother.motherCitizenId, + history.motherLive = familyMother.motherLive, + history.lastUpdateUserId = req.user.sub, + history.lastUpdateFullName = req.user.name; + + await Promise.all([ + this.ProfileFamilyMother.save(familyMother), + this.ProfileFamilyMotherHistory.save(history), + ]); + + return new HttpSuccess(); + } + +} diff --git a/src/controllers/ProfileFamilyMotherEmployeeController.ts b/src/controllers/ProfileFamilyMotherEmployeeController.ts new file mode 100644 index 00000000..28889e7e --- /dev/null +++ b/src/controllers/ProfileFamilyMotherEmployeeController.ts @@ -0,0 +1,196 @@ +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 { ProfileEmployee } from "../entities/ProfileEmployee"; +import { ProfileFamilyMother, CreateProfileEmployeeFamilyMother, UpdateProfileFamilyMother } from "../entities/ProfileFamilyMother"; +import { ProfileFamilyMotherHistory } from "../entities/ProfileFamilyMotherHistory"; + +@Route("api/v1/org/profile-employee/family/mother") +@Tags("ProfileEmployeeFamilyMother") +@Security("bearerAuth") +export class ProfileFamilyMotherEmployeeController extends Controller { + private profileRepo = AppDataSource.getRepository(ProfileEmployee); + private ProfileFamilyMother = AppDataSource.getRepository(ProfileFamilyMother); + private ProfileFamilyMotherHistory = AppDataSource.getRepository(ProfileFamilyMotherHistory); + + @Get("{profileEmployeeId}") + @Example({ + status: 200, + message: "สำเร็จ", + result: { + id: "6207ae29-05ef-4abb-9a37-a887265d671e", + motherPrefix: "string", + motherFirstName: "string", + motherLastName: "string", + motherCareer: "string", + motherCitizenId: "string", + motherLive: true, + profileEmployeeId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201", + }, + }) + public async getFamilyMother(@Path() profileEmployeeId: string) { + const profile = await this.profileRepo.findOne({ + where: { id: profileEmployeeId } + }) + + if (!profile) { + throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); + } + const familyMother = await this.ProfileFamilyMother.findOne({ + select: [ + "id", "motherPrefix", "motherFirstName", "motherLastName", + "motherCareer", "motherCitizenId", "motherLive", "profileEmployeeId", + ], + where: { profileEmployeeId }, + }); + + return new HttpSuccess(familyMother); + } + + @Get("history/{profileEmployeeId}") + @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: "สาวิตรี ศรีสมัย", + motherPrefix: "string", + motherFirstName: "string", + motherLastName: "string", + motherCareer: "string", + motherCitizenId: "string", + motherLive: true, + profileFamilyMotherId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201", + profileEmployeeId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201", + } + ], + }) + public async familyMotherHistory(@Path() profileEmployeeId: string) { + const profile = await this.profileRepo.findOne({ + where: { id: profileEmployeeId } + }) + if (!profile) { + throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); + } + + const familyMother = await this.ProfileFamilyMother.find({ + relations: ["histories"], + order: { lastUpdatedAt: "DESC" }, + where: { profileEmployeeId: profileEmployeeId}, + }); + + const mapData = familyMother.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, + motherPrefix: y.motherPrefix, + motherFirstName: y.motherFirstName, + motherLastName: y.motherLastName, + motherCareer: y.motherCareer, + motherCitizenId: y.motherCitizenId, + motherLive: y.motherLive, + profileFamilyMotherId: y.profileFamilyMotherId, + profileEmployeeId: profileEmployeeId, + })); + + return new HttpSuccess(mapData); + } + + @Post() + public async FamilyMother( + @Request() req: RequestWithUser, + @Body() body: CreateProfileEmployeeFamilyMother, + ) { + const familyMother = Object.assign(new ProfileFamilyMother(), body); + if (!familyMother) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + } + const profile = await this.profileRepo.findOneBy({ id: body.profileEmployeeId }); + if (!profile) { + throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); + } + familyMother.createdUserId = req.user.sub; + familyMother.createdFullName = req.user.name; + familyMother.lastUpdateUserId = req.user.sub; + familyMother.lastUpdateFullName = req.user.name; + await this.ProfileFamilyMother.save(familyMother); + + if (familyMother) { + const history: ProfileFamilyMotherHistory = Object.assign(new ProfileFamilyMotherHistory(), { + profileFamilyMotherId: familyMother.id, + motherPrefix: familyMother.motherPrefix, + motherFirstName: familyMother.motherFirstName, + motherLastName: familyMother.motherLastName, + motherCareer: familyMother.motherCareer, + motherCitizenId: familyMother.motherCitizenId, + motherLive: familyMother.motherLive, + createdUserId: req.user.sub, + createdFullName: req.user.name, + lastUpdateUserId: req.user.sub, + lastUpdateFullName: req.user.name, + }); + await this.ProfileFamilyMotherHistory.save(history); + } + return new HttpSuccess(familyMother.id); + } + + @Patch("{profileEmployeeId}") + public async editFamilyMother( + @Request() req: RequestWithUser, + @Body() body: UpdateProfileFamilyMother, + @Path() profileEmployeeId: string, + ) { + const familyMother = await this.ProfileFamilyMother.findOneBy({ profileEmployeeId: profileEmployeeId }); + if (!familyMother) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + + const history = new ProfileFamilyMotherHistory(); + Object.assign(history, { ...familyMother, id: undefined }); + Object.assign(familyMother, body); + familyMother.lastUpdateUserId = req.user.sub, + familyMother.lastUpdateFullName = req.user.name; + + history.profileFamilyMotherId = familyMother.id; + history.motherPrefix = familyMother.motherPrefix, + history.motherFirstName = familyMother.motherFirstName, + history.motherLastName = familyMother.motherLastName, + history.motherCareer = familyMother.motherCareer, + history.motherCitizenId = familyMother.motherCitizenId, + history.motherLive = familyMother.motherLive, + history.lastUpdateUserId = req.user.sub, + history.lastUpdateFullName = req.user.name; + + await Promise.all([ + this.ProfileFamilyMother.save(familyMother), + this.ProfileFamilyMotherHistory.save(history), + ]); + + return new HttpSuccess(); + } + +} From 277805791d8b62b373d62507c34d8c6afe59db22 Mon Sep 17 00:00:00 2001 From: Kittapath Date: Wed, 15 May 2024 16:09:25 +0700 Subject: [PATCH 2/3] no message --- src/controllers/ProfileAvatarController.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/controllers/ProfileAvatarController.ts b/src/controllers/ProfileAvatarController.ts index 70d5d99a..85b9d70e 100644 --- a/src/controllers/ProfileAvatarController.ts +++ b/src/controllers/ProfileAvatarController.ts @@ -22,6 +22,21 @@ export class ProfileAvatarController extends Controller { return new HttpSuccess(lists); } + @Get("select/{profileId}/{id}") + public async selectAvatar(@Path() profileId: string, @Path() id: string) { + const result = await this.avatarRepository.findOneBy({ id: id }); + if (!result) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + } + const profile = await this.profileRepository.findOneBy({ id: profileId }); + if (!profile) { + throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); + } + profile.avatar = result.avatar; + profile.avatarName = result.avatarName; + return new HttpSuccess(); + } + @Post() public async newAvatar(@Request() req: RequestWithUser, @Body() body: CreateProfileAvatar) { const profile = await this.profileRepository.findOneBy({ id: body.profileId }); From 4fa11d2aea98152bb3fa5b676f4cb37e9a767093 Mon Sep 17 00:00:00 2001 From: Bright Date: Wed, 15 May 2024 18:02:16 +0700 Subject: [PATCH 3/3] =?UTF-8?q?=E0=B8=9B=E0=B8=A3=E0=B8=B1=E0=B8=9A=20chil?= =?UTF-8?q?dren?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/ProfileChildrenController.ts | 36 ++++++++++++----- .../ProfileChildrenEmployeeController.ts | 39 ++++++++++++++----- .../ProfileFamilyCoupleController.ts | 6 +-- src/entities/ProfileChildren.ts | 15 ++++--- src/entities/ProfileChildrenHistory.ts | 8 +++- 5 files changed, 75 insertions(+), 29 deletions(-) diff --git a/src/controllers/ProfileChildrenController.ts b/src/controllers/ProfileChildrenController.ts index 32c0be05..eacb151d 100644 --- a/src/controllers/ProfileChildrenController.ts +++ b/src/controllers/ProfileChildrenController.ts @@ -25,7 +25,7 @@ import { UpdateProfileChildren, } from "../entities/ProfileChildren"; -@Route("api/v1/org/profile/children") +@Route("api/v1/org/profile/family/children") @Tags("ProfileChildren") @Security("bearerAuth") export class ProfileChildrenController extends Controller { @@ -59,7 +59,6 @@ export class ProfileChildrenController extends Controller { } const data = new ProfileChildren(); - const meta = { createdUserId: req.user.sub, createdFullName: req.user.name, @@ -68,9 +67,23 @@ export class ProfileChildrenController extends Controller { }; Object.assign(data, { ...body, ...meta }); - await this.childrenRepository.save(data); - + if(data){ + const history: ProfileChildrenHistory = Object.assign(new ProfileChildrenHistory(), { + profileChildrenId: data.id, + childrenCareer: data.childrenCareer, + childrenFirstName: data.childrenFirstName, + childrenLastName: data.childrenLastName, + childrenPrefix: data.childrenPrefix, + childrenLive: data.childrenLive, + childrenCitizenId: data.childrenCitizenId, + createdUserId: req.user.sub, + createdFullName: req.user.name, + lastUpdateUserId: req.user.sub, + lastUpdateFullName: req.user.name, + }); + await this.childrenHistoryRepository.save(history); + } return new HttpSuccess(); } @@ -81,17 +94,22 @@ export class ProfileChildrenController extends Controller { @Path() childrenId: string, ) { const record = await this.childrenRepository.findOneBy({ id: childrenId }); - if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); const history = new ProfileChildrenHistory(); - Object.assign(history, { ...record, id: undefined }); Object.assign(record, body); - history.profileChildrenId = childrenId; + record.lastUpdateUserId = req.user.sub; record.lastUpdateFullName = req.user.name; + history.profileChildrenId = record.id; + history.childrenCareer = record.childrenCareer; + history.childrenFirstName = record.childrenFirstName; + history.childrenLastName = record.childrenLastName; + history.childrenPrefix = record.childrenPrefix; + history.childrenLive = record.childrenLive; + history.childrenCitizenId = record.childrenCitizenId; + history.lastUpdateUserId = req.user.sub, history.lastUpdateFullName = req.user.name; - await Promise.all([ this.childrenRepository.save(record), this.childrenHistoryRepository.save(history), @@ -108,7 +126,7 @@ export class ProfileChildrenController extends Controller { const result = await this.childrenRepository.delete({ id: childrenId }); - if (result.affected && result.affected <= 0) { + if (result.affected == undefined || result.affected <= 0) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); } diff --git a/src/controllers/ProfileChildrenEmployeeController.ts b/src/controllers/ProfileChildrenEmployeeController.ts index bb71274a..64c28c61 100644 --- a/src/controllers/ProfileChildrenEmployeeController.ts +++ b/src/controllers/ProfileChildrenEmployeeController.ts @@ -27,7 +27,7 @@ import { } from "../entities/ProfileChildren"; import { ProfileEmployee } from "../entities/ProfileEmployee"; -@Route("api/v1/org/profile-employee/children") +@Route("api/v1/org/profile-employee/family/children") @Tags("ProfileChildren") @Security("bearerAuth") export class ProfileChildrenEmployeeController extends Controller { @@ -35,10 +35,10 @@ export class ProfileChildrenEmployeeController extends Controller { private childrenRepository = AppDataSource.getRepository(ProfileChildren); private childrenHistoryRepository = AppDataSource.getRepository(ProfileChildrenHistory); - @Get("{profileId}") - public async getChildren(@Path() profileId: string) { + @Get("{profileEmployeeId}") + public async getChildren(@Path() profileEmployeeId: string) { const lists = await this.childrenRepository.find({ - where: { profileEmployeeId: profileId }, + where: { profileEmployeeId: profileEmployeeId }, }); return new HttpSuccess(lists); } @@ -73,8 +73,23 @@ export class ProfileChildrenEmployeeController extends Controller { }; Object.assign(data, { ...body, ...meta }); - await this.childrenRepository.save(data); + if(data){ + const history: ProfileChildrenHistory = Object.assign(new ProfileChildrenHistory(), { + profileChildrenId: data.id, + childrenCareer: data.childrenCareer, + childrenFirstName: data.childrenFirstName, + childrenLastName: data.childrenLastName, + childrenPrefix: data.childrenPrefix, + childrenLive: data.childrenLive, + childrenCitizenId: data.childrenCitizenId, + createdUserId: req.user.sub, + createdFullName: req.user.name, + lastUpdateUserId: req.user.sub, + lastUpdateFullName: req.user.name, + }); + await this.childrenHistoryRepository.save(history); + } return new HttpSuccess(); } @@ -86,15 +101,21 @@ export class ProfileChildrenEmployeeController extends Controller { @Path() childrenId: string, ) { const record = await this.childrenRepository.findOneBy({ id: childrenId }); - if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); const history = new ProfileChildrenHistory(); - Object.assign(history, { ...record, id: undefined }); Object.assign(record, body); - history.profileChildrenId = childrenId; + record.lastUpdateUserId = req.user.sub; record.lastUpdateFullName = req.user.name; + history.profileChildrenId = record.id; + history.childrenCareer = record.childrenCareer; + history.childrenFirstName = record.childrenFirstName; + history.childrenLastName = record.childrenLastName; + history.childrenPrefix = record.childrenPrefix; + history.childrenLive = record.childrenLive; + history.childrenCitizenId = record.childrenCitizenId; + history.lastUpdateUserId = req.user.sub, history.lastUpdateFullName = req.user.name; await Promise.all([ @@ -113,7 +134,7 @@ export class ProfileChildrenEmployeeController extends Controller { const result = await this.childrenRepository.delete({ id: childrenId }); - if (result.affected && result.affected <= 0) { + if (result.affected == undefined || result.affected <= 0) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); } diff --git a/src/controllers/ProfileFamilyCoupleController.ts b/src/controllers/ProfileFamilyCoupleController.ts index e79a5977..c2d3d2e9 100644 --- a/src/controllers/ProfileFamilyCoupleController.ts +++ b/src/controllers/ProfileFamilyCoupleController.ts @@ -33,7 +33,7 @@ export class ProfileFamilyCoupleController extends Controller { @Example({ status: 200, message: "สำเร็จ", - result: { + result: [{ id: "6207ae29-05ef-4abb-9a37-a887265d671e", couple: true, couplePrefix: "string", @@ -45,7 +45,7 @@ export class ProfileFamilyCoupleController extends Controller { coupleLive: true, relationship: "string", profileId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201", - }, + }], }) public async getFamilyCouple(@Path() profileId: string) { const profile = await this.profileRepo.findOne({ @@ -55,7 +55,7 @@ export class ProfileFamilyCoupleController extends Controller { if (!profile) { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } - const familyCouple = await this.ProfileFamilyCouple.findOne({ + const familyCouple = await this.ProfileFamilyCouple.find({ select: [ "id", "couple", "couplePrefix", "coupleFirstName", "coupleLastName", "coupleLastNameOld", "coupleCareer", "coupleCitizenId", "coupleLive", "relationship", "profileId", diff --git a/src/entities/ProfileChildren.ts b/src/entities/ProfileChildren.ts index 4e3c929b..c277713e 100644 --- a/src/entities/ProfileChildren.ts +++ b/src/entities/ProfileChildren.ts @@ -74,11 +74,14 @@ export class ProfileChildren extends EntityBase { @JoinColumn({ name: "profileEmployeeId" }) profileEmployee: ProfileEmployee; - @OneToMany( - () => ProfileChildrenHistory, - (profileChildrenHistory) => profileChildrenHistory.histories, - ) - profileChildrenHistories: ProfileChildrenHistory[]; + // @OneToMany( + // () => ProfileChildrenHistory, + // (profileChildrenHistory) => profileChildrenHistory.histories, + // ) + // profileChildrenHistories: ProfileChildrenHistory[]; + + @OneToMany(() => ProfileChildrenHistory, (v) => v.profileChildrenHistories) + histories: ProfileChildrenHistory[]; } export type CreateProfileChildren = { @@ -102,7 +105,7 @@ export type CreateProfileChildrenEmployee = { }; export type UpdateProfileChildren = { - id: string; + // id: string; childrenCareer?: string | null; childrenFirstName?: string | null; childrenLastName?: string | null; diff --git a/src/entities/ProfileChildrenHistory.ts b/src/entities/ProfileChildrenHistory.ts index 3c2dae6e..2ce86751 100644 --- a/src/entities/ProfileChildrenHistory.ts +++ b/src/entities/ProfileChildrenHistory.ts @@ -57,9 +57,13 @@ export class ProfileChildrenHistory extends EntityBase { }) profileChildrenId: string; - @ManyToOne(() => ProfileChildren, (profileChildren) => profileChildren.profileChildrenHistories) + // @ManyToOne(() => ProfileChildren, (profileChildren) => profileChildren.profileChildrenHistories) + // @JoinColumn({ name: "profileChildrenId" }) + // histories: ProfileChildren; + + @ManyToOne(() => ProfileChildren, (v) => v.histories) @JoinColumn({ name: "profileChildrenId" }) - histories: ProfileChildren; + profileChildrenHistories: ProfileChildren; } export type CreateChildren = {