-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
99 lines (91 loc) · 2.68 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
const { Client } = require('@notionhq/client')
const notion = new Client({ auth: process.env.NOTION_KEY })
const AWS = require('aws-sdk');
const taskIsSharedFilter = {
or: [
{
property: "Is shared",
formula: {
checkbox: {
equals: true
}
}
}
]
}
const dbsSearchObj = {
query: '',
filter: {
property: 'object',
value: 'database'
}
}
const toClonedTaskDecorator = (inputs) => async ({ id: mainTaskId, properties }) => {
const { sharedTasks, sharedTaskDBId } = inputs
const existingClonedTask = sharedTasks.find(({properties}) => {
return properties["Cloned from"]?.relation[0]?.id === mainTaskId
})
const clonedProps = [
"Status",
"Due",
"Tags",
"Project",
"Theme",
"Assignee",
].reduce((prev, key) => {
const matchedProp = properties[key]
const type = matchedProp?.type
return matchedProp
? { ...prev, [key]: {[type]: matchedProp[type]} }
: prev
}, {
title: {
title: [
{
"text": {
"content": properties.Name.title[0].plain_text
}
}
]
},
"Cloned from": {
relation: [
{
id: mainTaskId
}
]
}
})
if (existingClonedTask) {
await notion.pages.update({
page_id: existingClonedTask.id,
properties: clonedProps
})
} else {
await notion.pages.create({
parent: { database_id: sharedTaskDBId },
properties: clonedProps
})
}
}
exports.handler = async (event, context, callback) => {
const { results: dbsSearch } = await notion.search(dbsSearchObj)
const getDBId = (dbTitle) => dbsSearch.find(({ title: [{ plain_text }]}) => plain_text === dbTitle).id
const mainTaskDBId = getDBId("Tasks")
const sharedTaskDBId = getDBId("Tasks (shared)")
// Tasks in the main DB marked for sharing
const { results: mainTasks } = await notion.databases.query({
database_id: mainTaskDBId,
filter: taskIsSharedFilter
})
// Tasks currently in the shared DB
const { results: sharedTasks } = await notion.databases.query({
database_id: sharedTaskDBId
})
const clonedTasks = await Promise.all(mainTasks.map(toClonedTaskDecorator(
{ sharedTasks, sharedTaskDBId }
)))
const msg = `Successfully synced ${clonedTasks.length} tasks`
console.log(msg)
return msg
}