11import importProcedures from './import' ;
22import getProcedureUpdates from '../graphql/queries/getProcedureUpdates' ;
3- import client from '../graphql/client' ;
3+ import createClient from '../graphql/client' ;
44import ProcedureModel from '../models/Procedure' ;
55
66export default async ( data ) => {
7+ const client = createClient ( ) ;
8+
79 // Count local Data in groups
8- const groups = await ProcedureModel . aggregate ( [ {
9- // Group by Period & Type
10- $group : {
11- _id : { period : '$period' , type : '$type' } ,
12- count : { $sum : 1 } ,
10+ const groups = await ProcedureModel . aggregate ( [
11+ {
12+ // Group by Period & Type
13+ $group : {
14+ _id : { period : '$period' , type : '$type' } ,
15+ count : { $sum : 1 } ,
16+ } ,
17+ } ,
18+ {
19+ // Group by Period
20+ $group : {
21+ _id : '$_id.period' ,
22+ types : { $push : { type : '$_id.type' , count : '$count' } } ,
23+ } ,
1324 } ,
14- } ,
15- {
16- // Group by Period
17- $group : {
18- _id : '$_id.period' ,
19- types : { $push : { type : '$_id.type' , count : '$count' } } ,
25+ {
26+ // Rename _id Field to period
27+ $project : { _id : 0 , period : '$_id' , types : 1 } ,
2028 } ,
21- } ,
22- {
23- // Rename _id Field to period
24- $project : { _id : 0 , period : '$_id' , types : 1 } ,
25- } ] ) ;
29+ ] ) ;
2630
2731 const update = [ ] ;
2832 await Promise . all ( data . map ( async ( d ) => {
2933 const period = parseInt ( d . period , 10 ) ;
3034 const { type, countBefore, changedIds } = d . types . find ( t => t . type === 'Gesetzgebung' ) ;
3135 const group = groups . find ( c => c . period === period ) ;
32- const localCount = group ? group . types
33- . find ( ct => ct . type === type ) . count : 0 ;
36+ const localCount = group ? group . types . find ( ct => ct . type === type ) . count : 0 ;
3437 // Append Changed IDs
3538 update . concat ( changedIds ) ;
3639 // Compare Counts Remote & Local
@@ -40,10 +43,12 @@ export default async (data) => {
4043 query : getProcedureUpdates ,
4144 variables : { period, type } ,
4245 } ) ;
43- // Find local Procedure Updates
44- const localProcedureUpdates = await ProcedureModel
45- . find ( { period, type } , { procedureId : 1 , lastUpdateDate : 1 } ) ;
46- // Compare
46+ // Find local Procedure Updates
47+ const localProcedureUpdates = await ProcedureModel . find (
48+ { period, type } ,
49+ { procedureId : 1 , lastUpdateDate : 1 } ,
50+ ) ;
51+ // Compare
4752 procedureUpdates . forEach ( ( pu ) => {
4853 const localData = localProcedureUpdates . find ( ld => ld . procedureId === pu . procedureId ) ;
4954 if ( ! localData || new Date ( localData . lastUpdateDate ) < new Date ( pu . updatedAt ) ) {
0 commit comments