invoice-processing-google-d.../api/routers/invoices.py
2025-08-26 12:26:03 +02:00

36 lines
1.2 KiB
Python

# api/routers/invoices.py
from fastapi import APIRouter, Depends, UploadFile, File, HTTPException
from typing import Dict
from api.dependencies import get_current_active_user
from services import invoice_processor_service
from core.config import settings
from db.models import User
router = APIRouter()
@router.post("/upload", response_model=Dict[str, str])
async def upload_invoice(
file: UploadFile = File(...),
current_user: User = Depends(get_current_active_user)
):
"""
Endpoint para subir una factura, procesarla y devolver los datos extraídos.
Requiere autenticación.
"""
if not file.content_type in ["application/pdf", "image/jpeg", "image/png"]:
raise HTTPException(status_code=400, detail="Tipo de archivo no soportado.")
try:
file_bytes = await file.read()
extracted_data = invoice_processor_service.process_invoice_from_bytes(
project_id=settings.GCP_PROJECT_ID,
location=settings.GCP_LOCATION,
processor_id=settings.DOCAI_PROCESSOR_ID,
file_bytes=file_bytes,
mime_type=file.content_type
)
return extracted_data
except Exception as e:
raise HTTPException(status_code=500, detail=f"Error al procesar la factura: {e}")