diff --git a/src/controllers/service/service-controller.ts b/src/controllers/service/service-controller.ts index bdbb259..95f7d4d 100644 --- a/src/controllers/service/service-controller.ts +++ b/src/controllers/service/service-controller.ts @@ -247,26 +247,40 @@ export class ServiceController extends Controller { if (!(await prisma.service.findUnique({ where: { id: serviceId } }))) { throw new HttpError(HttpStatus.NOT_FOUND, "Service cannot be found.", "serviceNotFound"); } - const { work, ...payload } = body; - - const record = await prisma.service.update({ - data: { - ...payload, - work: { - deleteMany: {}, - createMany: { - data: - work?.map((v, i) => ({ - order: i + 1, - ...v, - })) || [], + const record = await prisma.$transaction(async (tx) => { + const workList = await Promise.all( + (work || []).map(async (w, wIdx) => + tx.work.create({ + data: { + name: w.name, + order: wIdx + 1, + attributes: w.attributes, + productOnWork: { + createMany: { + data: w.productId.map((p, pIdx) => ({ + productId: p, + order: pIdx + 1, + })), + }, + }, + }, + }), + ), + ); + return await prisma.service.update({ + data: { + ...payload, + work: { + deleteMany: {}, + connect: workList.map((v) => ({ id: v.id })), }, + updateBy: req.user.name, }, - updateBy: req.user.name, - }, - where: { id: serviceId }, + where: { id: serviceId }, + }); }); + return Object.assign(record, { imageUrl: await presignedGetObjectIfExist( MINIO_BUCKET,