summaryrefslogtreecommitdiffstats
path: root/server/lib
diff options
context:
space:
mode:
authorUdo Walter2019-04-16 04:40:48 +0200
committerUdo Walter2019-04-16 04:40:48 +0200
commitc2b52afa3f95f77e9f71d0ca0aa0a0c6424bf7bd (patch)
tree97fa78d224f44e5b30a94bba3e1e30086e8c3b8f /server/lib
parent[server] Small bug fixes on the production server (diff)
downloadbas-c2b52afa3f95f77e9f71d0ca0aa0a0c6424bf7bd.tar.gz
bas-c2b52afa3f95f77e9f71d0ca0aa0a0c6424bf7bd.tar.xz
bas-c2b52afa3f95f77e9f71d0ca0aa0a0c6424bf7bd.zip
laterjs tests
Diffstat (limited to 'server/lib')
-rw-r--r--server/lib/eventhelper.js53
1 files changed, 53 insertions, 0 deletions
diff --git a/server/lib/eventhelper.js b/server/lib/eventhelper.js
new file mode 100644
index 0000000..0986b25
--- /dev/null
+++ b/server/lib/eventhelper.js
@@ -0,0 +1,53 @@
+/* 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()
+}