From 35891a371a85e5b23cfffb7f7c61bd90b1358512 Mon Sep 17 00:00:00 2001 From: Kittapath Date: Tue, 14 May 2024 11:56:40 +0700 Subject: [PATCH 1/4] migrate --- src/controllers/EducationLevelController.ts | 2 +- ...24792-update_table_profileemployee_add_pk5.ts | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 src/migration/1715662524792-update_table_profileemployee_add_pk5.ts diff --git a/src/controllers/EducationLevelController.ts b/src/controllers/EducationLevelController.ts index 67da2294..a4e87734 100644 --- a/src/controllers/EducationLevelController.ts +++ b/src/controllers/EducationLevelController.ts @@ -157,7 +157,7 @@ export class EducationLevelController extends Controller { "createdFullName", "lastUpdateFullName", ], - order: { createdAt: "ASC" }, + order: { rank: "ASC" }, }); // if (!educationLevel) { diff --git a/src/migration/1715662524792-update_table_profileemployee_add_pk5.ts b/src/migration/1715662524792-update_table_profileemployee_add_pk5.ts new file mode 100644 index 00000000..8ab25ed3 --- /dev/null +++ b/src/migration/1715662524792-update_table_profileemployee_add_pk5.ts @@ -0,0 +1,16 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class UpdateTableProfileemployeeAddPk51715662524792 implements MigrationInterface { + name = 'UpdateTableProfileemployeeAddPk51715662524792' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`profileSalary\` ADD \`profileEmployeeId\` varchar(40) NULL COMMENT 'คีย์นอก(FK)ของตาราง ProfileEmployee'`); + await queryRunner.query(`ALTER TABLE \`profileSalary\` ADD CONSTRAINT \`FK_f5758428d496b6d2a051c8af92b\` FOREIGN KEY (\`profileEmployeeId\`) REFERENCES \`profileEmployee\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`profileSalary\` DROP FOREIGN KEY \`FK_f5758428d496b6d2a051c8af92b\``); + await queryRunner.query(`ALTER TABLE \`profileSalary\` DROP COLUMN \`profileEmployeeId\``); + } + +} From 4888b36eb1d965b58351154e7005369990629946 Mon Sep 17 00:00:00 2001 From: Bright Date: Tue, 14 May 2024 12:00:08 +0700 Subject: [PATCH 2/4] =?UTF-8?q?=E0=B9=80=E0=B8=9E=E0=B8=B4=E0=B9=88?= =?UTF-8?q?=E0=B8=A1=E0=B8=9F=E0=B8=B4=E0=B8=A5=E0=B8=94=E0=B9=8C=20Profil?= =?UTF-8?q?eGovernment.profileEmployeeId?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ProfileGovernmentEmployeeController.ts | 191 ++++++++++++++++++ src/entities/ProfileGovernment.ts | 28 ++- 2 files changed, 215 insertions(+), 4 deletions(-) create mode 100644 src/controllers/ProfileGovernmentEmployeeController.ts diff --git a/src/controllers/ProfileGovernmentEmployeeController.ts b/src/controllers/ProfileGovernmentEmployeeController.ts new file mode 100644 index 00000000..b60fbee2 --- /dev/null +++ b/src/controllers/ProfileGovernmentEmployeeController.ts @@ -0,0 +1,191 @@ +import { Body, Controller, Example, Get, Patch, Path, Delete, 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 { CreateProfileEmployeeGovernment, ProfileGovernment, UpdateProfileGovernment } from "../entities/ProfileGovernment"; +import { EmployeePosition } from "../entities/EmployeePosition"; +import { EmployeePosMaster } from "../entities/EmployeePosMaster"; +import { calculateAge, calculateRetireDate } from "../interfaces/utils"; + +@Route("api/v1/org/profile-employee/government") +@Tags("ProfileEmployeeGovernment") +@Security("bearerAuth") +export class ProfileGovernmentEmployeeController extends Controller { + private profileEmployeeRepo = AppDataSource.getRepository(ProfileEmployee); + private govRepo = AppDataSource.getRepository(ProfileGovernment); + private positionRepo = AppDataSource.getRepository(EmployeePosition); + private posMasterRepo = AppDataSource.getRepository(EmployeePosMaster); + + /** + * + * @summary ข้อมูลราชการ + * + */ + @Get("{profileEmployeeId}") + @Example({}) + public async getGovHistory(@Path() profileEmployeeId: string) { + const record = await this.profileEmployeeRepo.findOne({ + where: { id: profileEmployeeId }, + relations: { + posType: true, + posLevel: true, + }, + }); + const posMaster = await this.posMasterRepo.findOne({ + where: { + orgRevision: { + orgRevisionIsCurrent: true, + orgRevisionIsDraft: false, + }, + current_holderId: profileEmployeeId, + }, + order: { createdAt: "DESC" }, + relations: { + orgRoot: true, + orgChild1: true, + orgChild2: true, + orgChild3: true, + orgChild4: true, + }, + }); + const position = await this.positionRepo.findOne({ + where: { + positionIsSelected: true, + posMaster: { + orgRevision: { + orgRevisionIsCurrent: true, + orgRevisionIsDraft: false, + }, + current_holderId: profileEmployeeId, + }, + }, + order: { createdAt: "DESC" }, + }); + + if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + const fullNameParts = [ + posMaster == null || posMaster.orgChild4 == null ? null : posMaster.orgChild4.orgChild4Name, + posMaster == null || posMaster.orgChild3 == null ? null : posMaster.orgChild3.orgChild3Name, + posMaster == null || posMaster.orgChild2 == null ? null : posMaster.orgChild2.orgChild2Name, + posMaster == null || posMaster.orgChild1 == null ? null : posMaster.orgChild1.orgChild1Name, + posMaster == null || posMaster.orgRoot == null ? null : posMaster.orgRoot.orgRootName, + ]; + const org = fullNameParts.filter((part) => part !== undefined && part !== null).join("/"); + let orgShortName = ""; + if (posMaster != null) { + if (posMaster.orgChild1Id === null) { + orgShortName = posMaster.orgRoot?.orgRootShortName; + } else if (posMaster.orgChild2Id === null) { + orgShortName = posMaster.orgChild1?.orgChild1ShortName; + } else if (posMaster.orgChild3Id === null) { + orgShortName = posMaster.orgChild2?.orgChild2ShortName; + } else if (posMaster.orgChild4Id === null) { + orgShortName = posMaster.orgChild3?.orgChild3ShortName; + } else { + orgShortName = posMaster.orgChild4?.orgChild4ShortName; + } + } + const data = { + org: org, //สังกัด + position: record.position, //ตำแหน่ง + posLevel: record.posLevel == null ? null : record.posLevel.posLevelName, //ระดับ + posMasterNo: posMaster == null ? null : `${orgShortName} ${posMaster.posMasterNo}`, //เลขที่ตำแหน่ง + posType: record.posType == null ? null : record.posType.posTypeName, //ประเภท + dateLeave: record.birthDate == null ? null : calculateRetireDate(record.birthDate), //วันเกษียณ + }; + + return new HttpSuccess(data); + } + + /** + * + * @summary ประวัติข้อมูลราชการ + * + */ + @Get("history/{profileEmployeeId}") + @Example({}) + public async govHistory(@Path() profileEmployeeId: string) { + const record = await this.govRepo.find({ + order: { lastUpdatedAt: "DESC" }, + where: { profileEmployeeId: profileEmployeeId }, + }); + + // record.pop(); + + return new HttpSuccess(record); + } + + // @Post() + // public async newGov(@Request() req: RequestWithUser, @Body() body: CreateProfileGovernment) { + // if (!body.profileEmployeeId) { + // throw new HttpError(HttpStatus.BAD_REQUEST, "กรุณากรอก profileEmployeeId"); + // } + + // const profile = await this.profileEmployeeRepo.findOneBy({ id: body.profileEmployeeId }); + + // if (!profile) { + // throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); + // } + + // const data = new ProfileGovernment(); + + // 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.govRepo.save(data); + + // return new HttpSuccess(); + // } + + /** + * + * @summary แก้ไขข้อมูลราชการ + * + */ + @Patch("{profileEmployeeId}") + public async editGov( + @Request() req: RequestWithUser, + @Body() body: UpdateProfileGovernment, + @Path() profileEmployeeId: string, + ) { + const record = await this.profileEmployeeRepo.findOne({ + where: { id: profileEmployeeId }, + }); + + if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + + const historyData = new ProfileGovernment(); + + Object.assign(historyData, { ...record, ...body, id: undefined }); + Object.assign(record, body); + record.lastUpdateFullName = req.user.name; + record.lastUpdateFullName = req.user.name; + historyData.profileEmployeeId = profileEmployeeId; + historyData.lastUpdateFullName = req.user.name; + historyData.lastUpdateFullName = req.user.name; + + await Promise.all([this.profileEmployeeRepo.save(record), this.govRepo.save(historyData)]); + + return new HttpSuccess(); + } + + @Delete("{profileEmployeeId}") + public async deleteGov(@Path() profileEmployeeId: string) { + const result = await this.govRepo.delete({ profileEmployeeId: profileEmployeeId }); + + if (result.affected == undefined || result.affected <= 0) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + } + + return new HttpSuccess(); + } +} diff --git a/src/entities/ProfileGovernment.ts b/src/entities/ProfileGovernment.ts index 1b35fcb4..0f675353 100644 --- a/src/entities/ProfileGovernment.ts +++ b/src/entities/ProfileGovernment.ts @@ -1,7 +1,7 @@ -import { Column, Entity, ManyToOne } from "typeorm"; +import { Column, Entity, ManyToOne, JoinColumn } from "typeorm"; import { EntityBase } from "./base/Base"; import { Profile } from "./Profile"; - +import { ProfileEmployee } from "./ProfileEmployee"; @Entity("profileGovernment") export class ProfileGovernment extends EntityBase { @Column({ @@ -12,8 +12,13 @@ export class ProfileGovernment extends EntityBase { }) profileId: string; - @ManyToOne(() => Profile, (v) => v.profileGovernment) - profile: Profile; + @Column({ + nullable: true, + length: 40, + comment: "คีย์นอก(FK)ของตาราง ProfileEmployee", + default: null, + }) + profileEmployeeId: string; @Column({ nullable: true, @@ -52,6 +57,14 @@ export class ProfileGovernment extends EntityBase { default: null, }) reasonSameDate: string; + + @ManyToOne(() => Profile, (v) => v.profileGovernment) + @JoinColumn({ name: "profileId" }) + profile: Profile; + + @ManyToOne(() => ProfileEmployee, (ProfileEmployee) => ProfileEmployee.profileTrainings) + @JoinColumn({ name: "profileEmployeeId" }) + profileEmployee: ProfileEmployee; } export type CreateProfileGovernment = { @@ -61,6 +74,13 @@ export type CreateProfileGovernment = { reasonSameDate?: string | null; }; +export type CreateProfileEmployeeGovernment = { + profileEmployeeId: string; + dateAppoint?: Date | null; + dateStart?: Date | null; + reasonSameDate?: string | null; +}; + export type UpdateProfileGovernment = { dateAppoint?: Date | null; dateStart?: Date | null; From 4671825e8d2270b0beb2897ca371a0dcaa28053f Mon Sep 17 00:00:00 2001 From: Bright Date: Tue, 14 May 2024 12:00:08 +0700 Subject: [PATCH 3/4] =?UTF-8?q?=E0=B9=80=E0=B8=9E=E0=B8=B4=E0=B9=88?= =?UTF-8?q?=E0=B8=A1=E0=B8=9F=E0=B8=B4=E0=B8=A5=E0=B8=94=E0=B9=8C=20Profil?= =?UTF-8?q?eGovernment.profileEmployeeId?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ProfileGovernmentEmployeeController.ts | 191 ++++++++++++++++++ src/entities/ProfileGovernment.ts | 28 ++- 2 files changed, 215 insertions(+), 4 deletions(-) create mode 100644 src/controllers/ProfileGovernmentEmployeeController.ts diff --git a/src/controllers/ProfileGovernmentEmployeeController.ts b/src/controllers/ProfileGovernmentEmployeeController.ts new file mode 100644 index 00000000..b60fbee2 --- /dev/null +++ b/src/controllers/ProfileGovernmentEmployeeController.ts @@ -0,0 +1,191 @@ +import { Body, Controller, Example, Get, Patch, Path, Delete, 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 { CreateProfileEmployeeGovernment, ProfileGovernment, UpdateProfileGovernment } from "../entities/ProfileGovernment"; +import { EmployeePosition } from "../entities/EmployeePosition"; +import { EmployeePosMaster } from "../entities/EmployeePosMaster"; +import { calculateAge, calculateRetireDate } from "../interfaces/utils"; + +@Route("api/v1/org/profile-employee/government") +@Tags("ProfileEmployeeGovernment") +@Security("bearerAuth") +export class ProfileGovernmentEmployeeController extends Controller { + private profileEmployeeRepo = AppDataSource.getRepository(ProfileEmployee); + private govRepo = AppDataSource.getRepository(ProfileGovernment); + private positionRepo = AppDataSource.getRepository(EmployeePosition); + private posMasterRepo = AppDataSource.getRepository(EmployeePosMaster); + + /** + * + * @summary ข้อมูลราชการ + * + */ + @Get("{profileEmployeeId}") + @Example({}) + public async getGovHistory(@Path() profileEmployeeId: string) { + const record = await this.profileEmployeeRepo.findOne({ + where: { id: profileEmployeeId }, + relations: { + posType: true, + posLevel: true, + }, + }); + const posMaster = await this.posMasterRepo.findOne({ + where: { + orgRevision: { + orgRevisionIsCurrent: true, + orgRevisionIsDraft: false, + }, + current_holderId: profileEmployeeId, + }, + order: { createdAt: "DESC" }, + relations: { + orgRoot: true, + orgChild1: true, + orgChild2: true, + orgChild3: true, + orgChild4: true, + }, + }); + const position = await this.positionRepo.findOne({ + where: { + positionIsSelected: true, + posMaster: { + orgRevision: { + orgRevisionIsCurrent: true, + orgRevisionIsDraft: false, + }, + current_holderId: profileEmployeeId, + }, + }, + order: { createdAt: "DESC" }, + }); + + if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + const fullNameParts = [ + posMaster == null || posMaster.orgChild4 == null ? null : posMaster.orgChild4.orgChild4Name, + posMaster == null || posMaster.orgChild3 == null ? null : posMaster.orgChild3.orgChild3Name, + posMaster == null || posMaster.orgChild2 == null ? null : posMaster.orgChild2.orgChild2Name, + posMaster == null || posMaster.orgChild1 == null ? null : posMaster.orgChild1.orgChild1Name, + posMaster == null || posMaster.orgRoot == null ? null : posMaster.orgRoot.orgRootName, + ]; + const org = fullNameParts.filter((part) => part !== undefined && part !== null).join("/"); + let orgShortName = ""; + if (posMaster != null) { + if (posMaster.orgChild1Id === null) { + orgShortName = posMaster.orgRoot?.orgRootShortName; + } else if (posMaster.orgChild2Id === null) { + orgShortName = posMaster.orgChild1?.orgChild1ShortName; + } else if (posMaster.orgChild3Id === null) { + orgShortName = posMaster.orgChild2?.orgChild2ShortName; + } else if (posMaster.orgChild4Id === null) { + orgShortName = posMaster.orgChild3?.orgChild3ShortName; + } else { + orgShortName = posMaster.orgChild4?.orgChild4ShortName; + } + } + const data = { + org: org, //สังกัด + position: record.position, //ตำแหน่ง + posLevel: record.posLevel == null ? null : record.posLevel.posLevelName, //ระดับ + posMasterNo: posMaster == null ? null : `${orgShortName} ${posMaster.posMasterNo}`, //เลขที่ตำแหน่ง + posType: record.posType == null ? null : record.posType.posTypeName, //ประเภท + dateLeave: record.birthDate == null ? null : calculateRetireDate(record.birthDate), //วันเกษียณ + }; + + return new HttpSuccess(data); + } + + /** + * + * @summary ประวัติข้อมูลราชการ + * + */ + @Get("history/{profileEmployeeId}") + @Example({}) + public async govHistory(@Path() profileEmployeeId: string) { + const record = await this.govRepo.find({ + order: { lastUpdatedAt: "DESC" }, + where: { profileEmployeeId: profileEmployeeId }, + }); + + // record.pop(); + + return new HttpSuccess(record); + } + + // @Post() + // public async newGov(@Request() req: RequestWithUser, @Body() body: CreateProfileGovernment) { + // if (!body.profileEmployeeId) { + // throw new HttpError(HttpStatus.BAD_REQUEST, "กรุณากรอก profileEmployeeId"); + // } + + // const profile = await this.profileEmployeeRepo.findOneBy({ id: body.profileEmployeeId }); + + // if (!profile) { + // throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); + // } + + // const data = new ProfileGovernment(); + + // 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.govRepo.save(data); + + // return new HttpSuccess(); + // } + + /** + * + * @summary แก้ไขข้อมูลราชการ + * + */ + @Patch("{profileEmployeeId}") + public async editGov( + @Request() req: RequestWithUser, + @Body() body: UpdateProfileGovernment, + @Path() profileEmployeeId: string, + ) { + const record = await this.profileEmployeeRepo.findOne({ + where: { id: profileEmployeeId }, + }); + + if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + + const historyData = new ProfileGovernment(); + + Object.assign(historyData, { ...record, ...body, id: undefined }); + Object.assign(record, body); + record.lastUpdateFullName = req.user.name; + record.lastUpdateFullName = req.user.name; + historyData.profileEmployeeId = profileEmployeeId; + historyData.lastUpdateFullName = req.user.name; + historyData.lastUpdateFullName = req.user.name; + + await Promise.all([this.profileEmployeeRepo.save(record), this.govRepo.save(historyData)]); + + return new HttpSuccess(); + } + + @Delete("{profileEmployeeId}") + public async deleteGov(@Path() profileEmployeeId: string) { + const result = await this.govRepo.delete({ profileEmployeeId: profileEmployeeId }); + + if (result.affected == undefined || result.affected <= 0) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + } + + return new HttpSuccess(); + } +} diff --git a/src/entities/ProfileGovernment.ts b/src/entities/ProfileGovernment.ts index 1b35fcb4..0f675353 100644 --- a/src/entities/ProfileGovernment.ts +++ b/src/entities/ProfileGovernment.ts @@ -1,7 +1,7 @@ -import { Column, Entity, ManyToOne } from "typeorm"; +import { Column, Entity, ManyToOne, JoinColumn } from "typeorm"; import { EntityBase } from "./base/Base"; import { Profile } from "./Profile"; - +import { ProfileEmployee } from "./ProfileEmployee"; @Entity("profileGovernment") export class ProfileGovernment extends EntityBase { @Column({ @@ -12,8 +12,13 @@ export class ProfileGovernment extends EntityBase { }) profileId: string; - @ManyToOne(() => Profile, (v) => v.profileGovernment) - profile: Profile; + @Column({ + nullable: true, + length: 40, + comment: "คีย์นอก(FK)ของตาราง ProfileEmployee", + default: null, + }) + profileEmployeeId: string; @Column({ nullable: true, @@ -52,6 +57,14 @@ export class ProfileGovernment extends EntityBase { default: null, }) reasonSameDate: string; + + @ManyToOne(() => Profile, (v) => v.profileGovernment) + @JoinColumn({ name: "profileId" }) + profile: Profile; + + @ManyToOne(() => ProfileEmployee, (ProfileEmployee) => ProfileEmployee.profileTrainings) + @JoinColumn({ name: "profileEmployeeId" }) + profileEmployee: ProfileEmployee; } export type CreateProfileGovernment = { @@ -61,6 +74,13 @@ export type CreateProfileGovernment = { reasonSameDate?: string | null; }; +export type CreateProfileEmployeeGovernment = { + profileEmployeeId: string; + dateAppoint?: Date | null; + dateStart?: Date | null; + reasonSameDate?: string | null; +}; + export type UpdateProfileGovernment = { dateAppoint?: Date | null; dateStart?: Date | null; From d2c8a3b74146e2e12f9366a0c71c03a9510892b2 Mon Sep 17 00:00:00 2001 From: Bright Date: Tue, 14 May 2024 13:27:20 +0700 Subject: [PATCH 4/4] =?UTF-8?q?=E0=B8=82=E0=B9=89=E0=B8=AD=E0=B8=A1?= =?UTF-8?q?=E0=B8=B9=E0=B8=A5=E0=B8=A3=E0=B8=B2=E0=B8=8A=E0=B8=81=E0=B8=B2?= =?UTF-8?q?=E0=B8=A3=20(=E0=B8=A5=E0=B8=B9=E0=B8=81=E0=B8=88=E0=B9=89?= =?UTF-8?q?=E0=B8=B2=E0=B8=87)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ProfileGovernmentEmployeeController.ts | 67 +++++++++---------- 1 file changed, 32 insertions(+), 35 deletions(-) diff --git a/src/controllers/ProfileGovernmentEmployeeController.ts b/src/controllers/ProfileGovernmentEmployeeController.ts index b60fbee2..1e09d6c1 100644 --- a/src/controllers/ProfileGovernmentEmployeeController.ts +++ b/src/controllers/ProfileGovernmentEmployeeController.ts @@ -1,4 +1,4 @@ -import { Body, Controller, Example, Get, Patch, Path, Delete, Request, Route, Security, Tags } from "tsoa"; +import { Body, Controller, Example, Get, Patch, Path, Delete, 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"; @@ -96,7 +96,6 @@ export class ProfileGovernmentEmployeeController extends Controller { posType: record.posType == null ? null : record.posType.posTypeName, //ประเภท dateLeave: record.birthDate == null ? null : calculateRetireDate(record.birthDate), //วันเกษียณ }; - return new HttpSuccess(data); } @@ -112,39 +111,37 @@ export class ProfileGovernmentEmployeeController extends Controller { order: { lastUpdatedAt: "DESC" }, where: { profileEmployeeId: profileEmployeeId }, }); - - // record.pop(); - return new HttpSuccess(record); } - // @Post() - // public async newGov(@Request() req: RequestWithUser, @Body() body: CreateProfileGovernment) { - // if (!body.profileEmployeeId) { - // throw new HttpError(HttpStatus.BAD_REQUEST, "กรุณากรอก profileEmployeeId"); - // } + /** + * + * @summary เพิ่มข้อมูลราชการ + * + */ + @Post() + public async newGov(@Request() req: RequestWithUser, @Body() body: CreateProfileEmployeeGovernment) { + if (!body.profileEmployeeId) { + throw new HttpError(HttpStatus.BAD_REQUEST, "กรุณากรอก profileEmployeeId"); + } - // const profile = await this.profileEmployeeRepo.findOneBy({ id: body.profileEmployeeId }); + const profile = await this.profileEmployeeRepo.findOneBy({ id: body.profileEmployeeId }); + if (!profile) { + throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); + } - // if (!profile) { - // throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); - // } + const data = new ProfileGovernment(); + const meta = { + createdUserId: req.user.sub, + createdFullName: req.user.name, + lastUpdateUserId: req.user.sub, + lastUpdateFullName: req.user.name, + }; - // const data = new ProfileGovernment(); - - // 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.govRepo.save(data); - - // return new HttpSuccess(); - // } + Object.assign(data, { ...body, ...meta }); + await this.govRepo.save(data); + return new HttpSuccess(); + } /** * @@ -162,7 +159,6 @@ export class ProfileGovernmentEmployeeController extends Controller { }); if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); - const historyData = new ProfileGovernment(); Object.assign(historyData, { ...record, ...body, id: undefined }); @@ -172,20 +168,21 @@ export class ProfileGovernmentEmployeeController extends Controller { historyData.profileEmployeeId = profileEmployeeId; historyData.lastUpdateFullName = req.user.name; historyData.lastUpdateFullName = req.user.name; - await Promise.all([this.profileEmployeeRepo.save(record), this.govRepo.save(historyData)]); - return new HttpSuccess(); } + /** + * + * @summary ลบข้อมูลราชการ + * + */ @Delete("{profileEmployeeId}") public async deleteGov(@Path() profileEmployeeId: string) { const result = await this.govRepo.delete({ profileEmployeeId: profileEmployeeId }); - if (result.affected == undefined || result.affected <= 0) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); } - return new HttpSuccess(); } -} +} \ No newline at end of file