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

590 lines
19 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";
2024-10-09 16:20:43 +07:00
import { StateOperatorUser } from "../entities/StateOperatorUser";
import CallAPI from "../interfaces/call-api";
import { Profile } from "../entities/Profile";
2024-10-16 11:55:45 +07:00
import { StateUserComment } from "../entities/StateUserComment";
2024-10-16 14:40:37 +07:00
import { MetaWorkflow } from "../entities/MetaWorkflow";
import { MetaState } from "../entities/MetaState";
import { MetaStateOperator } from "../entities/MetaStateOperator";
2024-10-16 17:15:55 +07:00
import { PosMasterAssign } from "../entities/PosMasterAssign";
import { PosMaster } from "../entities/PosMaster";
2024-10-09 14:14:36 +07:00
@Route("api/v1/org/workflow")
2024-10-16 11:55:45 +07:00
@Tags("Workflow")
2024-10-09 14:14:36 +07:00
@Security("bearerAuth")
export class WorkflowController extends Controller {
private workflowRepo = AppDataSource.getRepository(Workflow);
private stateRepo = AppDataSource.getRepository(State);
private stateOperatorRepo = AppDataSource.getRepository(StateOperator);
2024-10-09 16:20:43 +07:00
private stateOperatorUserRepo = AppDataSource.getRepository(StateOperatorUser);
2024-10-16 11:55:45 +07:00
private stateUserCommentRepo = AppDataSource.getRepository(StateUserComment);
2024-10-09 16:20:43 +07:00
private profileRepo = AppDataSource.getRepository(Profile);
2024-10-16 14:40:37 +07:00
private metaWorkflowRepo = AppDataSource.getRepository(MetaWorkflow);
private metaStateRepo = AppDataSource.getRepository(MetaState);
private metaStateOperatorRepo = AppDataSource.getRepository(MetaStateOperator);
2024-10-16 17:15:55 +07:00
private posMasterRepo = AppDataSource.getRepository(PosMaster);
2024-10-16 14:40:37 +07:00
2024-10-16 11:55:45 +07:00
@Post("add-workflow")
2024-10-09 16:20:43 +07:00
public async checkWorkflow(
@Request() req: RequestWithUser,
@Body()
body: {
2024-10-16 14:40:37 +07:00
refId: string;
2024-10-09 16:20:43 +07:00
sysName: string;
posLevelName: string;
posTypeName: string;
},
) {
2024-10-16 14:40:37 +07:00
const profile = await this.profileRepo.findOne({
where: {
keycloak: req.user.sub,
},
});
if (!profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลผู้ใช้งาน");
const metaWorkflow = await this.metaWorkflowRepo.findOne({
2024-10-09 16:20:43 +07:00
where: {
sysName: body.sysName,
posLevelName: body.posLevelName,
posTypeName: body.posTypeName,
},
});
2024-10-16 14:40:37 +07:00
if (!metaWorkflow) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบกระบวนการนี้ได้");
const meta = {
createdUserId: req.user.sub,
createdFullName: req.user.name,
lastUpdateUserId: req.user.sub,
lastUpdateFullName: req.user.name,
createdAt: new Date(),
lastUpdatedAt: new Date(),
};
const workflow = new Workflow();
2024-10-16 17:15:55 +07:00
Object.assign(workflow, {
...metaWorkflow,
id: undefined,
...meta,
...body,
system: body.sysName,
});
await this.workflowRepo.save(workflow);
2024-10-16 14:40:37 +07:00
const metaState = await this.metaStateRepo.find({
where: {
metaWorkflowId: metaWorkflow.id,
},
2024-10-16 17:15:55 +07:00
order: { order: "ASC" },
2024-10-16 14:40:37 +07:00
});
2024-10-09 16:20:43 +07:00
await Promise.all(
2024-10-16 14:40:37 +07:00
metaState.map(async (item) => {
const state = new State();
Object.assign(state, { ...item, id: undefined, workflowId: workflow.id, ...meta });
await this.stateRepo.save(state);
2024-10-16 17:15:55 +07:00
if (state.order == 1) {
workflow.stateId = state.id;
await this.workflowRepo.save(workflow);
}
2024-10-16 14:40:37 +07:00
const metaStateOperator = await this.metaStateOperatorRepo.find({
where: {
metaStateId: item.id,
},
});
await Promise.all(
2024-10-16 17:15:55 +07:00
metaStateOperator.map(async (item1) => {
2024-10-16 14:40:37 +07:00
const stateOperator = new StateOperator();
2024-10-16 17:15:55 +07:00
Object.assign(stateOperator, { ...item1, id: undefined, stateId: state.id, ...meta });
2024-10-16 14:40:37 +07:00
await this.stateOperatorRepo.save(stateOperator);
}),
);
2024-10-09 16:20:43 +07:00
}),
);
2024-10-16 14:40:37 +07:00
const stateOperatorUser = new StateOperatorUser();
Object.assign(stateOperatorUser, {
profileId: profile.id,
2024-10-16 17:15:55 +07:00
operator: "Owner",
2024-10-16 14:40:37 +07:00
order: 1,
2024-10-09 16:20:43 +07:00
workflowId: workflow.id,
2024-10-16 14:40:37 +07:00
...meta,
2024-10-09 16:20:43 +07:00
});
2024-10-16 14:40:37 +07:00
await this.stateOperatorUserRepo.save(stateOperatorUser);
2024-10-16 17:15:55 +07:00
const profileOfficer = await this.posMasterRepo.find({
where: {
posMasterAssigns: { assignId: body.sysName },
orgRevision: { orgRevisionIsDraft: false, orgRevisionIsCurrent: true },
},
relations: ["orgChild1"],
});
await Promise.all(
profileOfficer.map(async (item, i) => {
if (item.orgChild1 == null || item.orgChild1.isOfficer == false) {
const stateOperatorUser = new StateOperatorUser();
Object.assign(stateOperatorUser, {
profileId: profile.id,
operator: "Officer",
order: i + 2,
workflowId: workflow.id,
...meta,
});
await this.stateOperatorUserRepo.save(stateOperatorUser);
} else {
const stateOperatorUser = new StateOperatorUser();
Object.assign(stateOperatorUser, {
profileId: profile.id,
operator: "PersonnelOfficer",
order: i + 2,
workflowId: workflow.id,
...meta,
});
await this.stateOperatorUserRepo.save(stateOperatorUser);
}
}),
);
2024-10-16 14:40:37 +07:00
return new HttpSuccess();
2024-10-09 16:20:43 +07:00
}
2024-10-09 14:14:36 +07:00
@Post("check-iscan")
public async checkIsCan(
@Request() req: RequestWithUser,
@Body()
body: {
workflowId: string;
stateId: string;
2024-10-16 11:55:45 +07:00
profileId: string;
2024-10-09 14:14:36 +07:00
action: string;
},
) {
2024-10-09 16:20:43 +07:00
const stateOperatorUser = await this.stateOperatorUserRepo.findOne({
where: {
2024-10-16 11:55:45 +07:00
profileId: body.profileId,
2024-10-09 16:20:43 +07:00
workflowId: body.workflowId,
},
});
if (!stateOperatorUser)
2024-10-16 11:55:45 +07:00
throw new HttpError(HttpStatus.NOT_FOUND, "ผู้ใช้งานนี้ไม่มีหน้าที่ในกระบวนการนี้");
2024-10-09 16:20:43 +07:00
2024-10-09 14:14:36 +07:00
const operator = await this.stateOperatorRepo.findOne({
where: {
2024-10-09 16:20:43 +07:00
operator: stateOperatorUser.operator,
2024-10-09 14:14:36 +07:00
state: { id: body.stateId, workflow: { id: body.workflowId } },
},
});
2024-10-16 11:55:45 +07:00
if (!operator) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่สามารถดำเนินการกระบวนการนี้ได้");
2024-10-09 14:14:36 +07:00
let isCan = false;
2024-10-09 16:20:43 +07:00
switch (body.action.trim().toLocaleUpperCase()) {
2024-10-09 14:14:36 +07:00
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();
}
}
2024-10-16 11:55:45 +07:00
@Post("check-state-now")
public async checkStateNow(
@Request() req: RequestWithUser,
@Body()
body: {
2024-10-16 12:00:22 +07:00
refId: string;
system: string;
2024-10-16 11:55:45 +07:00
},
) {
const workflow = await this.workflowRepo.findOne({
where: {
2024-10-16 12:00:22 +07:00
refId: body.refId,
2024-10-16 17:15:55 +07:00
sysName: body.system,
2024-10-16 11:55:45 +07:00
},
relations: ["stateOperatorUsers"],
});
if (!workflow) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่สามารถดำเนินการกระบวนการนี้ได้");
const state = await this.stateRepo.findOne({
where: {
id: workflow.stateId,
},
relations: ["stateOperators"],
});
if (!state) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลขั้นตอนการอนุมัติ");
return new HttpSuccess({
stateId: state.id,
stateNo: state.order,
stateName: state.name,
});
}
@Post("check-user-now")
public async checkUserNow(
2024-10-09 14:14:36 +07:00
@Request() req: RequestWithUser,
@Body()
body: {
2024-10-16 12:00:22 +07:00
refId: string;
system: string;
2024-10-09 14:14:36 +07:00
},
) {
2024-10-16 12:00:22 +07:00
const stateOperatorUser = await this.stateOperatorUserRepo.findOne({
2024-10-16 11:55:45 +07:00
where: {
2024-10-16 14:40:37 +07:00
workflow: {
refId: body.refId,
2024-10-16 17:15:55 +07:00
sysName: body.system,
2024-10-16 14:40:37 +07:00
},
profile: {
keycloak: req.user.sub,
},
2024-10-16 11:55:45 +07:00
},
2024-10-16 14:40:37 +07:00
relations: ["workflow"],
2024-10-16 11:55:45 +07:00
});
2024-10-16 12:00:22 +07:00
if (!stateOperatorUser)
throw new HttpError(HttpStatus.NOT_FOUND, "ผู้ใช้งานนี้ไม่มีหน้าที่ในกระบวนการนี้");
2024-10-09 14:14:36 +07:00
2024-10-09 16:20:43 +07:00
const operator = await this.stateOperatorRepo.findOne({
where: {
operator: stateOperatorUser.operator,
2024-10-16 14:40:37 +07:00
stateId: stateOperatorUser.workflow.stateId,
2024-10-16 11:55:45 +07:00
},
relations: ["state"],
});
if (!operator)
throw new HttpError(HttpStatus.NOT_FOUND, "ผู้ใช้งานนี้ไม่มีหน้าที่ในขั้นตอนนี้");
return new HttpSuccess({
stateId: operator.state.id,
stateNo: operator.state.order,
stateName: operator.state.name,
operator: operator.operator,
can_view: operator.canView,
can_update: operator.canUpdate,
can_operate: operator.canOperate,
can_change_state: operator.canChangeState,
can_delete: operator.canDelete,
can_cancel: operator.canCancel,
});
}
@Post("check-state-all")
public async checkStateAll(
@Request() req: RequestWithUser,
@Body()
body: {
2024-10-16 12:00:22 +07:00
refId: string;
system: string;
2024-10-16 11:55:45 +07:00
},
) {
const state = await this.stateRepo.find({
where: {
2024-10-16 14:40:37 +07:00
workflow: {
refId: body.refId,
2024-10-16 17:15:55 +07:00
sysName: body.system,
2024-10-16 14:40:37 +07:00
},
2024-10-09 16:20:43 +07:00
},
2024-10-16 14:40:37 +07:00
order: { order: "ASC", stateUserComments: { order: "ASC" } },
2024-10-16 11:55:45 +07:00
relations: ["stateUserComments"],
2024-10-09 14:14:36 +07:00
});
2024-10-16 11:55:45 +07:00
const _state = state.map((x) => ({
stateId: x.id,
stateNo: x.order,
stateName: x.name,
stateUserComments: x.stateUserComments,
}));
return new HttpSuccess(_state);
2024-10-09 14:14:36 +07:00
}
@Post("state-next")
public async stateNext(
@Request() req: RequestWithUser,
@Body()
body: {
2024-10-16 12:00:22 +07:00
refId: string;
system: string;
2024-10-09 14:14:36 +07:00
},
) {
2024-10-16 14:40:37 +07:00
const stateOperatorUser = await this.stateOperatorUserRepo.findOne({
where: {
workflow: {
refId: body.refId,
2024-10-16 17:15:55 +07:00
sysName: body.system,
2024-10-16 14:40:37 +07:00
},
profile: {
keycloak: req.user.sub,
},
},
});
if (!stateOperatorUser)
throw new HttpError(HttpStatus.NOT_FOUND, "ผู้ใช้งานนี้ไม่มีหน้าที่ในกระบวนการนี้");
2024-10-16 11:55:45 +07:00
const workflow = await this.workflowRepo.findOne({
where: {
2024-10-16 12:00:22 +07:00
refId: body.refId,
2024-10-16 17:15:55 +07:00
sysName: body.system,
2024-10-16 11:55:45 +07:00
},
relations: ["stateOperatorUsers"],
});
if (!workflow) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่สามารถดำเนินการกระบวนการนี้ได้");
2024-10-09 14:14:36 +07:00
const state = await this.stateRepo.findOne({
where: {
2024-10-16 11:55:45 +07:00
id: workflow.stateId,
2024-10-09 14:14:36 +07:00
},
2024-10-16 11:55:45 +07:00
relations: ["stateOperators"],
2024-10-09 14:14:36 +07:00
});
2024-10-16 11:55:45 +07:00
if (!state) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลขั้นตอนการอนุมัติ");
2024-10-09 14:14:36 +07:00
const _state = await this.stateRepo.findOne({
where: {
order: state.order + 1,
workflowId: state.workflowId,
},
2024-10-09 16:20:43 +07:00
relations: ["stateOperators"],
2024-10-09 14:14:36 +07:00
});
2024-10-09 16:20:43 +07:00
//noti
2024-10-16 11:55:45 +07:00
let profileNow = workflow.stateOperatorUsers
2024-10-09 16:20:43 +07:00
.filter((x) => state.stateOperators.map((s) => s.operator).includes(x.operator))
.map((x) => x.profile);
await new CallAPI()
.PostData(req, "/placement/noti/profiles", {
subject: `รายการถูกส่ง`,
body: `รายการถูกส่ง`,
2024-10-11 13:39:41 +07:00
receiverUserIds: profileNow,
2024-10-09 16:20:43 +07:00
payload: "", //แนบไฟล์
isSendMail: true,
isSendInbox: true,
2024-10-11 10:17:44 +07:00
isSendNotification: true,
2024-10-09 16:20:43 +07:00
})
.catch((error) => {
console.error("Error calling API:", error);
});
if (_state != null) {
2024-10-16 11:55:45 +07:00
let profileNext = workflow.stateOperatorUsers
2024-10-09 16:20:43 +07:00
.filter((x) => _state.stateOperators.map((s) => s.operator).includes(x.operator))
.map((x) => x.profile);
await new CallAPI()
.PostData(req, "/placement/noti/profiles", {
subject: `ได้รับรายการ`,
body: `ได้รับรายการ`,
2024-10-11 13:39:41 +07:00
receiverUserIds: profileNext,
2024-10-09 16:20:43 +07:00
payload: "", //แนบไฟล์
isSendMail: true,
isSendInbox: true,
2024-10-11 10:17:44 +07:00
isSendNotification: true,
2024-10-09 16:20:43 +07:00
})
.catch((error) => {
console.error("Error calling API:", error);
});
2024-10-16 11:55:45 +07:00
workflow.stateId = _state.id;
await this.workflowRepo.save(workflow);
2024-10-09 16:20:43 +07:00
}
2024-10-09 14:14:36 +07:00
return new HttpSuccess({
stateId: _state?.id || null,
2024-10-16 11:55:45 +07:00
stateNo: _state?.order || null,
2024-10-09 14:14:36 +07:00
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,
},
});
2024-10-16 11:55:45 +07:00
if (!state) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลขั้นตอนการอนุมัติ");
2024-10-09 14:14:36 +07:00
const _state = await this.stateRepo.findOne({
where: {
order: state.order - 1,
workflowId: state.workflowId,
},
});
return new HttpSuccess({
stateId: _state?.id || null,
2024-10-16 11:55:45 +07:00
stateNo: _state?.order || null,
2024-10-09 14:14:36 +07:00
stateName: _state?.name || null,
stateType: _state?.type || null,
});
}
2024-10-16 11:55:45 +07:00
@Post("add-step")
public async addStep(
@Request() req: RequestWithUser,
@Body()
body: {
stateId: string;
profileId: string;
isAcceptSetting: boolean;
isApproveSetting: boolean;
isReasonSetting: boolean;
},
) {
const profile = await this.profileRepo.findOne({
where: {
id: body.profileId,
},
});
if (!profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบผู้ใช้งานนี้");
const state = await this.stateRepo.findOne({
where: {
id: body.stateId,
},
relations: ["stateUserComments"],
});
if (!state) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลขั้นตอนการอนุมัติ");
const stateUserComment = new StateUserComment();
stateUserComment.order = state.stateUserComments.length + 1;
stateUserComment.stateId = body.stateId;
stateUserComment.profileId = body.profileId;
stateUserComment.isAcceptSetting = body.isAcceptSetting;
stateUserComment.isApproveSetting = body.isApproveSetting;
stateUserComment.isReasonSetting = body.isReasonSetting;
stateUserComment.createdUserId = req.user.sub;
stateUserComment.createdFullName = req.user.name;
stateUserComment.createdAt = new Date();
stateUserComment.lastUpdateUserId = req.user.sub;
stateUserComment.lastUpdateFullName = req.user.name;
stateUserComment.lastUpdatedAt = new Date();
await this.stateUserCommentRepo.save(stateUserComment);
await new CallAPI()
.PostData(req, "/placement/noti/profiles", {
subject: `ได้รับรายการ`,
body: `ได้รับรายการ`,
receiverUserIds: [body.profileId],
payload: "", //แนบไฟล์
isSendMail: true,
isSendInbox: true,
isSendNotification: true,
})
.catch((error) => {
console.error("Error calling API:", error);
});
return new HttpSuccess();
}
@Post("comment")
public async createcomment(
@Request() req: RequestWithUser,
@Body()
body: {
stateId: string;
isAccept?: boolean | null;
isApprove?: boolean | null;
reason?: string | null;
},
) {
const profile = await this.profileRepo.findOne({
where: {
keycloak: req.user.sub,
},
});
if (!profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลผู้ใช้งาน");
const state = await this.stateRepo.findOne({
where: {
id: body.stateId,
},
});
if (!state) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลขั้นตอนการอนุมัติ");
let _null: any = null;
const stateUserComment = new StateUserComment();
stateUserComment.stateId = body.stateId;
stateUserComment.profileId = profile.id;
stateUserComment.isAccept = body.isAccept == null ? _null : body.isAccept;
stateUserComment.isApprove = body.isApprove == null ? _null : body.isAccept;
stateUserComment.reason = body.reason == null ? _null : body.isAccept;
stateUserComment.createdUserId = req.user.sub;
stateUserComment.createdFullName = req.user.name;
stateUserComment.createdAt = new Date();
stateUserComment.lastUpdateUserId = req.user.sub;
stateUserComment.lastUpdateFullName = req.user.name;
stateUserComment.lastUpdatedAt = new Date();
await this.stateUserCommentRepo.save(stateUserComment);
return new HttpSuccess();
}
@Post("comment-state")
public async getCommentState(
@Request() req: RequestWithUser,
@Body()
body: {
stateId: string;
},
) {
const state = await this.stateRepo.findOne({
where: {
id: body.stateId,
},
order: { stateUserComments: { order: "ASC" } },
relations: ["stateUserComments"],
});
if (!state) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลขั้นตอนการอนุมัติ");
return new HttpSuccess(state.stateUserComments);
}
@Post("comment-state-user")
public async getCommentStateUser(
@Request() req: RequestWithUser,
@Body()
body: {
stateId: string;
},
) {
const stateUserComment = await this.stateUserCommentRepo.findOne({
where: {
2024-10-16 14:40:37 +07:00
profile: {
keycloak: req.user.sub,
},
2024-10-16 11:55:45 +07:00
stateId: body.stateId,
},
});
return new HttpSuccess({
isAccept: stateUserComment?.isAccept || null,
isApprove: stateUserComment?.isApprove || null,
reason: stateUserComment?.reason || null,
isAcceptSetting: stateUserComment?.isAcceptSetting || null,
isApproveSetting: stateUserComment?.isApproveSetting || null,
isReasonSetting: stateUserComment?.isReasonSetting || null,
order: stateUserComment?.order || null,
stateId: stateUserComment?.stateId || null,
profileId: stateUserComment?.profileId || null,
});
}
2024-10-09 14:14:36 +07:00
}