From e6a27c8e908ebec58ba3d417509e5e2eaa46355b Mon Sep 17 00:00:00 2001 From: Gaetan Craig-Riou Date: Tue, 13 Jun 2023 13:13:33 +1000 Subject: [PATCH] Add error if customer already exists --- app/controllers/admin/customers_controller.rb | 10 +++++++--- config/locales/en.yml | 2 ++ spec/system/admin/customers_spec.rb | 7 ++++--- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/app/controllers/admin/customers_controller.rb b/app/controllers/admin/customers_controller.rb index bb7fca7d0902..4dfd39015928 100644 --- a/app/controllers/admin/customers_controller.rb +++ b/app/controllers/admin/customers_controller.rb @@ -36,11 +36,15 @@ def show end def create - @customer = Customer.find_or_new(customer_params[:email], customer_params[:enterprise_id]) - @customer.assign_attributes(customer_params) - + @customer = Customer.find_or_new(customer_params[:email], + customer_params[:enterprise_id]) if user_can_create_customer? @customer.set_created_manually_flag + if @customer.id + @customer.errors.add(:base, I18n.t('admin.customers.create.customer_exists')) + return render json: { errors: @customer.errors.full_messages }, status: :bad_request + end + @customer.assign_attributes(customer_params) if @customer.save tag_rule_mapping = TagRule.mapping_for(Enterprise.where(id: @customer.enterprise)) render_as_json @customer, tag_rule_mapping: tag_rule_mapping diff --git a/config/locales/en.yml b/config/locales/en.yml index 395a6cfaae93..7140290625aa 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -685,6 +685,8 @@ en: balance_due: "Balance Due" destroy: has_associated_subscriptions: "Delete failed: This customer has active subscriptions. Cancel them first." + create: + customer_exists: This customer already exists contents: edit: title: Content diff --git a/spec/system/admin/customers_spec.rb b/spec/system/admin/customers_spec.rb index c9886244fc2f..4c35ec53a520 100644 --- a/spec/system/admin/customers_spec.rb +++ b/spec/system/admin/customers_spec.rb @@ -15,7 +15,7 @@ describe "using the customers index" do let!(:customer1) { - create(:customer, first_name: 'John', last_name: 'Doe', enterprise: managed_distributor1, + create(:customer, first_name: 'John', last_name: 'Doe', enterprise: managed_distributor1, code: nil, created_manually: true) } let!(:customer2) { @@ -373,12 +373,13 @@ def wait_for_modal_fade_in(time = 0.4) text: "Email is invalid" }.to_not change{ Customer.of(managed_distributor1).count } + # TODO fix this # When an existing email is used expect{ fill_in 'email', with: customer1.email click_button 'Add Customer' expect(page).to have_selector "#new-customer-dialog .error", - text: "Email is associated with an existing customer" + text: "This customer already exists" }.to change{ customer1.reload.created_manually }.from(false).to(true) .and change { Customer.of(managed_distributor1).count }.by(0) @@ -391,7 +392,7 @@ def wait_for_modal_fade_in(time = 0.4) expect( Customer.of(managed_distributor1).reorder(:id) - .last.created_manually + .last.created_manually ).to be true end end