Merged in feature/textExtractionsPart3 (pull request #195)
enrich doc responses * enrich doc responses
This commit is contained in:
@@ -5,12 +5,14 @@ import (
|
||||
"log/slog"
|
||||
"net/http"
|
||||
|
||||
"queryorchestration/internal/database/repository"
|
||||
"queryorchestration/internal/document/batch/storage"
|
||||
documentupload "queryorchestration/internal/document/upload"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/oapi-codegen/nullable"
|
||||
openapi_types "github.com/oapi-codegen/runtime/types"
|
||||
)
|
||||
|
||||
func (s *Controllers) UploadDocument(ctx echo.Context, clientId ClientID) error {
|
||||
@@ -64,18 +66,62 @@ func (s *Controllers) ListDocumentsByClientId(ctx echo.Context, clientId ClientI
|
||||
return ctx.JSON(http.StatusOK, docs)
|
||||
}
|
||||
|
||||
func (s *Controllers) GetDocument(ctx echo.Context, id DocumentID) error {
|
||||
document, err := s.svc.Document.GetExternal(ctx.Request().Context(), id)
|
||||
// GetDocument retrieves document details by ID with enriched metadata.
|
||||
// When textRecord=true, includes the full field extraction response.
|
||||
func (s *Controllers) GetDocument(ctx echo.Context, id DocumentID, params GetDocumentParams) error {
|
||||
includeTextRecord := params.TextRecord != nil && *params.TextRecord
|
||||
|
||||
document, err := s.svc.Document.GetEnriched(ctx.Request().Context(), uuid.UUID(id), includeTextRecord)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Unable to list documents: %s", err))
|
||||
slog.Error("unable to get document", "error", err, "document_id", id)
|
||||
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Unable to get document: %s", err))
|
||||
}
|
||||
|
||||
return ctx.JSON(http.StatusOK, Document{
|
||||
Id: document.Id,
|
||||
ClientId: document.ClientID,
|
||||
Hash: document.Hash,
|
||||
Fields: document.Fields,
|
||||
})
|
||||
// Convert labels to API format
|
||||
labels := make([]LabelRecord, len(document.Labels))
|
||||
for i, lbl := range document.Labels {
|
||||
labels[i] = LabelRecord{
|
||||
Id: openapi_types.UUID(lbl.ID),
|
||||
DocumentId: DocumentID(lbl.DocumentID),
|
||||
Label: lbl.Label,
|
||||
AppliedBy: openapi_types.Email(lbl.AppliedBy),
|
||||
AppliedAt: lbl.AppliedAt,
|
||||
}
|
||||
}
|
||||
|
||||
response := DocumentEnriched{
|
||||
Id: DocumentID(document.ID),
|
||||
ClientId: ClientID(document.ClientID),
|
||||
Hash: Hash(document.Hash),
|
||||
Fields: document.Fields,
|
||||
HasTextRecord: document.HasTextRecord,
|
||||
Labels: labels,
|
||||
}
|
||||
|
||||
if document.FolderID != nil {
|
||||
response.FolderId = (*openapi_types.UUID)(document.FolderID)
|
||||
}
|
||||
if document.Filename != nil {
|
||||
response.Filename = document.Filename
|
||||
}
|
||||
if document.OriginalPath != nil {
|
||||
response.OriginalPath = document.OriginalPath
|
||||
}
|
||||
|
||||
// Include text record if requested and available
|
||||
if document.TextRecord != nil {
|
||||
// The TextRecord contains repository types that we need to convert
|
||||
current, ok := document.TextRecord.SingleFields.(*repository.Currentfieldextraction)
|
||||
if ok && current != nil {
|
||||
arrayFields, ok := document.TextRecord.ArrayFields.([]*repository.Documentfieldextractionarrayfield)
|
||||
if ok {
|
||||
textRecord := buildFieldExtractionResponse(current, arrayFields)
|
||||
response.TextRecord = textRecord
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ctx.JSON(http.StatusOK, response)
|
||||
}
|
||||
|
||||
// ListDocumentBatches lists batch uploads for a client
|
||||
|
||||
Reference in New Issue
Block a user