using BMA.EHR.Application.Common.Interfaces; using BMA.EHR.Application.Responses; using BMA.EHR.Application.Responses.Messages; using BMA.EHR.Domain.Models.HR; using BMA.EHR.Domain.Models.Notifications; using BMA.EHR.Domain.Shared; using Microsoft.AspNetCore.Http; using Microsoft.EntityFrameworkCore; using Newtonsoft.Json; using System.Text.RegularExpressions; namespace BMA.EHR.Application.Repositories.MessageQueue { public class NotificationRepository : GenericRepository { #region " Fields " private readonly IApplicationDBContext _dbContext; private readonly IHttpContextAccessor _httpContextAccessor; #endregion #region " Constructor and Destuctor " public NotificationRepository(IApplicationDBContext dbContext, IHttpContextAccessor httpContextAccessor) : base(dbContext, httpContextAccessor) { _dbContext = dbContext; _httpContextAccessor = httpContextAccessor; } #endregion #region " Methods " public async Task> GetMyNotificationAsync() { try { var profile = await _dbContext.Set() .FirstOrDefaultAsync(p => p.KeycloakId == Guid.Parse(UserId!)); if (profile == null) { return new List(); // throw new Exception(GlobalMessages.DataNotFound); } var data = await _dbContext.Set() .Where(x => x.ReceiverUserId == profile.Id) .Where(x => x.IsOpen == false) .OrderByDescending(x => x.ReceiveDate) .Select(x => new NotificationResponse { Id = x.Id, Body = x.Body, ReceiverUserId = x.ReceiverUserId, IsOpen = x.IsOpen, Type = x.Type, ReceiveDate = x.ReceiveDate, OpenDate = x.OpenDate, Payload = x.Payload == "" ? null : JsonConvert.DeserializeObject(Regex.Unescape(x.Payload)) }) .Take(20) .ToListAsync(); return data; } catch { throw; } } public async Task DeleteMyNotificationAsync(Guid id) { try { var notification = await _dbContext.Set() .FirstOrDefaultAsync(p => p.Id == id); if (notification != null) { notification.IsOpen = true; // _dbContext.Set().Remove(notification); await _dbContext.SaveChangesAsync(); } } catch { throw; } } public async Task PushNotificationAsync(Guid ReceiverUserId, string Subject, string Body, string Payload = "", bool IsSendInbox = false, bool IsSendMail = false) { try { var profile = await _dbContext.Set().FirstOrDefaultAsync(x => x.Id == ReceiverUserId); if (profile != null) { _dbContext.Set().Add(new Notification { Body = Body, ReceiverUserId = ReceiverUserId, Type = "", Payload = Payload, CreatedUserId = UserId ?? "System Administrator", CreatedFullName = FullName ?? "", CreatedAt = DateTime.Now, LastUpdateFullName = FullName ?? "System Administrator", LastUpdateUserId = UserId ?? "", LastUpdatedAt = DateTime.Now, }); if (IsSendInbox == true) { _dbContext.Set().Add(new Inbox { Subject = Subject, Body = Body, ReceiverUserId = ReceiverUserId, Payload = Payload, CreatedUserId = UserId ?? "System Administrator", CreatedFullName = FullName ?? "", CreatedAt = DateTime.Now, LastUpdateFullName = FullName ?? "System Administrator", LastUpdateUserId = UserId ?? "", LastUpdatedAt = DateTime.Now, }); } if (IsSendMail == true) { // _context.Notifications.Add(new Notification // { // Body = req.Body, // ReceiverUserId = req.ReceiverUserId, // Type = "", // Payload = "", // CreatedUserId = UserId ?? "System Administrator", // CreatedFullName = FullName ?? "", // CreatedAt = DateTime.Now, // LastUpdateFullName = FullName ?? "System Administrator", // LastUpdateUserId = UserId ?? "", // LastUpdatedAt = DateTime.Now, // }); } await _dbContext.SaveChangesAsync(); } } catch { throw; } } #endregion } }