From 44022b311f5bc08dc4c8015f44cad53cb864996e Mon Sep 17 00:00:00 2001 From: Kittapath Date: Wed, 3 Apr 2024 12:07:57 +0700 Subject: [PATCH] =?UTF-8?q?=E0=B9=80=E0=B8=9E=E0=B8=B4=E0=B9=88=E0=B8=A1?= =?UTF-8?q?=E0=B8=9B=E0=B8=A3=E0=B8=B0=E0=B8=A7=E0=B8=B1=E0=B8=95=E0=B8=B4?= =?UTF-8?q?=E0=B8=A5=E0=B8=B9=E0=B8=81=E0=B8=88=E0=B9=89=E0=B8=B2=E0=B8=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DevelopmentEmployeeHistoryController.ts | 226 ++++++++++++++++++ .../DevelopmentHistoryController.ts | 38 ++- src/entities/DevelopmentHistory.ts | 24 ++ src/entities/EmployeePosLevel.ts | 6 +- src/entities/EmployeePosType.ts | 4 + ...pdate_table_developmentHistory_add_type.ts | 20 ++ 6 files changed, 316 insertions(+), 2 deletions(-) create mode 100644 src/controllers/DevelopmentEmployeeHistoryController.ts create mode 100644 src/migration/1712120794817-update_table_developmentHistory_add_type.ts diff --git a/src/controllers/DevelopmentEmployeeHistoryController.ts b/src/controllers/DevelopmentEmployeeHistoryController.ts new file mode 100644 index 0000000..4fcdb41 --- /dev/null +++ b/src/controllers/DevelopmentEmployeeHistoryController.ts @@ -0,0 +1,226 @@ +import { + Controller, + Get, + Post, + Put, + Delete, + Route, + Security, + Tags, + Body, + Path, + Request, + Query, +} from "tsoa"; +import { AppDataSource } from "../database/data-source"; +import { Not } from "typeorm"; +import HttpSuccess from "../interfaces/http-success"; +import HttpError from "../interfaces/http-error"; +import HttpStatusCode from "../interfaces/http-status"; +import { Development } from "../entities/Development"; +import { + CreateDevelopmentHistory, + DevelopmentHistory, + UpdateDevelopmentHistory, +} from "../entities/DevelopmentHistory"; +import { EmployeePosType } from "../entities/EmployeePosType"; +import { EmployeePosLevel } from "../entities/EmployeePosLevel"; + +@Route("api/v1/development/history/employee") +@Tags("DevelopmentEmployeeHistory") +@Security("bearerAuth") +export class DevelopmentHistoryController extends Controller { + private developmentHistoryRepository = AppDataSource.getRepository(DevelopmentHistory); + private developmentRepository = AppDataSource.getRepository(Development); + private posTypeRepository = AppDataSource.getRepository(EmployeePosType); + private posLevelRepository = AppDataSource.getRepository(EmployeePosLevel); + + /** + * API เพิ่มประวัติการฝึกอบรม/ดูงาน + * + * @summary DEV_006 - เพิ่มประวัติการฝึกอบรม/ดูงาน#6 + * + */ + @Post() + async CreateDevelopmentHistory( + @Body() requestBody: CreateDevelopmentHistory, + @Request() request: { user: Record }, + ) { + const type = "EMPLOYEE"; + const chk_name = await this.developmentHistoryRepository.find({ + where: { + citizenId: requestBody.citizenId, + developmentId: requestBody.developmentId, + type: type, + }, + }); + if (chk_name.length > 0) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ประวัติการฝึกอบรม/ดูงาน มีอยู่ในระบบแล้ว"); + } + + const checkId = await this.developmentRepository.findOne({ + where: { id: requestBody.developmentId }, + }); + if (!checkId) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรม"); + } + if (requestBody.posTypeId != null) { + const checkId = await this.posTypeRepository.findOne({ + where: { id: requestBody.posTypeId }, + }); + if (!checkId) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่ง"); + } + } + if (requestBody.posLevelId != null) { + const checkId = await this.posLevelRepository.findOne({ + where: { id: requestBody.posLevelId }, + }); + if (!checkId) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับตำแหน่ง"); + } + } + + const development = Object.assign(new DevelopmentHistory(), requestBody); + development.type = type; + development.employeePosTypeId = requestBody.posTypeId; + development.employeePosLevelId = requestBody.posLevelId; + development.createdUserId = request.user.sub; + development.createdFullName = request.user.name; + development.lastUpdateUserId = request.user.sub; + development.lastUpdateFullName = request.user.name; + await this.developmentHistoryRepository.save(development); + return new HttpSuccess(development.id); + } + + /** + * API แก้ไขประวัติการฝึกอบรม/ดูงาน + * + * @summary DEV_007 - แก้ไขประวัติการฝึกอบรม/ดูงาน #7 + * + * @param {string} id Id โครงการ + */ + @Put("{id}") + async UpdateDevelopmentHistory( + @Path() id: string, + @Body() requestBody: UpdateDevelopmentHistory, + @Request() request: { user: Record }, + ) { + const type = "EMPLOYEE"; + const development = await this.developmentHistoryRepository.findOne({ + where: { id: id, type: type }, + }); + if (!development) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลประวัติการฝึกอบรม/ดูงานนี้"); + } + const chk_name = await this.developmentHistoryRepository.find({ + where: { + citizenId: requestBody.citizenId, + developmentId: requestBody.developmentId, + type: type, + id: Not(id), + }, + }); + if (chk_name.length > 0) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ประวัติการฝึกอบรม/ดูงาน มีอยู่ในระบบแล้ว"); + } + const checkId = await this.developmentRepository.findOne({ + where: { id: requestBody.developmentId }, + }); + if (!checkId) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรม"); + } + if (requestBody.posTypeId != null) { + const checkId = await this.posTypeRepository.findOne({ + where: { id: requestBody.posTypeId }, + }); + if (!checkId) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่ง"); + } + } + if (requestBody.posLevelId != null) { + const checkId = await this.posLevelRepository.findOne({ + where: { id: requestBody.posLevelId }, + }); + if (!checkId) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับตำแหน่ง"); + } + } + Object.assign(development, requestBody); + development.type = type; + development.employeePosTypeId = requestBody.posTypeId; + development.employeePosLevelId = requestBody.posLevelId; + development.lastUpdateUserId = request.user.sub; + development.lastUpdateFullName = request.user.name; + await this.developmentHistoryRepository.save(development); + return new HttpSuccess(development.id); + } + + /** + * API ลบประวัติการฝึกอบรม/ดูงาน + * + * @summary DEV_008 - ลบประวัติการฝึกอบรม/ดูงาน #8 + * + * @param {string} id Id โครงการ + */ + @Delete("{id}") + async DeleteDevelopmentHistory(@Path() id: string) { + const type = "EMPLOYEE"; + const development = await this.developmentHistoryRepository.findOne({ + where: { id: id, type: type }, + }); + if (!development) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลประวัติการฝึกอบรม/ดูงานนี้"); + } + + await this.developmentHistoryRepository.remove(development); + return new HttpSuccess(); + } + + /** + * API รายการประวัติการฝึกอบรม/ดูงาน + * + * @summary DEV_009 - รายการประวัติการฝึกอบรม/ดูงาน #9 + * + */ + @Get() + async GetDevelopmentHistoryLists( + @Query("page") page: number = 1, + @Query("pageSize") pageSize: number = 10, + @Query("keyword") keyword?: string, + @Query("year") year?: number, + ) { + const type = "EMPLOYEE"; + const [development, total] = await AppDataSource.getRepository(DevelopmentHistory) + .createQueryBuilder("developmentHistory") + // .andWhere(year == null ? "developmentHistory.year LIKE :year" : "1=1", { year: `${year}` }) + // .orWhere("developmentHistory.projectName LIKE :keyword", { keyword: `${keyword}` }) + // .select(["development.id", "development.projectName", "development.year"]) + // .orderBy("developmentHistory.year", "DESC") + .orderBy("developmentHistory.createdAt", "DESC") + .skip((page - 1) * pageSize) + .take(pageSize) + .getManyAndCount(); + + return new HttpSuccess({ data: development, total }); + } + + /** + * API รายละเอียดประวัติการฝึกอบรม/ดูงาน + * + * @summary DEV_010 - รายละเอียดประวัติการฝึกอบรม/ดูงาน #10 + * + * @param {string} id Id โครงการ + */ + @Get("{id}") + async GetDevelopemtHistoryById(@Path() id: string) { + const type = "EMPLOYEE"; + const getDevelopment = await this.developmentHistoryRepository.findOne({ + where: { id: id, type: type }, + }); + if (!getDevelopment) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลประวัติการฝึกอบรม/ดูงานนี้"); + } + return new HttpSuccess(getDevelopment); + } +} diff --git a/src/controllers/DevelopmentHistoryController.ts b/src/controllers/DevelopmentHistoryController.ts index c9ca194..adc053b 100644 --- a/src/controllers/DevelopmentHistoryController.ts +++ b/src/controllers/DevelopmentHistoryController.ts @@ -23,13 +23,17 @@ import { DevelopmentHistory, UpdateDevelopmentHistory, } from "../entities/DevelopmentHistory"; +import { PosType } from "../entities/PosType"; +import { PosLevel } from "../entities/PosLevel"; @Route("api/v1/development/history/officer") -@Tags("DevelopmentHistory") +@Tags("DevelopmentOfficerHistory") @Security("bearerAuth") export class DevelopmentHistoryController extends Controller { private developmentHistoryRepository = AppDataSource.getRepository(DevelopmentHistory); private developmentRepository = AppDataSource.getRepository(Development); + private posTypeRepository = AppDataSource.getRepository(PosType); + private posLevelRepository = AppDataSource.getRepository(PosLevel); /** * API เพิ่มประวัติการฝึกอบรม/ดูงาน @@ -60,6 +64,22 @@ export class DevelopmentHistoryController extends Controller { if (!checkId) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรม"); } + if (requestBody.posTypeId != null) { + const checkId = await this.posTypeRepository.findOne({ + where: { id: requestBody.posTypeId }, + }); + if (!checkId) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่ง"); + } + } + if (requestBody.posLevelId != null) { + const checkId = await this.posLevelRepository.findOne({ + where: { id: requestBody.posLevelId }, + }); + if (!checkId) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับตำแหน่ง"); + } + } const development = Object.assign(new DevelopmentHistory(), requestBody); development.type = type; @@ -108,6 +128,22 @@ export class DevelopmentHistoryController extends Controller { if (!checkId) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงการ/หลักสูตรการฝึกอบรม"); } + if (requestBody.posTypeId != null) { + const checkId = await this.posTypeRepository.findOne({ + where: { id: requestBody.posTypeId }, + }); + if (!checkId) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่ง"); + } + } + if (requestBody.posLevelId != null) { + const checkId = await this.posLevelRepository.findOne({ + where: { id: requestBody.posLevelId }, + }); + if (!checkId) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับตำแหน่ง"); + } + } Object.assign(development, requestBody); development.type = type; development.lastUpdateUserId = request.user.sub; diff --git a/src/entities/DevelopmentHistory.ts b/src/entities/DevelopmentHistory.ts index 306e645..12b4f6a 100644 --- a/src/entities/DevelopmentHistory.ts +++ b/src/entities/DevelopmentHistory.ts @@ -3,6 +3,8 @@ import { EntityBase } from "./base/Base"; import { PosLevel } from "./PosLevel"; import { PosType } from "./PosType"; import { Development } from "./Development"; +import { EmployeePosType } from "./EmployeePosType"; +import { EmployeePosLevel } from "./EmployeePosLevel"; @Entity("developmentHistory") export class DevelopmentHistory extends EntityBase { @@ -84,6 +86,28 @@ export class DevelopmentHistory extends EntityBase { @JoinColumn({ name: "posTypeId" }) posType: PosType; + @Column({ + nullable: true, + length: 40, + comment: "ไอดีระดับตำแหน่ง", + }) + employeePosLevelId: string | null; + + @ManyToOne(() => EmployeePosLevel, (employeePosLevel) => employeePosLevel.developmentHistorys) + @JoinColumn({ name: "employeePosLevelId" }) + employeePosLevel: EmployeePosLevel; + + @Column({ + nullable: true, + length: 40, + comment: "ไอดีประเภทตำแหน่ง", + }) + employeePosTypeId: string | null; + + @ManyToOne(() => EmployeePosType, (employeePosType) => employeePosType.developmentHistorys) + @JoinColumn({ name: "employeePosTypeId" }) + employeePosType: EmployeePosType; + @Column({ nullable: true, comment: "โครงการ/หลักสูตรการฝึกอบรม", diff --git a/src/entities/EmployeePosLevel.ts b/src/entities/EmployeePosLevel.ts index c84e309..e84960f 100644 --- a/src/entities/EmployeePosLevel.ts +++ b/src/entities/EmployeePosLevel.ts @@ -1,6 +1,7 @@ -import { Entity, Column, ManyToOne, JoinColumn } from "typeorm"; +import { Entity, Column, ManyToOne, JoinColumn, OneToMany } from "typeorm"; import { EntityBase } from "./base/Base"; import { EmployeePosType } from "./EmployeePosType"; +import { DevelopmentHistory } from "./DevelopmentHistory"; enum EmployeePosLevelAuthoritys { HEAD = "HEAD", @@ -40,6 +41,9 @@ export class EmployeePosLevel extends EntityBase { @ManyToOne(() => EmployeePosType, (posType: EmployeePosType) => posType.posLevels) @JoinColumn({ name: "posTypeId" }) posType: EmployeePosType; + + @OneToMany(() => DevelopmentHistory, (developmentHistory) => developmentHistory.employeePosLevel) + developmentHistorys: DevelopmentHistory[]; } export class CreateEmployeePosLevel { diff --git a/src/entities/EmployeePosType.ts b/src/entities/EmployeePosType.ts index 99b1cdb..cafb49d 100644 --- a/src/entities/EmployeePosType.ts +++ b/src/entities/EmployeePosType.ts @@ -1,6 +1,7 @@ import { Entity, Column, OneToMany } from "typeorm"; import { EntityBase } from "./base/Base"; import { EmployeePosLevel } from "./EmployeePosLevel"; +import { DevelopmentHistory } from "./DevelopmentHistory"; @Entity("employeePosType") export class EmployeePosType extends EntityBase { @@ -27,6 +28,9 @@ export class EmployeePosType extends EntityBase { @OneToMany(() => EmployeePosLevel, (posLevel: EmployeePosLevel) => posLevel.posType) posLevels: EmployeePosLevel[]; + + @OneToMany(() => DevelopmentHistory, (developmentHistory) => developmentHistory.employeePosType) + developmentHistorys: DevelopmentHistory[]; } export class CreateEmployeePosType { diff --git a/src/migration/1712120794817-update_table_developmentHistory_add_type.ts b/src/migration/1712120794817-update_table_developmentHistory_add_type.ts new file mode 100644 index 0000000..52e2b57 --- /dev/null +++ b/src/migration/1712120794817-update_table_developmentHistory_add_type.ts @@ -0,0 +1,20 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class UpdateTableDevelopmentHistoryAddType1712120794817 implements MigrationInterface { + name = 'UpdateTableDevelopmentHistoryAddType1712120794817' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`developmentHistory\` ADD \`employeePosLevelId\` varchar(40) NULL COMMENT 'ไอดีระดับตำแหน่ง'`); + await queryRunner.query(`ALTER TABLE \`developmentHistory\` ADD \`employeePosTypeId\` varchar(40) NULL COMMENT 'ไอดีประเภทตำแหน่ง'`); + await queryRunner.query(`ALTER TABLE \`developmentHistory\` ADD CONSTRAINT \`FK_a905f077069e27d2fc9bb8f5f5c\` FOREIGN KEY (\`employeePosLevelId\`) REFERENCES \`employeePosLevel\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`); + await queryRunner.query(`ALTER TABLE \`developmentHistory\` ADD CONSTRAINT \`FK_25dd3134b725bb2ec455872374f\` FOREIGN KEY (\`employeePosTypeId\`) REFERENCES \`employeePosType\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`developmentHistory\` DROP FOREIGN KEY \`FK_25dd3134b725bb2ec455872374f\``); + await queryRunner.query(`ALTER TABLE \`developmentHistory\` DROP FOREIGN KEY \`FK_a905f077069e27d2fc9bb8f5f5c\``); + await queryRunner.query(`ALTER TABLE \`developmentHistory\` DROP COLUMN \`employeePosTypeId\``); + await queryRunner.query(`ALTER TABLE \`developmentHistory\` DROP COLUMN \`employeePosLevelId\``); + } + +}