Skip to content

Commit da293a8

Browse files
committed
cleaned up webhook
1 parent d2bec20 commit da293a8

File tree

1 file changed

+30
-37
lines changed

1 file changed

+30
-37
lines changed

src/index.js

Lines changed: 30 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -61,66 +61,59 @@ app.use(constants.GRAPHQL_PATH, (req, res, next) => {
6161
app.post('/webhooks/bundestagio/update', async (req, res) => {
6262
const { data } = req.body;
6363
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
7367
$group: {
74-
_id: {
75-
period: '$period',
76-
type: '$type',
77-
},
68+
_id: { period: '$period', type: '$type' },
7869
count: { $sum: 1 },
7970
},
8071
},
8172
{
73+
// Group by Period
8274
$group: {
8375
_id: '$_id.period',
84-
types: {
85-
$push: {
86-
type: '$_id.type',
87-
count: '$count',
88-
},
89-
},
76+
types: { $push: { type: '$_id.type', count: '$count' } },
9077
},
9178
},
9279
{
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 },
9882
}]);
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
10594
const { data: { procedureUpdates } } = await client.query({
10695
query: getProcedureUpdates,
107-
variables: { pageSize: PAGE_SIZE, period: parseInt(objectKey, 10), type: 'Gesetzgebung' },
96+
variables: { pageSize: 20, period, type },
10897
});
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);
114105
}
115106
return null;
116107
});
117108
}
118109
}));
119-
updated += await importProcedures(update2);
110+
// Update
111+
const updated = await importProcedures(update);
120112
res.send({
121113
updated,
122114
succeeded: true,
123115
});
116+
console.log(`Updated: ${updated}`);
124117
} catch (error) {
125118
res.send({
126119
error,

0 commit comments

Comments
 (0)