bigbluebutton-Github/bigbluebutton-html5/imports/api/2.0/polls/server/modifiers/addPoll.js

55 lines
1.1 KiB
JavaScript
Raw Normal View History

2017-06-29 04:37:45 +08:00
import Users from '/imports/api/2.0/users';
import Polls from '/imports/api/2.0/polls';
import Logger from '/imports/startup/server/logger';
2017-07-25 20:26:45 +08:00
import flat from 'flat';
2017-06-29 04:37:45 +08:00
import { check } from 'meteor/check';
export default function addPoll(meetingId, requesterId, poll) {
check(poll, Object);
check(requesterId, String);
check(meetingId, String);
let selector = {
meetingId,
};
const options = {
fields: {
'user.userid': 1,
_id: 0,
},
};
const userIds = Users.find(selector, options)
2017-07-25 20:26:45 +08:00
.fetch()
.map(user => user.user.userid);
2017-06-29 04:37:45 +08:00
selector = {
meetingId,
requester: requesterId,
2017-07-25 20:26:45 +08:00
id: poll.id,
2017-06-29 04:37:45 +08:00
};
2017-07-25 20:26:45 +08:00
const modifier = Object.assign(
{ meetingId },
{ requester: requesterId },
{ users: userIds },
flat(poll, { safe: true }),
);
2017-06-29 04:37:45 +08:00
const cb = (err, numChanged) => {
if (err != null) {
2017-06-30 22:26:19 +08:00
return Logger.error(`Adding Poll2x to collection: ${poll.id}`);
2017-06-29 04:37:45 +08:00
}
const { insertedId } = numChanged;
if (insertedId) {
2017-06-30 22:26:19 +08:00
return Logger.info(`Added Poll2x id=${poll.id}`);
2017-06-29 04:37:45 +08:00
}
2017-06-30 22:26:19 +08:00
return Logger.info(`Upserted Poll2x id=${poll.id}`);
2017-06-29 04:37:45 +08:00
};
return Polls.upsert(selector, modifier, cb);
}