Fixed - Template Mapping Issue - Tested Invoice JSON Extraction - Failed Other Type Document Scenario
This commit is contained in:
@@ -235,6 +235,45 @@ def get_document_template_matches(
|
||||
return results
|
||||
|
||||
|
||||
@router.post(
|
||||
"/{document_id}/extraction",
|
||||
response_model=dict,
|
||||
summary="Extract Document Data",
|
||||
description="Match a document against templates and extract key-value & table data.",
|
||||
)
|
||||
def extract_document_data(
|
||||
document_id: uuid.UUID,
|
||||
current_user: CurrentUser = None,
|
||||
db: Session = Depends(get_db),
|
||||
) -> dict:
|
||||
"""Extract document data using matched template mappings."""
|
||||
doc_repo = DocumentRepository(db)
|
||||
document = doc_repo.get_by_id(document_id)
|
||||
if not document:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail=f"Document '{document_id}' not found",
|
||||
)
|
||||
|
||||
if document.status != "completed":
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail=f"Document must be in 'completed' status. Current status: '{document.status}'",
|
||||
)
|
||||
|
||||
from app.services.extraction_service import ExtractionService
|
||||
extraction_service = ExtractionService(db)
|
||||
try:
|
||||
result = extraction_service.extract_document_data(document_id)
|
||||
return result
|
||||
except Exception as e:
|
||||
logger.exception("extraction_endpoint_failed", document_id=str(document_id), error=str(e))
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"Extraction failed: {str(e)}",
|
||||
)
|
||||
|
||||
|
||||
@router.delete(
|
||||
"/{document_id}",
|
||||
response_model=SuccessResponse,
|
||||
|
||||
Reference in New Issue
Block a user