diff --git a/prisma/migrations/20250513084929_add/migration.sql b/prisma/migrations/20250513084929_add/migration.sql new file mode 100644 index 0000000..f982e5b --- /dev/null +++ b/prisma/migrations/20250513084929_add/migration.sql @@ -0,0 +1,3 @@ +-- AlterTable +ALTER TABLE "QuotationWorker" ADD COLUMN "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, +ADD COLUMN "updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 73be520..47f82d6 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -1410,6 +1410,9 @@ model QuotationWorker { employeeId String quotation Quotation @relation(fields: [quotationId], references: [id], onDelete: Cascade) quotationId String + + createdAt DateTime @default(now()) + updatedAt DateTime @default(now()) @updatedAt } model QuotationProductServiceList { diff --git a/src/controllers/07-task-controller.ts b/src/controllers/07-task-controller.ts index 5e9977c..3f15e07 100644 --- a/src/controllers/07-task-controller.ts +++ b/src/controllers/07-task-controller.ts @@ -606,7 +606,21 @@ export class TaskActionController extends Controller { return await prisma.$transaction(async (tx) => { const promises = body.map(async (v) => { const record = await tx.task.findFirst({ - include: { requestWorkStep: true }, + include: { + requestWorkStep: { + include: { + requestWork: { + include: { + request: { + include: { + quotation: true, + }, + }, + }, + }, + }, + }, + }, where: { step: v.step, requestWorkId: v.requestWorkId, @@ -624,6 +638,20 @@ export class TaskActionController extends Controller { data: { userTaskStatus: UserTaskStatus.Restart }, }); } + + if (v.taskStatus === TaskStatus.Failed) { + const code = record.requestWorkStep.requestWork.request.quotation.code; + const name = record.requestWorkStep.requestWork.request.quotation.workName; + + await tx.notification.create({ + data: { + title: "ใบรายการคำขอที่จัดการเกิดปัญหา / Task Failed", + detail: `ใบรายการคำขอรหัส / code : ${code} - ${name} ใบรายการคำขอเกิดปัญหา`, + receiverId: record.requestWorkStep.requestWork.request.quotation.updatedByUserId, + }, + }); + } + return await tx.task.update({ where: { id: record.id }, data: { diff --git a/src/services/schedule.ts b/src/services/schedule.ts index bd12bf8..40e8763 100644 --- a/src/services/schedule.ts +++ b/src/services/schedule.ts @@ -38,6 +38,120 @@ const jobs = [ .catch((e) => console.error("[ERR]: Update expired quotation status, FAILED.", e)); }, }), + CronJob.from({ + cronTime: "0 0 0 * * *", + runOnInit: true, + onTick: async () => { + const employeeExpireData = await prisma.employee.findMany({ + include: { + employeePassport: { + orderBy: { + expireDate: "desc", + }, + take: 1, + }, + customerBranch: { + include: { + customer: true, + }, + }, + }, + where: { + employeePassport: { + some: { + expireDate: dayjs().add(90, "day").toDate(), + }, + }, + }, + }); + + await employeeExpireData.map(async (record) => { + const fullName = `${record.namePrefix}${record.firstNameEN} ${record.lastNameEN}`; + const expireDate = `${dayjs(record.employeePassport[0].expireDate).format("DD/MM")}/${dayjs(record.employeePassport[0].expireDate).year() + 543}`; + const textDetail = `ลูกจ้างรหัส / code : ${record.code} ชื่อ : ${fullName} หนังสือเดินทางจะหมดอายุในวันที่ ${expireDate}`; + const duplicateText = await prisma.notification.findFirst({ + where: { + detail: textDetail, + }, + }); + + if (!duplicateText) { + await prisma.notification + .create({ + data: { + title: "หนังสือเดินทางลูกจ้างหมดอายุ / Employee Passport Expire", + detail: textDetail, + groupReceiver: { + create: [{ name: "sale" }, { name: "head_of_sale" }], + }, + registeredBranchId: record.customerBranch.customer.registeredBranchId, + }, + }) + .then(() => console.log("[INFO]: Create notification employee passport expired, OK.")) + .catch((e) => + console.error("[ERR]: Create notification employee passport expired, FAILED.", e), + ); + } + }); + }, + }), + CronJob.from({ + cronTime: "0 0 0 * * *", + runOnInit: true, + onTick: async () => { + const employeeVisaData = await prisma.employee.findMany({ + include: { + employeeVisa: { + orderBy: { + expireDate: "desc", + }, + take: 1, + }, + customerBranch: { + include: { + customer: true, + }, + }, + }, + where: { + employeeVisa: { + some: { + expireDate: dayjs().add(90, "day").toDate(), + }, + }, + }, + }); + + await employeeVisaData.map(async (record) => { + const fullName = `${record.namePrefix}${record.firstNameEN} ${record.lastNameEN}`; + const expireDate = `${dayjs(record.employeeVisa[0].expireDate).format("DD/MM")}/${dayjs(record.employeeVisa[0].expireDate).year() + 543}`; + const textDetail = `ลูกจ้างรหัส / code : ${record.code} ชื่อ : ${fullName} ข้อมูลการตรวจลงตราจะหมดอายุในวันที่ ${expireDate}`; + const duplicateText = await prisma.notification.findFirst({ + where: { + detail: textDetail, + }, + }); + + if (!duplicateText) { + await prisma.notification + .create({ + data: { + title: "ข้อมูลการตรวจลงตราลูกจ้างหมดอายุ / Employee Visa Expire", + detail: textDetail, + groupReceiver: { + create: [{ name: "sale" }, { name: "head_of_sale" }], + }, + registeredBranchId: record.customerBranch.customer.registeredBranchId, + }, + }) + .then(() => console.log("[INFO]: Create notification employee visa expired, OK.")) + .catch((e) => + console.error("[ERR]: Create notification employee visa expired, FAILED.", e), + ); + } + }); + }, + }), ]; export function initSchedule() {