refactor(inventory): extract inventory management logic to InventoryManager service#360
Merged
nikhilb2 merged 2 commits intoMay 7, 2026
Conversation
- Add deterministic ORDER BY CASE in update_quantity and check_availability queries so a company-scoped row is always selected before a global (NULL company_id) row when both match the OR filter - Import sqlalchemy.case in inventory_service.py - Add 4 new tests: - test_prefers_company_row_over_global_row (update_quantity) - test_falls_back_to_global_row_when_only_global_exists (update_quantity) - test_uses_company_row_not_global_for_availability (check_availability) - test_falls_back_to_global_row_for_availability (check_availability)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #352
Extracts all inventory quantity operations from
InvoiceProcessorinto a dedicatedInventoryManagerservice class.Changes
New:
backend/src/services/inventory_service.pyInventoryManagerclass with focused, independently-testable methods:effect_for_voucher_type()update_quantity()check_availability()reverse_invoice_inventory()restore_invoice_inventory()apply_invoice_changes()apply_new_items()Refactored:
backend/src/services/invoice_processor.pyInvoiceProcessornow holdsself.inventory = InventoryManager(db)reverse_inventory,restore_inventory,apply_inventory_delta_for_updatedelegate toInventoryManagervalidate_itemsdelegates availability check toInventoryManager.check_availabilityapply_payloaddelegates new-item updates toInventoryManager.apply_new_itemschange_inventory_quantityandinventory_effect_for_voucher_typekept as backward-compatible shims so existing callers and tests continue to work unchangedNew:
backend/tests/services/test_inventory_service.py21 unit tests covering all
InventoryManagermethods across happy paths, edge cases, and error scenarios.Test Results