From 4f18a97d0b886859cf2ed2491fe902a8b9e9da39 Mon Sep 17 00:00:00 2001 From: Suphonchai Phoonsawat Date: Thu, 5 Feb 2026 11:57:19 +0700 Subject: [PATCH] Add GetOCStaffAsync method to UserProfileRepository and create GetOcStaff response models --- .../Repositories/UserProfileRepository.cs | 34 ++++++- .../Responses/Profiles/GetOcStaff.cs | 35 +++++++ BMA.EHR.Leave/Controllers/LeaveController.cs | 94 ++++++++++++++++++- 3 files changed, 158 insertions(+), 5 deletions(-) create mode 100644 BMA.EHR.Application/Responses/Profiles/GetOcStaff.cs diff --git a/BMA.EHR.Application/Repositories/UserProfileRepository.cs b/BMA.EHR.Application/Repositories/UserProfileRepository.cs index 4e0e986f..db63e056 100644 --- a/BMA.EHR.Application/Repositories/UserProfileRepository.cs +++ b/BMA.EHR.Application/Repositories/UserProfileRepository.cs @@ -186,6 +186,8 @@ namespace BMA.EHR.Application.Repositories } } + + public async Task GetProfileByKeycloakIdNewAsync(Guid keycloakId, string? accessToken,CancellationToken cancellationToken = default) { try @@ -256,6 +258,36 @@ namespace BMA.EHR.Application.Repositories } } + public async Task?> GetOCStaffAsync(Guid profileId, string? accessToken) + { + try + { + var apiPath = $"{_configuration["API"]}/org/dotnet/find-staff"; + var apiKey = _configuration["API_KEY"]; + var body = new + { + assignId = "SYS_LEAVE_LIST", + profileId = profileId + }; + + //var profiles = new List(); + + var apiResult = await PostExternalAPIAsync(apiPath, accessToken ?? "", body, apiKey); + if (apiResult != null) + { + var raw = JsonConvert.DeserializeObject(apiResult); + if (raw != null) + return raw.Result; + } + + return null; + } + catch + { + throw; + } + } + public async Task GetProfileLeaveReportByKeycloakIdAsync(Guid keycloakId, string? accessToken, string? report) { try @@ -268,7 +300,7 @@ namespace BMA.EHR.Application.Repositories report = report }; - var profiles = new List(); + //var profiles = new List(); var apiResult = await PostExternalAPIAsync(apiPath, accessToken, body, apiKey); if (apiResult != null) diff --git a/BMA.EHR.Application/Responses/Profiles/GetOcStaff.cs b/BMA.EHR.Application/Responses/Profiles/GetOcStaff.cs new file mode 100644 index 00000000..fa3ce936 --- /dev/null +++ b/BMA.EHR.Application/Responses/Profiles/GetOcStaff.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace BMA.EHR.Application.Responses.Profiles +{ + public class GetOcStaff + { + public Guid ProfileId { get; set; } + public Guid Keycloak { get; set; } + public string FullName { get; set; } = null!; + public Guid? RootId { get; set; } + public Guid? OrgChild1Id { get; set; } + public Guid? OrgChild2Id { get; set; } + public Guid? OrgChild3Id { get; set; } + public Guid? OrgChild4Id { get; set; } + public Guid? RootDnaId { get; set; } + public Guid? Child1DnaId { get; set; } + public Guid? Child2DnaId { get; set; } + public Guid? Child3DnaId { get; set; } + public Guid? Child4DnaId { get; set; } + + } + + public class GetOcStaffResultDto + { + public string Message { get; set; } = string.Empty; + + public int Status { get; set; } = -1; + + public List Result { get; set; } = new(); + + } +} \ No newline at end of file diff --git a/BMA.EHR.Leave/Controllers/LeaveController.cs b/BMA.EHR.Leave/Controllers/LeaveController.cs index 85b782cd..665b209c 100644 --- a/BMA.EHR.Leave/Controllers/LeaveController.cs +++ b/BMA.EHR.Leave/Controllers/LeaveController.cs @@ -6,6 +6,7 @@ using BMA.EHR.Application.Repositories.MessageQueue; using BMA.EHR.Application.Responses.Profiles; using BMA.EHR.Domain.Common; using BMA.EHR.Domain.Models.Leave.TimeAttendants; +using BMA.EHR.Domain.Models.Notifications; using BMA.EHR.Domain.Shared; using BMA.EHR.Infrastructure.Persistence; using BMA.EHR.Leave.Service.DTOs.AdditionalCheck; @@ -46,6 +47,7 @@ namespace BMA.EHR.Leave.Service.Controllers private readonly DutyTimeRepository _dutyTimeRepository; private readonly LeaveDbContext _context; + private readonly ApplicationDBContext _appDbContext; private readonly IHttpContextAccessor _httpContextAccessor; private readonly IWebHostEnvironment _hostingEnvironment; private readonly IConfiguration _configuration; @@ -94,10 +96,12 @@ namespace BMA.EHR.Leave.Service.Controllers PermissionRepository permission, NotificationRepository notificationRepository, CheckInJobStatusRepository checkInJobStatusRepository, - HttpClient httpClient) + HttpClient httpClient, + ApplicationDBContext appDbContext) { _dutyTimeRepository = dutyTimeRepository; _context = context; + _appDbContext = appDbContext; _httpContextAccessor = httpContextAccessor; _hostingEnvironment = hostingEnvironment; _configuration = configuration; @@ -928,16 +932,31 @@ namespace BMA.EHR.Leave.Service.Controllers if (profile == null) { await _checkInJobStatusRepository.UpdateToFailedAsync(taskId, "ไม่พบข้อมูลผู้ใช้"); + //var staffList = await _userProfileRepository.GetOCStaffAsync(profile) return Error(GlobalMessages.DataNotFound, StatusCodes.Status404NotFound); } + var currentDate = data.CurrentDate ?? DateTime.Now; + if (data.CheckInFileName == "no-file") { //throw new Exception(GlobalMessages.NoFileToUpload); await _checkInJobStatusRepository.UpdateToFailedAsync(taskId, GlobalMessages.NoFileToUpload); + + // send notification to user + var noti1 = new Notification + { + Body = $"ประมวลผลการลงเวลาวันที่ {currentDate.ToString("dd-MM-yyyy")} ไม่สำเร็จ \r\nเนื่องจาก {GlobalMessages.NoFileToUpload}", + ReceiverUserId = profile.Id, + Type = "", + Payload = "", + }; + _appDbContext.Set().Add(noti1); + await _appDbContext.SaveChangesAsync(); + return Error(GlobalMessages.NoFileToUpload, StatusCodes.Status400BadRequest); } - var currentDate = data.CurrentDate ?? DateTime.Now; + var check_status = data.CheckInId == null ? "check-in-picture" : "check-out-picture"; @@ -951,6 +970,19 @@ namespace BMA.EHR.Leave.Service.Controllers catch (Exception ex) { await _checkInJobStatusRepository.UpdateToFailedAsync(taskId, $"ไม่สามารถอัปโหลดรูปภาพได้: {ex.Message}"); + + // send notification to user + var noti1 = new Notification + { + Body = $"ประมวลผลการลงเวลาวันที่ {currentDate.ToString("dd-MM-yyyy")} ไม่สำเร็จ \r\nเนื่องจากไม่สามารถอัปโหลดรูปภาพได้ {ex.Message}", + ReceiverUserId = profile.Id, + Type = "", + Payload = "", + }; + _appDbContext.Set().Add(noti1); + await _appDbContext.SaveChangesAsync(); + + return Error($"ไม่สามารถอัปโหลดรูปภาพได้: {ex.Message}", StatusCodes.Status500InternalServerError); } @@ -960,6 +992,16 @@ namespace BMA.EHR.Leave.Service.Controllers if (defaultRound == null) { await _checkInJobStatusRepository.UpdateToFailedAsync(taskId, "ไม่พบรอบการลงเวลาทำงาน Default"); + // send notification to user + var noti1 = new Notification + { + Body = $"ประมวลผลการลงเวลาวันที่ {currentDate.ToString("dd-MM-yyyy")} ไม่สำเร็จ \r\nเนื่องจากไม่พบรอบการลงเวลาทำงาน Default", + ReceiverUserId = profile.Id, + Type = "", + Payload = "", + }; + _appDbContext.Set().Add(noti1); + await _appDbContext.SaveChangesAsync(); return Error("ไม่พบรอบการลงเวลาทำงาน Default", StatusCodes.Status404NotFound); } @@ -979,6 +1021,18 @@ namespace BMA.EHR.Leave.Service.Controllers if (currentCheckIn != null) { await _checkInJobStatusRepository.UpdateToFailedAsync(taskId, "ไม่สามารถลงเวลาได้ เนื่องจากมีการลงเวลาในวันนี้แล้ว"); + + // send notification to user + var noti1 = new Notification + { + Body = $"ประมวลผลการลงเวลาวันที่ {currentDate.ToString("dd-MM-yyyy")} ไม่สำเร็จ \r\nเนื่องจากมีการลงเวลาในวันนี้แล้ว", + ReceiverUserId = profile.Id, + Type = "", + Payload = "", + }; + _appDbContext.Set().Add(noti1); + await _appDbContext.SaveChangesAsync(); + return Error(new Exception("ไม่สามารถลงเวลาได้ เนื่องจากมีการลงเวลาในวันนี้แล้ว!"), StatusCodes.Status400BadRequest); } @@ -1117,6 +1171,18 @@ namespace BMA.EHR.Leave.Service.Controllers if (checkout == null) { await _checkInJobStatusRepository.UpdateToFailedAsync(taskId, "ไม่พบข้อมูลการลงเวลาทำงาน"); + + // send notification to user + var noti1 = new Notification + { + Body = $"ประมวลผลการลงเวลาวันที่ {currentDate.ToString("dd-MM-yyyy")} ไม่สำเร็จ \r\nเนื่องจากไม่พบข้อมูลการลงเวลาทำงาน", + ReceiverUserId = profile.Id, + Type = "", + Payload = "", + }; + _appDbContext.Set().Add(noti1); + await _appDbContext.SaveChangesAsync(); + return Error(new Exception(GlobalMessages.DataNotFound), StatusCodes.Status404NotFound); } @@ -1125,6 +1191,18 @@ namespace BMA.EHR.Leave.Service.Controllers if (currentCheckInProcess == null) { await _checkInJobStatusRepository.UpdateToFailedAsync(taskId, "ไม่พบข้อมูลการประมวลผลเวลาทำงาน (CheckIn)"); + + // send notification to user + var noti1 = new Notification + { + Body = $"ประมวลผลการลงเวลาวันที่ {currentDate.ToString("dd-MM-yyyy")} ไม่สำเร็จ \r\nเนื่องจากไม่พบข้อมูลการประมวลผลเวลาทำงาน (CheckIn)", + ReceiverUserId = profile.Id, + Type = "", + Payload = "", + }; + _appDbContext.Set().Add(noti1); + await _appDbContext.SaveChangesAsync(); + return Error(new Exception(GlobalMessages.DataNotFound), StatusCodes.Status404NotFound); } @@ -1275,11 +1353,19 @@ namespace BMA.EHR.Leave.Service.Controllers else { await _checkInJobStatusRepository.UpdateToFailedAsync(taskId, "ไม่พบข้อมูลการประมวลผลเวลาทำงาน"); + // send notification to user + var noti1 = new Notification + { + Body = $"ประมวลผลการลงเวลาวันที่ {currentDate.ToString("dd-MM-yyyy")} ไม่สำเร็จ \r\nเนื่องจากไม่พบข้อมูลการประมวลผลเวลาทำงาน", + ReceiverUserId = profile.Id, + Type = "", + Payload = "", + }; + _appDbContext.Set().Add(noti1); + await _appDbContext.SaveChangesAsync(); return Error(new Exception(GlobalMessages.DataNotFound), StatusCodes.Status404NotFound); } - } - // อัปเดตสถานะเป็น COMPLETED if (taskId != Guid.Empty) {