diff --git a/src/controllers/ProfileGovernmentController.ts b/src/controllers/ProfileGovernmentController.ts index f5019685..44302887 100644 --- a/src/controllers/ProfileGovernmentController.ts +++ b/src/controllers/ProfileGovernmentController.ts @@ -25,6 +25,7 @@ import { } from "../entities/ProfileGovernment"; import { Position } from "../entities/Position"; import { PosMaster } from "../entities/PosMaster"; +import { calculateAge, calculateRetireDate } from "../interfaces/utils"; @Route("api/v1/org/profile/government") @Tags("ProfileGovernment") @@ -40,7 +41,6 @@ export class ProfileGovernmentHistoryController extends Controller { public async getGovHistory(@Path() profileId: string) { const record = await this.profileRepo.findOne({ where: { id: profileId }, - order: { createdAt: "DESC" }, relations: { posType: true, posLevel: true, @@ -116,6 +116,12 @@ export class ProfileGovernmentHistoryController extends Controller { : position.posExecutive.posExecutiveName, //ตำแหน่งทางการบริหาร positionArea: position == null ? null : position.positionArea, //ด้าน/สาขา positionExecutiveField: position == null ? null : position.positionExecutiveField, //ด้านทางการบริหาร + dateLeave: record.birthDate == null ? null : calculateRetireDate(record.birthDate), + govAge: record.dateStart == null ? null : calculateAge(record.dateStart), + dateAppoint: record.dateAppoint, + dateStart: record.dateStart, + govAgeAbsent: record.govAgeAbsent, + govAgePlus: record.govAgePlus, }; return new HttpSuccess(data); @@ -134,33 +140,33 @@ export class ProfileGovernmentHistoryController extends Controller { return new HttpSuccess(record); } - @Post() - public async newGov(@Request() req: RequestWithUser, @Body() body: CreateProfileGovernment) { - if (!body.profileId) { - throw new HttpError(HttpStatus.BAD_REQUEST, "กรุณากรอก profileId"); - } + // @Post() + // public async newGov(@Request() req: RequestWithUser, @Body() body: CreateProfileGovernment) { + // if (!body.profileId) { + // throw new HttpError(HttpStatus.BAD_REQUEST, "กรุณากรอก profileId"); + // } - const profile = await this.profileRepo.findOneBy({ id: body.profileId }); + // const profile = await this.profileRepo.findOneBy({ id: body.profileId }); - if (!profile) { - throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); - } + // if (!profile) { + // throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); + // } - const data = new ProfileGovernment(); + // const data = new ProfileGovernment(); - const meta = { - createdUserId: req.user.sub, - createdFullName: req.user.name, - lastUpdateUserId: req.user.sub, - lastUpdateFullName: req.user.name, - }; + // const meta = { + // createdUserId: req.user.sub, + // createdFullName: req.user.name, + // lastUpdateUserId: req.user.sub, + // lastUpdateFullName: req.user.name, + // }; - Object.assign(data, { ...body, ...meta }); + // Object.assign(data, { ...body, ...meta }); - await this.govRepo.save(data); + // await this.govRepo.save(data); - return new HttpSuccess(); - } + // return new HttpSuccess(); + // } @Patch("{profileId}") public async editGov( @@ -168,35 +174,34 @@ export class ProfileGovernmentHistoryController extends Controller { @Body() body: UpdateProfileGovernment, @Path() profileId: string, ) { - const record = ( - await this.govRepo.find({ - take: 1, - order: { lastUpdatedAt: "DESC" }, - where: { profileId }, - }) - )[0]; + const record = await this.profileRepo.findOne({ + where: { id: profileId }, + }); 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.lastUpdateFullName = req.user.name; historyData.lastUpdateFullName = req.user.name; - await this.govRepo.save(historyData); + await Promise.all([this.profileRepo.save(record)]); return new HttpSuccess(); } - @Delete("{profileId}") - public async deleteGov(@Path() profileId: string) { - const result = await this.govRepo.delete({ profileId: profileId }); + // @Delete("{profileId}") + // public async deleteGov(@Path() profileId: string) { + // const result = await this.govRepo.delete({ profileId: profileId }); - if (result.affected && result.affected <= 0) { - throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); - } + // if (result.affected && result.affected <= 0) { + // throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + // } - return new HttpSuccess(); - } + // return new HttpSuccess(); + // } } diff --git a/src/entities/Profile.ts b/src/entities/Profile.ts index 7c654d69..f1e54cf4 100644 --- a/src/entities/Profile.ts +++ b/src/entities/Profile.ts @@ -147,6 +147,36 @@ export class Profile extends EntityBase { }) dateRetire: Date; + @Column({ + nullable: true, + type: "datetime", + comment: "วันที่บรรจุ", + default: null, + }) + dateAppoint: Date; + + @Column({ + nullable: true, + type: "datetime", + comment: "วันที่เริ่มปฏิบัติราชการ", + default: null, + }) + dateStart: Date; + + @Column({ + nullable: true, + comment: "ขาดราชการ", + default: null, + }) + govAgeAbsent: number; + + @Column({ + nullable: true, + comment: "อายุราชการเกื้อกูล", + default: null, + }) + govAgePlus: number; + @Column({ nullable: true, type: "datetime", diff --git a/src/entities/ProfileGovernment.ts b/src/entities/ProfileGovernment.ts index 0df081d7..d577b2dd 100644 --- a/src/entities/ProfileGovernment.ts +++ b/src/entities/ProfileGovernment.ts @@ -17,216 +17,42 @@ export class ProfileGovernment extends EntityBase { @Column({ nullable: true, - length: 40, + type: "datetime", + comment: "วันที่บรรจุ", default: null, - comment: "เลขที่ตำแหน่ง", - }) - posNoId: string; - - @Column({ - nullable: true, - length: 40, - default: null, - comment: "สังกัด", - }) - ocId: string; - - @Column({ - nullable: true, - length: 40, - default: null, - comment: "คีย์นอก(FK)ของตาราง Position", - }) - positionId: string; - - @Column({ - nullable: true, - default: null, - comment: "วันที่สั่งบรรจุ", }) dateAppoint: Date; @Column({ nullable: true, + type: "datetime", + comment: "วันที่เริ่มปฏิบัติราชการ", default: null, - comment: "เริ่มปฎิบัติราชการ", }) dateStart: Date; @Column({ nullable: true, - default: null, - comment: "วันเกษียณอายุ", - }) - retireDate: Date; - - @Column({ - nullable: true, - default: null, - comment: "อายุราชการ", - }) - govAge: string; - - @Column({ - nullable: true, - default: null, comment: "ขาดราชการ", + default: null, }) - govAgeAbsent: string; + govAgeAbsent: number; @Column({ nullable: true, - default: null, comment: "อายุราชการเกื้อกูล", - }) - govAgePlus: string; - - @Column({ - nullable: true, - default: null, - comment: "สังกัด", - }) - oc: string; - - @Column({ - nullable: true, - default: null, - comment: "เลขที่ตำแหน่ง", - }) - posNo: string; - - @Column({ - nullable: true, - default: null, - comment: "ตำแหน่ง", - }) - position: string; - - @Column({ - nullable: true, - default: null, - comment: "ระดับตำแหน่ง", - }) - positionLevel: string; - - @Column({ - nullable: true, - default: null, - comment: "สายงาน", - }) - positionLine: string; - - @Column({ - nullable: true, - default: null, - comment: "ประเภทตำแหน่ง", - }) - positionType: string; - - @Column({ - nullable: true, - default: null, - comment: "ตำแหน่งทางการบริหาร", - }) - positionExecutive: string; - - @Column({ - nullable: true, - default: null, - comment: "เหตุผลกรณีไม่ตรงวัน", - }) - reasonSameDate: string; - - @Column({ - nullable: true, - default: null, - comment: "กลุ่มงาน", - }) - positionEmployeeGroup: string; - - @Column({ - nullable: true, - default: null, - comment: "ระดับชั้นงาน", - }) - positionEmployeeLevel: string; - - @Column({ - nullable: true, - default: null, - comment: "ตำแหน่ง", - }) - positionEmployeePosition: string; - - @Column({ - nullable: true, - default: null, - comment: "ด้านของตำแหน่ง", - }) - positionEmployeePositionSide: string; - - @Column({ - nullable: true, default: null, }) - positionPathSide: string; - - @Column({ - nullable: true, - default: null, - }) - positionExecutiveSide: string; + govAgePlus: number; } export type CreateProfileGovernment = { profileId: string; - positionId: string | null; - posNoId: string | null; - ocId: string | null; - dateAppoint: Date | null; - dateStart: Date | null; - retireDate: Date | null; - govAge: string | null; - govAgeAbsent: string | null; - govAgePlus: string | null; - oc: string | null; - posNo: string | null; - postition: string | null; - positionLevel: string | null; - positionLine: string | null; - positionType: string | null; - positionExecutive: string | null; - reasonSameDate: string | null; - positionEmployeeGroup: string | null; - positionEmployeeLevel: string | null; - positionEmployeePosition: string | null; - positionEmployeePositionSide: string | null; - positionPathSide: string | null; - positionExecutiveSide: string | null; + dateAppoint?: Date | null; + dateStart?: Date | null; }; export type UpdateProfileGovernment = { - positionId?: string | null; - posNoId?: string | null; - ocId?: string | null; dateAppoint?: Date | null; dateStart?: Date | null; - retireDate?: Date | null; - govAge?: string | null; - govAgeAbsent?: string | null; - govAgePlus?: string | null; - oc?: string | null; - posNo?: string | null; - postition?: string | null; - positionLevel?: string | null; - positionLine?: string | null; - positionType?: string | null; - positionExecutive?: string | null; - reasonSameDate?: string | null; - positionEmployeeGroup?: string | null; - positionEmployeeLevel?: string | null; - positionEmployeePosition?: string | null; - positionEmployeePositionSide?: string | null; - positionPathSide?: string | null; - positionExecutiveSide?: string | null; }; diff --git a/src/migration/1711513058675-update_table_profilegov_add_dateAppoint.ts b/src/migration/1711513058675-update_table_profilegov_add_dateAppoint.ts new file mode 100644 index 00000000..4172b10b --- /dev/null +++ b/src/migration/1711513058675-update_table_profilegov_add_dateAppoint.ts @@ -0,0 +1,78 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class UpdateTableProfilegovAddDateAppoint1711513058675 implements MigrationInterface { + name = 'UpdateTableProfilegovAddDateAppoint1711513058675' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`profileGovernment\` DROP COLUMN \`govAge\``); + await queryRunner.query(`ALTER TABLE \`profileGovernment\` DROP COLUMN \`oc\``); + await queryRunner.query(`ALTER TABLE \`profileGovernment\` DROP COLUMN \`ocId\``); + await queryRunner.query(`ALTER TABLE \`profileGovernment\` DROP COLUMN \`position\``); + await queryRunner.query(`ALTER TABLE \`profileGovernment\` DROP COLUMN \`positionEmployeeGroup\``); + await queryRunner.query(`ALTER TABLE \`profileGovernment\` DROP COLUMN \`positionEmployeeLevel\``); + await queryRunner.query(`ALTER TABLE \`profileGovernment\` DROP COLUMN \`positionEmployeePosition\``); + await queryRunner.query(`ALTER TABLE \`profileGovernment\` DROP COLUMN \`positionEmployeePositionSide\``); + await queryRunner.query(`ALTER TABLE \`profileGovernment\` DROP COLUMN \`positionExecutive\``); + await queryRunner.query(`ALTER TABLE \`profileGovernment\` DROP COLUMN \`positionExecutiveSide\``); + await queryRunner.query(`ALTER TABLE \`profileGovernment\` DROP COLUMN \`positionId\``); + await queryRunner.query(`ALTER TABLE \`profileGovernment\` DROP COLUMN \`positionLevel\``); + await queryRunner.query(`ALTER TABLE \`profileGovernment\` DROP COLUMN \`positionLine\``); + await queryRunner.query(`ALTER TABLE \`profileGovernment\` DROP COLUMN \`positionPathSide\``); + await queryRunner.query(`ALTER TABLE \`profileGovernment\` DROP COLUMN \`positionType\``); + await queryRunner.query(`ALTER TABLE \`profileGovernment\` DROP COLUMN \`posNo\``); + await queryRunner.query(`ALTER TABLE \`profileGovernment\` DROP COLUMN \`posNoId\``); + await queryRunner.query(`ALTER TABLE \`profileGovernment\` DROP COLUMN \`reasonSameDate\``); + await queryRunner.query(`ALTER TABLE \`profileGovernment\` DROP COLUMN \`retireDate\``); + await queryRunner.query(`ALTER TABLE \`profile\` ADD \`dateAppoint\` datetime NULL COMMENT 'วันที่บรรจุ'`); + await queryRunner.query(`ALTER TABLE \`profile\` ADD \`dateStart\` datetime NULL COMMENT 'วันที่เริ่มปฏิบัติราชการ'`); + await queryRunner.query(`ALTER TABLE \`profile\` ADD \`govAgeAbsent\` int NULL COMMENT 'ขาดราชการ'`); + await queryRunner.query(`ALTER TABLE \`profile\` ADD \`govAgePlus\` int NULL COMMENT 'อายุราชการเกื้อกูล'`); + await queryRunner.query(`ALTER TABLE \`profileHistory\` ADD \`dateAppoint\` datetime NULL COMMENT 'วันที่บรรจุ'`); + await queryRunner.query(`ALTER TABLE \`profileHistory\` ADD \`dateStart\` datetime NULL COMMENT 'วันที่เริ่มปฏิบัติราชการ'`); + await queryRunner.query(`ALTER TABLE \`profileHistory\` ADD \`govAgeAbsent\` int NULL COMMENT 'ขาดราชการ'`); + await queryRunner.query(`ALTER TABLE \`profileHistory\` ADD \`govAgePlus\` int NULL COMMENT 'อายุราชการเกื้อกูล'`); + await queryRunner.query(`ALTER TABLE \`profileGovernment\` CHANGE \`dateAppoint\` \`dateAppoint\` datetime NULL COMMENT 'วันที่บรรจุ'`); + await queryRunner.query(`ALTER TABLE \`profileGovernment\` CHANGE \`dateStart\` \`dateStart\` datetime NULL COMMENT 'วันที่เริ่มปฏิบัติราชการ'`); + await queryRunner.query(`ALTER TABLE \`profileGovernment\` DROP COLUMN \`govAgeAbsent\``); + await queryRunner.query(`ALTER TABLE \`profileGovernment\` ADD \`govAgeAbsent\` int NULL COMMENT 'ขาดราชการ'`); + await queryRunner.query(`ALTER TABLE \`profileGovernment\` DROP COLUMN \`govAgePlus\``); + await queryRunner.query(`ALTER TABLE \`profileGovernment\` ADD \`govAgePlus\` int NULL COMMENT 'อายุราชการเกื้อกูล'`); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`profileGovernment\` DROP COLUMN \`govAgePlus\``); + await queryRunner.query(`ALTER TABLE \`profileGovernment\` ADD \`govAgePlus\` varchar(255) NULL COMMENT 'อายุราชการเกื้อกูล'`); + await queryRunner.query(`ALTER TABLE \`profileGovernment\` DROP COLUMN \`govAgeAbsent\``); + await queryRunner.query(`ALTER TABLE \`profileGovernment\` ADD \`govAgeAbsent\` varchar(255) NULL COMMENT 'ขาดราชการ'`); + await queryRunner.query(`ALTER TABLE \`profileGovernment\` CHANGE \`dateStart\` \`dateStart\` datetime NULL COMMENT 'เริ่มปฎิบัติราชการ'`); + await queryRunner.query(`ALTER TABLE \`profileGovernment\` CHANGE \`dateAppoint\` \`dateAppoint\` datetime NULL COMMENT 'วันที่สั่งบรรจุ'`); + await queryRunner.query(`ALTER TABLE \`profileHistory\` DROP COLUMN \`govAgePlus\``); + await queryRunner.query(`ALTER TABLE \`profileHistory\` DROP COLUMN \`govAgeAbsent\``); + await queryRunner.query(`ALTER TABLE \`profileHistory\` DROP COLUMN \`dateStart\``); + await queryRunner.query(`ALTER TABLE \`profileHistory\` DROP COLUMN \`dateAppoint\``); + await queryRunner.query(`ALTER TABLE \`profile\` DROP COLUMN \`govAgePlus\``); + await queryRunner.query(`ALTER TABLE \`profile\` DROP COLUMN \`govAgeAbsent\``); + await queryRunner.query(`ALTER TABLE \`profile\` DROP COLUMN \`dateStart\``); + await queryRunner.query(`ALTER TABLE \`profile\` DROP COLUMN \`dateAppoint\``); + await queryRunner.query(`ALTER TABLE \`profileGovernment\` ADD \`retireDate\` datetime NULL COMMENT 'วันเกษียณอายุ'`); + await queryRunner.query(`ALTER TABLE \`profileGovernment\` ADD \`reasonSameDate\` varchar(255) NULL COMMENT 'เหตุผลกรณีไม่ตรงวัน'`); + await queryRunner.query(`ALTER TABLE \`profileGovernment\` ADD \`posNoId\` varchar(40) NULL COMMENT 'เลขที่ตำแหน่ง'`); + await queryRunner.query(`ALTER TABLE \`profileGovernment\` ADD \`posNo\` varchar(255) NULL COMMENT 'เลขที่ตำแหน่ง'`); + await queryRunner.query(`ALTER TABLE \`profileGovernment\` ADD \`positionType\` varchar(255) NULL COMMENT 'ประเภทตำแหน่ง'`); + await queryRunner.query(`ALTER TABLE \`profileGovernment\` ADD \`positionPathSide\` varchar(255) NULL`); + await queryRunner.query(`ALTER TABLE \`profileGovernment\` ADD \`positionLine\` varchar(255) NULL COMMENT 'สายงาน'`); + await queryRunner.query(`ALTER TABLE \`profileGovernment\` ADD \`positionLevel\` varchar(255) NULL COMMENT 'ระดับตำแหน่ง'`); + await queryRunner.query(`ALTER TABLE \`profileGovernment\` ADD \`positionId\` varchar(40) NULL COMMENT 'คีย์นอก(FK)ของตาราง Position'`); + await queryRunner.query(`ALTER TABLE \`profileGovernment\` ADD \`positionExecutiveSide\` varchar(255) NULL`); + await queryRunner.query(`ALTER TABLE \`profileGovernment\` ADD \`positionExecutive\` varchar(255) NULL COMMENT 'ตำแหน่งทางการบริหาร'`); + await queryRunner.query(`ALTER TABLE \`profileGovernment\` ADD \`positionEmployeePositionSide\` varchar(255) NULL COMMENT 'ด้านของตำแหน่ง'`); + await queryRunner.query(`ALTER TABLE \`profileGovernment\` ADD \`positionEmployeePosition\` varchar(255) NULL COMMENT 'ตำแหน่ง'`); + await queryRunner.query(`ALTER TABLE \`profileGovernment\` ADD \`positionEmployeeLevel\` varchar(255) NULL COMMENT 'ระดับชั้นงาน'`); + await queryRunner.query(`ALTER TABLE \`profileGovernment\` ADD \`positionEmployeeGroup\` varchar(255) NULL COMMENT 'กลุ่มงาน'`); + await queryRunner.query(`ALTER TABLE \`profileGovernment\` ADD \`position\` varchar(255) NULL COMMENT 'ตำแหน่ง'`); + await queryRunner.query(`ALTER TABLE \`profileGovernment\` ADD \`ocId\` varchar(40) NULL COMMENT 'สังกัด'`); + await queryRunner.query(`ALTER TABLE \`profileGovernment\` ADD \`oc\` varchar(255) NULL COMMENT 'สังกัด'`); + await queryRunner.query(`ALTER TABLE \`profileGovernment\` ADD \`govAge\` varchar(255) NULL COMMENT 'อายุราชการ'`); + } + +}