github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/domain/operation/service.go (about) 1 package operation 2 3 import ( 4 "context" 5 "time" 6 7 "github.com/kyma-incubator/compass/components/director/pkg/log" 8 9 "github.com/kyma-incubator/compass/components/director/internal/model" 10 "github.com/pkg/errors" 11 ) 12 13 // OperationRepository is responsible for repository-layer operation operations 14 // 15 //go:generate mockery --name=OperationRepository --output=automock --outpkg=automock --case=underscore --disable-version-string 16 type OperationRepository interface { 17 Create(ctx context.Context, model *model.Operation) error 18 DeleteOlderThan(ctx context.Context, opType string, status model.OperationStatus, date time.Time) error 19 } 20 21 // UIDService is responsible for service-layer uid operations 22 // 23 //go:generate mockery --name=UIDService --output=automock --outpkg=automock --case=underscore --disable-version-string 24 type UIDService interface { 25 Generate() string 26 } 27 28 type service struct { 29 opRepo OperationRepository 30 uidService UIDService 31 } 32 33 // NewService creates operations service 34 func NewService(opRepo OperationRepository, uidService UIDService) *service { 35 return &service{ 36 opRepo: opRepo, 37 uidService: uidService, 38 } 39 } 40 41 // Create creates new operation entity 42 func (s *service) Create(ctx context.Context, in *model.OperationInput) error { 43 id := s.uidService.Generate() 44 op := in.ToOperation(id) 45 46 if err := s.opRepo.Create(ctx, op); err != nil { 47 return errors.Wrapf(err, "error occurred while creating an Operation with id %s and type %s", op.ID, op.OpType) 48 } 49 50 log.C(ctx).Infof("Successfully created an Operation with id %s and type %s", op.ID, op.OpType) 51 return nil 52 } 53 54 // CreateMultiple creates multiple operations 55 func (s *service) CreateMultiple(ctx context.Context, in []*model.OperationInput) error { 56 if in == nil { 57 return nil 58 } 59 60 for _, op := range in { 61 if op == nil { 62 continue 63 } 64 65 if err := s.Create(ctx, op); err != nil { 66 return err 67 } 68 } 69 70 return nil 71 } 72 73 // DeleteOlderThan deletes all operations of type `opType` with status `status` older than `days` 74 func (s *service) DeleteOlderThan(ctx context.Context, opType string, status model.OperationStatus, days int) error { 75 if err := s.opRepo.DeleteOlderThan(ctx, opType, status, time.Now().AddDate(0, 0, -1*days)); err != nil { 76 return errors.Wrapf(err, "while deleting Operations of type %s and status %s older than %d", opType, status, days) 77 } 78 79 return nil 80 }