101 lines
2.9 KiB
Go
101 lines
2.9 KiB
Go
package useCases
|
|
|
|
import (
|
|
"RenewCMS/internal/domain/audit"
|
|
"context"
|
|
"time"
|
|
)
|
|
|
|
type GetActivityRetentionUseCase struct {
|
|
settings audit.SettingsRepository
|
|
events audit.Repository
|
|
now func() time.Time
|
|
}
|
|
|
|
func NewGetActivityRetentionUseCase(settings audit.SettingsRepository, events audit.Repository, now func() time.Time) *GetActivityRetentionUseCase {
|
|
return &GetActivityRetentionUseCase{settings, events, now}
|
|
}
|
|
|
|
func (g *GetActivityRetentionUseCase) GetActivityRetention() (audit.Settings, error) {
|
|
current, err := g.settings.Get()
|
|
if err != nil {
|
|
return audit.Settings{}, err
|
|
}
|
|
current.LogSizeBytes, err = g.events.SizeBytes()
|
|
if err != nil {
|
|
return audit.Settings{}, err
|
|
}
|
|
return current.WithNextCleanup(g.now()), nil
|
|
}
|
|
|
|
type UpdateActivityRetentionCommand struct {
|
|
Days *int
|
|
MaxGB *int
|
|
}
|
|
|
|
type UpdateActivityRetentionUseCase struct {
|
|
settings audit.SettingsRepository
|
|
events audit.Repository
|
|
recorder audit.Recorder
|
|
now func() time.Time
|
|
}
|
|
|
|
func NewUpdateActivityRetentionUseCase(settings audit.SettingsRepository, events audit.Repository, recorder audit.Recorder, now func() time.Time) *UpdateActivityRetentionUseCase {
|
|
return &UpdateActivityRetentionUseCase{settings, events, recorder, now}
|
|
}
|
|
|
|
func (u *UpdateActivityRetentionUseCase) UpdateActivityRetention(ctx context.Context, command UpdateActivityRetentionCommand) (audit.Settings, error) {
|
|
if command.Days == nil && command.MaxGB == nil {
|
|
return audit.Settings{}, audit.ErrInvalidRetention
|
|
}
|
|
if command.Days != nil {
|
|
if err := audit.ValidateRetentionDays(*command.Days); err != nil {
|
|
return audit.Settings{}, err
|
|
}
|
|
}
|
|
if command.MaxGB != nil {
|
|
if err := audit.ValidateRetentionMaxGB(*command.MaxGB); err != nil {
|
|
return audit.Settings{}, err
|
|
}
|
|
}
|
|
current, err := u.settings.Get()
|
|
if err != nil {
|
|
return audit.Settings{}, err
|
|
}
|
|
size, err := u.events.SizeBytes()
|
|
if err != nil {
|
|
return audit.Settings{}, err
|
|
}
|
|
current.LogSizeBytes = size
|
|
|
|
updated := current
|
|
if command.Days != nil {
|
|
updated.RetentionDays = *command.Days
|
|
}
|
|
if command.MaxGB != nil {
|
|
updated.RetentionMaxGB = *command.MaxGB
|
|
}
|
|
daysChanged := updated.RetentionDays != current.RetentionDays
|
|
sizeChanged := updated.RetentionMaxGB != current.RetentionMaxGB
|
|
if !daysChanged && !sizeChanged {
|
|
return current.WithNextCleanup(u.now()), nil
|
|
}
|
|
if err := u.settings.SaveRetention(updated.RetentionDays, updated.RetentionMaxGB); err != nil {
|
|
return audit.Settings{}, err
|
|
}
|
|
|
|
event := audit.NewEvent(audit.ActionRetentionChanged, audit.CategorySystem)
|
|
event.Params = map[string]any{}
|
|
if daysChanged {
|
|
event.Params[audit.ParamFrom] = current.RetentionDays
|
|
event.Params[audit.ParamTo] = updated.RetentionDays
|
|
}
|
|
if sizeChanged {
|
|
event.Params[audit.ParamMaxGBFrom] = current.RetentionMaxGB
|
|
event.Params[audit.ParamMaxGBTo] = updated.RetentionMaxGB
|
|
}
|
|
u.recorder.Record(ctx, event)
|
|
|
|
return updated.WithNextCleanup(u.now()), nil
|
|
}
|