hrms-api-org/src/controllers/WorkflowController.ts

150 lines
4.1 KiB
TypeScript
Raw Normal View History

2024-10-09 14:14:36 +07:00
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,
});
}
}