2017-10-12 10:00:28 +08:00
|
|
|
import RedisPubSub from '/imports/startup/server/redis';
|
2016-10-22 02:56:42 +08:00
|
|
|
import { check } from 'meteor/check';
|
2017-10-12 10:00:28 +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-10-12 08:33:57 +08:00
|
|
|
export default function publishVote(credentials, id, pollAnswerId) { // TODO discuss location
|
2016-10-21 21:21:09 +08:00
|
|
|
const REDIS_CONFIG = Meteor.settings.redis;
|
2017-10-12 08:33:57 +08:00
|
|
|
const CHANNEL = REDIS_CONFIG.channels.toAkkaApps;
|
|
|
|
const EVENT_NAME = 'RespondToPollReqMsg';
|
2016-10-21 21:21:09 +08:00
|
|
|
|
2017-06-06 03:12:06 +08:00
|
|
|
const { meetingId, requesterUserId } = credentials;
|
2016-10-22 02:56:42 +08:00
|
|
|
|
|
|
|
const currentPoll = Polls.findOne({
|
|
|
|
users: requesterUserId,
|
2017-06-03 03:25:02 +08:00
|
|
|
meetingId,
|
2017-10-12 08:33:57 +08:00
|
|
|
'answers.id': pollAnswerId,
|
|
|
|
id,
|
2016-10-22 02:56:42 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
check(meetingId, String);
|
|
|
|
check(requesterUserId, String);
|
|
|
|
check(pollAnswerId, Number);
|
|
|
|
check(currentPoll.meetingId, String);
|
|
|
|
|
2017-06-03 03:25:02 +08:00
|
|
|
const payload = {
|
2017-10-12 08:33:57 +08:00
|
|
|
requesterId: requesterUserId,
|
|
|
|
pollId: currentPoll.id,
|
|
|
|
questionId: 0,
|
|
|
|
answerId: pollAnswerId,
|
2016-10-22 02:56:42 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const selector = {
|
|
|
|
users: requesterUserId,
|
2017-06-03 03:25:02 +08:00
|
|
|
meetingId,
|
2017-10-12 08:33:57 +08:00
|
|
|
'answers.id': pollAnswerId,
|
2016-10-22 02:56:42 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const modifier = {
|
|
|
|
$pull: {
|
2016-10-21 21:21:09 +08:00
|
|
|
users: requesterUserId,
|
2016-10-22 02:56:42 +08:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2017-06-06 03:12:06 +08:00
|
|
|
const cb = (err) => {
|
2016-10-22 02:56:42 +08:00
|
|
|
if (err) {
|
2017-10-13 03:07:02 +08:00
|
|
|
return Logger.error(`Updating Polls collection: ${err}`);
|
2016-10-21 21:21:09 +08:00
|
|
|
}
|
2016-10-22 02:56:42 +08:00
|
|
|
|
2017-10-13 03:07:02 +08:00
|
|
|
return Logger.info(`Updating Polls collection (meetingId: ${meetingId},
|
2017-10-12 08:33:57 +08:00
|
|
|
pollId: ${currentPoll.id}!)`);
|
2016-10-22 02:56:42 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
Polls.update(selector, modifier, cb);
|
2017-10-12 08:33:57 +08:00
|
|
|
|
|
|
|
return RedisPubSub.publishUserMessage(CHANNEL, EVENT_NAME, meetingId, requesterUserId, payload);
|
2016-10-21 21:21:09 +08:00
|
|
|
}
|