87 lines
3.2 KiB
Python
Executable File
87 lines
3.2 KiB
Python
Executable File
# /opt/docker/dev/service_finder/backend/app/api/v1/endpoints/documents.py
|
|
import uuid
|
|
from typing import Any, Dict
|
|
from fastapi import APIRouter, Depends, UploadFile, File, Form, BackgroundTasks, HTTPException, status
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.db.session import get_db
|
|
from app.api.deps import get_current_user
|
|
from app.services.document_service import DocumentService
|
|
from app.models.identity import User
|
|
|
|
router = APIRouter()
|
|
|
|
@router.post("/upload/{parent_type}/{parent_id}")
|
|
async def upload_document(
|
|
parent_type: str,
|
|
parent_id: str,
|
|
background_tasks: BackgroundTasks,
|
|
doc_type: str = Form(..., description="A dokumentum típusa: 'invoice', 'registration_card', 'sale_contract'"),
|
|
file: UploadFile = File(...),
|
|
db: AsyncSession = Depends(get_db),
|
|
current_user: User = Depends(get_current_user)
|
|
):
|
|
"""
|
|
MB 2.0 Dokumentum Pipeline.
|
|
1. Ellenőrzi a felhasználó havi OCR kvótáját (Admin 2.0).
|
|
2. Optimalizálja és NAS-ra menti a képet (WebP konverzió).
|
|
3. Automatikusan elindítja a Robot 1-et (OCR), ha a típus engedélyezett.
|
|
"""
|
|
|
|
# 1. Bemeneti validáció
|
|
valid_parents = ["organizations", "assets", "transfers"]
|
|
if parent_type not in valid_parents:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail=f"Érvénytelen cél-típus! Megengedett: {', '.join(valid_parents)}"
|
|
)
|
|
|
|
try:
|
|
# 2. Feldolgozás a szolgáltatás rétegben
|
|
# Itt történik a kvóta-ellenőrzés és a Robot 1 triggerelése is
|
|
doc = await DocumentService.process_upload(
|
|
db=db,
|
|
user_id=current_user.id,
|
|
file=file,
|
|
parent_type=parent_type,
|
|
parent_id=parent_id,
|
|
doc_type=doc_type,
|
|
background_tasks=background_tasks
|
|
)
|
|
|
|
# 3. Válasz összeállítása az állapot alapján
|
|
response_data = {
|
|
"document_id": doc.id,
|
|
"original_name": doc.original_name,
|
|
"status": doc.status,
|
|
"thumbnail": doc.thumbnail_path,
|
|
}
|
|
|
|
if doc.status == "processing":
|
|
response_data["message"] = "🤖 Robot 1 megkezdte a dokumentum elemzését. Értesítjük, ha kész!"
|
|
else:
|
|
response_data["message"] = "Dokumentum sikeresen archiválva a széfben."
|
|
|
|
return response_data
|
|
|
|
except HTTPException as he:
|
|
# Közvetlenül átengedjük a service-ből jövő (pl. kvóta) hibákat
|
|
raise he
|
|
except ValueError as ve:
|
|
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail=str(ve))
|
|
except Exception as e:
|
|
# Sentinel naplózás és általános hiba
|
|
raise HTTPException(
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
detail="Kritikus hiba a dokumentum feldolgozása során."
|
|
)
|
|
|
|
@router.get("/{document_id}/status")
|
|
async def get_document_status(
|
|
document_id: uuid.UUID,
|
|
db: AsyncSession = Depends(get_db),
|
|
current_user: User = Depends(get_current_user)
|
|
):
|
|
"""Lekérdezhető, hogy a robot végzett-e már a feldolgozással."""
|
|
# (Itt egy egyszerű lekérdezés a Document táblából a státuszra)
|
|
pass |