From 499bed69e2365a7e9dad329cf69bb8169886d2b6 Mon Sep 17 00:00:00 2001 From: Arpit Jalan Date: Wed, 15 Apr 2015 13:36:54 +0530 Subject: [PATCH] FIX: show error message if user already exist in group --- app/assets/javascripts/discourse/models/group.js.es6 | 6 ++++++ app/controllers/admin/groups_controller.rb | 6 +++++- config/locales/server.en.yml | 1 + spec/controllers/admin/groups_controller_spec.rb | 10 ++++++++++ 4 files changed, 22 insertions(+), 1 deletion(-) diff --git a/app/assets/javascripts/discourse/models/group.js.es6 b/app/assets/javascripts/discourse/models/group.js.es6 index 6f426073a..984941e89 100644 --- a/app/assets/javascripts/discourse/models/group.js.es6 +++ b/app/assets/javascripts/discourse/models/group.js.es6 @@ -52,6 +52,12 @@ const Group = Discourse.Model.extend({ }).then(function() { // reload member list self.findMembers(); + }).catch(function(error) { + if (error && error.responseText) { + bootbox.alert($.parseJSON(error.responseText).errors[0]); + } else { + bootbox.alert(I18n.t('generic_error')); + } }); }, diff --git a/app/controllers/admin/groups_controller.rb b/app/controllers/admin/groups_controller.rb index 69288921f..42ed206bf 100644 --- a/app/controllers/admin/groups_controller.rb +++ b/app/controllers/admin/groups_controller.rb @@ -83,7 +83,11 @@ class Admin::GroupsController < Admin::AdminController end users.each do |user| - group.add(user) + if !group.users.include?(user) + group.add(user) + else + return render_json_error I18n.t('groups.errors.member_already_exist', username: user.username) + end end if group.save diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index ee0ec4e0e..f7d1fd83a 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -180,6 +180,7 @@ en: groups: errors: can_not_modify_automatic: "You can not modify an automatic group" + member_already_exist: "'%{username}' is already a member of this group." default_names: everyone: "everyone" admins: "admins" diff --git a/spec/controllers/admin/groups_controller_spec.rb b/spec/controllers/admin/groups_controller_spec.rb index b8c543f4c..b7710e700 100644 --- a/spec/controllers/admin/groups_controller_spec.rb +++ b/spec/controllers/admin/groups_controller_spec.rb @@ -136,6 +136,16 @@ describe Admin::GroupsController do end end + it "returns 422 if member already exists" do + group = Fabricate(:group) + existing_member = Fabricate(:user) + group.add(existing_member) + group.save + + xhr :put, :add_members, id: group.id, usernames: existing_member.username + expect(response.status).to eq(422) + end + end context ".remove_member" do