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(requesterId, String);
|
|
|
|
check(meetingId, String);
|
|
|
|
|
2017-07-27 01:08:00 +08:00
|
|
|
check(poll, {
|
|
|
|
id: String,
|
|
|
|
answers: [
|
|
|
|
{
|
|
|
|
id: Number,
|
|
|
|
key: String,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
2017-06-29 04:37:45 +08:00
|
|
|
let selector = {
|
|
|
|
meetingId,
|
|
|
|
};
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
fields: {
|
2017-07-26 22:09:07 +08:00
|
|
|
userId: 1,
|
2017-06-29 04:37:45 +08:00
|
|
|
_id: 0,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const userIds = Users.find(selector, options)
|
2017-07-25 20:26:45 +08:00
|
|
|
.fetch()
|
2017-07-26 22:09:07 +08:00
|
|
|
.map(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);
|
|
|
|
}
|