"""Audit routes for price history.""" from fastapi import APIRouter, Depends from sqlalchemy.ext.asyncio import AsyncSession from auth import require_permission from database import get_db from mappers import PriceHistoryMapper from models import Permission, User from schemas import PriceHistoryResponse from services.price import PriceService router = APIRouter(prefix="/api/audit", tags=["audit"]) # ============================================================================= # Price History Endpoints # ============================================================================= @router.get("/price-history", response_model=list[PriceHistoryResponse]) async def get_price_history( db: AsyncSession = Depends(get_db), _current_user: User = Depends(require_permission(Permission.VIEW_AUDIT)), ) -> list[PriceHistoryResponse]: """Get the 20 most recent price history records.""" service = PriceService(db) records = await service.get_recent_prices() return [PriceHistoryMapper.to_response(record) for record in records] @router.post("/price-history/fetch", response_model=PriceHistoryResponse) async def fetch_price_now( db: AsyncSession = Depends(get_db), _current_user: User = Depends(require_permission(Permission.FETCH_PRICE)), ) -> PriceHistoryResponse: """Manually trigger a price fetch from Bitfinex.""" service = PriceService(db) record = await service.fetch_and_store_price() return PriceHistoryMapper.to_response(record)