diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 511e233..f96bff4 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -1393,6 +1393,8 @@ enum RequestDataStatus { model RequestData { id String @id @default(cuid()) + code String + employee Employee @relation(fields: [employeeId], references: [id], onDelete: Cascade) employeeId String diff --git a/src/controllers/05-payment-controller.ts b/src/controllers/05-payment-controller.ts index 68dfb60..bb179ce 100644 --- a/src/controllers/05-payment-controller.ts +++ b/src/controllers/05-payment-controller.ts @@ -134,7 +134,7 @@ export class QuotationPayment extends Controller { const year = `${current.getFullYear()}`.slice(-2).padStart(2, "0"); const month = `${current.getMonth() + 1}`.padStart(2, "0"); - const last = + const lastReceipt = body.paymentStatus === "PaymentSuccess" && record.paymentStatus !== "PaymentSuccess" ? await tx.runningNo.upsert({ where: { @@ -154,7 +154,9 @@ export class QuotationPayment extends Controller { where: { id: paymentId, invoice: { quotationId: quotation.id } }, data: { ...body, - code: last ? `RE${year}${month}${last.value.toString().padStart(6, "0")}` : undefined, + code: lastReceipt + ? `RE${year}${month}${lastReceipt.value.toString().padStart(6, "0")}` + : undefined, }, }); @@ -175,22 +177,36 @@ export class QuotationPayment extends Controller { paymentSum._sum.amount || 0 >= quotation.finalPrice ? "PaymentSuccess" : "PaymentInProcess", - requestData: - body.paymentStatus === "PaymentSuccess" && - (paymentSum._sum.amount || 0) - payment.amount <= 0 - ? { - create: quotation.worker.map((v) => ({ - employeeId: v.employeeId, - requestWork: { - create: quotation.productServiceList.flatMap((item) => - item.worker.findIndex((w) => w.employeeId === v.employeeId) !== -1 - ? { productServiceId: item.id } - : [], - ), - }, - })), - } - : undefined, + 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: 1, + }, + update: { value: { increment: 1 } }, + }); + return { + create: quotation.worker.map((v) => ({ + code: `TR${year}${month}${lastRequest.value.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 } + : [], + ), + }, + })), + }; + } + })(), }, });