ebf47c6013
track file sizes for all documents in system * feature complete needs dev testing
154 lines
4.0 KiB
Go
154 lines
4.0 KiB
Go
package document
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
|
|
"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 with computed fields for external API responses.
|
|
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
|
|
}
|
|
|
|
var fields map[string]interface{}
|
|
err = json.Unmarshal(doc.Fields, &fields)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &DocumentExternal{
|
|
Id: doc.ID,
|
|
ClientID: doc.Clientid,
|
|
Hash: doc.Hash,
|
|
Fields: fields,
|
|
}, 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.
|
|
func (s *Service) GetEnriched(ctx context.Context, id uuid.UUID, includeTextRecord bool) (*DocumentEnriched, error) {
|
|
queries := s.cfg.GetDBQueries()
|
|
|
|
// 1. Get basic document info + computed fields
|
|
docExternal, err := queries.GetDocumentExternal(ctx, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var fields map[string]interface{}
|
|
err = json.Unmarshal(docExternal.Fields, &fields)
|
|
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,
|
|
Fields: fields,
|
|
HasTextRecord: hasTextRecord,
|
|
FolderID: docEnriched.Folderid,
|
|
Filename: docEnriched.Filename,
|
|
OriginalPath: docEnriched.Originalpath,
|
|
Labels: labels,
|
|
FileSizeBytes: docEnriched.FileSizeBytes,
|
|
}
|
|
|
|
// 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
|
|
}
|