using HermesSocketLibrary.Requests.Messages; using HermesSocketServer.Models; using ILogger = Serilog.ILogger; namespace HermesSocketServer.Requests { public class UpdateRedemption : IRequest { public string Name => "update_redemption"; public string[] RequiredKeys => ["id", "redemption", "action", "order"]; private ILogger _logger; public UpdateRedemption(ILogger logger) { _logger = logger; } public Task Grant(Channel channel, IDictionary data) { var id = data["id"].ToString()!; string redemptionId = data["redemption"].ToString()!; string actionName = data["action"].ToString()!; if (channel.Actions.Get(actionName) == null) return Task.FromResult(RequestResult.Failed("Action Name must be an existing action.")); if (!int.TryParse(data["order"].ToString()!, out var order)) return Task.FromResult(RequestResult.Failed("Order must be an integer.")); bool state = data["state"].ToString()?.ToLower() == "true"; var redemption = new Redemption() { Id = id, UserId = channel.Id, RedemptionId = redemptionId, ActionName = actionName, Order = order, State = true, }; bool result = channel.Redemptions.Modify(id.ToString(), r => { if (r.UserId != channel.Id) return; r.RedemptionId = redemptionId; r.ActionName = actionName; r.Order = order; r.State = state; }); var r = channel.Redemptions.Get(id); if (result) { _logger.Information($"Added redemption to channel [id: {id}][redemption id: {redemptionId}][action: {actionName}][order: {order}][channel: {channel.Id}]"); return Task.FromResult(RequestResult.Successful(r)); } if (r == null || r.UserId != channel.Id) return Task.FromResult(RequestResult.Failed("Redemption does not exist.")); return Task.FromResult(RequestResult.Failed("Something went wrong when updating the cache.")); } } }