-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetDefaultCategory.ts
More file actions
114 lines (90 loc) · 2.7 KB
/
SetDefaultCategory.ts
File metadata and controls
114 lines (90 loc) · 2.7 KB
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
import dotenv from "dotenv";
dotenv.config();
import { Client, LogLevel } from "@notionhq/client";
import { DatabasesQueryParameters, PagesUpdateParameters } from "@notionhq/client/build/src/api-endpoints";
const categoryPropertyName = "Category";
const databaseID = "dae968ec2e6a4e15aec83a25c790b1a3";
const defaultCategory = "Personal";
class Task {
name: string;
pageID: string;
constructor(pageID: string, name: string) {
this.pageID = pageID;
this.name = name;
}
public static fromPage(page : any) {
return new Task(
page.id,
page.properties.Name.title[0].plain_text,
);
}
}
const main = async () => {
const client = new Client({
"auth": process.env.NOTION_TOKEN,
"logLevel": LogLevel.DEBUG,
});
const tasks = await getTasks(client);
for (const task of tasks) {
await updatePriority(client, task);
}
};
const getTasks = async (client : Client) : Promise<Task[]> => {
let hasMore = true;
let nextCursor : string | null = null;
const taskPages = [];
while (hasMore){
const request : DatabasesQueryParameters = {
"database_id": databaseID,
"sorts": [
{
"property": "Priority",
"direction": "ascending",
},
{
"property": "Start Date",
"direction": "ascending",
}
],
};
const categoryFilter = {
"or": [
{
"property": categoryPropertyName,
"select": {
"is_empty": true
}
}
]
};
request.filter = {
"and": [
categoryFilter
]
};
if (nextCursor !== null)
request.start_cursor = nextCursor;
const response = await client.databases.query(request);
taskPages.push(...response.results);
hasMore = response.has_more;
nextCursor = response.next_cursor;
}
const pages = taskPages.map(Task.fromPage);
return pages;
};
const updatePriority = async (client : Client, task : Task) => {
console.log(`Updating task '${task.name}' to category '${defaultCategory}'.`);
const request = {
"page_id": task.pageID,
"properties": {
[categoryPropertyName]: {
"select": {
"name": defaultCategory
}
}
}
};
const response = await client.pages.update(request as unknown as PagesUpdateParameters);
console.log(response);
};
main();