-
-
Notifications
You must be signed in to change notification settings - Fork 504
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
Fix event timing #3954
Fix event timing #3954
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ def self.publish(audit) | |
create( | ||
eventable: audit, | ||
organization_id: audit.organization_id, | ||
event_time: Time.zone.now, | ||
event_time: audit.updated_at, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah -- this one uses There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because audits have a state (they have to be finalized) and they can't be edited, so updated_at is the correct thing. For the others, they can be edited but they are always "valid", so created_at is correct. |
||
data: EventTypes::AuditPayload.new( | ||
storage_location_id: audit.storage_location_id, | ||
items: EventTypes::EventLineItem.from_line_items(audit.line_items, to: audit.storage_location_id) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,27 +8,37 @@ def on(*event_types, &block) | |
end | ||
end | ||
|
||
# @param organization_id | ||
# @param organization_id [Integer] | ||
# @param first_event [Integer] | ||
# @param last_event [Integer] | ||
# @param validate [Boolean] | ||
# @return [EventTypes::Inventory] | ||
def inventory_for(organization_id) | ||
def inventory_for(organization_id, first_event: nil, last_event: nil, validate: false) | ||
events = Event.for_organization(organization_id) | ||
if first_event | ||
events = events.where("id >= ?", first_event) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should these go off of one of the timestamps instead of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Honestly I don't have a good use for this just yet, it's for the eventual "start from last snapshot" behavior. We can double check if once that's in. |
||
end | ||
if last_event | ||
events = events.where("id <= ?", last_event) | ||
end | ||
inventory = EventTypes::Inventory.from(organization_id) | ||
events.group_by { |e| [e.eventable_type, e.eventable_id] }.each do |_, event_batch| | ||
last_event = event_batch.max_by(&:event_time) | ||
handle(last_event, inventory) | ||
last_grouped_event = event_batch.max_by(&:updated_at) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Conceptually, will there ever be events that have a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nope. |
||
handle(last_grouped_event, inventory, validate: validate) | ||
end | ||
inventory | ||
end | ||
|
||
# @param event [Event] | ||
# @param inventory [Inventory] | ||
def handle(event, inventory) | ||
# @param validate [Boolean] | ||
def handle(event, inventory, validate: false) | ||
handler = @handlers[event.class] | ||
if handler.nil? | ||
Rails.logger.warn("No handler found for #{event.class}, skipping") | ||
return | ||
end | ||
handler.call(event, inventory) | ||
handler.call(event, inventory, validate: validate) | ||
end | ||
|
||
# @param payload [EventTypes::InventoryPayload] | ||
|
@@ -46,7 +56,6 @@ def handle_inventory_event(payload, inventory, validate: true) | |
|
||
# @param payload [EventTypes::InventoryPayload] | ||
# @param inventory [EventTypes::Inventory] | ||
# @param validate [Boolean] | ||
def handle_audit_event(payload, inventory) | ||
payload.items.each do |line_item| | ||
inventory.set_item_quantity(item_id: line_item.item_id, | ||
|
@@ -59,15 +68,15 @@ def handle_audit_event(payload, inventory) | |
on DonationEvent, DistributionEvent, AdjustmentEvent, PurchaseEvent, | ||
TransferEvent, DistributionDestroyEvent, DonationDestroyEvent, | ||
PurchaseDestroyEvent, TransferDestroyEvent, | ||
KitAllocateEvent, KitDeallocateEvent do |event, inventory| | ||
handle_inventory_event(event.data, inventory, validate: false) | ||
KitAllocateEvent, KitDeallocateEvent do |event, inventory, validate: false| | ||
handle_inventory_event(event.data, inventory, validate: validate) | ||
end | ||
|
||
on AuditEvent do |event, inventory| | ||
on AuditEvent do |event, inventory, validate: false| | ||
handle_audit_event(event.data, inventory) | ||
end | ||
|
||
on SnapshotEvent do |event, inventory| | ||
on SnapshotEvent do |event, inventory, validate: false| | ||
inventory.storage_locations.clear | ||
inventory.storage_locations.merge!(event.data.storage_locations) | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class FixEventTimes < ActiveRecord::Migration[7.0] | ||
def change | ||
Event.where(type: %i(DistributionEvent DonationEvent PurchaseEvent)).find_each do |event| | ||
next if event.eventable.nil? | ||
|
||
event.update_attribute(:event_time, event.eventable.created_at) | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The passed in adjustment from AdjustmentCreateService, weirdly, could be an exisiting Adjustment rather than a freshly created one. Maybe so this gets a bit weird with that case; I'm not sure if
adjustment.updated_at
would be better or not.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adjustments are never updated, just created.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK for this one, I guess
AdjustmentCreateService
initialize we should sometime take out the first bit where it can be handed an Adjustment -- I verified that nothing does call it with one.