17fc813823
M1, M2 and M3 complete * M1, M2 and M3 complete * review changes * docs * docs
143 lines
4.0 KiB
Go
143 lines
4.0 KiB
Go
package document
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
// GetSummary returns minimal document identification info.
|
|
func (s *Service) GetSummary(ctx context.Context, id uuid.UUID) (*DocumentSummary, error) {
|
|
doc, err := s.cfg.GetDBQueries().GetDocumentSummary(ctx, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &DocumentSummary{
|
|
ID: doc.ID,
|
|
ClientID: doc.Clientid,
|
|
Hash: doc.Hash,
|
|
}, nil
|
|
}
|
|
|
|
// GetExternal returns document info for external API responses.
|
|
// Note: Query functionality (Fields) has been removed. See remove_query_plan.md for details.
|
|
func (s *Service) GetExternal(ctx context.Context, id uuid.UUID) (*DocumentExternal, error) {
|
|
doc, err := s.cfg.GetDBQueries().GetDocumentExternal(ctx, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &DocumentExternal{
|
|
Id: doc.ID,
|
|
ClientID: doc.Clientid,
|
|
Hash: doc.Hash,
|
|
}, nil
|
|
}
|
|
|
|
// GetEnriched returns full document details including metadata, labels, and optionally the text record.
|
|
// Parameters:
|
|
// - ctx: context for the operation
|
|
// - id: document UUID
|
|
// - includeTextRecord: if true, fetches and includes the full field extraction response
|
|
//
|
|
// Returns DocumentEnriched with all metadata, or error if document not found or other failure.
|
|
// Note: Query functionality (Fields) has been removed. See remove_query_plan.md for details.
|
|
func (s *Service) GetEnriched(ctx context.Context, id uuid.UUID, includeTextRecord bool) (*DocumentEnriched, error) {
|
|
queries := s.cfg.GetDBQueries()
|
|
|
|
// 1. Get basic document info
|
|
docExternal, err := queries.GetDocumentExternal(ctx, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// 2. Get extended document metadata (folder, filename, originalPath)
|
|
docEnriched, err := queries.GetDocumentEnriched(ctx, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// 3. Check if text record exists
|
|
hasTextRecord, err := queries.HasFieldExtraction(ctx, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// 4. Get labels for document
|
|
dbLabels, err := queries.GetDocumentLabels(ctx, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
labels := make([]LabelRecord, len(dbLabels))
|
|
for i, lbl := range dbLabels {
|
|
labels[i] = LabelRecord{
|
|
ID: lbl.ID,
|
|
DocumentID: lbl.Documentid,
|
|
Label: lbl.Label,
|
|
AppliedBy: lbl.Appliedby,
|
|
AppliedAt: lbl.Appliedat.Time,
|
|
}
|
|
}
|
|
|
|
result := &DocumentEnriched{
|
|
ID: docExternal.ID,
|
|
ClientID: docExternal.Clientid,
|
|
Hash: docExternal.Hash,
|
|
HasTextRecord: hasTextRecord,
|
|
FolderID: docEnriched.Folderid,
|
|
Filename: docEnriched.Filename,
|
|
OriginalPath: docEnriched.Originalpath,
|
|
Labels: labels,
|
|
FileSizeBytes: docEnriched.FileSizeBytes,
|
|
CustomSchemaID: docEnriched.CustomSchemaID,
|
|
HasCustomMetadata: docEnriched.HasCustomMetadata,
|
|
}
|
|
|
|
// 5. Optionally fetch full text record if requested and exists
|
|
if includeTextRecord && hasTextRecord {
|
|
textRecord, err := s.getTextRecord(ctx, id)
|
|
if err != nil {
|
|
// Log but don't fail - text record is optional enhancement
|
|
// Could add logging here if needed
|
|
return result, nil
|
|
}
|
|
result.TextRecord = textRecord
|
|
}
|
|
|
|
return result, nil
|
|
}
|
|
|
|
// getTextRecord fetches the current field extraction for a document.
|
|
// Returns nil if no extraction exists.
|
|
func (s *Service) getTextRecord(ctx context.Context, documentID uuid.UUID) (*TextRecord, error) {
|
|
queries := s.cfg.GetDBQueries()
|
|
|
|
current, err := queries.GetCurrentFieldExtraction(ctx, documentID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if current == nil {
|
|
return nil, nil
|
|
}
|
|
|
|
// Get array fields
|
|
arrayFields, err := queries.GetFieldExtractionArrayFields(ctx, current.ID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &TextRecord{
|
|
ID: current.ID,
|
|
DocumentID: current.Documentid,
|
|
//nolint:gosec // Version is a small number, int32 is sufficient
|
|
Version: int32(current.Version),
|
|
SingleFields: current, // Pass the whole struct - handler will convert
|
|
ArrayFields: arrayFields, // Pass array fields - handler will convert
|
|
CreatedAt: current.Createdat.Time,
|
|
CreatedBy: current.Createdby,
|
|
}, nil
|
|
}
|