refactor: invalidate roster cache after submission instead of force-refreshing

Simplify review found that force_refresh=True on every validate_transaction()
call caused redundant API fetches on every embed render and button press.

Instead, invalidate the roster cache after successful submit_transaction() so
the next operation fetches fresh data. This preserves the cache for normal
interaction flows while still preventing stale data after submissions.

Also adds type annotation for roster_svc DI parameter.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Cal Corum 2026-03-02 15:04:16 -06:00 committed by cal
parent c70669d474
commit ffa0366441

View File

@ -14,7 +14,7 @@ from models.transaction import Transaction
from models.team import Team
from models.player import Player
from models.roster import TeamRoster
from services.roster_service import roster_service
from services.roster_service import RosterService, roster_service
from services.transaction_service import transaction_service
from services.league_service import league_service
from models.team import RosterType
@ -179,7 +179,7 @@ class TransactionBuilder:
team: Team,
user_id: int,
season: int = get_config().sba_season,
roster_svc=None,
roster_svc: Optional[RosterService] = None,
):
"""
Initialize transaction builder.
@ -367,9 +367,7 @@ class TransactionBuilder:
Returns:
RosterValidationResult with validation details
"""
# Always refresh roster data before validation to prevent stale cache
# from causing incorrect roster counts (e.g. after a previous /ilmove submission)
await self.load_roster_data(force_refresh=True)
await self.load_roster_data()
# Load existing transactions if next_week is provided
if next_week is not None:
@ -694,8 +692,17 @@ class TransactionBuilder:
logger.info(
f"Created {len(transactions)} transactions for submission with move_id {move_id}"
)
# Invalidate roster cache so subsequent operations fetch fresh data
self.invalidate_roster_cache()
return transactions
def invalidate_roster_cache(self) -> None:
"""Invalidate cached roster data so next load fetches fresh data."""
self._roster_loaded = False
self._current_roster = None
def clear_moves(self) -> None:
"""Clear all moves from the transaction builder."""
self.moves.clear()