hrms-api-backend/BMA.EHR.Application/Repositories/MessageQueue/NotificationRepository.cs

196 lines
7.7 KiB
C#

using BMA.EHR.Application.Common.Interfaces;
using BMA.EHR.Application.Messaging;
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<Guid, Notification>
{
#region " Fields "
private readonly IApplicationDBContext _dbContext;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly EmailSenderService _emailSenderService;
#endregion
#region " Constructor and Destuctor "
public NotificationRepository(IApplicationDBContext dbContext,
EmailSenderService emailSenderService,
IHttpContextAccessor httpContextAccessor) : base(dbContext, httpContextAccessor)
{
_dbContext = dbContext;
_httpContextAccessor = httpContextAccessor;
_emailSenderService = emailSenderService;
}
#endregion
#region " Methods "
public async Task<dynamic> GetMyNotificationAsync(int page = 1, int pageSize = 25)
{
try
{
var profile = await _dbContext.Set<Profile>()
.FirstOrDefaultAsync(p => p.KeycloakId == Guid.Parse(UserId!));
if (profile == null)
{
return new List<NotificationResponse>();
// throw new Exception(GlobalMessages.DataNotFound);
}
var data_search = await _dbContext.Set<Notification>()
.Where(x => x.ReceiverUserId == profile.Id)
.Where(x => x.DeleteDate == null)
.OrderByDescending(x => x.ReceiveDate)
.ToListAsync();
var totalNoti = data_search.Where(x => x.IsOpen == false).Count();
var data = data_search
.Skip((page - 1) * pageSize)
.Take(pageSize)
.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<CommandPayload>(Regex.Unescape(x.Payload))
}).ToList();
var data_opens = await _dbContext.Set<Notification>()
.Where(x => x.ReceiverUserId == profile.Id)
.Where(x => x.DeleteDate == null)
.OrderByDescending(x => x.ReceiveDate)
.Skip((page - 1) * pageSize)
.Take(pageSize)
.Where(x => x.IsOpen == false)
.ToListAsync();
foreach (var data_open in data_opens)
{
data_open.IsOpen = true;
data_open.OpenDate = DateTime.Now;
}
await _dbContext.SaveChangesAsync();
var _data = new { data, total = data_search.Count(), totalNoti = totalNoti };
return _data;
}
catch
{
throw;
}
}
public async Task<dynamic> GetMyNotificationAsyncNoread()
{
try
{
var profile = await _dbContext.Set<Profile>()
.FirstOrDefaultAsync(p => p.KeycloakId == Guid.Parse(UserId!));
if (profile == null)
{
return 0;
}
var data_search = await _dbContext.Set<Notification>()
.Where(x => x.ReceiverUserId == profile.Id)
.Where(x => x.DeleteDate == null)
.Where(x => x.IsOpen == false)
.OrderByDescending(x => x.ReceiveDate)
.ToListAsync();
return data_search.Count();
}
catch
{
throw;
}
}
public async Task DeleteMyNotificationAsync(Guid id)
{
try
{
var notification = await _dbContext.Set<Notification>()
.FirstOrDefaultAsync(p => p.Id == id);
if (notification != null)
{
notification.DeleteDate = DateTime.Now;
// _dbContext.Set<Notification>().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<Profile>().FirstOrDefaultAsync(x => x.Id == ReceiverUserId);
if (profile != null)
{
_dbContext.Set<Notification>().Add(new Notification
{
Body = Body,
ReceiverUserId = ReceiverUserId,
Type = "",
Payload = Payload,
CreatedFullName = FullName ?? "System Administrator",
CreatedUserId = UserId ?? "",
CreatedAt = DateTime.Now,
LastUpdateFullName = FullName ?? "System Administrator",
LastUpdateUserId = UserId ?? "",
LastUpdatedAt = DateTime.Now,
});
if (IsSendInbox == true)
{
_dbContext.Set<Inbox>().Add(new Inbox
{
Subject = Subject,
Body = Body,
ReceiverUserId = ReceiverUserId,
Payload = Payload,
CreatedFullName = FullName ?? "System Administrator",
CreatedUserId = UserId ?? "",
CreatedAt = DateTime.Now,
LastUpdateFullName = FullName ?? "System Administrator",
LastUpdateUserId = UserId ?? "",
LastUpdatedAt = DateTime.Now,
});
}
if (IsSendMail == true)
{
_emailSenderService.SendMail(Subject, Body, "kittapath@frappet.com");
}
await _dbContext.SaveChangesAsync();
}
}
catch
{
throw;
}
}
#endregion
}
}