jws-backend/src/services/schedule.ts
Methapon2001 c7fae98516
All checks were successful
Spell Check / Spell Check with Typos (push) Successful in 7s
feat: clear expired notification date
2025-03-05 17:22:38 +07:00

45 lines
1.3 KiB
TypeScript

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();
}