2017-10-07 00:10:23 +08:00
|
|
|
import Logger from '/imports/startup/server/logger';
|
2017-10-12 10:00:28 +08:00
|
|
|
import Users from '/imports/api/users';
|
2017-10-07 00:10:23 +08:00
|
|
|
|
2017-10-26 01:29:03 +08:00
|
|
|
export default function changeRole(role, status, userId, meetingId, changedBy) {
|
2017-10-26 03:00:31 +08:00
|
|
|
const USER_CONFIG = Meteor.settings.public.user;
|
|
|
|
const ROLE_PRESENTER = USER_CONFIG.role_presenter;
|
|
|
|
|
2017-10-07 00:10:23 +08:00
|
|
|
const selector = {
|
|
|
|
meetingId,
|
|
|
|
userId,
|
|
|
|
};
|
|
|
|
|
2017-10-26 01:29:03 +08:00
|
|
|
const action = status ? '$push' : '$pop';
|
|
|
|
|
|
|
|
const user = Users.findOne(selector);
|
|
|
|
|
2017-10-07 00:10:23 +08:00
|
|
|
const modifier = {
|
|
|
|
$set: {
|
2017-10-26 03:00:31 +08:00
|
|
|
role: (role === ROLE_PRESENTER ? user.role : role),
|
2017-10-26 01:29:03 +08:00
|
|
|
[role.toLowerCase()]: status,
|
2017-10-07 01:52:14 +08:00
|
|
|
},
|
2017-10-26 01:29:03 +08:00
|
|
|
[action]: {
|
|
|
|
roles: (role.toLowerCase()),
|
2017-10-07 00:10:23 +08:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const cb = (err, numChanged) => {
|
2017-10-26 01:29:03 +08:00
|
|
|
const actionVerb = (status) ? 'Changed' : 'Removed';
|
|
|
|
|
2017-10-07 00:10:23 +08:00
|
|
|
if (err) {
|
|
|
|
return Logger.error(`Changed user role: ${err}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (numChanged) {
|
2017-10-27 23:10:53 +08:00
|
|
|
return Logger.info(`${actionVerb} user role=${role} id=${userId} meeting=${meetingId}`
|
|
|
|
+ `${changedBy ? ` changedBy=${changedBy}` : ''}`);
|
2017-10-07 00:10:23 +08:00
|
|
|
}
|
2017-10-07 01:56:56 +08:00
|
|
|
|
|
|
|
return null;
|
2017-10-07 00:10:23 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
return Users.update(selector, modifier, cb);
|
|
|
|
}
|