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) { diff --git a/BMA.EHR.Leave/Controllers/LeaveReportController.cs b/BMA.EHR.Leave/Controllers/LeaveReportController.cs index 2a113d94..4e5a0961 100644 --- a/BMA.EHR.Leave/Controllers/LeaveReportController.cs +++ b/BMA.EHR.Leave/Controllers/LeaveReportController.cs @@ -1389,57 +1389,57 @@ namespace BMA.EHR.Leave.Service.Controllers var sickDaySum = beginningData.FirstOrDefault(x => x.ProfileId == p.Id && x.LeaveType.Code == "LV-001"); var sickDay = leaveDays.FirstOrDefault(x => x.KeycloakUserId == keycloakUserId && x.LeaveTypeCode == "LV-001"); var sickDayCount = sickDaySum != null ? sickDaySum.LeaveDaysUsed : 0; - var sickCount = sickDay != null ? sickDay.CountLeaveDay : 0; + var sickCount = sickDaySum != null ? sickDaySum.LeaveCount : 0; var personalDaySum = beginningData.FirstOrDefault(x => x.ProfileId == p.Id && x.LeaveType.Code == "LV-002"); var personalDay = leaveDays.FirstOrDefault(x => x.KeycloakUserId == keycloakUserId && x.LeaveTypeCode == "LV-002"); var personalDayCount = personalDaySum != null ? personalDaySum.LeaveDaysUsed : 0; - var personalCount = personalDay != null ? personalDay.CountLeaveDay : 0; + var personalCount = personalDaySum != null ? personalDaySum.LeaveCount : 0; var maternityDaySum = beginningData.FirstOrDefault(x => x.ProfileId == p.Id && x.LeaveType.Code == "LV-003"); var maternityDay = leaveDays.FirstOrDefault(x => x.KeycloakUserId == keycloakUserId && x.LeaveTypeCode == "LV-003"); var maternityDayCount = maternityDaySum != null ? maternityDaySum.LeaveDaysUsed : 0; - var maternityCount = maternityDay != null ? maternityDay.CountLeaveDay : 0; + var maternityCount = maternityDaySum != null ? maternityDaySum.LeaveCount : 0; var wifeDaySum = beginningData.FirstOrDefault(x => x.ProfileId == p.Id && x.LeaveType.Code == "LV-004"); var wifeDay = leaveDays.FirstOrDefault(x => x.KeycloakUserId == keycloakUserId && x.LeaveTypeCode == "LV-004"); var wifeDayCount = wifeDaySum != null ? wifeDaySum.LeaveDaysUsed : 0; - var wifeCount = wifeDay != null ? wifeDay.CountLeaveDay : 0; + var wifeCount = wifeDaySum != null ? wifeDaySum.LeaveCount : 0; var restDaySum = beginningData.FirstOrDefault(x => x.ProfileId == p.Id && x.LeaveType.Code == "LV-005"); var restDay = leaveDays.FirstOrDefault(x => x.KeycloakUserId == keycloakUserId && x.LeaveTypeCode == "LV-005"); var restDayCount = restDaySum != null ? restDaySum.LeaveDaysUsed : 0; - var restCount = restDay != null ? restDay.CountLeaveDay : 0; + var restCount = restDaySum != null ? restDaySum.LeaveCount : 0; var ordainDaySum = beginningData.FirstOrDefault(x => x.ProfileId == p.Id && x.LeaveType.Code == "LV-006"); var ordainDay = leaveDays.FirstOrDefault(x => x.KeycloakUserId == keycloakUserId && x.LeaveTypeCode == "LV-006"); var ordainDayCount = ordainDaySum != null ? ordainDaySum.LeaveDaysUsed : 0; - var ordainCount = ordainDay != null ? ordainDay.CountLeaveDay : 0; + var ordainCount = ordainDaySum != null ? ordainDaySum.LeaveCount : 0; var absentDaySum = beginningData.FirstOrDefault(x => x.ProfileId == p.Id && x.LeaveType.Code == "LV-007"); var absentDay = leaveDays.FirstOrDefault(x => x.KeycloakUserId == keycloakUserId && x.LeaveTypeCode == "LV-007"); var absentDayCount = absentDaySum != null ? absentDaySum.LeaveDaysUsed : 0; - var absentCount = absentDay != null ? absentDay.CountLeaveDay : 0; + var absentCount = absentDaySum != null ? absentDaySum.LeaveCount : 0; var studyDaySum = beginningData.FirstOrDefault(x => x.ProfileId == p.Id && x.LeaveType.Code == "LV-008"); var studyDay = leaveDays.FirstOrDefault(x => x.KeycloakUserId == keycloakUserId && x.LeaveTypeCode == "LV-008"); var studyDayCount = studyDaySum != null ? studyDaySum.LeaveDaysUsed : 0; - var studyCount = studyDay != null ? studyDay.CountLeaveDay : 0; + var studyCount = studyDaySum != null ? studyDaySum.LeaveCount : 0; var agencyDaySum = beginningData.FirstOrDefault(x => x.ProfileId == p.Id && x.LeaveType.Code == "LV-009"); var agencyDay = leaveDays.FirstOrDefault(x => x.KeycloakUserId == keycloakUserId && x.LeaveTypeCode == "LV-009"); var agencyDayCount = agencyDaySum != null ? agencyDaySum.LeaveDaysUsed : 0; - var agencyCount = agencyDay != null ? agencyDay.CountLeaveDay : 0; + var agencyCount = agencyDaySum != null ? agencyDaySum.LeaveCount : 0; var coupleDaySum = beginningData.FirstOrDefault(x => x.ProfileId == p.Id && x.LeaveType.Code == "LV-010"); var coupleDay = leaveDays.FirstOrDefault(x => x.KeycloakUserId == keycloakUserId && x.LeaveTypeCode == "LV-010"); var coupleDayCount = coupleDaySum != null ? coupleDaySum.LeaveDaysUsed : 0; - var coupleCount = coupleDay != null ? coupleDay.CountLeaveDay : 0; + var coupleCount = coupleDaySum != null ? coupleDaySum.LeaveCount : 0; var therapyDaySum = beginningData.FirstOrDefault(x => x.ProfileId == p.Id && x.LeaveType.Code == "LV-011"); var therapyDay = leaveDays.FirstOrDefault(x => x.KeycloakUserId == keycloakUserId && x.LeaveTypeCode == "LV-011"); var therapyDayCount = therapyDaySum != null ? therapyDaySum.LeaveDaysUsed : 0; - var therapyCount = therapyDay != null ? therapyDay.CountLeaveDay : 0; + var therapyCount = therapyDaySum != null ? therapyDaySum.LeaveCount : 0; var timeStamps = await _processUserTimeStampRepository.GetTimeStampHistoryByRangeForUserAsync(p.Keycloak ?? Guid.Empty, req.StartDate, req.EndDate);