From 5e117d1703ead82bda11e87f7f9c770d8836f448 Mon Sep 17 00:00:00 2001 From: waruneeauy Date: Wed, 16 Oct 2024 21:39:37 +0700 Subject: [PATCH] updated appoint chairman/committee --- src/controllers/AppointController.ts | 174 +++++++++++++++++++++++++ src/entities/Appoint.ts | 61 +++++++++ src/entities/AppointDirector.ts | 58 +++++++++ src/migration/1729088301267-appoint.ts | 25 ++++ 4 files changed, 318 insertions(+) create mode 100644 src/controllers/AppointController.ts create mode 100644 src/entities/Appoint.ts create mode 100644 src/entities/AppointDirector.ts create mode 100644 src/migration/1729088301267-appoint.ts diff --git a/src/controllers/AppointController.ts b/src/controllers/AppointController.ts new file mode 100644 index 0000000..8883788 --- /dev/null +++ b/src/controllers/AppointController.ts @@ -0,0 +1,174 @@ +import { + Controller, + Route, + Security, + Tags, + Body, + Request, + SuccessResponse, + Response, + Get, + Query, + Put, + Post, + Path, + Delete, +} from "tsoa"; +import { setLogDataDiff } from "../interfaces/utils"; + +import { AppDataSource } from "../database/data-source"; +import HttpSuccess from "../interfaces/http-success"; +import HttpStatusCode from "../interfaces/http-status"; +import HttpError from "../interfaces/http-error"; +import permission from "../interfaces/permission"; +import { RequestWithUser } from "../middlewares/user"; +import { Appoint, CreateAppoint, Person, UpdateAppoint } from "../entities/Appoint"; +import { AppointDirector } from "../entities/AppointDirector"; +import HttpStatus from "../interfaces/http-status"; + +@Route("api/v1/probation/appoint") +@Tags("Appoint Director") +@Security("bearerAuth") +@Response( + HttpStatusCode.INTERNAL_SERVER_ERROR, + "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง", +) +@SuccessResponse(HttpStatusCode.OK, "สำเร็จ") +export class AppointController extends Controller { + private appointRepository = AppDataSource.getRepository(Appoint); + private appointDirectorRepository = AppDataSource.getRepository(AppointDirector); + + /** + * API รายการแต่งตั้งคณะกรรมการฯ ทดลองงาน + * + * @summary รายการแต่งตั้งคณะกรรมการฯ + * + */ + @Get("") + async GetList(@Request() request: RequestWithUser) { + await new permission().PermissionList(request, "SYS_PROBATION"); + + const appoint = await this.appointRepository.find({}); + + return new HttpSuccess(appoint); + } + + /** + * API รายการแต่งตั้งคณะกรรมการฯ ที่ออกคำสั่งแล้วทดลองงาน + * + * @summary รายการแต่งตั้งคณะกรรมการฯ ที่ออกคำสั่งแล้ว + * + */ + @Get("list") + async GetListCommand(@Request() request: RequestWithUser) { + const appoint = await this.appointRepository.find({ + relations: ["directors"], + where: { status: "DONE" }, + }); + + return new HttpSuccess(appoint); + } + + /** + * API สร้างการแต่งตั้งคณะกรรมการฯ ทดลองงาน + * + * @summary สร้างการแต่งตั้งคณะกรรมการฯ + * + */ + @Post("") + async Create(@Request() request: RequestWithUser, @Body() requestBody: CreateAppoint) { + await new permission().PermissionCreate(request, "SYS_PROBATION"); + + const data: any = { + topic: requestBody.topic, + profileId: requestBody.profileId, + createdUserId: request.user.sub, + createdFullName: request.user.name, + }; + const before = null; + const appoint = await this.appointRepository.save(data, { data: request }); + setLogDataDiff(request, { before, after: appoint }); + + return new HttpSuccess(appoint.id); + } + + /** + * API ดึงข้อมูลแต่งตั้งคณะกรรมการฯ + * + * @summary API ดึงข้อมูลแต่งตั้งคณะกรรมการฯ ตาม id + * + */ + @Get("{id}") + async GetById(@Request() request: RequestWithUser, @Path() id: string) { + await new permission().PermissionGet(request, "SYS_PROBATION"); + + const appoint = await this.appointRepository.findOne({ + select: ["id", "topic", "status"], + where: { id }, + relations: ["directors"], + }); + + return new HttpSuccess(appoint); + } + + /** + * API แก้ไขการแต่งตั้งคณะกรรมการฯ ทดลองงาน + * + * @summary แก้ไขการแต่งตั้งคณะกรรมการฯ + * + */ + @Put("{id}") + async Update( + @Request() request: RequestWithUser, + @Body() requestBody: UpdateAppoint, + @Path() id: string, + ) { + await new permission().PermissionUpdate(request, "SYS_PROBATION"); + + const appoint: any = await this.appointRepository.findOne({ where: { id } }); + + if (!appoint) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลการแต่งตั้งคณะกรรมการฯ"); + } + + const before = appoint; + + appoint.topic = requestBody.topic; + appoint.updateUserId = request.user.sub; + appoint.updateFullName = request.user.name; + + await this.appointDirectorRepository.delete({ appointId: id }); + const directors = await requestBody.persons.map((x: Person) => ({ + ...x, + appointId: id, + createdUserId: request.user.sub, + createdFullName: request.user.name, + updateUserId: request.user.sub, + updateFullName: request.user.name, + })); + await this.appointDirectorRepository.save(directors); + + await this.appointRepository.save(appoint, { data: request }); + setLogDataDiff(request, { before, after: appoint }); + + return new HttpSuccess(); + } + + /** + * API ลบรายการแต่งตั้งคณะกรรมการฯ + * + * @summary ลบรายการแต่งตั้งคณะกรรมการฯ + * + */ + @Delete("{id}") + public async deleteRole(@Path() id: string, @Request() request: RequestWithUser) { + await new permission().PermissionDelete(request, "SYS_PROBATION"); + + await this.appointDirectorRepository.delete({ appointId: id }); + + const result = await this.appointRepository.delete({ id }); + if (!result) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + + return new HttpSuccess(); + } +} diff --git a/src/entities/Appoint.ts b/src/entities/Appoint.ts new file mode 100644 index 0000000..38008d7 --- /dev/null +++ b/src/entities/Appoint.ts @@ -0,0 +1,61 @@ +import { Entity, Column, PrimaryGeneratedColumn, OneToMany, JoinColumn } from "typeorm"; +import { EntityBase } from "./base/Base"; +import { AppointDirector } from "./AppointDirector"; + +@Entity("appoint") +export class Appoint extends EntityBase { + @PrimaryGeneratedColumn("uuid") + id: string; + + @Column({ + nullable: false, + comment: "id ของคนทดลองงาน", + }) + profileId: string; + + @Column({ + nullable: false, + comment: "หัวข้อ", + default: 0, + }) + topic: string; + + @Column({ + nullable: true, + comment: "เลขที่คำสั่ง", + }) + commandNo: string; + + @Column({ + type: "enum", + enum: ["PENDING", "REPORT", "DONE"], + nullable: false, + default: "PENDING", + }) + status: string; + + @OneToMany(() => AppointDirector, (director: AppointDirector) => director.appoint) + @JoinColumn({ name: "id" }) + directors: AppointDirector[]; +} + +export class CreateAppoint { + @Column() + topic: string; + profileId: string; +} + +export type Person = { + profileId: string; + name: string; + position: string; + positionType: string; + positionLevel: string; + role: string; +}; + +export class UpdateAppoint { + @Column() + topic: string; + persons: Person[]; +} diff --git a/src/entities/AppointDirector.ts b/src/entities/AppointDirector.ts new file mode 100644 index 0000000..3ce7086 --- /dev/null +++ b/src/entities/AppointDirector.ts @@ -0,0 +1,58 @@ +import { Entity, Column, PrimaryGeneratedColumn, ManyToOne, JoinColumn } from "typeorm"; +import { EntityBase } from "./base/Base"; +import { Appoint } from "./Appoint"; + +@Entity("appointDirector") +export class AppointDirector extends EntityBase { + @PrimaryGeneratedColumn("uuid") + id: string; + + @Column({ + nullable: false, + comment: "id ของการแต่งตั้งกรรมการ", + }) + appointId: string; + + @Column({ + nullable: false, + comment: "id ของคน", + }) + profileId: string; + + @Column({ + nullable: false, + comment: "ชื่อ-นามสกุลของประธาน/กรรมการ", + }) + name: string; + + @Column({ + nullable: true, + comment: "ตำแหน่ง", + }) + position: string; + + @Column({ + nullable: true, + comment: "ประเภทตำแหน่ง", + }) + positionType: string; + + @Column({ + nullable: true, + comment: "ระดับตำแหน่ง", + }) + positionLevel: string; + + @Column({ + type: "enum", + enum: ["chairman", "committee"], + nullable: false, + default: "committee", + comment: "บทบาท ประธาน/กรรมการ", + }) + role: string; + + @ManyToOne(() => Appoint, (appoint: Appoint) => appoint.directors) + @JoinColumn({ name: "appointId" }) + appoint: Appoint; +} diff --git a/src/migration/1729088301267-appoint.ts b/src/migration/1729088301267-appoint.ts new file mode 100644 index 0000000..ea6f209 --- /dev/null +++ b/src/migration/1729088301267-appoint.ts @@ -0,0 +1,25 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class Appoint1729088301267 implements MigrationInterface { + name = "Appoint1729088301267"; + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query( + `CREATE TABLE \`appoint\` (\`createdAt\` datetime(6) NOT NULL COMMENT 'สร้างข้อมูลเมื่อ' DEFAULT CURRENT_TIMESTAMP(6), \`createdUserId\` varchar(40) NOT NULL COMMENT 'User Id ที่สร้างข้อมูล' DEFAULT '00000000-0000-0000-0000-000000000000', \`updatedAt\` datetime(6) NOT NULL COMMENT 'แก้ไขข้อมูลล่าสุดเมื่อ' DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), \`updateUserId\` varchar(40) NOT NULL COMMENT 'User Id ที่แก้ไขข้อมูล' DEFAULT '00000000-0000-0000-0000-000000000000', \`createdFullName\` varchar(200) NOT NULL COMMENT 'ชื่อ User ที่สร้างข้อมูล' DEFAULT 'System Administrator', \`updateFullName\` varchar(200) NOT NULL COMMENT 'ชื่อ User ที่แก้ไขข้อมูลล่าสุด' DEFAULT 'System Administrator', \`id\` varchar(36) NOT NULL, \`profileId\` varchar(255) NOT NULL COMMENT 'id ของคนทดลองงาน', \`topic\` varchar(255) NOT NULL COMMENT 'หัวข้อ' DEFAULT '0', \`commandNo\` varchar(255) NULL COMMENT 'เลขที่คำสั่ง', \`status\` enum ('PENDING', 'REPORT', 'DONE') NOT NULL DEFAULT 'PENDING', PRIMARY KEY (\`id\`)) ENGINE=InnoDB`, + ); + await queryRunner.query( + `CREATE TABLE \`appointDirector\` (\`createdAt\` datetime(6) NOT NULL COMMENT 'สร้างข้อมูลเมื่อ' DEFAULT CURRENT_TIMESTAMP(6), \`createdUserId\` varchar(40) NOT NULL COMMENT 'User Id ที่สร้างข้อมูล' DEFAULT '00000000-0000-0000-0000-000000000000', \`updatedAt\` datetime(6) NOT NULL COMMENT 'แก้ไขข้อมูลล่าสุดเมื่อ' DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), \`updateUserId\` varchar(40) NOT NULL COMMENT 'User Id ที่แก้ไขข้อมูล' DEFAULT '00000000-0000-0000-0000-000000000000', \`createdFullName\` varchar(200) NOT NULL COMMENT 'ชื่อ User ที่สร้างข้อมูล' DEFAULT 'System Administrator', \`updateFullName\` varchar(200) NOT NULL COMMENT 'ชื่อ User ที่แก้ไขข้อมูลล่าสุด' DEFAULT 'System Administrator', \`id\` varchar(36) NOT NULL, \`appointId\` varchar(255) NOT NULL COMMENT 'id ของการแต่งตั้งกรรมการ', \`profileId\` varchar(255) NOT NULL COMMENT 'id ของคน', \`name\` varchar(255) NOT NULL COMMENT 'ชื่อ-นามสกุลของประธาน/กรรมการ', \`position\` varchar(255) NULL COMMENT 'ตำแหน่ง', \`positionType\` varchar(255) NULL COMMENT 'ประเภทตำแหน่ง', \`positionLevel\` varchar(255) NULL COMMENT 'ระดับตำแหน่ง', \`role\` enum ('chairman', 'committee') NOT NULL COMMENT 'บทบาท ประธาน/กรรมการ' DEFAULT 'committee', PRIMARY KEY (\`id\`)) ENGINE=InnoDB`, + ); + await queryRunner.query( + `ALTER TABLE \`appointDirector\` ADD CONSTRAINT \`FK_3c69c0caf7adbeb5b6bd31694f8\` FOREIGN KEY (\`appointId\`) REFERENCES \`appoint\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`, + ); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query( + `ALTER TABLE \`appointDirector\` DROP FOREIGN KEY \`FK_3c69c0caf7adbeb5b6bd31694f8\``, + ); + await queryRunner.query(`DROP TABLE \`appointDirector\``); + await queryRunner.query(`DROP TABLE \`appoint\``); + } +}