55 lines
2.0 KiB
Python
55 lines
2.0 KiB
Python
from fastapi import APIRouter, Depends
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
from sqlalchemy import select, func
|
|
from app.core.database import get_db
|
|
from app.core.security import get_current_user_id
|
|
from app.models.familia import Familia
|
|
from app.models.checklist import ChecklistItem
|
|
from app.models.beneficio import Beneficio
|
|
from app.models.documento import Documento
|
|
from app.schemas.schemas import DashboardOut
|
|
|
|
router = APIRouter(prefix="/dashboard", tags=["dashboard"])
|
|
|
|
@router.get("/", response_model=DashboardOut)
|
|
async def dashboard(user_id: int = Depends(get_current_user_id), db: AsyncSession = Depends(get_db)):
|
|
fams = await db.execute(select(Familia).where(Familia.usuario_id == user_id))
|
|
familias = fams.scalars().all()
|
|
fam_ids = [f.id for f in familias]
|
|
|
|
pendentes = 0
|
|
bens_count = 0
|
|
docs_count = 0
|
|
fam_list = []
|
|
|
|
for fam in familias:
|
|
ch = await db.execute(select(ChecklistItem).where(ChecklistItem.familia_id == fam.id))
|
|
items = ch.scalars().all()
|
|
total = len(items)
|
|
done = len([i for i in items if i.status == "concluido"])
|
|
pendentes += len([i for i in items if i.status == "pendente"])
|
|
|
|
bn = await db.execute(select(func.count()).select_from(Beneficio).where(Beneficio.familia_id == fam.id))
|
|
bc = bn.scalar() or 0
|
|
bens_count += bc
|
|
|
|
dc = await db.execute(select(func.count()).select_from(Documento).where(Documento.familia_id == fam.id))
|
|
docs_count += dc.scalar() or 0
|
|
|
|
fam_list.append({
|
|
"id": fam.id,
|
|
"nome": fam.nome,
|
|
"total_items": total,
|
|
"concluidos": done,
|
|
"progresso": round(done / total * 100) if total else 0,
|
|
"beneficios": bc,
|
|
})
|
|
|
|
return DashboardOut(
|
|
familias_ativas=len(familias),
|
|
itens_pendentes=pendentes,
|
|
beneficios_encontrados=bens_count,
|
|
documentos_gerados=docs_count,
|
|
familias=fam_list,
|
|
)
|