|
| 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