"""Dashboard API endpoints: KPIs and activity feed.""" from __future__ import annotations from fastapi import APIRouter, Depends from sqlalchemy.ext.asyncio import AsyncSession from app.core.db import get_db from app.core.deps import get_current_user from app.models.user import User from app.schemas.dashboard import ActivityFeedItem, KPIOut from app.services.dashboard_service import ( get_activity_feed, get_kpis, ) router = APIRouter(prefix="/dashboard", tags=["dashboard"]) @router.get( "/kpis", response_model=KPIOut, summary="Get dashboard KPIs (open deals, pipeline value, won this month, conversion rate)", ) async def get_kpis_endpoint( current_user: User = Depends(get_current_user), db: AsyncSession = Depends(get_db), ) -> KPIOut: kpis = await get_kpis(db, org_id=current_user.org_id) return KPIOut(**kpis) # type: ignore[arg-type] @router.get( "/feed", response_model=list[ActivityFeedItem], summary="Get the latest 20 activities (sorted by created_at desc)", ) async def get_activity_feed_endpoint( limit: int = 20, current_user: User = Depends(get_current_user), db: AsyncSession = Depends(get_db), ) -> list[ActivityFeedItem]: activities = await get_activity_feed(db, org_id=current_user.org_id, limit=limit) return [ActivityFeedItem.model_validate(a) for a in activities] __all__ = ["router"]