-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
212 lines (202 loc) · 6.62 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
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
205
206
207
208
209
210
211
212
require('dotenv').config();
const date = require('date-fns');
const shell = require('shelljs');
const path = require('path');
const os = require('os');
const fs = require('fs');
const DateService = require('./DateService');
const NotionAPI = require('./NotionAPIServices');
const requirements = [
'TOKEN',
'TITLE_FIELD',
'DATE_FIELD',
'CHECKBOX_FIELD',
'REPETITIVE_FIELD',
'TZ',
];
requirements.forEach((req) => {
if (!process.env[req]) {
console.error(
`Couldn't find ${req} on .env file, please make sure to fill the .env variables with the right values.`
);
process.exit();
}
});
if (
os.type().toLowerCase().includes('windows') &&
!fs.existsSync('Notion-repetitive.bat')
) {
fs.writeFileSync(
'Notion-repetitive.bat',
`cd "${path.resolve(__dirname)}"
node index.js`
);
shell.echo(
"Schedule the .bat file on your task scheduler so you don't have to worry about it."
);
} else if (
os.type().toLowerCase().includes('darwin') &&
!fs.existsSync('Notion-repetitive.sh')
) {
fs.writeFileSync(
'Notion-repetitive.sh',
`cd "${path.resolve(__dirname)}"
node index.js`
);
shell.echo(
"Schedule the .sh file on your task scheduler so you don't have to worry about it."
);
}
async function main() {
try {
const response = await NotionAPI.queryDB();
const results = response.results;
if (response.has_more) {
let Res = response;
while (Res.has_more) {
Res = await notion.databases.query({
database_id: databaseID,
start_cursor: Res.next_cursor,
});
results.push(...Res.results);
}
}
for (const card of results) {
const cardDate = date.parseISO(
card.properties[process.env.DATE_FIELD].date.start
);
const hasHours =
card.properties[process.env.DATE_FIELD].date.start.length > 10;
const wasYesterday = date.isYesterday(cardDate);
const repetitive =
card.properties[process.env.REPETITIVE_FIELD].select.name.toLowerCase();
const isChecked = card.properties[process.env.CHECKBOX_FIELD].checkbox;
if (wasYesterday) {
switch (repetitive) {
case 'daily':
if (isChecked) {
let updatedDate = DateService.addDay(cardDate, hasHours, 1);
await NotionAPI.updateCard(card, updatedDate);
console.warn(
`Changed ${
card.properties[process.env.TITLE_FIELD].title[0].text.content
} (daily task) to next occurency`
);
} else {
await NotionAPI.handleImcompleteTask(card, cardDate, hasHours, 1);
}
break;
case 'weekly':
if (isChecked) {
let updatedDate = DateService.addDay(cardDate, hasHours, 7);
await NotionAPI.updateCard(card, updatedDate);
console.warn(
`Changed ${
card.properties[process.env.TITLE_FIELD].title[0].text.content
} (weekly task) to next occurency`
);
} else {
await NotionAPI.handleImcompleteTask(card, cardDate, hasHours, 7);
}
break;
case 'every other day':
if (isChecked) {
let updatedDate = DateService.addDay(cardDate, hasHours, 2);
await NotionAPI.updateCard(card, updatedDate);
console.warn(
`Changed ${
card.properties[process.env.TITLE_FIELD].title[0].text.content
} (Every other day task) to next occurency`
);
} else {
await NotionAPI.handleImcompleteTask(card, cardDate, hasHours, 2);
}
break;
//? Not checked
case 'monthly':
(async () => {
let updatedDate = DateService.addMonth(cardDate, hasHours);
if (isChecked) {
await NotionAPI.updateCard(card, updatedDate);
console.warn(
`Changed ${
card.properties[process.env.TITLE_FIELD].title[0].text
.content
} (Monthly) task to next week`
);
} else {
await NotionAPI.updateCard(card, updatedDate);
await NotionAPI.createAlert(card);
}
})();
break;
case 'weekdays':
(async () => {
let updatedDate;
if (cardDate.getDay() < 5) {
updatedDate = DateService.addDay(cardDate, hasHours, 1);
} else {
updatedDate = DateService.addDay(cardDate, hasHours, 3);
}
if (isChecked) {
await NotionAPI.updateCard(card, updatedDate);
console.warn(
`Changed ${
card.properties[process.env.TITLE_FIELD].title[0].text
.content
} (Weekday) task to next week`
);
} else {
await NotionAPI.updateCard(card, updatedDate);
await NotionAPI.createAlert(card);
}
})();
break;
case 'every other week':
if (isChecked) {
let updatedDate = DateService.addDay(cardDate, hasHours, 14);
await NotionAPI.updateCard(card, updatedDate);
console.warn(
`Changed ${
card.properties[process.env.TITLE_FIELD].title[0].text.content
} (every other week task) to next occurency`
);
} else {
await NotionAPI.handleImcompleteTask(
card,
cardDate,
hasHours,
14
);
}
break;
case 'every x days':
if (isChecked) {
let updatedDate = DateService.addDay(
cardDate,
hasHours,
card.properties[process.env.DAY_COUNT].number
);
await NotionAPI.updateCard(card, updatedDate);
console.warn(
`Changed ${
card.properties[process.env.TITLE_FIELD].title[0].text.content
} (every x days task) to next occurency`
);
} else {
await NotionAPI.handleImcompleteTask(
card,
cardDate,
hasHours,
card.properties[process.env.DAY_COUNT].number
);
}
break;
}
}
};
} catch (err) {
console.warn(err);
}
}
main();