1
1
/* eslint no-underscore-dangle: ["error", { "allow": ["_id"] }] */
2
2
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
+
3
16
export default {
4
17
Query : {
5
18
votes : ( parent , { procedure } , { VoteModel } ) =>
6
19
VoteModel . findOne ( { procedure } ) . then ( result => result . voteResults ) ,
7
20
} ,
8
21
9
22
Mutation : {
10
- vote : async ( parent , { procedure, selection } , { VoteModel, user } ) => {
23
+ vote : async (
24
+ parent ,
25
+ { procedure : procedureId , selection } ,
26
+ { VoteModel, ProcedureModel, user } ,
27
+ ) => {
11
28
// 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
+
12
37
let vote = await VoteModel . findOne ( { procedure } ) ;
13
38
if ( ! vote ) {
14
- console . log ( '### Create new Vote Instance' ) ;
15
- vote = await VoteModel . create ( { procedure } ) ;
39
+ vote = await VoteModel . create ( { procedure, state } ) ;
16
40
}
17
41
const hasVoted = vote . users . some ( uId => uId . equals ( user . _id ) ) ;
18
42
if ( ! hasVoted ) {
@@ -31,11 +55,19 @@ export default {
31
55
default :
32
56
break ;
33
57
}
34
- return VoteModel . findByIdAndUpdate ( vote . _id , voteUpdate , {
35
- new : true ,
36
- } ) . then ( result => result . voteResults ) ;
58
+ await VoteModel . findByIdAndUpdate ( vote . _id , { ...voteUpdate , state } ) ;
37
59
}
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 } ) ;
39
71
} ,
40
72
} ,
41
73
} ;
0 commit comments