Skip to content

Commit ac7e9bd

Browse files
committed
fix missing client
1 parent 6318c78 commit ac7e9bd

File tree

3 files changed

+89
-72
lines changed

3 files changed

+89
-72
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules/
2-
.env
2+
*.env
3+
*.log

src/index.js

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,46 @@
11
/* eslint-disable no-console */
22

3-
import express from 'express';
4-
import bodyParser from 'body-parser';
5-
import { graphqlExpress, graphiqlExpress } from 'apollo-server-express';
6-
import { makeExecutableSchema } from 'graphql-tools';
7-
import { createServer } from 'http';
8-
import { Engine } from 'apollo-engine';
3+
import express from "express";
4+
import bodyParser from "body-parser";
5+
import { graphqlExpress, graphiqlExpress } from "apollo-server-express";
6+
import { makeExecutableSchema } from "graphql-tools";
7+
import { createServer } from "http";
8+
import { Engine } from "apollo-engine";
99

10-
import './config/db';
11-
import constants from './config/constants';
12-
import typeDefs from './graphql/schemas';
13-
import resolvers from './graphql/resolvers';
10+
import "./config/db";
11+
import constants from "./config/constants";
12+
import typeDefs from "./graphql/schemas";
13+
import resolvers from "./graphql/resolvers";
1414

15-
import webhook from './scripts/webhook';
15+
import webhook from "./scripts/webhook";
1616

1717
// Models
18-
import ProcedureModel from './models/Procedure';
18+
import ProcedureModel from "./models/Procedure";
1919

2020
const app = express();
2121

2222
const schema = makeExecutableSchema({
2323
typeDefs,
24-
resolvers,
24+
resolvers
2525
});
2626

2727
if (process.env.ENGINE_API_KEY) {
2828
const engine = new Engine({
2929
engineConfig: { apiKey: process.env.ENGINE_API_KEY },
30-
graphqlPort: constants.PORT,
30+
graphqlPort: constants.PORT
3131
});
3232
engine.start();
3333
app.use(engine.expressMiddleware());
3434
}
3535

3636
app.use(bodyParser.json());
3737

38-
if (process.env.ENVIRONMENT !== 'production') {
38+
if (process.env.ENVIRONMENT !== "production") {
3939
app.use(
4040
constants.GRAPHIQL_PATH,
4141
graphiqlExpress({
42-
endpointURL: constants.GRAPHQL_PATH,
43-
}),
42+
endpointURL: constants.GRAPHQL_PATH
43+
})
4444
);
4545
}
4646

@@ -49,34 +49,34 @@ app.use(constants.GRAPHQL_PATH, (req, res, next) => {
4949
schema,
5050
context: {
5151
// Models
52-
ProcedureModel,
52+
ProcedureModel
5353
},
5454
tracing: true,
55-
cacheControl: true,
55+
cacheControl: true
5656
})(req, res, next);
5757
});
5858

59-
app.post('/webhooks/bundestagio/update', async (req, res) => {
59+
app.post("/webhooks/bundestagio/update", async (req, res) => {
6060
const { data } = req.body;
6161
try {
6262
const updated = await webhook(data);
6363
res.send({
6464
updated,
65-
succeeded: true,
65+
succeeded: true
6666
});
6767
console.log(`Updated: ${updated}`);
6868
} catch (error) {
6969
console.log(error);
7070
res.send({
7171
error,
72-
succeeded: false,
72+
succeeded: false
7373
});
7474
}
7575
});
7676

7777
const graphqlServer = createServer(app);
7878

79-
graphqlServer.listen(constants.PORT, (err) => {
79+
graphqlServer.listen(constants.PORT, err => {
8080
if (err) {
8181
console.error(err);
8282
} else {

src/scripts/webhook.js

Lines changed: 64 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,73 @@
1-
import importProcedures from './import';
2-
import getProcedureUpdates from '../graphql/queries/getProcedureUpdates';
3-
import client from '../graphql/client';
4-
import ProcedureModel from '../models/Procedure';
1+
import importProcedures from "./import";
2+
import getProcedureUpdates from "../graphql/queries/getProcedureUpdates";
3+
import createClient from "../graphql/client";
4+
import ProcedureModel from "../models/Procedure";
5+
6+
export default async data => {
7+
const client = createClient();
58

6-
export default async (data) => {
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+
}
1317
},
14-
},
15-
{
16-
// Group by Period
17-
$group: {
18-
_id: '$_id.period',
19-
types: { $push: { type: '$_id.type', count: '$count' } },
18+
{
19+
// Group by Period
20+
$group: {
21+
_id: "$_id.period",
22+
types: { $push: { type: "$_id.type", count: "$count" } }
23+
}
2024
},
21-
},
22-
{
23-
// Rename _id Field to period
24-
$project: { _id: 0, period: '$_id', types: 1 },
25-
}]);
25+
{
26+
// Rename _id Field to period
27+
$project: { _id: 0, period: "$_id", types: 1 }
28+
}
29+
]);
2630

2731
const update = [];
28-
await Promise.all(data.map(async (d) => {
29-
const period = parseInt(d.period, 10);
30-
const { type, countBefore, changedIds } = d.types.find(t => t.type === 'Gesetzgebung');
31-
const group = groups.find(c => c.period === period);
32-
const localCount = group ? group.types
33-
.find(ct => ct.type === type).count : 0;
34-
// Append Changed IDs
35-
update.concat(changedIds);
36-
// Compare Counts Remote & Local
37-
if (countBefore > localCount) {
38-
// Find remote Procedure Updates
39-
const { data: { procedureUpdates } } = await client.query({
40-
query: getProcedureUpdates,
41-
variables: { period, type },
42-
});
43-
// Find local Procedure Updates
44-
const localProcedureUpdates = await ProcedureModel
45-
.find({ period, type }, { procedureId: 1, lastUpdateDate: 1 });
46-
// Compare
47-
procedureUpdates.forEach((pu) => {
48-
const localData = localProcedureUpdates.find(ld => ld.procedureId === pu.procedureId);
49-
if (!localData || new Date(localData.lastUpdateDate) < new Date(pu.updatedAt)) {
50-
update.push(pu.procedureId);
51-
}
52-
});
53-
}
54-
}));
32+
await Promise.all(
33+
data.map(async d => {
34+
const period = parseInt(d.period, 10);
35+
const { type, countBefore, changedIds } = d.types.find(
36+
t => t.type === "Gesetzgebung"
37+
);
38+
const group = groups.find(c => c.period === period);
39+
const localCount = group
40+
? group.types.find(ct => ct.type === type).count
41+
: 0;
42+
// Append Changed IDs
43+
update.concat(changedIds);
44+
// Compare Counts Remote & Local
45+
if (countBefore > localCount) {
46+
// Find remote Procedure Updates
47+
const { data: { procedureUpdates } } = await client.query({
48+
query: getProcedureUpdates,
49+
variables: { period, type }
50+
});
51+
// Find local Procedure Updates
52+
const localProcedureUpdates = await ProcedureModel.find(
53+
{ period, type },
54+
{ procedureId: 1, lastUpdateDate: 1 }
55+
);
56+
// Compare
57+
procedureUpdates.forEach(pu => {
58+
const localData = localProcedureUpdates.find(
59+
ld => ld.procedureId === pu.procedureId
60+
);
61+
if (
62+
!localData ||
63+
new Date(localData.lastUpdateDate) < new Date(pu.updatedAt)
64+
) {
65+
update.push(pu.procedureId);
66+
}
67+
});
68+
}
69+
})
70+
);
5571

5672
// Splitt in Chunks & Update
5773
const chunkSize = 100;

0 commit comments

Comments
 (0)