-
Notifications
You must be signed in to change notification settings - Fork 0
/
calendar.js
55 lines (43 loc) · 1.71 KB
/
calendar.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
require('dotenv').config();
const calendarAPI = require('./calendarAPI');
const excel = require('./excel');
const { findLastRecordedSession, hasUniqueStartTime, findOnRoster } = require('./logic');
Promise.all([calendarAPI(), excel.read()])
.then(([calendarData, { logData, rosterData }]) => {
let sessionsToWrite = [];
calendarData.forEach(newSessionOnCalendar => {
let newSession = [];
if (hasUniqueStartTime(newSessionOnCalendar, logData)) {
let lastRecordedSession = findLastRecordedSession(newSessionOnCalendar, logData);
if (lastRecordedSession) {
newSession = lastRecordedSession.map((column, index) => {
if (index === 5) return new Date(newSessionOnCalendar.startTime);
else return column;
})
newSession = newSession.slice(0, 7);
sessionsToWrite.push(newSession);
}
else {
let rosterEntry = findOnRoster(newSessionOnCalendar, rosterData);
if (newSessionOnCalendar.studentEmail === 'No Email' || !rosterEntry) {
newSession[5] = new Date(newSessionOnCalendar.startTime);
newSession.fill('*', 0, 5);
sessionsToWrite.push(newSession);
}
else if (rosterEntry) {
newSession = rosterEntry;
newSession[5] = new Date(newSessionOnCalendar.startTime);
sessionsToWrite.push(newSession);
}
else {
console.log('The following session was not imported ' + newSessionOnCalendar.startTime);
}
}
}
});
if (sessionsToWrite.length < 1) return console.log('Spreadsheet Already Up To Date!');
excel.write(sessionsToWrite)
.then(() => console.log('Spreadsheet Updated Successfully!'))
.catch(err => { throw new Error('Spreadsheet not updated!\n' + err)})
})
.catch(err => console.log(err));