-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterfaces.js
149 lines (137 loc) · 4.35 KB
/
interfaces.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
/**
* interfaces.js
* By Lance Mathias <[email protected]>
* Methods for interfacing with Notion and Google Calendar apis
*/
'use strict'
import { google } from 'googleapis';
async function getNotion(client, filter, sorts) {
try {
const response = await client.databases.query({
database_id: process.env.NOTION_ID,
filter: filter,
sorts: sorts
});
process.env.LAST_CHECKED = new Date();
return response.results;
}
catch (err) {
console.log(err);
}
}
async function getAuth(credentials, token) {
//TODO: combine all token files at some point LATER??
const { client_secret, client_id, redirect_uris } = credentials.installed;
const auth = new google.auth.OAuth2(client_id, client_secret, redirect_uris[0]);
auth.setCredentials(token);
return auth
}
function notionToGcal(event) {
const task = event.properties;
if ('Due' in task && 'date' in task.Due && 'start' in task.Due.date) {
try {
//get rid of time, all events are all-day
const date = task.Due.date.start.substring(0, 10);
const event = {
'summary': task.Name.title[0].text.content,
'start': {
'date': date
},
'end': {
'date': date
}
}
if ('Status' in task) {
event['colorId'] = colors[task.Status.select.color];
}
return event;
}
catch (err) {
console.log(err);
}
}
}
function blockToGcal(block) {
//Colors for GCal events
const colors = { 'red': 4, 'yellow': 5, 'green': 2, 'overdue': 11 }
try {
const event = {
'summary': block.task.name,
'description': block.task.id,
'start': {
'dateTime': block.start
},
'end': {
'dateTime': block.end
},
eventType: 'focusTime',
}
if (block.end > block.task.due) { event.colorId = colors.overdue }
return event
}
catch (err) { console.log(err) }
}
async function uploadEvent(calendar, auth, block) {
//Colors for GCal events
const colors = { 'red': 4, 'yellow': 5, 'green': 2, 'overdue': 11 }
try {
const event = {
'summary': block.task.name,
'description': block.task.id,
'start': {
'dateTime': block.start
},
'end': {
'dateTime': block.end
},
eventType: 'focusTime',
}
if (block.end > block.task.due) { event.colorId = colors.overdue }
if(block.task == 'Free' && block.gc_id) {
const response = await calendar.events.delete({
auth: auth, eventId: block.gc_id,
calendarId: process.env.TASK_CALENDAR_ID
})
}
else if (block.gc_id) {
const response = await calendar.events.update({
auth: auth,
eventId: block.gc_id,
calendarId: process.env.TASK_CALENDAR_ID,
resource: event
})
return response
}
else {
const response = await calendar.events.insert({
auth: auth,
calendarId: process.env.TASK_CALENDAR_ID,
resource: event
})
block.gc_id = response.eventId
return response
}
}
catch (err) {
console.log(err);
}
}
//Get list of potentially conflicting calendar events
async function getConflicts(calendar, tasks) {
try {
let filtered = tasks.sort((a, b) => a.due - b.due);
const start = new Date();
const end = filtered[filtered.length - 1].due;
const res = await calendar.events.list({
calendarId: 'primary', //which calendar should we check???
timeMin: start,
timeMax: end,
singleEvents: true,
orderBy: 'startTime'
})
return res.data.items.filter(event => 'dateTime' in event.start)
.map(event => ({ start: event.start.dateTime, end: event.end.dateTime }));
}
catch (err) { return console.log(err); }
}
export { getNotion, getAuth, notionToGcal, blockToGcal, uploadEvent, getConflicts}