From 0114e41c3acf4dc4ba647a10ee113aea21ebc2b9 Mon Sep 17 00:00:00 2001 From: Kittapath Date: Tue, 14 May 2024 17:26:06 +0700 Subject: [PATCH] =?UTF-8?q?=E0=B9=81=E0=B8=81=E0=B9=89=E0=B9=84=E0=B8=82?= =?UTF-8?q?=E0=B8=9A=E0=B8=B8=E0=B8=95=E0=B8=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/ProfileChildrenController.ts | 117 +++++++++++++ .../ProfileChildrenEmployeeController.ts | 122 ++++++++++++++ src/entities/Profile.ts | 5 +- src/entities/ProfileChildren.ts | 112 +++++++++++++ src/entities/ProfileChildrenHistory.ts | 155 ++++++++++++++++++ src/entities/ProfileEmployee.ts | 3 +- src/entities/ProfileFamily.ts | 103 ------------ 7 files changed, 511 insertions(+), 106 deletions(-) create mode 100644 src/controllers/ProfileChildrenController.ts create mode 100644 src/controllers/ProfileChildrenEmployeeController.ts create mode 100644 src/entities/ProfileChildren.ts create mode 100644 src/entities/ProfileChildrenHistory.ts diff --git a/src/controllers/ProfileChildrenController.ts b/src/controllers/ProfileChildrenController.ts new file mode 100644 index 00000000..32c0be05 --- /dev/null +++ b/src/controllers/ProfileChildrenController.ts @@ -0,0 +1,117 @@ +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 { ProfileChildrenHistory } from "../entities/ProfileChildrenHistory"; +import { RequestWithUser } from "../middlewares/user"; +import { Profile } from "../entities/Profile"; +import { + CreateProfileChildren, + ProfileChildren, + UpdateProfileChildren, +} from "../entities/ProfileChildren"; + +@Route("api/v1/org/profile/children") +@Tags("ProfileChildren") +@Security("bearerAuth") +export class ProfileChildrenController extends Controller { + private profileRepository = AppDataSource.getRepository(Profile); + private childrenRepository = AppDataSource.getRepository(ProfileChildren); + private childrenHistoryRepository = AppDataSource.getRepository(ProfileChildrenHistory); + + @Get("{profileId}") + public async getChildren(@Path() profileId: string) { + const lists = await this.childrenRepository.find({ + where: { profileId: profileId }, + }); + return new HttpSuccess(lists); + } + + @Get("history/{childrenId}") + public async childrenHistory(@Path() childrenId: string) { + const record = await this.childrenHistoryRepository.find({ + where: { profileChildrenId: childrenId }, + order: { createdAt: "DESC" }, + }); + return new HttpSuccess(record); + } + + @Post() + public async newChildren(@Request() req: RequestWithUser, @Body() body: CreateProfileChildren) { + const profile = await this.profileRepository.findOneBy({ id: body.profileId }); + + if (!profile) { + throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); + } + + const data = new ProfileChildren(); + + const meta = { + createdUserId: req.user.sub, + createdFullName: req.user.name, + lastUpdateUserId: req.user.sub, + lastUpdateFullName: req.user.name, + }; + + Object.assign(data, { ...body, ...meta }); + + await this.childrenRepository.save(data); + + return new HttpSuccess(); + } + + @Patch("{childrenId}") + public async editChildren( + @Request() req: RequestWithUser, + @Body() body: UpdateProfileChildren, + @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.lastUpdateFullName = req.user.name; + history.lastUpdateFullName = req.user.name; + + await Promise.all([ + this.childrenRepository.save(record), + this.childrenHistoryRepository.save(history), + ]); + + return new HttpSuccess(); + } + + @Delete("{childrenId}") + public async deleteTraning(@Path() childrenId: string) { + await this.childrenHistoryRepository.delete({ + profileChildrenId: childrenId, + }); + + const result = await this.childrenRepository.delete({ id: childrenId }); + + if (result.affected && result.affected <= 0) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + } + + return new HttpSuccess(); + } +} diff --git a/src/controllers/ProfileChildrenEmployeeController.ts b/src/controllers/ProfileChildrenEmployeeController.ts new file mode 100644 index 00000000..bb71274a --- /dev/null +++ b/src/controllers/ProfileChildrenEmployeeController.ts @@ -0,0 +1,122 @@ +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 { ProfileChildrenHistory } from "../entities/ProfileChildrenHistory"; +import { RequestWithUser } from "../middlewares/user"; +import { Profile } from "../entities/Profile"; +import { + CreateProfileChildren, + CreateProfileChildrenEmployee, + ProfileChildren, + UpdateProfileChildren, +} from "../entities/ProfileChildren"; +import { ProfileEmployee } from "../entities/ProfileEmployee"; + +@Route("api/v1/org/profile-employee/children") +@Tags("ProfileChildren") +@Security("bearerAuth") +export class ProfileChildrenEmployeeController extends Controller { + private profileRepository = AppDataSource.getRepository(ProfileEmployee); + private childrenRepository = AppDataSource.getRepository(ProfileChildren); + private childrenHistoryRepository = AppDataSource.getRepository(ProfileChildrenHistory); + + @Get("{profileId}") + public async getChildren(@Path() profileId: string) { + const lists = await this.childrenRepository.find({ + where: { profileEmployeeId: profileId }, + }); + return new HttpSuccess(lists); + } + + @Get("history/{childrenId}") + public async childrenHistory(@Path() childrenId: string) { + const record = await this.childrenHistoryRepository.find({ + where: { profileChildrenId: childrenId }, + order: { createdAt: "DESC" }, + }); + return new HttpSuccess(record); + } + + @Post() + public async newChildren( + @Request() req: RequestWithUser, + @Body() body: CreateProfileChildrenEmployee, + ) { + const profile = await this.profileRepository.findOneBy({ id: body.profileEmployeeId }); + + if (!profile) { + throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); + } + + const data = new ProfileChildren(); + + const meta = { + createdUserId: req.user.sub, + createdFullName: req.user.name, + lastUpdateUserId: req.user.sub, + lastUpdateFullName: req.user.name, + }; + + Object.assign(data, { ...body, ...meta }); + + await this.childrenRepository.save(data); + + return new HttpSuccess(); + } + + @Patch("{childrenId}") + public async editChildren( + @Request() req: RequestWithUser, + @Body() body: UpdateProfileChildren, + @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.lastUpdateFullName = req.user.name; + history.lastUpdateFullName = req.user.name; + + await Promise.all([ + this.childrenRepository.save(record), + this.childrenHistoryRepository.save(history), + ]); + + return new HttpSuccess(); + } + + @Delete("{childrenId}") + public async deleteTraning(@Path() childrenId: string) { + await this.childrenHistoryRepository.delete({ + profileChildrenId: childrenId, + }); + + const result = await this.childrenRepository.delete({ id: childrenId }); + + if (result.affected && result.affected <= 0) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + } + + return new HttpSuccess(); + } +} diff --git a/src/entities/Profile.ts b/src/entities/Profile.ts index 1d7b8d04..7608089b 100644 --- a/src/entities/Profile.ts +++ b/src/entities/Profile.ts @@ -16,12 +16,13 @@ import { ProfileAbility } from "./ProfileAbility"; import { ProfileDuty } from "./ProfileDuty"; import { ProfileNopaid } from "./ProfileNopaid"; import { ProfileOther } from "./ProfileOther"; -import { ProfileChildren, ProfileFamilyHistory } from "./ProfileFamily"; +import { ProfileFamilyHistory } from "./ProfileFamily"; import { ProfileGovernment } from "./ProfileGovernment"; import { Province } from "./Province"; import { SubDistrict } from "./SubDistrict"; import { District } from "./District"; import { ProfileAvatar } from "./ProfileAvatar"; +import { ProfileChildren } from "./ProfileChildren"; @Entity("profile") export class Profile extends EntityBase { @@ -309,7 +310,7 @@ export class Profile extends EntityBase { profileFamily: ProfileFamilyHistory[]; @OneToMany(() => ProfileChildren, (profileChildren) => profileChildren.profile) - profileChildren: ProfileChildren[]; + profileChildrens: ProfileChildren[]; @OneToMany(() => ProfileGovernment, (profileGovernment) => profileGovernment.profile) profileGovernment: ProfileGovernment[]; diff --git a/src/entities/ProfileChildren.ts b/src/entities/ProfileChildren.ts new file mode 100644 index 00000000..4e3c929b --- /dev/null +++ b/src/entities/ProfileChildren.ts @@ -0,0 +1,112 @@ +import { Column, Entity, ManyToOne, OneToMany, JoinColumn } from "typeorm"; +import { EntityBase } from "./base/Base"; +import { Profile } from "./Profile"; +import { ProfileEmployee } from "./ProfileEmployee"; +import { ProfileChildrenHistory } from "./ProfileChildrenHistory"; + +@Entity("profileChildren") +export class ProfileChildren extends EntityBase { + @Column({ + nullable: true, + default: null, + comment: "อาชีพบุตร", + }) + childrenCareer: string; + + @Column({ + nullable: true, + default: null, + comment: "ชื่อบุตร", + }) + childrenFirstName: string; + + @Column({ + nullable: true, + default: null, + comment: "นามสกุลบุตร", + }) + childrenLastName: string; + + @Column({ + nullable: true, + default: null, + comment: "คำนำหน้าบุตร", + }) + childrenPrefix: string; + + @Column({ + nullable: true, + default: null, + type: "boolean", + comment: "มีชีวิตบุตร", + }) + childrenLive: boolean; + + @Column({ + nullable: true, + default: null, + comment: "เลขที่บัตรประชาชนบุตร", + }) + childrenCitizenId: string; + + @Column({ + nullable: true, + length: 40, + type: "uuid", + comment: "คีย์นอก(FK) ของตาราง Profile", + default: null, + }) + profileId: string; + + @Column({ + nullable: true, + length: 40, + comment: "คีย์นอก(FK)ของตาราง ProfileEmployee", + default: null, + }) + profileEmployeeId: string; + + @ManyToOne(() => Profile, (Profile) => Profile.profileChildrens) + @JoinColumn({ name: "profileId" }) + profile: Profile; + + @ManyToOne(() => ProfileEmployee, (ProfileEmployee) => ProfileEmployee.profileChildrens) + @JoinColumn({ name: "profileEmployeeId" }) + profileEmployee: ProfileEmployee; + + @OneToMany( + () => ProfileChildrenHistory, + (profileChildrenHistory) => profileChildrenHistory.histories, + ) + profileChildrenHistories: ProfileChildrenHistory[]; +} + +export type CreateProfileChildren = { + profileId: string; + childrenCareer: string; + childrenFirstName: string; + childrenLastName: string; + childrenPrefix: string; + childrenLive: boolean; + childrenCitizenId: string; +}; + +export type CreateProfileChildrenEmployee = { + profileEmployeeId: string; + childrenCareer: string; + childrenFirstName: string; + childrenLastName: string; + childrenPrefix: string; + childrenLive: boolean; + childrenCitizenId: string; +}; + +export type UpdateProfileChildren = { + id: string; + childrenCareer?: string | null; + childrenFirstName?: string | null; + childrenLastName?: string | null; + childrenPrefix?: string | null; + childrenLive?: boolean | null; + childrenCitizenId?: string | null; +}; diff --git a/src/entities/ProfileChildrenHistory.ts b/src/entities/ProfileChildrenHistory.ts new file mode 100644 index 00000000..3c2dae6e --- /dev/null +++ b/src/entities/ProfileChildrenHistory.ts @@ -0,0 +1,155 @@ +import { Column, Entity, ManyToOne, OneToMany, JoinColumn } from "typeorm"; +import { EntityBase } from "./base/Base"; +import { Profile } from "./Profile"; +import { ProfileEmployee } from "./ProfileEmployee"; +import { ProfileChildren } from "./ProfileChildren"; + +@Entity("profileChildrenHistory") +export class ProfileChildrenHistory extends EntityBase { + @Column({ + nullable: true, + default: null, + comment: "อาชีพบุตร", + }) + childrenCareer: string; + + @Column({ + nullable: true, + default: null, + comment: "ชื่อบุตร", + }) + childrenFirstName: string; + + @Column({ + nullable: true, + default: null, + comment: "นามสกุลบุตร", + }) + childrenLastName: string; + + @Column({ + nullable: true, + default: null, + comment: "คำนำหน้าบุตร", + }) + childrenPrefix: string; + + @Column({ + nullable: true, + default: null, + type: "boolean", + comment: "มีชีวิตบุตร", + }) + childrenLive: boolean; + + @Column({ + nullable: true, + default: null, + comment: "เลขที่บัตรประชาชนบุตร", + }) + childrenCitizenId: string; + + @Column({ + nullable: true, + length: 40, + comment: "คีย์นอก(FK)ของตาราง ProfileChildren", + default: null, + }) + profileChildrenId: string; + + @ManyToOne(() => ProfileChildren, (profileChildren) => profileChildren.profileChildrenHistories) + @JoinColumn({ name: "profileChildrenId" }) + histories: ProfileChildren; +} + +export type CreateChildren = { + childrenCareer: string; + childrenFirstName: string; + childrenLastName: string; + childrenPrefix: string; + childrenLive: boolean; + childrenCitizenId: string; +}; + +export type UpdateChildren = { + id: string; + childrenCareer?: string | null; + childrenFirstName?: string | null; + childrenLastName?: string | null; + childrenPrefix?: string | null; + childrenLive?: boolean | null; + childrenCitizenId?: string | null; +}; + +export type CreateProfileFamily = { + couple: boolean | null; + couplePrefix: string | null; + coupleFirstName: string | null; + coupleLastName: string | null; + coupleLastNameOld: string | null; + coupleCareer: string | null; + coupleCitizenId: string | null; + coupleLive: boolean | null; + fatherPrefix: string | null; + fatherFirstName: string | null; + fatherLastName: string | null; + fatherCareer: string | null; + fatherCitizenId: string | null; + fatherLive: boolean | null; + motherPrefix: string | null; + motherFirstName: string | null; + motherLastName: string | null; + motherCareer: string | null; + motherCitizenId: string | null; + motherLive: boolean | null; + profileId: string; + children: CreateChildren[]; +}; +export type CreateProfileFamilyEmployee = { + couple: boolean | null; + couplePrefix: string | null; + coupleFirstName: string | null; + coupleLastName: string | null; + coupleLastNameOld: string | null; + coupleCareer: string | null; + coupleCitizenId: string | null; + coupleLive: boolean | null; + fatherPrefix: string | null; + fatherFirstName: string | null; + fatherLastName: string | null; + fatherCareer: string | null; + fatherCitizenId: string | null; + fatherLive: boolean | null; + motherPrefix: string | null; + motherFirstName: string | null; + motherLastName: string | null; + motherCareer: string | null; + motherCitizenId: string | null; + motherLive: boolean | null; + profileEmployeeId: string | null; + children: CreateChildren[]; +}; + +export type UpdateProfileFamily = { + couple?: boolean | null; + couplePrefix?: string | null; + coupleFirstName?: string | null; + coupleLastName?: string | null; + coupleLastNameOld?: string | null; + coupleCareer?: string | null; + coupleCitizenId?: string | null; + coupleLive?: boolean | null; + fatherPrefix?: string | null; + fatherFirstName?: string | null; + fatherLastName?: string | null; + fatherCareer?: string | null; + fatherCitizenId?: string | null; + fatherLive?: boolean | null; + motherPrefix?: string | null; + motherFirstName?: string | null; + motherLastName?: string | null; + motherCareer?: string | null; + motherCitizenId?: string | null; + motherLive?: boolean | null; + children: UpdateChildren[]; +}; diff --git a/src/entities/ProfileEmployee.ts b/src/entities/ProfileEmployee.ts index 53d5b1cd..9acd9ae0 100644 --- a/src/entities/ProfileEmployee.ts +++ b/src/entities/ProfileEmployee.ts @@ -15,12 +15,13 @@ import { ProfileDuty } from "./ProfileDuty"; import { ProfileNopaid } from "./ProfileNopaid"; import { ProfileDiscipline } from "./ProfileDiscipline"; import { ProfileChangeName } from "./ProfileChangeName"; -import { ProfileChildren, ProfileFamilyHistory } from "./ProfileFamily"; +import { ProfileFamilyHistory } from "./ProfileFamily"; import { ProfileEducation } from "./ProfileEducation"; import { ProfileAbility } from "./ProfileAbility"; import { ProfileOther } from "./ProfileOther"; import { ProfileAvatar } from "./ProfileAvatar"; import { ProfileGovernment } from "./ProfileGovernment"; +import { ProfileChildren } from "./ProfileChildren"; @Entity("profileEmployee") export class ProfileEmployee extends EntityBase { @Column({ diff --git a/src/entities/ProfileFamily.ts b/src/entities/ProfileFamily.ts index eceef17d..baee34ff 100644 --- a/src/entities/ProfileFamily.ts +++ b/src/entities/ProfileFamily.ts @@ -167,114 +167,11 @@ export class ProfileFamilyHistory extends EntityBase { @ManyToOne(() => Profile, (v) => v.profileFamily) profile: Profile; - @OneToMany(() => ProfileChildrenHistory, (v) => v.profileFamilyHistory) - profileChildrenHistories: ProfileChildrenHistory[]; - @ManyToOne(() => ProfileEmployee, (ProfileEmployee) => ProfileEmployee.profileFamilys) @JoinColumn({ name: "profileEmployeeId" }) profileEmployee: ProfileEmployee; } -@Entity("profileChildren") -export class ProfileChildren extends EntityBase { - @Column({ - nullable: true, - default: null, - comment: "อาชีพบุตร", - }) - childrenCareer: string; - - @Column({ - nullable: true, - default: null, - comment: "ชื่อบุตร", - }) - childrenFirstName: string; - - @Column({ - nullable: true, - default: null, - comment: "นามสกุลบุตร", - }) - childrenLastName: string; - - @Column({ - nullable: true, - default: null, - comment: "คำนำหน้าบุตร", - }) - childrenPrefix: string; - - @Column({ - nullable: true, - default: null, - type: "boolean", - comment: "มีชีวิตบุตร", - }) - childrenLive: boolean; - - @Column({ - nullable: true, - default: null, - comment: "เลขที่บัตรประชาชนบุตร", - }) - childrenCitizenId: string; - - @Column({ - nullable: true, - length: 40, - type: "uuid", - comment: "คีย์นอก(FK) ของตาราง Profile", - default: null, - }) - profileId: string; - - @Column({ - nullable: true, - length: 40, - comment: "คีย์นอก(FK)ของตาราง ProfileEmployee", - default: null, - }) - profileEmployeeId: string; - - @ManyToOne(() => Profile, (v) => v.profileFamily, { onDelete: "CASCADE" }) - profile: Profile; - - @OneToMany(() => ProfileChildrenHistory, (v) => v.profileChildren) - histories: Profile; - - @ManyToOne(() => ProfileEmployee, (ProfileEmployee) => ProfileEmployee.profileChildrens) - @JoinColumn({ name: "profileEmployeeId" }) - profileEmployee: ProfileEmployee; -} - -@Entity("profileChildrenHistory") -export class ProfileChildrenHistory extends ProfileChildren { - @Column({ - nullable: true, - length: 40, - type: "uuid", - comment: "คีย์นอก(FK) ของตาราง Profile", - default: null, - }) - profileFamilyHistoryId: string; - - @ManyToOne(() => ProfileFamilyHistory, (v) => v.profileChildrenHistories, { onDelete: "CASCADE" }) - profileFamilyHistory: ProfileFamilyHistory; - - @Column({ - nullable: true, - length: 40, - type: "uuid", - comment: "คีย์นอก(FK) ของตาราง Profile", - default: null, - }) - profileChildrenId: string; - - @ManyToOne(() => ProfileChildren, (v) => v.histories, { onDelete: "CASCADE" }) - profileChildren: ProfileChildren; -} - export type CreateChildren = { childrenCareer: string; childrenFirstName: string;