109 lines
2.6 KiB
TypeScript
109 lines
2.6 KiB
TypeScript
|
|
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,
|
||
|
|
) {
|
||
|
|
return await prisma.workflowTemplate.findMany({
|
||
|
|
include: {
|
||
|
|
step: {
|
||
|
|
include: { value: true },
|
||
|
|
},
|
||
|
|
},
|
||
|
|
orderBy: { createdAt: "asc" },
|
||
|
|
take: pageSize,
|
||
|
|
skip: (page - 1) * pageSize,
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
@Get("{templateId}")
|
||
|
|
async getFlowTemplateById(@Request() _req: RequestWithUser, @Path() templateId: string) {
|
||
|
|
return await prisma.workflowTemplate.findFirst({
|
||
|
|
include: {
|
||
|
|
step: {
|
||
|
|
include: { value: true },
|
||
|
|
},
|
||
|
|
},
|
||
|
|
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 },
|
||
|
|
});
|
||
|
|
}
|
||
|
|
}
|