diff --git a/BMA.EHR.Application/Repositories/Leaves/LeaveRequests/LeaveRequestRepository.cs b/BMA.EHR.Application/Repositories/Leaves/LeaveRequests/LeaveRequestRepository.cs index 609047a1..25595c5f 100644 --- a/BMA.EHR.Application/Repositories/Leaves/LeaveRequests/LeaveRequestRepository.cs +++ b/BMA.EHR.Application/Repositories/Leaves/LeaveRequests/LeaveRequestRepository.cs @@ -633,11 +633,11 @@ namespace BMA.EHR.Application.Repositories.Leaves.LeaveRequests throw new Exception("ไม่สามารถอัพเดตการยกเลิกรายการลาไปยังระบบทะเบียนประวัติ"); //var _result = await _res.Content.ReadAsStringAsync(); } - } + } // TODO: remove วันลา - // Send Noti + // Send Noti หาเจ้าของใบลา var noti = new Notification { Body = $"การขอยกเลิกใบลาของคุณได้รับการอนุมัติ", @@ -646,6 +646,25 @@ namespace BMA.EHR.Application.Repositories.Leaves.LeaveRequests Payload = "", }; _appDbContext.Set().Add(noti); + + var commanders = rawData.Approvers + .Where(x => x.ApproveType!.ToUpper() == "COMMANDER") + .OrderBy(x => x.Seq) + .ToList(); + + foreach(var commander in commanders) + { + var noti1 = new Notification + { + Body = $"การขอยกเลิกใบลาของ {rawData.FirstName} {rawData.LastName} ได้รับการอนุมัติแล้ว", + ReceiverUserId = commander.ProfileId, + Type = "", + Payload = "", + }; + _appDbContext.Set().Add(noti1); + } + + await _appDbContext.SaveChangesAsync(); @@ -708,6 +727,25 @@ namespace BMA.EHR.Application.Repositories.Leaves.LeaveRequests Payload = "", }; _appDbContext.Set().Add(noti); + + var commanders = rawData.Approvers + .Where(x => x.ApproveType!.ToUpper() == "COMMANDER") + .OrderBy(x => x.Seq) + .ToList(); + + foreach (var commander in commanders) + { + var noti1 = new Notification + { + Body = $"การขอยกเลิกใบลาของ {rawData.FirstName} {rawData.LastName} ไม่ได้รับการอนุมัติ \r\nเนืองจาก {Reason}", + ReceiverUserId = commander.ProfileId, + Type = "", + Payload = "", + }; + _appDbContext.Set().Add(noti1); + } + + await _appDbContext.SaveChangesAsync(); } diff --git a/BMA.EHR.Leave/Controllers/LeaveRequestController.cs b/BMA.EHR.Leave/Controllers/LeaveRequestController.cs index e2c80240..a69dcf59 100644 --- a/BMA.EHR.Leave/Controllers/LeaveRequestController.cs +++ b/BMA.EHR.Leave/Controllers/LeaveRequestController.cs @@ -8,6 +8,7 @@ using BMA.EHR.Domain.Common; using BMA.EHR.Domain.Extensions; using BMA.EHR.Domain.Models.Leave.Commons; using BMA.EHR.Domain.Models.Leave.Requests; +using BMA.EHR.Domain.Models.Notifications; using BMA.EHR.Domain.Shared; using BMA.EHR.Infrastructure.Persistence; using BMA.EHR.Leave.Service.DTOs.LeaveRequest; @@ -35,6 +36,7 @@ namespace BMA.EHR.Leave.Service.Controllers #region " Fields " private readonly LeaveDbContext _context; + private readonly ApplicationDBContext _appDbContext; private readonly IHttpContextAccessor _httpContextAccessor; private readonly IWebHostEnvironment _hostingEnvironment; private readonly IConfiguration _configuration; @@ -54,6 +56,8 @@ namespace BMA.EHR.Leave.Service.Controllers private const string APPROVE_STEP_APPROVE = "st4"; private const string APPROVE_STEP_REJECT = "st5"; + private readonly string URL = string.Empty; + #endregion #region " Constuctor and Destructor " @@ -70,7 +74,8 @@ namespace BMA.EHR.Leave.Service.Controllers CommandRepository commandRepository, UserCalendarRepository userCalendarRepository, PermissionRepository permission, - LeaveBeginningRepository leaveBeginningRepository) + LeaveBeginningRepository leaveBeginningRepository, + ApplicationDBContext appDbContext) { _context = context; _httpContextAccessor = httpContextAccessor; @@ -85,6 +90,9 @@ namespace BMA.EHR.Leave.Service.Controllers _userCalendarRepository = userCalendarRepository; _permission = permission; _leaveBeginningRepository = leaveBeginningRepository; + _appDbContext = appDbContext; + + URL = (_configuration["VITE_URL_MGT"]).Replace("/api/v1", ""); } #endregion @@ -1726,6 +1734,27 @@ namespace BMA.EHR.Leave.Service.Controllers // save to database await _leaveRequestRepository.UpdateWithTrackingAsync(data); + + // TODO: Send notification to 1st Commander + var approvers = data.Approvers + .Where(x => x.ApproveType!.ToUpper() == "APPROVER") + .OrderBy(x => x.Seq) + .ToList(); + + foreach(var approver in approvers) + { + // Send Notification + var noti1 = new Notification + { + Body = $"การขอลาของคุณ {data.FirstName} {data.LastName} รอรับการอนุมัติจากคุณ", + ReceiverUserId = approver!.ProfileId, + Type = "", + Payload = $"{URL}/leave/detail/{id}", + }; + _appDbContext.Set().Add(noti1); + } + + return Success(); }