Changes poll api to the correct conventions

This commit is contained in:
Gabriel Carvalho de Campes 2016-10-21 15:11:28 -02:00
parent a7335633d4
commit 0a873c302c
8 changed files with 92 additions and 55 deletions

View File

@ -1,9 +1,9 @@
import RedisPubSub from '/imports/startup/server/redis'; import RedisPubSub from '/imports/startup/server/redis';
import handleShowResult from './handlers/showResult'; import handlePollStopped from './handlers/pollStopped';
import handlePollStarted from './handlers/pollStarted'; import handlePollStarted from './handlers/pollStarted';
import handleUserVoted from './handlers/userVoted'; import handleUserVoted from './handlers/userVoted';
RedisPubSub.on('poll_show_result_message', handleShowResult); RedisPubSub.on('poll_show_result_message', handlePollStopped);
RedisPubSub.on('poll_started_message', handlePollStarted); RedisPubSub.on('poll_started_message', handlePollStarted);
RedisPubSub.on('poll_stopped_message', handleShowResult); RedisPubSub.on('poll_stopped_message', handlePollStopped);
RedisPubSub.on('user_voted_poll_message', handleUserVoted); RedisPubSub.on('user_voted_poll_message', handleUserVoted);

View File

@ -19,14 +19,18 @@ export default function pollStarted({ payload }) {
}); });
if (documentExists) { if (documentExists) {
const users = Users.find({ const selector = {
meetingId: meetingId, meetingId: meetingId,
}, { };
const options = {
fields: { fields: {
'user.userid': 1, 'user.userid': 1,
_id: 0, _id: 0,
}, },
}).fetch(); };
const users = Users.find(selector, options).fetch();
addPoll(poll, requesterId, users, meetingId); addPoll(poll, requesterId, users, meetingId);
} }

View File

@ -1,15 +1,22 @@
import { check } from 'meteor/check'; import { check } from 'meteor/check';
import removePoll from '../modifiers/removePoll';
import clearPolls from '../modifiers/clearPolls'; import clearPolls from '../modifiers/clearPolls';
export default function pollStopped({ payload }) { export default function pollStopped({ payload }) {
check(payload, Object); check(payload, Object);
console.log(payload);
const meetingId = payload.meeting_id; const meetingId = payload.meeting_id;
const pollId = payload.poll.id; const poll = payload.poll;
check(meetingId, String); check(meetingId, String);
check(pollId, String);
clearPolls(meetingId, pollId); if (poll) {
const pollId = poll.id;
check(pollId, String);
removePoll(meetingId, pollId);
} else {
clearPolls(meetingId);
}
} }

View File

@ -30,15 +30,20 @@ export default function publishVote(credentials, pollId, pollAnswerId) { //TODO
answer_id: pollAnswerId, answer_id: pollAnswerId,
}, },
}; };
Polls.update({
const selector = {
users: requesterUserId, users: requesterUserId,
meetingId: meetingId, meetingId: meetingId,
'poll.answers.id': pollAnswerId, 'poll.answers.id': pollAnswerId,
}, { };
const modifier = {
$pull: { $pull: {
users: requesterUserId, users: requesterUserId,
}, },
}); };
Polls.update(selector, modifier);
message = appendMessageHeader(eventName, message); message = appendMessageHeader(eventName, message);
logger.info('publishing Poll response to redis'); logger.info('publishing Poll response to redis');
return publish(REDIS_CONFIG.channels.toBBBApps.polling, message); return publish(REDIS_CONFIG.channels.toBBBApps.polling, message);

View File

@ -1,37 +1,37 @@
import Polls from '/imports/api/polls'; import Polls from '/imports/api/polls';
import { logger } from '/imports/startup/server/logger'; import Logger from '/imports/startup/server/logger';
import { check } from 'meteor/check'; import { check } from 'meteor/check';
export default function addPoll(poll, requesterId, users, meetingId) { export default function addPoll(poll, requesterId, users, meetingId) {
check(poll, Object); check(poll, Object);
check(requesterId, String); check(requesterId, String);
check(users, Array); check(users, Array);
check(meetingId, String);
if (meetingId) {
check(meetingId, String);
}
const userIds = users.map(user => user.user.userid); const userIds = users.map(user => user.user.userid);
// adding the initial number of votes for each answer const modifier = {
// _answers = poll.answers; meetingId,
// _answers_length = _answers.length; poll,
// for (j = 0; j < _answers_length; j++) {
// answer = _answers[j];
// answer.num_votes = 0;
// }
// adding the initial number of responders and respondents to the poll, which will be displayed
// for presenter (in HTML5 client) when they start the poll
numResponders = -1;
numRespondents = -1;
// adding all together and inserting into the Polls collection
const newPoll = {
meetingId: meetingId,
poll: poll,
requester: requesterId, requester: requesterId,
users: userIds, users: userIds,
num_responders: -1,
num_respondents: -1,
}; };
logger.info(`added poll _id=[${poll.id}]:meetingId=[${meetingId}].`);
return Polls.insert(newPoll); const cb = (err, numChanged) => {
if (err != null) {
return Logger.error(`Adding poll to collection: ${err}`);
}
const { insertedId } = numChanged;
if (insertedId) {
return Logger.info(`Added poll id=${poll.id}`);
}
return Logger.info(`Added poll id=${poll.id}`);
};
return Polls.insert(modifier, cb);
}; };

View File

@ -1,18 +1,10 @@
import Polls from '/imports/api/polls'; import Polls from '/imports/api/polls';
import { check } from 'meteor/check'; import Logger from '/imports/startup/server/logger';
import { logger } from '/imports/startup/server/logger';
export default function clearPolls(meetingId, pollId) { export default function clearPolls(meetingId) {
//TODO make it so you can delete the polls based only on meetingId if (meetingId) {
check(meetingId, String); return Polls.remove({ meetingId, }, Logger.info(`Cleared Polls (${meetingId})`));
check(pollId, String);
if (meetingId && pollId) {
return Polls.remove({
meetingId: meetingId,
'poll.id': pollId,
}, logger.info(`cleared Polls Collection (meetingId: ${meetingId}, pollId: ${pollId}!)`));
} else { } else {
return Polls.remove({}, logger.info('cleared Polls Collection (all meetings)!')); return Polls.remove({}, Logger.info('Cleared Polls (all)'));
} }
}; };

View File

@ -0,0 +1,17 @@
import Polls from '/imports/api/polls';
import { check } from 'meteor/check';
import Logger from '/imports/startup/server/logger';
export default function removePoll(meetingId, pollId) {
check(meetingId, String);
check(pollId, String);
const selector = {
meetingId,
'poll.id': pollId,
};
const cb = () => { Logger.info(`Removed Poll: (meetingId: ${meetingId}) (pollId: ${pollId})`); };
return Polls.remove(selector, cb);
};

View File

@ -1,6 +1,6 @@
import Polls from '/imports/api/polls'; import Polls from '/imports/api/polls';
import { check } from 'meteor/check'; import { check } from 'meteor/check';
import { logger } from '/imports/startup/server/logger'; import Logger from '/imports/startup/server/logger';
export default function updatePoll(poll, meetingId, requesterId) { export default function updatePoll(poll, meetingId, requesterId) {
check(meetingId, String); check(meetingId, String);
@ -21,15 +21,27 @@ export default function updatePoll(poll, meetingId, requesterId) {
check(numResponders, Number); check(numResponders, Number);
check(numRespondents, Number); check(numRespondents, Number);
return Polls.update({ const selector = {
meetingId: meetingId, meetingId,
requester: requesterId, requester: requesterId,
poll: { id: id }, 'poll.id': id,
}, { };
const modifier = {
$set: { $set: {
poll: { answers: answers }, poll: { answers: answers },
poll: { num_responders: numResponders }, poll: { num_responders: numResponders },
poll: { num_respondents: numRespondents }, poll: { num_respondents: numRespondents },
}, },
}, logger.info(`updating Polls Collection (meetingId: ${meetingId}, pollId: ${id}!)`)); };
const cb = (err, numChanged) => {
if (err != null) {
return Logger.error(`updating Polls collection: ${err}`);
}
Logger.info(`updating Polls collection (meetingId: ${meetingId}, pollId: ${id}!)`);
};
return Polls.update(selector, modifier, cb);
}; };