@@ -2,22 +2,18 @@ const { Client } = require('@notionhq/client')
2
2
const notion = new Client ( { auth : process . env . NOTION_KEY } )
3
3
const AWS = require ( 'aws-sdk' ) ;
4
4
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
16
12
}
17
13
}
18
- ]
19
- }
20
- } )
14
+ }
15
+ ]
16
+ }
21
17
const dbsSearchObj = {
22
18
query : '' ,
23
19
filter : {
@@ -26,16 +22,20 @@ const dbsSearchObj = {
26
22
}
27
23
}
28
24
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 = [
31
31
"Status" ,
32
32
"Due" ,
33
33
"Tags" ,
34
34
"Project" ,
35
35
"Theme" ,
36
36
"Assignee" ,
37
37
] . reduce ( ( prev , key ) => {
38
- const matchedProp = task . properties [ key ]
38
+ const matchedProp = properties [ key ]
39
39
const type = matchedProp ?. type
40
40
return matchedProp
41
41
? { ...prev , [ key ] : { [ type ] : matchedProp [ type ] } }
@@ -45,41 +45,55 @@ const toClonedTaskDecorator = (database_id) => async (task) => {
45
45
title : [
46
46
{
47
47
"text" : {
48
- "content" : task . properties . Name . title [ 0 ] . plain_text
48
+ "content" : properties . Name . title [ 0 ] . plain_text
49
49
}
50
50
}
51
51
]
52
+ } ,
53
+ "Cloned from" : {
54
+ relation : [
55
+ {
56
+ id : mainTaskId
57
+ }
58
+ ]
52
59
}
53
60
} )
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
+ }
59
73
}
60
74
61
75
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
+ } )
76
91
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
+ ) ) )
82
95
83
- console . log ( success )
84
- return success
96
+ const msg = `Successfully synced ${ clonedTasks . length } tasks`
97
+ console . log ( msg )
98
+ return msg
85
99
}
0 commit comments