Runs every 15 min
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
import process from "node:process";
import { Client } from "npm:@notionhq/client";
import dayjs from "npm:dayjs";
const DATABASE_ID = "519446a0d3ed47038fffd669b9ece770";
const notion = new Client({ auth: process.env.NOTION_API_KEY });
const intervalMapping = {
"Weekly": (date) => dayjs(date).add(1, "week"),
"Monthly": (date) => dayjs(date).add(1, "month"),
"Quarterly": (date) => dayjs(date).add(3, "months"),
};
export default async function(interval: Interval) {
const response = await notion.databases.query({
database_id: DATABASE_ID,
filter: {
and: [
{
property: "Done",
checkbox: {
equals: true,
},
},
{
property: "Repeat",
select: {
is_not_empty: true,
},
},
],
},
});
response.results.forEach(async item => {
const interval = intervalMapping[item.properties.Repeat.select.name];
const date = item.properties.Date.date.start;
if (!interval || !date) return;
const nextItem = {
parent: { database_id: DATABASE_ID },
properties: {
...item.properties,
"Done": { type: "checkbox", checkbox: false },
"Date": { type: "date", date: { start: interval(date).format("YYYY-MM-DD") } },
},
};
await notion.pages.create(nextItem);
await notion.pages.update({
page_id: item.id,
properties: {
"Repeat": null,
},
});
});
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
August 13, 2024