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";
workflowId?: string;
work?: {
id?: string;
name: string;
product: {
id: string;
@ -401,17 +402,36 @@ export class ServiceController extends Controller {
...payload,
statusOrder: +(payload.status === "INACTIVE"),
work: {
deleteMany: {},
create: (work || []).map((w, wIdx) => ({
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,
})),
deleteMany: {
id: work?.some((v) => !!v.id)
? { notIn: work.flatMap((v) => (!!v.id ? v.id : [])) }
: undefined,
},
upsert: (work || []).map((w, wIdx) => ({
where: { id: w.id },
create: {
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,
})),
},
},
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,
})),
},
},
})),
},