From c5ea4073dd5099ad923c62955be8d27c5cbeffb5 Mon Sep 17 00:00:00 2001 From: McEileen Date: Mon, 25 Nov 2024 08:56:01 -0500 Subject: [PATCH 1/7] fix item minimum error to consider quantity across organization --- app/services/inventory_check_service.rb | 2 +- .../distributions_controller_spec.rb | 58 +++++++++++++-- spec/services/inventory_check_service_spec.rb | 71 ++++++++++++++----- 3 files changed, 109 insertions(+), 22 deletions(-) diff --git a/app/services/inventory_check_service.rb b/app/services/inventory_check_service.rb index 8f331887cb..edf45afa49 100644 --- a/app/services/inventory_check_service.rb +++ b/app/services/inventory_check_service.rb @@ -34,7 +34,7 @@ def items_below_minimum_quantity # Done this way to prevent N+1 query on items unless @items_below_minimum_quantity item_ids = @distribution.line_items.select do |line_item| - quantity = @inventory.quantity_for(storage_location: @distribution.storage_location_id, item_id: line_item.item_id) + quantity = @inventory.quantity_for(item_id: line_item.item_id) quantity < (line_item.item.on_hand_minimum_quantity || 0) end.map(&:item_id) diff --git a/spec/controllers/distributions_controller_spec.rb b/spec/controllers/distributions_controller_spec.rb index ec8210a941..ea47e3934a 100644 --- a/spec/controllers/distributions_controller_spec.rb +++ b/spec/controllers/distributions_controller_spec.rb @@ -9,7 +9,55 @@ end describe "POST #create" do - context "when distribution causes inventory quantity to be below minimum quantity" do + let(:available_item) { create(:item, name: "Available Item", organization: organization, on_hand_minimum_quantity: 5) } + let!(:first_storage_location) { create(:storage_location, :with_items, item: available_item, item_quantity: 20, organization: organization) } + let!(:second_storage_location) { create(:storage_location, :with_items, item: available_item, item_quantity: 20, organization: organization) } + context "when distribution causes inventory to remain above minimum quantity for an organization" do + let(:params) do + { + organization_name: organization.id, + distribution: { + partner_id: partner.id, + storage_location_id: first_storage_location.id, + line_items_attributes: + { + "0": { item_id: first_storage_location.items.first.id, quantity: 10 } + } + } + } + end + + subject { post :create, params: params.merge(format: :turbo_stream) } + + it "does not display an error" do + subject + + expect(flash[:error]).to be_nil + end + + context "when distribution causes inventory to fall below minimum quantity for a storage location" do + let(:params) do + { + organization_name: organization.id, + distribution: { + partner_id: partner.id, + storage_location_id: second_storage_location.id, + line_items_attributes: + { + "0": { item_id: second_storage_location.items.first.id, quantity: 18 } + } + } + } + end + it "does not display an error" do + subject + expect(flash[:notice]).to eq("Distribution created!") + expect(flash[:error]).to be_nil + end + end + end + + context "when distribution causes inventory quantity to be below minimum quantity for an organization" do let(:item) { create(:item, name: "Item 1", organization: organization, on_hand_minimum_quantity: 5) } let(:storage_location) { create(:storage_location, :with_items, item: item, item_quantity: 20, organization: organization) } let(:params) do @@ -35,7 +83,7 @@ end end - context "multiple line_items that have inventory quantity below minimum quantity" do + context "multiple line_items that have inventory quantity below minimum quantity for an organization" do let(:item1) { create(:item, name: "Item 1", organization: organization, on_hand_minimum_quantity: 5, on_hand_recommended_quantity: 10) } let(:item2) { create(:item, name: "Item 2", organization: organization, on_hand_minimum_quantity: 5, on_hand_recommended_quantity: 10) } let(:storage_location) { create(:storage_location, organization: organization) } @@ -74,7 +122,7 @@ end end - context "multiple line_items that have inventory quantity below recommended quantity" do + context "multiple line_items that have inventory quantity below recommended quantity for an organization" do let(:item1) { create(:item, name: "Item 1", organization: organization, on_hand_recommended_quantity: 5) } let(:item2) { create(:item, name: "Item 2", organization: organization, on_hand_recommended_quantity: 5) } let(:storage_location) { create(:storage_location, organization: organization) } @@ -136,7 +184,7 @@ end describe "PUT #update" do - context "when distribution causes inventory quantity to be below recommended quantity" do + context "when distribution causes inventory quantity to be below recommended quantity for an organization" do let(:item1) { create(:item, name: "Item 1", organization: organization, on_hand_recommended_quantity: 5) } let(:item2) { create(:item, name: "Item 2", organization: organization, on_hand_recommended_quantity: 5) } let(:storage_location) { create(:storage_location, organization: organization) } @@ -173,7 +221,7 @@ end end - context "when distribution causes inventory quantity to be below minimum quantity" do + context "when distribution causes inventory quantity to be below minimum quantity for an organization" do let(:item1) { create(:item, name: "Item 1", organization: organization, on_hand_minimum_quantity: 5) } let(:item2) { create(:item, name: "Item 2", organization: organization, on_hand_minimum_quantity: 5) } let(:storage_location) { create(:storage_location) } diff --git a/spec/services/inventory_check_service_spec.rb b/spec/services/inventory_check_service_spec.rb index 7f6aa9efa6..33dc91debc 100644 --- a/spec/services/inventory_check_service_spec.rb +++ b/spec/services/inventory_check_service_spec.rb @@ -3,27 +3,27 @@ subject { InventoryCheckService } describe "call" do - let(:item1) { create(:item, name: "Item 1", organization: organization, on_hand_minimum_quantity: 5, on_hand_recommended_quantity: 10) } - let(:item2) { create(:item, name: "Item 2", organization: organization, on_hand_minimum_quantity: 5, on_hand_recommended_quantity: 10) } - - context "error" do + context "when on hand quantity is below the minimum for the organization" do + let(:first_item) { create(:item, name: "Item 1", organization: organization, on_hand_minimum_quantity: 5, on_hand_recommended_quantity: 10) } + let(:second_item) { create(:item, name: "Item 2", organization: organization, on_hand_minimum_quantity: 5, on_hand_recommended_quantity: 10) } let(:storage_location) do storage_location = create(:storage_location, organization: organization) TestInventory.create_inventory(storage_location.organization, { storage_location.id => { - item1.id => 4, - item2.id => 4 + first_item.id => 4, + second_item.id => 4 } }) storage_location end + let(:distribution) { create(:distribution, storage_location_id: storage_location.id) } + before do + create(:line_item, item: first_item, itemizable_type: "Distribution", itemizable_id: distribution.id, quantity: 16) + create(:line_item, item: second_item, itemizable_type: "Distribution", itemizable_id: distribution.id, quantity: 16) + end it "should set the error" do - distribution = create(:distribution, storage_location_id: storage_location.id) - create(:line_item, item: item1, itemizable_type: "Distribution", itemizable_id: distribution.id, quantity: 16) - create(:line_item, item: item2, itemizable_type: "Distribution", itemizable_id: distribution.id, quantity: 16) - result = subject.new(distribution.reload).call expect(result.error).to eq("The following items have fallen below the minimum on hand quantity: Item 1, Item 2") @@ -31,13 +31,52 @@ end end - context "alert" do + context "when on hand quantity is above the minimum for the organization" do + let(:available_item) { create(:item, name: "Available Item", organization: organization, on_hand_minimum_quantity: 5, on_hand_recommended_quantity: 10) } + let!(:first_storage_location) do + first_storage_location = create(:storage_location, organization: organization) + TestInventory.create_inventory(first_storage_location.organization, { + first_storage_location.id => { + available_item.id => 9 + } + }) + + first_storage_location + end + let!(:second_storage_location) do + second_storage_location = create(:storage_location, organization: organization) + TestInventory.create_inventory(second_storage_location.organization, { + second_storage_location.id => { + available_item.id => 4 + } + }) + + second_storage_location + end + + context "when on hand quantity is below the minimum for one storage location" do + let(:distribution) { create(:distribution, storage_location_id: second_storage_location.id) } + before do + create(:line_item, item: available_item, itemizable_type: "Distribution", itemizable_id: distribution.id, quantity: 2) + end + it "should not set the error" do + result = subject.new(distribution.reload).call + + expect(result.error).to be_nil + end + end + end + + context "when on hand quantity is below the recommended amount for the organization" do + let(:somewhat_stocked_organization) { create(:organization) } + let(:first_item) { create(:item, name: "Item 1", organization: somewhat_stocked_organization, on_hand_minimum_quantity: 5, on_hand_recommended_quantity: 10) } + let(:second_item) { create(:item, name: "Item 2", organization: somewhat_stocked_organization, on_hand_minimum_quantity: 5, on_hand_recommended_quantity: 10) } let(:storage_location) do - storage_location = create(:storage_location, organization: organization) + storage_location = create(:storage_location, organization: somewhat_stocked_organization) TestInventory.create_inventory(storage_location.organization, { storage_location.id => { - item1.id => 9, - item2.id => 9 + first_item.id => 9, + second_item.id => 9 } }) @@ -46,8 +85,8 @@ it "should set the alert" do distribution = create(:distribution, storage_location_id: storage_location.id) - create(:line_item, item: item1, itemizable_type: "Distribution", itemizable_id: distribution.id, quantity: 16) - create(:line_item, item: item2, itemizable_type: "Distribution", itemizable_id: distribution.id, quantity: 16) + create(:line_item, item: first_item, itemizable_type: "Distribution", itemizable_id: distribution.id, quantity: 16) + create(:line_item, item: second_item, itemizable_type: "Distribution", itemizable_id: distribution.id, quantity: 16) result = subject.new(distribution.reload).call From 01a3d870169a464e0d3181e8e68c40cb4ce29b26 Mon Sep 17 00:00:00 2001 From: McEileen Date: Tue, 26 Nov 2024 21:00:00 -0500 Subject: [PATCH 2/7] display correct alerts when distribution causes distinct items to dip below recommended and minimum levels --- app/controllers/distributions_controller.rb | 12 +++-- app/services/inventory_check_service.rb | 22 ++++----- .../distributions_controller_spec.rb | 47 ++++++++++++++----- spec/factories/storage_locations.rb | 33 +++++++++++++ spec/services/inventory_check_service_spec.rb | 10 ++-- 5 files changed, 91 insertions(+), 33 deletions(-) diff --git a/app/controllers/distributions_controller.rb b/app/controllers/distributions_controller.rb index a394087146..5c9cef2756 100644 --- a/app/controllers/distributions_controller.rb +++ b/app/controllers/distributions_controller.rb @@ -316,11 +316,13 @@ def filter_params def perform_inventory_check inventory_check_result = InventoryCheckService.new(@distribution).call - if inventory_check_result.error.present? - flash[:error] = inventory_check_result.error - end - if inventory_check_result.alert.present? - flash[:alert] = inventory_check_result.alert + if inventory_check_result.minimum_alert.present? && inventory_check_result.recommended_alert.present? + flash[:alert] = inventory_check_result.minimum_alert + flash[:alert] += inventory_check_result.recommended_alert + elsif inventory_check_result.minimum_alert.present? + flash[:alert] = inventory_check_result.minimum_alert + elsif inventory_check_result.recommended_alert.present? + flash[:alert] = inventory_check_result.recommended_alert end end end diff --git a/app/services/inventory_check_service.rb b/app/services/inventory_check_service.rb index edf45afa49..e5f63c3614 100644 --- a/app/services/inventory_check_service.rb +++ b/app/services/inventory_check_service.rb @@ -1,33 +1,33 @@ class InventoryCheckService - attr_reader :error, :alert + attr_reader :minimum_alert, :recommended_alert def initialize(distribution) @distribution = distribution - @alert = nil - @error = nil + @minimum_alert = nil + @recommended_alert = nil end def call @inventory = View::Inventory.new(@distribution.organization_id) unless items_below_minimum_quantity.empty? - set_error + set_minimum_alert end unless deduplicate_items_below_recommended_quantity.empty? - set_alert + set_recommended_alert end self end - def set_error - @error = "The following items have fallen below the minimum " \ - "on hand quantity: #{items_below_minimum_quantity.map(&:name).sort.join(", ")}" + def set_minimum_alert + @minimum_alert = "The following items have fallen below the minimum " \ + "on hand quantity, bank-wide: #{items_below_minimum_quantity.map(&:name).sort.join(", ")}" end - def set_alert - @alert = "The following items have fallen below the recommended " \ - "on hand quantity: #{deduplicate_items_below_recommended_quantity.map(&:name).sort.join(", ")}" + def set_recommended_alert + @recommended_alert = "The following items have fallen below the recommended " \ + "on hand quantity, bank-wide: #{deduplicate_items_below_recommended_quantity.map(&:name).sort.join(", ")}" end def items_below_minimum_quantity diff --git a/spec/controllers/distributions_controller_spec.rb b/spec/controllers/distributions_controller_spec.rb index ea47e3934a..50987e7e68 100644 --- a/spec/controllers/distributions_controller_spec.rb +++ b/spec/controllers/distributions_controller_spec.rb @@ -32,7 +32,7 @@ it "does not display an error" do subject - expect(flash[:error]).to be_nil + expect(flash[:alert]).to be_nil end context "when distribution causes inventory to fall below minimum quantity for a storage location" do @@ -58,8 +58,8 @@ end context "when distribution causes inventory quantity to be below minimum quantity for an organization" do - let(:item) { create(:item, name: "Item 1", organization: organization, on_hand_minimum_quantity: 5) } - let(:storage_location) { create(:storage_location, :with_items, item: item, item_quantity: 20, organization: organization) } + let(:first_item) { create(:item, name: "Item 1", organization: organization, on_hand_minimum_quantity: 5) } + let(:storage_location) { create(:storage_location, :with_items, item: first_item, item_quantity: 20, organization: organization) } let(:params) do { organization_name: organization.id, @@ -79,7 +79,32 @@ it "redirects with a flash notice and a flash error" do expect(subject).to have_http_status(:redirect) expect(flash[:notice]).to eq("Distribution created!") - expect(flash[:error]).to eq("The following items have fallen below the minimum on hand quantity: Item 1") + expect(flash[:alert]).to eq("The following items have fallen below the minimum on hand quantity, bank-wide: Item 1") + end + + context "when distribution causes inventory quantity to be below recommended quantity for an organization" do + let(:second_item) { create(:item, name: "Item 2", organization: organization, on_hand_minimum_quantity: 5, on_hand_recommended_quantity: 10) } + let(:storage_location) { create(:storage_location, :with_items_mixed, item: first_item, second_item: second_item, item_quantity: 20, organization: organization) } + let(:params) do + { + organization_name: organization.id, + distribution: { + partner_id: partner.id, + storage_location_id: storage_location.id, + line_items_attributes: + { + "0": { item_id: storage_location.items.first.id, quantity: 18 }, + "1": { item_id: storage_location.items.second.id, quantity: 15 } + } + } + } + end + it "displays an error for both minimum and recommended quantity for an organization" do + expect(subject).to have_http_status(:redirect) + expect(flash[:notice]).to eq("Distribution created!") + expect(flash[:alert]).to include("The following items have fallen below the recommended on hand quantity, bank-wide: Item 2") + expect(flash[:alert]).to include("The following items have fallen below the minimum on hand quantity, bank-wide: Item 1") + end end end @@ -115,10 +140,9 @@ it "redirects with a flash notice and a flash error" do expect(subject).to have_http_status(:redirect) expect(flash[:notice]).to eq("Distribution created!") - expect(flash[:error]).to include("The following items have fallen below the minimum on hand quantity") - expect(flash[:error]).to include("Item 1") - expect(flash[:error]).to include("Item 2") - expect(flash[:alert]).to be_nil + expect(flash[:alert]).to include("The following items have fallen below the minimum on hand quantity, bank-wide") + expect(flash[:alert]).to include("Item 1") + expect(flash[:alert]).to include("Item 2") end end @@ -154,7 +178,7 @@ it "redirects with a flash notice and a flash alert" do expect(subject).to have_http_status(:redirect) expect(flash[:notice]).to eq("Distribution created!") - expect(flash[:alert]).to eq("The following items have fallen below the recommended on hand quantity: Item 1, Item 2") + expect(flash[:alert]).to eq("The following items have fallen below the recommended on hand quantity, bank-wide: Item 1, Item 2") end end @@ -217,7 +241,7 @@ it "redirects with a flash notice and a flash error" do expect(subject).to have_http_status(:redirect) expect(flash[:notice]).to eq("Distribution updated!") - expect(flash[:alert]).to eq("The following items have fallen below the recommended on hand quantity: Item 1, Item 2") + expect(flash[:alert]).to eq("The following items have fallen below the recommended on hand quantity, bank-wide: Item 1, Item 2") end end @@ -254,8 +278,7 @@ it "redirects with a flash notice and a flash error" do expect(subject).to have_http_status(:redirect) expect(flash[:notice]).to eq("Distribution updated!") - expect(flash[:error]).to eq("The following items have fallen below the minimum on hand quantity: Item 1, Item 2") - expect(flash[:alert]).to be_nil + expect(flash[:alert]).to eq("The following items have fallen below the minimum on hand quantity, bank-wide: Item 1, Item 2") end end diff --git a/spec/factories/storage_locations.rb b/spec/factories/storage_locations.rb index 1c479fa5b3..1b59d4a34d 100644 --- a/spec/factories/storage_locations.rb +++ b/spec/factories/storage_locations.rb @@ -59,5 +59,38 @@ end end end + + trait :with_items_mixed do + with_items + + transient do + second_item_quantity { 100 } + second_item { nil } + end + + after(:create) do |storage_location, evaluator| + if evaluator.second_item.nil? && !evaluator.second_item_count.zero? + second_item_count = evaluator.item_count + + TestInventory.create_inventory( + storage_location.organization, { + storage_location.id => (0...second_item_count).to_h do + second_item = create(:second_item, organization_id: storage_location.organization_id) + [second_item.id, evaluator.item_quantity] + end + } + ) + elsif evaluator.second_item + second_item = evaluator.second_item + second_item.save if second_item.new_record? + TestInventory.create_inventory( + storage_location.organization, + { + storage_location.id => {second_item.id => evaluator.item_quantity} + } + ) + end + end + end end end diff --git a/spec/services/inventory_check_service_spec.rb b/spec/services/inventory_check_service_spec.rb index 33dc91debc..58162d0e96 100644 --- a/spec/services/inventory_check_service_spec.rb +++ b/spec/services/inventory_check_service_spec.rb @@ -26,8 +26,8 @@ it "should set the error" do result = subject.new(distribution.reload).call - expect(result.error).to eq("The following items have fallen below the minimum on hand quantity: Item 1, Item 2") - expect(result.alert).to be_nil + expect(result.minimum_alert).to eq("The following items have fallen below the minimum on hand quantity, bank-wide: Item 1, Item 2") + expect(result.recommended_alert).to be_nil end end @@ -62,7 +62,7 @@ it "should not set the error" do result = subject.new(distribution.reload).call - expect(result.error).to be_nil + expect(result.minimum_alert).to be_nil end end end @@ -90,8 +90,8 @@ result = subject.new(distribution.reload).call - expect(result.alert).to eq("The following items have fallen below the recommended on hand quantity: Item 1, Item 2") - expect(result.error).to be_nil + expect(result.recommended_alert).to eq("The following items have fallen below the recommended on hand quantity, bank-wide: Item 1, Item 2") + expect(result.minimum_alert).to be_nil end end end From 7b878db317c3718b761e2472c971fc22f9d5561f Mon Sep 17 00:00:00 2001 From: McEileen Date: Tue, 26 Nov 2024 21:32:52 -0500 Subject: [PATCH 3/7] refine recommended quantity error to occur based on org quantity, not storage location quantity --- app/services/inventory_check_service.rb | 2 +- spec/services/inventory_check_service_spec.rb | 56 +++++++++++++++++-- 2 files changed, 53 insertions(+), 5 deletions(-) diff --git a/app/services/inventory_check_service.rb b/app/services/inventory_check_service.rb index e5f63c3614..72886dcdb4 100644 --- a/app/services/inventory_check_service.rb +++ b/app/services/inventory_check_service.rb @@ -48,7 +48,7 @@ def items_below_recommended_quantity # Done this way to prevent N+1 query on items unless @items_below_recommended_quantity item_ids = @distribution.line_items.select do |line_item| - quantity = @inventory.quantity_for(storage_location: @distribution.storage_location_id, item_id: line_item.item_id) + quantity = @inventory.quantity_for(item_id: line_item.item_id) quantity < (line_item.item.on_hand_recommended_quantity || 0) end.map(&:item_id) diff --git a/spec/services/inventory_check_service_spec.rb b/spec/services/inventory_check_service_spec.rb index 58162d0e96..9182d68eb8 100644 --- a/spec/services/inventory_check_service_spec.rb +++ b/spec/services/inventory_check_service_spec.rb @@ -67,11 +67,58 @@ end end + context "when on hand quantity is above the recommended amount for the organization" do + let(:first_item) { create(:item, name: "Item 1", organization: organization, on_hand_minimum_quantity: 5, on_hand_recommended_quantity: 10) } + let!(:storage_location) do + storage_location = create(:storage_location, organization: organization) + TestInventory.create_inventory(storage_location.organization, { + storage_location.id => { + first_item.id => 20 + } + }) + + storage_location + end + let(:distribution) { create(:distribution, storage_location_id: storage_location.id) } + before do + create(:line_item, item: first_item, itemizable_type: "Distribution", itemizable_id: distribution.id, quantity: 2) + end + it "should not set the alert" do + result = subject.new(distribution.reload).call + + expect(result.recommended_alert).to be_nil + expect(result.minimum_alert).to be_nil + end + + context "when on hand quantity is below the recommended amount for one storage location" do + let!(:second_storage_location) do + second_storage_location = create(:storage_location, organization: organization) + TestInventory.create_inventory(second_storage_location.organization, { + second_storage_location.id => { + first_item.id => 8 + } + }) + + second_storage_location + end + let(:distribution) { create(:distribution, storage_location_id: second_storage_location.id) } + before do + create(:line_item, item: first_item, itemizable_type: "Distribution", itemizable_id: distribution.id, quantity: 2) + end + it "should not set the alert" do + result = subject.new(distribution.reload).call + + expect(result.recommended_alert).to be_nil + expect(result.minimum_alert).to be_nil + end + end + end + context "when on hand quantity is below the recommended amount for the organization" do let(:somewhat_stocked_organization) { create(:organization) } let(:first_item) { create(:item, name: "Item 1", organization: somewhat_stocked_organization, on_hand_minimum_quantity: 5, on_hand_recommended_quantity: 10) } let(:second_item) { create(:item, name: "Item 2", organization: somewhat_stocked_organization, on_hand_minimum_quantity: 5, on_hand_recommended_quantity: 10) } - let(:storage_location) do + let!(:storage_location) do storage_location = create(:storage_location, organization: somewhat_stocked_organization) TestInventory.create_inventory(storage_location.organization, { storage_location.id => { @@ -82,12 +129,13 @@ storage_location end - - it "should set the alert" do - distribution = create(:distribution, storage_location_id: storage_location.id) + let(:distribution) { create(:distribution, storage_location_id: storage_location.id) } + before do create(:line_item, item: first_item, itemizable_type: "Distribution", itemizable_id: distribution.id, quantity: 16) create(:line_item, item: second_item, itemizable_type: "Distribution", itemizable_id: distribution.id, quantity: 16) + end + it "should set the alert" do result = subject.new(distribution.reload).call expect(result.recommended_alert).to eq("The following items have fallen below the recommended on hand quantity, bank-wide: Item 1, Item 2") From 3d1ec15c44aeb2f42f1f5f50da0869d15b7b4d97 Mon Sep 17 00:00:00 2001 From: McEileen Date: Tue, 26 Nov 2024 22:24:50 -0500 Subject: [PATCH 4/7] fix distribution system specs that failed due to updated error message content --- spec/system/distribution_system_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/system/distribution_system_spec.rb b/spec/system/distribution_system_spec.rb index 923abd90ee..5a98ee1ba0 100644 --- a/spec/system/distribution_system_spec.rb +++ b/spec/system/distribution_system_spec.rb @@ -233,7 +233,7 @@ end expect(page).not_to have_content('New Distribution') - expect(page).to have_content("The following items have fallen below the minimum on hand quantity: #{item.name}") + expect(page).to have_content("The following items have fallen below the minimum on hand quantity, bank-wide: #{item.name}") end end @@ -268,7 +268,7 @@ click_button "Yes, it's correct" end - expect(page).to have_content("The following items have fallen below the recommended on hand quantity: #{item.name}") + expect(page).to have_content("The following items have fallen below the recommended on hand quantity, bank-wide: #{item.name}") end end From c198965e0bb0d98ed15d174315f2a5935ed38df9 Mon Sep 17 00:00:00 2001 From: McEileen Date: Sat, 30 Nov 2024 12:59:58 -0500 Subject: [PATCH 5/7] display minimum and recommended amount error messages on separate lines --- app/controllers/distributions_controller.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/controllers/distributions_controller.rb b/app/controllers/distributions_controller.rb index d218049017..d75cf59761 100644 --- a/app/controllers/distributions_controller.rb +++ b/app/controllers/distributions_controller.rb @@ -318,6 +318,7 @@ def perform_inventory_check if inventory_check_result.minimum_alert.present? && inventory_check_result.recommended_alert.present? flash[:alert] = inventory_check_result.minimum_alert + flash[:alert] += "\n" flash[:alert] += inventory_check_result.recommended_alert elsif inventory_check_result.minimum_alert.present? flash[:alert] = inventory_check_result.minimum_alert From 6d00cbc43dd8c544400c2988404801ad2dc7fc4d Mon Sep 17 00:00:00 2001 From: McEileen Date: Sun, 1 Dec 2024 17:57:41 -0500 Subject: [PATCH 6/7] refactor inventory check alert messages and related spec --- app/controllers/distributions_controller.rb | 12 ++----- .../distributions_controller_spec.rb | 9 ++++- spec/factories/storage_locations.rb | 33 ------------------- 3 files changed, 11 insertions(+), 43 deletions(-) diff --git a/app/controllers/distributions_controller.rb b/app/controllers/distributions_controller.rb index d75cf59761..6a3a55146f 100644 --- a/app/controllers/distributions_controller.rb +++ b/app/controllers/distributions_controller.rb @@ -316,14 +316,8 @@ def filter_params def perform_inventory_check inventory_check_result = InventoryCheckService.new(@distribution).call - if inventory_check_result.minimum_alert.present? && inventory_check_result.recommended_alert.present? - flash[:alert] = inventory_check_result.minimum_alert - flash[:alert] += "\n" - flash[:alert] += inventory_check_result.recommended_alert - elsif inventory_check_result.minimum_alert.present? - flash[:alert] = inventory_check_result.minimum_alert - elsif inventory_check_result.recommended_alert.present? - flash[:alert] = inventory_check_result.recommended_alert - end + alerts = [inventory_check_result.minimum_alert, inventory_check_result.recommended_alert] + merged_alert = alerts.compact.join("\n") + flash[:alert] = merged_alert if merged_alert.present? end end diff --git a/spec/controllers/distributions_controller_spec.rb b/spec/controllers/distributions_controller_spec.rb index 50987e7e68..8e6befd335 100644 --- a/spec/controllers/distributions_controller_spec.rb +++ b/spec/controllers/distributions_controller_spec.rb @@ -84,7 +84,7 @@ context "when distribution causes inventory quantity to be below recommended quantity for an organization" do let(:second_item) { create(:item, name: "Item 2", organization: organization, on_hand_minimum_quantity: 5, on_hand_recommended_quantity: 10) } - let(:storage_location) { create(:storage_location, :with_items_mixed, item: first_item, second_item: second_item, item_quantity: 20, organization: organization) } + let(:storage_location) { create(:storage_location, organization: organization) } let(:params) do { organization_name: organization.id, @@ -99,6 +99,13 @@ } } end + before do + TestInventory.create_inventory(organization, { + storage_location.id => { + first_item.id => 20, + second_item.id => 20} + }) + end it "displays an error for both minimum and recommended quantity for an organization" do expect(subject).to have_http_status(:redirect) expect(flash[:notice]).to eq("Distribution created!") diff --git a/spec/factories/storage_locations.rb b/spec/factories/storage_locations.rb index 1b59d4a34d..1c479fa5b3 100644 --- a/spec/factories/storage_locations.rb +++ b/spec/factories/storage_locations.rb @@ -59,38 +59,5 @@ end end end - - trait :with_items_mixed do - with_items - - transient do - second_item_quantity { 100 } - second_item { nil } - end - - after(:create) do |storage_location, evaluator| - if evaluator.second_item.nil? && !evaluator.second_item_count.zero? - second_item_count = evaluator.item_count - - TestInventory.create_inventory( - storage_location.organization, { - storage_location.id => (0...second_item_count).to_h do - second_item = create(:second_item, organization_id: storage_location.organization_id) - [second_item.id, evaluator.item_quantity] - end - } - ) - elsif evaluator.second_item - second_item = evaluator.second_item - second_item.save if second_item.new_record? - TestInventory.create_inventory( - storage_location.organization, - { - storage_location.id => {second_item.id => evaluator.item_quantity} - } - ) - end - end - end end end From e61a8123d156ec90485461c0741786a9f39eafd8 Mon Sep 17 00:00:00 2001 From: McEileen Date: Sun, 1 Dec 2024 18:07:51 -0500 Subject: [PATCH 7/7] fix lint errors --- spec/controllers/distributions_controller_spec.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/spec/controllers/distributions_controller_spec.rb b/spec/controllers/distributions_controller_spec.rb index 8e6befd335..a04a165377 100644 --- a/spec/controllers/distributions_controller_spec.rb +++ b/spec/controllers/distributions_controller_spec.rb @@ -103,7 +103,8 @@ TestInventory.create_inventory(organization, { storage_location.id => { first_item.id => 20, - second_item.id => 20} + second_item.id => 20 + } }) end it "displays an error for both minimum and recommended quantity for an organization" do