bigbluebutton-Github/bigbluebutton-html5/imports/api/1.1/polls/server/modifiers/addPoll.js
2017-06-19 08:57:32 -03:00

55 lines
1.2 KiB
JavaScript

import Meetings from '/imports/api/1.1/meetings';
import Users from '/imports/api/1.1/users';
import Polls from '/imports/api/1.1/polls';
import Logger from '/imports/startup/server/logger';
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)
.fetch()
.map(user => user.user.userid);
selector = {
meetingId,
requester: requesterId,
'poll.id': poll.id,
};
const modifier = {
meetingId,
poll,
requester: requesterId,
users: userIds,
};
const cb = (err, numChanged) => {
if (err != null) {
return Logger.error(`Adding Poll to collection: ${poll.id}`);
}
const { insertedId } = numChanged;
if (insertedId) {
return Logger.info(`Added Poll id=${poll.id}`);
}
return Logger.info(`Upserted Poll id=${poll.id}`);
};
return Polls.upsert(selector, modifier, cb);
}