2017-10-12 10:00:28 +08:00
|
|
|
import Users from '/imports/api/users';
|
|
|
|
import Polls from '/imports/api/polls';
|
2016-10-22 01:11:28 +08:00
|
|
|
import Logger from '/imports/startup/server/logger';
|
2017-10-12 08:33:57 +08:00
|
|
|
import flat from 'flat';
|
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(requesterId, String);
|
2016-10-22 01:42:43 +08:00
|
|
|
check(meetingId, String);
|
2017-10-12 08:33:57 +08:00
|
|
|
check(poll, {
|
|
|
|
id: String,
|
|
|
|
answers: [
|
|
|
|
{
|
|
|
|
id: Number,
|
|
|
|
key: String,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
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: {
|
2017-10-12 08:33:57 +08:00
|
|
|
userId: 1,
|
2016-10-22 02:56:42 +08:00
|
|
|
_id: 0,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const userIds = Users.find(selector, options)
|
2017-10-12 08:33:57 +08:00
|
|
|
.fetch()
|
2018-09-24 06:57:03 +08:00
|
|
|
.map((user) => {
|
|
|
|
if (user.userId !== requesterId) return user.userId;
|
|
|
|
});
|
2016-10-22 02:56:42 +08:00
|
|
|
|
|
|
|
selector = {
|
2016-10-22 01:42:43 +08:00
|
|
|
meetingId,
|
|
|
|
requester: requesterId,
|
2017-10-12 08:33:57 +08:00
|
|
|
id: poll.id,
|
2016-10-22 01:42:43 +08:00
|
|
|
};
|
|
|
|
|
2017-10-12 08:33:57 +08:00
|
|
|
const modifier = Object.assign(
|
|
|
|
{ meetingId },
|
|
|
|
{ requester: requesterId },
|
|
|
|
{ users: userIds },
|
|
|
|
flat(poll, { safe: true }),
|
|
|
|
);
|
2016-10-22 01:11:28 +08:00
|
|
|
|
|
|
|
const cb = (err, numChanged) => {
|
2016-10-22 02:56:42 +08:00
|
|
|
if (err != null) {
|
2017-10-13 03:07:02 +08:00
|
|
|
return Logger.error(`Adding Poll to collection: ${poll.id}`);
|
2016-10-22 01:11:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const { insertedId } = numChanged;
|
|
|
|
if (insertedId) {
|
2017-10-13 03:07:02 +08:00
|
|
|
return Logger.info(`Added Poll id=${poll.id}`);
|
2016-10-22 01:11:28 +08:00
|
|
|
}
|
|
|
|
|
2017-10-13 03:07:02 +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
|
|
|
}
|