feat: update now detect already exists step

This commit is contained in:
Methapon Metanipat 2024-10-28 16:07:02 +07:00
parent af37ac850c
commit 6be298acfb

View file

@ -28,6 +28,7 @@ import { filterStatus } from "../services/prisma";
type WorkflowPayload = { type WorkflowPayload = {
name: string; name: string;
step: { step: {
id?: string;
name: string; name: string;
type?: string; type?: string;
value?: string[]; value?: string[];
@ -176,8 +177,12 @@ export class FlowTemplateController extends Controller {
...body, ...body,
statusOrder: +(body.status === "INACTIVE"), statusOrder: +(body.status === "INACTIVE"),
step: { step: {
deleteMany: {}, deleteMany: {
create: body.step.map((v, i) => ({ id: { notIn: body.step.flatMap((v) => v.id || []) },
},
upsert: body.step.map((v, i) => ({
where: { id: v.id || "" },
create: {
type: v.type, type: v.type,
name: v.name, name: v.name,
order: i + 1, order: i + 1,
@ -187,6 +192,19 @@ export class FlowTemplateController extends Controller {
responsiblePerson: { responsiblePerson: {
create: v.responsiblePersonId?.map((id) => ({ userId: id })), create: v.responsiblePersonId?.map((id) => ({ userId: id })),
}, },
id: undefined,
},
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 })),
},
},
})), })),
}, },
}, },