Skip to content

Commit 53c986e

Browse files
committed
Add update vs create distinction to avoid duplicate clones
1 parent 09c8d75 commit 53c986e

File tree

1 file changed

+58
-44
lines changed

1 file changed

+58
-44
lines changed

index.js

+58-44
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,18 @@ const { Client } = require('@notionhq/client')
22
const notion = new Client({ auth: process.env.NOTION_KEY })
33
const AWS = require('aws-sdk');
44

5-
const dbByTitleFilter = (str) => ({ title: [{plain_text}] }) => plain_text === str
6-
const getTasksQueryObj = (dbId) => ({
7-
database_id: dbId,
8-
filter: {
9-
or: [
10-
{
11-
property: "Is shared",
12-
formula: {
13-
checkbox: {
14-
equals: true
15-
}
5+
const taskIsSharedFilter = {
6+
or: [
7+
{
8+
property: "Is shared",
9+
formula: {
10+
checkbox: {
11+
equals: true
1612
}
1713
}
18-
]
19-
}
20-
})
14+
}
15+
]
16+
}
2117
const dbsSearchObj = {
2218
query: '',
2319
filter: {
@@ -26,16 +22,20 @@ const dbsSearchObj = {
2622
}
2723
}
2824

29-
const toClonedTaskDecorator = (database_id) => async (task) => {
30-
const props = [
25+
const toClonedTaskDecorator = (inputs) => async ({ id: mainTaskId, properties }) => {
26+
const { sharedTasks, sharedTaskDBId } = inputs
27+
const existingClonedTask = sharedTasks.find(({properties}) => {
28+
return properties["Cloned from"]?.relation[0]?.id === mainTaskId
29+
})
30+
const clonedProps = [
3131
"Status",
3232
"Due",
3333
"Tags",
3434
"Project",
3535
"Theme",
3636
"Assignee",
3737
].reduce((prev, key) => {
38-
const matchedProp = task.properties[key]
38+
const matchedProp = properties[key]
3939
const type = matchedProp?.type
4040
return matchedProp
4141
? { ...prev, [key]: {[type]: matchedProp[type]} }
@@ -45,41 +45,55 @@ const toClonedTaskDecorator = (database_id) => async (task) => {
4545
title: [
4646
{
4747
"text": {
48-
"content": task.properties.Name.title[0].plain_text
48+
"content": properties.Name.title[0].plain_text
4949
}
5050
}
5151
]
52+
},
53+
"Cloned from": {
54+
relation: [
55+
{
56+
id: mainTaskId
57+
}
58+
]
5259
}
5360
})
54-
55-
await notion.pages.create({
56-
parent: { database_id },
57-
properties: props
58-
})
61+
62+
if (existingClonedTask) {
63+
await notion.pages.update({
64+
page_id: existingClonedTask.id,
65+
properties: clonedProps
66+
})
67+
} else {
68+
await notion.pages.create({
69+
parent: { database_id: sharedTaskDBId },
70+
properties: clonedProps
71+
})
72+
}
5973
}
6074

6175
exports.handler = async (event, context, callback) => {
62-
const dbsSearch = await notion.search(dbsSearchObj)
63-
const dbs = dbsSearch.results
64-
const dbsMap = {
65-
tasks: {
66-
main: dbs.find(dbByTitleFilter("Tasks")).id,
67-
shared: dbs.find(dbByTitleFilter("Tasks (shared)")).id
68-
},
69-
projects: {
70-
main: dbs.find(dbByTitleFilter("Projects")).id
71-
},
72-
rules: {
73-
main: dbs.find(dbByTitleFilter("Rules")).id
74-
}
75-
}
76+
const { results: dbsSearch } = await notion.search(dbsSearchObj)
77+
const getDBId = (dbTitle) => dbsSearch.find(({ title: [{ plain_text }]}) => plain_text === dbTitle).id
78+
const mainTaskDBId = getDBId("Tasks")
79+
const sharedTaskDBId = getDBId("Tasks (shared)")
80+
81+
// Tasks in the main DB marked for sharing
82+
const { results: mainTasks } = await notion.databases.query({
83+
database_id: mainTaskDBId,
84+
filter: taskIsSharedFilter
85+
})
86+
87+
// Tasks currently in the shared DB
88+
const { results: sharedTasks } = await notion.databases.query({
89+
database_id: sharedTaskDBId
90+
})
7691

77-
const tasksQueryObj = getTasksQueryObj(dbsMap.tasks.main)
78-
const tasksQuery = await notion.databases.query(tasksQueryObj)
79-
const tasks = tasksQuery.results
80-
const clonedTasks = await Promise.all(tasks.map(toClonedTaskDecorator(dbsMap.tasks.shared)))
81-
const success = `Successfully cloned ${clonedTasks.length} tasks`
92+
const clonedTasks = await Promise.all(mainTasks.map(toClonedTaskDecorator(
93+
{ sharedTasks, sharedTaskDBId }
94+
)))
8295

83-
console.log(success)
84-
return success
96+
const msg = `Successfully synced ${clonedTasks.length} tasks`
97+
console.log(msg)
98+
return msg
8599
}

0 commit comments

Comments
 (0)