feat: update quotation status on expired

This commit is contained in:
Methapon Metanipat 2024-11-07 13:44:49 +07:00
parent 4d2dadbc05
commit 18fb64b9cc
5 changed files with 52 additions and 0 deletions

View file

@ -7,6 +7,7 @@ import { RegisterRoutes } from "./routes";
import { initThailandAreaDatabase } from "./utils/thailand-area";
import { initFirstAdmin } from "./utils/database";
import { apiReference } from "@scalar/express-api-reference";
import { initSchedule } from "./services/schedule";
const APP_HOST = process.env.APP_HOST || "0.0.0.0";
const APP_PORT = +(process.env.APP_PORT || 3000);
@ -17,6 +18,8 @@ const APP_PORT = +(process.env.APP_PORT || 3000);
await initThailandAreaDatabase();
await initFirstAdmin();
initSchedule();
const originalSend = app.response.json;
app.response.json = function (body: unknown) {

25
src/services/schedule.ts Normal file
View file

@ -0,0 +1,25 @@
import { CronJob } from "cron";
import prisma from "../db";
const jobs = [
CronJob.from({
cronTime: "0 0 0 * * *",
runOnInit: true,
onTick: async () => {
await prisma.quotation
.updateMany({
where: {
dueDate: { lte: new Date() },
},
data: { quotationStatus: "Expired" },
})
.then(() => console.log("[INFO]: Update expired quotation status, OK."))
.catch((e) => console.error(e));
},
}),
];
export function initSchedule() {
for (const job of jobs) job.start();
}