Refactor domain_utils.py overlap helpers, add ONE_DAY constant, remove inflated comments, and standardize _add_row() calls
This commit is contained in:
+17
-63
@@ -6,6 +6,9 @@ from sqlalchemy.orm import Session
|
||||
from src.models.category import Category, CategoryBudget, CategoryBudgetDb, CategoryDb
|
||||
|
||||
|
||||
ONE_DAY = timedelta(days=1)
|
||||
|
||||
|
||||
def find_category_db(session: Session, category: Category | None) -> CategoryDb | None:
|
||||
"""Find a CategoryDb in the database given a Pydantic Category object.
|
||||
|
||||
@@ -57,6 +60,7 @@ def set_category_budget(session: Session, category_budget: CategoryBudget):
|
||||
|
||||
new_start = category_budget.valid_from
|
||||
new_end = category_budget.valid_to
|
||||
new_amount = category_budget.amount_eur
|
||||
|
||||
# Helper to add a budget row
|
||||
def _add_row(amount_eur: Decimal, valid_from: date | None, valid_to: date | None):
|
||||
@@ -70,13 +74,7 @@ def set_category_budget(session: Session, category_budget: CategoryBudget):
|
||||
# Helper to check if two date ranges overlap
|
||||
def _ranges_overlap(start1: date | None, end1: date | None,
|
||||
start2: date | None, end2: date | None) -> bool:
|
||||
"""Check if two date ranges overlap.
|
||||
|
||||
Two bounded ranges overlap if start2 < end1 and start1 < end2.
|
||||
An unbounded range (None, None) only overlaps with another fully unbounded range.
|
||||
Partially unbounded ranges (e.g., valid_from=None but valid_to=date)
|
||||
only overlap if the bounded ranges themselves would overlap.
|
||||
"""
|
||||
"""Check if two date ranges overlap."""
|
||||
# Both completely unbounded - always overlap
|
||||
if start1 is None and end1 is None and start2 is None and end2 is None:
|
||||
return True
|
||||
@@ -94,47 +92,19 @@ def set_category_budget(session: Session, category_budget: CategoryBudget):
|
||||
start2: date | None, end2: date | None) -> tuple[date | None, date | None]:
|
||||
"""Return the overlapping sub-range of two ranges."""
|
||||
|
||||
# Handle completely unbounded cases
|
||||
# Handle completely unbounded cases first
|
||||
if start1 is None and end1 is None:
|
||||
# First range is completely unbounded - overlap is second range
|
||||
return (start2, end2) if (start2 is not None or end2 is not None) else (None, None)
|
||||
|
||||
return (start2, end2) if any(x is not None for x in (start2, end2)) else (None, None)
|
||||
if start2 is None and end2 is None:
|
||||
# Second range is completely unbounded - overlap is first range
|
||||
return (start1, end1) if (start1 is not None or end1 is not None) else (None, None)
|
||||
return (start1, end1) if any(x is not None for x in (start1, end1)) else (None, None)
|
||||
|
||||
# Initialize with defaults
|
||||
o_start = None
|
||||
o_end = None
|
||||
|
||||
# Handle one-sided unbounded cases
|
||||
if start1 is not None and end1 is not None:
|
||||
# Range 1 is bounded, range 2 may be unbounded
|
||||
if start2 is None: # New starts at beginning, so overlap starts at existing_start
|
||||
o_start = start1
|
||||
else:
|
||||
o_start = max(start1, start2)
|
||||
|
||||
if end2 is None: # New ends at end of time, so overlap ends at existing_end
|
||||
o_end = end1
|
||||
elif o_start is not None:
|
||||
o_end = min(end1, end2)
|
||||
|
||||
if start2 is not None and end2 is not None:
|
||||
# Range 2 is bounded, range 1 may be unbounded
|
||||
if start1 is None: # Existing starts at beginning
|
||||
o_start = start2
|
||||
elif o_start is None:
|
||||
o_start = max(start1, start2)
|
||||
|
||||
if end1 is None: # Existing ends at end of time
|
||||
o_end = end2
|
||||
elif o_start is not None:
|
||||
o_end = min(end1, end2)
|
||||
# Both ranges are bounded or partially unbounded
|
||||
o_start = max((s for s in (start1, start2) if s is not None), default=None)
|
||||
o_end = min((e for e in (end1, end2) if e is not None), default=None)
|
||||
|
||||
# Check for actual overlap
|
||||
if o_start is None or o_start > o_end:
|
||||
return (None, None) # No overlap
|
||||
return (None, None)
|
||||
|
||||
return (o_start, o_end)
|
||||
|
||||
@@ -163,7 +133,7 @@ def set_category_budget(session: Session, category_budget: CategoryBudget):
|
||||
|
||||
if overlap_start is None or overlap_end is None:
|
||||
# Full unbounded overlap - replace entirely with new budget
|
||||
row = _add_row(category_budget.amount_eur, category_budget.valid_from, category_budget.valid_to)
|
||||
row = _add_row(new_amount, new_start, new_end)
|
||||
session.add(row)
|
||||
return [row]
|
||||
|
||||
@@ -172,28 +142,16 @@ def set_category_budget(session: Session, category_budget: CategoryBudget):
|
||||
|
||||
# Part 1: Before the overlap (if any)
|
||||
if budget.valid_from is not None and budget.valid_from < overlap_start:
|
||||
row = _add_row(
|
||||
amount_eur=budget.amount_eur,
|
||||
valid_from=budget.valid_from,
|
||||
valid_to=overlap_start - timedelta(days=1),
|
||||
)
|
||||
row = _add_row(budget.amount_eur, budget.valid_from, overlap_start - ONE_DAY)
|
||||
parts.append(row)
|
||||
|
||||
# Part 2: After the overlap (if any)
|
||||
if budget.valid_to is not None and overlap_end < budget.valid_to:
|
||||
row = _add_row(
|
||||
amount_eur=budget.amount_eur,
|
||||
valid_from=overlap_end + timedelta(days=1),
|
||||
valid_to=budget.valid_to,
|
||||
)
|
||||
row = _add_row(budget.amount_eur, overlap_end + ONE_DAY, budget.valid_to)
|
||||
parts.append(row)
|
||||
|
||||
# Add the new budget
|
||||
row = _add_row(
|
||||
amount_eur=category_budget.amount_eur,
|
||||
valid_from=category_budget.valid_from,
|
||||
valid_to=category_budget.valid_to,
|
||||
)
|
||||
row = _add_row(new_amount, new_start, new_end)
|
||||
parts.append(row)
|
||||
|
||||
# Add all parts to session
|
||||
@@ -220,9 +178,5 @@ def set_category_budget(session: Session, category_budget: CategoryBudget):
|
||||
|
||||
# Always add the new budget unless it was already added during overlap handling
|
||||
if not new_budget_added:
|
||||
row = _add_row(
|
||||
amount_eur=category_budget.amount_eur,
|
||||
valid_from=category_budget.valid_from,
|
||||
valid_to=category_budget.valid_to,
|
||||
)
|
||||
row = _add_row(new_amount, new_start, new_end)
|
||||
session.add(row)
|
||||
|
||||
Reference in New Issue
Block a user