using System.Text.Json; using HermesSocketLibrary.Requests.Messages; using HermesSocketServer.Models; using ILogger = Serilog.ILogger; namespace HermesSocketServer.Requests { public class UpdateRedeemableAction : IRequest { public string Name => "update_redeemable_action"; public string[] RequiredKeys => ["name", "data", "type"]; private ILogger _logger; public UpdateRedeemableAction(ILogger logger) { _logger = logger; } public Task Grant(Channel channel, IDictionary data) { string name = data["name"].ToString()!; string d = data["data"].ToString()!; string type = data["type"].ToString()!; IDictionary dict = new Dictionary(); try { dict = JsonSerializer.Deserialize>(d)!; } catch (Exception ex) { _logger.Error(ex, $"Failed to parse data on redeemable action while updating action [name: {name}][type: {type}][data: {d}]"); return Task.FromResult(RequestResult.Failed("Could not parse the data on this action.")); } var action = new RedeemableAction() { UserId = channel.Id, Name = name, Data = dict, Type = type, }; bool result = channel.Actions.Modify(name, action => { action.Type = type; action.Data = dict; }); if (result) { _logger.Information($"Added redeemable action to channel [name: {name}][type: {type}][channel: {channel.Id}]"); return Task.FromResult(RequestResult.Successful(action)); } if (channel.Actions.Get(name) == null) return Task.FromResult(RequestResult.Failed("Action does not exist.")); return Task.FromResult(RequestResult.Failed("Something went wrong when updating the cache.")); } } }