Skip to content

Commit c816fc9

Browse files
committed
working till stage procedureUpdates (missing update date from server)
1 parent 21a8e35 commit c816fc9

File tree

2 files changed

+65
-4
lines changed

2 files changed

+65
-4
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import gql from 'graphql-tag';
2+
3+
export default gql`
4+
query procedureUpdates($pageSize: Int, $period: [Int!], $type: [String!]) {
5+
procedureUpdates(pageSize: $pageSize, period: $period, type: $type) {
6+
procedureId
7+
lastUpdateDate
8+
}
9+
}
10+
`;

src/index.js

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ import resolvers from './graphql/resolvers';
1515
import importProcedures from './scripts/import';
1616

1717
// 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';
1921

2022
const app = express();
2123

@@ -49,18 +51,67 @@ app.use(constants.GRAPHQL_PATH, (req, res, next) => {
4951
schema,
5052
context: {
5153
// Models
52-
ProcedureModel,
54+
Procedure,
5355
},
5456
tracing: true,
5557
cacheControl: true,
5658
})(req, res, next);
5759
});
5860

5961
app.post('/webhooks/bundestagio/update', async (req, res) => {
60-
const { procedureIds } = req.body;
62+
const { data } = req.body;
6163
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+
});
62113
res.send({
63-
updated: await importProcedures(procedureIds),
114+
updated,
64115
succeeded: true,
65116
});
66117
} catch (error) {

0 commit comments

Comments
 (0)