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';
|
2020-02-07 04:47:28 +08:00
|
|
|
import { extractCredentials } from '/imports/api/common/server/helpers';
|
2016-10-21 21:21:09 +08:00
|
|
|
|
2020-09-29 03:28:35 +08:00
|
|
|
export default function publishVote(pollId, pollAnswerId) {
|
2018-01-08 08:24:05 +08:00
|
|
|
const REDIS_CONFIG = Meteor.settings.private.redis;
|
2017-10-12 08:33:57 +08:00
|
|
|
const CHANNEL = REDIS_CONFIG.channels.toAkkaApps;
|
|
|
|
const EVENT_NAME = 'RespondToPollReqMsg';
|
2020-02-07 04:47:28 +08:00
|
|
|
const { meetingId, requesterUserId } = extractCredentials(this.userId);
|
2020-09-29 03:28:35 +08:00
|
|
|
|
|
|
|
check(pollAnswerId, Number);
|
|
|
|
check(pollId, String);
|
|
|
|
|
2020-11-05 22:19:36 +08:00
|
|
|
const allowedToVote = Polls.findOne({ id: pollId, users: { $in: [requesterUserId] } }, {
|
|
|
|
fields: {
|
2020-10-29 02:33:09 +08:00
|
|
|
users: 1,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-11-05 22:19:36 +08:00
|
|
|
if (!allowedToVote) {
|
2020-11-06 01:02:31 +08:00
|
|
|
Logger.info(`Poll User={${requesterUserId}} has already voted in PollId={${pollId}}`);
|
|
|
|
return null;
|
2020-11-05 22:19:36 +08:00
|
|
|
}
|
2020-10-29 02:33:09 +08:00
|
|
|
|
2020-09-29 03:28:35 +08:00
|
|
|
const selector = {
|
2016-10-22 02:56:42 +08:00
|
|
|
users: requesterUserId,
|
2017-06-03 03:25:02 +08:00
|
|
|
meetingId,
|
2017-10-12 08:33:57 +08:00
|
|
|
'answers.id': pollAnswerId,
|
2020-09-29 03:28:35 +08:00
|
|
|
};
|
2016-10-22 02:56:42 +08:00
|
|
|
|
2017-06-03 03:25:02 +08:00
|
|
|
const payload = {
|
2017-10-12 08:33:57 +08:00
|
|
|
requesterId: requesterUserId,
|
2020-09-29 03:28:35 +08:00
|
|
|
pollId,
|
2017-10-12 08:33:57 +08:00
|
|
|
questionId: 0,
|
2021-02-04 22:43:04 +08:00
|
|
|
answerIds: [pollAnswerId],
|
2016-10-22 02:56:42 +08:00
|
|
|
};
|
|
|
|
|
2020-09-29 03:28:35 +08:00
|
|
|
/*
|
|
|
|
We keep an array of people who were in the meeting at the time the poll
|
|
|
|
was started. The poll is published to them only.
|
|
|
|
Once they vote - their ID is removed and they cannot see the poll anymore
|
|
|
|
*/
|
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
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-11-27 00:23:57 +08:00
|
|
|
try {
|
|
|
|
const numberAffected = Polls.update(selector, modifier);
|
2020-10-29 02:33:09 +08:00
|
|
|
|
2020-11-27 00:23:57 +08:00
|
|
|
if (numberAffected) {
|
|
|
|
Logger.info(`Removed responded user=${requesterUserId} from poll (meetingId: ${meetingId}, pollId: ${pollId}!)`);
|
2016-10-22 02:56:42 +08:00
|
|
|
|
2020-11-27 00:23:57 +08:00
|
|
|
RedisPubSub.publishUserMessage(CHANNEL, EVENT_NAME, meetingId, requesterUserId, payload);
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
Logger.error(`Removing responded user from Polls collection: ${err}`);
|
|
|
|
}
|
2016-10-21 21:21:09 +08:00
|
|
|
}
|