using System; using System.Linq; using System.Threading; using System.Threading.Tasks; using System.Web.Services.Description; using Dapr.Client; using GB5Shared.DTO.EventLog; using GB5Shared.DTO.Framework.Login; using GB5Shared.DTO.PubSub; using GB5Shared.DTO.PushNotification; using Microsoft.Extensions.Logging; using Newtonsoft.Json; namespace GB5Shared.EventLogPublish { public class EventLogPublish { private readonly DaprClient DaprClient; private readonly ILogger Logger; public EventLogPublish(DaprClient daprClient, ILogger logger) { DaprClient = daprClient; Logger = logger; } /// /// Generic helper to create EventLogDTO and publish event. /// Made public so it can be called outside the class. /// public async Task PublishEventLogAsync( string EventText, T entity, int EventTypeId, int DataId, LoginDTO loginDTO, string topic = "eventlog-topic", CancellationToken ct = default) { try { // Serialize the entity to JSON for DATA string jsonData = JsonConvert.SerializeObject(entity); // Create object first EventLogDTO eventLogDTO = new(); // Assign properties one by one eventLogDTO.EventText = EventText; eventLogDTO.Link = ""; eventLogDTO.SourceId = -1; eventLogDTO.UserId = loginDTO.UserId; eventLogDTO.Tags = ""; eventLogDTO.EventValue = ""; eventLogDTO.Data = jsonData; eventLogDTO.Geo = loginDTO.MachineIP; // mapping IP to Geo (adjust if needed) eventLogDTO.EventTypeId = EventTypeId; eventLogDTO.EventClassId = -1; eventLogDTO.LogBookId = -1; eventLogDTO.DataId = DataId; eventLogDTO.EventChannelId = -1; eventLogDTO.Ouid = -1; eventLogDTO.MachineIP = loginDTO.MachineIP; eventLogDTO.ModeOfWorking = loginDTO.ModeOfWorking; eventLogDTO.LoginEventLogId = loginDTO.LoginEventLogId; eventLogDTO.Login = loginDTO; eventLogDTO.Timestamp = DateTime.UtcNow; // Prepare PublishDTO PublishDTO publishDTO = new(); publishDTO.PubSubTopicName = topic; publishDTO.Request = eventLogDTO; publishDTO.Login = loginDTO; // Publish the event (actual send) await PublishEventsAsync(publishDTO, loginDTO, eventLogDTO, ct); } catch (Exception ex) { Logger.LogWarning(ex, "Failed to publish event log."); } } public async Task PublishPushNotification(LoginDTO LoginDTO,string Topic = "GBUSER",CancellationToken ct = default) { try { string Login = JsonConvert.SerializeObject(LoginDTO); // Publish via Dapr pub/sub await DaprClient.PublishEventAsync("pubsub", Topic, Login, cancellationToken: ct); Logger.LogInformation($"Published Push Notification: {Topic}", LoginDTO); } catch (Exception ex) { Logger.LogWarning(ex, "Failed to publish Notification."); } } /// /// Low-level method that enriches PublishDTO and actually sends it to pubsub. /// protected virtual async Task PublishEventsAsync( PublishDTO publishDTO, LoginDTO loginDTO, TRequest apiRequest, CancellationToken ct) { try { // Map LoginDTO properties into PublishDTO publishDTO.UserId = loginDTO.UserId; publishDTO.RoleId = loginDTO.RoleId; publishDTO.MachineIP = loginDTO.MachineIP; publishDTO.OUId = loginDTO.WorkOUId; publishDTO.ModeOfWorking = loginDTO.ModeOfWorking; publishDTO.Geo = loginDTO.Geo; publishDTO.LoginEventLogId = loginDTO.LoginEventLogId; // Filter "Login" property if present in apiRequest var loginProp = apiRequest?.GetType().GetProperty("Login"); if (loginProp != null) { var filteredProps = apiRequest! .GetType() .GetProperties() .Where(p => p.Name != "Login") .ToDictionary(p => p.Name, p => p.GetValue(apiRequest)); publishDTO.Request = filteredProps; } else { publishDTO.Request = apiRequest; } publishDTO.Login = loginDTO; // Serialize PublishDTO to JSON string message = JsonConvert.SerializeObject(publishDTO); // Publish via Dapr pub/sub await DaprClient.PublishEventAsync("pubsub", publishDTO.PubSubTopicName, message, cancellationToken: ct); Logger.LogInformation("Published event successfully: {Message}", message); } catch (Exception ex) { Logger.LogWarning(ex, "Failed to publish event."); } } } }