@@ -16,17 +16,32 @@ const statesCompleted = [
1616
1717export default {
1818 Query : {
19- votes : ( parent , { procedure } , { VoteModel } ) => VoteModel . aggregate ( [
20- { $match : { procedure : Types . ObjectId ( procedure ) } } ,
21- {
22- $group : {
23- _id : '$procedure' ,
24- yes : { $sum : '$voteResults.yes' } ,
25- no : { $sum : '$voteResults.no' } ,
26- abstination : { $sum : '$voteResults.abstination' } ,
19+ votes : ( parent , { procedure } , { VoteModel, user } ) =>
20+ VoteModel . aggregate ( [
21+ { $match : { procedure : Types . ObjectId ( procedure ) } } ,
22+ { $addFields : { voted : { $in : [ user . _id , '$users' ] } } } ,
23+ {
24+ $group : {
25+ _id : '$procedure' ,
26+ yes : { $sum : '$voteResults.yes' } ,
27+ no : { $sum : '$voteResults.no' } ,
28+ abstination : { $sum : '$voteResults.abstination' } ,
29+ voted : { $first : '$voted' } ,
30+ } ,
2731 } ,
28- } ,
29- ] ) . then ( result => result [ 0 ] || { yes : null , no : null , abstination : null } ) ,
32+ {
33+ $project : {
34+ _id : 1 ,
35+ voted : 1 ,
36+ voteResults : {
37+ yes : '$yes' ,
38+ no : '$no' ,
39+ abstination : '$abstination' ,
40+ } ,
41+ } ,
42+ } ,
43+ ] ) . then ( result =>
44+ result [ 0 ] || { voted : false , voteResults : { yes : null , no : null , abstination : null } } ) ,
3045 } ,
3146
3247 Mutation : {
@@ -35,6 +50,9 @@ export default {
3550 { procedure : procedureId , selection } ,
3651 { VoteModel, ProcedureModel, user } ,
3752 ) => {
53+ if ( ! user ) {
54+ throw new Error ( 'No Auth!' ) ;
55+ }
3856 // TODO check if procedure is votable
3957 const procedure = await ProcedureModel . findById ( procedureId ) ;
4058 let state ;
@@ -69,15 +87,29 @@ export default {
6987 }
7088 return VoteModel . aggregate ( [
7189 { $match : { procedure : procedure . _id } } ,
90+ { $addFields : { voted : { $in : [ user . _id , '$users' ] } } } ,
7291 {
7392 $group : {
7493 _id : '$procedure' ,
7594 yes : { $sum : '$voteResults.yes' } ,
7695 no : { $sum : '$voteResults.no' } ,
7796 abstination : { $sum : '$voteResults.abstination' } ,
97+ voted : { $first : '$voted' } ,
98+ } ,
99+ } ,
100+ {
101+ $project : {
102+ _id : 1 ,
103+ voted : 1 ,
104+ voteResults : {
105+ yes : '$yes' ,
106+ no : '$no' ,
107+ abstination : '$abstination' ,
108+ } ,
78109 } ,
79110 } ,
80- ] ) . then ( result => result [ 0 ] || { yes : null , no : null , abstination : null } ) ;
111+ ] ) . then ( result =>
112+ result [ 0 ] || { voted : false , voteResults : { yes : null , no : null , abstination : null } } ) ;
81113 } ,
82114 } ,
83115} ;
0 commit comments