Later = Npm.require('later');

defaultFrequency = 7; // once a week

getSchedule = function (parser) {
  var frequency = getSetting('newsletterFrequency', defaultFrequency);
  switch (frequency) {
    case 1: // every day
    // sched = {schedules: [{dw: [1,2,3,4,5,6,0]}]};
    return parser.recur().on(1,2,3,4,5,6,0).dayOfWeek();

    case 2: // Mondays, Wednesdays, Fridays
    // sched = {schedules: [{dw: [2,4,6]}]};
    return parser.recur().on(2,4,6).dayOfWeek();

    case 3: // Mondays, Thursdays
    // sched = {schedules: [{dw: [2,5]}]};
    return parser.recur().on(2,5).dayOfWeek();

    case 7: // Once a week (Mondays)
    // sched = {schedules: [{dw: [2]}]};
    return parser.recur().on(2).dayOfWeek();

    default: // Don't send
    return null;
  }  
}

SyncedCron.getNext = function (jobName) {
  var scheduledAt;
  try {
    _.some(this._entries, function(entry) {
      if(entry.name === jobName){
        var schedule = entry.schedule(Later.parse);
        scheduledAt = Later.schedule(schedule).next(1);
        return true;
      }
    });
  } 
  catch (error) {
    console.log(error)
    scheduledAt = 'No job scheduled';
  }
  return scheduledAt;
}

getNextCampaignSchedule = function () {
  return SyncedCron.getNext('Schedule newsletter');
}

SyncedCron.add({
  name: 'Schedule newsletter',
  schedule: function(parser) {
    // parser is a later.parse object
    // var sched;
    return getSchedule(parser)
    
  }, 
  job: function() {
    scheduleNextCampaign();
  }
});

Meteor.startup(function() {
  if(getSetting('newsletterFrequency', defaultFrequency) != 0) {
    SyncedCron.start();
  };
});

Meteor.methods({
  getNextJob: function (jobName) {
    var nextJob = getNextCampaignSchedule();
    console.log(nextJob);
    return nextJob;
  }
});
@@ -782,533 +782,32 @@
 ron.
-getNext = function (jobName) %7B%0A  var scheduledAt;%0A  try %7B%0A    _.some(this._entries, function(entry) %7B%0A      if(entry.name === jobName)%7B%0A        var schedule = entry.schedule(Later.parse);%0A        scheduledAt = Later.schedule(schedule).next(1);%0A        return true;%0A      %7D%0A    %7D);%0A  %7D %0A  catch (error) %7B%0A    console.log(error)%0A    scheduledAt = 'No job scheduled';%0A  %7D%0A  return scheduledAt;%0A%7D%0A%0AgetNextCampaignSchedule = function () %7B%0A  return SyncedCron.getNext('Schedule newsletter');%0A%7D%0A%0ASyncedCron.add(%7B%0A  name: 'Schedule n
+add(%7B%0A  name: 'scheduleN
 ewsl
@@ -936,13 +936,8 @@
 er)%0A
-    %0A
   %7D,
@@ -1167,15 +1167,8 @@
 on (
-jobName
 ) %7B%0A
@@ -1189,32 +1189,59 @@
 b = 
-getNextCampaignSchedule(
+SyncedCron.nextScheduledAtDate('scheduleNewsletter'
 );%0A 

clean up newsletter cron code