-
Notifications
You must be signed in to change notification settings - Fork 0
/
cron.js
74 lines (65 loc) · 2.1 KB
/
cron.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"use strict";
var { User } = require('./models');
var { Reminder } = require('./models')
var { Web } = require('./slackBot')
var date = new Date();
var utcDate = new Date(date.toUTCString());
var msDate = utcDate.setHours(utcDate.getHours()-7);
var usDate = new Date(utcDate);
var tomorrow = new Date(msDate + 172800000).toISOString().substring(0, 10);
var yesterday = new Date(msDate - 86400000).toISOString().substring(0, 10);
//date: d.toISOString()
console.log('hello')
Reminder.find({date: {$gt : yesterday, $lt : tomorrow}})
// Reminder.find({date: new Date().toISOString().substring(0, 10)}) // 2017-07-20
.then(function(reminders) { // return array of reminders
console.log('LIST OF REMINDERS', reminders)
console.log('DAYS', yesterday, tomorrow)
if (reminders.length === 0) {
process.exit(0);
} else {
var count = reminders.length;
reminders.forEach(function(reminder) {
User.findOne({slackId: reminder.user})
.then(function(user) {
Web.chat.postMessage(user.slackDMId,
`:bell: You have the following reminder upcoming on ${reminder.date} : ${reminder.subject}.`
)
count--;
if (count === 0 || !reminders) {
process.exit(0);
}
})
})
}
})
// function postMessage(channelId, message) {
// return new Promise(function(resolve, reject) {
// web.chat.postMessage(user.slackDMId, 'The current time is ' + new Date(), function (err) {
// if (err) {
// reject(err);
// } else {
// resolve();
// }
// })
// })
// }
//
// var postMessage2 = bluebird.promisify(web.chat.postMessage.bind(web.chat));
//
// User.find()
// .then(function(users) {
// var promises = users.map(function(user) {
// return postMessage2(user.slackDMId, 'The current itme is ' + new Date());
// })
// return Promise.all(promises);
// })
// .then(function() {
// console.log('DONE')
// process.exit(0);
// })
// .catch(function(err)) {
// console.log('ERROR', err)
// process.exit(0);
// }
//Date.toISOString().substring(0, 10) , $lt: Date.toISOString().substring(0, 10