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

692 lines
22 KiB
TypeScript
Raw Normal View History

2024-10-17 15:13:59 +07:00
import { Body, Controller, Get, Path, Post, 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";
import { IsNull, Not } from "typeorm";
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: item.current_holderId,
2024-10-16 17:15:55 +07:00
operator: "Officer",
order: i + 2,
workflowId: workflow.id,
...meta,
});
await this.stateOperatorUserRepo.save(stateOperatorUser);
} else {
const stateOperatorUser = new StateOperatorUser();
Object.assign(stateOperatorUser, {
profileId: item.current_holderId,
2024-10-16 17:15:55 +07:00
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-18 11:04:46 +07:00
const workflow = await this.workflowRepo.findOne({
where: {
refId: body.refId,
sysName: body.system,
},
});
if (!workflow) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่สามารถดำเนินการกระบวนการนี้ได้");
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-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,
});
}
@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-10-17 11:33:35 +07:00
const stateOperatorUserNow = await this.stateOperatorUserRepo.findOne({
where: {
workflow: {
refId: body.refId,
sysName: body.system,
},
profile: {
keycloak: req.user.sub,
},
},
});
2024-10-16 17:49:55 +07:00
const 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-10-16 17:49:55 +07:00
await this.stateOperatorUserRepo.remove(stateOperatorUser);
2024-10-16 14:40:37 +07:00
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: {
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-18 11:33:04 +07:00
if (!stateUserComment)
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลขั้นตอนการอนุมัติ");
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,
});
}
/**
*
*
*/
@Get("commander")
async getProfilePlacement(@Request() req: RequestWithUser) {
const posMasterUser = await this.posMasterRepo.findOne({
where: {
2024-10-17 09:55:06 +07:00
orgRevision: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false },
current_holder: { keycloak: req.user.sub },
},
});
if (!posMasterUser || !posMasterUser.orgRootId)
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบตำแหน่งผู้ใช้งาน");
const posMasters = await this.posMasterRepo.find({
where: {
2024-10-17 09:55:06 +07:00
orgRevision: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false },
isDirector: true,
current_holderId: Not(IsNull()),
orgRootId: posMasterUser.orgRootId,
},
relations: ["current_holder", "orgRoot"],
});
let _posMasters = posMasters.map((data) => ({
id: data.current_holderId,
prefix: data.current_holder.prefix,
firstName: data.current_holder.firstName,
lastName: data.current_holder.lastName,
position: data.current_holder.position,
posLevel: data.current_holder.posLevel,
posType: data.current_holder.posType,
orgRoot: data.orgRoot.orgRootName,
}));
return new HttpSuccess(_posMasters);
}
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"],
});
if (!profileOfficer) return new HttpSuccess({ isOfficer: false, isStaff: false });
let isOfficer = profileOfficer.orgChild1 == null ? false : profileOfficer.orgChild1.isOfficer;
return new HttpSuccess({ isOfficer: isOfficer, isStaff: !isOfficer });
}
2024-10-09 14:14:36 +07:00
}