Skip to content

Commit 730f78e

Browse files
authored
Merge pull request #34 from demokratie-live/sprint#7/abstimmung
add named voting results
2 parents cd106e9 + 745517d commit 730f78e

File tree

8 files changed

+150
-42
lines changed

8 files changed

+150
-42
lines changed

scripts/importAll.js

Lines changed: 0 additions & 39 deletions
This file was deleted.

src/graphql/queries/getAllProcedures.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import gql from 'graphql-tag';
22

33
export default gql`
4-
query allProcedures() {
5-
allProcedures() {
4+
query allProcedures {
5+
allProcedures {
66
title
77
procedureId
88
type
@@ -16,6 +16,8 @@ export default gql`
1616
initiator
1717
decision {
1818
tenor
19+
type
20+
comment
1921
}
2022
date
2123
}

src/graphql/queries/getProcedures.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ export default gql`
1616
initiator
1717
decision {
1818
tenor
19+
type
20+
comment
1921
}
2022
date
2123
}

src/graphql/schemas/Procedure.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ enum ProcedureType {
55
HOT
66
}
77
8+
type VoteResult {
9+
yes: Int
10+
no: Int
11+
abstination: Int
12+
notVote: Int
13+
}
14+
815
type Procedure {
916
_id: ID!
1017
title: String!
@@ -19,6 +26,7 @@ type Procedure {
1926
submissionDate: Date
2027
activityIndex: ActivityIndex
2128
importantDocuments: [Document]
29+
voteResults: VoteResult
2230
}
2331
2432
type Query {

src/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import typeDefs from './graphql/schemas';
1313
import resolvers from './graphql/resolvers';
1414

1515
import webhook from './scripts/webhook';
16+
// import importAll from './scripts/importAll';
1617

1718
import auth from './express/auth';
1819

@@ -83,6 +84,9 @@ app.post('/webhooks/bundestagio/update', async (req, res) => {
8384
}
8485
});
8586

87+
// Darf in Production nicht ausführbar sein!
88+
// app.get('/webhooks/bundestagio/import-all', importAll);
89+
8690
const graphqlServer = createServer(app);
8791

8892
graphqlServer.listen(constants.PORT, (err) => {

src/models/Procedure.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ const ProcedureSchema = new Schema(
2121
lastUpdateDate: Date,
2222
subjectGroups: [String],
2323
importantDocuments: [Document],
24+
voteResults: {
25+
yes: { type: Number, required: true },
26+
no: { type: Number, required: true },
27+
abstination: { type: Number, required: true },
28+
notVote: { type: Number, required: true },
29+
},
2430
},
2531
{ timestamps: true },
2632
);

src/scripts/import.js

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,24 @@
1+
import _ from 'lodash';
2+
13
import createClient from '../graphql/client';
24
import Procedure from '../models/Procedure';
35
import getProcedures from '../graphql/queries/getProcedures';
46

7+
const deputiesNumber = {
8+
8: 518,
9+
9: 519,
10+
10: 520,
11+
11: 663,
12+
12: 662,
13+
13: 672,
14+
14: 665,
15+
15: 601,
16+
16: 611,
17+
17: 620,
18+
18: 630,
19+
19: 709,
20+
};
21+
522
export default async (procedureIds) => {
623
const client = createClient();
724
// Start Importing
@@ -21,14 +38,39 @@ export default async (procedureIds) => {
2138
} else if (newBIoProcedure.currentStatus === 'Zurückgezogen') {
2239
newBIoProcedure.voteDate = lastHistory.date;
2340
}
41+
let voteResults;
42+
bIoProcedure.history.some((h) => {
43+
if (h.decision) {
44+
return h.decision.some((decision) => {
45+
if (decision.type === 'Namentliche Abstimmung') {
46+
const voteResultsRegEx = /(\d{1,3}:\d{1,3}:\d{1,3})/;
47+
const vResults = decision.comment.match(voteResultsRegEx)[0].split(':');
48+
voteResults = {
49+
yes: vResults[0],
50+
no: vResults[1],
51+
abstination: vResults[2],
52+
notVote:
53+
deputiesNumber[bIoProcedure.period] -
54+
vResults.reduce((pv, cv) => pv + parseInt(cv, 10), 0),
55+
};
56+
return true;
57+
}
58+
return false;
59+
});
60+
}
61+
return false;
62+
});
63+
newBIoProcedure.voteResults = voteResults;
2464

2565
newBIoProcedure.lastUpdateDate = lastHistory.date;
2666

2767
newBIoProcedure.submissionDate = newBIoProcedure.history[0].date;
2868
}
2969
return Procedure.findOneAndUpdate(
3070
{ procedureId: newBIoProcedure.procedureId },
31-
newBIoProcedure,
71+
_(newBIoProcedure)
72+
.omitBy(x => _.isNull(x) || _.isUndefined(x))
73+
.value(),
3274
{
3375
upsert: true,
3476
},

src/scripts/importAll.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import _ from 'lodash';
2+
3+
import createClient from '../graphql/client';
4+
import Procedure from '../models/Procedure';
5+
import getAllProcedures from '../graphql/queries/getAllProcedures';
6+
7+
const deputiesNumber = {
8+
8: 518,
9+
9: 519,
10+
10: 520,
11+
11: 663,
12+
12: 662,
13+
13: 672,
14+
14: 665,
15+
15: 601,
16+
16: 611,
17+
17: 620,
18+
18: 630,
19+
19: 709,
20+
};
21+
22+
export default async () => {
23+
const client = createClient();
24+
console.log('Start Importing');
25+
const { data: { allProcedures } } = await client.query({
26+
query: getAllProcedures,
27+
// variables: {},
28+
});
29+
console.log(allProcedures.map(({ procedureId }) => procedureId));
30+
console.log('Start Inserting');
31+
// Start Inserting
32+
await allProcedures.forEach(async (bIoProcedure) => {
33+
const newBIoProcedure = { ...bIoProcedure };
34+
if (bIoProcedure && bIoProcedure.history) {
35+
const [lastHistory] = newBIoProcedure.history.slice(-1);
36+
const btWithDecisions = bIoProcedure.history.filter(({ assignment, initiator }) => assignment === 'BT' && initiator === '2. Beratung');
37+
if (btWithDecisions.length > 0) {
38+
newBIoProcedure.voteDate = new Date(btWithDecisions.pop().date);
39+
} else if (newBIoProcedure.currentStatus === 'Zurückgezogen') {
40+
newBIoProcedure.voteDate = lastHistory.date;
41+
}
42+
let voteResults;
43+
bIoProcedure.history.some((h) => {
44+
if (h.decision) {
45+
return h.decision.some((decision) => {
46+
if (decision.type === 'Namentliche Abstimmung') {
47+
const voteResultsRegEx = /(\d{1,3}:\d{1,3}:\d{1,3})/;
48+
const vResults = decision.comment.match(voteResultsRegEx)[0].split(':');
49+
voteResults = {
50+
yes: vResults[0],
51+
no: vResults[1],
52+
abstination: vResults[2],
53+
notVote:
54+
deputiesNumber[bIoProcedure.period] -
55+
vResults.reduce((pv, cv) => pv + parseInt(cv, 10), 0),
56+
};
57+
return true;
58+
}
59+
return false;
60+
});
61+
}
62+
return false;
63+
});
64+
65+
newBIoProcedure.voteResults = voteResults;
66+
67+
newBIoProcedure.lastUpdateDate = lastHistory.date;
68+
69+
newBIoProcedure.submissionDate = newBIoProcedure.history[0].date;
70+
}
71+
72+
await Procedure.findOneAndUpdate(
73+
{ procedureId: newBIoProcedure.procedureId },
74+
_(newBIoProcedure)
75+
.omitBy(x => _.isNull(x) || _.isUndefined(x))
76+
.value(),
77+
{
78+
upsert: true,
79+
},
80+
);
81+
});
82+
console.log('Imported everything!'); // eslint-disable-line
83+
};

0 commit comments

Comments
 (0)