From c9c14f052b289f103efe7621dbabc24faa13bad4 Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Wed, 15 May 2024 15:29:12 +0700 Subject: [PATCH 1/2] orderBy --- src/controllers/BloodGroupController.ts | 2 +- src/controllers/GenderController.ts | 2 +- src/controllers/PosExecutiveController.ts | 1 + src/controllers/PositionController.ts | 11 +++++++++++ src/controllers/PrefixController.ts | 2 +- src/controllers/RankController.ts | 2 +- src/controllers/RelationshipController.ts | 2 +- src/controllers/ReligionController.ts | 2 +- 8 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/controllers/BloodGroupController.ts b/src/controllers/BloodGroupController.ts index bffb007d..815d7603 100644 --- a/src/controllers/BloodGroupController.ts +++ b/src/controllers/BloodGroupController.ts @@ -142,7 +142,7 @@ export class BloodGroupController extends Controller { async listBloodGroup() { const bloodGroup = await this.bloodGroupRepository.find({ select: ["id", "name", "createdAt", "lastUpdatedAt", "createdFullName", "lastUpdateFullName"], - order: { createdAt: "ASC" }, + order: { name: "ASC" }, }); // if (!bloodGroup) { diff --git a/src/controllers/GenderController.ts b/src/controllers/GenderController.ts index 841380c1..c9e33785 100644 --- a/src/controllers/GenderController.ts +++ b/src/controllers/GenderController.ts @@ -41,7 +41,7 @@ export class GenderController extends Controller { async GetResult() { const _gender = await this.genderRepository.find({ select: ["id", "name", "createdAt", "lastUpdatedAt", "createdFullName", "lastUpdateFullName"], - order: { createdAt: "ASC" }, + order: { name: "ASC" }, }); // if (!_gender) { // return new HttpSuccess([]); diff --git a/src/controllers/PosExecutiveController.ts b/src/controllers/PosExecutiveController.ts index eee0e55e..dcab6724 100644 --- a/src/controllers/PosExecutiveController.ts +++ b/src/controllers/PosExecutiveController.ts @@ -199,6 +199,7 @@ export class PosExecutiveController extends Controller { async GetPosExecutive() { const posExecutive = await this.posExecutiveRepository.find({ select: ["id", "posExecutiveName", "posExecutivePriority"], + order: { posExecutivePriority: "ASC" }, }); // if (!posExecutive) { // return new HttpSuccess([]); diff --git a/src/controllers/PositionController.ts b/src/controllers/PositionController.ts index da545b3e..05160272 100644 --- a/src/controllers/PositionController.ts +++ b/src/controllers/PositionController.ts @@ -352,6 +352,7 @@ export class PositionController extends Controller { findPosDict = await this.posDictRepository.find({ where: { posDictName: Like(`%${keyword}%`) }, relations: ["posType", "posLevel", "posExecutive"], + order: { posDictName: "ASC" }, }); // if (!findPosDict) { // throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล " + keyword); @@ -362,6 +363,7 @@ export class PositionController extends Controller { findPosDict = await this.posDictRepository.find({ where: { posDictField: Like(`%${keyword}%`) }, relations: ["posType", "posLevel", "posExecutive"], + order: { posDictField: "ASC" }, }); // if (!findPosDict) { // throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล " + keyword); @@ -371,33 +373,39 @@ export class PositionController extends Controller { case "positionType": const findTypes: PosType[] = await this.posTypeRepository.find({ where: { posTypeName: Like(`%${keyword}%`) }, + order: { posTypeName: "ASC" }, select: ["id"], }); findPosDict = await this.posDictRepository.find({ where: { posTypeId: In(findTypes.map((x) => x.id)) }, relations: ["posType", "posLevel", "posExecutive"], + order: { posDictName: "ASC" }, }); break; case "positionLevel": const findLevel: PosLevel[] = await this.posLevelRepository.find({ where: { posLevelName: Like(`%${keyword}%`) }, + order: { posLevelName: "ASC" }, select: ["id"], }); findPosDict = await this.posDictRepository.find({ where: { posLevelId: In(findLevel.map((x) => x.id)) }, relations: ["posType", "posLevel", "posExecutive"], + order: { posDictName: "ASC" }, }); break; case "positionExecutive": const findExecutive: PosExecutive[] = await this.posExecutiveRepository.find({ where: { posExecutiveName: Like(`%${keyword}%`) }, + order: { posExecutiveName: "ASC" }, select: ["id"], }); findPosDict = await this.posDictRepository.find({ where: { posExecutiveId: In(findExecutive.map((x) => x.id)) }, relations: ["posType", "posLevel", "posExecutive"], + order: { posDictName: "ASC" }, }); break; @@ -405,6 +413,7 @@ export class PositionController extends Controller { findPosDict = await this.posDictRepository.find({ where: { posDictExecutiveField: Like(`%${keyword}%`) }, relations: ["posType", "posLevel", "posExecutive"], + order: { posDictName: "ASC" }, }); // if (!findPosDict) { // throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล " + keyword); @@ -415,6 +424,7 @@ export class PositionController extends Controller { findPosDict = await this.posDictRepository.find({ where: { posDictArea: Like(`%${keyword}%`) }, relations: ["posType", "posLevel", "posExecutive"], + order: { posDictName: "ASC" }, }); // if (!findPosDict) { // throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล " + keyword); @@ -424,6 +434,7 @@ export class PositionController extends Controller { default: findPosDict = await this.posDictRepository.find({ relations: ["posType", "posLevel", "posExecutive"], + order: { posDictName: "ASC" }, }); // if (!findPosDict) { // throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); diff --git a/src/controllers/PrefixController.ts b/src/controllers/PrefixController.ts index 27941f43..4f0cbedd 100644 --- a/src/controllers/PrefixController.ts +++ b/src/controllers/PrefixController.ts @@ -41,7 +41,7 @@ export class PrefixController extends Controller { async Get() { const _prefix = await this.prefixRepository.find({ select: ["id", "name", "createdAt", "lastUpdatedAt", "createdFullName", "lastUpdateFullName"], - order: { createdAt: "ASC" }, + order: { name: "ASC" }, }); // if (!_prefix) { // return new HttpSuccess([]); diff --git a/src/controllers/RankController.ts b/src/controllers/RankController.ts index 6f4018d9..32c002ba 100644 --- a/src/controllers/RankController.ts +++ b/src/controllers/RankController.ts @@ -141,7 +141,7 @@ export class RankController extends Controller { async listRank() { const rank = await this.rankRepository.find({ select: ["id", "name", "createdAt", "lastUpdatedAt", "createdFullName", "lastUpdateFullName"], - order: { createdAt: "ASC" }, + order: { name: "ASC" }, }); // if (!rank) { diff --git a/src/controllers/RelationshipController.ts b/src/controllers/RelationshipController.ts index 81baef95..b43b5e3e 100644 --- a/src/controllers/RelationshipController.ts +++ b/src/controllers/RelationshipController.ts @@ -141,7 +141,7 @@ export class RelationshipController extends Controller { async listRelationship() { const relationship = await this.relationshipRepository.find({ select: ["id", "name", "createdAt", "lastUpdatedAt", "createdFullName", "lastUpdateFullName"], - order: { createdAt: "ASC" }, + order: { name: "ASC" }, }); // if (!relationship) { // return new HttpSuccess([]); diff --git a/src/controllers/ReligionController.ts b/src/controllers/ReligionController.ts index 5bfea001..b3fdbed7 100644 --- a/src/controllers/ReligionController.ts +++ b/src/controllers/ReligionController.ts @@ -141,7 +141,7 @@ export class ReligionController extends Controller { async listReligion() { const religion = await this.religionRepository.find({ select: ["id", "name", "createdAt", "lastUpdatedAt", "createdFullName", "lastUpdateFullName"], - order: { createdAt: "ASC" }, + order: { name: "ASC" }, }); // if (!religion) { From 702ed615ea010ac7cddbffc5699ba174316c672b Mon Sep 17 00:00:00 2001 From: Bright Date: Wed, 15 May 2024 15:41:51 +0700 Subject: [PATCH 2/2] 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(); + } + +}