2018-10-10 23:49:58 +08:00
|
|
|
import { check } from 'meteor/check';
|
|
|
|
import Polls from '/imports/api/polls';
|
|
|
|
import Logger from '/imports/startup/server/logger';
|
|
|
|
|
2023-03-07 21:47:43 +08:00
|
|
|
export default async function userResponded({ body }) {
|
2021-02-04 22:43:04 +08:00
|
|
|
const { pollId, userId, answerIds } = body;
|
2018-10-10 23:49:58 +08:00
|
|
|
|
|
|
|
check(pollId, String);
|
|
|
|
check(userId, String);
|
2021-02-04 22:43:04 +08:00
|
|
|
check(answerIds, Array);
|
2018-10-10 23:49:58 +08:00
|
|
|
|
|
|
|
const selector = {
|
|
|
|
id: pollId,
|
|
|
|
};
|
|
|
|
|
|
|
|
const modifier = {
|
|
|
|
$pull: {
|
|
|
|
users: userId,
|
|
|
|
},
|
|
|
|
$push: {
|
2021-02-11 18:13:57 +08:00
|
|
|
responses: { userId, answerIds },
|
2018-10-10 23:49:58 +08:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-11-27 00:23:57 +08:00
|
|
|
try {
|
2023-03-07 21:47:43 +08:00
|
|
|
const numberAffected = await Polls.updateAsync(selector, modifier);
|
2018-10-10 23:49:58 +08:00
|
|
|
|
2020-11-27 00:23:57 +08:00
|
|
|
if (numberAffected) {
|
2021-02-11 18:13:57 +08:00
|
|
|
Logger.info(`Updating Poll response (userId: ${userId}, response: ${JSON.stringify(answerIds)}, pollId: ${pollId})`);
|
2020-11-27 00:23:57 +08:00
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
Logger.error(`Updating Poll responses: ${err}`);
|
|
|
|
}
|
2018-10-10 23:49:58 +08:00
|
|
|
}
|