import dayjs from "dayjs"; import { CronJob } from "cron"; import prisma from "../db"; 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)); }, }), ]; export function initSchedule() { for (const job of jobs) job.start(); }