using Dapr; using DocumentFormat.OpenXml.Office.CoverPageProps; using FrameworkBLL.EventLogSubscriber; using GB5Shared.PushNotification; using GB5Shared.DTO.PushNotification; using GB5Shared.DTO.Framework.Login; using GB5Shared.DTO.PubSub; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using Microsoft.VisualBasic; using Newtonsoft.Json; namespace FrameworkSL.Controllers.Subscriber { [ApiController] [Route("PushNotification/[controller]")] public class PushNotificationSubscriber : ControllerBase { private readonly IEventLogSubscriberBLL _eventLogSubscriberBLL; private readonly IPushNotificationBLL _pushNotificationBLL; private readonly ILogger _logger; public PushNotificationSubscriber(IEventLogSubscriberBLL eventLogSubscriberBLL, IPushNotificationBLL pushNotificationBLL, ILogger logger) { _eventLogSubscriberBLL = eventLogSubscriberBLL; _pushNotificationBLL = pushNotificationBLL; _logger = logger; } [Topic("pubsub", "GBUSER")] [HttpPost("Notification")] public async Task PushNotification([FromBody] string jsonMessage) { _logger.LogDebug("PushNotificationSubscriber received message {Message}", jsonMessage); if (string.IsNullOrEmpty(jsonMessage)) return BadRequest("Received empty message."); try { LoginDTO LoginDTO = JsonConvert.DeserializeObject(jsonMessage)!; PushNotificationDTO PushNotificationDTO = new(); string result1 = await _pushNotificationBLL.SubscribeUserToTopic(LoginDTO.PushNotificationToken, $"GBUSER-{LoginDTO.ConnectionDatabaseName}"); PushNotificationDTO.NotificationTopic = $"GBUSER-{LoginDTO.ConnectionDatabaseName}"; PushNotificationDTO.NotificationTitle = $"{LoginDTO.UserCode} Logged In {LoginDTO.ConnectionDatabaseName}"; PushNotificationDTO.NotificationBody = $"User {LoginDTO.UserCode} successfully logged in at {DateAndTime.Now}."; string result2 = await _pushNotificationBLL.SendNotificationToTopic(PushNotificationDTO); return Ok(new { Message = result2 }); } catch (Exception ex) { return StatusCode(500, $"Error while processing notification: {ex.Message}"); } } } }