-
Notifications
You must be signed in to change notification settings - Fork 2
/
auditevents.js
305 lines (241 loc) · 8.51 KB
/
auditevents.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// 'use strict';
/*
* --------------------------------------------------------
* Calendar2Voice
*
* Purpose: A flash briefing that given an ICS url to a public
* calendar, will find any events for today and return them
* to be read. If there are no events for today, it will
* return the next day that has events.
*
* see config.js for the data for this.
* --------------------------------------------------------
*/
const ics2json = require('./icsToJson');
const cfg = require('config');
const Calendar = require('containers/calendar');
const Event = require('containers/event');
// eslint-disable-next-line no-unused-vars
const util = require('util');
// this module is used internally by the moment timeszone
// eslint-disable-next-line no-unused-vars
const { DateTime } = require("luxon");
var moment = require('moment-timezone');
// text we build up of each event to later return to Alexa
var responsetext = "";
var eventresponse = "";
var responseJSON = "";
// var eventarray = [];
/*
* --------------------------------------------------------
* function: createReturn
* purpose: creates standard JSON return to alexa
* --------------------------------------------------------
*/
// eslint-disable-next-line no-unused-vars
function createReturn(stscode,responsetext, calobj) {
let response = "";
try {
response = {
statusCode: stscode,
body: responsetext
}
}
catch (e) {
console.log("Error createing response return!",e);
}
return response;
}
/*
* --------------------------------------------------------
* function: select_todays_events
* purpose: convert the iCS JSON for the calendar into a series
* of standard event objects and for the ones that are today,
* push to a collecton that can be later sorted and reported
* --------------------------------------------------------
*/
function select_audited_events(jdata, thiscal) {
let arSelectedEvents = [];
let arEvents = [];
let stdevent = {};
// console.log("Selecting events:" + JSON.stringify(jdata));
try {
// look at each event. Pull out ones for today AND save the next day that events
// occur just incase we need it since we're going through them all anyway
jdata.forEach(function (oneevent) {
if ( oneevent && oneevent.startDate && oneevent.summary ) {
// create our standard event
stdevent = new Event()
stdevent.load(thiscal,oneevent);
if ( stdevent.isvalid ) {
stdevent.msg = "";
if ( stdevent.eventStart.hour() == 0 && stdevent.eventStart.minute() == 0) {
var Midev = new Event();
Midev.load(thiscal,oneevent);
Midev.msg = ( Midev.isalldayevent) ? "All-day event" : "Midnight event";
// console.log("$$$ all or midnight:" + JSON.stringify(Midev) );
arSelectedEvents.push(Midev);
}
if ( stdevent.ismultidayevent ) {
var mulEv = new Event();
mulEv.load(thiscal,oneevent);
mulEv.msg = "Muilt-day event";
// console.log("$$$ Multil:" + JSON.stringify(mulEv) );
arSelectedEvents.push(mulEv);
}
let data = arEvents.find( ( o ) => o.summary === stdevent.summary && o.eventStartString == stdevent.eventStartString);
if ( data) {
var dupEv = new Event();
dupEv.load(thiscal,oneevent);
dupEv.msg = "Duplicate event";
arSelectedEvents.push(dupEv);
} else {
arEvents.push(stdevent);
}
} else { // not a valid event for us but we don't tell user that
console.log("event entry NOT valid, skipping");
console.log(util.inspect(stdevent, {showHidden: false, depth: null}));
}
}
}); // foreach event
}
catch (e) {
console.log("Error setting object:",e)
}
return arSelectedEvents;
}
/*
* --------------------------------------------------------
* function: process_events.
* format the events that were selected for responding back
* --------------------------------------------------------
*/
// eslint-disable-next-line no-unused-vars
function process_events(eventarray, thiscal) {
let retmessage = "";
console.log("Processing selected events");
if ( eventarray.length > 0) {
// remember sort mutates the object
eventarray.sort( function(a,b) {
if ( a.sortkey(0) > b.sortkey(0) ) return 1;
if ( b.sortkey(0) > a.sortkey(0) ) return -1;
return 0;
});
// we're building basic HTML response rows in a table
eventarray.forEach(function (oneevt) {
console.log(util.inspect(oneevt, {showHidden: false, depth: null}));
// retmessage += "<tr><td>" + oneevt.msg + "</td><td>" + oneevt.summary + "</td><td>" + oneevt.eventStart.format('hh:mm A') + "</td></tr>";
retmessage += "'" + oneevt.msg + "','" + oneevt.summary + "','" + oneevt.eventStartString + "' \n";
});
}
return retmessage;
}
/*
* --------------------------------------------------------
* function: init
* --------------------------------------------------------
*/
function init(event) {
const evtpointer = ( event && event.queryStringParameters ) ? event.queryStringParameters : event;
var retcal = new Calendar();
responsetext = "";
eventresponse = "";
responseJSON = "";
// we can get fordate and forcalendar off the passed API params
if ( evtpointer.fordate && evtpointer.fordate > '' && moment(evtpointer.fordate,'YYYY-MM-DD').isValid() ) {
retcal.invokedDate = evtpointer.fordate;
}
if ( evtpointer.forcalendar && evtpointer.forcalendar > '' ) {
retcal.selectedcalendar = evtpointer.forcalendar.toLowerCase();
}
try {
console.log("Searching for:" + retcal.selectedcalendar);
let data = cfg.calendars.find( ( o ) => o.calendarName === retcal.selectedcalendar);
retcal.load(data);
}
catch (e) {
console.log("error loading calendar",e);
}
console.log("returning:" + JSON.stringify(retcal));
return retcal;
}
function selectevents(caldata, thiscal) {
let eventarray = [];
try {
const jdata = ics2json.icsToJson(caldata);
eventarray = select_audited_events(jdata, thiscal);
}
catch (e) {
console.log("error converting data",e);
eventresponse = "";
}
console.log("selectevents returning:" + JSON.stringify(eventarray));
return eventarray;
}
function getevents(thiscal) {
return new Promise((resolve) => {
console.log("getevents for:" + thiscal.calendarname);
let evarray = [];
let evarraynext = [];
let evarrayret = [];
if (thiscal.calendarname == "startwheelhr" ) {
var currentDate = moment().startOf('month');
var futureMonth = moment(currentDate).add(1, 'M');
let nexturl = "https://startwheel.org/events/?ical=1&tribe_display=month&tribe-bar-date=" + futureMonth.format('YYYY') + "-" +futureMonth.format('MM')
console.log("Nextmonth url = " + nexturl);
return thiscal.getICSdata().then( (caldata) => {
try {
console.log("ICS curmonth returned:" + JSON.stringify(caldata));
evarray = selectevents(caldata,thiscal);
return thiscal.getICSdata(nexturl).then( (caldatanext) => {
console.log("ICS nextmonth returned:" + JSON.stringify(caldata));
evarraynext = selectevents(caldatanext,thiscal);
evarrayret = evarray.concat(evarraynext)
resolve(evarrayret);
});
}
catch (e) {
console.log("error converting data",e);
eventresponse = "";
resolve(evarrayret);
}
});
} else {
return thiscal.getICSdata().then( (caldata) => {
console.log("ICS curmonth returned:" + JSON.stringify(caldata));
let evarrayret = selectevents(caldata,thiscal);
resolve(evarrayret);
});
}
});
}
/*
* --------------------------------------------------------
* function: main
* --------------------------------------------------------
*/
exports.auditevents = function(event, context, callback) {
// log for testing sake
console.log(util.inspect(event, {showHidden: false, depth: null}));
var thiscal = init(event);
console.log("cali is:" + JSON.stringify(thiscal));
if ( thiscal.isvalid ) {
console.log("getting ICS for:" + thiscal.calendarname);
getevents(thiscal).then( (eventarray) => {
try {
eventresponse = process_events(eventarray, thiscal);
}
catch (e) {
console.log("error converting data",e);
eventresponse = "";
}
//responsetext = "<html><head></head><body><h1>Events that failed audit</h1><table><tr><th>Error Message</th><th>Event Summary</th><th>Event Startdate</th></tr>" + eventresponse + "</table></body></html>";
responsetext = eventresponse + "\n";
responseJSON = createReturn(200,responsetext, thiscal);
console.log(util.inspect(responseJSON, {showHidden: false, depth: null}));
return callback(null, responseJSON);
});
} else {
console.log("Calendar was not valid");
}
};