no message

This commit is contained in:
kittapath 2024-10-16 11:55:45 +07:00
parent ad97270752
commit 3ea2c7c3b0
9 changed files with 420 additions and 35 deletions

View file

@ -10,18 +10,20 @@ import { StateOperator } from "../entities/StateOperator";
import { StateOperatorUser } from "../entities/StateOperatorUser";
import CallAPI from "../interfaces/call-api";
import { Profile } from "../entities/Profile";
import { StateUserComment } from "../entities/StateUserComment";
@Route("api/v1/org/workflow")
@Tags("AuthRole")
@Tags("Workflow")
@Security("bearerAuth")
export class WorkflowController extends Controller {
private workflowRepo = AppDataSource.getRepository(Workflow);
private stateRepo = AppDataSource.getRepository(State);
private stateOperatorRepo = AppDataSource.getRepository(StateOperator);
private stateOperatorUserRepo = AppDataSource.getRepository(StateOperatorUser);
private stateUserCommentRepo = AppDataSource.getRepository(StateUserComment);
private profileRepo = AppDataSource.getRepository(Profile);
@Post("check-workflow")
@Post("add-workflow")
public async checkWorkflow(
@Request() req: RequestWithUser,
@Body()
@ -30,7 +32,7 @@ export class WorkflowController extends Controller {
posLevelName: string;
posTypeName: string;
profiles: {
profile: string;
profileId: string;
operator: string;
order: number;
}[];
@ -48,7 +50,7 @@ export class WorkflowController extends Controller {
await Promise.all(
body.profiles.map(async (item) => {
const operatoeUser = new StateOperatorUser();
operatoeUser.profile = item.profile;
operatoeUser.profileId = item.profileId;
operatoeUser.operator = item.operator.trim().toLocaleUpperCase();
operatoeUser.order = item.order;
operatoeUser.workflowId = workflow.id;
@ -76,18 +78,18 @@ export class WorkflowController extends Controller {
body: {
workflowId: string;
stateId: string;
profile: string;
profileId: string;
action: string;
},
) {
const stateOperatorUser = await this.stateOperatorUserRepo.findOne({
where: {
profile: body.profile,
profileId: body.profileId,
workflowId: body.workflowId,
},
});
if (!stateOperatorUser)
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่สามารถดำเนินการกระบวนการนี้ได้");
throw new HttpError(HttpStatus.NOT_FOUND, "ผู้ใช้งานนี้ไม่มีหน้าที่ในกระบวนการนี้");
const operator = await this.stateOperatorRepo.findOne({
where: {
@ -95,7 +97,7 @@ export class WorkflowController extends Controller {
state: { id: body.stateId, workflow: { id: body.workflowId } },
},
});
if (!operator) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
if (!operator) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่สามารถดำเนินการกระบวนการนี้ได้");
let isCan = false;
switch (body.action.trim().toLocaleUpperCase()) {
@ -125,13 +127,42 @@ export class WorkflowController extends Controller {
}
}
@Post("check-iscan-all")
public async checkIsCanAll(
@Post("check-state-now")
public async checkStateNow(
@Request() req: RequestWithUser,
@Body()
body: {
workflowId: string;
},
) {
const workflow = await this.workflowRepo.findOne({
where: {
id: body.workflowId,
},
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(
@Request() req: RequestWithUser,
@Body()
body: {
workflowId: string;
stateId: string;
},
) {
const profile = await this.profileRepo.findOne({
@ -139,24 +170,68 @@ export class WorkflowController extends Controller {
keycloak: req.user.sub,
},
});
if (!profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
if (!profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลผู้ใช้งาน");
const stateOperatorUser = await this.stateOperatorUserRepo.findOne({
where: {
profile: profile.id,
profileId: profile.id,
workflowId: body.workflowId,
},
});
if (!stateOperatorUser)
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่สามารถดำเนินการกระบวนการนี้ได้");
throw new HttpError(HttpStatus.NOT_FOUND, "ผู้ใช้งานนี้ไม่มีหน้าที่ในกระบวนการนี้");
const workflow = await this.workflowRepo.findOne({
where: {
id: body.workflowId,
},
});
if (!workflow) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่สามารถดำเนินการกระบวนการนี้ได้");
const operator = await this.stateOperatorRepo.findOne({
where: {
operator: stateOperatorUser.operator,
state: { id: body.stateId, workflow: { id: body.workflowId } },
stateId: workflow.stateId,
},
relations: ["state"],
});
if (!operator) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
return new HttpSuccess(operator);
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: {
workflowId: string;
},
) {
const state = await this.stateRepo.find({
where: {
workflowId: body.workflowId,
},
order: { stateUserComments: { order: "ASC" } },
relations: ["stateUserComments"],
});
const _state = state.map((x) => ({
stateId: x.id,
stateNo: x.order,
stateName: x.name,
stateUserComments: x.stateUserComments,
}));
return new HttpSuccess(_state);
}
@Post("state-next")
@ -164,16 +239,24 @@ export class WorkflowController extends Controller {
@Request() req: RequestWithUser,
@Body()
body: {
stateId: string;
workflowId: string;
},
) {
const workflow = await this.workflowRepo.findOne({
where: {
id: body.workflowId,
},
relations: ["stateOperatorUsers"],
});
if (!workflow) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่สามารถดำเนินการกระบวนการนี้ได้");
const state = await this.stateRepo.findOne({
where: {
id: body.stateId,
id: workflow.stateId,
},
relations: ["stateOperators", "workflow", "workflow.stateOperatorUsers"],
relations: ["stateOperators"],
});
if (!state) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
if (!state) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลขั้นตอนการอนุมัติ");
const _state = await this.stateRepo.findOne({
where: {
order: state.order + 1,
@ -182,7 +265,7 @@ export class WorkflowController extends Controller {
relations: ["stateOperators"],
});
//noti
let profileNow = state.workflow.stateOperatorUsers
let profileNow = workflow.stateOperatorUsers
.filter((x) => state.stateOperators.map((s) => s.operator).includes(x.operator))
.map((x) => x.profile);
await new CallAPI()
@ -199,7 +282,7 @@ export class WorkflowController extends Controller {
console.error("Error calling API:", error);
});
if (_state != null) {
let profileNext = state.workflow.stateOperatorUsers
let profileNext = workflow.stateOperatorUsers
.filter((x) => _state.stateOperators.map((s) => s.operator).includes(x.operator))
.map((x) => x.profile);
await new CallAPI()
@ -215,10 +298,13 @@ export class WorkflowController extends Controller {
.catch((error) => {
console.error("Error calling API:", error);
});
workflow.stateId = _state.id;
await this.workflowRepo.save(workflow);
}
return new HttpSuccess({
stateId: _state?.id || null,
stateNo: _state?.order || null,
stateName: _state?.name || null,
stateType: _state?.type || null,
});
@ -237,7 +323,7 @@ export class WorkflowController extends Controller {
id: body.stateId,
},
});
if (!state) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
if (!state) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลขั้นตอนการอนุมัติ");
const _state = await this.stateRepo.findOne({
where: {
order: state.order - 1,
@ -247,8 +333,162 @@ export class WorkflowController extends Controller {
return new HttpSuccess({
stateId: _state?.id || null,
stateNo: _state?.order || null,
stateName: _state?.name || null,
stateType: _state?.type || null,
});
}
@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 profile = await this.profileRepo.findOne({
where: {
keycloak: req.user.sub,
},
});
if (!profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลผู้ใช้งาน");
const stateUserComment = await this.stateUserCommentRepo.findOne({
where: {
profileId: profile.id,
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,
});
}
}