2024-11-08 10:19:11 +07:00
|
|
|
import { Body, Controller, Get, Path, Post, Put, Request, Route, Security, Tags } from "tsoa";
|
2024-10-09 14:14:36 +07:00
|
|
|
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 { PosMaster } from "../entities/PosMaster";
|
2024-11-27 16:09:18 +07:00
|
|
|
import { Brackets, IsNull, Not } from "typeorm";
|
2024-10-21 10:22:39 +07:00
|
|
|
import { Assign } from "../entities/Assign";
|
2024-10-22 11:59:12 +07:00
|
|
|
import { PosMasterAct } from "../entities/PosMasterAct";
|
2024-11-08 10:19:11 +07:00
|
|
|
import { viewDirectorActing } from "../entities/view/viewDirectorActing";
|
|
|
|
|
import { viewDirector } from "../entities/view/viewDirector";
|
2024-11-19 10:43:25 +07:00
|
|
|
import { ProfileEmployee } from "../entities/ProfileEmployee";
|
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-11-19 10:43:25 +07:00
|
|
|
private profileEmployeeRepo = AppDataSource.getRepository(ProfileEmployee);
|
2024-10-09 16:20:43 +07:00
|
|
|
|
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-22 11:59:12 +07:00
|
|
|
private posMasterActRepo = AppDataSource.getRepository(PosMasterAct);
|
2024-10-21 10:22:39 +07:00
|
|
|
private assignRepo = AppDataSource.getRepository(Assign);
|
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-11-19 10:43:25 +07:00
|
|
|
let profileType = "OFFICER";
|
|
|
|
|
let profile: any = await this.profileRepo.findOne({
|
2024-10-16 14:40:37 +07:00
|
|
|
where: {
|
|
|
|
|
keycloak: req.user.sub,
|
|
|
|
|
},
|
|
|
|
|
});
|
2024-11-19 10:43:25 +07:00
|
|
|
if (!profile) {
|
|
|
|
|
profileType = "EMPLOYEE";
|
|
|
|
|
profile = await this.profileEmployeeRepo.findOne({
|
|
|
|
|
where: {
|
|
|
|
|
keycloak: req.user.sub,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
if (!profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลผู้ใช้งาน");
|
|
|
|
|
}
|
2024-10-16 14:40:37 +07:00
|
|
|
|
|
|
|
|
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,
|
2024-11-19 10:43:25 +07:00
|
|
|
profileType: profileType,
|
2024-10-16 17:15:55 +07:00
|
|
|
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-11-29 09:56:00 +07:00
|
|
|
let num = 1;
|
2024-10-16 14:40:37 +07:00
|
|
|
const stateOperatorUser = new StateOperatorUser();
|
2024-11-27 16:20:03 +07:00
|
|
|
if (profile) {
|
|
|
|
|
Object.assign(stateOperatorUser, {
|
|
|
|
|
profileId: profileType == "OFFICER" ? profile.id : null,
|
|
|
|
|
profileEmployeeId: profileType != "OFFICER" ? profile.id : null,
|
|
|
|
|
profileType: profileType,
|
|
|
|
|
operator: "Owner",
|
2024-11-29 09:56:00 +07:00
|
|
|
order: num,
|
2024-11-27 16:20:03 +07:00
|
|
|
workflowId: workflow.id,
|
|
|
|
|
...meta,
|
|
|
|
|
});
|
|
|
|
|
}
|
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) => {
|
2024-11-27 16:20:03 +07:00
|
|
|
if (item.current_holderId) {
|
2024-11-29 09:56:00 +07:00
|
|
|
num = num + 1;
|
2024-11-27 16:20:03 +07:00
|
|
|
if (item.orgChild1 == null || item.orgChild1.isOfficer == false) {
|
|
|
|
|
const stateOperatorUser = new StateOperatorUser();
|
|
|
|
|
Object.assign(stateOperatorUser, {
|
|
|
|
|
profileId: item.current_holderId,
|
|
|
|
|
operator: "Officer",
|
2024-11-29 10:18:20 +07:00
|
|
|
profileType: "OFFICER",
|
2024-11-29 09:56:00 +07:00
|
|
|
order: num,
|
2024-11-27 16:20:03 +07:00
|
|
|
workflowId: workflow.id,
|
|
|
|
|
...meta,
|
|
|
|
|
});
|
|
|
|
|
await this.stateOperatorUserRepo.save(stateOperatorUser);
|
|
|
|
|
} else {
|
|
|
|
|
const stateOperatorUser = new StateOperatorUser();
|
|
|
|
|
Object.assign(stateOperatorUser, {
|
|
|
|
|
profileId: item.current_holderId,
|
|
|
|
|
operator: "PersonnelOfficer",
|
2024-11-29 10:18:20 +07:00
|
|
|
profileType: "OFFICER",
|
2024-11-29 09:56:00 +07:00
|
|
|
order: num,
|
2024-11-27 16:20:03 +07:00
|
|
|
workflowId: workflow.id,
|
|
|
|
|
...meta,
|
|
|
|
|
});
|
|
|
|
|
await this.stateOperatorUserRepo.save(stateOperatorUser);
|
|
|
|
|
}
|
2024-10-16 17:15:55 +07:00
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
|
2024-10-29 09:38:06 +07:00
|
|
|
const _workflow = await this.workflowRepo.findOne({
|
|
|
|
|
where: { id: workflow.id },
|
|
|
|
|
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, "ไม่พบข้อมูลขั้นตอนการอนุมัติ");
|
|
|
|
|
let profileNow = _workflow.stateOperatorUsers
|
|
|
|
|
.filter((x) => _state.stateOperators.map((s) => s.operator).includes(x.operator))
|
|
|
|
|
.map((x) => ({
|
2024-11-20 16:06:48 +07:00
|
|
|
receiverUserId: x.profileType == "OFFICER" ? x.profileId : x.profileEmployeeId,
|
2024-10-29 09:38:06 +07:00
|
|
|
notiLink: "",
|
|
|
|
|
}));
|
|
|
|
|
await new CallAPI()
|
|
|
|
|
.PostData(req, "/placement/noti/profiles", {
|
|
|
|
|
subject: `รายการถูกส่ง`,
|
|
|
|
|
body: `รายการถูกส่ง`,
|
|
|
|
|
receiverUserIds: profileNow,
|
|
|
|
|
payload: "", //แนบไฟล์
|
|
|
|
|
isSendMail: true,
|
|
|
|
|
isSendInbox: true,
|
|
|
|
|
isSendNotification: true,
|
|
|
|
|
})
|
|
|
|
|
.catch((error) => {
|
|
|
|
|
console.error("Error calling API:", error);
|
|
|
|
|
});
|
|
|
|
|
|
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-11-19 10:43:25 +07:00
|
|
|
let stateOperatorUser = await this.stateOperatorUserRepo.findOne({
|
2024-10-09 16:20:43 +07:00
|
|
|
where: {
|
2024-10-16 11:55:45 +07:00
|
|
|
profileId: body.profileId,
|
2024-10-09 16:20:43 +07:00
|
|
|
workflowId: body.workflowId,
|
|
|
|
|
},
|
|
|
|
|
});
|
2024-11-19 10:43:25 +07:00
|
|
|
if (!stateOperatorUser) {
|
|
|
|
|
stateOperatorUser = await this.stateOperatorUserRepo.findOne({
|
|
|
|
|
where: {
|
|
|
|
|
profileEmployeeId: body.profileId,
|
|
|
|
|
workflowId: body.workflowId,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
if (!stateOperatorUser)
|
|
|
|
|
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-11-20 16:06:48 +07:00
|
|
|
const workflow = await this.workflowRepo.findOne({
|
|
|
|
|
where: {
|
|
|
|
|
refId: body.refId,
|
|
|
|
|
sysName: body.system,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
if (!workflow) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่สามารถดำเนินการกระบวนการนี้ได้");
|
|
|
|
|
|
|
|
|
|
let 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-11-27 16:09:18 +07:00
|
|
|
if (!stateOperatorUser) {
|
2024-11-20 16:06:48 +07:00
|
|
|
stateOperatorUser = await this.stateOperatorUserRepo.findOne({
|
|
|
|
|
where: {
|
|
|
|
|
workflow: {
|
|
|
|
|
refId: body.refId,
|
|
|
|
|
sysName: body.system,
|
|
|
|
|
},
|
|
|
|
|
profileEmployee: {
|
|
|
|
|
keycloak: req.user.sub,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
relations: ["workflow"],
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-10-09 16:20:43 +07:00
|
|
|
const operator = await this.stateOperatorRepo.findOne({
|
|
|
|
|
where: {
|
2024-10-18 11:04:46 +07:00
|
|
|
operator: stateOperatorUser?.operator || "",
|
|
|
|
|
stateId: workflow.stateId,
|
2024-10-16 11:55:45 +07:00
|
|
|
},
|
|
|
|
|
relations: ["state"],
|
|
|
|
|
});
|
2024-10-17 12:10:23 +07:00
|
|
|
if (!operator) {
|
|
|
|
|
const state = await this.stateRepo.findOne({
|
|
|
|
|
where: {
|
2024-10-18 11:04:46 +07:00
|
|
|
id: workflow.stateId,
|
2024-10-17 12:10:23 +07:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
return new HttpSuccess({
|
|
|
|
|
stateId: state?.id || null,
|
|
|
|
|
stateNo: state?.order || null,
|
|
|
|
|
stateName: state?.name || null,
|
2024-10-18 11:04:46 +07:00
|
|
|
operator: stateOperatorUser?.operator || null,
|
2024-10-17 12:10:23 +07:00
|
|
|
can_view: false,
|
|
|
|
|
can_update: false,
|
|
|
|
|
can_operate: false,
|
|
|
|
|
can_change_state: false,
|
|
|
|
|
can_delete: false,
|
|
|
|
|
can_cancel: false,
|
2024-10-22 13:59:59 +07:00
|
|
|
can_sign: false,
|
2024-10-17 12:10:23 +07:00
|
|
|
});
|
|
|
|
|
}
|
2024-10-16 11:55:45 +07:00
|
|
|
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,
|
2024-10-22 13:59:59 +07:00
|
|
|
can_sign: operator.canSign,
|
2024-10-16 11:55:45 +07:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@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-17 11:06:58 +07:00
|
|
|
relations: ["stateUserComments", "stateUserComments.profile"],
|
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,
|
2024-10-17 11:06:58 +07:00
|
|
|
stateUserComments: x.stateUserComments.map((x) => ({
|
|
|
|
|
id: x.id,
|
|
|
|
|
prefix: x.profile.prefix,
|
|
|
|
|
firstName: x.profile.firstName,
|
|
|
|
|
lastName: x.profile.lastName,
|
2024-10-18 11:04:46 +07:00
|
|
|
isComment: x.profile.keycloak == req.user.sub ? true : false,
|
2024-10-17 11:06:58 +07:00
|
|
|
profileId: x.profileId,
|
|
|
|
|
isAcceptSetting: x.isAcceptSetting,
|
|
|
|
|
isApproveSetting: x.isApproveSetting,
|
|
|
|
|
isReasonSetting: x.isReasonSetting,
|
|
|
|
|
isAccept: x.isAccept,
|
|
|
|
|
isApprove: x.isApprove,
|
|
|
|
|
reason: x.reason,
|
|
|
|
|
})),
|
2024-10-16 11:55:45 +07:00
|
|
|
}));
|
|
|
|
|
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-11-20 16:06:48 +07:00
|
|
|
const workflow = await this.workflowRepo.findOne({
|
|
|
|
|
where: {
|
|
|
|
|
refId: body.refId,
|
|
|
|
|
sysName: body.system,
|
|
|
|
|
},
|
|
|
|
|
relations: ["stateOperatorUsers"],
|
|
|
|
|
});
|
|
|
|
|
if (!workflow) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่สามารถดำเนินการกระบวนการนี้ได้");
|
|
|
|
|
|
|
|
|
|
let stateOperatorUserNow = await this.stateOperatorUserRepo.findOne({
|
2024-10-17 11:33:35 +07:00
|
|
|
where: {
|
|
|
|
|
workflow: {
|
|
|
|
|
refId: body.refId,
|
|
|
|
|
sysName: body.system,
|
|
|
|
|
},
|
|
|
|
|
profile: {
|
|
|
|
|
keycloak: req.user.sub,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
2024-11-27 16:09:18 +07:00
|
|
|
if (!stateOperatorUserNow) {
|
2024-11-20 16:06:48 +07:00
|
|
|
stateOperatorUserNow = await this.stateOperatorUserRepo.findOne({
|
|
|
|
|
where: {
|
|
|
|
|
workflow: {
|
|
|
|
|
refId: body.refId,
|
|
|
|
|
sysName: body.system,
|
|
|
|
|
},
|
|
|
|
|
profileEmployee: {
|
|
|
|
|
keycloak: req.user.sub,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
let stateOperatorUser = await this.stateOperatorUserRepo.find({
|
2024-10-16 14:40:37 +07:00
|
|
|
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: {
|
2024-10-16 17:49:55 +07:00
|
|
|
keycloak: Not(req.user.sub),
|
2024-10-16 14:40:37 +07:00
|
|
|
},
|
2024-10-17 11:33:35 +07:00
|
|
|
operator: stateOperatorUserNow?.operator || "",
|
2024-10-16 14:40:37 +07:00
|
|
|
},
|
|
|
|
|
});
|
2024-11-27 16:09:18 +07:00
|
|
|
if (!stateOperatorUser) {
|
2024-11-20 16:06:48 +07:00
|
|
|
stateOperatorUser = await this.stateOperatorUserRepo.find({
|
|
|
|
|
where: {
|
|
|
|
|
workflow: {
|
|
|
|
|
refId: body.refId,
|
|
|
|
|
sysName: body.system,
|
|
|
|
|
},
|
|
|
|
|
profileEmployee: {
|
|
|
|
|
keycloak: Not(req.user.sub),
|
|
|
|
|
},
|
|
|
|
|
operator: stateOperatorUserNow?.operator || "",
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-10-16 17:49:55 +07:00
|
|
|
await this.stateOperatorUserRepo.remove(stateOperatorUser);
|
2024-10-16 14:40:37 +07:00
|
|
|
|
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))
|
2024-10-22 14:36:25 +07:00
|
|
|
.map((x) => ({
|
2024-11-20 16:06:48 +07:00
|
|
|
receiverUserId: x.profileType == "OFFICER" ? x.profileId : x.profileEmployeeId,
|
2024-10-22 14:36:25 +07:00
|
|
|
notiLink: "",
|
|
|
|
|
}));
|
2024-10-09 16:20:43 +07:00
|
|
|
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))
|
2024-10-22 14:36:25 +07:00
|
|
|
.map((x) => ({
|
2024-11-20 16:06:48 +07:00
|
|
|
receiverUserId: x.profileType == "OFFICER" ? x.profileId : x.profileEmployeeId,
|
2024-10-22 14:36:25 +07:00
|
|
|
notiLink: "",
|
|
|
|
|
}));
|
2024-10-09 16:20:43 +07:00
|
|
|
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,
|
|
|
|
|
},
|
2024-10-21 10:22:39 +07:00
|
|
|
relations: ["stateUserComments", "workflow"],
|
2024-10-16 11:55:45 +07:00
|
|
|
});
|
|
|
|
|
if (!state) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลขั้นตอนการอนุมัติ");
|
|
|
|
|
|
2024-11-29 18:09:16 +07:00
|
|
|
if (state.stateUserComments.filter((x) => x.profileId == body.profileId).length > 0)
|
2024-12-11 23:59:49 +07:00
|
|
|
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่สามารถเลือกซ้ำได้");
|
2024-11-29 18:09:16 +07:00
|
|
|
|
2024-10-16 11:55:45 +07:00
|
|
|
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);
|
2024-10-21 10:22:39 +07:00
|
|
|
state.workflow.sysName;
|
|
|
|
|
const assign = await this.assignRepo.findOne({
|
|
|
|
|
where: {
|
|
|
|
|
id: state.workflow.sysName,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
if (!assign) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลขั้นตอนการอนุมัติ");
|
2024-10-16 11:55:45 +07:00
|
|
|
await new CallAPI()
|
|
|
|
|
.PostData(req, "/placement/noti/profiles", {
|
|
|
|
|
subject: `ได้รับรายการ`,
|
|
|
|
|
body: `ได้รับรายการ`,
|
2024-10-21 10:22:39 +07:00
|
|
|
receiverUserIds: [
|
|
|
|
|
{
|
|
|
|
|
receiverUserId: body.profileId,
|
|
|
|
|
notiLink: `${assign.path}/${state.workflow.refId}`,
|
|
|
|
|
},
|
|
|
|
|
],
|
2024-10-16 11:55:45 +07:00
|
|
|
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: {
|
2024-10-18 11:33:04 +07:00
|
|
|
stateUserCommentId: string;
|
2024-10-16 11:55:45 +07:00
|
|
|
isAccept?: boolean | null;
|
|
|
|
|
isApprove?: boolean | null;
|
|
|
|
|
reason?: string | null;
|
|
|
|
|
},
|
|
|
|
|
) {
|
2024-10-18 11:33:04 +07:00
|
|
|
const stateUserComment = await this.stateUserCommentRepo.findOne({
|
2024-10-16 11:55:45 +07:00
|
|
|
where: {
|
2024-10-18 11:33:04 +07:00
|
|
|
id: body.stateUserCommentId,
|
2024-10-16 11:55:45 +07:00
|
|
|
},
|
2024-10-22 16:09:46 +07:00
|
|
|
relations: ["state", "state.workflow", "state.stateOperators"],
|
2024-10-16 11:55:45 +07:00
|
|
|
});
|
2024-10-18 11:33:04 +07:00
|
|
|
if (!stateUserComment)
|
|
|
|
|
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลขั้นตอนการอนุมัติ");
|
2024-10-22 16:09:46 +07:00
|
|
|
stateUserComment.state.stateOperators;
|
|
|
|
|
|
|
|
|
|
const workflow = await this.workflowRepo.findOne({
|
|
|
|
|
where: {
|
|
|
|
|
refId: stateUserComment.state.workflow.refId,
|
|
|
|
|
sysName: stateUserComment.state.workflow.sysName,
|
|
|
|
|
},
|
|
|
|
|
relations: ["stateOperatorUsers"],
|
|
|
|
|
});
|
|
|
|
|
if (!workflow) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่สามารถดำเนินการกระบวนการนี้ได้");
|
|
|
|
|
let profileNow = workflow.stateOperatorUsers
|
|
|
|
|
.filter((x) =>
|
|
|
|
|
stateUserComment.state.stateOperators.map((s) => s.operator).includes(x.operator),
|
|
|
|
|
)
|
|
|
|
|
.map((x) => ({
|
2024-11-20 16:06:48 +07:00
|
|
|
receiverUserId: x.profileType == "OFFICER" ? x.profileId : x.profileEmployeeId,
|
2024-10-22 16:09:46 +07:00
|
|
|
notiLink: "",
|
|
|
|
|
}));
|
|
|
|
|
await new CallAPI()
|
|
|
|
|
.PostData(req, "/placement/noti/profiles", {
|
|
|
|
|
subject: `ผู้บังคับบัญชาดำเนินการ`,
|
|
|
|
|
body: `ผู้บังคับบัญชาดำเนินการ`,
|
|
|
|
|
receiverUserIds: profileNow,
|
|
|
|
|
payload: "", //แนบไฟล์
|
|
|
|
|
isSendMail: true,
|
|
|
|
|
isSendInbox: true,
|
|
|
|
|
isSendNotification: true,
|
|
|
|
|
})
|
|
|
|
|
.catch((error) => {
|
|
|
|
|
console.error("Error calling API:", error);
|
|
|
|
|
});
|
2024-10-16 11:55:45 +07:00
|
|
|
let _null: any = null;
|
|
|
|
|
stateUserComment.isAccept = body.isAccept == null ? _null : body.isAccept;
|
2024-10-18 11:45:16 +07:00
|
|
|
stateUserComment.isApprove = body.isApprove == null ? _null : body.isApprove;
|
|
|
|
|
stateUserComment.reason = body.reason == null ? _null : body.reason;
|
2024-10-16 11:55:45 +07:00
|
|
|
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({
|
2024-10-18 11:33:04 +07:00
|
|
|
id: stateUserComment?.id || null,
|
2024-10-16 11:55:45 +07:00
|
|
|
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-17 09:46:55 +07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
*/
|
2024-11-08 10:19:11 +07:00
|
|
|
@Put("commander/{type}")
|
|
|
|
|
async getProfilePlacement(
|
|
|
|
|
@Request() request: RequestWithUser,
|
|
|
|
|
@Path() type: string,
|
|
|
|
|
@Body()
|
|
|
|
|
body: {
|
|
|
|
|
isAct: boolean;
|
|
|
|
|
keyword: string;
|
|
|
|
|
page: number;
|
|
|
|
|
pageSize: number;
|
|
|
|
|
},
|
|
|
|
|
) {
|
2024-10-17 09:46:55 +07:00
|
|
|
const posMasterUser = await this.posMasterRepo.findOne({
|
|
|
|
|
where: {
|
2024-10-17 09:55:06 +07:00
|
|
|
orgRevision: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false },
|
2024-11-08 10:19:11 +07:00
|
|
|
current_holder: { keycloak: request.user.sub },
|
2024-10-17 09:46:55 +07:00
|
|
|
},
|
2024-10-22 11:59:12 +07:00
|
|
|
relations: ["current_holder", "current_holder.posType", "current_holder.posLevel"],
|
2024-10-17 09:46:55 +07:00
|
|
|
});
|
|
|
|
|
if (!posMasterUser || !posMasterUser.orgRootId)
|
|
|
|
|
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบตำแหน่งผู้ใช้งาน");
|
2024-11-08 10:19:11 +07:00
|
|
|
|
|
|
|
|
let condition: any = {
|
2024-11-08 11:46:27 +07:00
|
|
|
isDirector: true,
|
|
|
|
|
orgRootId: posMasterUser.orgRootId,
|
2025-01-22 15:15:29 +07:00
|
|
|
orgRevisionId: posMasterUser.orgRevisionId,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let conditionOfficer: any = {
|
|
|
|
|
isDirector: true,
|
|
|
|
|
isOfficer: true,
|
|
|
|
|
orgRevisionId: posMasterUser.orgRevisionId,
|
2024-10-22 11:59:12 +07:00
|
|
|
};
|
2024-10-17 09:46:55 +07:00
|
|
|
|
2024-12-15 20:40:11 +07:00
|
|
|
if (type.trim().toUpperCase() == "OPERATE") {
|
2024-10-22 11:59:12 +07:00
|
|
|
} else {
|
|
|
|
|
if (
|
|
|
|
|
(posMasterUser.current_holder.posType.posTypeName == "ทั่วไป" &&
|
|
|
|
|
posMasterUser.current_holder.posLevel.posLevelName == "ชำนาญงาน") ||
|
|
|
|
|
(posMasterUser.current_holder.posType.posTypeName == "ทั่วไป" &&
|
|
|
|
|
posMasterUser.current_holder.posLevel.posLevelName == "ปฏิบัติงาน") ||
|
|
|
|
|
(posMasterUser.current_holder.posType.posTypeName == "วิชาการ" &&
|
|
|
|
|
posMasterUser.current_holder.posLevel.posLevelName == "ปฏิบัติการ") ||
|
|
|
|
|
(posMasterUser.current_holder.posType.posTypeName == "วิชาการ" &&
|
|
|
|
|
posMasterUser.current_holder.posLevel.posLevelName == "ชำนาญการ")
|
|
|
|
|
) {
|
2024-11-08 10:19:11 +07:00
|
|
|
condition = {
|
2024-11-08 11:46:27 +07:00
|
|
|
isDirector: true,
|
|
|
|
|
orgRootId: posMasterUser.orgRootId,
|
2025-01-22 15:15:29 +07:00
|
|
|
orgRevisionId: posMasterUser.orgRevisionId,
|
2024-11-08 11:46:27 +07:00
|
|
|
orgChild1Id: IsNull(),
|
|
|
|
|
orgChild2Id: IsNull(),
|
|
|
|
|
orgChild3Id: IsNull(),
|
|
|
|
|
orgChild4Id: IsNull(),
|
2024-10-22 11:59:12 +07:00
|
|
|
};
|
|
|
|
|
} else if (
|
|
|
|
|
(posMasterUser.current_holder.posType.posTypeName == "ทั่วไป" &&
|
|
|
|
|
posMasterUser.current_holder.posLevel.posLevelName == "อาวุโส") ||
|
|
|
|
|
(posMasterUser.current_holder.posType.posTypeName == "วิชาการ" &&
|
|
|
|
|
posMasterUser.current_holder.posLevel.posLevelName == "ชำนาญการพิเศษ") ||
|
|
|
|
|
(posMasterUser.current_holder.posType.posTypeName == "อำนวยการ" &&
|
|
|
|
|
posMasterUser.current_holder.posLevel.posLevelName == "ต้น")
|
|
|
|
|
) {
|
2024-11-08 10:19:11 +07:00
|
|
|
condition = {
|
2024-11-08 11:46:27 +07:00
|
|
|
isDirector: true,
|
|
|
|
|
isDeputy: true,
|
2025-01-22 15:15:29 +07:00
|
|
|
orgRevisionId: posMasterUser.orgRevisionId,
|
2024-11-08 11:46:27 +07:00
|
|
|
orgChild1Id: IsNull(),
|
|
|
|
|
orgChild2Id: IsNull(),
|
|
|
|
|
orgChild3Id: IsNull(),
|
|
|
|
|
orgChild4Id: IsNull(),
|
2024-10-22 11:59:12 +07:00
|
|
|
};
|
|
|
|
|
} else {
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-11-08 10:19:11 +07:00
|
|
|
|
|
|
|
|
if (body.isAct == true) {
|
|
|
|
|
const [lists, total] = await AppDataSource.getRepository(viewDirectorActing)
|
|
|
|
|
.createQueryBuilder("viewDirectorActing")
|
2025-01-22 15:15:29 +07:00
|
|
|
.andWhere(
|
|
|
|
|
new Brackets((qb) => {
|
|
|
|
|
qb.orWhere(condition).orWhere(conditionOfficer);
|
|
|
|
|
}),
|
|
|
|
|
)
|
2024-11-08 10:19:11 +07:00
|
|
|
.andWhere(
|
|
|
|
|
new Brackets((qb) => {
|
|
|
|
|
qb.orWhere(
|
|
|
|
|
body.keyword != null && body.keyword != ""
|
|
|
|
|
? "CONCAT(viewDirectorActing.prefix,viewDirectorActing.firstName,' ',viewDirectorActing.lastName) LIKE :keyword"
|
|
|
|
|
: "1=1",
|
|
|
|
|
{
|
|
|
|
|
keyword: `%${body.keyword}%`,
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
.orWhere(
|
|
|
|
|
body.keyword != null && body.keyword != ""
|
|
|
|
|
? "viewDirectorActing.citizenId LIKE :keyword"
|
|
|
|
|
: "1=1",
|
|
|
|
|
{
|
|
|
|
|
keyword: `%${body.keyword}%`,
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
.orWhere(
|
|
|
|
|
body.keyword != null && body.keyword != ""
|
|
|
|
|
? "viewDirectorActing.position LIKE :keyword"
|
|
|
|
|
: "1=1",
|
|
|
|
|
{
|
|
|
|
|
keyword: `%${body.keyword}%`,
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
.orWhere(
|
|
|
|
|
body.keyword != null && body.keyword != ""
|
|
|
|
|
? "viewDirectorActing.posLevel LIKE :keyword"
|
|
|
|
|
: "1=1",
|
|
|
|
|
{
|
|
|
|
|
keyword: `%${body.keyword}%`,
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
.orWhere(
|
|
|
|
|
body.keyword != null && body.keyword != ""
|
|
|
|
|
? "viewDirectorActing.posType LIKE :keyword"
|
|
|
|
|
: "1=1",
|
|
|
|
|
{
|
|
|
|
|
keyword: `%${body.keyword}%`,
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
.orWhere(
|
|
|
|
|
body.keyword != null && body.keyword != ""
|
|
|
|
|
? "viewDirectorActing.actFullName LIKE :keyword"
|
|
|
|
|
: "1=1",
|
|
|
|
|
{
|
|
|
|
|
keyword: `%${body.keyword}%`,
|
|
|
|
|
},
|
2024-11-29 18:09:16 +07:00
|
|
|
)
|
|
|
|
|
.orWhere(
|
2024-11-29 13:15:40 +07:00
|
|
|
body.keyword != null && body.keyword != ""
|
|
|
|
|
? "viewDirectorActing.posNo LIKE :keyword"
|
|
|
|
|
: "1=1",
|
|
|
|
|
{
|
|
|
|
|
keyword: `%${body.keyword}%`,
|
|
|
|
|
},
|
2024-11-29 18:09:16 +07:00
|
|
|
)
|
|
|
|
|
.orWhere(
|
2024-11-29 13:15:40 +07:00
|
|
|
body.keyword != null && body.keyword != ""
|
|
|
|
|
? "viewDirectorActing.posExecutiveName LIKE :keyword"
|
|
|
|
|
: "1=1",
|
|
|
|
|
{
|
|
|
|
|
keyword: `%${body.keyword}%`,
|
|
|
|
|
},
|
2024-11-08 10:19:11 +07:00
|
|
|
);
|
|
|
|
|
}),
|
|
|
|
|
)
|
|
|
|
|
.skip((body.page - 1) * body.pageSize)
|
|
|
|
|
.take(body.pageSize)
|
|
|
|
|
.getManyAndCount();
|
|
|
|
|
return new HttpSuccess({ data: lists, total });
|
|
|
|
|
} else {
|
|
|
|
|
const [lists, total] = await AppDataSource.getRepository(viewDirector)
|
|
|
|
|
.createQueryBuilder("viewDirector")
|
2025-01-22 15:15:29 +07:00
|
|
|
.andWhere(
|
|
|
|
|
new Brackets((qb) => {
|
|
|
|
|
qb.orWhere(condition).orWhere(conditionOfficer);
|
|
|
|
|
}),
|
|
|
|
|
)
|
2024-11-08 10:19:11 +07:00
|
|
|
.andWhere(
|
|
|
|
|
new Brackets((qb) => {
|
|
|
|
|
qb.orWhere(
|
|
|
|
|
body.keyword != null && body.keyword != ""
|
|
|
|
|
? "CONCAT(viewDirector.prefix,viewDirector.firstName,' ',viewDirector.lastName) LIKE :keyword"
|
|
|
|
|
: "1=1",
|
|
|
|
|
{
|
|
|
|
|
keyword: `%${body.keyword}%`,
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
.orWhere(
|
|
|
|
|
body.keyword != null && body.keyword != ""
|
|
|
|
|
? "viewDirector.citizenId LIKE :keyword"
|
|
|
|
|
: "1=1",
|
|
|
|
|
{
|
|
|
|
|
keyword: `%${body.keyword}%`,
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
.orWhere(
|
|
|
|
|
body.keyword != null && body.keyword != ""
|
|
|
|
|
? "viewDirector.position LIKE :keyword"
|
|
|
|
|
: "1=1",
|
|
|
|
|
{
|
|
|
|
|
keyword: `%${body.keyword}%`,
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
.orWhere(
|
|
|
|
|
body.keyword != null && body.keyword != ""
|
|
|
|
|
? "viewDirector.posLevel LIKE :keyword"
|
|
|
|
|
: "1=1",
|
|
|
|
|
{
|
|
|
|
|
keyword: `%${body.keyword}%`,
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
.orWhere(
|
|
|
|
|
body.keyword != null && body.keyword != ""
|
|
|
|
|
? "viewDirector.posType LIKE :keyword"
|
|
|
|
|
: "1=1",
|
|
|
|
|
{
|
|
|
|
|
keyword: `%${body.keyword}%`,
|
2024-11-29 13:15:40 +07:00
|
|
|
},
|
2024-11-29 18:09:16 +07:00
|
|
|
)
|
|
|
|
|
.orWhere(
|
2024-11-29 13:15:40 +07:00
|
|
|
body.keyword != null && body.keyword != ""
|
|
|
|
|
? "viewDirector.posNo LIKE :keyword"
|
|
|
|
|
: "1=1",
|
|
|
|
|
{
|
|
|
|
|
keyword: `%${body.keyword}%`,
|
|
|
|
|
},
|
2024-11-29 18:09:16 +07:00
|
|
|
)
|
|
|
|
|
.orWhere(
|
2024-11-29 13:15:40 +07:00
|
|
|
body.keyword != null && body.keyword != ""
|
|
|
|
|
? "viewDirector.posExecutiveName LIKE :keyword"
|
|
|
|
|
: "1=1",
|
|
|
|
|
{
|
|
|
|
|
keyword: `%${body.keyword}%`,
|
2024-11-08 10:19:11 +07:00
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}),
|
|
|
|
|
)
|
|
|
|
|
.skip((body.page - 1) * body.pageSize)
|
|
|
|
|
.take(body.pageSize)
|
|
|
|
|
.getManyAndCount();
|
|
|
|
|
return new HttpSuccess({ data: lists, total });
|
|
|
|
|
}
|
2024-10-17 09:46:55 +07:00
|
|
|
}
|
2024-10-17 15:13:59 +07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* API เช็ค สกจ
|
|
|
|
|
*
|
|
|
|
|
* @summary เช็ค สกจ
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
@Get("keycloak/isofficer/{system}")
|
|
|
|
|
async getIsOfficerByKeycloak(@Path() system: string, @Request() req: RequestWithUser) {
|
|
|
|
|
const profile = await this.profileRepo.findOne({
|
|
|
|
|
where: {
|
|
|
|
|
keycloak: req.user.sub,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
if (!profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลผู้ใช้งาน");
|
|
|
|
|
|
|
|
|
|
const profileOfficer = await this.posMasterRepo.findOne({
|
|
|
|
|
where: {
|
|
|
|
|
posMasterAssigns: { assignId: system },
|
|
|
|
|
orgRevision: { orgRevisionIsDraft: false, orgRevisionIsCurrent: true },
|
|
|
|
|
current_holderId: profile.id,
|
|
|
|
|
},
|
|
|
|
|
relations: ["orgChild1"],
|
|
|
|
|
});
|
2024-11-14 11:41:52 +07:00
|
|
|
const profileDirector = await this.posMasterRepo.findOne({
|
|
|
|
|
where: {
|
|
|
|
|
orgRevision: { orgRevisionIsDraft: false, orgRevisionIsCurrent: true },
|
|
|
|
|
current_holderId: profile.id,
|
|
|
|
|
},
|
|
|
|
|
});
|
2024-11-14 10:31:14 +07:00
|
|
|
if (!profileOfficer)
|
2024-11-14 11:41:52 +07:00
|
|
|
return new HttpSuccess({
|
|
|
|
|
isOfficer: false,
|
|
|
|
|
isStaff: false,
|
|
|
|
|
isDirector: profileDirector?.isDirector ?? false,
|
|
|
|
|
});
|
2024-10-17 15:13:59 +07:00
|
|
|
let isOfficer = profileOfficer.orgChild1 == null ? false : profileOfficer.orgChild1.isOfficer;
|
2024-11-05 14:56:14 +07:00
|
|
|
return new HttpSuccess({
|
|
|
|
|
isOfficer: isOfficer,
|
|
|
|
|
isStaff: !isOfficer,
|
2024-11-14 11:41:52 +07:00
|
|
|
isDirector: profileDirector?.isDirector ?? false,
|
2024-11-05 14:56:14 +07:00
|
|
|
});
|
2024-10-17 15:13:59 +07:00
|
|
|
}
|
2024-10-21 14:28:11 +07:00
|
|
|
|
2024-11-14 10:31:14 +07:00
|
|
|
/**
|
|
|
|
|
* API เช็ค สกจ
|
|
|
|
|
*
|
|
|
|
|
* @summary เช็ค สกจ
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
@Get("keycloak/isofficer-root/{system}")
|
|
|
|
|
async getIsOfficerByKeycloakRoot(@Path() system: string, @Request() req: RequestWithUser) {
|
|
|
|
|
const profile = await this.profileRepo.findOne({
|
|
|
|
|
where: {
|
|
|
|
|
keycloak: req.user.sub,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
if (!profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลผู้ใช้งาน");
|
|
|
|
|
|
|
|
|
|
const profileOfficer = await this.posMasterRepo.findOne({
|
|
|
|
|
where: {
|
|
|
|
|
posMasterAssigns: { assignId: system },
|
|
|
|
|
orgRevision: { orgRevisionIsDraft: false, orgRevisionIsCurrent: true },
|
|
|
|
|
current_holderId: profile.id,
|
|
|
|
|
},
|
|
|
|
|
relations: ["orgChild1"],
|
|
|
|
|
});
|
2024-11-14 11:41:52 +07:00
|
|
|
const profileDirector = await this.posMasterRepo.findOne({
|
|
|
|
|
where: {
|
|
|
|
|
orgRevision: { orgRevisionIsDraft: false, orgRevisionIsCurrent: true },
|
|
|
|
|
current_holderId: profile.id,
|
|
|
|
|
},
|
|
|
|
|
});
|
2024-11-14 10:31:14 +07:00
|
|
|
if (!profileOfficer)
|
2024-11-14 11:41:52 +07:00
|
|
|
return new HttpSuccess({
|
|
|
|
|
isOfficer: false,
|
|
|
|
|
isStaff: false,
|
|
|
|
|
isDirector: profileDirector?.isDirector ?? false,
|
|
|
|
|
});
|
2024-11-14 10:31:14 +07:00
|
|
|
let isOfficer = profileOfficer.orgChild1 == null ? false : profileOfficer.orgChild1.isOfficer;
|
|
|
|
|
return new HttpSuccess({
|
|
|
|
|
isOfficer: isOfficer,
|
|
|
|
|
isStaff: !isOfficer,
|
2024-11-14 11:41:52 +07:00
|
|
|
isDirector: profileDirector
|
|
|
|
|
? !profileDirector.orgChild1Id && profileDirector.orgRootId
|
|
|
|
|
? profileDirector.isDirector
|
|
|
|
|
: false
|
|
|
|
|
: false,
|
2024-11-14 10:31:14 +07:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-21 14:28:11 +07:00
|
|
|
/**
|
|
|
|
|
* API เช็ค สกจ
|
|
|
|
|
*
|
|
|
|
|
* @summary เช็ค สกจ
|
|
|
|
|
*
|
|
|
|
|
*/
|
2024-10-21 17:38:20 +07:00
|
|
|
@Post("keycloak/isofficer")
|
2024-10-21 14:28:11 +07:00
|
|
|
async checkPermissionWorkflow(
|
|
|
|
|
@Request() req: RequestWithUser,
|
|
|
|
|
@Body()
|
|
|
|
|
body: {
|
|
|
|
|
refId: string;
|
2024-10-22 08:20:45 +07:00
|
|
|
sysName: string;
|
2024-10-21 14:28:11 +07:00
|
|
|
},
|
|
|
|
|
) {
|
|
|
|
|
const profile = await this.profileRepo.findOne({
|
|
|
|
|
where: {
|
|
|
|
|
keycloak: req.user.sub,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
if (!profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลผู้ใช้งาน");
|
|
|
|
|
|
|
|
|
|
const profileOfficer = await this.workflowRepo.findOne({
|
|
|
|
|
where: {
|
|
|
|
|
states: { stateUserComments: { profile: { keycloak: req.user.sub } } },
|
|
|
|
|
refId: body.refId,
|
2024-10-22 11:59:12 +07:00
|
|
|
// sysName: body.sysName,
|
2024-10-21 14:28:11 +07:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
if (!profileOfficer) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลสิทธิ์");
|
|
|
|
|
return new HttpSuccess();
|
|
|
|
|
}
|
2024-10-09 14:14:36 +07:00
|
|
|
}
|