summaryrefslogblamecommitdiffstats
path: root/server/lib/eventhelper.js
blob: 0986b25b7141680224772c8da010acd949090d4d (plain) (tree)




















































                                                                                                                         
/* global __appdir */
var path = require('path')
var db = require(path.join(__appdir, 'lib', 'sequelize'))
const later = require('later')

function isActive (eventData) {
  const startDate = new Date(eventData.startDate * 1000)
  const endDate = new Date(eventData.endDate * 1000)
  let now = new Date()
  if (eventData.repetitive) {
    if (startDate < now && now < endDate) return false
    return later.schedule(getTimeSpans(eventData)).isValid(now)
  } else {
    const startTime = eventData.startTime.split(':')
    startDate.setHours(startTime[0])
    startDate.setMinutes(startTime[1])
    const endTime = eventData.endTime.split(':')
    endDate.setHours(endTime[0])
    endDate.setMinutes(endTime[1])
    return startDate < now && now < endDate
  }
}

function getTimeSpans (eventData) {
  const schedule = later.parse.recur()
  schedule.after(eventData.startTime).time()
  const startTime = eventData.startTime.split(':').map(x => parseInt(x))
  const endTime = eventData.endTime.split(':').map(x => parseInt(x))
  if (startTime[0] > endTime[0] || (startTime[0] === endTime[0] && startTime[1] > endTime[1])) {
    _setUpSchedule(eventData, schedule)
    schedule.and()
  }
  schedule.before(eventData.endTime).time()
  _setUpSchedule(eventData, schedule)
  return schedule
}

function getStartTimes (eventData) {
  const schedule = later.parse.recur()
  schedule.on(eventData.startTime).time()
  _setUpSchedule(eventData, schedule)
  return schedule
}

function _setUpSchedule (eventData, schedule) {
  schedule.on(eventData.monthMap.reduce((arr, v, i) => { if (v) arr.push(i + 1); return arr }, [])).month()
  schedule.on(eventData.dayMap.reduce((arr, v, i) => { if (v) arr.push(((i + 1) % 7) + 1); return arr }, [])).dayOfWeek()
  schedule.every(parseInt(eventData.interval))
  const type = eventData.intervalType
  if (type === 'month') schedule.month()
  else if (type === 'week') schedule.weekOfYear()
  else schedule.dayOfYear()
}