"""Fundamental data read access. ``fundamental_data`` is the compat cache scoring reads. It is written solely by ``fundamental_data_refresh_service`` from SEC snapshots, Dolt earnings events and stored closes; nothing fetches it per ticker. """ from __future__ import annotations import logging from sqlalchemy import select from sqlalchemy.ext.asyncio import AsyncSession from app.exceptions import NotFoundError from app.models.fundamental import FundamentalData from app.models.ticker import Ticker logger = logging.getLogger(__name__) async def _get_ticker(db: AsyncSession, symbol: str) -> Ticker: """Look up a ticker by symbol.""" normalised = symbol.strip().upper() result = await db.execute(select(Ticker).where(Ticker.symbol == normalised)) ticker = result.scalar_one_or_none() if ticker is None: raise NotFoundError(f"Ticker not found: {normalised}") return ticker async def get_fundamental( db: AsyncSession, symbol: str, ) -> FundamentalData | None: """Get the latest fundamental data for a ticker.""" ticker = await _get_ticker(db, symbol) result = await db.execute( select(FundamentalData).where(FundamentalData.ticker_id == ticker.id) ) return result.scalar_one_or_none()