@@ -15,7 +15,9 @@ import resolvers from './graphql/resolvers';
15
15
import importProcedures from './scripts/import' ;
16
16
17
17
// Models
18
- import ProcedureModel from './models/Procedure' ;
18
+ import Procedure from './models/Procedure' ;
19
+ import getProcedureUpdates from './graphql/queries/getProcedureUpdates' ;
20
+ import client from './graphql/client' ;
19
21
20
22
const app = express ( ) ;
21
23
@@ -49,18 +51,67 @@ app.use(constants.GRAPHQL_PATH, (req, res, next) => {
49
51
schema,
50
52
context : {
51
53
// Models
52
- ProcedureModel ,
54
+ Procedure ,
53
55
} ,
54
56
tracing : true ,
55
57
cacheControl : true ,
56
58
} ) ( req , res , next ) ;
57
59
} ) ;
58
60
59
61
app . post ( '/webhooks/bundestagio/update' , async ( req , res ) => {
60
- const { procedureIds } = req . body ;
62
+ const { data } = req . body ;
61
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
+ const updated = await importProcedures ( update ) ;
71
+
72
+ const counts = await Procedure . aggregate ( [ {
73
+ $group : {
74
+ _id : {
75
+ period : '$period' ,
76
+ type : '$type' ,
77
+ } ,
78
+ count : { $sum : 1 } ,
79
+ } ,
80
+ } ,
81
+ {
82
+ $group : {
83
+ _id : '$_id.period' ,
84
+ types : {
85
+ $push : {
86
+ type : '$_id.type' ,
87
+ count : '$count' ,
88
+ } ,
89
+ } ,
90
+ } ,
91
+ } ,
92
+ {
93
+ $project : {
94
+ _id : 0 ,
95
+ period : '$_id' ,
96
+ types : 1 ,
97
+ } ,
98
+ } ] ) ;
99
+ Object . keys ( data ) . map ( async ( objectKey ) => {
100
+ const { count } = data [ objectKey ] . find ( d => d . type === 'Gesetzgebung' ) ;
101
+ const localCount = counts . find ( d => d . period === parseInt ( objectKey , 10 ) ) . types . find ( d => d . type === 'Gesetzgebung' ) . count ;
102
+ if ( count > localCount ) {
103
+ console . log ( count ) ;
104
+ console . log ( localCount ) ;
105
+ const PAGE_SIZE = 20 ;
106
+ const { data : { procedureUpdates } } = await client . query ( {
107
+ query : getProcedureUpdates ,
108
+ variables : { pageSize : PAGE_SIZE , period : parseInt ( objectKey , 10 ) , type : 'Gesetzgebung' } ,
109
+ } ) ;
110
+ console . log ( procedureUpdates ) ;
111
+ }
112
+ } ) ;
62
113
res . send ( {
63
- updated : await importProcedures ( procedureIds ) ,
114
+ updated,
64
115
succeeded : true ,
65
116
} ) ;
66
117
} catch ( error ) {
0 commit comments