diff --git a/BMA.EHR.Placement.Service/Controllers/NotifyController.cs b/BMA.EHR.Placement.Service/Controllers/NotifyController.cs index c99b934b..7413ef53 100644 --- a/BMA.EHR.Placement.Service/Controllers/NotifyController.cs +++ b/BMA.EHR.Placement.Service/Controllers/NotifyController.cs @@ -12,7 +12,9 @@ using BMA.EHR.Placement.Service.Requests; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; +using Newtonsoft.Json; using Swashbuckle.AspNetCore.Annotations; +using System.Net.Http.Headers; using System.Security.Claims; using System.Security.Cryptography; @@ -31,18 +33,21 @@ namespace BMA.EHR.Placement.Service.Controllers private readonly MinIOService _documentService; private readonly IHttpContextAccessor _httpContextAccessor; private readonly NotificationRepository _repositoryNoti; + private readonly IConfiguration _configuration; public NotifyController(PlacementRepository repository, ApplicationDBContext context, MinIOService documentService, NotificationRepository repositoryNoti, - IHttpContextAccessor httpContextAccessor) + IHttpContextAccessor httpContextAccessor, + IConfiguration configuration) { _repository = repository; _repositoryNoti = repositoryNoti; _context = context; _documentService = documentService; _httpContextAccessor = httpContextAccessor; + _configuration = configuration; } #region " Properties " @@ -50,6 +55,7 @@ namespace BMA.EHR.Placement.Service.Controllers private string? UserId => _httpContextAccessor?.HttpContext?.User?.FindFirst(ClaimTypes.NameIdentifier)?.Value; private string? FullName => _httpContextAccessor?.HttpContext?.User?.FindFirst("name")?.Value; + private string? token => _httpContextAccessor?.HttpContext?.Request.Headers["Authorization"]; #endregion @@ -79,14 +85,27 @@ namespace BMA.EHR.Placement.Service.Controllers // if (profile == null) // return Error(GlobalMessages.DataNotFound); - await _repositoryNoti.PushNotificationAsync( - req.ReceiverUserId, - req.Subject, - req.Body, - req.Payload, - req.IsSendInbox, - req.IsSendMail - ); + var apiUrl = $"{_configuration["API"]}org/profile/keycloak/position"; + 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 (org != null && org.result != null) + { + await _repositoryNoti.PushNotificationAsync( + Guid.Parse(org.result.profileId), + req.Subject, + req.Body, + req.Payload, + req.IsSendInbox, + req.IsSendMail + ); + } + return Success(); + } return Success(); } diff --git a/BMA.EHR.Retirement.Service/Controllers/RetirementDeceasedController.cs b/BMA.EHR.Retirement.Service/Controllers/RetirementDeceasedController.cs index 7e412955..180a93ba 100644 --- a/BMA.EHR.Retirement.Service/Controllers/RetirementDeceasedController.cs +++ b/BMA.EHR.Retirement.Service/Controllers/RetirementDeceasedController.cs @@ -669,7 +669,7 @@ namespace BMA.EHR.Retirement.Service.Controllers var profile = req.Persons.FirstOrDefault(x => x.ProfileId == item.profileId); if (profile != null) { - await _repositoryNoti.PushNotificationAsyncV2( + await _repositoryNoti.PushNotificationAsync( item.profileId, $"หนังสือเวียนถึงแก่กรรมของ {item.RetirementDeceased.prefix}{item.RetirementDeceased.firstName} {item.RetirementDeceased.lastName}", $"แจ้งข่าวการถึงแก่กรรมของ {item.RetirementDeceased.prefix}{item.RetirementDeceased.firstName} {item.RetirementDeceased.lastName}", @@ -683,7 +683,7 @@ namespace BMA.EHR.Retirement.Service.Controllers } else { - await _repositoryNoti.PushNotificationAsyncV2( + await _repositoryNoti.PushNotificationAsync( item.profileId, $"หนังสือเวียนถึงแก่กรรมของ {item.RetirementDeceased.prefix}{item.RetirementDeceased.firstName} {item.RetirementDeceased.lastName}", $"แจ้งข่าวการถึงแก่กรรมของ {item.RetirementDeceased.prefix}{item.RetirementDeceased.firstName} {item.RetirementDeceased.lastName}", diff --git a/BMA.EHR.Retirement.Service/Controllers/RetirementResignController.cs b/BMA.EHR.Retirement.Service/Controllers/RetirementResignController.cs index c195f125..57b17072 100644 --- a/BMA.EHR.Retirement.Service/Controllers/RetirementResignController.cs +++ b/BMA.EHR.Retirement.Service/Controllers/RetirementResignController.cs @@ -491,7 +491,7 @@ namespace BMA.EHR.Retirement.Service.Controllers } } await _repositoryNoti.PushNotificationAsync( - Guid.Parse("08dbc953-6268-4e2c-80a3-aca65eedc6d0"), + Guid.Parse("aec26ac3-417c-4cf9-9cbe-874939f99ecc"), $"{retirementResign.prefix}{retirementResign.firstName} {retirementResign.lastName} ได้ทำการยื่นขอลาออก", $"{retirementResign.prefix}{retirementResign.firstName} {retirementResign.lastName} ได้ทำการยื่นขอลาออก", "", @@ -742,14 +742,14 @@ namespace BMA.EHR.Retirement.Service.Controllers updated.LastUpdateUserId = UserId ?? ""; updated.LastUpdatedAt = DateTime.Now; await _repositoryNoti.PushNotificationAsync( - Guid.Parse("08dbca3a-8b6a-4a4e-8b23-1f62e4f30ef6"), + Guid.Parse("08dc432c-2bc5-4b81-8089-9c057c51192c"), $"คำขอลาออกขอ {updated.prefix}{updated.firstName} {updated.lastName} ได้รับการอนุมัติจากการเจ้าหน้าที่", $"คำขอลาออกขอ {updated.prefix}{updated.firstName} {updated.lastName} ได้รับการอนุมัติจากการเจ้าหน้าที่", "", true ); await _repositoryNoti.PushNotificationAsync( - Guid.Parse("08dbc953-61ac-47eb-82d7-0e72df7669b5"), + updated.profileId, $"คำขอลาออกขอ {updated.prefix}{updated.firstName} {updated.lastName} ได้รับการอนุมัติจากการเจ้าหน้าที่", $"คำขอลาออกขอ {updated.prefix}{updated.firstName} {updated.lastName} ได้รับการอนุมัติจากการเจ้าหน้าที่", "", @@ -786,14 +786,14 @@ namespace BMA.EHR.Retirement.Service.Controllers updated.LastUpdateUserId = UserId ?? ""; updated.LastUpdatedAt = DateTime.Now; await _repositoryNoti.PushNotificationAsync( - Guid.Parse("08dbca3a-8b6a-4a4e-8b23-1f62e4f30ef6"), + Guid.Parse("08dc432c-2bc5-4b81-8089-9c057c51192c"), $"คำขอลาออกขอ {updated.prefix}{updated.firstName} {updated.lastName} ถูกยับยั้งจากการเจ้าหน้าที่", $"คำขอลาออกขอ {updated.prefix}{updated.firstName} {updated.lastName} ถูกยับยั้งจากการเจ้าหน้าที่", "", true ); await _repositoryNoti.PushNotificationAsync( - Guid.Parse("08dbc953-61ac-47eb-82d7-0e72df7669b5"), + updated.profileId, $"คำขอลาออกขอ {updated.prefix}{updated.firstName} {updated.lastName} ถูกยับยั้งจากการเจ้าหน้าที่", $"คำขอลาออกขอ {updated.prefix}{updated.firstName} {updated.lastName} ถูกยับยั้งจากการเจ้าหน้าที่", "", @@ -828,14 +828,14 @@ namespace BMA.EHR.Retirement.Service.Controllers updated.LastUpdateUserId = UserId ?? ""; updated.LastUpdatedAt = DateTime.Now; await _repositoryNoti.PushNotificationAsync( - Guid.Parse("08dbca3a-8b6a-4a4e-8b23-1f62e4f30ef6"), + Guid.Parse("08dc4307-0adc-4bcd-8213-5479bb010236"), $"คำขอลาออกขอ {updated.prefix}{updated.firstName} {updated.lastName} ได้รับการอนุมัติจากผู้บังคับบัญชา", $"คำขอลาออกขอ {updated.prefix}{updated.firstName} {updated.lastName} ได้รับการอนุมัติจากผู้บังคับบัญชา", "", true ); await _repositoryNoti.PushNotificationAsync( - Guid.Parse("08dbc953-61ac-47eb-82d7-0e72df7669b5"), + updated.profileId, $"คำขอลาออกขอ {updated.prefix}{updated.firstName} {updated.lastName} ได้รับการอนุมัติจากผู้บังคับบัญชา", $"คำขอลาออกขอ {updated.prefix}{updated.firstName} {updated.lastName} ได้รับการอนุมัติจากผู้บังคับบัญชา", "", @@ -872,14 +872,14 @@ namespace BMA.EHR.Retirement.Service.Controllers updated.LastUpdateUserId = UserId ?? ""; updated.LastUpdatedAt = DateTime.Now; await _repositoryNoti.PushNotificationAsync( - Guid.Parse("08dbca3a-8b6a-4a4e-8b23-1f62e4f30ef6"), + Guid.Parse("08dc4307-0adc-4bcd-8213-5479bb010236"), $"คำขอลาออกขอ {updated.prefix}{updated.firstName} {updated.lastName} ถูกยับยั้งจากผู้บังคับบัญชา", $"คำขอลาออกขอ {updated.prefix}{updated.firstName} {updated.lastName} ถูกยับยั้งจากผู้บังคับบัญชา", "", true ); await _repositoryNoti.PushNotificationAsync( - Guid.Parse("08dbc953-61ac-47eb-82d7-0e72df7669b5"), + updated.profileId, $"คำขอลาออกขอ {updated.prefix}{updated.firstName} {updated.lastName} ถูกยับยั้งจากผู้บังคับบัญชา", $"คำขอลาออกขอ {updated.prefix}{updated.firstName} {updated.lastName} ถูกยับยั้งจากผู้บังคับบัญชา", "", @@ -912,8 +912,15 @@ namespace BMA.EHR.Retirement.Service.Controllers updated.LastUpdateFullName = FullName ?? "System Administrator"; updated.LastUpdateUserId = UserId ?? ""; updated.LastUpdatedAt = DateTime.Now; - await _repositoryNoti.PushNotificationAsyncV2( - updated.CreatedUserId, + await _repositoryNoti.PushNotificationAsync( + Guid.Parse("aec26ac3-417c-4cf9-9cbe-874939f99ecc"), + $"คำขอลาออกขอ {updated.prefix}{updated.firstName} {updated.lastName} ได้รับการอนุมัติจากผู้มีอำนาจ", + $"คำขอลาออกขอ {updated.prefix}{updated.firstName} {updated.lastName} ได้รับการอนุมัติจากผู้มีอำนาจ", + "", + true + ); + await _repositoryNoti.PushNotificationAsync( + updated.profileId, $"คำขอลาออกขอ {updated.prefix}{updated.firstName} {updated.lastName} ได้รับการอนุมัติจากผู้มีอำนาจ", $"คำขอลาออกขอ {updated.prefix}{updated.firstName} {updated.lastName} ได้รับการอนุมัติจากผู้มีอำนาจ", "", @@ -949,8 +956,15 @@ namespace BMA.EHR.Retirement.Service.Controllers updated.LastUpdateFullName = FullName ?? "System Administrator"; updated.LastUpdateUserId = UserId ?? ""; updated.LastUpdatedAt = DateTime.Now; - await _repositoryNoti.PushNotificationAsyncV2( - updated.CreatedUserId, + await _repositoryNoti.PushNotificationAsync( + Guid.Parse("aec26ac3-417c-4cf9-9cbe-874939f99ecc"), + $"คำขอลาออกขอ {updated.prefix}{updated.firstName} {updated.lastName} ได้รับการอนุมัติจากผู้มีอำนาจ", + $"คำขอลาออกขอ {updated.prefix}{updated.firstName} {updated.lastName} ได้รับการอนุมัติจากผู้มีอำนาจ", + "", + true + ); + await _repositoryNoti.PushNotificationAsync( + updated.profileId, $"คำขอลาออกขอ {updated.prefix}{updated.firstName} {updated.lastName} ถูกยับยั้งจากผู้มีอำนาจ", $"คำขอลาออกขอ {updated.prefix}{updated.firstName} {updated.lastName} ถูกยับยั้งจากผู้มีอำนาจ", "", @@ -1331,8 +1345,8 @@ namespace BMA.EHR.Retirement.Service.Controllers uppdated.LastUpdateFullName = FullName ?? "System Administrator"; uppdated.LastUpdateUserId = UserId ?? ""; uppdated.LastUpdatedAt = DateTime.Now; - await _repositoryNoti.PushNotificationAsyncV2( - uppdated.RetirementResign.CreatedUserId, + await _repositoryNoti.PushNotificationAsync( + uppdated.RetirementResign.profileId, $"การนัดสัมภาษณ์เหตุผลการลาออก {req.AppointDate.ToThaiFullDate()}", $"การนัดสัมภาษณ์เหตุผลการลาออก {req.AppointDate.ToThaiFullDate()}", "",