diff --git a/app/controllers/organization_users_controller.rb b/app/controllers/organization_users_controller.rb index ba81ece6c5..4b50277a2e 100644 --- a/app/controllers/organization_users_controller.rb +++ b/app/controllers/organization_users_controller.rb @@ -14,23 +14,27 @@ class OrganizationUsersController < ApplicationController password = SecureRandom.uuid org = @current_user.organization - user = User.create( - first_name: first_name, - last_name: last_name, - email: email, - password: password, - password_confirmation: password, - organization: org, - invitation_token: SecureRandom.uuid - ) + if User.find_by(email: email).present? + render json: { message: "Email address is already taken" }, status: :unprocessable_entity + else + user = User.create( + first_name: first_name, + last_name: last_name, + email: email, + password: password, + password_confirmation: password, + organization: org, + invitation_token: SecureRandom.uuid + ) - org_user = OrganizationUser.new( - role: role, - user: user, - organization: org - ) + org_user = OrganizationUser.new( + role: role, + user: user, + organization: org + ) - UserMailer.with(user: user, sender: @current_user).invitation_email.deliver if org_user.save + UserMailer.with(user: user, sender: @current_user).invitation_email.deliver if org_user.save + end end def change_role diff --git a/frontend/src/ManageOrgUsers/ManageOrgUsers.jsx b/frontend/src/ManageOrgUsers/ManageOrgUsers.jsx index da0ecbc9b7..8114f28e61 100644 --- a/frontend/src/ManageOrgUsers/ManageOrgUsers.jsx +++ b/frontend/src/ManageOrgUsers/ManageOrgUsers.jsx @@ -86,11 +86,17 @@ class ManageOrgUsers extends React.Component { const { firstName, lastName, email, role } = this.state.newUser; - organizationUserService.create(firstName, lastName, email, role).then(() => { - this.setState({ creatingUser: false, showNewUserForm: false, newUser: {} }); - toast.success('User has been created', { hideProgressBar: true, position: 'top-center' }); - this.fetchUsers(); - }); + organizationUserService + .create(firstName, lastName, email, role) + .then(() => { + this.setState({ creatingUser: false, showNewUserForm: false, newUser: {} }); + toast.success('User has been created', { hideProgressBar: true, position: 'top-center' }); + this.fetchUsers(); + }) + .catch(({ error }) => { + toast.error(error, { hideProgressBar: true, position: 'top-center' }); + this.setState({ creatingUser: false, showNewUserForm: true, newUser: {} }); + }); }; logout = () => {