You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
1.4 KiB
Go

package admin
import (
"context"
"time"
"github.com/google/uuid"
"knowfoolery/backend/services/admin-service/internal/domain/audit"
)
// Service provides admin use-cases.
type Service struct {
auditRepo audit.Repository
retention time.Duration
}
// NewService builds an admin service with a retention period in days.
func NewService(auditRepo audit.Repository, retentionDays int) *Service {
if retentionDays <= 0 {
retentionDays = 90
}
return &Service{
auditRepo: auditRepo,
retention: time.Duration(retentionDays) * 24 * time.Hour,
}
}
func (s *Service) AppendAudit(ctx context.Context, actorID, actorEmail, action, resource string, details any) error {
return s.auditRepo.Append(ctx, audit.Entry{
ID: uuid.NewString(),
At: time.Now().UTC(),
ActorID: actorID,
ActorEmail: actorEmail,
Action: action,
Resource: resource,
Details: details,
})
}
func (s *Service) ListAudit(ctx context.Context, limit, offset int) ([]audit.Entry, error) {
return s.auditRepo.List(ctx, limit, offset)
}
func (s *Service) Dashboard(ctx context.Context) (map[string]any, error) {
auditCount, err := s.auditRepo.Count(ctx)
if err != nil {
return nil, err
}
return map[string]any{
"audit_entries_total": auditCount,
"generated_at": time.Now().UTC(),
}, nil
}
func (s *Service) PruneAudit(ctx context.Context) (int64, error) {
before := time.Now().UTC().Add(-s.retention)
return s.auditRepo.PruneBefore(ctx, before)
}