2017-09-25 22:09:09 +02:00
|
|
|
import { addCallback, addGraphQLSchema, addGraphQLResolvers, addGraphQLMutation, Utils, registerSetting, getSetting } from 'meteor/vulcan:core';
|
|
|
|
import { voteOnItem } from '../modules/vote.js';
|
2017-09-04 20:56:03 +09:00
|
|
|
import { VoteableCollections } from '../modules/make_voteable.js';
|
|
|
|
import { createError } from 'apollo-errors';
|
2017-09-25 22:09:09 +02:00
|
|
|
import Votes from '../modules/votes/collection.js';
|
|
|
|
|
2017-09-04 20:56:03 +09:00
|
|
|
|
|
|
|
function CreateVoteableUnionType() {
|
2017-09-25 22:09:09 +02:00
|
|
|
const voteableSchema = VoteableCollections.length ? `union Voteable = ${VoteableCollections.map(collection => collection.typeName).join(' | ')}` : '';
|
|
|
|
addGraphQLSchema(voteableSchema);
|
2017-09-04 20:56:03 +09:00
|
|
|
return {}
|
|
|
|
}
|
|
|
|
addCallback('graphql.init.before', CreateVoteableUnionType);
|
2016-11-22 18:39:50 -05:00
|
|
|
|
|
|
|
|
2017-01-29 11:17:00 +09:00
|
|
|
const resolverMap = {
|
2017-09-04 20:56:03 +09:00
|
|
|
Voteable: {
|
2017-01-29 11:17:00 +09:00
|
|
|
__resolveType(obj, context, info){
|
2017-09-04 20:56:03 +09:00
|
|
|
return obj.__typename;
|
2017-01-29 11:17:00 +09:00
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
2017-01-09 15:42:24 +09:00
|
|
|
|
2017-07-13 17:37:59 +09:00
|
|
|
addGraphQLResolvers(resolverMap);
|
2017-01-09 15:42:24 +09:00
|
|
|
|
2017-09-25 22:09:09 +02:00
|
|
|
addGraphQLMutation('vote(documentId: String, operationType: String, collectionName: String) : Voteable');
|
2016-11-22 18:39:50 -05:00
|
|
|
|
|
|
|
const voteResolver = {
|
|
|
|
Mutation: {
|
2017-09-25 22:09:09 +02:00
|
|
|
async vote(root, {documentId, operationType, collectionName}, context) {
|
2017-09-04 20:56:03 +09:00
|
|
|
|
2017-09-25 22:09:09 +02:00
|
|
|
const { currentUser } = context;
|
2017-01-09 15:42:24 +09:00
|
|
|
const collection = context[Utils.capitalize(collectionName)];
|
2017-09-25 22:09:09 +02:00
|
|
|
|
|
|
|
// query for document being voted on
|
|
|
|
const document = await collection.queryOne(documentId, {
|
|
|
|
fragmentText: `
|
|
|
|
fragment DocumentVoteFragment on ${collection.typeName} {
|
|
|
|
__typename
|
|
|
|
_id
|
|
|
|
currentUserVotes{
|
|
|
|
_id
|
|
|
|
voteType
|
|
|
|
power
|
|
|
|
}
|
|
|
|
baseScore
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
context
|
|
|
|
});
|
|
|
|
|
|
|
|
if (context.Users.canDo(currentUser, `${collectionName.toLowerCase()}.${operationType}`)) {
|
|
|
|
|
|
|
|
// put document through voteOnItem and get result
|
|
|
|
const voteResult = voteOnItem(collection, document, currentUser, operationType);
|
2017-09-04 20:56:03 +09:00
|
|
|
|
2017-09-25 22:09:09 +02:00
|
|
|
// get new version of document
|
|
|
|
const newDocument = voteResult.document;
|
|
|
|
newDocument.__typename = collection.typeName;
|
|
|
|
|
|
|
|
// get created or cancelled vote
|
|
|
|
const vote = voteResult.vote;
|
|
|
|
|
|
|
|
if (operationType === 'cancelVote' && vote) {
|
|
|
|
// if a vote has been cancelled, delete it
|
|
|
|
Votes.remove(vote._id);
|
|
|
|
} else {
|
|
|
|
// if a vote has been created, insert it
|
|
|
|
delete vote.__typename;
|
|
|
|
Votes.insert(vote);
|
|
|
|
}
|
|
|
|
|
|
|
|
// in any case, return the document that was voted on
|
|
|
|
return newDocument;
|
2017-09-04 20:56:03 +09:00
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
const VoteError = createError('cannot_vote');
|
|
|
|
throw new VoteError();
|
|
|
|
|
|
|
|
}
|
2016-11-22 18:39:50 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2017-07-13 17:37:59 +09:00
|
|
|
addGraphQLResolvers(voteResolver);
|