-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotion.js
66 lines (58 loc) · 1.37 KB
/
notion.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
require('dotenv').config();
import { Client } from '@notionhq/client';
async function getAllPages(notion) {
const response = await notion.databases.query({
database_id: process.env.NOTION_DATABASE_ID,
});
return response.results;
}
async function getSelectedPage(notion) {
const response = await notion.databases.query({
database_id: process.env.NOTION_DATABASE_ID,
filter: {
or: [
{
property: 'Show',
checkbox: {
equals: true,
},
},
],
},
});
return response.results[0];
}
async function getRandomPage(pages) {
return pages[Math.floor(Math.random() * pages.length)];
}
async function updatePage(notion, pageId, properties) {
await notion.pages.update({
page_id: pageId,
properties,
});
}
async function run() {
try {
const notion = new Client({
auth: this.notion$auth.oauth_access_token,
});
const pages = await getAllPages(notion);
const selectedPage = await getSelectedPage(notion);
const randomPage = await getRandomPage(pages);
if (selectedPage !== undefined) {
await updatePage(notion, selectedPage.id, {
Show: {
checkbox: false,
},
});
}
await updatePage(notion, randomPage.id, {
Show: {
checkbox: true,
},
});
} catch (error) {
console.error(error.body);
}
}
run();