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';
|
2016-10-22 01:11:28 +08:00
|
|
|
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
|
|
|
|
2016-10-22 02:56:42 +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
|
|
|
|
2016-10-22 02:56:42 +08:00
|
|
|
let selector = {
|
2017-06-03 03:25:02 +08:00
|
|
|
meetingId,
|
2016-10-22 02:56:42 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
2016-10-22 01:11:28 +08:00
|
|
|
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
|
|
|
};
|
2016-10-22 01:11:28 +08:00
|
|
|
|
|
|
|
const cb = (err, numChanged) => {
|
2016-10-22 02:56:42 +08:00
|
|
|
if (err != null) {
|
|
|
|
return Logger.error(`Adding Poll to collection: ${poll.id}`);
|
2016-10-22 01:11:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const { insertedId } = numChanged;
|
|
|
|
if (insertedId) {
|
2016-10-22 02:56:42 +08:00
|
|
|
return Logger.info(`Added Poll id=${poll.id}`);
|
2016-10-22 01:11:28 +08:00
|
|
|
}
|
|
|
|
|
2016-10-22 02:56:42 +08:00
|
|
|
return Logger.info(`Upserted Poll id=${poll.id}`);
|
2016-10-22 01:11:28 +08:00
|
|
|
};
|
|
|
|
|
2016-10-22 01:42:43 +08:00
|
|
|
return Polls.upsert(selector, modifier, cb);
|
2017-06-03 03:25:02 +08:00
|
|
|
}
|