From 17b92b4012744db45798d1c1bd11e6e4524a33e9 Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Wed, 5 Mar 2025 14:41:55 +0700 Subject: [PATCH] feat: notify request, task update --- src/controllers/05-quotation-controller.ts | 78 +++++----- src/controllers/07-task-controller.ts | 164 ++++++++++++--------- 2 files changed, 137 insertions(+), 105 deletions(-) diff --git a/src/controllers/05-quotation-controller.ts b/src/controllers/05-quotation-controller.ts index de771fd..86a53f6 100644 --- a/src/controllers/05-quotation-controller.ts +++ b/src/controllers/05-quotation-controller.ts @@ -1136,41 +1136,53 @@ export class QuotationActionController extends Controller { }, update: { value: { increment: quotation.worker.length } }, }); - await tx.quotation.update({ - where: { id: quotationId, isDebitNote: false }, - data: { - quotationStatus: QuotationStatus.PaymentSuccess, // NOTE: change back if already complete or canceled - worker: { - createMany: { - data: rearrange - .filter((lhs) => !quotation.worker.find((rhs) => rhs.employeeId === lhs.workerId)) - .map((v, i) => ({ - no: quotation._count.worker + i + 1, - employeeId: v.workerId, - })), + await tx.quotation + .update({ + include: { requestData: true }, + where: { id: quotationId, isDebitNote: false }, + data: { + quotationStatus: QuotationStatus.PaymentSuccess, // NOTE: change back if already complete or canceled + worker: { + createMany: { + data: rearrange + .filter((lhs) => !quotation.worker.find((rhs) => rhs.employeeId === lhs.workerId)) + .map((v, i) => ({ + no: quotation._count.worker + i + 1, + employeeId: v.workerId, + })), + }, }, + requestData: + quotation.quotationStatus === "PaymentInProcess" || + quotation.quotationStatus === "PaymentSuccess" + ? { + create: rearrange + .filter( + (lhs) => + !quotation.worker.find((rhs) => rhs.employeeId === lhs.workerId) && + lhs.productServiceId.length > 0, + ) + .map((v, i) => ({ + code: `TR${year}${month}${(lastRequest.value - quotation._count.worker + i + 1).toString().padStart(6, "0")}`, + employeeId: v.workerId, + requestWork: { + create: v.productServiceId.map((v) => ({ productServiceId: v })), + }, + })), + } + : undefined, }, - requestData: - quotation.quotationStatus === "PaymentInProcess" || - quotation.quotationStatus === "PaymentSuccess" - ? { - create: rearrange - .filter( - (lhs) => - !quotation.worker.find((rhs) => rhs.employeeId === lhs.workerId) && - lhs.productServiceId.length > 0, - ) - .map((v, i) => ({ - code: `TR${year}${month}${(lastRequest.value - quotation._count.worker + i + 1).toString().padStart(6, "0")}`, - employeeId: v.workerId, - requestWork: { - create: v.productServiceId.map((v) => ({ productServiceId: v })), - }, - })), - } - : undefined, - }, - }); + }) + .then(async (ret) => { + await prisma.notification.create({ + data: { + title: "New Request", + detail: "New request: " + ret.requestData.map((v) => v.code).join(", "), + registeredBranchId: ret.registeredBranchId, + groupReceiver: { create: { name: "document_checker" } }, + }, + }); + }); }); } } diff --git a/src/controllers/07-task-controller.ts b/src/controllers/07-task-controller.ts index 31f44fc..59d26d7 100644 --- a/src/controllers/07-task-controller.ts +++ b/src/controllers/07-task-controller.ts @@ -433,88 +433,101 @@ export class TaskController extends Controller { ); } - return await prisma.$transaction(async (tx) => { - await Promise.all( - record.taskList - .filter( - (lhs) => - !body.taskList.find( - (rhs) => lhs.requestWorkId === rhs.requestWorkId && lhs.step === rhs.step, - ), - ) - .map((v) => - tx.task.update({ - where: { id: v.id }, - data: { - requestWorkStep: { update: { workStatus: "Ready" } }, - }, - }), - ), - ); + return await prisma + .$transaction(async (tx) => { + await Promise.all( + record.taskList + .filter( + (lhs) => + !body.taskList.find( + (rhs) => lhs.requestWorkId === rhs.requestWorkId && lhs.step === rhs.step, + ), + ) + .map((v) => + tx.task.update({ + where: { id: v.id }, + data: { + requestWorkStep: { update: { workStatus: "Ready" } }, + }, + }), + ), + ); - await tx.requestWorkStepStatus.updateMany({ - where: { - OR: body.taskList, - workStatus: RequestWorkStatus.Ready, - }, - data: { workStatus: RequestWorkStatus.InProgress }, - }); - - const work = await tx.requestWorkStepStatus.findMany({ - include: { - requestWork: { - include: { - request: { - include: { quotation: true }, - }, - }, + await tx.requestWorkStepStatus.updateMany({ + where: { + OR: body.taskList, + workStatus: RequestWorkStatus.Ready, }, - }, - where: { OR: body.taskList }, - }); + data: { workStatus: RequestWorkStatus.InProgress }, + }); - return await tx.taskOrder.update({ - where: { id: taskOrderId }, - include: { - taskList: { - include: { - requestWorkStep: { - include: { - requestWork: true, + const work = await tx.requestWorkStepStatus.findMany({ + include: { + requestWork: { + include: { + request: { + include: { quotation: true }, }, }, }, }, - institution: true, - registeredBranch: true, - createdBy: true, - }, - data: { - ...body, - urgent: work.some((v) => v.requestWork.request.quotation.urgent), - taskList: { - deleteMany: record?.taskList - .filter( - (lhs) => - !body.taskList.find( - (rhs) => lhs.requestWorkId === rhs.requestWorkId && lhs.step === rhs.step, - ), - ) - .map((v) => ({ id: v.id })), - createMany: { - data: body.taskList.filter( - (lhs) => - !record?.taskList.find( - (rhs) => lhs.requestWorkId === rhs.requestWorkId && lhs.step === rhs.step, - ), - ), - skipDuplicates: true, + where: { OR: body.taskList }, + }); + + return await tx.taskOrder.update({ + where: { id: taskOrderId }, + include: { + taskList: { + include: { + requestWorkStep: { + include: { + requestWork: true, + }, + }, + }, }, + institution: true, + registeredBranch: true, + createdBy: true, }, - taskProduct: { deleteMany: {}, create: body.taskProduct }, - }, + data: { + ...body, + urgent: work.some((v) => v.requestWork.request.quotation.urgent), + taskList: { + deleteMany: record?.taskList + .filter( + (lhs) => + !body.taskList.find( + (rhs) => lhs.requestWorkId === rhs.requestWorkId && lhs.step === rhs.step, + ), + ) + .map((v) => ({ id: v.id })), + createMany: { + data: body.taskList.filter( + (lhs) => + !record?.taskList.find( + (rhs) => lhs.requestWorkId === rhs.requestWorkId && lhs.step === rhs.step, + ), + ), + skipDuplicates: true, + }, + }, + taskProduct: { deleteMany: {}, create: body.taskProduct }, + }, + }); + }) + .then(async (ret) => { + if (body.taskOrderStatus && record.taskOrderStatus !== body.taskOrderStatus) { + await prisma.notification.create({ + data: { + title: "Task Submitted", + detail: "Task submitted in order: " + record.code, + receiverId: record.createdByUserId, + }, + }); + } + return ret; }); - }); } @Delete("{taskOrderId}") @@ -651,6 +664,13 @@ export class TaskActionController extends Controller { }, data: { userTaskStatus: UserTaskStatus.Submit, submittedAt: new Date() }, }), + prisma.notification.create({ + data: { + title: "Task Submitted", + detail: "Task submitted in order: " + record.code, + receiverId: record.createdByUserId, + }, + }), ]); }