using BMA.EHR.Application.Common.Interfaces; using BMA.EHR.Domain.Models.HR; using BMA.EHR.Domain.Models.Notifications; using BMA.EHR.Domain.Shared; using Microsoft.AspNetCore.Http; using Microsoft.EntityFrameworkCore; 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) { throw new Exception(GlobalMessages.DataNotFound); } var data = await _dbContext.Set() .Where(x => x.ReceiverUserId == profile.Id) .OrderByDescending(x => x.ReceiveDate) .ToListAsync(); return data; } catch { throw; } } public async Task PushNotificationAsync(Guid ReceiverUserId, string Subject, string Body, bool IsSendNotification = true, bool IsSendInbox = false, bool IsSendMail = false) { try { var profile = await _dbContext.Set().FirstOrDefaultAsync(x => x.Id == ReceiverUserId); if (profile != null) { if (IsSendNotification == true) { _dbContext.Set().Add(new Notification { Body = Body, ReceiverUserId = ReceiverUserId, Type = "", Payload = "", CreatedUserId = FullName ?? "", CreatedFullName = UserId ?? "System Administrator", 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 = "", CreatedUserId = FullName ?? "", CreatedFullName = UserId ?? "System Administrator", 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 = FullName ?? "", // CreatedFullName = UserId ?? "System Administrator", // CreatedAt = DateTime.Now, // LastUpdateFullName = FullName ?? "System Administrator", // LastUpdateUserId = UserId ?? "", // LastUpdatedAt = DateTime.Now, // }); } await _dbContext.SaveChangesAsync(); } } catch { throw; } } #endregion } }