Skip to content

Commit fd2c826

Browse files
committed
add vote process to server
1 parent 4f72929 commit fd2c826

File tree

6 files changed

+80
-11
lines changed

6 files changed

+80
-11
lines changed

.eslintrc.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
module.exports = {
2-
extends: 'airbnb-base',
3-
};
4-
2+
extends: "airbnb-base",
3+
rules: { "newline-per-chained-call": [2] }
4+
};

src/graphql/resolvers/Vote.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/* eslint no-underscore-dangle: ["error", { "allow": ["_id"] }] */
2+
3+
export default {
4+
Query: {
5+
votes: (parent, { procedure }, { VoteModel }) =>
6+
VoteModel.findOne({ procedure }).then(result => result.voteResults),
7+
},
8+
9+
Mutation: {
10+
vote: async (parent, { procedure, selection }, { VoteModel, user }) => {
11+
let vote = await VoteModel.findOne({ procedure });
12+
if (!vote) {
13+
console.log('### Create new Vote Instance');
14+
vote = await VoteModel.create({ procedure });
15+
}
16+
const hasVoted = vote.users.some(uId => uId.equals(user._id));
17+
if (!hasVoted) {
18+
const voteUpdate = { $push: { users: user } };
19+
switch (selection) {
20+
case 'YES':
21+
voteUpdate.$inc = { 'voteResults.yes': 1 };
22+
break;
23+
case 'NO':
24+
voteUpdate.$inc = { 'voteResults.no': 1 };
25+
break;
26+
case 'ABSTINATION':
27+
voteUpdate.$inc = { 'voteResults.abstination': 1 };
28+
break;
29+
30+
default:
31+
break;
32+
}
33+
return VoteModel.findByIdAndUpdate(vote._id, voteUpdate, {
34+
new: true,
35+
}).then(result => result.voteResults);
36+
}
37+
return vote.voteResults;
38+
},
39+
},
40+
};

src/graphql/schemas/Procedure.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,6 @@ enum ProcedureType {
55
HOT
66
}
77
8-
type VoteResult {
9-
yes: Int
10-
no: Int
11-
abstination: Int
12-
notVote: Int
13-
}
14-
158
type Procedure {
169
_id: ID!
1710
title: String!

src/graphql/schemas/Vote.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
export default `
2+
3+
enum VoteSelection {
4+
YES
5+
NO
6+
ABSTINATION
7+
}
8+
9+
type VoteResult {
10+
yes: Int
11+
no: Int
12+
abstination: Int
13+
notVote: Int
14+
}
15+
16+
type Vote {
17+
_id: ID!
18+
voted: Boolean
19+
voteResults: VoteResult
20+
}
21+
22+
type Mutation {
23+
vote(procedure: ID!, selection: VoteSelection!): VoteResult
24+
}
25+
26+
type Query {
27+
votes(procedure: ID!): VoteResult
28+
}
29+
`;

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import auth from './express/auth';
2121
import ProcedureModel from './models/Procedure';
2222
import UserModel from './models/User';
2323
import ActivityModel from './models/Activity';
24+
import VoteModel from './models/Vote';
2425

2526
const app = express();
2627

@@ -60,6 +61,7 @@ app.use(constants.GRAPHQL_PATH, (req, res, next) => {
6061
ProcedureModel,
6162
UserModel,
6263
ActivityModel,
64+
VoteModel,
6365
},
6466
tracing: true,
6567
cacheControl: true,

src/models/Vote.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22
import mongoose, { Schema } from 'mongoose';
33

44
const VoteSchema = new Schema({
5-
procedure: { type: Schema.Types.ObjectId, ref: 'Procedure', required: true },
5+
procedure: {
6+
type: Schema.Types.ObjectId,
7+
ref: 'Procedure',
8+
required: true,
9+
index: { unique: true },
10+
},
611
users: [{ type: Schema.Types.ObjectId, ref: 'User', required: true }],
712
voteResults: {
813
yes: { type: Number, default: 0 },

0 commit comments

Comments
 (0)