2024-01-15 07:00:08 +00:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"gitea.dwysokinski.me/twhelp/corev3/internal/domain"
|
|
|
|
)
|
|
|
|
|
|
|
|
type TribeSnapshotRepository interface {
|
|
|
|
// Create persists tribe snapshots in a store (e.g. Postgres).
|
|
|
|
// Duplicates are ignored.
|
|
|
|
Create(ctx context.Context, params ...domain.CreateTribeSnapshotParams) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type TribeSnapshotService struct {
|
|
|
|
repo TribeSnapshotRepository
|
|
|
|
tribeSvc *TribeService
|
|
|
|
pub SnapshotPublisher
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewTribeSnapshotService(
|
|
|
|
repo TribeSnapshotRepository,
|
|
|
|
tribeSvc *TribeService,
|
|
|
|
pub SnapshotPublisher,
|
|
|
|
) *TribeSnapshotService {
|
|
|
|
return &TribeSnapshotService{repo: repo, tribeSvc: tribeSvc, pub: pub}
|
|
|
|
}
|
|
|
|
|
|
|
|
//nolint:gocyclo
|
|
|
|
func (svc *TribeSnapshotService) Create(
|
|
|
|
ctx context.Context,
|
|
|
|
createSnapshotsCmdPayload domain.CreateSnapshotsCmdPayload,
|
|
|
|
) error {
|
|
|
|
serverKey := createSnapshotsCmdPayload.ServerKey()
|
|
|
|
date := createSnapshotsCmdPayload.Date()
|
|
|
|
|
|
|
|
listTribesParams := domain.NewListTribesParams()
|
|
|
|
if err := listTribesParams.SetServerKeys([]string{serverKey}); err != nil {
|
|
|
|
return fmt.Errorf("%s: %w", serverKey, err)
|
|
|
|
}
|
|
|
|
if err := listTribesParams.SetDeleted(domain.NullBool{
|
|
|
|
Value: false,
|
|
|
|
Valid: true,
|
|
|
|
}); err != nil {
|
|
|
|
return fmt.Errorf("%s: %w", serverKey, err)
|
|
|
|
}
|
|
|
|
if err := listTribesParams.SetSort([]domain.TribeSort{domain.TribeSortIDASC}); err != nil {
|
|
|
|
return fmt.Errorf("%s: %w", serverKey, err)
|
|
|
|
}
|
|
|
|
if err := listTribesParams.SetLimit(domain.TribeListMaxLimit); err != nil {
|
|
|
|
return fmt.Errorf("%s: %w", serverKey, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
2024-02-12 07:07:15 +00:00
|
|
|
res, err := svc.tribeSvc.List(ctx, listTribesParams)
|
2024-01-15 07:00:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%s: %w", serverKey, err)
|
|
|
|
}
|
2024-02-12 07:07:15 +00:00
|
|
|
tribes := res.Tribes()
|
2024-01-15 07:00:08 +00:00
|
|
|
|
|
|
|
if len(tribes) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
params, err := domain.NewCreateTribeSnapshotParams(tribes, date)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%s: %w", serverKey, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = svc.repo.Create(ctx, params...); err != nil {
|
|
|
|
return fmt.Errorf("%s: %w", serverKey, err)
|
|
|
|
}
|
|
|
|
|
2024-02-12 07:07:15 +00:00
|
|
|
if res.Next().IsZero() {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = listTribesParams.SetCursor(res.Next()); err != nil {
|
2024-01-15 07:00:08 +00:00
|
|
|
return fmt.Errorf("%s: %w", serverKey, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
payload, err := domain.NewSnapshotsCreatedEventPayload(serverKey, createSnapshotsCmdPayload.VersionCode())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%s: couldn't construct domain.SnapshotsCreatedEventPayload: %w", serverKey, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = svc.pub.EventCreated(ctx, payload); err != nil {
|
|
|
|
return fmt.Errorf("%s: %w", serverKey, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|