From ab484f4026fb1cf4688c96af227c289399c889de Mon Sep 17 00:00:00 2001 From: Methapon Metanipat Date: Mon, 19 Aug 2024 10:55:32 +0700 Subject: [PATCH] refactor: use prisma nested create instead --- src/controllers/quotation-controller.ts | 260 ++++++++---------- src/controllers/service/service-controller.ts | 70 ++--- 2 files changed, 138 insertions(+), 192 deletions(-) diff --git a/src/controllers/quotation-controller.ts b/src/controllers/quotation-controller.ts index adf0427..d9fbe16 100644 --- a/src/controllers/quotation-controller.ts +++ b/src/controllers/quotation-controller.ts @@ -406,83 +406,7 @@ export class QuotationController extends Controller { update: { value: { increment: 1 } }, }); - const quotation = await tx.quotation.create({ - data: { - ...rest, - statusOrder: +(rest.status === "INACTIVE"), - code: `${currentYear.toString().padStart(2, "0")}${currentMonth.toString().padStart(2, "0")}${currentDate.toString().padStart(2, "0")}${lastQuotation.value.toString().padStart(4, "0")}`, - worker: { - createMany: { - data: sortedEmployeeId.map((v, i) => ({ - no: i, - code: "", - employeeId: v, - })), - }, - }, - totalPrice: price.totalPrice, - totalDiscount: price.totalDiscount, - - vat: price.totalVat, - vatExcluded: 0, - - finalPrice: price.totalPrice - price.totalDiscount, - - paySplit: { - createMany: { - data: (rest.paySplit || []).map((v, i) => ({ - no: i + 1, - date: v, - })), - }, - }, - - createdByUserId: req.user.sub, - updatedByUserId: req.user.sub, - }, - }); - - await Promise.all( - restructureService.map(async (a) => { - const { id: _currentServiceId } = await tx.quotationService.create({ - data: { - code: a.code, - name: a.name, - detail: a.detail, - attributes: a.attributes, - quotationId: quotation.id, - refServiceId: a.id, - }, - }); - - await Promise.all( - a.work.map(async (b) => { - await tx.quotationServiceWork.create({ - data: { - order: b.order, - name: b.name, - attributes: b.attributes, - serviceId: _currentServiceId, - productOnWork: { - createMany: { - data: b.product.map((v, i) => ({ - productId: v.id, - order: i + 1, - vat: v.vat, - amount: v.amount, - discount: v.discount, - pricePerUnit: v.pricePerUnit, - })), - }, - }, - }, - }); - }), - ); - }), - ); - - return await tx.quotation.findUnique({ + return await tx.quotation.create({ include: { service: { include: { @@ -504,7 +428,64 @@ export class QuotationController extends Controller { select: { service: true }, }, }, - where: { id: quotation.id }, + + data: { + ...rest, + statusOrder: +(rest.status === "INACTIVE"), + code: `${currentYear.toString().padStart(2, "0")}${currentMonth.toString().padStart(2, "0")}${currentDate.toString().padStart(2, "0")}${lastQuotation.value.toString().padStart(4, "0")}`, + worker: { + createMany: { + data: sortedEmployeeId.map((v, i) => ({ + no: i, + code: "", + employeeId: v, + })), + }, + }, + totalPrice: price.totalPrice, + totalDiscount: price.totalDiscount, + vat: price.totalVat, + vatExcluded: 0, + finalPrice: price.totalPrice - price.totalDiscount, + paySplit: { + createMany: { + data: (rest.paySplit || []).map((v, i) => ({ + no: i + 1, + date: v, + })), + }, + }, + service: { + create: restructureService.map((a) => ({ + code: a.code, + name: a.name, + detail: a.detail, + attributes: a.attributes, + refServiceId: a.id, + work: { + create: a.work.map((b) => ({ + order: b.order, + name: b.name, + attributes: b.attributes, + productOnWork: { + createMany: { + data: b.product.map((v, i) => ({ + productId: v.id, + order: i + 1, + vat: v.vat, + amount: v.amount, + discount: v.discount, + pricePerUnit: v.pricePerUnit, + })), + }, + }, + })), + }, + })), + }, + createdByUserId: req.user.sub, + updatedByUserId: req.user.sub, + }, }); }); } @@ -691,7 +672,28 @@ export class QuotationController extends Controller { }; }); - const quotation = await tx.quotation.update({ + return await tx.quotation.update({ + include: { + service: { + include: { + work: { + include: { + productOnWork: { + include: { product: true }, + }, + }, + }, + }, + }, + paySplit: true, + worker: true, + customerBranch: { + include: { customer: true }, + }, + _count: { + select: { service: true }, + }, + }, where: { id: quotationId }, data: { ...rest, @@ -731,78 +733,42 @@ export class QuotationController extends Controller { } : undefined, - service: body.service ? { deleteMany: {} } : undefined, + service: + body.service && restructureService + ? { + deleteMany: {}, + create: restructureService.map((a) => ({ + code: a.code, + name: a.name, + detail: a.detail, + attributes: a.attributes, + refServiceId: a.id, + work: { + create: a.work.map((b) => ({ + order: b.order, + name: b.name, + attributes: b.attributes, + productOnWork: { + createMany: { + data: b.product.map((v, i) => ({ + productId: v.id, + order: i + 1, + vat: v.vat, + amount: v.amount, + discount: v.discount, + pricePerUnit: v.pricePerUnit, + })), + }, + }, + })), + }, + })), + } + : undefined, updatedByUserId: req.user.sub, }, }); - - if (restructureService) { - await Promise.all( - restructureService.map(async (a) => { - const { id: _currentServiceId } = await tx.quotationService.create({ - data: { - code: a.code, - name: a.name, - detail: a.detail, - attributes: a.attributes, - quotationId: quotation.id, - refServiceId: a.id, - }, - }); - - await Promise.all( - a.work.map(async (b) => { - await tx.quotationServiceWork.create({ - data: { - order: b.order, - name: b.name, - attributes: b.attributes, - serviceId: _currentServiceId, - productOnWork: { - createMany: { - data: b.product.map((v, i) => ({ - productId: v.id, - order: i + 1, - vat: v.vat, - amount: v.amount, - discount: v.discount, - pricePerUnit: v.pricePerUnit, - })), - }, - }, - }, - }); - }), - ); - }), - ); - } - - return await tx.quotation.findUnique({ - include: { - service: { - include: { - work: { - include: { - productOnWork: { - include: { product: true }, - }, - }, - }, - }, - }, - paySplit: true, - worker: true, - customerBranch: { - include: { customer: true }, - }, - _count: { - select: { service: true }, - }, - }, - where: { id: quotation.id }, - }); }); } diff --git a/src/controllers/service/service-controller.ts b/src/controllers/service/service-controller.ts index b0e5f55..49d217e 100644 --- a/src/controllers/service/service-controller.ts +++ b/src/controllers/service/service-controller.ts @@ -286,34 +286,12 @@ export class ServiceController extends Controller { update: { value: { increment: 1 } }, }); - 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 tx.service.create({ include: { work: { include: { productOnWork: { - include: { - product: true, - }, + include: { product: true }, orderBy: { order: "asc" }, }, }, @@ -326,7 +304,19 @@ export class ServiceController extends Controller { productTypeId, statusOrder: +(body.status === "INACTIVE"), code: `${body.code.toLocaleUpperCase()}${last.value.toString().padStart(3, "0")}`, - work: { connect: workList.map((v) => ({ id: v.id })) }, + work: { + create: (work || []).map((w, wIdx) => ({ + name: w.name, + order: wIdx + 1, + attributes: w.attributes, + productOnWork: { + create: w.productId.map((p, pIdx) => ({ + productId: p, + order: pIdx + 1, + })), + }, + })), + }, createdByUserId: req.user.sub, updatedByUserId: req.user.sub, }, @@ -412,26 +402,6 @@ export class ServiceController extends Controller { } 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 tx.service.update({ include: { createdBy: true, @@ -442,7 +412,17 @@ export class ServiceController extends Controller { statusOrder: +(payload.status === "INACTIVE"), work: { deleteMany: {}, - connect: workList.map((v) => ({ id: v.id })), + create: (work || []).map((w, wIdx) => ({ + name: w.name, + order: wIdx + 1, + attributes: w.attributes, + productOnWork: { + create: w.productId.map((p, pIdx) => ({ + productId: p, + order: pIdx + 1, + })), + }, + })), }, updatedByUserId: req.user.sub, },