@@ -67,7 +67,7 @@ app.post('/webhooks/bundestagio/update', async (req, res) => {
67
67
update = update . concat ( value . changedIds ) ;
68
68
return null ;
69
69
} ) ;
70
- const updated = await importProcedures ( update ) ;
70
+ let updated = await importProcedures ( update ) ;
71
71
72
72
const counts = await Procedure . aggregate ( [ {
73
73
$group : {
@@ -96,20 +96,27 @@ app.post('/webhooks/bundestagio/update', async (req, res) => {
96
96
types : 1 ,
97
97
} ,
98
98
} ] ) ;
99
- Object . keys ( data ) . map ( async ( objectKey ) => {
99
+ const update2 = [ ] ;
100
+ await Promise . all ( Object . keys ( data ) . map ( async ( objectKey ) => {
100
101
const { count } = data [ objectKey ] . find ( d => d . type === 'Gesetzgebung' ) ;
101
102
const localCount = counts . find ( d => d . period === parseInt ( objectKey , 10 ) ) . types . find ( d => d . type === 'Gesetzgebung' ) . count ;
102
103
if ( count > localCount ) {
103
- console . log ( count ) ;
104
- console . log ( localCount ) ;
105
104
const PAGE_SIZE = 20 ;
106
105
const { data : { procedureUpdates } } = await client . query ( {
107
106
query : getProcedureUpdates ,
108
107
variables : { pageSize : PAGE_SIZE , period : parseInt ( objectKey , 10 ) , type : 'Gesetzgebung' } ,
109
108
} ) ;
110
- console . log ( procedureUpdates ) ;
109
+ const localProcedureUpdates = await Procedure . find ( { period : parseInt ( objectKey , 10 ) , type : 'Gesetzgebung' } , { procedureId : 1 , lastUpdateDate : 1 } ) ;
110
+ procedureUpdates . map ( ( data2 ) => {
111
+ const localData = localProcedureUpdates . find ( d => d . procedureId === data2 . procedureId ) ;
112
+ if ( ! localData || new Date ( localData . lastUpdateDate ) < new Date ( data2 . updatedAt ) ) {
113
+ update2 . push ( data2 . procedureId ) ;
114
+ }
115
+ return null ;
116
+ } ) ;
111
117
}
112
- } ) ;
118
+ } ) ) ;
119
+ updated += await importProcedures ( update2 ) ;
113
120
res . send ( {
114
121
updated,
115
122
succeeded : true ,
0 commit comments