CARONTE v1.0 - Plataforma de Gestão Social
This commit is contained in:
62
backend/app/api/v1/documentos.py
Normal file
62
backend/app/api/v1/documentos.py
Normal file
@@ -0,0 +1,62 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from fastapi.responses import FileResponse
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy import select
|
||||
from app.core.database import get_db
|
||||
from app.core.security import get_current_user_id
|
||||
from app.models.documento import Documento
|
||||
from app.models.falecido import Falecido
|
||||
from app.models.familia import Familia, MembroFamilia
|
||||
from app.schemas.schemas import DocumentoOut, DocumentoGerarRequest
|
||||
from app.services.document_generator import GENERATORS
|
||||
import os
|
||||
|
||||
router = APIRouter(prefix="/familias/{familia_id}/documentos", tags=["documentos"])
|
||||
|
||||
@router.get("/", response_model=list[DocumentoOut])
|
||||
async def listar(familia_id: int, db: AsyncSession = Depends(get_db)):
|
||||
result = await db.execute(select(Documento).where(Documento.familia_id == familia_id))
|
||||
return result.scalars().all()
|
||||
|
||||
@router.post("/gerar", response_model=DocumentoOut)
|
||||
async def gerar(familia_id: int, data: DocumentoGerarRequest, db: AsyncSession = Depends(get_db)):
|
||||
gen = GENERATORS.get(data.tipo)
|
||||
if not gen:
|
||||
raise HTTPException(400, f"Tipo de documento inválido: {data.tipo}")
|
||||
|
||||
fam = await db.execute(select(Familia).where(Familia.id == familia_id))
|
||||
familia = fam.scalar_one_or_none()
|
||||
if not familia:
|
||||
raise HTTPException(404, "Família não encontrada")
|
||||
|
||||
fal = await db.execute(select(Falecido).where(Falecido.id == data.falecido_id))
|
||||
falecido = fal.scalar_one_or_none()
|
||||
if not falecido:
|
||||
raise HTTPException(404, "Falecido não encontrado")
|
||||
|
||||
membros = await db.execute(select(MembroFamilia).where(MembroFamilia.familia_id == familia_id))
|
||||
membro = membros.scalars().first()
|
||||
membro_nome = membro.nome if membro else "Representante da Família"
|
||||
|
||||
path = gen(familia.nome, falecido.nome, membro_nome, falecido.cpf or "000.000.000-00")
|
||||
|
||||
doc = Documento(
|
||||
familia_id=familia_id,
|
||||
falecido_id=data.falecido_id,
|
||||
tipo=data.tipo,
|
||||
nome=f"{data.tipo.replace('_',' ').title()} - {falecido.nome}",
|
||||
arquivo_path=path,
|
||||
status="gerado",
|
||||
)
|
||||
db.add(doc)
|
||||
await db.commit()
|
||||
await db.refresh(doc)
|
||||
return doc
|
||||
|
||||
@router.get("/{doc_id}/download")
|
||||
async def download(familia_id: int, doc_id: int, db: AsyncSession = Depends(get_db)):
|
||||
result = await db.execute(select(Documento).where(Documento.id == doc_id, Documento.familia_id == familia_id))
|
||||
doc = result.scalar_one_or_none()
|
||||
if not doc or not doc.arquivo_path or not os.path.exists(doc.arquivo_path):
|
||||
raise HTTPException(404, "Documento não encontrado")
|
||||
return FileResponse(doc.arquivo_path, filename=os.path.basename(doc.arquivo_path), media_type="application/pdf")
|
||||
Reference in New Issue
Block a user