-
Notifications
You must be signed in to change notification settings - Fork 2
/
functions.js
237 lines (204 loc) · 5.89 KB
/
functions.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
var {User, Reminder, Meeting} = require('./models');
var axios = require('axios');
var moment = require('moment');
var _ = require('underscore');
moment().format();
var google = require('googleapis');
var OAuth2 = google.auth.OAuth2;
function getGoogleAuth() {
return new OAuth2(
process.env.GOOGLE_CLIENT_ID,
process.env.GOOGLE_CLIENT_SECRET,
process.env.DOMAIN + '/connect/callback'
);
}
var checkAccessToken = ( user ) => {
var curTime = Date.now();
if ( curTime > user.googleAccount.expiry_date ) {
console.log("EXPIRED ACCESS TOKEN");
var googleAuth = getGoogleAuth();
googleAuth.setCredentials(user.googleAccount);
return googleAuth.refreshAccessToken(function(err, tokens) {
console.log("REFRESH ACTOKEN PROCESS, TOKENS:", tokens);
user.googleAccount = Object.assign({}, user.googleAccount, tokens);
console.log("CHECK GOOGLEACCOUNT in checkAccessToken (functions.js)", user.googleAccount);
return user.save(function(err,user){
return user;
})
})
}
else {
console.log('token still good of the guy using the bot.');
return user;
}
}
var checkThis = (attendee) => {
console.log("entered check this");
return User.findOne({slack_ID:attendee.slack_ID})
.then(function(user){
console.log(user.slack_Username);
var curTime = Date.now();
if ( curTime > user.googleAccount.expiry_date ) {
console.log("EXPIRED ACCESS TOKEN");
var googleAuth = getGoogleAuth();
googleAuth.setCredentials(user.googleAccount);
googleAuth.refreshAccessToken(function(err, tokens) {
console.log("REFRESH ACTOKEN PROCESS, TOKENS:", tokens);
user.googleAccount = Object.assign({}, user.googleAccount, tokens);
console.log("CHECK GOOGLEACCOUNT in checkAccessToken (functions.js)", user.googleAccount);
user.save(function(user) {
return user;
})
})
}
else {
console.log('token still good');
return user;
}
})
.then(function(user){
console.log("setting access token of attendee from user");
attendee.access_token = user.googleAccount.access_token;
console.log("from checkThis passing this attendee");
return attendee;
})
.catch(function(error){
console.log("error", error);
})
}
var clearState = function(user){
user.pendingState = {
subject: "",
date: "",
time: "",
invitees: [],
inviteesBySlackid: [],
};
user.active = 0 ;
user.save(function(err){
if(err)console.log(err);
});
}
var taskPath = function (user) {
console.log("entered taskPath");
if(user){
var state = user.pendingState;
var new_event = {
"end": {
"date": state.date
},
"start": {
"date": state.date
},
"description": "Chief Keef is a fucking legend",
"summary": state.subject
}
return axios.post(`https://www.googleapis.com/calendar/v3/calendars/primary/events?access_token=${user.googleAccount.access_token}`, new_event)
.then(function(response){
var reminder = new Reminder({
subject: state.subject,
day: state.date,
googCalID: user.googleAccount.profile_ID,
reqID: user.slack_ID
})
reminder.save(function(err) {
if(err) {
console.log('there is an error', err);
} else {
console.log('saved reminder in mongo');
}
});
if(response.status === 200){
return true;
}else{
return false;
}
})
.catch(function(err){
console.log(err);
})
}
}
function findAttendees(state){
return User.find({})
.then(function(users){
var attendees = [];
users.forEach(function(item){
var id = item.slack_ID;
if(state.inviteesBySlackid.indexOf(id) !== -1){
attendees.push({"email": item.googleAccount.email});
}
})
return attendees;
})
}
function calculateEndTimeString(state){
//set up for default 30 minute meetings until api.ai is trained better
var meetingLength = 60;
var end = state.date + 'T' + state.time;
var endMoment = moment(end);
endMoment.add(meetingLength, 'minute');
return endMoment;
}
function calculateStartTimeString(state){
var start = state.date + 'T' + state.time;
var startMoment = moment(start);
return startMoment;
}
var meetingPath = function(user){
var state = user.pendingState;
var start = calculateStartTimeString(state);
var end = calculateEndTimeString(state);
var subject = state.subject || ' ';
if(user){
return findAttendees(state)
.then(function(attendees){
var new_event = {
"end": {
"dateTime": end,
"timeZone": "America/Los_Angeles"
},
"start": {
"dateTime": start,
"timeZone": "America/Los_Angeles"
},
"summary": subject,
"attendees": attendees
}
return axios.post(`https://www.googleapis.com/calendar/v3/calendars/primary/events?access_token=${user.googleAccount.access_token}`, new_event)
.then(function(response){
var reminder = new Reminder({
subject: state.subject,
day: state.date,
googCalID: user.googleAccount.profile_ID,
reqID: user.slack_ID
})
reminder.save(function(err) {
if(err) {
console.log('there is an error', err);
} else {
console.log('saved reminder in mongo');
}
});
if(response.status === 200){
console.log("this is the response status", response.status);
return true;
}
else return false;
})
.catch(function(err){
console.log(err);
})
})
.catch( function(error) {
console.log(error);
})
}
}
module.exports = {
clearState,
taskPath,
meetingPath,
checkAccessToken,
checkThis
}