From 7b5e6e5fb002f467f931b7cd3bca1e112da85d91 Mon Sep 17 00:00:00 2001 From: kittapath Date: Mon, 7 Oct 2024 17:00:54 +0700 Subject: [PATCH] =?UTF-8?q?=E0=B8=A1=E0=B8=AD=E0=B8=9A=E0=B8=AB=E0=B8=A1?= =?UTF-8?q?=E0=B8=B2=E0=B8=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/CommandSysController.ts | 25 +++++ src/controllers/OrganizationController.ts | 4 + src/controllers/PositionController.ts | 86 +++++++++++++++- src/entities/Assign.ts | 98 +++++++++++++++++++ src/entities/CommandSys.ts | 4 + src/entities/PosMaster.ts | 4 + src/entities/PosMasterAssign.ts | 38 +++++++ .../1728290588277-add_table_assign.ts | 22 +++++ 8 files changed, 279 insertions(+), 2 deletions(-) create mode 100644 src/entities/Assign.ts create mode 100644 src/entities/PosMasterAssign.ts create mode 100644 src/migration/1728290588277-add_table_assign.ts diff --git a/src/controllers/CommandSysController.ts b/src/controllers/CommandSysController.ts index 316697af..7c6a8aa1 100644 --- a/src/controllers/CommandSysController.ts +++ b/src/controllers/CommandSysController.ts @@ -53,6 +53,31 @@ export class CommandSysController extends Controller { return new HttpSuccess(_commandSys); } + /** + * API รายละเอียดรายการระบบคำสั่ง + * + * @summary ORG_058 - CRUD ระบบคำสั่ง (ADMIN) #62 + * + * @param {string} id Id ระบบคำสั่ง + */ + @Get("assign") + async GetAssgin() { + const _commandSys = await this.commandSysRepository.find({ + order: { order: "ASC" }, + relations: ["assgins"], + }); + const data = _commandSys.map((data) => ({ + id: data.id, + sysName: data.sysName, + assgins: data.assgins.map((x) => ({ + id: x.id, + name: x.name, + description: x.description, + })), + })); + return new HttpSuccess(data); + } + /** * API รายละเอียดรายการระบบคำสั่ง * diff --git a/src/controllers/OrganizationController.ts b/src/controllers/OrganizationController.ts index 26cfad2f..8db60923 100644 --- a/src/controllers/OrganizationController.ts +++ b/src/controllers/OrganizationController.ts @@ -831,6 +831,7 @@ export class OrganizationController extends Controller { orgRevisionId: orgRoot.orgRevisionId, orgRootName: orgRoot.orgRootName, responsibility: orgRoot.responsibility, + isOfficer: false, labelName: orgRoot.orgRootName + " " + orgRoot.orgRootCode + "00" + " " + orgRoot.orgRootShortName, totalPosition: await this.posMasterRepository.count({ @@ -1057,6 +1058,7 @@ export class OrganizationController extends Controller { orgRevisionId: orgRoot.orgRevisionId, orgRootName: orgRoot.orgRootName, responsibility: orgChild2.responsibility, + isOfficer: orgChild1.isOfficer, labelName: orgChild2.orgChild2Name + " " + @@ -1175,6 +1177,7 @@ export class OrganizationController extends Controller { orgRevisionId: orgRoot.orgRevisionId, orgRootName: orgRoot.orgRootName, responsibility: orgChild3.responsibility, + isOfficer: orgChild1.isOfficer, labelName: orgChild3.orgChild3Name + " " + @@ -1293,6 +1296,7 @@ export class OrganizationController extends Controller { orgRevisionId: orgRoot.orgRevisionId, orgRootName: orgRoot.orgRootName, responsibility: orgChild4.responsibility, + isOfficer: orgChild1.isOfficer, labelName: orgChild4.orgChild4Name + " " + diff --git a/src/controllers/PositionController.ts b/src/controllers/PositionController.ts index 8f1c3fc1..7dc8f8d9 100644 --- a/src/controllers/PositionController.ts +++ b/src/controllers/PositionController.ts @@ -42,6 +42,9 @@ import { RequestWithUser } from "../middlewares/user"; import permission from "../interfaces/permission"; import { request } from "axios"; import { setLogDataDiff } from "../interfaces/utils"; +import { PosMasterAssign } from "../entities/PosMasterAssign"; +import { CommandSys } from "../entities/CommandSys"; +import { Assign } from "../entities/Assign"; @Route("api/v1/org/pos") @Tags("Position") @Security("bearerAuth") @@ -67,6 +70,8 @@ export class PositionController extends Controller { private child3Repository = AppDataSource.getRepository(OrgChild3); private child4Repository = AppDataSource.getRepository(OrgChild4); private authRoleRepo = AppDataSource.getRepository(AuthRole); + private posMasterAssignRepo = AppDataSource.getRepository(PosMasterAssign); + private assignRepo = AppDataSource.getRepository(Assign); /** * API เพิ่มตำแหน่ง @@ -150,6 +155,73 @@ export class PositionController extends Controller { return new HttpSuccess(posDict.id); } + /** + * API มอบหมาย + * + * @summary มอบหมาย + * + */ + @Post("assign") + async createPositionMasterAssgin( + @Body() + requestBody: { + assignIds: string[]; + posMasterId: string; + }, + @Request() request: RequestWithUser, + ) { + const posMaster = await this.posMasterRepository.findOne({ + where: { id: requestBody.posMasterId }, + }); + if (!posMaster) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่ง"); + } + await this.posMasterAssignRepo.delete({ posMasterId: posMaster.id }); + + const assigns = await this.assignRepo.find({ + where: { id: In(requestBody.assignIds) }, + }); + await Promise.all( + await assigns.map(async (x) => { + let _posMasterAssign = await this.posMasterAssignRepo.findOne({ + where: { posMasterId: requestBody.posMasterId, assignId: x.id }, + }); + if (_posMasterAssign == null) { + const posMasterAssign = new PosMasterAssign(); + posMasterAssign.posMasterId = requestBody.posMasterId; + posMasterAssign.assignId = x.id; + posMasterAssign.createdUserId = request.user.sub; + posMasterAssign.createdFullName = request.user.name; + posMasterAssign.lastUpdateUserId = request.user.sub; + posMasterAssign.lastUpdateFullName = request.user.name; + posMasterAssign.createdAt = new Date(); + posMasterAssign.lastUpdatedAt = new Date(); + await this.posMasterAssignRepo.save(posMasterAssign); + } + }), + ); + return new HttpSuccess(); + } + + /** + * API ลบมอบหมาย + * + * @summary ลบมอบหมาย + * + * @param {string} id Id posMaster + */ + @Delete("assign/{id}") + async deletePositionMasterAssgin(@Path() id: string, @Request() request: RequestWithUser) { + const posMaster = await this.posMasterRepository.findOne({ + where: { id: id }, + }); + if (!posMaster) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่ง"); + } + await this.posMasterAssignRepo.delete({ posMasterId: posMaster.id }); + return new HttpSuccess(); + } + /** * API เพิ่มตำแหน่ง * @@ -990,7 +1062,7 @@ export class PositionController extends Controller { posMaster.createdAt = new Date(); posMaster.lastUpdatedAt = new Date(); await this.posMasterRepository.save(posMaster, { data: request }); - setLogDataDiff( request, { before, after: posMaster }); + setLogDataDiff(request, { before, after: posMaster }); await this.positionRepository.delete({ posMasterId: posMaster.id }); await Promise.all( @@ -1256,6 +1328,8 @@ export class PositionController extends Controller { .leftJoinAndSelect("posMaster.current_holder", "current_holder") .leftJoinAndSelect("posMaster.next_holder", "next_holder") .leftJoinAndSelect("posMaster.orgRevision", "orgRevision") + .leftJoinAndSelect("posMaster.posMasterAssigns", "posMasterAssigns") + .leftJoinAndSelect("posMasterAssigns.assign", "assign") .where(conditions) .andWhere( new Brackets((qb) => { @@ -1446,6 +1520,14 @@ export class PositionController extends Controller { authRoleId: posMaster.authRoleId, authRoleName: authRoleName == null || authRoleName.roleName == null ? null : authRoleName.roleName, + isPosMasterAssign: posMaster.posMasterAssigns.length > 0 ? true : false, + posMasterAssigns: posMaster.posMasterAssigns.map((x) => ({ + id: x.id, + assignId: x.assignId, + commandSysId: x.assign.commandSysId, + name: x.assign.name, + description: x.assign.description, + })), positions: positions.map((position: any) => ({ id: position.id, positionName: position.positionName, @@ -3000,7 +3082,7 @@ export class PositionController extends Controller { dataMaster.isSit = requestBody.isSit; dataMaster.next_holderId = requestBody.profileId; await this.posMasterRepository.save(dataMaster, { data: request }); - setLogDataDiff( request, { before, after: dataMaster }); + setLogDataDiff(request, { before, after: dataMaster }); return new HttpSuccess(); } diff --git a/src/entities/Assign.ts b/src/entities/Assign.ts new file mode 100644 index 00000000..ea331d01 --- /dev/null +++ b/src/entities/Assign.ts @@ -0,0 +1,98 @@ +import { + Entity, + Column, + CreateDateColumn, + UpdateDateColumn, + PrimaryColumn, + OneToMany, + ManyToOne, + JoinColumn, +} from "typeorm"; +import { CommandType } from "./CommandType"; +import { PosMasterAssign } from "./PosMasterAssign"; +import { CommandSys } from "./CommandSys"; + +@Entity("assign") +export class Assign { + @PrimaryColumn({ + comment: "ไอดีหลักของตาราง", + length: 255, + }) + id: string; + + @CreateDateColumn({ comment: "สร้างข้อมูลเมื่อ" }) + createdAt!: Date; + + @Column({ + comment: "User Id ที่สร้างข้อมูล", + length: 40, + default: "00000000-0000-0000-0000-000000000000", + }) + createdUserId!: String; + + @UpdateDateColumn({ comment: "แก้ไขข้อมูลล่าสุดเมื่อ" }) + lastUpdatedAt!: Date; + + @Column({ + comment: "User Id ที่แก้ไขข้อมูล", + length: 40, + default: "00000000-0000-0000-0000-000000000000", + }) + lastUpdateUserId!: String; + + @Column({ comment: "ชื่อ User ที่สร้างข้อมูล", length: 200, default: "string" }) + createdFullName!: String; + + @Column({ comment: "ชื่อ User ที่แก้ไขข้อมูลล่าสุด", length: 200, default: "string" }) + lastUpdateFullName!: String; + + @Column({ + nullable: true, + comment: "ชื่อระบบ", + length: 255, + default: null, + }) + name: string; + + @Column({ + nullable: true, + comment: "รายละเอียด", + length: 255, + default: null, + }) + description: string; + + @OneToMany(() => PosMasterAssign, (posMasterAssign) => posMasterAssign.assign) + posMasterAssigns: PosMasterAssign[]; + + @Column({ + nullable: true, + length: 255, + comment: "คีย์นอก(FK)ของตาราง CommandSys", + default: null, + }) + commandSysId: string; + + @ManyToOne(() => CommandSys, (commandSys) => commandSys.assgins) + @JoinColumn({ name: "commandSysId" }) + commandAssignSys: CommandSys; +} + +export class CreateCommandSys { + @PrimaryColumn() + id: string; + + @Column() + name: string; + + @Column() + description: string; +} + +export class UpdateCommandSys { + @Column() + name: string; + + @Column() + description: string; +} diff --git a/src/entities/CommandSys.ts b/src/entities/CommandSys.ts index cb74490d..77c1be5d 100644 --- a/src/entities/CommandSys.ts +++ b/src/entities/CommandSys.ts @@ -8,6 +8,7 @@ import { } from "typeorm"; import { CommandType } from "./CommandType"; import { CommandSalary } from "./CommandSalary"; +import { Assign } from "./Assign"; @Entity("commandSys") export class CommandSys { @@ -71,6 +72,9 @@ export class CommandSys { @OneToMany(() => CommandSalary, (commandSalary) => commandSalary.commandSalarySys) commandSalarys: CommandSalary[]; + + @OneToMany(() => Assign, (assgin) => assgin.commandAssignSys) + assgins: Assign[]; } export class CreateCommandSys { diff --git a/src/entities/PosMaster.ts b/src/entities/PosMaster.ts index 2c8a2a41..8aea9570 100644 --- a/src/entities/PosMaster.ts +++ b/src/entities/PosMaster.ts @@ -11,6 +11,7 @@ import { OrgChild4 } from "./OrgChild4"; import { Profile } from "./Profile"; import { AuthRole } from "./AuthRole"; import { PosMasterAct } from "./PosMasterAct"; +import { PosMasterAssign } from "./PosMasterAssign"; enum PosMasterLine { MAIN = "MAIN", @@ -238,6 +239,9 @@ export class PosMaster extends EntityBase { @OneToMany(() => PosMasterAct, (posMasterAct) => posMasterAct.posMasterChild) posMasterActChilds: PosMasterAct[]; + + @OneToMany(() => PosMasterAssign, (posMasterAssign) => posMasterAssign.posMaster) + posMasterAssigns: PosMasterAssign[]; } export class CreatePosMaster { diff --git a/src/entities/PosMasterAssign.ts b/src/entities/PosMasterAssign.ts new file mode 100644 index 00000000..056b08fb --- /dev/null +++ b/src/entities/PosMasterAssign.ts @@ -0,0 +1,38 @@ +import { Entity, Column, ManyToOne, JoinColumn } from "typeorm"; +import { EntityBase } from "./base/Base"; +import { PosMaster } from "./PosMaster"; +import { Assign } from "./Assign"; + +@Entity("posMasterAssign") +export class PosMasterAssign extends EntityBase { + @Column({ + nullable: true, + default: null, + length: 40, + comment: "คีย์นอก(FK)ของตาราง posMaster", + }) + posMasterId: string; + + @ManyToOne(() => PosMaster, (posMaster) => posMaster.posMasterAssigns) + @JoinColumn({ name: "posMasterId" }) + posMaster: PosMaster; + + @Column({ + nullable: true, + default: null, + length: 40, + comment: "คีย์นอก(FK)ของตาราง assign", + }) + assignId: string; + + @ManyToOne(() => Assign, (assign) => assign.posMasterAssigns) + @JoinColumn({ name: "assignId" }) + assign: Assign; +} + +export class CreatePosMaster { + @Column() + posMasterNoPrefix: string; +} + +export type UpdatePosMaster = Partial; diff --git a/src/migration/1728290588277-add_table_assign.ts b/src/migration/1728290588277-add_table_assign.ts new file mode 100644 index 00000000..91e6a813 --- /dev/null +++ b/src/migration/1728290588277-add_table_assign.ts @@ -0,0 +1,22 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class AddTableAssign1728290588277 implements MigrationInterface { + name = 'AddTableAssign1728290588277' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`CREATE TABLE \`posMasterAssign\` (\`id\` varchar(36) NOT NULL, \`createdAt\` datetime(6) NOT NULL COMMENT 'สร้างข้อมูลเมื่อ' DEFAULT CURRENT_TIMESTAMP(6), \`createdUserId\` varchar(40) NOT NULL COMMENT 'User Id ที่สร้างข้อมูล' DEFAULT '00000000-0000-0000-0000-000000000000', \`lastUpdatedAt\` datetime(6) NOT NULL COMMENT 'แก้ไขข้อมูลล่าสุดเมื่อ' DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), \`lastUpdateUserId\` varchar(40) NOT NULL COMMENT 'User Id ที่แก้ไขข้อมูล' DEFAULT '00000000-0000-0000-0000-000000000000', \`createdFullName\` varchar(200) NOT NULL COMMENT 'ชื่อ User ที่สร้างข้อมูล' DEFAULT 'string', \`lastUpdateFullName\` varchar(200) NOT NULL COMMENT 'ชื่อ User ที่แก้ไขข้อมูลล่าสุด' DEFAULT 'string', \`posMasterId\` varchar(40) NULL COMMENT 'คีย์นอก(FK)ของตาราง posMaster', \`assignId\` varchar(40) NULL COMMENT 'คีย์นอก(FK)ของตาราง assign', PRIMARY KEY (\`id\`)) ENGINE=InnoDB`); + await queryRunner.query(`CREATE TABLE \`assign\` (\`id\` varchar(255) NOT NULL COMMENT 'ไอดีหลักของตาราง', \`createdAt\` datetime(6) NOT NULL COMMENT 'สร้างข้อมูลเมื่อ' DEFAULT CURRENT_TIMESTAMP(6), \`createdUserId\` varchar(40) NOT NULL COMMENT 'User Id ที่สร้างข้อมูล' DEFAULT '00000000-0000-0000-0000-000000000000', \`lastUpdatedAt\` datetime(6) NOT NULL COMMENT 'แก้ไขข้อมูลล่าสุดเมื่อ' DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), \`lastUpdateUserId\` varchar(40) NOT NULL COMMENT 'User Id ที่แก้ไขข้อมูล' DEFAULT '00000000-0000-0000-0000-000000000000', \`createdFullName\` varchar(200) NOT NULL COMMENT 'ชื่อ User ที่สร้างข้อมูล' DEFAULT 'string', \`lastUpdateFullName\` varchar(200) NOT NULL COMMENT 'ชื่อ User ที่แก้ไขข้อมูลล่าสุด' DEFAULT 'string', \`name\` varchar(255) NULL COMMENT 'ชื่อระบบ', \`description\` varchar(255) NULL COMMENT 'รายละเอียด', \`commandSysId\` varchar(255) NULL COMMENT 'คีย์นอก(FK)ของตาราง CommandSys', PRIMARY KEY (\`id\`)) ENGINE=InnoDB`); + await queryRunner.query(`ALTER TABLE \`posMasterAssign\` ADD CONSTRAINT \`FK_f5ae6d6c7bc95f304d8652577c4\` FOREIGN KEY (\`posMasterId\`) REFERENCES \`posMaster\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`); + await queryRunner.query(`ALTER TABLE \`posMasterAssign\` ADD CONSTRAINT \`FK_6eab8fe0c6745fce4e584d612a1\` FOREIGN KEY (\`assignId\`) REFERENCES \`assign\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`); + await queryRunner.query(`ALTER TABLE \`assign\` ADD CONSTRAINT \`FK_e5f8c525f66832567d1c82361df\` FOREIGN KEY (\`commandSysId\`) REFERENCES \`commandSys\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`assign\` DROP FOREIGN KEY \`FK_e5f8c525f66832567d1c82361df\``); + await queryRunner.query(`ALTER TABLE \`posMasterAssign\` DROP FOREIGN KEY \`FK_6eab8fe0c6745fce4e584d612a1\``); + await queryRunner.query(`ALTER TABLE \`posMasterAssign\` DROP FOREIGN KEY \`FK_f5ae6d6c7bc95f304d8652577c4\``); + await queryRunner.query(`DROP TABLE \`assign\``); + await queryRunner.query(`DROP TABLE \`posMasterAssign\``); + } + +}