2025-02-03 17:30:50 +00:00
|
|
|
package documentinit
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2025-02-05 17:44:01 +00:00
|
|
|
"database/sql"
|
|
|
|
|
"errors"
|
2025-02-11 15:22:59 +00:00
|
|
|
"log/slog"
|
2025-03-05 12:05:46 +00:00
|
|
|
|
2025-02-12 19:00:25 +00:00
|
|
|
docsyncrunner "queryorchestration/api/docSyncRunner"
|
2025-02-03 17:30:50 +00:00
|
|
|
"queryorchestration/internal/database/repository"
|
2025-04-03 19:17:24 +00:00
|
|
|
"queryorchestration/internal/serviceconfig/objectstore"
|
2025-02-03 18:51:44 +00:00
|
|
|
"queryorchestration/internal/serviceconfig/queue"
|
2025-02-03 17:30:50 +00:00
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Create struct {
|
2025-11-26 19:23:42 +00:00
|
|
|
Bucket string
|
|
|
|
|
Key objectstore.BucketKey
|
|
|
|
|
Hash string
|
|
|
|
|
Filename *string // Optional filename for batch processing
|
|
|
|
|
OriginalPath *string // Original path provided during upload (immutable after creation)
|
|
|
|
|
FolderID *uuid.UUID // Optional folder ID if folder hierarchy was pre-created
|
2025-02-03 17:30:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Service) Create(ctx context.Context, doc *Create) (uuid.UUID, error) {
|
|
|
|
|
params, err := s.getCreateParams(ctx, doc)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return uuid.Nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
id, err := s.submitCreate(ctx, params)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return uuid.Nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-06 15:00:26 +00:00
|
|
|
if params.ID == nil {
|
2025-02-12 19:00:25 +00:00
|
|
|
err := s.cfg.SendToQueue(ctx, &queue.SendParams{
|
|
|
|
|
QueueURL: s.cfg.GetDocumentSyncURL(),
|
|
|
|
|
Body: docsyncrunner.Body{
|
2025-04-02 18:50:03 +00:00
|
|
|
DocumentID: id,
|
2025-02-12 19:00:25 +00:00
|
|
|
},
|
|
|
|
|
})
|
2025-02-06 15:00:26 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return uuid.Nil, err
|
|
|
|
|
}
|
2025-02-03 17:30:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return id, nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-05 17:44:01 +00:00
|
|
|
type createDocumentParams struct {
|
2025-11-26 19:23:42 +00:00
|
|
|
ID *uuid.UUID
|
|
|
|
|
Hash string
|
|
|
|
|
Bucket string
|
|
|
|
|
Key objectstore.BucketKey
|
|
|
|
|
Filename *string
|
|
|
|
|
OriginalPath *string
|
|
|
|
|
FolderID *uuid.UUID
|
2025-02-05 17:44:01 +00:00
|
|
|
}
|
2025-02-03 17:30:50 +00:00
|
|
|
|
2025-02-05 17:44:01 +00:00
|
|
|
func (s *Service) getCreateParams(ctx context.Context, doc *Create) (*createDocumentParams, error) {
|
|
|
|
|
idbyhash, err := s.cfg.GetDBQueries().GetDocumentIDByHash(ctx, &repository.GetDocumentIDByHashParams{
|
2025-04-03 19:17:24 +00:00
|
|
|
Clientid: doc.Key.ClientID,
|
2025-03-10 11:03:00 +00:00
|
|
|
Hash: doc.Hash,
|
2025-02-05 17:44:01 +00:00
|
|
|
})
|
2025-03-20 11:06:41 +00:00
|
|
|
var docID *uuid.UUID
|
2025-02-05 17:44:01 +00:00
|
|
|
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
|
|
|
|
return nil, err
|
|
|
|
|
} else if err == nil {
|
|
|
|
|
docID = &idbyhash
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &createDocumentParams{
|
2025-11-26 19:23:42 +00:00
|
|
|
ID: docID,
|
|
|
|
|
Hash: doc.Hash,
|
|
|
|
|
Key: doc.Key,
|
|
|
|
|
Bucket: doc.Bucket,
|
|
|
|
|
Filename: doc.Filename,
|
|
|
|
|
OriginalPath: doc.OriginalPath,
|
|
|
|
|
FolderID: doc.FolderID,
|
2025-02-03 17:30:50 +00:00
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-05 17:44:01 +00:00
|
|
|
func (s *Service) submitCreate(ctx context.Context, params *createDocumentParams) (uuid.UUID, error) {
|
|
|
|
|
var id uuid.UUID
|
|
|
|
|
|
|
|
|
|
err := s.cfg.ExecuteDBTransaction(ctx, func(ctx context.Context, q *repository.Queries) error {
|
2025-03-20 11:06:41 +00:00
|
|
|
var dbid uuid.UUID
|
2025-02-05 17:44:01 +00:00
|
|
|
if params.ID == nil {
|
|
|
|
|
createid, err := s.cfg.GetDBQueries().CreateDocument(ctx, &repository.CreateDocumentParams{
|
2025-11-26 19:23:42 +00:00
|
|
|
Clientid: params.Key.ClientID,
|
|
|
|
|
Hash: params.Hash,
|
|
|
|
|
BatchID: params.Key.BatchID,
|
|
|
|
|
Filename: params.Filename,
|
|
|
|
|
Folderid: params.FolderID,
|
|
|
|
|
Originalpath: params.OriginalPath,
|
2025-02-05 17:44:01 +00:00
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2025-04-03 19:17:24 +00:00
|
|
|
slog.Debug("document created", "id", createid.String(), "client", params.Key.ClientID)
|
2025-02-05 17:44:01 +00:00
|
|
|
|
|
|
|
|
dbid = createid
|
|
|
|
|
} else {
|
2025-04-03 19:17:24 +00:00
|
|
|
slog.Debug("document exists", "id", params.ID.String(), "client", params.Key.ClientID)
|
2025-02-11 15:22:59 +00:00
|
|
|
|
2025-02-05 17:44:01 +00:00
|
|
|
dbid = *params.ID
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err := s.cfg.GetDBQueries().AddDocumentEntry(ctx, &repository.AddDocumentEntryParams{
|
|
|
|
|
Documentid: dbid,
|
2025-04-03 19:17:24 +00:00
|
|
|
Bucket: params.Bucket,
|
|
|
|
|
Key: params.Key.String(),
|
2025-02-05 17:44:01 +00:00
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-20 11:06:41 +00:00
|
|
|
id = dbid
|
2025-02-05 17:44:01 +00:00
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
})
|
2025-02-03 17:30:50 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return uuid.Nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return id, nil
|
|
|
|
|
}
|