2016-07-04 4 views
1

私は数日間、この1つに頭を向けています。私はスケジュールされた時間に私のMongoデータベースから文書を引っ張って、文書のコピーを作成する必要があります。文書作成のための定期的なイベント

例:

スケジュール:月曜日、水曜日、土曜日

ドキュメント上のすべての30週間:

{ 
    _id: 'abcxyz', 
    service: 'HVAC', 
    assignedTo: 'xyzabc', 
    details: 'Check HVAC System for problems' 
} 

私はすべての異なるタイムスケジュールとの文書の多くを持っています。 3ヶ月ごとの第1月曜日(四半期)などのものもあります。

私はlater-jsなどのものを使用しようとしましたが、後でjsは30 weeksのようなものを理解していないようです。なぜなら30 weeksは通常はcrontabで行うことではないからです。

これを完了するには、nextRunDateを生成し、今日のnextRunDateの各ドキュメントをプルすると仮定しています。今の実行日が最初の場合は、nextRunDateと計算する必要があります。Monday次回の実行日を30週ではなく水曜日にする方法今?

とにかくこの問題の援助に心から感謝します。私が上で言ったことが混乱しているなら、私はそれがGoogleカレンダースケジューラと非常に似ていると信じています。

Google Calendar Scheduler

答えて

1

多くのライブラリを見た後、私はmomentjs以降-JSが欠落している部分の両方が、これを可能にすることがわかりました。しかし、機能的には必要なツールのほとんどが提供されています。

momentjsには、特定の曜日、つまり月曜日の月曜日の最初の月曜日をターゲットにする機能が欠けていました。

later-jsには、every(30).weeks()オプションがないため、30週の予定を立てる機能がありません。

私の解決策は、first Monday of a monthがすぐに見つからず、次の定期的な日付を計算する方法を作成することでした。ここで

はそう

import moment from 'moment'; 

/** 
* Gets the first week day of type in a month 
* @param {Date} date the original date 
* @param {Number} day the day of the week we are looking for 
* @return {Date}   the new date object 
*/ 
const getFirstWeekDay = (date = new Date(), day = 0) => { 
    // set the date to the first of the month 
    date.setDate(1); 

    // Get the first weekday of the month 
    while (date.getDay() !== day) { 
    date.setDate(date.getDate() + 1); 
    } 
    // return the new date 
    return date; 
}; 

/** 
* Returns a starting point 
* @param {Date} startDate the date to start from 
* @return {moment}   a moment object 
*/ 
const start = (startDate) => moment(startDate).startOf('day'); 

/** 
* Calculates a Schedule on a weekly basis 
* @param {Date} startDate the date to start from 
* @param {Array} days  days of the week to create 
* @param {Number} weeks  number of weeks for recurrance 
* @return {Date}    the next run date 
*/ 
const weekly = ({ startDate, days, weeks }) => { 
    const today = start(startDate); 
    const index = _.indexOf(days, today.day()); 
    if (index === (days.length - 1)) { 
    return today.add(weeks, 'weeks').day(days[0]).toDate(); 
    } 

    return today.day(days[index + 1]).toDate(); 
}; 

/** 
* Calculates a Schedule on a monthly basis 
* @param {Date} startDate the date to start from 
* @param {Number} day   day of the week or month 
* @param {Number} months  number of months for recurrance 
* @param {Boolean} weekDay  starting at weekday or date 
* @return {Date}    the next run date 
*/ 
const monthly = ({ startDate, day, months, weekDay }) => { 
    const next = start(startDate).startOf('month').add(months, 'months'); 
    if (weekDay) { 
    return getFirstWeekDay(next.toDate(), day); 
    } 

    return next.date(day).toDate(); 
}; 

// register the function in a object so we can find them 
const schedules = { 
    weekly, 
    monthly, 
}; 

/** 
* Returns the next run date based on a config 
* @param {Object} config  the config for recurrence 
* @return {Date}    the next run date 
*/ 
export default (config) => schedules[config.type](config); 

// this will get the next date 2 months out on the 30th of the month 
    console.log(
    getSchedule({ 
     type: 'monthly', 
     months: 2, 
     day: 30, 
    }) 
); 
    // this will get the next date 2 months out on the first Wednesday 
    // of the month 
    console.log(
    getSchedule({ 
     type: 'monthly', 
     months: 2, 
     day: 3, 
     weekDay: true, 
    }) 
); 
    // this will get the next date 30 weeks out and if it is Monday it will 
    // instead of creating another date 30 weeks out it will create a new date 
    // for Wednesday. Once Wednesday comes it will then find the next Monday 
    // that is 30 weeks out 
    console.log(
    getSchedule({ 
     type: 'weekly', 
     weeks: 30, 
     days: [1, 3], 
     startDate: moment().add(1, 'weeks'), 
    }) 
); 
を次のようにあなたがこれを使用することができますを行うためのコードです
関連する問題