@@ -16,17 +16,32 @@ const statesCompleted = [
16
16
17
17
export default {
18
18
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
+ } ,
27
31
} ,
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 } } ) ,
30
45
} ,
31
46
32
47
Mutation : {
@@ -35,6 +50,9 @@ export default {
35
50
{ procedure : procedureId , selection } ,
36
51
{ VoteModel, ProcedureModel, user } ,
37
52
) => {
53
+ if ( ! user ) {
54
+ throw new Error ( 'No Auth!' ) ;
55
+ }
38
56
// TODO check if procedure is votable
39
57
const procedure = await ProcedureModel . findById ( procedureId ) ;
40
58
let state ;
@@ -69,15 +87,29 @@ export default {
69
87
}
70
88
return VoteModel . aggregate ( [
71
89
{ $match : { procedure : procedure . _id } } ,
90
+ { $addFields : { voted : { $in : [ user . _id , '$users' ] } } } ,
72
91
{
73
92
$group : {
74
93
_id : '$procedure' ,
75
94
yes : { $sum : '$voteResults.yes' } ,
76
95
no : { $sum : '$voteResults.no' } ,
77
96
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
+ } ,
78
109
} ,
79
110
} ,
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 } } ) ;
81
113
} ,
82
114
} ,
83
115
} ;
0 commit comments