diff --git a/src/controllers/WorkflowController.ts b/src/controllers/WorkflowController.ts new file mode 100644 index 00000000..8121499a --- /dev/null +++ b/src/controllers/WorkflowController.ts @@ -0,0 +1,149 @@ +import { Body, Controller, Post, Request, Route, Security, Tags } from "tsoa"; +import { AppDataSource } from "../database/data-source"; +import { RequestWithUser } from "../middlewares/user"; +import HttpError from "../interfaces/http-error"; +import HttpStatus from "../interfaces/http-status"; +import HttpSuccess from "../interfaces/http-success"; +import { Workflow } from "../entities/Workflow"; +import { State } from "../entities/State"; +import { StateOperator } from "../entities/StateOperator"; + +@Route("api/v1/org/workflow") +@Tags("AuthRole") +@Security("bearerAuth") +export class WorkflowController extends Controller { + private workflowRepo = AppDataSource.getRepository(Workflow); + private stateRepo = AppDataSource.getRepository(State); + private stateOperatorRepo = AppDataSource.getRepository(StateOperator); + + @Post("check-iscan") + public async checkIsCan( + @Request() req: RequestWithUser, + @Body() + body: { + workflowId: string; + stateId: string; + operator: string; + action: string; + }, + ) { + const operator = await this.stateOperatorRepo.findOne({ + where: { + operator: body.operator, + state: { id: body.stateId, workflow: { id: body.workflowId } }, + }, + }); + if (!operator) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + + let isCan = false; + switch (body.action) { + case "VIEW": + isCan = operator.canView; + case "UPDATE": + isCan = operator.canUpdate; + case "DELETE": + isCan = operator.canDelete; + case "CANCEL": + isCan = operator.canCancel; + case "OPERATE": + isCan = operator.canOperate; + case "CHANGESTATE": + isCan = operator.canChangeState; + case "COMMENT": + isCan = operator.canComment; + case "SIGN": + isCan = operator.canSign; + default: + isCan = false; + } + if (isCan == false) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่สามารถดำเนินการได้"); + } else { + return new HttpSuccess(); + } + } + + @Post("check-workflow") + public async checkWorkflow( + @Request() req: RequestWithUser, + @Body() + body: { + sysName: string; + posLevelName: string; + posTypeName: string; + }, + ) { + const workflow = await this.workflowRepo.findOne({ + where: { + sysName: body.sysName, + posLevelName: body.posLevelName, + posTypeName: body.posTypeName, + }, + relations: ["states"], + }); + if (!workflow) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + + return new HttpSuccess({ + workflowId: workflow.id, + stateId: workflow.states.sort((a, b) => a.order - b.order)[0].id, + stateName: workflow.states.sort((a, b) => a.order - b.order)[0].name, + stateType: workflow.states.sort((a, b) => a.order - b.order)[0].type, + }); + } + + @Post("state-next") + public async stateNext( + @Request() req: RequestWithUser, + @Body() + body: { + stateId: string; + }, + ) { + const state = await this.stateRepo.findOne({ + where: { + id: body.stateId, + }, + }); + if (!state) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + const _state = await this.stateRepo.findOne({ + where: { + order: state.order + 1, + workflowId: state.workflowId, + }, + }); + + return new HttpSuccess({ + stateId: _state?.id || null, + stateName: _state?.name || null, + stateType: _state?.type || null, + }); + } + + @Post("state-back") + public async stateBack( + @Request() req: RequestWithUser, + @Body() + body: { + stateId: string; + }, + ) { + const state = await this.stateRepo.findOne({ + where: { + id: body.stateId, + }, + }); + if (!state) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + const _state = await this.stateRepo.findOne({ + where: { + order: state.order - 1, + workflowId: state.workflowId, + }, + }); + + return new HttpSuccess({ + stateId: _state?.id || null, + stateName: _state?.name || null, + stateType: _state?.type || null, + }); + } +} diff --git a/src/entities/StateOperator.ts b/src/entities/StateOperator.ts index 07ae241c..5438ece9 100644 --- a/src/entities/StateOperator.ts +++ b/src/entities/StateOperator.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 { State } from "./State"; +import { StateOperatorUser } from "./StateOperatorUser"; @Entity("stateOperator") export class StateOperator extends EntityBase { @@ -71,4 +72,7 @@ export class StateOperator extends EntityBase { @ManyToOne(() => State, (state) => state.stateOperators) @JoinColumn({ name: "stateId" }) state: State; + + @OneToMany(() => StateOperatorUser, (stateOperatorUser) => stateOperatorUser.stateOperator) + stateOperatorUsers: StateOperatorUser[]; } diff --git a/src/entities/StateOperatorUser.ts b/src/entities/StateOperatorUser.ts new file mode 100644 index 00000000..dd4bc5cc --- /dev/null +++ b/src/entities/StateOperatorUser.ts @@ -0,0 +1,42 @@ +import { Entity, Column, ManyToOne, JoinColumn, OneToMany } from "typeorm"; +import { EntityBase } from "./base/Base"; +import { StateOperator } from "./StateOperator"; +import { Workflow } from "./Workflow"; + +@Entity("stateOperatorUser") +export class StateOperatorUser extends EntityBase { + @Column({ + nullable: true, + comment: "", + length: 255, + default: null, + }) + profile: string; + + @Column({ + nullable: true, + comment: "", + length: 255, + default: null, + }) + type: string; //commander + + @Column({ + nullable: true, + comment: "ลำดับ", + default: null, + }) + order: number; // + + @Column({ + nullable: true, + length: 40, + comment: "คีย์นอก(FK)ของตาราง stateOperator", + default: null, + }) + stateOperatorId: string; + + @ManyToOne(() => StateOperator, (stateOperator) => stateOperator.stateOperatorUsers) + @JoinColumn({ name: "stateOperatorId" }) + stateOperator: StateOperator; +} diff --git a/src/migration/1728456793035-add_table_workflow2.ts b/src/migration/1728456793035-add_table_workflow2.ts new file mode 100644 index 00000000..748304cd --- /dev/null +++ b/src/migration/1728456793035-add_table_workflow2.ts @@ -0,0 +1,26 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class AddTableWorkflow21728456793035 implements MigrationInterface { + name = 'AddTableWorkflow21728456793035' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`workflow\` CHANGE \`commandSysName\` \`sysName\` varchar(100) NULL COMMENT 'ชื่อระบบ'`); + await queryRunner.query(`CREATE TABLE \`stateOperatorUser\` (\`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', \`profile\` varchar(255) NULL, \`type\` varchar(255) NULL, \`order\` int NULL COMMENT 'ลำดับ', \`stateOperatorId\` varchar(40) NULL COMMENT 'คีย์นอก(FK)ของตาราง stateOperator', PRIMARY KEY (\`id\`)) ENGINE=InnoDB`); + await queryRunner.query(`ALTER TABLE \`command\` DROP COLUMN \`isBangkok\``); + await queryRunner.query(`ALTER TABLE \`command\` ADD \`isBangkok\` varchar(20) NULL COMMENT 'คำสั่งกรุงเทพมหานคร'`); + await queryRunner.query(`ALTER TABLE \`state\` DROP COLUMN \`order\``); + await queryRunner.query(`ALTER TABLE \`state\` ADD \`order\` int NULL COMMENT 'ลำดับ'`); + await queryRunner.query(`ALTER TABLE \`stateOperatorUser\` ADD CONSTRAINT \`FK_bef7391c37f03a3b809406beebf\` FOREIGN KEY (\`stateOperatorId\`) REFERENCES \`stateOperator\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`stateOperatorUser\` DROP FOREIGN KEY \`FK_bef7391c37f03a3b809406beebf\``); + await queryRunner.query(`ALTER TABLE \`state\` DROP COLUMN \`order\``); + await queryRunner.query(`ALTER TABLE \`state\` ADD \`order\` varchar(255) NULL COMMENT 'ลำดับ'`); + await queryRunner.query(`ALTER TABLE \`command\` DROP COLUMN \`isBangkok\``); + await queryRunner.query(`ALTER TABLE \`command\` ADD \`isBangkok\` tinyint NULL COMMENT 'คำสั่งกรุงเทพมหานคร'`); + await queryRunner.query(`DROP TABLE \`stateOperatorUser\``); + await queryRunner.query(`ALTER TABLE \`workflow\` CHANGE \`sysName\` \`commandSysName\` varchar(100) NULL COMMENT 'ชื่อระบบ'`); + } + +}