I am a new developer trying to build a todo app in nodejs with graphql and mongodb. I want to schedule some tasks which would come under routine things that the user would want added to their daily list of tasks. I decided to use node-cron for this purpose but I a little confused. This is what I'm doing right now:
const routineCron = cron.schedule(
cronString,
async () => {
const actions = await model.findOne({ user: user }, "actions");
actions.map(async (action) => {
action.routineType = null;
action.currentList = calendar;
const savedAction = new Action(action);
await savedAction.save();
});
},
{ timezone: timeZone }
);
The cronString is derived from a switch case checking for which type of routine it is:
switch (routineType) {
case "Daily":
model = Daily;
cronString = "0 0 * * *";
break;
case "Weekly":
model = Weekly;
cronString = "0 0 * * 0";
break;
case "Monthly":
model = Monthly;
cronString = "0 0 1 * *";
break;
case "Quarterly":
model = Quarterly;
cronString = "0 0 1 */3 *";
break;
case "Yearly":
model = Yearly;
cronString = "0 0 0 1 *";
break;
default:
throw new Error("Routine Type incorrect");
}
Now the problem is I want to find the actions from the model(defined in switch case) and add them to the user's current day list named calendar which would create a cronjob for each user. But if a user deletes the routines then there would be no actions in his model and the cron job would be unnecessary. What should I do in this situation. Also I assigned the cronjob to a const but when the next user makes a cronjob would it work because the same const name is assigned? Any help is appreciated.