diff --git a/app/controllers/case_contacts/form_controller.rb b/app/controllers/case_contacts/form_controller.rb index da639f9654..7a80057c80 100644 --- a/app/controllers/case_contacts/form_controller.rb +++ b/app/controllers/case_contacts/form_controller.rb @@ -73,6 +73,7 @@ def get_contact_types .joins(:casa_case_contact_types) .active .where(casa_case_contact_types: {casa_case_id: @casa_cases.pluck(:id)}) + .distinct if case_contact_types.present? case_contact_types @@ -83,6 +84,7 @@ def get_contact_types .active .where(contact_type_group: {casa_org: current_organization}) .order("contact_type_group.name ASC", :name) + .distinct end end diff --git a/app/values/case_contact_parameters.rb b/app/values/case_contact_parameters.rb index d5abc5abb6..e01e05870b 100644 --- a/app/values/case_contact_parameters.rb +++ b/app/values/case_contact_parameters.rb @@ -36,6 +36,7 @@ def normalize_params(params) params[:case_contact][:miles_driven] = convert_miles_driven(params) end params[:case_contact][:notes] = params.dig(:case_contact, :notes).presence + params[:case_contact][:contact_type_ids] = params[:case_contact][:contact_type_ids]&.uniq params end diff --git a/spec/requests/case_contacts/form_spec.rb b/spec/requests/case_contacts/form_spec.rb index ea62581259..183a40321c 100644 --- a/spec/requests/case_contacts/form_spec.rb +++ b/spec/requests/case_contacts/form_spec.rb @@ -212,6 +212,18 @@ end end + context "with duplicate contact type ids in params" do + let(:contact_type_ids) { [contact_types.first.id, contact_types.first.id] } + + it "dedupes and updates the contact type ids" do + expect(case_contact.contact_type_ids).to be_empty + request + expect(response).to have_http_status(:redirect) + + expect(case_contact.reload.contact_type_ids).to contain_exactly(contact_types.first.id) + end + end + context "when contact types were previously assigned" do before { case_contact.update!(contact_type_ids: [contact_types.second.id]) }