jws-backend/src/services/schedule.ts

46 lines
1.3 KiB
TypeScript
Raw Normal View History

2025-03-05 17:22:38 +07:00
import dayjs from "dayjs";
import { CronJob } from "cron";
import prisma from "../db";
const jobs = [
CronJob.from({
cronTime: "0 0 0 * * *",
runOnInit: true,
onTick: async () => {
2024-11-07 15:16:35 +07:00
const current = new Date();
const date = current.getDate();
const month = current.getMonth();
const year = current.getFullYear();
await prisma.quotation
.updateMany({
where: {
quotationStatus: "Issued",
2024-11-07 15:16:35 +07:00
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."))
2024-11-07 15:17:58 +07:00
.catch((e) => console.error("[ERR]: Update expired quotation status, FAILED.", e));
},
}),
2025-03-05 17:22:38 +07:00
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();
}