-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.js
204 lines (189 loc) · 5.74 KB
/
main.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
/**
* npm i nodemon -g
* npm i @notionhq/client
* npm i dotenv
* npm i --save lodash
*/
const { Client, APIErrorCode } = require("@notionhq/client")
const { CanvasHelper } = require("./canvashelper")
const { NotionHelper } = require("./notionhelper")
const canvash = new CanvasHelper()
const notionh = new NotionHelper()
const notion = new Client({ auth: notionh.token})
/**
* Creates a page and adds it to the Notion database.
*
* @param {Promise<Array<{ name: string, date: string, course: string, id: string }>>} assignment_properties
*/
async function createPage(assignment_properties) {
try {
const newPage = await notion.pages.create({
parent: {
type: "database_id",
database_id: await notionh.databaseId
},
properties: assignment_properties,
})
console.log(`SUCCESS: new page ${assignment_properties.ID.number} has been created!`)
} catch (error) {
console.log(`ERROR: createPage failed!\n${error}`)
}
}
/**
* For assignments of a course, check if the assignment is already in the database.
* If the assignment is in the database, send assignment for updates, otherwise
* create the assignment page
*
* @param {number} courseID
* @param {string} courseName
*/
async function createNotionPages(courseID, courseName) {
const assignments = await canvash.getCourseAssignments(courseID, courseName)
for (let assignment of await assignments) {
if (checkNotionPages(await assignment.ID.number)) {
await updateNotionPages(assignment)
} else {
await createPage(assignment)
}
}
}
/**
* Create Notion database with properties to describe assignments.
*/
async function createNotionDatabase() {
try {
// parameters for database, subject to change
const newDatabase = await notion.databases.create({
parent: {
type: "page_id",
page_id: await notionh.pageId,
},
title: [
{
type: "text",
text: {
content: "Canvas Assignments",
},
},
],
properties: {
"Assignment Name": {
type: "title",
title: {},
},
"Due Date": {
type: "date",
date: {},
},
"Course": {
select: {
options: await canvash.courses,
},
},
"Completion": {
type: "checkbox",
checkbox: {}
},
"ID": {
type: "number",
number: {
format: "number"
}
},
},
})
console.log(`SUCCESS: Canvas Assignments database has been created!`)
// set database id in the environment
notionh.databaseId = newDatabase.id
notionh.setDatabaseId(newDatabase.id)
// populate database
createAssignments()
}
catch (error) {
console.log(`ERROR: ${error}`)
}
}
/**
* Create assignment for each course.
*/
async function createAssignments() {
const courses = await canvash.courses
for (let i = 0; i < courses.length; i++) {
createNotionPages(courses[i].id, courses[i].name)
}
}
/**
* Update Notion Database Course Property if changed,
* Prompt to createAssignments to check if assignments
* need to be added or changed
*
*/
async function updateNotionDatabase() {
try {
const response = await notion.databases.update({
database_id: await notionh.databaseId,
properties: {
"Course": {
select: {
options: await canvash.courses,
},
},
},
})
createAssignments()
console.log(`SUCCESS: Database has been updated!`)
} catch (error) {
console.log(`ERROR: Database could not be update! ${error}`)
}
}
/**
* Update page properties.
*
* NOT COMPLETE !!
*/
async function updateNotionPages(assignment) {
try {
//console.log(await notionh.pages)
} catch (error) {
console.log(`ERROR: Could not update page ${assignment.ID.number}`)
}
}
/**
* Check if assignment exists in the Notion Database
* Returns true if exists, false otherwise
* @param {number} pageID
* @returns {boolean}
*/
async function checkNotionPages(pageID) {
const pages = await notionh.pages
if ((await pages).includes(pageID) == true) {
console.log(`FOUND: Assignment ${pageID} exists!`)
return true
} else {
console.log(`NOT FOUND: Assignment ${pageID} does not exist in database`)
return false
}
}
/**
* Check if the course assignment database exists.
*/
async function checkNotionDatabase() {
try {
// find database using id
const response = await notion.databases.query({
database_id: await notionh.databaseId
})
// database exists, update possible values
console.log('FOUND: Database exists! Retrieving database data...')
updateNotionDatabase()
} catch (error) {
if (error.code === APIErrorCode.ObjectNotFound || notionh.databaseId === 'invalid') {
// database does not exist, create database and add pages
console.log('NOT FOUND: Database does not exist! Creating new database...')
createNotionDatabase()
} else {
console.log(`ERROR: checkNotionDatabase failed\n${error}`)
}
}
}
checkNotionDatabase()