diff --git a/BMA.EHR.Application/Repositories/MessageQueue/NotificationRepository.cs b/BMA.EHR.Application/Repositories/MessageQueue/NotificationRepository.cs index 73f292af..091bc1c3 100644 --- a/BMA.EHR.Application/Repositories/MessageQueue/NotificationRepository.cs +++ b/BMA.EHR.Application/Repositories/MessageQueue/NotificationRepository.cs @@ -54,32 +54,39 @@ namespace BMA.EHR.Application.Repositories.MessageQueue .Where(x => x.ReceiverUserId == profile.Id) .Where(x => x.DeleteDate == null) .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)) - }) .ToListAsync(); - + var totalNoti = data_search.Where(x => x.IsOpen == false).Count(); var data = data_search .Skip((page - 1) * pageSize) .Take(pageSize) - .ToList(); + .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)) + }).ToList(); - var data_opens = data.Where(x => x.IsOpen == false).ToList(); + var data_opens = await _dbContext.Set() + .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 = data_search.Where(x => x.IsOpen == false).Count() }; + var _data = new { data, total = data_search.Count(), totalNoti = totalNoti }; return _data; } catch @@ -88,6 +95,33 @@ namespace BMA.EHR.Application.Repositories.MessageQueue } } + public async Task GetMyNotificationAsyncNoread() + { + try + { + var profile = await _dbContext.Set() + .FirstOrDefaultAsync(p => p.KeycloakId == Guid.Parse(UserId!)); + + if (profile == null) + { + return 0; + } + + var data_search = await _dbContext.Set() + .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 diff --git a/BMA.EHR.Command.Service/Controllers/MessageController.cs b/BMA.EHR.Command.Service/Controllers/MessageController.cs index cde70c46..a4bbc006 100644 --- a/BMA.EHR.Command.Service/Controllers/MessageController.cs +++ b/BMA.EHR.Command.Service/Controllers/MessageController.cs @@ -134,6 +134,31 @@ namespace BMA.EHR.Command.Service.Controllers } } + /// + /// แสดงข้อมูล Notification ของ user ที่ Login ที่ยังไม่ได้อ่าน + /// + /// + /// เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ + /// ไม่ได้ Login เข้าระบบ + /// เมื่อเกิดข้อผิดพลาดในการทำงาน + [HttpGet("my-notifications/noread")] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status401Unauthorized)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] + public async Task> GetMyNotificationAsyncNoread() + { + try + { + var noti = await _notificationRepository.GetMyNotificationAsyncNoread(); + + return Success(noti); + } + catch + { + throw; + } + } + /// /// ลบข้อมูล Notification ของ user ที่ Login ///