refactor: prevent recreation of work

This commit is contained in:
Methapon Metanipat 2024-11-25 15:03:49 +07:00
parent c7ae96d119
commit 6580c17273

View file

@ -91,6 +91,7 @@ type ServiceUpdate = {
status?: "ACTIVE" | "INACTIVE"; status?: "ACTIVE" | "INACTIVE";
workflowId?: string; workflowId?: string;
work?: { work?: {
id?: string;
name: string; name: string;
product: { product: {
id: string; id: string;
@ -401,17 +402,36 @@ export class ServiceController extends Controller {
...payload, ...payload,
statusOrder: +(payload.status === "INACTIVE"), statusOrder: +(payload.status === "INACTIVE"),
work: { work: {
deleteMany: {}, deleteMany: {
create: (work || []).map((w, wIdx) => ({ id: work?.some((v) => !!v.id)
name: w.name, ? { notIn: work.flatMap((v) => (!!v.id ? v.id : [])) }
order: wIdx + 1, : undefined,
attributes: w.attributes, },
productOnWork: { upsert: (work || []).map((w, wIdx) => ({
create: w.product.map((p, pIdx) => ({ where: { id: w.id },
productId: p.id, create: {
installmentNo: p.installmentNo, name: w.name,
order: pIdx + 1, order: wIdx + 1,
})), attributes: w.attributes,
productOnWork: {
create: w.product.map((p, pIdx) => ({
productId: p.id,
installmentNo: p.installmentNo,
order: pIdx + 1,
})),
},
},
update: {
name: w.name,
order: wIdx + 1,
attributes: w.attributes,
productOnWork: {
create: w.product.map((p, pIdx) => ({
productId: p.id,
installmentNo: p.installmentNo,
order: pIdx + 1,
})),
},
}, },
})), })),
}, },