-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
77 lines (66 loc) · 1.83 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
73
74
75
76
77
/**
* index.js
* By Lance Mathias <[email protected]>
* Creates Google calendar time blocks from Notion. For more info, read README
*/
'use strict'
import {} from 'dotenv/config'
import { Client } from '@notionhq/client';
import fs from 'fs';
import { google } from 'googleapis';
import { Task, Record, Block, assignBlocks } from './scheduler.js';
import { getNotion, getAuth, uploadEvent, getConflicts} from './interfaces.js'
//Pull locally cached blocks record
let record
try {
record = new Record(JSON.parse(fs.readFileSync('./record.json')))
}
catch (err) {
console.log(err)
record = new Record()
}
//Sorts and filters for relevant Notion tasks
const filters = {
and: [
{
property: 'Last Edited',
date: {
after: process.env.LAST_CHECKED
}
},
{
property: 'Status',
select: {
does_not_equal: 'Done'
}
}
]
};
const sorts = [
{
property: 'Due',
direction: 'descending'
}
];
async function main() {
const notion = await getNotion(new Client({ auth: process.env.NOTION_KEY }), filters, sorts)
const tasks = notion.map(task => new Task(task))
//Initialize GCal client
const auth = await getAuth(JSON.parse(process.env.GCAL_CREDENTIALS), JSON.parse(process.env.GCAL_TOKEN));
const calendar = google.calendar({ version: 'v3', auth });
const conflicts = await getConflicts(calendar, tasks)
const changedBlocks = assignBlocks(tasks, conflicts, record)
record.blocks.forEach(block => {
uploadEvent(calendar, auth, block)
})
//Cache updated record
try {
if(record) {
fs.writeFileSync('./record.json', JSON.stringify(record, null, 4))
}
}
catch (err) {
console.log(err)
}
}
await main()