Files
query-orchestration/internal/folder/service.go
T
Jay Brown a94637ab13 Merged in feature/fix_test_timeouts (pull request #194)
text extractions tests

* increase timeout

for slow ci/cd systems

* add tests
2025-12-04 22:45:15 +00:00

238 lines
6.9 KiB
Go

package folder
import (
"context"
"fmt"
"queryorchestration/internal/database/repository"
"queryorchestration/internal/serviceconfig"
"github.com/google/uuid"
)
// Service provides folder management operations
type Service struct {
cfg serviceconfig.ConfigProvider
}
// New creates a new folder service instance
func New(cfg serviceconfig.ConfigProvider) *Service {
return &Service{
cfg: cfg,
}
}
// CreateFolder creates a new folder with validation.
// Validates folder path constraints and that parent folder exists if parentId is provided.
//
// Parameters:
// - ctx: request context
// - path: folder path (must pass all path constraints)
// - parentID: optional parent folder UUID
// - clientID: client identifier
// - createdBy: email of user creating the folder
//
// Returns:
// - *repository.Folder: the created folder
// - error: validation error (PathValidationError) or database error
func (s *Service) CreateFolder(ctx context.Context, path string, parentID *uuid.UUID, clientID string, createdBy string) (*repository.Folder, error) {
if clientID == "" {
return nil, fmt.Errorf("clientID cannot be empty")
}
if createdBy == "" {
return nil, fmt.Errorf("createdBy cannot be empty")
}
// Check if root folder already exists for this client
rootExists := false
if path == "/" {
_, err := s.cfg.GetDBQueries().GetFolderByPath(ctx, &repository.GetFolderByPathParams{
Clientid: clientID,
Path: "/",
})
if err == nil {
rootExists = true
}
}
// Validate folder path constraints
if err := ValidateFolderPathForCreate(path, rootExists); err != nil {
return nil, err
}
// Validate parent folder exists if parentID is provided
if parentID != nil {
parent, err := s.cfg.GetDBQueries().GetFolderByID(ctx, *parentID)
if err != nil {
return nil, fmt.Errorf("parent folder not found: %w", err)
}
// Ensure parent belongs to same client
if parent.Clientid != clientID {
return nil, fmt.Errorf("parent folder belongs to different client")
}
}
folder, err := s.cfg.GetDBQueries().CreateFolder(ctx, &repository.CreateFolderParams{
Path: path,
Parentid: parentID,
Clientid: clientID,
Createdby: createdBy,
})
if err != nil {
return nil, fmt.Errorf("failed to create folder: %w", err)
}
return folder, nil
}
// GetFolder retrieves a folder by ID
func (s *Service) GetFolder(ctx context.Context, folderID uuid.UUID) (*repository.Folder, error) {
folder, err := s.cfg.GetDBQueries().GetFolderByID(ctx, folderID)
if err != nil {
return nil, fmt.Errorf("folder not found: %w", err)
}
return folder, nil
}
// GetFolderByPath retrieves a folder by client and path
func (s *Service) GetFolderByPath(ctx context.Context, clientID string, path string) (*repository.Folder, error) {
folder, err := s.cfg.GetDBQueries().GetFolderByPath(ctx, &repository.GetFolderByPathParams{
Clientid: clientID,
Path: path,
})
if err != nil {
return nil, fmt.Errorf("folder not found: %w", err)
}
return folder, nil
}
// RenameFolder renames a folder.
// Validates folder path constraints and prevents renaming the root folder.
//
// Parameters:
// - ctx: request context
// - folderID: UUID of the folder to rename
// - newPath: new folder path (must pass all path constraints)
//
// Returns:
// - error: validation error (PathValidationError) or database error
func (s *Service) RenameFolder(ctx context.Context, folderID uuid.UUID, newPath string) error {
// Verify folder exists and get current path
folder, err := s.cfg.GetDBQueries().GetFolderByID(ctx, folderID)
if err != nil {
return fmt.Errorf("folder not found: %w", err)
}
// Check if this is the root folder
isRootFolder := folder.Path == "/"
// Validate folder path constraints for rename
if err := ValidateFolderPathForRename(newPath, isRootFolder); err != nil {
return err
}
err = s.cfg.GetDBQueries().RenameFolder(ctx, &repository.RenameFolderParams{
ID: folderID,
Path: newPath,
})
if err != nil {
return fmt.Errorf("failed to rename folder: %w", err)
}
return nil
}
// GetFolderTree retrieves a folder and all its descendants (recursive)
func (s *Service) GetFolderTree(ctx context.Context, folderID uuid.UUID) ([]*repository.GetFolderTreeRow, error) {
folders, err := s.cfg.GetDBQueries().GetFolderTree(ctx, &folderID)
if err != nil {
return nil, fmt.Errorf("failed to get folder tree: %w", err)
}
return folders, nil
}
// GetFoldersByClient retrieves all folders for a client
func (s *Service) GetFoldersByClient(ctx context.Context, clientID string) ([]*repository.Folder, error) {
folders, err := s.cfg.GetDBQueries().GetFoldersByClientID(ctx, clientID)
if err != nil {
return nil, fmt.Errorf("failed to get folders: %w", err)
}
return folders, nil
}
// GetRootFolders retrieves all root folders (parentId IS NULL) for a client
func (s *Service) GetRootFolders(ctx context.Context, clientID string) ([]*repository.Folder, error) {
folders, err := s.cfg.GetDBQueries().GetRootFolders(ctx, clientID)
if err != nil {
return nil, fmt.Errorf("failed to get root folders: %w", err)
}
return folders, nil
}
// GetSubfolders retrieves direct child folders of a parent folder
func (s *Service) GetSubfolders(ctx context.Context, parentID uuid.UUID) ([]*repository.Folder, error) {
folders, err := s.cfg.GetDBQueries().GetFoldersByParentID(ctx, &parentID)
if err != nil {
return nil, fmt.Errorf("failed to get subfolders: %w", err)
}
return folders, nil
}
// GetDocumentsByFolder retrieves all documents in a specific folder
func (s *Service) GetDocumentsByFolder(ctx context.Context, folderID uuid.UUID) ([]*repository.Document, error) {
documents, err := s.cfg.GetDBQueries().GetDocumentsByFolder(ctx, &folderID)
if err != nil {
return nil, fmt.Errorf("failed to get documents by folder: %w", err)
}
return documents, nil
}
// FolderMetrics contains aggregated metrics for a folder
type FolderMetrics struct {
TotalDocuments int32
ByLabel map[string]int32
}
// GetFolderMetrics retrieves processing metrics for a folder including document counts by label
func (s *Service) GetFolderMetrics(ctx context.Context, folderID uuid.UUID) (*FolderMetrics, error) {
// Get total document count
total, err := s.cfg.GetDBQueries().CountDocumentsByFolder(ctx, &folderID)
if err != nil {
return nil, fmt.Errorf("failed to count documents: %w", err)
}
// Get label counts
labelCounts, err := s.cfg.GetDBQueries().GetFolderLabelCounts(ctx, &folderID)
if err != nil {
return nil, fmt.Errorf("failed to get label counts: %w", err)
}
byLabel := make(map[string]int32)
for _, lc := range labelCounts {
byLabel[lc.Label] = safeInt64ToInt32(lc.Count)
}
return &FolderMetrics{
TotalDocuments: safeInt64ToInt32(total),
ByLabel: byLabel,
}, nil
}
// safeInt64ToInt32 safely converts int64 to int32, clamping at max int32 if necessary
func safeInt64ToInt32(n int64) int32 {
const maxInt32 = 2147483647
if n > maxInt32 {
return maxInt32
}
return int32(n) //nolint:gosec // bounds check performed above
}