Allow a user to change their own username

This commit is contained in:
Wyatt Johnson
2018-04-16 17:37:08 -06:00
parent ce291448f1
commit ba9f034bfb
5 changed files with 172 additions and 46 deletions
+18 -3
View File
@@ -1,4 +1,5 @@
const { isString } = require('lodash');
const { get, isString } = require('lodash');
const moment = require('moment');
const { check } = require('../utils');
const types = require('../constants');
@@ -12,8 +13,22 @@ module.exports = (user, perm) => {
isString(user.password) &&
user.password.length > 0
);
case types.CHANGE_USERNAME:
return user.status.username.status === 'REJECTED';
case types.CHANGE_USERNAME: {
// Only users who have their usernames rejected or those users who
// not changed their usernames within 14 days can change their usernames.
const now = moment();
return (
user.status.username.status === 'REJECTED' ||
get(user, 'status.username.history', [])
.filter(({ status }) => status === 'CHANGED')
.every(({ created_at }) =>
moment(created_at)
.add(14, 'days')
.isAfter(now)
)
);
}
case types.SET_USERNAME:
return user.status.username.status === 'UNSET';