diff --git a/app/controllers/categories_controller.rb b/app/controllers/categories_controller.rb index 74219e7d995..9de2c23383e 100644 --- a/app/controllers/categories_controller.rb +++ b/app/controllers/categories_controller.rb @@ -87,6 +87,9 @@ class CategoriesController < ApplicationController if category_params.key? :email_in and category_params[:email_in].length == 0 # properly null the value so the database constrain doesn't catch us category_params[:email_in] = nil + elsif category_params.key? :email_in and existing_category = Category.find_by(email_in: category_params[:email_in]) and existing_category.id != @category.id + # check if email_in address is already in use for other category + return render_json_error I18n.t('category.errors.email_in_already_exist', {email_in: category_params[:email_in], category_name: existing_category.name}) end category_params.delete(:position) diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 5a172035731..e69873aefd9 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -341,6 +341,7 @@ en: uncategorized_parent: "Uncategorized can't have a parent category" self_parent: "A subcategory's parent cannot be itself" depth: "You can't nest a subcategory under another" + email_in_already_exist: "Incoming email address '%{email_in}' is already in use for '%{category_name}' category." cannot_delete: uncategorized: "Can't delete Uncategorized" has_subcategories: "Can't delete this category because it has sub-categories." diff --git a/spec/controllers/categories_controller_spec.rb b/spec/controllers/categories_controller_spec.rb index 9a7d342ae62..0f1a4e93476 100644 --- a/spec/controllers/categories_controller_spec.rb +++ b/spec/controllers/categories_controller_spec.rb @@ -174,6 +174,14 @@ describe CategoriesController do end end + it "returns 422 if email_in address is already in use for other category" do + @other_category = Fabricate(:category, name: "Other", email_in: "mail@examle.com" ) + xhr :put, :update, id: @category.id, name: "Email", email_in: "mail@examle.com", color: "ff0", text_color: "fff" + + expect(response).not_to be_success + expect(response.code.to_i).to eq(422) + end + describe "success" do it "updates the group correctly" do