-
Notifications
You must be signed in to change notification settings - Fork 4
/
notionAPIServices.js
98 lines (90 loc) · 2.42 KB
/
notionAPIServices.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
const {Client} = require('@notionhq/client');
const DateService = require('./DateService');
const notionCheckbox = process.env.CHECKBOX_FIELD;
const notionDate = process.env.DATE_FIELD;
const notionTitle = process.env.TITLE_FIELD;
const notionRepetitive = process.env.REPETITIVE_FIELD;
const notionDatabaseURL = process.env.DATABASE_URL;
const notionDatabaseID = process.env.DATABASE_ID;
const parseSharedURL = (URL) => URL?.split('?')[0].split('so/')[1];
function uncheckAndNextOcurrency(updatedDate) {
return JSON.parse(
`{
"${notionCheckbox}": {
"checkbox": false
},
"${notionDate}": {
"date": {
"start": "${updatedDate}"
}
}
}`
);
}
class NotionService {
#notion;
constructor() {
this.#notion = new Client({
auth: process.env.TOKEN,
});
}
// Creates a page with telling you that you didn't your task the day before.
async createAlert(card) {
const alertDate = DateService.addDay(new Date().toISOString(), false, 0);
await this.#notion.pages.create({
parent: card.parent,
properties: JSON.parse(
`{
"${notionTitle}": {
"title": [
{
"text": {
"content": "${card.properties[notionTitle].title[0].text.content} was't done yesterday"
}
}
]
},
"${notionDate}": {
"date": {
"start": "${alertDate}"
}
}
}`
),
});
}
async handleImcompleteTask(card, cardDate, hasHours, daysAmount) {
let updatedDate = DateService.addDay(cardDate, hasHours, daysAmount);
await this.updateCard(card, updatedDate);
await this.createAlert(card);
}
async updateCard(card, updatedDate) {
await this.#notion.pages.update({
page_id: card.id,
properties: uncheckAndNextOcurrency(updatedDate),
});
}
async queryDB() {
const response = await this.#notion.databases.query({
database_id: parseSharedURL(notionDatabaseURL) || notionDatabaseID,
filter: {
and: [
{
property: notionRepetitive,
select: {
is_not_empty: true,
},
},
{
property: notionDate,
date: {
is_not_empty: true,
},
},
],
},
});
return response;
}
}
module.exports = new NotionService();