Move previous suspendUser feature to rejectUsername

This commit is contained in:
Chi Vinh Le
2017-05-18 02:28:01 +07:00
parent 056ff427a5
commit 76e87f4c34
13 changed files with 146 additions and 57 deletions
+50 -19
View File
@@ -450,28 +450,60 @@ module.exports = class UsersService {
}
/**
* Suspend a user. It changes the status to BANNED and canEditName to True.
* Suspend a user until specified time.
* @param {String} id id of a user
* @param {String} message message to be send to the user
* @param {Boolean} mustChangeUsername if set the suspension lasts at least until user changed its username.
* @param {Date} until if set the suspension lasts at least until date.
* @param {Date} until date until the suspension is valid.
*/
static suspendUser(id, message, mustChangeUsername, until) {
const changes = {
$set: {
suspensionDetails: {},
}
};
if (mustChangeUsername) {
changes.$set.status = 'BANNED';
changes.$set.canEditName = true;
changes.$set.suspensionDetails.mustChangeUsername = true;
}
if (until) {
changes.$set.suspensionDetails.until = until;
}
return UserModel.findOneAndUpdate({id}, changes)
static suspendUser(id, message, until) {
console.log('SUSPEND');
return UserModel.findOneAndUpdate(
{id}, {
$set: {
suspension: {
until,
},
}
})
.then((user) => {
console.log(user);
if (message) {
let localProfile = user.profiles.find((profile) => profile.provider === 'local');
if (localProfile) {
const options =
{
template: 'suspension', // needed to know which template to render!
locals: { // specifies the template locals.
body: message
},
subject: 'Email Suspension',
to: localProfile.id // This only works if the user has registered via e-mail.
// We may want a standard way to access a user's e-mail address in the future
};
return MailerService.sendSimple(options);
} else {
return Promise.reject(errors.ErrMissingEmail);
}
}
});
}
/**
* Reject username. It changes the status to BANNED and canEditName to True.
* @param {String} id id of a user
* @param {String} message message to be send to the user
* @param {Date} until date until the suspension is valid.
*/
static rejectUsername(id, message) {
return UserModel.findOneAndUpdate(
{id}, {
$set: {
status: 'BANNED',
canEditName: true,
}
}).then((user) => {
if (message) {
let localProfile = user.profiles.find((profile) => profile.provider === 'local');
@@ -822,7 +854,6 @@ module.exports = class UsersService {
lowercaseUsername: username.toLowerCase(),
canEditName: false,
status: 'PENDING',
'suspensionDetails.mustChangeUsername': false,
}
})
.then((result) => {