Skip to content

Commit 183a998

Browse files
committed
handle voting status
1 parent 72f44f8 commit 183a998

File tree

2 files changed

+43
-9
lines changed

2 files changed

+43
-9
lines changed

src/graphql/resolvers/Vote.js

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,42 @@
11
/* eslint no-underscore-dangle: ["error", { "allow": ["_id"] }] */
22

3+
const statesVoting = ['Beschlussempfehlung liegt vor'];
4+
const statesCompleted = [
5+
'Zurückgezogen',
6+
'Erledigt durch Ablauf der Wahlperiode',
7+
'Einbringung abgelehnt',
8+
'Für erledigt erklärt',
9+
'Bundesrat hat zugestimmt',
10+
'Abgelehnt',
11+
'Verkündet',
12+
'Verabschiedet',
13+
'Zusammengeführt mit... (siehe Vorgangsablauf)',
14+
];
15+
316
export default {
417
Query: {
518
votes: (parent, { procedure }, { VoteModel }) =>
619
VoteModel.findOne({ procedure }).then(result => result.voteResults),
720
},
821

922
Mutation: {
10-
vote: async (parent, { procedure, selection }, { VoteModel, user }) => {
23+
vote: async (
24+
parent,
25+
{ procedure: procedureId, selection },
26+
{ VoteModel, ProcedureModel, user },
27+
) => {
1128
// TODO check if procedure is votable
29+
const procedure = await ProcedureModel.findById(procedureId);
30+
let state;
31+
if (statesVoting.some(s => s === procedure.currentStatus)) {
32+
state = 'VOTING';
33+
} else if (statesCompleted.some(s => s === procedure.currentStatus)) {
34+
state = 'COMPLETED';
35+
}
36+
1237
let vote = await VoteModel.findOne({ procedure });
1338
if (!vote) {
14-
console.log('### Create new Vote Instance');
15-
vote = await VoteModel.create({ procedure });
39+
vote = await VoteModel.create({ procedure, state });
1640
}
1741
const hasVoted = vote.users.some(uId => uId.equals(user._id));
1842
if (!hasVoted) {
@@ -31,11 +55,19 @@ export default {
3155
default:
3256
break;
3357
}
34-
return VoteModel.findByIdAndUpdate(vote._id, voteUpdate, {
35-
new: true,
36-
}).then(result => result.voteResults);
58+
await VoteModel.findByIdAndUpdate(vote._id, { ...voteUpdate, state });
3759
}
38-
return vote.voteResults;
60+
return VoteModel.aggregate([
61+
{ $match: { procedure: procedure._id } },
62+
{
63+
$group: {
64+
_id: '$procedure',
65+
yes: { $sum: '$voteResults.yes' },
66+
no: { $sum: '$voteResults.no' },
67+
abstination: { $sum: '$voteResults.abstination' },
68+
},
69+
},
70+
]).then(result => result[0] || { yes: null, no: null, abstination: null });
3971
},
4072
},
4173
};

src/models/Vote.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@ const VoteSchema = new Schema({
66
type: Schema.Types.ObjectId,
77
ref: 'Procedure',
88
required: true,
9-
index: { unique: true },
109
},
11-
users: [{ type: Schema.Types.ObjectId, ref: 'User', required: true }],
10+
state: { type: String, enum: ['VOTING', 'COMPLETED'], required: true },
11+
users: [{ type: Schema.Types.ObjectId, ref: 'User' }],
1212
voteResults: {
1313
yes: { type: Number, default: 0 },
1414
no: { type: Number, default: 0 },
1515
abstination: { type: Number, default: 0 },
1616
},
1717
});
1818

19+
VoteSchema.index({ procedure: 1, state: 1 }, { unique: true });
20+
1921
export default mongoose.model('Vote', VoteSchema);

0 commit comments

Comments
 (0)