2024-12-02 11:42:24 +07:00
|
|
|
import {
|
|
|
|
|
Body,
|
|
|
|
|
Controller,
|
|
|
|
|
Delete,
|
|
|
|
|
Get,
|
|
|
|
|
Path,
|
|
|
|
|
Post,
|
|
|
|
|
Put,
|
|
|
|
|
Query,
|
|
|
|
|
Request,
|
|
|
|
|
Route,
|
|
|
|
|
Security,
|
|
|
|
|
Tags,
|
|
|
|
|
} from "tsoa";
|
|
|
|
|
import prisma from "../db";
|
|
|
|
|
import { notFoundError } from "../utils/error";
|
2024-12-04 10:26:50 +07:00
|
|
|
import { TaskStatus } from "@prisma/client";
|
2024-12-02 11:42:24 +07:00
|
|
|
import { RequestWithUser } from "../interfaces/user";
|
2024-12-03 17:11:44 +07:00
|
|
|
import {
|
|
|
|
|
branchRelationPermInclude,
|
|
|
|
|
createPermCheck,
|
|
|
|
|
createPermCondition,
|
|
|
|
|
} from "../services/permission";
|
|
|
|
|
import HttpError from "../interfaces/http-error";
|
|
|
|
|
import HttpStatus from "../interfaces/http-status";
|
|
|
|
|
|
|
|
|
|
const MANAGE_ROLES = ["system", "head_of_admin", "admin", "document_checker"];
|
|
|
|
|
|
|
|
|
|
function globalAllow(user: RequestWithUser["user"]) {
|
|
|
|
|
const allowList = ["system", "head_of_admin"];
|
|
|
|
|
return allowList.some((v) => user.roles?.includes(v));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const permissionCondCompany = createPermCondition((_) => true);
|
|
|
|
|
|
|
|
|
|
const permissionCheck = createPermCheck(globalAllow);
|
|
|
|
|
const permissionCheckCompany = createPermCheck((_) => true);
|
2024-12-02 11:42:24 +07:00
|
|
|
|
|
|
|
|
@Route("/api/v1/task")
|
|
|
|
|
@Tags("Task Order")
|
|
|
|
|
export class TaskController extends Controller {
|
2024-12-03 09:37:35 +07:00
|
|
|
@Get("stats")
|
|
|
|
|
async getTaskOrderStats() {
|
|
|
|
|
const task = await prisma.taskOrder.groupBy({
|
|
|
|
|
by: ["taskStatus"],
|
|
|
|
|
_count: true,
|
|
|
|
|
});
|
2024-12-03 10:20:45 +07:00
|
|
|
return task.reduce<Record<TaskStatus, number>>(
|
2024-12-03 09:37:35 +07:00
|
|
|
(a, c) => Object.assign(a, { [c.taskStatus]: c._count }),
|
2024-12-03 10:20:45 +07:00
|
|
|
{
|
|
|
|
|
[TaskStatus.Pending]: 0,
|
|
|
|
|
[TaskStatus.InProgress]: 0,
|
|
|
|
|
[TaskStatus.Validate]: 0,
|
|
|
|
|
[TaskStatus.Complete]: 0,
|
2024-12-04 14:26:22 +07:00
|
|
|
[TaskStatus.Canceled]: 0,
|
2024-12-03 10:20:45 +07:00
|
|
|
},
|
2024-12-03 09:37:35 +07:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-02 11:42:24 +07:00
|
|
|
@Get()
|
|
|
|
|
@Security("keycloak")
|
2024-12-03 13:35:33 +07:00
|
|
|
async getTaskOrderList(
|
2024-12-03 17:11:44 +07:00
|
|
|
@Request() req: RequestWithUser,
|
2024-12-03 13:35:33 +07:00
|
|
|
@Query() query: string = "",
|
|
|
|
|
@Query() page = 1,
|
|
|
|
|
@Query() pageSize = 30,
|
|
|
|
|
@Query() taskStatus?: TaskStatus,
|
|
|
|
|
) {
|
2024-12-02 11:42:24 +07:00
|
|
|
const [result, total] = await prisma.$transaction([
|
|
|
|
|
prisma.taskOrder.findMany({
|
2024-12-03 13:35:33 +07:00
|
|
|
where: {
|
|
|
|
|
taskStatus,
|
2024-12-03 17:11:44 +07:00
|
|
|
registeredBranch: { OR: permissionCondCompany(req.user) },
|
2024-12-03 13:35:33 +07:00
|
|
|
OR: [
|
|
|
|
|
{ code: { contains: query, mode: "insensitive" } },
|
|
|
|
|
{ taskName: { contains: query } },
|
|
|
|
|
{ contactName: { contains: query } },
|
|
|
|
|
{ contactTel: { contains: query } },
|
|
|
|
|
],
|
|
|
|
|
},
|
2024-12-02 11:42:24 +07:00
|
|
|
include: {
|
|
|
|
|
institution: true,
|
|
|
|
|
createdBy: true,
|
|
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
prisma.taskOrder.count(),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
return { result, total, page, pageSize };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Get("{taskId}")
|
|
|
|
|
@Security("keycloak")
|
2024-12-03 17:11:44 +07:00
|
|
|
async getTaskOrder(@Request() req: RequestWithUser, @Path() taskId: string) {
|
2024-12-02 11:42:24 +07:00
|
|
|
const record = await prisma.taskOrder.findFirst({
|
2024-12-03 17:11:44 +07:00
|
|
|
where: { id: taskId, registeredBranch: { OR: permissionCondCompany(req.user) } },
|
2024-12-02 11:42:24 +07:00
|
|
|
include: {
|
|
|
|
|
taskList: {
|
|
|
|
|
include: {
|
|
|
|
|
requestWork: {
|
|
|
|
|
include: {
|
|
|
|
|
productService: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
institution: true,
|
|
|
|
|
createdBy: true,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!record) throw notFoundError("Task Order");
|
|
|
|
|
|
|
|
|
|
return record;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Post()
|
2024-12-03 17:11:44 +07:00
|
|
|
@Security("keycloak", MANAGE_ROLES)
|
2024-12-02 11:42:24 +07:00
|
|
|
async createTaskOrderList(
|
|
|
|
|
@Request() req: RequestWithUser,
|
|
|
|
|
@Body()
|
|
|
|
|
body: {
|
|
|
|
|
taskName: string;
|
|
|
|
|
|
|
|
|
|
contactName: string;
|
|
|
|
|
contactTel: string;
|
|
|
|
|
|
|
|
|
|
institutionId: string;
|
2024-12-03 17:11:44 +07:00
|
|
|
registeredBranchId?: string;
|
2024-12-02 11:42:24 +07:00
|
|
|
|
2024-12-04 10:49:22 +07:00
|
|
|
taskList: { requestWorkId: string; step: number }[];
|
2024-12-02 11:42:24 +07:00
|
|
|
},
|
|
|
|
|
) {
|
|
|
|
|
return await prisma.$transaction(async (tx) => {
|
|
|
|
|
const last = await tx.runningNo.upsert({
|
|
|
|
|
where: {
|
|
|
|
|
key: "TASK",
|
|
|
|
|
},
|
|
|
|
|
create: {
|
|
|
|
|
key: "TASK",
|
|
|
|
|
value: 1,
|
|
|
|
|
},
|
|
|
|
|
update: {
|
|
|
|
|
value: { increment: 1 },
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
const current = new Date();
|
|
|
|
|
const year = `${current.getFullYear()}`.slice(-2).padStart(2, "0");
|
|
|
|
|
const month = `${current.getMonth() + 1}`.padStart(2, "0");
|
|
|
|
|
|
|
|
|
|
const code = `PO${year}${month}${last.value.toString().padStart(6, "0")}`;
|
|
|
|
|
|
2024-12-04 10:49:22 +07:00
|
|
|
const { taskList, ...rest } = body;
|
2024-12-03 13:39:23 +07:00
|
|
|
|
2024-12-03 17:11:44 +07:00
|
|
|
const userAffiliatedBranch = await tx.branch.findFirst({
|
|
|
|
|
include: branchRelationPermInclude(req.user),
|
|
|
|
|
where: body.registeredBranchId
|
|
|
|
|
? { id: body.registeredBranchId }
|
|
|
|
|
: {
|
|
|
|
|
user: { some: { userId: req.user.sub } },
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
if (!userAffiliatedBranch) {
|
|
|
|
|
throw new HttpError(
|
|
|
|
|
HttpStatus.BAD_REQUEST,
|
|
|
|
|
"You must be affilated with at least one branch or specify branch to be registered (System permission required).",
|
|
|
|
|
"reqMinAffilatedBranch",
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
await permissionCheckCompany(req.user, userAffiliatedBranch);
|
|
|
|
|
|
2024-12-02 11:42:24 +07:00
|
|
|
await tx.taskOrder.create({
|
|
|
|
|
include: {
|
|
|
|
|
taskList: {
|
|
|
|
|
include: {
|
|
|
|
|
requestWork: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
institution: true,
|
|
|
|
|
createdBy: true,
|
|
|
|
|
},
|
|
|
|
|
data: {
|
2024-12-03 13:39:23 +07:00
|
|
|
...rest,
|
2024-12-02 11:42:24 +07:00
|
|
|
code,
|
2024-12-03 17:11:44 +07:00
|
|
|
registeredBranchId: userAffiliatedBranch.id,
|
2024-12-02 11:42:24 +07:00
|
|
|
createdByUserId: req.user.sub,
|
|
|
|
|
taskList: {
|
2024-12-04 10:49:22 +07:00
|
|
|
connect: taskList.map((v) => ({ step_requestWorkId: v })),
|
2024-12-02 11:42:24 +07:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Put("{taskId}")
|
|
|
|
|
async editTaskById(
|
2024-12-03 17:11:44 +07:00
|
|
|
@Request() req: RequestWithUser,
|
2024-12-02 11:42:24 +07:00
|
|
|
@Path() taskId: string,
|
|
|
|
|
@Body()
|
|
|
|
|
body: {
|
|
|
|
|
taskName: string;
|
|
|
|
|
taskStatus: TaskStatus;
|
|
|
|
|
|
|
|
|
|
contactName: string;
|
|
|
|
|
contactTel: string;
|
|
|
|
|
|
|
|
|
|
institutionId: string;
|
|
|
|
|
|
2024-12-04 10:49:22 +07:00
|
|
|
taskList: { requestWorkId: string; step: number }[];
|
2024-12-02 11:42:24 +07:00
|
|
|
},
|
|
|
|
|
) {
|
|
|
|
|
const record = await prisma.taskOrder.findFirst({
|
|
|
|
|
where: { id: taskId },
|
|
|
|
|
include: {
|
2024-12-03 17:11:44 +07:00
|
|
|
registeredBranch: { include: branchRelationPermInclude(req.user) },
|
2024-12-02 11:42:24 +07:00
|
|
|
taskList: {
|
|
|
|
|
include: { requestWork: true },
|
|
|
|
|
},
|
|
|
|
|
institution: true,
|
|
|
|
|
createdBy: true,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2024-12-03 17:11:44 +07:00
|
|
|
if (!record) throw notFoundError("Task Order");
|
|
|
|
|
|
|
|
|
|
await permissionCheckCompany(req.user, record.registeredBranch);
|
|
|
|
|
|
2024-12-02 11:42:24 +07:00
|
|
|
await prisma.taskOrder.update({
|
|
|
|
|
where: { id: taskId },
|
|
|
|
|
include: {
|
|
|
|
|
taskList: {
|
|
|
|
|
include: {
|
|
|
|
|
requestWork: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
institution: true,
|
2024-12-03 17:11:44 +07:00
|
|
|
registeredBranch: true,
|
2024-12-02 11:42:24 +07:00
|
|
|
createdBy: true,
|
|
|
|
|
},
|
|
|
|
|
data: {
|
|
|
|
|
...body,
|
|
|
|
|
taskList: {
|
|
|
|
|
disconnect: record?.taskList
|
|
|
|
|
.filter(
|
|
|
|
|
(lhs) =>
|
2024-12-04 10:49:22 +07:00
|
|
|
!body.taskList.find(
|
2024-12-02 11:42:24 +07:00
|
|
|
(rhs) => lhs.requestWorkId === rhs.requestWorkId && lhs.step === rhs.step,
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
.map((v) => ({
|
|
|
|
|
step_requestWorkId: {
|
|
|
|
|
requestWorkId: v.requestWorkId,
|
|
|
|
|
step: v.step,
|
|
|
|
|
},
|
|
|
|
|
})),
|
2024-12-04 10:49:22 +07:00
|
|
|
connect: body.taskList.map((v) => ({ step_requestWorkId: v })),
|
2024-12-02 11:42:24 +07:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Delete("{taskId}")
|
2024-12-03 17:11:44 +07:00
|
|
|
@Security("keycloak", MANAGE_ROLES)
|
|
|
|
|
async deleteTask(@Request() req: RequestWithUser, @Path() taskId: string) {
|
2024-12-02 11:42:24 +07:00
|
|
|
await prisma.$transaction(async (tx) => {
|
2024-12-03 17:11:44 +07:00
|
|
|
let record = await tx.taskOrder.findFirst({
|
|
|
|
|
where: { id: taskId },
|
|
|
|
|
include: {
|
|
|
|
|
registeredBranch: {
|
|
|
|
|
include: branchRelationPermInclude(req.user),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!record) throw notFoundError("Task Order");
|
2024-12-02 11:42:24 +07:00
|
|
|
|
2024-12-03 17:11:44 +07:00
|
|
|
await permissionCheck(req.user, record.registeredBranch);
|
2024-12-02 11:42:24 +07:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Route("/api/v1/task/{taskId}")
|
|
|
|
|
@Tags("Task Order")
|
|
|
|
|
export class TaskActionController extends Controller {
|
|
|
|
|
@Post("accept")
|
2024-12-04 14:52:14 +07:00
|
|
|
@Security("keycloak")
|
2024-12-02 11:42:24 +07:00
|
|
|
async acceptTaskOrder(@Request() req: RequestWithUser, @Path() taskId: string) {
|
|
|
|
|
const record = await prisma.taskOrder.findFirst({
|
|
|
|
|
include: {
|
|
|
|
|
taskList: {
|
|
|
|
|
orderBy: { step: "asc" },
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
where: { id: taskId },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!record) throw notFoundError("Task Order");
|
|
|
|
|
|
|
|
|
|
return await prisma.$transaction(async (tx) => {
|
|
|
|
|
await tx.taskOrder.update({
|
|
|
|
|
where: { id: taskId },
|
|
|
|
|
data: {
|
2024-12-02 13:13:50 +07:00
|
|
|
taskStatus: "InProgress",
|
2024-12-02 11:42:24 +07:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
await tx.requestWorkStepStatus.updateMany({
|
|
|
|
|
where: { taskOrderId: taskId },
|
|
|
|
|
data: {
|
|
|
|
|
workStatus: "InProgress",
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
await tx.requestData.updateMany({
|
|
|
|
|
where: {
|
|
|
|
|
requestWork: {
|
|
|
|
|
some: {
|
|
|
|
|
stepStatus: {
|
|
|
|
|
some: { taskOrderId: taskId },
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
data: { requestDataStatus: "InProgress" },
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-12-04 10:49:22 +07:00
|
|
|
|
|
|
|
|
@Route("api/v1/task-order/{taskId}")
|
|
|
|
|
@Tags("Task Order")
|
|
|
|
|
export class TaskOrderAttachmentController extends Controller {}
|