47 lines
1.8 KiB
Python
47 lines
1.8 KiB
Python
# api/routers/invoices.py
|
|
from fastapi import APIRouter, Depends, UploadFile, File, HTTPException, status, Form
|
|
from typing import Dict, Optional
|
|
|
|
from api.dependencies import get_current_active_user
|
|
# Importamos la función específica para que el código sea más claro
|
|
from services.invoice_processor_service import process_invoice_from_bytes
|
|
from db.models import User
|
|
|
|
router = APIRouter()
|
|
|
|
ALLOWED_CONTENT_TYPES = ["application/pdf", "image/jpeg", "image/png", "image/tiff"]
|
|
|
|
@router.post("/upload", response_model=Dict[str, str])
|
|
async def upload_invoice(
|
|
current_user: User = Depends(get_current_active_user),
|
|
file: UploadFile = File(...),
|
|
# --- PUNTO CRÍTICO ---
|
|
# El nombre del parámetro aquí, `default_confidence_override`, DEBE coincidir
|
|
# con el nombre usado en el formData.append() del JavaScript.
|
|
default_confidence_override: Optional[float] = Form(None, ge=0.0, le=1.0)
|
|
):
|
|
"""
|
|
Endpoint para subir una factura. Acepta un umbral para sobrescribir
|
|
la confianza por defecto.
|
|
"""
|
|
if file.content_type not in ALLOWED_CONTENT_TYPES:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail=f"Tipo de archivo no soportado. Permitidos: {', '.join(ALLOWED_CONTENT_TYPES)}"
|
|
)
|
|
|
|
try:
|
|
file_bytes = await file.read()
|
|
|
|
# Pasamos el parámetro con el nombre correcto al servicio
|
|
extracted_data = process_invoice_from_bytes(
|
|
file_bytes=file_bytes,
|
|
mime_type=file.content_type,
|
|
default_confidence_override=default_confidence_override
|
|
)
|
|
return extracted_data
|
|
except Exception as e:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
detail=f"Error al procesar la factura: {e}"
|
|
) |