-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
72 lines (55 loc) · 2.52 KB
/
index.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
if (process.env.NODE_ENV === "production") {
require('dotenv').config({ path: '/home/graham/notion-schoology-integration/.env' });
} else {
require('dotenv').config({ path: './.env' });
}
const util = require('./util.js');
const schoology = require('./schoology.js');
const notion = require('./notion.js');
const { SchoologyEvent } = require('./classes/SchoologyEvent.js');
const { NotionPage } = require('./classes/NotionPage.js');
const { Entry } = require('./classes/Entry.js');
// TODO: Add error handling
// TODO: Less loops, faster code
// TODO: Make sgyEvent and notionEntry classes?
// Make Notion and Schoology classes? extend these for everything else?
(async () => {
const scrapeStartDate = util.getISODate();
// get events from schoology & notion (7 day range default)
let sgyEvents = await schoology.getUserEvents(process.env.SCHOOLOGY_USER_ID, scrapeStartDate);
sgyEvents = sgyEvents.filter(event => {
// workaround for the weird date range issue with the schoology API (could be an issue with my dates/timezones)
return event.start >= scrapeStartDate && event.start <= util.addDaysToDate(scrapeStartDate, 7);
}).map((event) => {
return new SchoologyEvent(event);
})
let notionPages = await notion.getEntries(scrapeStartDate);
notionPages = notionPages.map((entry) => {
return new NotionPage(entry);
})
// get new and duplicate entries
const duplicates = Entry.findDuplicates(sgyEvents, notionPages);
const newEntries = await Entry.findNewEntries(sgyEvents, duplicates);
// handle duplicates
if (duplicates.length > 0) {
console.info(`${util.logDatetime()} Duplicate entries found, checking for entries to update...`);
const entriesToUpdate = Entry.findEntriesToUpdate(duplicates);
// update duplicates that were changed
if (entriesToUpdate.length > 0) {
console.info(`${util.logDatetime()} Entries to update found. Updating...`);
Entry.update(entriesToUpdate);
} else {
console.info(`${util.logDatetime()} No entries to update found.`);
}
} else {
console.info(`${util.logDatetime()} No duplicate entries found.`);
}
// check if any duplicate needs to be updated
// add new entries
if (newEntries.length > 0) {
console.info(`${util.logDatetime()} New entries found, creating...`);
Entry.createEntries(newEntries);
} else {
console.info(`${util.logDatetime()} No new entries found in schoology`);
}
})()