diff --git a/src/controllers/04-flow-template-controller.ts b/src/controllers/04-flow-template-controller.ts index 8217bd3..7361d09 100644 --- a/src/controllers/04-flow-template-controller.ts +++ b/src/controllers/04-flow-template-controller.ts @@ -28,6 +28,7 @@ import { filterStatus } from "../services/prisma"; type WorkflowPayload = { name: string; step: { + id?: string; name: string; type?: string; value?: string[]; @@ -176,16 +177,33 @@ export class FlowTemplateController extends Controller { ...body, statusOrder: +(body.status === "INACTIVE"), step: { - deleteMany: {}, - create: body.step.map((v, i) => ({ - type: v.type, - name: v.name, - order: i + 1, - value: { - create: v.value?.map((val) => ({ value: val })), + deleteMany: { + id: { notIn: body.step.flatMap((v) => v.id || []) }, + }, + upsert: body.step.map((v, i) => ({ + where: { id: v.id || "" }, + create: { + 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 })), + }, + id: undefined, }, - responsiblePerson: { - create: v.responsiblePersonId?.map((id) => ({ userId: id })), + update: { + 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 })), + }, }, })), },