2024-12-02 11:42:24 +07:00
|
|
|
import {
|
|
|
|
|
Body,
|
|
|
|
|
Controller,
|
|
|
|
|
Delete,
|
|
|
|
|
Get,
|
2024-12-06 17:36:45 +07:00
|
|
|
Head,
|
2024-12-02 11:42:24 +07:00
|
|
|
Path,
|
|
|
|
|
Post,
|
|
|
|
|
Put,
|
|
|
|
|
Query,
|
|
|
|
|
Request,
|
|
|
|
|
Route,
|
|
|
|
|
Security,
|
|
|
|
|
Tags,
|
|
|
|
|
} from "tsoa";
|
|
|
|
|
import prisma from "../db";
|
|
|
|
|
import { notFoundError } from "../utils/error";
|
2024-12-11 14:43:47 +07:00
|
|
|
import {
|
|
|
|
|
Prisma,
|
|
|
|
|
RequestDataStatus,
|
|
|
|
|
TaskOrderStatus,
|
|
|
|
|
TaskStatus,
|
|
|
|
|
UserTaskStatus,
|
|
|
|
|
} 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";
|
2024-12-06 17:36:45 +07:00
|
|
|
import { deleteFile, fileLocation, getFile, getPresigned, listFile, setFile } from "../utils/minio";
|
2024-12-11 14:43:47 +07:00
|
|
|
import { queryOrNot } from "../utils/relation";
|
2024-12-03 17:11:44 +07:00
|
|
|
|
|
|
|
|
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({
|
2024-12-10 10:03:51 +07:00
|
|
|
by: ["taskOrderStatus"],
|
2024-12-03 09:37:35 +07:00
|
|
|
_count: true,
|
|
|
|
|
});
|
2024-12-10 10:03:51 +07:00
|
|
|
return task.reduce<Record<TaskOrderStatus, number>>(
|
|
|
|
|
(a, c) => Object.assign(a, { [c.taskOrderStatus]: c._count }),
|
2024-12-03 10:20:45 +07:00
|
|
|
{
|
2024-12-10 10:03:51 +07:00
|
|
|
[TaskOrderStatus.Pending]: 0,
|
|
|
|
|
[TaskOrderStatus.InProgress]: 0,
|
|
|
|
|
[TaskOrderStatus.Validate]: 0,
|
|
|
|
|
[TaskOrderStatus.Complete]: 0,
|
|
|
|
|
[TaskOrderStatus.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,
|
2024-12-11 09:15:11 +07:00
|
|
|
@Query() assignedByUserId?: string,
|
2024-12-10 10:03:51 +07:00
|
|
|
@Query() taskOrderStatus?: TaskOrderStatus,
|
2024-12-06 13:21:57 +07:00
|
|
|
) {
|
2024-12-11 09:15:11 +07:00
|
|
|
return this.getTaskOrderListByCriteria(
|
|
|
|
|
req,
|
|
|
|
|
query,
|
|
|
|
|
page,
|
|
|
|
|
pageSize,
|
|
|
|
|
assignedByUserId,
|
|
|
|
|
taskOrderStatus,
|
|
|
|
|
);
|
2024-12-06 13:21:57 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Post("list")
|
|
|
|
|
@Security("keycloak")
|
|
|
|
|
async getTaskOrderListByCriteria(
|
|
|
|
|
@Request() req: RequestWithUser,
|
|
|
|
|
@Query() query: string = "",
|
|
|
|
|
@Query() page = 1,
|
|
|
|
|
@Query() pageSize = 30,
|
2024-12-11 14:43:47 +07:00
|
|
|
@Query() assignedUserId?: string,
|
2024-12-10 10:03:51 +07:00
|
|
|
@Query() taskOrderStatus?: TaskOrderStatus,
|
2024-12-06 13:21:57 +07:00
|
|
|
@Body() body?: { code?: string[] },
|
2024-12-03 13:35:33 +07:00
|
|
|
) {
|
2024-12-11 14:43:47 +07:00
|
|
|
const where = {
|
|
|
|
|
taskOrderStatus,
|
|
|
|
|
registeredBranch: { OR: permissionCondCompany(req.user) },
|
|
|
|
|
taskList: assignedUserId
|
|
|
|
|
? {
|
|
|
|
|
some: {
|
|
|
|
|
requestWorkStep: { responsibleUserId: assignedUserId },
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
: undefined,
|
|
|
|
|
code: body?.code ? { in: body.code } : undefined,
|
|
|
|
|
OR: queryOrNot(query, [
|
|
|
|
|
{ code: { contains: query, mode: "insensitive" } },
|
|
|
|
|
{ taskName: { contains: query } },
|
|
|
|
|
{ contactName: { contains: query } },
|
|
|
|
|
{ contactTel: { contains: query } },
|
|
|
|
|
]),
|
|
|
|
|
} satisfies Prisma.TaskOrderWhereInput;
|
|
|
|
|
|
2024-12-02 11:42:24 +07:00
|
|
|
const [result, total] = await prisma.$transaction([
|
|
|
|
|
prisma.taskOrder.findMany({
|
2024-12-11 14:43:47 +07:00
|
|
|
where,
|
2024-12-02 11:42:24 +07:00
|
|
|
include: {
|
|
|
|
|
institution: true,
|
|
|
|
|
createdBy: true,
|
|
|
|
|
},
|
|
|
|
|
}),
|
2024-12-11 14:43:47 +07:00
|
|
|
prisma.taskOrder.count({ where }),
|
2024-12-02 11:42:24 +07:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
return { result, total, page, pageSize };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Get("{taskId}")
|
|
|
|
|
@Security("keycloak")
|
2024-12-11 09:15:11 +07:00
|
|
|
async getTaskOrder(
|
|
|
|
|
@Request() req: RequestWithUser,
|
|
|
|
|
@Path() taskId: string,
|
2024-12-11 14:43:47 +07:00
|
|
|
@Query() taskAssignedUserId?: string,
|
2024-12-11 09:15:11 +07:00
|
|
|
) {
|
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: {
|
2024-12-11 09:15:11 +07:00
|
|
|
where: {
|
2024-12-11 14:43:47 +07:00
|
|
|
requestWorkStep: { responsibleUserId: taskAssignedUserId },
|
2024-12-11 09:15:11 +07:00
|
|
|
},
|
2024-12-02 11:42:24 +07:00
|
|
|
include: {
|
2024-12-10 10:03:51 +07:00
|
|
|
requestWorkStep: {
|
2024-12-02 11:42:24 +07:00
|
|
|
include: {
|
2024-12-11 16:41:57 +07:00
|
|
|
responsibleUser: true,
|
2024-12-10 10:03:51 +07:00
|
|
|
requestWork: {
|
2024-12-06 16:42:19 +07:00
|
|
|
include: {
|
2024-12-10 10:03:51 +07:00
|
|
|
request: {
|
|
|
|
|
include: {
|
|
|
|
|
employee: true,
|
|
|
|
|
quotation: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
productService: {
|
|
|
|
|
include: {
|
|
|
|
|
service: true,
|
|
|
|
|
work: true,
|
|
|
|
|
product: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
2024-12-06 14:37:23 +07:00
|
|
|
},
|
|
|
|
|
},
|
2024-12-02 11:42:24 +07:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
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-06 11:25:38 +07:00
|
|
|
return await tx.taskOrder.create({
|
2024-12-02 11:42:24 +07:00
|
|
|
include: {
|
|
|
|
|
taskList: {
|
|
|
|
|
include: {
|
2024-12-10 10:03:51 +07:00
|
|
|
requestWorkStep: {
|
2024-12-06 17:17:05 +07:00
|
|
|
include: {
|
2024-12-10 10:03:51 +07:00
|
|
|
requestWork: {
|
2024-12-06 17:17:05 +07:00
|
|
|
include: {
|
2024-12-10 10:03:51 +07:00
|
|
|
request: {
|
|
|
|
|
include: {
|
|
|
|
|
employee: true,
|
|
|
|
|
quotation: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
productService: {
|
|
|
|
|
include: {
|
|
|
|
|
service: true,
|
|
|
|
|
work: true,
|
|
|
|
|
product: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
2024-12-06 17:17:05 +07:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
2024-12-02 11:42:24 +07:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
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,
|
2024-12-10 10:03:51 +07:00
|
|
|
taskList: { create: taskList },
|
2024-12-02 11:42:24 +07:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Put("{taskId}")
|
2024-12-06 16:44:02 +07:00
|
|
|
@Security("keycloak")
|
2024-12-02 11:42:24 +07:00
|
|
|
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;
|
2024-12-10 10:03:51 +07:00
|
|
|
taskOrderStatus: TaskOrderStatus;
|
2024-12-02 11:42:24 +07:00
|
|
|
|
|
|
|
|
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: {
|
2024-12-10 10:03:51 +07:00
|
|
|
include: {
|
|
|
|
|
requestWorkStep: {
|
|
|
|
|
include: { requestWork: true },
|
|
|
|
|
},
|
|
|
|
|
},
|
2024-12-02 11:42:24 +07:00
|
|
|
},
|
|
|
|
|
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: {
|
2024-12-10 10:03:51 +07:00
|
|
|
requestWorkStep: {
|
|
|
|
|
include: {
|
|
|
|
|
requestWork: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
2024-12-02 11:42:24 +07:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
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: {
|
2024-12-10 10:03:51 +07:00
|
|
|
deleteMany: record?.taskList.filter(
|
|
|
|
|
(lhs) =>
|
|
|
|
|
!body.taskList.find(
|
|
|
|
|
(rhs) => lhs.requestWorkId === rhs.requestWorkId && lhs.step === rhs.step,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
createMany: {
|
|
|
|
|
data: body.taskList,
|
|
|
|
|
skipDuplicates: true,
|
|
|
|
|
},
|
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");
|
|
|
|
|
|
2024-12-11 12:52:11 +07:00
|
|
|
await prisma.$transaction([
|
2024-12-11 12:06:29 +07:00
|
|
|
prisma.taskOrder.update({
|
2024-12-02 11:42:24 +07:00
|
|
|
where: { id: taskId },
|
|
|
|
|
data: {
|
2024-12-10 10:03:51 +07:00
|
|
|
taskOrderStatus: TaskOrderStatus.InProgress,
|
2024-12-11 14:43:47 +07:00
|
|
|
userTask: {
|
|
|
|
|
create: {
|
|
|
|
|
userId: req.user.sub,
|
|
|
|
|
userTaskStatus: UserTaskStatus.Accept,
|
|
|
|
|
},
|
|
|
|
|
},
|
2024-12-02 11:42:24 +07:00
|
|
|
},
|
2024-12-11 12:06:29 +07:00
|
|
|
}),
|
|
|
|
|
prisma.task.updateMany({
|
|
|
|
|
where: {
|
|
|
|
|
taskOrderId: taskId,
|
|
|
|
|
requestWorkStep: { responsibleUserId: req.user.sub },
|
|
|
|
|
},
|
|
|
|
|
data: {
|
|
|
|
|
taskStatus: TaskStatus.InProgress,
|
|
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
prisma.requestData.updateMany({
|
2024-12-02 11:42:24 +07:00
|
|
|
where: {
|
|
|
|
|
requestWork: {
|
|
|
|
|
some: {
|
|
|
|
|
stepStatus: {
|
2024-12-10 10:03:51 +07:00
|
|
|
some: { task: { some: { taskOrderId: taskId } } },
|
2024-12-02 11:42:24 +07:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
2024-12-10 14:40:43 +07:00
|
|
|
data: { requestDataStatus: RequestDataStatus.InProgress },
|
2024-12-11 12:06:29 +07:00
|
|
|
}),
|
|
|
|
|
]);
|
2024-12-02 11:42:24 +07:00
|
|
|
}
|
2024-12-04 14:52:50 +07:00
|
|
|
|
|
|
|
|
@Post("submit")
|
|
|
|
|
@Security("keycloak")
|
2024-12-11 12:06:29 +07:00
|
|
|
async submitTaskOrder(
|
|
|
|
|
@Request() req: RequestWithUser,
|
|
|
|
|
@Path() taskId: string,
|
|
|
|
|
@Query() submitUserId?: string, // for explicit
|
|
|
|
|
) {
|
|
|
|
|
submitUserId = submitUserId ?? req.user.sub;
|
|
|
|
|
|
|
|
|
|
const record = await prisma.taskOrder.findFirst({ where: { id: taskId } });
|
|
|
|
|
|
|
|
|
|
if (!record) throw notFoundError("Task Order");
|
|
|
|
|
|
|
|
|
|
await prisma.task.updateMany({
|
|
|
|
|
where: {
|
|
|
|
|
taskOrderId: taskId,
|
|
|
|
|
taskStatus: TaskStatus.Success,
|
|
|
|
|
requestWorkStep: { responsibleUserId: submitUserId },
|
|
|
|
|
},
|
|
|
|
|
data: {
|
|
|
|
|
taskStatus: TaskStatus.Validate,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-12-04 14:52:50 +07:00
|
|
|
|
2024-12-04 15:34:20 +07:00
|
|
|
@Post("complete")
|
|
|
|
|
@Security("keycloak")
|
|
|
|
|
async completeTaskOrder(@Request() req: RequestWithUser, @Path() taskId: string) {}
|
2024-12-02 11:42:24 +07:00
|
|
|
}
|
2024-12-04 10:49:22 +07:00
|
|
|
|
2024-12-10 10:03:51 +07:00
|
|
|
@Route("api/v1/task/{taskId}")
|
2024-12-04 10:49:22 +07:00
|
|
|
@Tags("Task Order")
|
2024-12-06 17:36:45 +07:00
|
|
|
export class TaskOrderAttachmentController extends Controller {
|
|
|
|
|
private async checkPermission(user: RequestWithUser["user"], id: string) {
|
|
|
|
|
const data = await prisma.taskOrder.findUnique({
|
|
|
|
|
include: { registeredBranch: { include: branchRelationPermInclude(user) } },
|
|
|
|
|
where: { id },
|
|
|
|
|
});
|
2024-12-06 17:42:27 +07:00
|
|
|
if (!data) throw notFoundError("Task Order");
|
2024-12-06 17:36:45 +07:00
|
|
|
await permissionCheck(user, data.registeredBranch);
|
|
|
|
|
}
|
2024-12-11 12:06:29 +07:00
|
|
|
|
2024-12-06 17:36:45 +07:00
|
|
|
@Get("attachment")
|
|
|
|
|
@Security("keycloak")
|
|
|
|
|
async listAttachment(@Request() req: RequestWithUser, @Path() taskId: string) {
|
|
|
|
|
await this.checkPermission(req.user, taskId);
|
|
|
|
|
return await listFile(fileLocation.task.attachment(taskId));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Get("attachment/{name}")
|
|
|
|
|
@Security("keycloak")
|
|
|
|
|
async getAttachment(@Path() taskId: string, @Path() name: string) {
|
|
|
|
|
return await getFile(fileLocation.task.attachment(taskId, name));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Head("attachment/{name}")
|
|
|
|
|
async headAttachment(@Path() taskId: string, @Path() name: string) {
|
|
|
|
|
return await getPresigned("head", fileLocation.task.attachment(taskId, name));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Put("attachment/{name}")
|
|
|
|
|
@Security("keycloak")
|
|
|
|
|
async putAttachment(
|
|
|
|
|
@Request() req: RequestWithUser,
|
|
|
|
|
@Path() taskId: string,
|
|
|
|
|
@Path() name: string,
|
|
|
|
|
) {
|
|
|
|
|
await this.checkPermission(req.user, taskId);
|
|
|
|
|
return await setFile(fileLocation.task.attachment(taskId, name));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Delete("attachment/{name}")
|
|
|
|
|
@Security("keycloak")
|
|
|
|
|
async delAttachment(
|
|
|
|
|
@Request() req: RequestWithUser,
|
|
|
|
|
@Path() taskId: string,
|
|
|
|
|
@Path() name: string,
|
|
|
|
|
) {
|
|
|
|
|
await this.checkPermission(req.user, taskId);
|
|
|
|
|
return await deleteFile(fileLocation.task.attachment(taskId, name));
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-12-11 14:43:47 +07:00
|
|
|
|
|
|
|
|
@Route("api/v1/user-task")
|
|
|
|
|
@Tags("Task Order")
|
|
|
|
|
export class UserTaskController extends Controller {
|
|
|
|
|
@Get()
|
|
|
|
|
@Security("keycloak")
|
|
|
|
|
async getUserTask(
|
|
|
|
|
@Request() req: RequestWithUser,
|
|
|
|
|
@Query() query: string = "",
|
|
|
|
|
@Query() page = 1,
|
|
|
|
|
@Query() pageSize = 30,
|
|
|
|
|
@Query() userTaskStatus?: UserTaskStatus,
|
|
|
|
|
) {
|
|
|
|
|
const where = {
|
|
|
|
|
taskList: {
|
|
|
|
|
some: {
|
|
|
|
|
requestWorkStep: { responsibleUserId: req.user.sub },
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
userTask: userTaskStatus
|
|
|
|
|
? {
|
|
|
|
|
some:
|
|
|
|
|
userTaskStatus !== UserTaskStatus.Pending
|
|
|
|
|
? {
|
|
|
|
|
userTaskStatus,
|
|
|
|
|
userId: req.user.sub,
|
|
|
|
|
}
|
|
|
|
|
: undefined,
|
|
|
|
|
none: userTaskStatus === UserTaskStatus.Pending ? { userId: req.user.sub } : undefined,
|
|
|
|
|
}
|
|
|
|
|
: undefined,
|
|
|
|
|
OR: queryOrNot(query, [
|
|
|
|
|
{ code: { contains: query, mode: "insensitive" } },
|
|
|
|
|
{ taskName: { contains: query } },
|
|
|
|
|
{ contactName: { contains: query } },
|
|
|
|
|
{ contactTel: { contains: query } },
|
|
|
|
|
]),
|
|
|
|
|
} satisfies Prisma.TaskOrderWhereInput;
|
|
|
|
|
|
|
|
|
|
const [result, total] = await prisma.$transaction([
|
|
|
|
|
prisma.taskOrder.findMany({
|
|
|
|
|
where,
|
|
|
|
|
include: {
|
2024-12-11 15:45:50 +07:00
|
|
|
userTask: true,
|
2024-12-11 14:43:47 +07:00
|
|
|
institution: true,
|
|
|
|
|
createdBy: true,
|
|
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
prisma.taskOrder.count({ where }),
|
|
|
|
|
]);
|
|
|
|
|
|
2024-12-11 15:45:50 +07:00
|
|
|
return {
|
|
|
|
|
result: result.map((lhs) => ({
|
|
|
|
|
...lhs,
|
|
|
|
|
taskOrderStatus:
|
|
|
|
|
lhs.userTask.find((rhs) => rhs.taskOrderId === lhs.id)?.userTaskStatus ??
|
|
|
|
|
lhs.taskOrderStatus,
|
|
|
|
|
userTask: undefined,
|
|
|
|
|
})),
|
|
|
|
|
page,
|
|
|
|
|
pageSize,
|
|
|
|
|
total,
|
|
|
|
|
};
|
2024-12-11 14:43:47 +07:00
|
|
|
}
|
|
|
|
|
}
|