feat: add workflow template
This commit is contained in:
parent
e845bd7fd6
commit
c4f3677ca6
3 changed files with 219 additions and 0 deletions
108
src/controllers/04-flow-template-controller.ts
Normal file
108
src/controllers/04-flow-template-controller.ts
Normal file
|
|
@ -0,0 +1,108 @@
|
|||
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 },
|
||||
});
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue