|
| 1 | +/* eslint no-underscore-dangle: ["error", { "allow": ["_id"] }] */ |
| 2 | +import { Types } from 'mongoose'; |
| 3 | + |
| 4 | +import Activity from './Activity'; |
| 5 | + |
| 6 | +const statesVoting = ['Beschlussempfehlung liegt vor']; |
| 7 | +const statesCompleted = [ |
| 8 | + 'Zurückgezogen', |
| 9 | + 'Erledigt durch Ablauf der Wahlperiode', |
| 10 | + 'Einbringung abgelehnt', |
| 11 | + 'Für erledigt erklärt', |
| 12 | + 'Bundesrat hat zugestimmt', |
| 13 | + 'Abgelehnt', |
| 14 | + 'Verkündet', |
| 15 | + 'Verabschiedet', |
| 16 | + 'Zusammengeführt mit... (siehe Vorgangsablauf)', |
| 17 | + 'Bundesrat hat Vermittlungsausschuss nicht angerufen', |
| 18 | +]; |
| 19 | + |
| 20 | +export default { |
| 21 | + Query: { |
| 22 | + votes: (parent, { procedure }, { VoteModel, user }) => |
| 23 | + VoteModel.aggregate([ |
| 24 | + { $match: { procedure: Types.ObjectId(procedure) } }, |
| 25 | + { $addFields: { voted: { $in: [user ? user._id : false, '$users'] } } }, |
| 26 | + { |
| 27 | + $group: { |
| 28 | + _id: '$procedure', |
| 29 | + yes: { $sum: '$voteResults.yes' }, |
| 30 | + no: { $sum: '$voteResults.no' }, |
| 31 | + abstination: { $sum: '$voteResults.abstination' }, |
| 32 | + voted: { $first: '$voted' }, |
| 33 | + }, |
| 34 | + }, |
| 35 | + { |
| 36 | + $project: { |
| 37 | + _id: 1, |
| 38 | + voted: 1, |
| 39 | + voteResults: { |
| 40 | + yes: '$yes', |
| 41 | + no: '$no', |
| 42 | + abstination: '$abstination', |
| 43 | + }, |
| 44 | + }, |
| 45 | + }, |
| 46 | + ]).then(result => |
| 47 | + result[0] || { voted: false, voteResults: { yes: null, no: null, abstination: null } }), |
| 48 | + }, |
| 49 | + |
| 50 | + Mutation: { |
| 51 | + vote: async ( |
| 52 | + parent, |
| 53 | + { procedure: procedureId, selection }, |
| 54 | + { |
| 55 | + VoteModel, ProcedureModel, ActivityModel, user, |
| 56 | + }, |
| 57 | + ) => { |
| 58 | + if (!user) { |
| 59 | + throw new Error('No Auth!'); |
| 60 | + } |
| 61 | + // TODO check if procedure is votable |
| 62 | + const procedure = await ProcedureModel.findById(procedureId); |
| 63 | + let state; |
| 64 | + if (statesVoting.some(s => s === procedure.currentStatus)) { |
| 65 | + state = 'VOTING'; |
| 66 | + } else if (statesCompleted.some(s => s === procedure.currentStatus)) { |
| 67 | + state = 'COMPLETED'; |
| 68 | + } |
| 69 | + |
| 70 | + let vote = await VoteModel.findOne({ procedure }); |
| 71 | + if (!vote) { |
| 72 | + vote = await VoteModel.create({ procedure, state }); |
| 73 | + } |
| 74 | + const hasVoted = vote.users.some(uId => uId.equals(user._id)); |
| 75 | + if (!hasVoted) { |
| 76 | + const voteUpdate = { $push: { users: user } }; |
| 77 | + switch (selection) { |
| 78 | + case 'YES': |
| 79 | + voteUpdate.$inc = { 'voteResults.yes': 1 }; |
| 80 | + break; |
| 81 | + case 'NO': |
| 82 | + voteUpdate.$inc = { 'voteResults.no': 1 }; |
| 83 | + break; |
| 84 | + case 'ABSTINATION': |
| 85 | + voteUpdate.$inc = { 'voteResults.abstination': 1 }; |
| 86 | + break; |
| 87 | + |
| 88 | + default: |
| 89 | + break; |
| 90 | + } |
| 91 | + await VoteModel.findByIdAndUpdate(vote._id, { ...voteUpdate, state }); |
| 92 | + } |
| 93 | + await Activity.Mutation.increaseActivity( |
| 94 | + parent, |
| 95 | + { procedureId }, |
| 96 | + { ProcedureModel, ActivityModel, user }, |
| 97 | + ); |
| 98 | + return VoteModel.aggregate([ |
| 99 | + { $match: { procedure: procedure._id } }, |
| 100 | + { $addFields: { voted: { $in: [user._id, '$users'] } } }, |
| 101 | + { |
| 102 | + $group: { |
| 103 | + _id: '$procedure', |
| 104 | + yes: { $sum: '$voteResults.yes' }, |
| 105 | + no: { $sum: '$voteResults.no' }, |
| 106 | + abstination: { $sum: '$voteResults.abstination' }, |
| 107 | + voted: { $first: '$voted' }, |
| 108 | + }, |
| 109 | + }, |
| 110 | + { |
| 111 | + $project: { |
| 112 | + _id: 1, |
| 113 | + voted: 1, |
| 114 | + voteResults: { |
| 115 | + yes: '$yes', |
| 116 | + no: '$no', |
| 117 | + abstination: '$abstination', |
| 118 | + }, |
| 119 | + }, |
| 120 | + }, |
| 121 | + ]).then(result => |
| 122 | + result[0] || { voted: false, voteResults: { yes: null, no: null, abstination: null } }); |
| 123 | + }, |
| 124 | + }, |
| 125 | +}; |
0 commit comments