diff --git a/src/controllers/05-payment-controller.ts b/src/controllers/05-payment-controller.ts index 1177dba..e0bbd20 100644 --- a/src/controllers/05-payment-controller.ts +++ b/src/controllers/05-payment-controller.ts @@ -177,55 +177,66 @@ export class QuotationPayment extends Controller { }, }); - await tx.quotation.update({ - where: { id: quotation.id }, - data: { - quotationStatus: - (paymentSum._sum.amount || 0) >= quotation.finalPrice - ? "PaymentSuccess" - : "PaymentInProcess", - requestData: await (async () => { - if ( - body.paymentStatus === "PaymentSuccess" && - (paymentSum._sum.amount || 0) - payment.amount <= 0 - ) { - const lastRequest = await tx.runningNo.upsert({ - where: { - key: `REQUEST_${year}${month}`, - }, - create: { - key: `REQUEST_${year}${month}`, - value: quotation.worker.length, - }, - update: { value: { increment: quotation.worker.length } }, - }); - return { - create: quotation.worker.flatMap((v, i) => { - const productEmployee = quotation.productServiceList.flatMap((item) => - item.worker.findIndex((w) => w.employeeId === v.employeeId) !== -1 - ? { productServiceId: item.id } - : [], - ); + await tx.quotation + .update({ + where: { id: quotation.id }, + data: { + quotationStatus: + (paymentSum._sum.amount || 0) >= quotation.finalPrice + ? "PaymentSuccess" + : "PaymentInProcess", + requestData: await (async () => { + if ( + body.paymentStatus === "PaymentSuccess" && + (paymentSum._sum.amount || 0) - payment.amount <= 0 + ) { + const lastRequest = await tx.runningNo.upsert({ + where: { + key: `REQUEST_${year}${month}`, + }, + create: { + key: `REQUEST_${year}${month}`, + value: quotation.worker.length, + }, + update: { value: { increment: quotation.worker.length } }, + }); + return { + create: quotation.worker.flatMap((v, i) => { + const productEmployee = quotation.productServiceList.flatMap((item) => + item.worker.findIndex((w) => w.employeeId === v.employeeId) !== -1 + ? { productServiceId: item.id } + : [], + ); - if (productEmployee.length <= 0) return []; + if (productEmployee.length <= 0) return []; - return { - code: `TR${year}${month}${(lastRequest.value - quotation.worker.length + i + 1).toString().padStart(6, "0")}`, - employeeId: v.employeeId, - requestWork: { - create: quotation.productServiceList.flatMap((item) => - item.worker.findIndex((w) => w.employeeId === v.employeeId) !== -1 - ? { productServiceId: item.id } - : [], - ), - }, - }; - }), - }; - } - })(), - }, - }); + return { + code: `TR${year}${month}${(lastRequest.value - quotation.worker.length + i + 1).toString().padStart(6, "0")}`, + employeeId: v.employeeId, + requestWork: { + create: quotation.productServiceList.flatMap((item) => + item.worker.findIndex((w) => w.employeeId === v.employeeId) !== -1 + ? { productServiceId: item.id } + : [], + ), + }, + }; + }), + }; + } + })(), + }, + }) + .then(async (res) => { + if (quotation.quotationStatus !== res.quotationStatus) + await tx.notification.create({ + data: { + title: "Quotation Status Updated", + detail: res.code + res.quotationStatus, + receiverId: res.createdByUserId, + }, + }); + }); return payment; }); diff --git a/src/controllers/05-quotation-controller.ts b/src/controllers/05-quotation-controller.ts index 9ae2ae1..de771fd 100644 --- a/src/controllers/05-quotation-controller.ts +++ b/src/controllers/05-quotation-controller.ts @@ -454,7 +454,7 @@ export class QuotationController extends Controller { const { productServiceList: _productServiceList, worker: _worker, ...rest } = body; - return await prisma.$transaction(async (tx) => { + const ret = await prisma.$transaction(async (tx) => { const nonExistEmployee = body.worker.filter((v) => typeof v !== "string"); const lastEmployee = await tx.runningNo.upsert({ where: { @@ -639,6 +639,17 @@ export class QuotationController extends Controller { }, }); }); + + await prisma.notification.create({ + data: { + title: "New Quotation", + detail: "New quotation: " + ret.code, + registeredBranchId: ret.registeredBranchId, + groupReceiver: { create: [{ name: "accountant" }, { name: "head_of_accountant" }] }, + }, + }); + + return ret; } @Put("{quotationId}") diff --git a/src/controllers/06-request-list-controller.ts b/src/controllers/06-request-list-controller.ts index bbb2e39..47d6039 100644 --- a/src/controllers/06-request-list-controller.ts +++ b/src/controllers/06-request-list-controller.ts @@ -268,14 +268,24 @@ export class RequestDataActionController extends Controller { }), ]); await Promise.all([ - tx.quotation.updateMany({ - where: { - requestData: { - every: { requestDataStatus: RequestDataStatus.Canceled }, + tx.quotation + .updateManyAndReturn({ + where: { + requestData: { + every: { requestDataStatus: RequestDataStatus.Canceled }, + }, }, - }, - data: { quotationStatus: QuotationStatus.Canceled, urgent: false }, - }), + data: { quotationStatus: QuotationStatus.Canceled, urgent: false }, + }) + .then(async (res) => { + await tx.notification.createMany({ + data: res.map((v) => ({ + title: "Quotation Status Updated", + detail: v.code + "Canceled", + receiverId: v.createdByUserId, + })), + }); + }), tx.taskOrder.updateMany({ where: { taskList: { @@ -405,14 +415,25 @@ export class RequestDataActionController extends Controller { data: { taskStatus: TaskStatus.Canceled }, }); await Promise.all([ - tx.quotation.updateMany({ - where: { - requestData: { - every: { requestDataStatus: RequestDataStatus.Canceled }, + tx.quotation + .updateManyAndReturn({ + where: { + quotationStatus: { not: QuotationStatus.Canceled }, + requestData: { + every: { requestDataStatus: RequestDataStatus.Canceled }, + }, }, - }, - data: { quotationStatus: QuotationStatus.Canceled, urgent: false }, - }), + data: { quotationStatus: QuotationStatus.Canceled, urgent: false }, + }) + .then(async (res) => { + await tx.notification.createMany({ + data: res.map((v) => ({ + title: "Quotation Status Updated", + detail: v.code + "Canceled", + receiverId: v.createdByUserId, + })), + }); + }), tx.taskOrder.updateMany({ where: { taskList: { @@ -479,21 +500,31 @@ export class RequestDataActionController extends Controller { where: { id: { in: completed } }, data: { requestDataStatus: RequestDataStatus.Completed }, }); - await tx.quotation.updateMany({ - where: { - quotationStatus: { - notIn: [QuotationStatus.Canceled, QuotationStatus.ProcessComplete], - }, - requestData: { - every: { - requestDataStatus: { - in: [RequestDataStatus.Canceled, RequestDataStatus.Completed], + await tx.quotation + .updateManyAndReturn({ + where: { + quotationStatus: { + notIn: [QuotationStatus.Canceled, QuotationStatus.ProcessComplete], + }, + requestData: { + every: { + requestDataStatus: { + in: [RequestDataStatus.Canceled, RequestDataStatus.Completed], + }, }, }, }, - }, - data: { quotationStatus: QuotationStatus.ProcessComplete, urgent: false }, - }); + data: { quotationStatus: QuotationStatus.ProcessComplete, urgent: false }, + }) + .then(async (res) => { + await tx.notification.createMany({ + data: res.map((v) => ({ + title: "Quotation Status Updated", + detail: v.code + "Completed", + receiverId: v.createdByUserId, + })), + }); + }); // dataRecord.push(record); return data; }); @@ -812,14 +843,25 @@ export class RequestListController extends Controller { data: { taskStatus: TaskStatus.Canceled }, }); await Promise.all([ - tx.quotation.updateMany({ - where: { - requestData: { - every: { requestDataStatus: RequestDataStatus.Canceled }, + tx.quotation + .updateManyAndReturn({ + where: { + quotationStatus: { not: QuotationStatus.Canceled }, + requestData: { + every: { requestDataStatus: RequestDataStatus.Canceled }, + }, }, - }, - data: { quotationStatus: QuotationStatus.Canceled, urgent: false }, - }), + data: { quotationStatus: QuotationStatus.Canceled, urgent: false }, + }) + .then(async (res) => { + await tx.notification.createMany({ + data: res.map((v) => ({ + title: "Quotation Status Updated", + detail: v.code + "Canceled", + receiverId: v.createdByUserId, + })), + }); + }), tx.taskOrder.updateMany({ where: { taskList: { @@ -887,19 +929,32 @@ export class RequestListController extends Controller { where: { id: { in: completed } }, data: { requestDataStatus: RequestDataStatus.Completed }, }); - await tx.quotation.updateMany({ - where: { - quotationStatus: { - notIn: [QuotationStatus.Canceled, QuotationStatus.ProcessComplete], - }, - requestData: { - every: { - requestDataStatus: { in: [RequestDataStatus.Canceled, RequestDataStatus.Completed] }, + await tx.quotation + .updateManyAndReturn({ + where: { + quotationStatus: { + notIn: [QuotationStatus.Canceled, QuotationStatus.ProcessComplete], + }, + requestData: { + every: { + requestDataStatus: { + in: [RequestDataStatus.Canceled, RequestDataStatus.Completed], + }, + }, }, }, - }, - data: { quotationStatus: QuotationStatus.ProcessComplete, urgent: false }, - }); + data: { quotationStatus: QuotationStatus.ProcessComplete, urgent: false }, + }) + .then(async (res) => { + await tx.notification.createMany({ + data: res.map((v) => ({ + title: "Quotation Status Updated", + detail: v.code + "Completed", + receiverId: v.createdByUserId, + })), + }); + }); + return record; }); } diff --git a/src/controllers/07-task-controller.ts b/src/controllers/07-task-controller.ts index a572962..c782294 100644 --- a/src/controllers/07-task-controller.ts +++ b/src/controllers/07-task-controller.ts @@ -785,19 +785,31 @@ export class TaskActionController extends Controller { where: { id: { in: completed } }, data: { requestDataStatus: RequestDataStatus.Completed }, }); - await tx.quotation.updateMany({ - where: { - quotationStatus: { - notIn: [QuotationStatus.Canceled, QuotationStatus.ProcessComplete], - }, - requestData: { - every: { - requestDataStatus: { in: [RequestDataStatus.Canceled, RequestDataStatus.Completed] }, + await tx.quotation + .updateManyAndReturn({ + where: { + quotationStatus: { + notIn: [QuotationStatus.Canceled, QuotationStatus.ProcessComplete], + }, + requestData: { + every: { + requestDataStatus: { + in: [RequestDataStatus.Canceled, RequestDataStatus.Completed], + }, + }, }, }, - }, - data: { quotationStatus: QuotationStatus.ProcessComplete, urgent: false }, - }); + data: { quotationStatus: QuotationStatus.ProcessComplete, urgent: false }, + }) + .then(async (res) => { + await tx.notification.createMany({ + data: res.map((v) => ({ + title: "Quotation Status Updated", + detail: v.code + "Completed", + receiverId: v.createdByUserId, + })), + }); + }); }); } }