diff --git a/app/services/itemizable_update_service.rb b/app/services/itemizable_update_service.rb index 7b1c25e382..cd095b4100 100644 --- a/app/services/itemizable_update_service.rb +++ b/app/services/itemizable_update_service.rb @@ -37,7 +37,7 @@ def call(itemizable:, params: {}, event_class: nil) # TODO once event sourcing has been out for long enough, we can safely remove this if Event.where(eventable: itemizable).none? || UpdateExistingEvent.where(eventable: itemizable).any? UpdateExistingEvent.publish(itemizable, previous, original_storage_location) - elsif inventory_changes?(previous, params[:line_items_attributes]) + elsif inventory_changes?(previous, params[:line_items_attributes]) || itemizable.storage_location_id != original_storage_location.id event_class&.publish(itemizable) end end diff --git a/spec/services/itemizable_update_service_spec.rb b/spec/services/itemizable_update_service_spec.rb index 4345e23805..5977f81df0 100644 --- a/spec/services/itemizable_update_service_spec.rb +++ b/spec/services/itemizable_update_service_spec.rb @@ -215,6 +215,24 @@ expect(UpdateExistingEvent.count).to eq(1) expect(View::Inventory.total_inventory(organization.id)).to eq(75) # 40 - 5 (item1) - 10 (item2) + 50 (item3) end + + it "should send an event when the storage location changes" do + DonationEvent.publish(itemizable) + expect(DonationEvent.count).to eq(1) + + # attributes that keep quantities the same but change storage location + same_quantities_attributes = { + storage_location_id: new_storage_location.id, + line_items_attributes: { + "0" => {item_id: item1.id, quantity: 10}, + "1" => {item_id: item2.id, quantity: 10} + } + } + + described_class.call(itemizable: itemizable, params: same_quantities_attributes, event_class: DonationEvent) + + expect(DonationEvent.count).to eq(2) + end end describe "with distributions" do before(:each) do