Skip to content

Commit

Permalink
Add error if customer already exists
Browse files Browse the repository at this point in the history
  • Loading branch information
rioug authored and abdellani committed Jun 14, 2023
1 parent 0c85d92 commit e6a27c8
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
10 changes: 7 additions & 3 deletions app/controllers/admin/customers_controller.rb
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions config/locales/en.yml
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions spec/system/admin/customers_spec.rb
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)

Expand All @@ -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
Expand Down

0 comments on commit e6a27c8

Please sign in to comment.