updated appoint chairman/committee

This commit is contained in:
Warunee Tamkoo 2024-10-16 21:39:37 +07:00
parent 76d9be4da1
commit 5e117d1703
4 changed files with 318 additions and 0 deletions

View file

@ -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();
}
}

61
src/entities/Appoint.ts Normal file
View file

@ -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[];
}

View file

@ -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;
}

View file

@ -0,0 +1,25 @@
import { MigrationInterface, QueryRunner } from "typeorm";
export class Appoint1729088301267 implements MigrationInterface {
name = "Appoint1729088301267";
public async up(queryRunner: QueryRunner): Promise<void> {
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<void> {
await queryRunner.query(
`ALTER TABLE \`appointDirector\` DROP FOREIGN KEY \`FK_3c69c0caf7adbeb5b6bd31694f8\``,
);
await queryRunner.query(`DROP TABLE \`appointDirector\``);
await queryRunner.query(`DROP TABLE \`appoint\``);
}
}