2016-10-22 02:56:42 +08:00
|
|
|
import RedisPubSub from '/imports/startup/server/redis';
|
|
|
|
import { check } from 'meteor/check';
|
2016-10-21 21:21:09 +08:00
|
|
|
import Polls from '/imports/api/polls';
|
2016-10-22 02:56:42 +08:00
|
|
|
import Logger from '/imports/startup/server/logger';
|
2016-10-21 21:21:09 +08:00
|
|
|
|
2017-06-03 03:25:02 +08:00
|
|
|
export default function publishVote(credentials, pollId, pollAnswerId) { // TODO discuss location
|
2016-10-21 21:21:09 +08:00
|
|
|
const REDIS_CONFIG = Meteor.settings.redis;
|
2016-10-22 02:56:42 +08:00
|
|
|
const CHANNEL = REDIS_CONFIG.channels.toBBBApps.polling;
|
2016-11-15 02:25:23 +08:00
|
|
|
const EVENT_NAME = 'vote_poll_user_request_message';
|
2016-10-21 21:21:09 +08:00
|
|
|
|
2016-10-22 02:56:42 +08:00
|
|
|
const { meetingId, requesterUserId, requesterToken } = credentials;
|
|
|
|
|
|
|
|
const currentPoll = Polls.findOne({
|
|
|
|
users: requesterUserId,
|
2017-06-03 03:25:02 +08:00
|
|
|
meetingId,
|
2016-10-22 02:56:42 +08:00
|
|
|
'poll.answers.id': pollAnswerId,
|
|
|
|
'poll.id': pollId,
|
|
|
|
});
|
|
|
|
|
|
|
|
check(meetingId, String);
|
|
|
|
check(requesterUserId, String);
|
|
|
|
check(pollAnswerId, Number);
|
|
|
|
check(currentPoll.meetingId, String);
|
|
|
|
|
2017-06-03 03:25:02 +08:00
|
|
|
const payload = {
|
2016-10-22 02:56:42 +08:00
|
|
|
meeting_id: currentPoll.meetingId,
|
|
|
|
user_id: requesterUserId,
|
|
|
|
poll_id: currentPoll.poll.id,
|
|
|
|
question_id: 0,
|
|
|
|
answer_id: pollAnswerId,
|
|
|
|
};
|
|
|
|
|
|
|
|
const selector = {
|
|
|
|
users: requesterUserId,
|
2017-06-03 03:25:02 +08:00
|
|
|
meetingId,
|
2016-10-22 02:56:42 +08:00
|
|
|
'poll.answers.id': pollAnswerId,
|
|
|
|
};
|
|
|
|
|
|
|
|
const modifier = {
|
|
|
|
$pull: {
|
2016-10-21 21:21:09 +08:00
|
|
|
users: requesterUserId,
|
2016-10-22 02:56:42 +08:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const cb = (err, numChanged) => {
|
|
|
|
if (err) {
|
|
|
|
return Logger.error(`Updating Polls collection: ${err}`);
|
2016-10-21 21:21:09 +08:00
|
|
|
}
|
2016-10-22 02:56:42 +08:00
|
|
|
|
|
|
|
Logger.info(`Updating Polls collection (meetingId: ${meetingId},
|
|
|
|
pollId: ${currentPoll.poll.id}!)`);
|
|
|
|
};
|
|
|
|
|
|
|
|
Polls.update(selector, modifier, cb);
|
2016-11-15 02:25:23 +08:00
|
|
|
return RedisPubSub.publish(CHANNEL, EVENT_NAME, payload);
|
2016-10-21 21:21:09 +08:00
|
|
|
}
|