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

55 lines
1.2 KiB
JavaScript
Raw Normal View History

2017-06-19 19:57:32 +08:00
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';
2016-10-21 21:21:09 +08:00
import { check } from 'meteor/check';
2016-05-13 01:43:59 +08:00
export default function addPoll(meetingId, requesterId, poll) {
2016-10-21 21:21:09 +08:00
check(poll, Object);
check(requesterId, String);
2016-10-22 01:42:43 +08:00
check(meetingId, String);
2016-10-21 21:21:09 +08:00
let selector = {
2017-06-03 03:25:02 +08:00
meetingId,
};
const options = {
fields: {
'user.userid': 1,
_id: 0,
},
};
const userIds = Users.find(selector, options)
.fetch()
.map(user => user.user.userid);
selector = {
2016-10-22 01:42:43 +08:00
meetingId,
requester: requesterId,
'poll.id': poll.id,
};
const modifier = {
meetingId,
poll,
2016-05-16 22:35:59 +08:00
requester: requesterId,
2016-10-21 21:21:09 +08:00
users: userIds,
2016-05-16 22:35:59 +08:00
};
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}`);
};
2016-10-22 01:42:43 +08:00
return Polls.upsert(selector, modifier, cb);
2017-06-03 03:25:02 +08:00
}