0

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.

Ripul
  • 41
  • 1
  • 2
  • What do you want exactly? – Mohammad Yaser Ahmadi Feb 02 '21 at 11:48
  • I want to create a cronjob for each user which looks at the database, gets all the actions that need to be added to their current day list but only run the operations if there are any actions. The way I'm doing it right now would end up creating a whole bunch of cronjobs that if the user deletes all routines would run for no reason and I want to avoid that. – Ripul Feb 02 '21 at 19:57

0 Answers0