import dayjs from "dayjs"; import { CronJob } from "cron"; import prisma from "../db"; import { Prisma } from "@prisma/client"; const jobs = [ CronJob.from({ cronTime: "0 0 0 * * *", runOnInit: true, onTick: async () => { const current = new Date(); const date = current.getDate(); const month = current.getMonth(); const year = current.getFullYear(); await prisma.quotation .updateMany({ where: { quotationStatus: "Issued", dueDate: { lte: new Date(year, date - 1 === 0 ? month - 1 : month, date - 1) }, }, data: { quotationStatus: "Expired" }, }) .then(() => console.log("[INFO]: Update expired quotation status, OK.")) .catch((e) => console.error("[ERR]: Update expired quotation status, FAILED.", e)); }, }), CronJob.from({ cronTime: "0 0 0 * * *", runOnInit: true, onTick: async () => { await prisma.notification .deleteMany({ where: { createdAt: { lte: dayjs().subtract(1, "month").toDate() } }, }) .then(() => console.log("[INFO]: Delete expired notification, OK.")) .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, }, }, quotationWorker: { include: { quotation: true, }, orderBy: { createdAt: "desc", }, take: 1, }, }, where: { employeePassport: { some: { expireDate: dayjs().add(90, "day").toDate(), }, }, }, }); await Promise.all( 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, }, }); const dataNotification: Prisma.NotificationCreateArgs["data"] = { title: "หนังสือเดินทางลูกจ้างหมดอายุ / Employee Passport Expire", detail: textDetail, }; if (record.quotationWorker && record.quotationWorker.length > 0) { dataNotification.receiverId = record.quotationWorker[0].quotation.updatedByUserId; dataNotification.registeredBranchId = record.quotationWorker[0].quotation.registeredBranchId; } else { (dataNotification.groupReceiver = { create: [{ name: "sale" }, { name: "head_of_sale" }], }), (dataNotification.registeredBranchId = record.customerBranch.customer.registeredBranchId); } if (!duplicateText) { await prisma.notification .create({ data: dataNotification, }) .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, }, }, quotationWorker: { include: { quotation: true, }, orderBy: { createdAt: "desc", }, take: 1, }, }, where: { employeeVisa: { some: { expireDate: dayjs().add(90, "day").toDate(), }, }, }, }); await Promise.all( 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, }, }); const dataNotification: Prisma.NotificationCreateArgs["data"] = { title: "ข้อมูลการตรวจลงตราลูกจ้างหมดอายุ / Employee Visa Expire", detail: textDetail, }; if (record.quotationWorker && record.quotationWorker.length > 0) { dataNotification.receiverId = record.quotationWorker[0].quotation.updatedByUserId; dataNotification.registeredBranchId = record.quotationWorker[0].quotation.registeredBranchId; } else { (dataNotification.groupReceiver = { create: [{ name: "sale" }, { name: "head_of_sale" }], }), (dataNotification.registeredBranchId = record.customerBranch.customer.registeredBranchId); } if (!duplicateText) { await prisma.notification .create({ data: dataNotification, }) .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() { for (const job of jobs) job.start(); }