using HermesSocketServer.Models; using HermesSocketServer.Services; using ILogger = Serilog.ILogger; namespace HermesSocketServer.Requests { public class UpdatePolicy : IRequest { public string Name => "update_policy"; public string[] RequiredKeys => ["id", "groupId", "path", "count", "span"]; private ChannelManager _channels; private ILogger _logger; public UpdatePolicy(ChannelManager channels, ILogger logger) { _channels = channels; _logger = logger; } public async Task Grant(string sender, IDictionary? data) { var id = Guid.Parse(data["id"].ToString()!); string groupId = data["groupId"].ToString()!; string path = data["path"].ToString()!; int count = int.Parse(data["count"].ToString()!); int span = int.Parse(data["span"].ToString()!); var channel = _channels.Get(sender)!; bool result = channel.Policies.Set(id.ToString(), new PolicyMessage() { Id = id, UserId = sender, GroupId = Guid.Parse(groupId), Path = path, Usage = count, Span = span, }); if (result) { var policy = channel.Policies.Get(id.ToString()); _logger.Information($"Updated policy to channel [policy id: {id}][group id: {groupId}][path: {path}][count: {count}][span: {span}][channel: {sender}]"); return RequestResult.Successful(policy); } return RequestResult.Failed("Something went wrong when updating the cache."); } } }