2025-02-03 17:30:50 +00:00
|
|
|
package documentinit
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"queryorchestration/internal/database"
|
|
|
|
|
"queryorchestration/internal/database/repository"
|
|
|
|
|
"queryorchestration/internal/document"
|
|
|
|
|
documentclean "queryorchestration/internal/document/clean"
|
|
|
|
|
"queryorchestration/internal/job"
|
2025-02-03 18:51:44 +00:00
|
|
|
"queryorchestration/internal/serviceconfig/queue"
|
2025-02-03 17:30:50 +00:00
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go-v2/service/sqs/types"
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Create struct {
|
|
|
|
|
JobID uuid.UUID `json:"jobId" validate:"required,uuid"`
|
|
|
|
|
Location document.Location `json:"location" validate:"required"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Service) Create(ctx context.Context, doc *Create) (uuid.UUID, error) {
|
|
|
|
|
j, err := s.svc.Job.Get(ctx, doc.JobID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return uuid.Nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = s.informCreate(ctx, id, j)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return uuid.Nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return id, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Service) getCreateParams(ctx context.Context, doc *Create) (*repository.CreateDocumentParams, error) {
|
|
|
|
|
// TODO - get document
|
|
|
|
|
|
|
|
|
|
hash, err := s.getHash()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &repository.CreateDocumentParams{
|
|
|
|
|
Jobid: database.MustToDBUUID(doc.JobID),
|
|
|
|
|
Hash: hash,
|
|
|
|
|
Location: doc.Location,
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Service) submitCreate(ctx context.Context, params *repository.CreateDocumentParams) (uuid.UUID, error) {
|
|
|
|
|
// TODO create - or if hash exists log attempt
|
|
|
|
|
dbid, err := s.cfg.GetDBQueries().CreateDocument(ctx, params)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return uuid.Nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
id := database.MustToUUID(dbid)
|
|
|
|
|
|
|
|
|
|
return id, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Service) getHash() (string, error) {
|
|
|
|
|
// TODO
|
|
|
|
|
return "example_hash", nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Service) informCreate(ctx context.Context, id uuid.UUID, j *job.Job) error {
|
|
|
|
|
if !j.CanSync {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-03 18:51:44 +00:00
|
|
|
err := s.cfg.SendToQueue(ctx, &queue.SendParams{
|
|
|
|
|
QueueURL: s.cfg.GetDocumentCleanURL(),
|
|
|
|
|
Body: documentclean.Create{
|
|
|
|
|
ID: id,
|
|
|
|
|
},
|
|
|
|
|
Attributes: map[string]types.MessageAttributeValue{},
|
|
|
|
|
})
|
2025-02-03 17:30:50 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|