-
Notifications
You must be signed in to change notification settings - Fork 0
Refactoring Immutabilité + PartnerInventory #4
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
base: main
Are you sure you want to change the base?
Changes from all commits
56b6eed
fb35737
6bcbf05
c3195bb
c363e36
f9b03b8
05e7a4f
11c7c18
5272013
fddd3c5
30a8fd8
764ad30
f1daea3
756d493
0b6d38a
2464db2
a5c56a7
6a0e8b6
1533001
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 |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| # from app.errors import NotFound | ||
| from .context import Context | ||
| from .errors import NotFound | ||
| from domain.sales_report import SalesReport | ||
| from app.context import Context | ||
| from app.errors import NotFound | ||
| from infra.errors import DataIntegrityError | ||
|
|
||
|
|
||
| def get_dr_or_raise(ctx: Context, dr_id: int): | ||
|
|
@@ -15,3 +16,20 @@ def get_sr_or_raise(ctx: Context, sr_id: int): | |
| if not sr: | ||
| raise NotFound(f"sales report not found: {sr_id}") | ||
| return sr | ||
|
|
||
|
|
||
| def restore_quantities_or_raise( | ||
| ctx: Context, sr: SalesReport, autocommit: bool | ||
| ) -> None: | ||
| missing_items_ids = [] | ||
| for it in sr.items: | ||
| pi = ctx.pi_repo.get(sr.partner_id, it.book_id) | ||
| if pi is None: | ||
| missing_items_ids.append(it.book_id) | ||
| continue | ||
| pi = pi.restore_sale(it.quantity) | ||
| ctx.pi_repo.save(pi, autocommit=False) | ||
| if missing_items_ids: | ||
| raise DataIntegrityError( | ||
| f"Sales report with id {sr.id} contains following items ({', '.join(missing_items_ids)}), for which no inventory line exists" | ||
| ) | ||
|
Comment on lines
+32
to
+35
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,29 @@ | ||
| # from collections import defaultdict | ||
| from typing import List | ||
| from typing import Dict, List | ||
|
|
||
| from domain.sales_report import SalesReport | ||
| from infra.sql.sql_partner_inventory_repo import SqlPartnerInventoryRepo | ||
|
|
||
|
|
||
| def reports_by_partner( | ||
| reports: List[SalesReport], partner_id: str | ||
| ) -> List[SalesReport]: | ||
| return [r for r in reports if r.partner_id == partner_id] | ||
|
|
||
|
|
||
| def get_partner_inventory( | ||
| partner_id: str, pi_repo: SqlPartnerInventoryRepo | ||
| ) -> Dict[str, int]: | ||
| rows = ( | ||
| pi_repo.conn.cursor() | ||
| .execute( | ||
| """ | ||
| SELECT book_sku, current_quantity | ||
| FROM partner_inventories | ||
| WHERE partner_id = ? | ||
| """, | ||
| (partner_id,), | ||
| ) | ||
| .fetchall() | ||
| ) | ||
| return {sku: qty for sku, qty in rows} |
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
| @@ -1,5 +1,5 @@ | ||||
| from typing import List, Tuple | ||||
|
|
||||
| from domain.errors import InsufficientStock | ||||
| from domain.partner_inventory import PartnerInventory | ||||
| from domain.delivery_request import RequestItem, DeliveryRequest | ||||
| from domain.sales_report import AlreadyVoided, SalesReport, ReportItem | ||||
| from policies.identity import Forbidden, Role, Actor | ||||
|
|
@@ -10,16 +10,16 @@ | |||
| from policies.validations import ( | ||||
| validate_report_items_in_catalog, | ||||
| validate_request_items_in_catalog, | ||||
| validate_sales_report_against_stock, | ||||
| ) | ||||
| from .context import Context | ||||
| from .helpers import get_dr_or_raise, get_sr_or_raise | ||||
| from .helpers import get_dr_or_raise, get_sr_or_raise, restore_quantities_or_raise | ||||
| from .errors import ValidationError | ||||
| # from infra.errors import DataIntegrityError # leaving it there for now | ||||
|
||||
| # from infra.errors import DataIntegrityError # leaving it there for now |
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.
restore_quantities_or_raiseaccepts anautocommitparameter but ignores it and always callsctx.pi_repo.save(..., autocommit=False). This is misleading for callers and could cause subtle transaction bugs if the function is reused elsewhere. Either remove the parameter or thread it through consistently (and document the expected transaction boundary).