I need to update columns in my SQL database once a day automatically.
My backend logic works with deadlines (deadlineTime which is the due date and deadline which is the status of the deadline, "on time" and "out of time" for example).
For example when a task is created it is assigned a deadlineTime of 4 days for example, where if today is the 14th, the deadlineTime is set so that the due date falls on the 18th. In that range of days the deadline column will be "on time", but when the stipulated date of the deadlineTime passes, it will no longer be "on time", it should go to "out of time". And I need it to change automatically as soon as the system detects that today's date is already greater than the date of the deadlineTime.
Currently I manage this with a script from the node-cron library that I make it run once a day at 12 o'clock at night, that reads all the tasks in deadline="on time" and sets the date of the moment and the deadlineTime, if the date of the moment is less, it continues, but if it is greater, it changes the status of the deadline, saves the task and continues analyzing others.
Is this procedure OK? Or are there more optimized or best practice ways to do it?
This is the my script actually, it works but I don't know if this is the best option to my backend.
cron.schedule("* * * * * *", async () => {
const procedures = await procedureHistoryRepository.find({
where: {
deadline: {
deadline: "on time"
}
},
relations: {
deadline: true,
procedure: true
},
select: {
id: true,
procedure: {
title: true,
description: true
},
deadline: {
deadline: true
},
deadlineTime: true
}
});
const proceduresInPause = await procedureHistoryRepository.findOne({
where: {
deadline: {
deadline: "paused for requirement"
}
},
relations: {
deadline: true,
procedure: true
},
select: {
id: true,
procedure: {
title: true,
description: true
},
deadline: {
deadline: true
},
deadlineTime: true
}
});
if (!proceduresInPause) {
console.log("No procedures are paused due to requirements");
}
const deadlineFinishedBad = await deadlineRepository.findOne({
where: {
deadline: "out of time"
},
select: {
id: true,
deadline: true
}
});
if (!deadlineFinishedBad) {
return "Doesn't exist deadline";
}
if (procedures.length === 0) {
return "Doesn't exist procedures yet";
}
for (let i = 0; i < procedures.length; i++) {
const deadlineTime = differenceInBusinessDays(procedures[i].deadlineTime, new Date());
if (deadlineTime > 0) {
console.log(`Procedure ${procedures[i].id} on time`);
}
else {
procedures[i].deadline = deadlineFinishedBad;
procedures[i].days_after_deadline = new Date();
await procedureHistoryRepository.save(procedures[i]);
}
}
});
Edit: Finally, I decided to calculate the deadline in the HTTP Request, like this (deadline don't save on the DB, only shows in JSON response)
if (historyItem.status.status === "condition") {
historyItem.deadline = "string 1"
} else if (deadlineDays <= 0) {
historyItem.deadline = "string 2"
}
With this code, I follow the rules of normalization databases