efe87321b2
Testing Tweaks * parallel * slowestquery * split * cleanup * health * dynamiccores * go * profile * timeout * commitmychanges * taskfile * sqlcheckstart * client * cost * ctxtimeout
105 lines
2.1 KiB
Go
105 lines
2.1 KiB
Go
package clientupdate
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"log/slog"
|
|
|
|
clientsyncrunner "queryorchestration/api/clientSyncRunner"
|
|
"queryorchestration/internal/client"
|
|
"queryorchestration/internal/database/repository"
|
|
"queryorchestration/internal/serviceconfig/queue"
|
|
"queryorchestration/internal/validation"
|
|
)
|
|
|
|
type Update struct {
|
|
Name *string
|
|
CanSync *bool
|
|
}
|
|
|
|
func (s *Service) Update(ctx context.Context, id string, entity *Update) error {
|
|
current, err := s.svc.Client.Get(ctx, id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = s.normalizeUpdateParams(id, current, entity)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = s.submitUpdate(ctx, id, entity)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = s.informUpdate(ctx, id, entity)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *Service) informUpdate(ctx context.Context, id string, entity *Update) error {
|
|
if entity.CanSync == nil || !*entity.CanSync {
|
|
return nil
|
|
}
|
|
|
|
return s.cfg.SendToQueue(ctx, &queue.SendParams{
|
|
QueueURL: s.cfg.GetClientSyncURL(),
|
|
Body: clientsyncrunner.Body{
|
|
ClientID: id,
|
|
},
|
|
})
|
|
}
|
|
|
|
func (s *Service) submitUpdate(ctx context.Context, id string, entity *Update) error {
|
|
return s.cfg.ExecuteDBTransaction(ctx, func(ctx context.Context, q *repository.Queries) error {
|
|
id := id
|
|
|
|
if entity.Name != nil {
|
|
err := q.UpdateClient(ctx, &repository.UpdateClientParams{
|
|
Clientid: id,
|
|
Name: *entity.Name,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
if entity.CanSync != nil {
|
|
err := q.AddClientCanSync(ctx, &repository.AddClientCanSyncParams{
|
|
Clientid: id,
|
|
Cansync: *entity.CanSync,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
slog.Debug("client updated", "update", *entity)
|
|
|
|
return nil
|
|
})
|
|
}
|
|
|
|
func (s *Service) normalizeUpdateParams(id string, current *client.Client, entity *Update) error {
|
|
if entity == nil || current == nil || current.ID != id {
|
|
return errors.New("no updates presented")
|
|
}
|
|
|
|
err := current.NormalizeNameUpdate(&entity.Name)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
current.NormalizeCanSyncUpdate(&entity.CanSync)
|
|
|
|
if validation.AreAllPointersNilExcept(entity) {
|
|
return errors.New("no updates presented")
|
|
}
|
|
|
|
return nil
|
|
}
|