From 31f0cb12b3002f01581db1742063c02f8c3e6e0a Mon Sep 17 00:00:00 2001 From: Wyatt Johnson Date: Mon, 30 Apr 2018 15:43:12 -0600 Subject: [PATCH] Fixed bug in mutation --- perms/reducers/mutation.js | 22 ++++++++----------- .../components/ChangeUsername.js | 9 +++----- .../containers/ChangeUsername.js | 4 ++-- .../talk-plugin-auth/client/translations.yml | 5 +++-- services/users.js | 20 ++++++++++++----- 5 files changed, 32 insertions(+), 28 deletions(-) diff --git a/perms/reducers/mutation.js b/perms/reducers/mutation.js index f063fe1c2..44f66cf88 100644 --- a/perms/reducers/mutation.js +++ b/perms/reducers/mutation.js @@ -14,25 +14,21 @@ module.exports = (user, perm) => { user.password.length > 0 ); - case types.CHANGE_USERNAME: { + case types.CHANGE_USERNAME: + return user.status.username.status === 'REJECTED'; + + case types.SET_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(); + const deadline = moment().subtract(14, 'days'); 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) - ) + user.status.username.status === 'UNSET' || + get(user, 'status.username.history', []).every(({ created_at }) => + moment(created_at).isBefore(deadline) + ) ); } - case types.SET_USERNAME: - return user.status.username.status === 'UNSET'; - case types.CREATE_COMMENT: case types.CREATE_ACTION: case types.DELETE_ACTION: diff --git a/plugins/talk-plugin-auth/client/profile-settings/components/ChangeUsername.js b/plugins/talk-plugin-auth/client/profile-settings/components/ChangeUsername.js index df8b3a641..4d6bc41f6 100644 --- a/plugins/talk-plugin-auth/client/profile-settings/components/ChangeUsername.js +++ b/plugins/talk-plugin-auth/client/profile-settings/components/ChangeUsername.js @@ -50,13 +50,10 @@ class ChangeUsername extends React.Component { saveChanges = async () => { const { newUsername } = this.state.formData; - const { id } = this.props; + const { setUsername } = this.props; try { - await this.props.changeUsername({ - id, - username: newUsername, - }); + await setUsername(newUsername); this.props.notify( 'success', t('talk-plugin-auth.change_username.changed_username_success_msg') @@ -173,7 +170,7 @@ class ChangeUsername extends React.Component { } ChangeUsername.propTypes = { - changeUsername: PropTypes.func.isRequired, + setUsername: PropTypes.func.isRequired, notify: PropTypes.func.isRequired, username: PropTypes.string, emailAddress: PropTypes.string, diff --git a/plugins/talk-plugin-auth/client/profile-settings/containers/ChangeUsername.js b/plugins/talk-plugin-auth/client/profile-settings/containers/ChangeUsername.js index 87e1e18b5..5381ef162 100644 --- a/plugins/talk-plugin-auth/client/profile-settings/containers/ChangeUsername.js +++ b/plugins/talk-plugin-auth/client/profile-settings/containers/ChangeUsername.js @@ -3,10 +3,10 @@ import { bindActionCreators } from 'redux'; import { connect } from 'plugin-api/beta/client/hocs'; import ChangeUsername from '../components/ChangeUsername'; import { notify } from 'coral-framework/actions/notification'; -import { withChangeUsername } from 'plugin-api/beta/client/hocs'; +import { withSetUsername } from 'plugin-api/beta/client/hocs'; const mapDispatchToProps = dispatch => bindActionCreators({ notify }, dispatch); -export default compose(connect(null, mapDispatchToProps), withChangeUsername)( +export default compose(connect(null, mapDispatchToProps), withSetUsername)( ChangeUsername ); diff --git a/plugins/talk-plugin-auth/client/translations.yml b/plugins/talk-plugin-auth/client/translations.yml index 80f0be761..8362a2e0b 100644 --- a/plugins/talk-plugin-auth/client/translations.yml +++ b/plugins/talk-plugin-auth/client/translations.yml @@ -142,8 +142,9 @@ en: cancel: "Cancel" edit: "Edit" changed_password_msg: "Changed Password - Your password has been successfully changed" - change_username: + change_username: change_username_note: "Usernames can be changed every 14 days" + changed_username_success_msg: "Username has been updated" save: "Save" edit_profile: "Edit Profile" cancel: "Cancel" @@ -255,7 +256,7 @@ es: cancel: "Cancelar" edit: "Editar" changed_password_msg: "Contraseña Actualizada - Tu contraseña ha sido exitosamente actualizada" - change_username: + change_username: change_username_note: "El usuario puede ser cambiado cada 14 días" save: "Guardar" edit_profile: "Editar Perfil" diff --git a/services/users.js b/services/users.js index a3301be10..a4cb0645e 100644 --- a/services/users.js +++ b/services/users.js @@ -253,9 +253,19 @@ class Users { }, { 'status.username.status': { $in: ['APPROVED', 'SET'] }, - 'status.username.history.created_at': { - $lte: oldestEditTime, - }, + $or: [ + { + 'status.username.history.created_at': { + $lte: oldestEditTime, + }, + }, + { + 'status.username.history': [], + }, + { + 'status.username.history': { $exists: false }, + }, + ], }, ], }; @@ -286,8 +296,8 @@ class Users { if ( !['UNSET', 'APPROVED', 'SET'].includes(user.status.username.status) || - !user.status.username.history.every(({ created_at }) => - oldestEditTime.isAfter(created_at) + !user.status.username.history.some(({ created_at }) => + moment(created_at).isAfter(oldestEditTime) ) ) { throw new ErrPermissionUpdateUsername();