Skip to content

Commit bfd1db6

Browse files
author
Olexii Kasianenko
committed
Enhance inventory flow query to include AuditEvent handling and update related specs for accurate quantity calculations
1 parent 0d50f31 commit bfd1db6

2 files changed

Lines changed: 10 additions & 6 deletions

File tree

app/queries/items_flow_query.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ def call
2222
or (e.type = 'PurchaseEvent' and (item->>'to_storage_location')::int = :id)
2323
or (e.type = 'AdjustmentEvent' and (item->>'to_storage_location')::int = :id)
2424
or (e.type = 'TransferEvent' and (item->>'to_storage_location')::int = :id)
25+
or (e.type = 'AuditEvent' and (item->>'to_storage_location')::int = :id)
2526
and e.organization_id = :organization_id
2627
then (item->>'quantity')::int
2728
else 0
@@ -31,6 +32,7 @@ def call
3132
when (e.type = 'DistributionEvent' and (item->>'from_storage_location')::int = :id)
3233
or (e.type = 'AdjustmentEvent' and (item->>'from_storage_location')::int = :id)
3334
or (e.type = 'TransferEvent' and (item->>'from_storage_location')::int = :id)
35+
or (e.type = 'AuditEvent' and (item->>'from_storage_location')::int = :id)
3436
and e.organization_id = :organization_id
3537
then case when (item->>'quantity')::int < 0 then -(item->>'quantity')::int else (item->>'quantity')::int end
3638
else 0

spec/queries/items_flow_query_spec.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,19 @@
1414
quantity_in: 10,
1515
quantity_out: 5,
1616
change: 5,
17-
total_quantity_in: 16,
17+
total_quantity_in: 19,
1818
total_quantity_out: 7,
19-
total_change: 9
19+
total_change: 12
2020
},
2121
{
2222
item_id: items[1].id,
2323
item_name: items[1].name,
24-
quantity_in: 6,
24+
quantity_in: 9,
2525
quantity_out: 2,
26-
change: 4,
27-
total_quantity_in: 16,
26+
change: 7,
27+
total_quantity_in: 19,
2828
total_quantity_out: 7,
29-
total_change: 9
29+
total_change: 12
3030
}
3131
].map(&:with_indifferent_access)
3232
end
@@ -40,6 +40,8 @@
4040
AdjustmentEvent.publish(adjustment)
4141
transfer = create(:transfer, :with_items, item: items[1], item_quantity: 2, from: storage_location, to: create(:storage_location))
4242
TransferEvent.publish(transfer)
43+
audit = create(:audit, :with_items, item: items[1], item_quantity: 3, adjustment: adjustment, storage_location: storage_location)
44+
AuditEvent.publish(audit)
4345
end
4446

4547
subject { described_class.new(organization: organization, storage_location: storage_location).call }

0 commit comments

Comments
 (0)