Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow editing of records pre-snapshot #4287

Merged
merged 6 commits into from
May 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion app/events/inventory_aggregate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ def inventory_for(organization_id, event_time: nil, validate: false)
event_hash = {}
events.group_by(&:group_id).each do |_, event_batch|
last_grouped_event = event_batch.max_by(&:updated_at)
# don't do grouping for UpdateExistingEvents
if event_batch.any? { |e| e.is_a?(UpdateExistingEvent) }
handle(last_grouped_event, inventory, validate: validate)
next
end
previous_event = event_hash[last_grouped_event.eventable]
event_hash[last_grouped_event.eventable] = last_grouped_event
handle(last_grouped_event, inventory, validate: validate, previous_event: previous_event)
Expand Down Expand Up @@ -104,7 +109,8 @@ def handle_audit_event(payload, inventory)
# diff previous event
on DonationEvent, DistributionEvent, AdjustmentEvent, PurchaseEvent,
TransferEvent, DistributionDestroyEvent, DonationDestroyEvent,
PurchaseDestroyEvent, TransferDestroyEvent do |event, inventory, validate: false, previous_event: nil|
PurchaseDestroyEvent, TransferDestroyEvent,
UpdateExistingEvent do |event, inventory, validate: false, previous_event: nil|
handle_inventory_event(event.data, inventory, validate: validate, previous_event: previous_event)
rescue InventoryError => e
e.event = event
Expand Down
58 changes: 58 additions & 0 deletions app/events/update_existing_event.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
class UpdateExistingEvent < Event
class << self
# @param line_items [Array<LineItem>]
# @param storage_location [StorageLocation]
# @param direction [Symbol]
# @return [Hash<Integer, EventTypes::EventLineItem>]
def item_quantities(line_items, storage_location, direction)
line_items.to_h do |line_item|
opts = (direction == :from) ?
{from: storage_location.id} :
{to: storage_location.id}
[line_item.item_id, EventTypes::EventLineItem.from_line_item(line_item, **opts)]
end
end

# @param previous [Hash<Integer, EventTypes::EventLineItem>]
# @param current [Hash<Integer, EventTypes::EventLineItem>]
# @return [Array<EventTypes::EventLineItem>]
def diff(previous, current)
previous.each do |id, event_item|
previous[id] = if current[id]
event_item.new(quantity: current[id].quantity - event_item.quantity)
else
event_item.new(quantity: -event_item.quantity)
end
end
all_items = previous.values
(current.keys - previous.keys).each do |id|
all_items.push(current[id]) # it's been added
end
all_items
end

# @param itemizable [Itemizable]
# @return [Symbol]
def direction(itemizable)
itemizable.is_a?(Distribution) ? :from : :to
end

# @param itemizable [Itemizable]
def publish(itemizable, previous_line_items)
dir = direction(itemizable)
previous_items = item_quantities(previous_line_items, itemizable.storage_location, dir)
current_items = item_quantities(itemizable.line_items, itemizable.storage_location, dir)
diff_items = diff(previous_items, current_items)

create(
eventable: itemizable,
group_id: "existing-#{itemizable.id}-#{SecureRandom.hex}",
organization_id: itemizable.organization_id,
event_time: Time.zone.now,
data: EventTypes::InventoryPayload.new(
items: diff_items
)
)
end
end
end
12 changes: 11 additions & 1 deletion app/services/itemizable_update_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ def self.call(itemizable:, type: :increase, params: {}, event_class: nil)
apply_change_method = (type == :increase) ? :increase_inventory : :decrease_inventory
undo_change_method = (type == :increase) ? :decrease_inventory : :increase_inventory

previous = 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?
previous = itemizable.line_items.map(&:dup)
end

line_item_attrs = Array.wrap(params[:line_items_attributes]&.values)
line_item_attrs.each { |attr| attr.delete(:id) }

Expand All @@ -27,7 +33,11 @@ def self.call(itemizable:, type: :increase, params: {}, event_class: nil)
params: params,
from_location: from_location,
to_location: to_location)
event_class&.publish(itemizable)
if previous
UpdateExistingEvent.publish(itemizable, previous)
else
event_class&.publish(itemizable)
end
end
end

Expand Down
57 changes: 57 additions & 0 deletions spec/events/inventory_aggregate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,63 @@
}
))
end

it "should handle multiple UpdateExisting events" do
TestInventory.create_inventory(organization,
{
storage_location1.id => {
item1.id => 70,
item2.id => 60,
item3.id => 20
}
})
donation = FactoryBot.create(:donation, organization: organization, storage_location: storage_location1)
donation.line_items << build(:line_item, quantity: 50, item: item1)
donation.line_items << build(:line_item, quantity: 30, item: item2)
donation.save!

attributes = {line_items_attributes: {"0": {item_id: item1.id, quantity: 40}, "1": {item_id: item2.id, quantity: 25}}}
ItemizableUpdateService.call(itemizable: donation, type: :increase, event_class: DonationEvent, params: attributes)

result = InventoryAggregate.inventory_for(organization.id)
expect(result).to eq(EventTypes::Inventory.new(
organization_id: organization.id,
storage_locations: {
storage_location1.id => EventTypes::EventStorageLocation.new(
id: storage_location1.id,
items: {
# (orig donation) 50 - (new donation) 40 = 10; (orig inventory)70 - (diff)10 = 60
# (orig donation) 30 - (new donation) 25 = 5; (orig inventory)60 - (diff)5 = 55
# no change to item3 so still 20
item1.id => EventTypes::EventItem.new(item_id: item1.id, quantity: 60, storage_location_id: storage_location1.id),
item2.id => EventTypes::EventItem.new(item_id: item2.id, quantity: 55, storage_location_id: storage_location1.id),
item3.id => EventTypes::EventItem.new(item_id: item3.id, quantity: 20, storage_location_id: storage_location1.id)
}
)
}
))

attributes = {line_items_attributes: {"0": {item_id: item1.id, quantity: 35}, "1": {item_id: item2.id, quantity: 30}}}
ItemizableUpdateService.call(itemizable: donation, type: :increase, event_class: DonationEvent, params: attributes)

result = InventoryAggregate.inventory_for(organization.id)
expect(result).to eq(EventTypes::Inventory.new(
organization_id: organization.id,
storage_locations: {
storage_location1.id => EventTypes::EventStorageLocation.new(
id: storage_location1.id,
items: {
# (orig donation) 50 - (new donation) 35 = 15; (orig inventory)70 - (diff)15 = 55
# item2 back to original 60
# no change to item3 so still 20
item1.id => EventTypes::EventItem.new(item_id: item1.id, quantity: 55, storage_location_id: storage_location1.id),
item2.id => EventTypes::EventItem.new(item_id: item2.id, quantity: 60, storage_location_id: storage_location1.id),
item3.id => EventTypes::EventItem.new(item_id: item3.id, quantity: 20, storage_location_id: storage_location1.id)
}
)
}
))
end
end

describe "validation" do
Expand Down
99 changes: 98 additions & 1 deletion spec/services/itemizable_update_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
let(:new_storage_location) { create(:storage_location, organization: organization, item_count: 0) }
let(:item1) { create(:item, organization: organization, name: "My Item 1") }
let(:item2) { create(:item, organization: organization, name: "My Item 2") }
let(:item3) { create(:item, organization: organization, name: "My Item 3") }
before(:each) do
TestInventory.create_inventory(storage_location.organization, {
storage_location.id => {
Expand Down Expand Up @@ -59,7 +60,7 @@
expect(storage_location.size).to eq(14)
expect(new_storage_location.size).to eq(20)
expect(itemizable.issued_at).to eq(2.days.ago)
expect(DonationEvent.count).to eq(1)
expect(UpdateExistingEvent.count).to eq(1)
end

it "should update quantity in different locations" do
Expand Down Expand Up @@ -128,4 +129,100 @@
expect { subject }.to raise_error(msg)
end
end

describe "events" do
before(:each) do
allow(Event).to receive(:read_events?).and_return(true)
end
describe "with donations" do
let(:itemizable) do
line_items = [
create(:line_item, item_id: item1.id, quantity: 10),
create(:line_item, item_id: item2.id, quantity: 10)
]
create(:donation,
organization: organization,
storage_location: storage_location,
line_items: line_items,
issued_at: 1.day.ago)
end
before(:each) do
allow(Event).to receive(:read_events?).and_return(true)
end
let(:attributes) do
{
issued_at: 2.days.ago,
line_items_attributes: {"0": {item_id: item1.id, quantity: 5}, "1": {item_id: item3.id, quantity: 50}}
}
end
it "should send an itemizable event if it already exists" do
DonationEvent.publish(itemizable)
expect(DonationEvent.count).to eq(1)
expect(View::Inventory.total_inventory(organization.id)).to eq(60)

described_class.call(itemizable: itemizable, params: attributes, type: :increase, event_class: DonationEvent)

expect(DonationEvent.count).to eq(2)
expect(View::Inventory.total_inventory(organization.id)).to eq(95)
end

it "should send an update event if it does not exist" do
expect(DonationEvent.count).to eq(0)
expect(View::Inventory.total_inventory(organization.id)).to eq(40)

described_class.call(itemizable: itemizable, params: attributes, type: :increase, event_class: DonationEvent)

expect(DonationEvent.count).to eq(0)
expect(UpdateExistingEvent.count).to eq(1)
expect(View::Inventory.total_inventory(organization.id)).to eq(75) # 40 - 5 (item1) - 10 (item2) + 50 (item3)
end
end
describe "with distributions" do
before(:each) do
TestInventory.create_inventory(storage_location.organization, {
storage_location.id => {
item3.id => 10
}
})
end
let(:itemizable) do
line_items = [
create(:line_item, item_id: item1.id, quantity: 5),
create(:line_item, item_id: item2.id, quantity: 5)
]
create(:distribution,
organization: organization,
storage_location: storage_location,
line_items: line_items,
issued_at: 1.day.ago)
end
let(:attributes) do
{
issued_at: 2.days.ago,
line_items_attributes: {"0": {item_id: item1.id, quantity: 2}, "1": {item_id: item3.id, quantity: 6}}
}
end
it "should send an itemizable event if it already exists" do
DistributionEvent.publish(itemizable)
expect(DistributionEvent.count).to eq(1)
expect(View::Inventory.total_inventory(organization.id)).to eq(40)

described_class.call(itemizable: itemizable, params: attributes, type: :decrease, event_class: DistributionEvent)

expect(DistributionEvent.count).to eq(2)
expect(View::Inventory.total_inventory(organization.id)).to eq(42)
end

it "should send an update event if it does not exist" do
expect(DistributionEvent.count).to eq(0)
expect(View::Inventory.total_inventory(organization.id)).to eq(50)

described_class.call(itemizable: itemizable, params: attributes, type: :decrease, event_class: DistributionEvent)

expect(DistributionEvent.count).to eq(0)
expect(UpdateExistingEvent.count).to eq(1)
expect(View::Inventory.total_inventory(organization.id)).to eq(52) # 50 + 3 (item1) + 5 (item2) +- 6 (item3)
end
end
end
end
Loading