diff --git a/local/model/activity_log.go b/local/model/activity_log.go index b925b1a..e23523b 100644 --- a/local/model/activity_log.go +++ b/local/model/activity_log.go @@ -15,6 +15,8 @@ const ( ActionServerStop ActionType = "server_stop" ActionServerRestart ActionType = "server_restart" ActionLeaderboardUpdate ActionType = "leaderboard_update" + ActionPresetCreate ActionType = "preset_create" + ActionPresetApply ActionType = "preset_apply" ) type ActivityLog struct { diff --git a/local/service/config.go b/local/service/config.go index 68f593f..95601b4 100755 --- a/local/service/config.go +++ b/local/service/config.go @@ -564,7 +564,7 @@ func (as *ConfigService) SaveConfiguration(server *model.Server, config *model.C // ApplySection writes a single config section to the server's config file and // records the change. It is used by ConfigPresetService to apply preset sections. func (as *ConfigService) ApplySection(ctx context.Context, serverID, configFile string, body *map[string]interface{}, actorID, actorUsername string) (*model.Config, error) { - return as.updateConfigInternal(ctx, serverID, configFile, body, false, actorID, actorUsername) + return as.updateConfigInternal(ctx, serverID, configFile, body, true, actorID, actorUsername) } func (as *ConfigService) SetServerName(server *model.Server, name string) error { diff --git a/local/service/config_preset.go b/local/service/config_preset.go index 0b7d2e6..be0f4c4 100644 --- a/local/service/config_preset.go +++ b/local/service/config_preset.go @@ -3,6 +3,7 @@ package service import ( "acc-server-manager/local/model" "acc-server-manager/local/repository" + "acc-server-manager/local/utl/logging" "context" "encoding/json" "fmt" @@ -19,12 +20,14 @@ const ( type ConfigPresetService struct { repository *repository.ConfigPresetRepository configService *ConfigService + activityLog *ActivityLogService } -func NewConfigPresetService(repo *repository.ConfigPresetRepository, configService *ConfigService) *ConfigPresetService { +func NewConfigPresetService(repo *repository.ConfigPresetRepository, configService *ConfigService, activityLog *ActivityLogService) *ConfigPresetService { return &ConfigPresetService{ repository: repo, configService: configService, + activityLog: activityLog, } } @@ -66,6 +69,8 @@ func (s *ConfigPresetService) CreatePreset(ctx context.Context, req *model.Confi if err := s.repository.Insert(ctx, preset); err != nil { return nil, err } + + logging.Info("Preset created: id=%s name=%q", preset.ID, preset.Name) return preset, nil } @@ -110,6 +115,11 @@ func (s *ConfigPresetService) ApplyPreset(ctx context.Context, serverID string, return nil, err } + serverUUID, err := uuid.Parse(serverID) + if err != nil { + return nil, fmt.Errorf("invalid server ID: %v", err) + } + type sectionEntry struct { fileName string raw string @@ -141,6 +151,19 @@ func (s *ConfigPresetService) ApplyPreset(ctx context.Context, serverID string, } } + details := fmt.Sprintf(`{"preset_id":%q,"preset_name":%q,"sections_applied":%d}`, presetID, preset.Name, len(results)) + go func() { + if logErr := s.activityLog.Log(context.Background(), &model.ActivityLog{ + ServerID: serverUUID, + UserID: actorID, + Username: actorUsername, + Action: model.ActionPresetApply, + Details: details, + }); logErr != nil { + logging.Error("Failed to log preset apply: %v", logErr) + } + }() + return results, nil }