From 50e00e893f39af4dfa7f4e3a0b82ade2264d38c3 Mon Sep 17 00:00:00 2001 From: Enoch Hsu <1529929+ewoknock@users.noreply.github.com> Date: Wed, 11 Sep 2024 17:30:27 -0700 Subject: [PATCH] 4515 - Deactivated partners should not appear in the dropdown when producing a new distribution. (#4639) * Filters out deactive partners on new distribution This commit adds in a conditional check to the _form partial that checks where the partial is coming from. If it is being rendered from the 'new' view, it will then generate a partner list that filters out the deactivated partners. If it is being rendered from anywhere else, it will generate the complete partner list unfiltered. [Ticket: 4515] * Updates the re-render of create on a fail to also filter out the deactivated partners * Adds in unit tests for distribution dropdown * Removed unneeded variable declaration for unit tests * Moves the partner_list query from the view into the controller actions * Moved rspec tests from 'system' to 'requests' --- app/controllers/distributions_controller.rb | 5 +++ app/views/distributions/_form.html.erb | 2 +- spec/requests/distributions_requests_spec.rb | 36 ++++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/app/controllers/distributions_controller.rb b/app/controllers/distributions_controller.rb index f7f0e0d62a..a56003a716 100644 --- a/app/controllers/distributions_controller.rb +++ b/app/controllers/distributions_controller.rb @@ -115,6 +115,8 @@ def create @distribution.initialize_request_items end @items = current_organization.items.alphabetized + @partner_list = current_organization.partners.where.not(status: 'deactivated').alphabetized + if Event.read_events?(current_organization) inventory = View::Inventory.new(@distribution.organization_id) @storage_locations = current_organization.storage_locations.active_locations.alphabetized.select do |storage_loc| @@ -147,6 +149,8 @@ def new @distribution.copy_from_donation(params[:donation_id], params[:storage_location_id]) end @items = current_organization.items.alphabetized + @partner_list = current_organization.partners.where.not(status: 'deactivated').alphabetized + if Event.read_events?(current_organization) inventory = View::Inventory.new(current_organization.id) @storage_locations = current_organization.storage_locations.active_locations.alphabetized.select do |storage_loc| @@ -175,6 +179,7 @@ def edit current_user.has_role?(Role::ORG_ADMIN, current_organization) @distribution.line_items.build if @distribution.line_items.size.zero? @items = current_organization.items.alphabetized + @partner_list = current_organization.partners.alphabetized @audit_warning = current_organization.audits .where(storage_location_id: @distribution.storage_location_id) .where("updated_at > ?", @distribution.created_at).any? diff --git a/app/views/distributions/_form.html.erb b/app/views/distributions/_form.html.erb index b72329f950..f28782234c 100644 --- a/app/views/distributions/_form.html.erb +++ b/app/views/distributions/_form.html.erb @@ -9,7 +9,7 @@ <% end %> <%= f.association :partner, - collection: current_organization.partners.alphabetized, + collection: @partner_list, label: "Partner", error: "Which partner is this distribution going to?" %> diff --git a/spec/requests/distributions_requests_spec.rb b/spec/requests/distributions_requests_spec.rb index a5b6243cfc..34e06e4af5 100644 --- a/spec/requests/distributions_requests_spec.rb +++ b/spec/requests/distributions_requests_spec.rb @@ -129,6 +129,21 @@ expect(response).to have_http_status(400) expect(response).to have_error end + + context "Deactivated partners should not be displayed in partner dropdown" do + before do + create(:partner, name: 'Active Partner', organization: organization, status: "approved") + create(:partner, name: 'Deactivated Partner', organization: organization, status: "deactivated") + end + + it "should not display deactivated partners after error and re-render of form" do + post distributions_path(distribution: { comment: nil, partner_id: nil, storage_location_id: nil }, format: :turbo_stream) + expect(response).to have_http_status(400) + expect(response).to have_error + expect(response.body).not_to include("Deactivated Partner") + expect(response.body).to include("Active Partner") + end + end end describe "GET #new" do @@ -179,6 +194,19 @@ end end + context "Deactivated partners should not be displayed in partner dropdown" do + before do + create(:partner, name: 'Active Partner', organization: organization, status: "approved") + create(:partner, name: 'Deactivated Partner', organization: organization, status: "deactivated") + end + + it "should not display deactivated partners on new distribution" do + get new_distribution_path(default_params) + expect(response.body).not_to include("Deactivated Partner") + expect(response.body).to include("Active Partner") + end + end + context 'with units' do before(:each) do Flipper.enable(:enable_packs) @@ -464,6 +492,14 @@ expect(response.body).not_to include("You’ve had an audit since this distribution was started.") end + it "should display deactivated partners in partner dropdown" do + create(:partner, name: 'Active Partner', organization: organization, status: "approved") + create(:partner, name: 'Deactivated Partner', organization: organization, status: "deactivated") + get edit_distribution_path(id: distribution.id) + expect(response.body).to include("Deactivated Partner") + expect(response.body).to include("Active Partner") + end + context 'with units' do let!(:request) { create(:request,