147 lines
5.5 KiB
C#
147 lines
5.5 KiB
C#
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 Microsoft.Extensions.Configuration;
|
|
using Newtonsoft.Json;
|
|
using System.Net.Http.Headers;
|
|
using System.Text.RegularExpressions;
|
|
|
|
namespace BMA.EHR.Application.Repositories.MessageQueue
|
|
{
|
|
public class InboxRepository : GenericRepository<Guid, Inbox>
|
|
{
|
|
#region " Fields "
|
|
|
|
private readonly IApplicationDBContext _dbContext;
|
|
private readonly IHttpContextAccessor _httpContextAccessor;
|
|
private readonly IConfiguration _configuration;
|
|
|
|
#endregion
|
|
|
|
#region " Constructor and Destuctor "
|
|
|
|
public InboxRepository(IApplicationDBContext dbContext,
|
|
IHttpContextAccessor httpContextAccessor,
|
|
IConfiguration configuration) : base(dbContext, httpContextAccessor)
|
|
{
|
|
_dbContext = dbContext;
|
|
_httpContextAccessor = httpContextAccessor;
|
|
_configuration = configuration;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region " Methods "
|
|
|
|
public async Task<dynamic> GetMyInboxAsync(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<InboxResponse>();
|
|
// // throw new Exception(GlobalMessages.DataNotFound);
|
|
// }
|
|
|
|
var apiUrl = $"{_configuration["API"]}/org/profile/keycloak/position";
|
|
var profileId = "";
|
|
using (var client = new HttpClient())
|
|
{
|
|
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", AccessToken.Replace("Bearer ", ""));
|
|
client.DefaultRequestHeaders.Add("api-key", _configuration["API_KEY"]);
|
|
var _req = new HttpRequestMessage(HttpMethod.Get, apiUrl);
|
|
var _res = await client.SendAsync(_req);
|
|
var _result = await _res.Content.ReadAsStringAsync();
|
|
|
|
var org = JsonConvert.DeserializeObject<OrgRequest>(_result);
|
|
|
|
if (org == null || org.result == null)
|
|
return new List<InboxResponse>();
|
|
profileId = org.result.profileId == null ? "" : org.result.profileId;
|
|
}
|
|
if (profileId == null || profileId == "")
|
|
return new List<InboxResponse>();
|
|
|
|
var data_search = await _dbContext.Set<Inbox>()
|
|
.Where(x => x.ReceiverUserId == Guid.Parse(profileId))
|
|
.Where(x => x.DeleteDate == null)
|
|
.OrderByDescending(x => x.ReceiveDate)
|
|
.Select(x => new InboxResponse
|
|
{
|
|
Id = x.Id,
|
|
Subject = x.Subject,
|
|
Body = x.Body,
|
|
ReceiverUserId = x.ReceiverUserId,
|
|
IsOpen = x.IsOpen,
|
|
ReceiveDate = x.ReceiveDate,
|
|
OpenDate = x.OpenDate,
|
|
Payload = x.Payload == "" ? null : JsonConvert.DeserializeObject<CommandPayload>(Regex.Unescape(x.Payload))
|
|
})
|
|
.ToListAsync();
|
|
|
|
var data = data_search
|
|
.Skip((page - 1) * pageSize)
|
|
.Take(pageSize)
|
|
.ToList();
|
|
|
|
var _data = new { data, total = data_search.Count(), totalNoti = data_search.Where(x => x.IsOpen == false).Count() };
|
|
return _data;
|
|
}
|
|
catch
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task DeleteMyInboxAsync(Guid id)
|
|
{
|
|
try
|
|
{
|
|
var inbox = await _dbContext.Set<Inbox>()
|
|
.FirstOrDefaultAsync(p => p.Id == id);
|
|
|
|
if (inbox != null)
|
|
{
|
|
inbox.DeleteDate = DateTime.Now;
|
|
// _dbContext.Set<Inbox>().Remove(inbox);
|
|
await _dbContext.SaveChangesAsync();
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task GetByIdMyInboxAsync(Guid id)
|
|
{
|
|
try
|
|
{
|
|
var inbox = await _dbContext.Set<Inbox>()
|
|
.FirstOrDefaultAsync(p => p.Id == id);
|
|
|
|
if (inbox != null)
|
|
{
|
|
inbox.IsOpen = true;
|
|
inbox.OpenDate = DateTime.Now;
|
|
// _dbContext.Set<Inbox>().Remove(inbox);
|
|
await _dbContext.SaveChangesAsync();
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|