2017-10-12 10:00:28 +08:00
|
|
|
import Users from '/imports/api/users';
|
|
|
|
import Polls from '/imports/api/polls';
|
2016-10-22 01:11:28 +08:00
|
|
|
import Logger from '/imports/startup/server/logger';
|
2017-10-12 08:33:57 +08:00
|
|
|
import flat from 'flat';
|
2016-10-21 21:21:09 +08:00
|
|
|
import { check } from 'meteor/check';
|
2016-05-13 01:43:59 +08:00
|
|
|
|
2023-03-07 21:47:43 +08:00
|
|
|
export default async function addPoll(meetingId, requesterId, poll, pollType, secretPoll, question = '') {
|
2016-10-21 21:21:09 +08:00
|
|
|
check(requesterId, String);
|
2016-10-22 01:42:43 +08:00
|
|
|
check(meetingId, String);
|
2017-10-12 08:33:57 +08:00
|
|
|
check(poll, {
|
|
|
|
id: String,
|
|
|
|
answers: [
|
|
|
|
{
|
|
|
|
id: Number,
|
|
|
|
key: String,
|
|
|
|
},
|
|
|
|
],
|
2021-02-08 17:57:39 +08:00
|
|
|
isMultipleResponse: Boolean,
|
2017-10-12 08:33:57 +08:00
|
|
|
});
|
|
|
|
|
2018-11-01 01:28:35 +08:00
|
|
|
const userSelector = {
|
2017-06-03 03:25:02 +08:00
|
|
|
meetingId,
|
2018-10-31 22:03:48 +08:00
|
|
|
userId: { $ne: requesterId },
|
2019-03-01 15:12:12 +08:00
|
|
|
clientType: { $ne: 'dial-in-user' },
|
2016-10-22 02:56:42 +08:00
|
|
|
};
|
|
|
|
|
2023-03-07 21:47:43 +08:00
|
|
|
const users = await Users.find(userSelector, { fields: { userId: 1 } })
|
|
|
|
.fetchAsync();
|
|
|
|
const userIds = users.map(user => user.userId);
|
2016-10-22 02:56:42 +08:00
|
|
|
|
2018-11-01 01:28:35 +08:00
|
|
|
const selector = {
|
2016-10-22 01:42:43 +08:00
|
|
|
meetingId,
|
|
|
|
requester: requesterId,
|
2017-10-12 08:33:57 +08:00
|
|
|
id: poll.id,
|
2016-10-22 01:42:43 +08:00
|
|
|
};
|
|
|
|
|
2017-10-12 08:33:57 +08:00
|
|
|
const modifier = Object.assign(
|
|
|
|
{ meetingId },
|
|
|
|
{ requester: requesterId },
|
|
|
|
{ users: userIds },
|
2021-06-17 21:25:03 +08:00
|
|
|
{ question, pollType, secretPoll },
|
2017-10-12 08:33:57 +08:00
|
|
|
flat(poll, { safe: true }),
|
|
|
|
);
|
2016-10-22 01:11:28 +08:00
|
|
|
|
2020-11-23 21:13:46 +08:00
|
|
|
try {
|
2023-03-07 21:47:43 +08:00
|
|
|
const { insertedId } = await Polls.upsertAsync(selector, modifier);
|
2020-11-23 21:13:46 +08:00
|
|
|
|
2016-10-22 01:11:28 +08:00
|
|
|
if (insertedId) {
|
2020-11-23 21:13:46 +08:00
|
|
|
Logger.info(`Added Poll id=${poll.id}`);
|
|
|
|
} else {
|
|
|
|
Logger.info(`Upserted Poll id=${poll.id}`);
|
2016-10-22 01:11:28 +08:00
|
|
|
}
|
2020-11-23 21:13:46 +08:00
|
|
|
} catch (err) {
|
|
|
|
Logger.error(`Adding Poll to collection: ${poll.id}`);
|
|
|
|
}
|
2017-06-03 03:25:02 +08:00
|
|
|
}
|