package usecase import ( "encoding/json" "users_management/m/model/entity" "users_management/m/repository" "github.com/google/uuid" ) type ActivityLogUseCase interface { LogActivity(userID uuid.UUID, action, resource string, resourceID *string, oldData, newData interface{}, ipAddress, userAgent, details string) error GetUserLogs(userID uuid.UUID, page, limit int) ([]entity.ActivityLog, int64, error) GetAllLogs(page, limit int) ([]entity.ActivityLog, int64, error) GetTeknisinLogs(page, limit int) ([]entity.ActivityLog, int64, error) } type activityLogUseCase struct { activityLogRepo repository.ActivityLogRepo } func NewActivityLogUseCase(activityLogRepo repository.ActivityLogRepo) ActivityLogUseCase { return &activityLogUseCase{ activityLogRepo: activityLogRepo, } } func (uc *activityLogUseCase) LogActivity(userID uuid.UUID, action, resource string, resourceID *string, oldData, newData interface{}, ipAddress, userAgent, details string) error { var oldDataJSON, newDataJSON *string if oldData != nil { oldBytes, err := json.Marshal(oldData) if err == nil { oldStr := string(oldBytes) oldDataJSON = &oldStr } } if newData != nil { newBytes, err := json.Marshal(newData) if err == nil { newStr := string(newBytes) newDataJSON = &newStr } } var detailsPtr *string if details != "" { detailsPtr = &details } log := entity.ActivityLog{ ID: uuid.New(), UserID: userID, Action: action, Resource: resource, ResourceID: resourceID, OldData: oldDataJSON, NewData: newDataJSON, IPAddress: ipAddress, UserAgent: userAgent, Details: detailsPtr, } return uc.activityLogRepo.Create(log) } func (uc *activityLogUseCase) GetUserLogs(userID uuid.UUID, page, limit int) ([]entity.ActivityLog, int64, error) { offset := (page - 1) * limit logs, err := uc.activityLogRepo.GetByUserID(userID, limit, offset) if err != nil { return nil, 0, err } total, err := uc.activityLogRepo.CountByUserID(userID) return logs, total, err } func (uc *activityLogUseCase) GetAllLogs(page, limit int) ([]entity.ActivityLog, int64, error) { offset := (page - 1) * limit logs, err := uc.activityLogRepo.GetAllForAdmins(limit, offset) if err != nil { return nil, 0, err } total, err := uc.activityLogRepo.CountAll() return logs, total, err } func (uc *activityLogUseCase) GetTeknisinLogs(page, limit int) ([]entity.ActivityLog, int64, error) { offset := (page - 1) * limit logs, err := uc.activityLogRepo.GetByUserRole("Teknisi", limit, offset) if err != nil { return nil, 0, err } total, err := uc.activityLogRepo.CountByUserRole("Teknisi") return logs, total, err }