2024-10-07 11:58:43 +07:00
|
|
|
import { Body, Controller, Delete, Get, Path, Post, Put, Query, Request, Route, Tags } from "tsoa";
|
|
|
|
|
import { RequestWithUser } from "../interfaces/user";
|
|
|
|
|
import prisma from "../db";
|
|
|
|
|
|
|
|
|
|
type WorkflowPayload = {
|
|
|
|
|
name: string;
|
|
|
|
|
step: {
|
|
|
|
|
name: string;
|
|
|
|
|
type?: string;
|
|
|
|
|
value?: string[];
|
|
|
|
|
responsiblePersonId?: string[];
|
|
|
|
|
}[];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
@Route("api/v1/workflow-template")
|
|
|
|
|
@Tags("Workflow")
|
|
|
|
|
export class FlowTemplateController extends Controller {
|
|
|
|
|
@Get()
|
|
|
|
|
async getFlowTemplate(
|
|
|
|
|
@Request() _req: RequestWithUser,
|
|
|
|
|
@Query() page: number = 1,
|
|
|
|
|
@Query() pageSize: number = 30,
|
|
|
|
|
) {
|
2024-10-24 13:37:42 +07:00
|
|
|
const [result, total] = await prisma.$transaction([
|
|
|
|
|
prisma.workflowTemplate.findMany({
|
|
|
|
|
include: {
|
|
|
|
|
step: {
|
|
|
|
|
include: {
|
|
|
|
|
value: true,
|
|
|
|
|
responsiblePerson: {
|
|
|
|
|
include: { user: true },
|
|
|
|
|
},
|
2024-10-24 13:25:23 +07:00
|
|
|
},
|
|
|
|
|
},
|
2024-10-07 11:58:43 +07:00
|
|
|
},
|
2024-10-24 13:37:42 +07:00
|
|
|
orderBy: { createdAt: "asc" },
|
|
|
|
|
take: pageSize,
|
|
|
|
|
skip: (page - 1) * pageSize,
|
|
|
|
|
}),
|
|
|
|
|
prisma.workflowTemplate.count(),
|
|
|
|
|
]);
|
|
|
|
|
return { result, page, pageSize, total };
|
2024-10-07 11:58:43 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Get("{templateId}")
|
|
|
|
|
async getFlowTemplateById(@Request() _req: RequestWithUser, @Path() templateId: string) {
|
|
|
|
|
return await prisma.workflowTemplate.findFirst({
|
|
|
|
|
include: {
|
|
|
|
|
step: {
|
2024-10-24 13:25:23 +07:00
|
|
|
include: {
|
|
|
|
|
value: true,
|
|
|
|
|
responsiblePerson: {
|
|
|
|
|
include: { user: true },
|
|
|
|
|
},
|
|
|
|
|
},
|
2024-10-07 11:58:43 +07:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
where: { id: templateId },
|
|
|
|
|
orderBy: { createdAt: "asc" },
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Post()
|
|
|
|
|
async createFlowTemplate(@Request() _req: RequestWithUser, @Body() body: WorkflowPayload) {
|
|
|
|
|
return await prisma.workflowTemplate.create({
|
|
|
|
|
data: {
|
|
|
|
|
...body,
|
|
|
|
|
step: {
|
|
|
|
|
create: body.step.map((v, i) => ({
|
|
|
|
|
type: v.type,
|
|
|
|
|
value: {
|
|
|
|
|
create: v.value?.map((val) => ({
|
|
|
|
|
value: val,
|
|
|
|
|
})),
|
|
|
|
|
},
|
|
|
|
|
name: v.name,
|
|
|
|
|
order: i + 1,
|
|
|
|
|
responsiblePerson: {
|
|
|
|
|
create: v.responsiblePersonId?.map((id) => ({
|
|
|
|
|
userId: id,
|
|
|
|
|
})),
|
|
|
|
|
},
|
|
|
|
|
})),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Put("{templateId}")
|
|
|
|
|
async updateFlowTemplate(
|
|
|
|
|
@Request() _req: RequestWithUser,
|
|
|
|
|
@Path() templateId: string,
|
|
|
|
|
@Body() body: WorkflowPayload,
|
|
|
|
|
) {
|
|
|
|
|
return await prisma.workflowTemplate.update({
|
|
|
|
|
where: { id: templateId },
|
|
|
|
|
data: {
|
|
|
|
|
...body,
|
|
|
|
|
step: {
|
|
|
|
|
create: body.step.map((v, i) => ({
|
|
|
|
|
type: v.type,
|
|
|
|
|
name: v.name,
|
|
|
|
|
order: i + 1,
|
|
|
|
|
value: {
|
|
|
|
|
create: v.value?.map((val) => ({ value: val })),
|
|
|
|
|
},
|
|
|
|
|
responsiblePerson: {
|
|
|
|
|
create: v.responsiblePersonId?.map((id) => ({ userId: id })),
|
|
|
|
|
},
|
|
|
|
|
})),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Delete("{templateId}")
|
|
|
|
|
async deleteFlowTemplateById(@Path() templateId: string) {
|
|
|
|
|
return await prisma.workflowTemplate.delete({
|
|
|
|
|
where: { id: templateId },
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|