mirror of
https://github.com/SamyRai/turash.git
synced 2025-12-26 23:01:33 +00:00
69 lines
1.8 KiB
Go
69 lines
1.8 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"time"
|
|
|
|
"bugulma/backend/internal/domain"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
type ActivityService struct {
|
|
activityRepo domain.ActivityLogRepository
|
|
}
|
|
|
|
func NewActivityService(activityRepo domain.ActivityLogRepository) *ActivityService {
|
|
return &ActivityService{
|
|
activityRepo: activityRepo,
|
|
}
|
|
}
|
|
|
|
// LogActivity logs an activity
|
|
func (s *ActivityService) LogActivity(
|
|
ctx context.Context,
|
|
userID string,
|
|
action domain.ActivityAction,
|
|
targetType string,
|
|
targetID string,
|
|
metadata map[string]interface{},
|
|
ipAddress string,
|
|
userAgent string,
|
|
) error {
|
|
metadataJSON, err := json.Marshal(metadata)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to marshal metadata: %w", err)
|
|
}
|
|
|
|
activity := &domain.ActivityLog{
|
|
ID: uuid.New().String(),
|
|
UserID: userID,
|
|
Action: action,
|
|
TargetType: targetType,
|
|
TargetID: targetID,
|
|
Metadata: string(metadataJSON),
|
|
IPAddress: ipAddress,
|
|
UserAgent: userAgent,
|
|
Timestamp: time.Now(),
|
|
}
|
|
|
|
return s.activityRepo.Create(ctx, activity)
|
|
}
|
|
|
|
// GetUserActivity retrieves activity logs for a user
|
|
func (s *ActivityService) GetUserActivity(ctx context.Context, userID string, limit, offset int) ([]*domain.ActivityLog, int64, error) {
|
|
return s.activityRepo.GetByUser(ctx, userID, limit, offset)
|
|
}
|
|
|
|
// GetRecentActivity retrieves recent activity logs
|
|
func (s *ActivityService) GetRecentActivity(ctx context.Context, limit int) ([]*domain.ActivityLog, error) {
|
|
return s.activityRepo.GetRecent(ctx, limit)
|
|
}
|
|
|
|
// GetTargetActivity retrieves activity logs for a target entity
|
|
func (s *ActivityService) GetTargetActivity(ctx context.Context, targetType, targetID string, limit, offset int) ([]*domain.ActivityLog, int64, error) {
|
|
return s.activityRepo.GetByTarget(ctx, targetType, targetID, limit, offset)
|
|
}
|