-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotion-pipedream.mjs
113 lines (107 loc) · 2.63 KB
/
notion-pipedream.mjs
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
import { Client } from '@notionhq/client';
async function getAllPages(notion, databaseId) {
const response = await notion.databases.query({
database_id: databaseId,
});
return response.results;
}
async function getSelectedPage(notion, databaseId) {
const response = await notion.databases.query({
database_id: databaseId,
filter: {
or: [
{
property: 'Show',
checkbox: {
equals: true,
},
},
],
},
});
return response.results[0];
}
async function getRandomPage(pages) {
return pages[Math.floor(Math.random() * pages.length)];
}
export default defineComponent({
name: 'Notion Random Item',
key: 'notion-query-database',
description: '',
props: {
notion: {
type: 'app',
app: 'notion',
description: '',
},
databaseId: {
type: 'string',
label: 'Target database',
description: 'Select the database',
async options({ prevContext }) {
const notion = new Client({
auth: this.notion.$auth.oauth_access_token,
});
const response = await notion.search({
filter: {
value: 'database',
property: 'object',
},
start_cursor: prevContext.nextPageParameters ?? undefined,
});
const options = response.results.map((db) => {
const title = db.title
.map((title) => title.plain_text)
.filter((title) => title.length > 0)
.reduce((prev, next) => prev + next, '');
return {
label: title || 'Untitled',
value: db.id,
};
});
return {
context: {
cursor: response.next_cursor,
},
options,
};
},
},
},
methods: {
getAllPages,
getSelectedPage,
getRandomPage,
},
async run() {
try {
const notion = new Client({
auth: this.notion.$auth.oauth_access_token,
});
const selectedPage = await this.getSelectedPage(notion, this.databaseId);
const randomPage = await this.getRandomPage(
await this.getAllPages(notion, this.databaseId)
);
if (selectedPage !== undefined) {
await notion.pages.update({
page_id: selectedPage.id,
properties: {
Show: {
checkbox: false,
},
},
});
}
await notion.pages.update({
page_id: randomPage.id,
properties: {
Show: {
checkbox: true,
},
},
});
} catch (error) {
console.error('An error occurred:', error);
}
},
});