diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index fc9a7dccba..84863fa2fa 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -86,10 +86,6 @@ Layout/ExtraSpacing: - 'Guardfile' - 'app/models/barcode_item.rb' - 'app/queries/items_by_storage_collection_query.rb' - - 'app/queries/items_in_query.rb' - - 'app/queries/items_in_total_query.rb' - - 'app/queries/items_out_query.rb' - - 'app/queries/items_out_total_query.rb' - 'config/environments/development.rb' - 'config/environments/test.rb' - 'config/puma.rb' @@ -169,10 +165,6 @@ Layout/MultilineMethodCallIndentation: - 'app/models/partner_distribution.rb' - 'app/models/storage_location.rb' - 'app/queries/items_by_storage_collection_query.rb' - - 'app/queries/items_in_query.rb' - - 'app/queries/items_in_total_query.rb' - - 'app/queries/items_out_query.rb' - - 'app/queries/items_out_total_query.rb' - 'app/services/reports/acquisition_report_service.rb' - 'app/services/reports/adult_incontinence_report_service.rb' - 'app/services/reports/children_served_report_service.rb' diff --git a/app/controllers/storage_locations_controller.rb b/app/controllers/storage_locations_controller.rb index 620c3a40df..469e03fb32 100644 --- a/app/controllers/storage_locations_controller.rb +++ b/app/controllers/storage_locations_controller.rb @@ -54,15 +54,12 @@ def edit @storage_location = current_organization.storage_locations.find(params[:id]) end - # TODO: Move these queries to Query Object def show + setup_date_range_picker @storage_location = current_organization.storage_locations.find(params[:id]) version_date = params[:version_date].presence&.to_date - # TODO: Find a way to do these with less hard SQL. These queries have to be manually updated because they're not in-sync with the Model - @items_out = ItemsOutQuery.new(organization: current_organization, storage_location: @storage_location).call - @items_out_total = ItemsOutTotalQuery.new(organization: current_organization, storage_location: @storage_location).call - @items_in = ItemsInQuery.new(organization: current_organization, storage_location: @storage_location).call - @items_in_total = ItemsInTotalQuery.new(organization: current_organization, storage_location: @storage_location).call + @flow = ItemsFlowQuery.new(organization: current_organization, storage_location: @storage_location, + date_range: helpers.selected_range).call if View::Inventory.within_snapshot?(current_organization.id, version_date) @inventory = View::Inventory.new(current_organization.id, event_time: version_date) else diff --git a/app/events/audit_event.rb b/app/events/audit_event.rb index aab873ada2..f065c7f81d 100644 --- a/app/events/audit_event.rb +++ b/app/events/audit_event.rb @@ -14,4 +14,9 @@ def self.publish(audit) ) ) end + + # Sets quantities outright rather than editing an earlier version. + def is_edit? + false + end end diff --git a/app/events/inventory_aggregate.rb b/app/events/inventory_aggregate.rb index bfc6a2bdff..e561add670 100644 --- a/app/events/inventory_aggregate.rb +++ b/app/events/inventory_aggregate.rb @@ -34,14 +34,13 @@ 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) - yield last_grouped_event, inventory if block_given? - next + previous_event = nil + # only edits are diffed against the previous version of their record + if last_grouped_event.is_edit? + key = [last_grouped_event.eventable_type, last_grouped_event.eventable_id] + previous_event = event_hash[key] + event_hash[key] = last_grouped_event end - previous_event = event_hash[[last_grouped_event.eventable_type, last_grouped_event.eventable_id]] - event_hash[[last_grouped_event.eventable_type, last_grouped_event.eventable_id]] = last_grouped_event handle(last_grouped_event, inventory, validate: validate, previous_event: previous_event) yield last_grouped_event, inventory if block_given? end diff --git a/app/events/kit_allocate_event.rb b/app/events/kit_allocate_event.rb index aa34b0c078..2ffe82ccdf 100644 --- a/app/events/kit_allocate_event.rb +++ b/app/events/kit_allocate_event.rb @@ -30,4 +30,9 @@ def self.publish(kit, storage_location, quantity) ) ) end + + # Each allocation is its own change rather than an edit of the kit. + def is_edit? + false + end end diff --git a/app/events/kit_deallocate_event.rb b/app/events/kit_deallocate_event.rb index 88521212e1..6f13f1ae39 100644 --- a/app/events/kit_deallocate_event.rb +++ b/app/events/kit_deallocate_event.rb @@ -30,4 +30,9 @@ def self.publish(kit, storage_location, quantity) ) ) end + + # Each deallocation is its own change rather than an edit of the kit. + def is_edit? + false + end end diff --git a/app/events/update_existing_event.rb b/app/events/update_existing_event.rb index 90133e2e39..99e311f898 100644 --- a/app/events/update_existing_event.rb +++ b/app/events/update_existing_event.rb @@ -60,4 +60,10 @@ def publish(itemizable, previous_line_items, original_storage_location) ) end end + + # Carries the difference from the previous version rather than a full + # version, so it is applied on its own instead of replacing an earlier event. + def is_edit? + false + end end diff --git a/app/models/event.rb b/app/models/event.rb index 44d9972948..a368e11106 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -54,6 +54,14 @@ def no_intervening_snapshot end end + # Whether events of this type edit their record: each one is a full version + # of the eventable (e.g. a donation, then each later edit of it) that replaces + # the previous event for that record, rather than a standalone change. + # @return [Boolean] + def is_edit? + true + end + # @return [Array