@@ -61,66 +61,59 @@ app.use(constants.GRAPHQL_PATH, (req, res, next) => {
61
61
app . post ( '/webhooks/bundestagio/update' , async ( req , res ) => {
62
62
const { data } = req . body ;
63
63
try {
64
- let update = [ ] ;
65
- Object . keys ( data ) . map ( ( objectKey ) => {
66
- const value = data [ objectKey ] . find ( d => d . type === 'Gesetzgebung' ) ;
67
- update = update . concat ( value . changedIds ) ;
68
- return null ;
69
- } ) ;
70
- let updated = await importProcedures ( update ) ;
71
-
72
- const counts = await Procedure . aggregate ( [ {
64
+ // Count local Data in groups
65
+ const groups = await Procedure . aggregate ( [ {
66
+ // Group by Period & Type
73
67
$group : {
74
- _id : {
75
- period : '$period' ,
76
- type : '$type' ,
77
- } ,
68
+ _id : { period : '$period' , type : '$type' } ,
78
69
count : { $sum : 1 } ,
79
70
} ,
80
71
} ,
81
72
{
73
+ // Group by Period
82
74
$group : {
83
75
_id : '$_id.period' ,
84
- types : {
85
- $push : {
86
- type : '$_id.type' ,
87
- count : '$count' ,
88
- } ,
89
- } ,
76
+ types : { $push : { type : '$_id.type' , count : '$count' } } ,
90
77
} ,
91
78
} ,
92
79
{
93
- $project : {
94
- _id : 0 ,
95
- period : '$_id' ,
96
- types : 1 ,
97
- } ,
80
+ // Rename _id Field to period
81
+ $project : { _id : 0 , period : '$_id' , types : 1 } ,
98
82
} ] ) ;
99
- const update2 = [ ] ;
100
- await Promise . all ( Object . keys ( data ) . map ( async ( objectKey ) => {
101
- const { count } = data [ objectKey ] . find ( d => d . type === 'Gesetzgebung' ) ;
102
- const localCount = counts . find ( d => d . period === parseInt ( objectKey , 10 ) ) . types . find ( d => d . type === 'Gesetzgebung' ) . count ;
103
- if ( count > localCount ) {
104
- const PAGE_SIZE = 20 ;
83
+
84
+ const update = [ ] ;
85
+ await Promise . all ( data . map ( async ( d ) => {
86
+ const period = parseInt ( d . period , 10 ) ;
87
+ const { type, countBefore, changedIds } = d . types . find ( t => t . type === 'Gesetzgebung' ) ;
88
+ const localCount = groups . find ( c => c . period === period ) . types . find ( ct => ct . type === type ) . count ;
89
+ // Append Changed IDs
90
+ update . concat ( changedIds ) ;
91
+ // Compare Counts Remote & Local
92
+ if ( countBefore > localCount ) {
93
+ // Find remote Procedure Updates
105
94
const { data : { procedureUpdates } } = await client . query ( {
106
95
query : getProcedureUpdates ,
107
- variables : { pageSize : PAGE_SIZE , period : parseInt ( objectKey , 10 ) , type : 'Gesetzgebung' } ,
96
+ variables : { pageSize : 20 , period, type } ,
108
97
} ) ;
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 ) ;
98
+ // Find local Procedure Updates
99
+ const localProcedureUpdates = await Procedure . find ( { period, type } , { procedureId : 1 , lastUpdateDate : 1 } ) ;
100
+ // Compare
101
+ procedureUpdates . map ( ( pu ) => {
102
+ const localData = localProcedureUpdates . find ( ld => ld . procedureId === pu . procedureId ) ;
103
+ if ( ! localData || new Date ( localData . lastUpdateDate ) < new Date ( pu . updatedAt ) ) {
104
+ update . push ( pu . procedureId ) ;
114
105
}
115
106
return null ;
116
107
} ) ;
117
108
}
118
109
} ) ) ;
119
- updated += await importProcedures ( update2 ) ;
110
+ // Update
111
+ const updated = await importProcedures ( update ) ;
120
112
res . send ( {
121
113
updated,
122
114
succeeded : true ,
123
115
} ) ;
116
+ console . log ( `Updated: ${ updated } ` ) ;
124
117
} catch ( error ) {
125
118
res . send ( {
126
119
error,
0 commit comments