diff --git a/BMA.EHR.Application/Repositories/MessageQueue/NotificationRepository.cs b/BMA.EHR.Application/Repositories/MessageQueue/NotificationRepository.cs index 717069ce..633dcf2e 100644 --- a/BMA.EHR.Application/Repositories/MessageQueue/NotificationRepository.cs +++ b/BMA.EHR.Application/Repositories/MessageQueue/NotificationRepository.cs @@ -233,6 +233,54 @@ namespace BMA.EHR.Application.Repositories.MessageQueue throw; } } + + public async Task PushNotificationsAsync(Guid[] ReceiverUserIds, string Subject, string Body, string Payload = "", bool IsSendInbox = false, bool IsSendMail = false) + { + try + { + foreach (var ReceiverUserId in ReceiverUserIds) + { + _dbContext.Set().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().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; + } + } public async Task PushNotificationAsyncV2(string? ReceiverUserId, string Subject, string Body, string Payload = "", bool IsSendInbox = false, bool IsSendMail = false) { try diff --git a/BMA.EHR.Placement.Service/Controllers/NotifyController.cs b/BMA.EHR.Placement.Service/Controllers/NotifyController.cs index 01a40ae3..2fdd8609 100644 --- a/BMA.EHR.Placement.Service/Controllers/NotifyController.cs +++ b/BMA.EHR.Placement.Service/Controllers/NotifyController.cs @@ -62,10 +62,6 @@ namespace BMA.EHR.Placement.Service.Controllers [HttpPost()] public async Task> UpdatePropertyByUser([FromBody] NotiRequest req) { - // var profile = await _context.Profiles.FirstOrDefaultAsync(x => x.Id == req.ReceiverUserId); - // if (profile == null) - // return Error(GlobalMessages.DataNotFound); - await _repositoryNoti.PushNotificationAsync( Guid.Parse(req.ReceiverUserId), req.Subject, @@ -81,10 +77,6 @@ namespace BMA.EHR.Placement.Service.Controllers [HttpPost("keycloak")] public async Task> UpdatePropertyByUserKeycloak([FromBody] NotiRequest req) { - // var profile = await _context.Profiles.FirstOrDefaultAsync(x => x.KeycloakId == req.ReceiverUserId); - // if (profile == null) - // return Error(GlobalMessages.DataNotFound); - var apiUrl = $"{_configuration["API"]}/org/profile/keycloakid/position/" + req.ReceiverUserId; using (var client = new HttpClient()) { @@ -112,18 +104,6 @@ namespace BMA.EHR.Placement.Service.Controllers [HttpPost("profile")] public async Task> UpdatePropertyByUserProfile([FromBody] NotiRequest req) { - // var profile = await _context.Profiles.FirstOrDefaultAsync(x => x.KeycloakId == req.ReceiverUserId); - // if (profile == null) - // return Error(GlobalMessages.DataNotFound); - - // var apiUrl = $"{_configuration["API"]}/org/profile/profileid/position/" + req.ReceiverUserId; - // using (var client = new HttpClient()) - // { - // client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.Replace("Bearer ", "")); - // var _req = new HttpRequestMessage(HttpMethod.Get, apiUrl); - // var _res = await client.SendAsync(_req); - // var _result = await _res.Content.ReadAsStringAsync(); - // var org = JsonConvert.DeserializeObject(_result); if (req.ReceiverUserId != null) { await _repositoryNoti.PushNotificationAsync( @@ -136,7 +116,20 @@ namespace BMA.EHR.Placement.Service.Controllers ); } return Success(); - // } + } + + [HttpPost("profiles")] + public async Task> UpdatePropertyByUserProfiles([FromBody] NotisRequest req) + { + await _repositoryNoti.PushNotificationsAsync( + req.ReceiverUserIds.Select(x => Guid.Parse(x)).ToArray(), + req.Subject, + req.Body, + req.Payload, + req.IsSendInbox, + req.IsSendMail + ); + return Success(); } [HttpPut("{id:length(36)}")] @@ -170,11 +163,6 @@ namespace BMA.EHR.Placement.Service.Controllers } return Success(); } - // var profile = await _context.Profiles.FirstOrDefaultAsync(x => x.KeycloakId == Guid.Parse(inbox.CreatedUserId)); - // if (profile == null) - // return Error(GlobalMessages.DataNotFound); - - return Success(); } [HttpPost("cronjob")] diff --git a/BMA.EHR.Placement.Service/Controllers/PlacementController.cs b/BMA.EHR.Placement.Service/Controllers/PlacementController.cs index 2aab3c38..3bb2f8ce 100644 --- a/BMA.EHR.Placement.Service/Controllers/PlacementController.cs +++ b/BMA.EHR.Placement.Service/Controllers/PlacementController.cs @@ -144,6 +144,7 @@ namespace BMA.EHR.Placement.Service.Controllers { Id = x.Id, PersonalId = x.Id, + x.profileId, Avatar = x.ProfileImg == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : x.ProfileImg.Id, FullName = $"{x.Prefix}{x.Firstname} {x.Lastname}", Prefix = x.Prefix, @@ -203,6 +204,7 @@ namespace BMA.EHR.Placement.Service.Controllers p.Lastname, p.IdCard, p.CitizenId, + p.profileId, p.ExamNumber, p.posmasterId, p.root, @@ -282,6 +284,7 @@ namespace BMA.EHR.Placement.Service.Controllers { Id = x.Id, PersonalId = x.Id, + x.profileId, Avatar = x.ProfileImg == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : x.ProfileImg.Id, FullName = $"{x.Prefix}{x.Firstname} {x.Lastname}", Prefix = x.Prefix, @@ -341,6 +344,7 @@ namespace BMA.EHR.Placement.Service.Controllers p.Lastname, p.IdCard, p.CitizenId, + p.profileId, p.ExamNumber, p.posmasterId, p.root, diff --git a/BMA.EHR.Placement.Service/Controllers/PlacementOfficerController.cs b/BMA.EHR.Placement.Service/Controllers/PlacementOfficerController.cs index 95ea3a91..99268cd2 100644 --- a/BMA.EHR.Placement.Service/Controllers/PlacementOfficerController.cs +++ b/BMA.EHR.Placement.Service/Controllers/PlacementOfficerController.cs @@ -110,6 +110,7 @@ namespace BMA.EHR.Placement.Service.Controllers { p.Id, p.citizenId, + p.profileId, p.prefix, p.firstName, p.lastName, diff --git a/BMA.EHR.Placement.Service/Requests/NotisRequest.cs b/BMA.EHR.Placement.Service/Requests/NotisRequest.cs new file mode 100644 index 00000000..201bde0a --- /dev/null +++ b/BMA.EHR.Placement.Service/Requests/NotisRequest.cs @@ -0,0 +1,16 @@ +using BMA.EHR.Domain.Models.MetaData; +using Microsoft.EntityFrameworkCore; + +namespace BMA.EHR.Placement.Service.Requests +{ + public class NotisRequest + { + public string Subject { get; set; } + public string Body { get; set; } + public string Payload { get; set; } + public string[] ReceiverUserIds { get; set; } + public bool IsSendMail { get; set; } + public bool IsSendInbox { get; set; } + public bool IsSendNotification { get; set; } + } +}