From ee4ef415473bf233d0a7d2c6c78acc971cd95672 Mon Sep 17 00:00:00 2001 From: Suphonchai Phoonsawat Date: Wed, 17 Jan 2024 12:42:19 +0700 Subject: [PATCH] =?UTF-8?q?=E0=B9=81=E0=B8=81=E0=B9=89=20defect=20add=20se?= =?UTF-8?q?nd=20noti=20when=20create=20=E0=B8=A5=E0=B8=87=E0=B9=80?= =?UTF-8?q?=E0=B8=A7=E0=B8=A5=E0=B8=B2=E0=B8=81=E0=B8=A3=E0=B8=93=E0=B8=B5?= =?UTF-8?q?=E0=B8=9E=E0=B8=B4=E0=B9=80=E0=B8=A8=E0=B8=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Leaves/GenericLeaveRepository.cs | 2 +- .../LeaveRequests/LeaveRequestRepository.cs | 10 ++- .../AdditionalCheckRequestRepository.cs | 62 ++++++++++++++++++- .../Repositories/UserProfileRepository.cs | 23 +++++++ .../Controllers/LeaveController.cs | 9 ++- .../Controllers/LeaveRequestController.cs | 30 +++++++-- .../GetLeaveRequestForAdminDto.cs | 4 ++ .../GetLeaveRequestForAdminResultDto.cs | 16 ++++- 8 files changed, 144 insertions(+), 12 deletions(-) diff --git a/BMA.EHR.Application/Repositories/Leaves/GenericLeaveRepository.cs b/BMA.EHR.Application/Repositories/Leaves/GenericLeaveRepository.cs index e180fc83..8b80c36e 100644 --- a/BMA.EHR.Application/Repositories/Leaves/GenericLeaveRepository.cs +++ b/BMA.EHR.Application/Repositories/Leaves/GenericLeaveRepository.cs @@ -2,6 +2,7 @@ using BMA.EHR.Domain.Models.Base; using Microsoft.AspNetCore.Http; using Microsoft.EntityFrameworkCore; +using System.IO.Pipes; using System.Security.Claims; namespace BMA.EHR.Application.Repositories.Leaves @@ -24,7 +25,6 @@ namespace BMA.EHR.Application.Repositories.Leaves _dbContext = dbContext; _dbSet = _dbContext.Set(); _httpContextAccessor = httpContextAccessor; - } #endregion diff --git a/BMA.EHR.Application/Repositories/Leaves/LeaveRequests/LeaveRequestRepository.cs b/BMA.EHR.Application/Repositories/Leaves/LeaveRequests/LeaveRequestRepository.cs index f6469452..73b65065 100644 --- a/BMA.EHR.Application/Repositories/Leaves/LeaveRequests/LeaveRequestRepository.cs +++ b/BMA.EHR.Application/Repositories/Leaves/LeaveRequests/LeaveRequestRepository.cs @@ -146,7 +146,7 @@ namespace BMA.EHR.Application.Repositories.Leaves.LeaveRequests return await rawData.ToListAsync(); } - public async Task> GetLeaveRequestForAdminAsync(int year, Guid type, string status) + public async Task> GetLeaveRequestForAdminAsync(int year, Guid type, string status, DateTime startDate, DateTime endDate) { var rawData = _dbContext.Set() .Include(x => x.Type) @@ -164,6 +164,12 @@ namespace BMA.EHR.Application.Repositories.Leaves.LeaveRequests if (status.Trim().ToUpper() != "ALL") rawData = rawData.Where(x => x.LeaveStatus == status); + if (startDate != DateTime.MinValue) + rawData = rawData.Where(x => x.LeaveStartDate >= startDate); + + if (endDate != DateTime.MinValue) + rawData = rawData.Where(x => x.LeaveEndDate <= endDate); + return await rawData.ToListAsync(); } @@ -570,7 +576,7 @@ namespace BMA.EHR.Application.Repositories.Leaves.LeaveRequests .Include(x => x.Type) .Where(x => x.KeycloakUserId == keycloakUserId) .Where(x => x.LeaveStartDate.Date == startDate.Date || x.LeaveEndDate.Date == endDate.Date) - .Where(x => x.LeaveStatus == "DELETE" && x.LeaveStatus == "REJECT") + .Where(x => x.LeaveStatus == "NEW" || x.LeaveStatus == "PENDING" || x.LeaveStatus == "APPROVE") .ToListAsync(); return data.Count > 0; diff --git a/BMA.EHR.Application/Repositories/Leaves/TimeAttendants/AdditionalCheckRequestRepository.cs b/BMA.EHR.Application/Repositories/Leaves/TimeAttendants/AdditionalCheckRequestRepository.cs index e51ec08f..02bd15a8 100644 --- a/BMA.EHR.Application/Repositories/Leaves/TimeAttendants/AdditionalCheckRequestRepository.cs +++ b/BMA.EHR.Application/Repositories/Leaves/TimeAttendants/AdditionalCheckRequestRepository.cs @@ -1,6 +1,9 @@ using BMA.EHR.Application.Common.Interfaces; using BMA.EHR.Application.Messaging; +using BMA.EHR.Application.Repositories.Commands; +using BMA.EHR.Application.Responses; using BMA.EHR.Domain.Models.Leave.TimeAttendants; +using BMA.EHR.Domain.Models.Notifications; using BMA.EHR.Domain.Shared; using Microsoft.AspNetCore.Http; using Microsoft.EntityFrameworkCore; @@ -18,6 +21,8 @@ namespace BMA.EHR.Application.Repositories.Leaves.TimeAttendants private readonly UserProfileRepository _userProfileRepository; private readonly IConfiguration _configuration; private readonly EmailSenderService _emailSenderService; + private readonly IApplicationDBContext _appContext; + private readonly CommandRepository _commandRepository; #endregion @@ -28,7 +33,9 @@ namespace BMA.EHR.Application.Repositories.Leaves.TimeAttendants OrganizationCommonRepository organizationCommonRepository, UserProfileRepository userProfileRepository, IConfiguration configuration, - EmailSenderService emailSenderService) : base(dbContext, httpContextAccessor) + EmailSenderService emailSenderService, + IApplicationDBContext appContext, + CommandRepository commandRepository) : base(dbContext, httpContextAccessor) { _dbContext = dbContext; _httpContextAccessor = httpContextAccessor; @@ -36,6 +43,8 @@ namespace BMA.EHR.Application.Repositories.Leaves.TimeAttendants _userProfileRepository = userProfileRepository; _configuration = configuration; _emailSenderService = emailSenderService; + _appContext = appContext; + _commandRepository = commandRepository; } #endregion @@ -57,6 +66,57 @@ namespace BMA.EHR.Application.Repositories.Leaves.TimeAttendants #region " Methods " + public override async Task AddAsync(AdditionalCheckRequest entity) + { + await base.AddAsync(entity); + + var userId = UserId != null ? Guid.Parse(UserId) : Guid.Empty; + var profile = await _userProfileRepository.GetProfileByKeycloakIdAsync(userId); + var profile_id = profile == null ? Guid.Empty : profile.Id; + + var rootOc = _userProfileRepository.GetRootOcId(profile_id); + var approver = string.Empty; + var list = new List(); + if (rootOc != null) + { + list = await _commandRepository.GetOrgApproverAsync(rootOc ?? Guid.Empty); + } + if (list.Count > 0) + { + var appr = list.FirstOrDefault(); + + // send inbox and notification + var subject_str = $"มีการขออนุมัติลงเวลากรณีพิเศษ"; + var body_str = $"โปรดพิจารณาคำร้องขอลงเวลาในกรณีพิเศษจาก {profile.Prefix.Name}{profile.FirstName} {profile.LastName}"; + + var subject = subject_str; + var body = body_str; + _emailSenderService.SendMail(subject, body, "dev@frappet.com"); + + + var inbox = new Inbox + { + Subject = subject_str, + Body = body_str, + ReceiverUserId = appr.Id, + Payload = "", + }; + _appContext.Set().Add(inbox); + + var noti = new Notification + { + Body = body_str, + ReceiverUserId = appr.Id, + Type = "", + Payload = "", + }; + _appContext.Set().Add(noti); + await _appContext.SaveChangesAsync(); + } + + return entity; + } + public async Task> GetAdditionalCheckRequestsByUserId(Guid keycloakId, int year, int month) { try diff --git a/BMA.EHR.Application/Repositories/UserProfileRepository.cs b/BMA.EHR.Application/Repositories/UserProfileRepository.cs index e1d5fbe5..893af1d7 100644 --- a/BMA.EHR.Application/Repositories/UserProfileRepository.cs +++ b/BMA.EHR.Application/Repositories/UserProfileRepository.cs @@ -1,6 +1,7 @@ using System.Security.Cryptography.X509Certificates; using BMA.EHR.Application.Common.Interfaces; using BMA.EHR.Domain.Models.HR; +using BMA.EHR.Domain.Models.MetaData; using BMA.EHR.Domain.Models.Organizations; using BMA.EHR.Domain.Shared; using Microsoft.AspNetCore.Http; @@ -29,7 +30,29 @@ namespace BMA.EHR.Application.Repositories #region " Methods " + public async Task GetOrganizationById(Guid id) + { + var data = await _dbContext.Set().AsQueryable() + .FirstOrDefaultAsync(x => x.Id == id); + return data; + } + + public async Task GetOrgAgencyById(Guid id) + { + var data = await _dbContext.Set().AsQueryable() + .FirstOrDefaultAsync(x => x.Id == id); + + return data; + } + + public async Task GetOrgGovAgencyById(Guid id) + { + var data = await _dbContext.Set().AsQueryable() + .FirstOrDefaultAsync(x => x.Id == id); + + return data; + } public async Task GetProfileByKeycloakIdAsync(Guid keycloakId) { diff --git a/BMA.EHR.Leave.Service/Controllers/LeaveController.cs b/BMA.EHR.Leave.Service/Controllers/LeaveController.cs index aab9a70f..35fa9371 100644 --- a/BMA.EHR.Leave.Service/Controllers/LeaveController.cs +++ b/BMA.EHR.Leave.Service/Controllers/LeaveController.cs @@ -1,5 +1,7 @@ using BMA.EHR.Application.Repositories; +using BMA.EHR.Application.Repositories.Commands; using BMA.EHR.Application.Repositories.Leaves.TimeAttendants; +using BMA.EHR.Application.Responses; using BMA.EHR.Domain.Common; using BMA.EHR.Domain.Models.Leave.TimeAttendants; using BMA.EHR.Domain.Shared; @@ -41,6 +43,8 @@ namespace BMA.EHR.Leave.Service.Controllers private readonly UserCalendarRepository _userCalendarRepository; + private readonly CommandRepository _commandRepository; + private readonly string _bucketName = "check-in"; #endregion @@ -58,7 +62,8 @@ namespace BMA.EHR.Leave.Service.Controllers ProcessUserTimeStampRepository processUserTimeStampRepository, UserDutyTimeRepository userDutyTimeRepository, AdditionalCheckRequestRepository additionalCheckRequestRepository, - UserCalendarRepository userCalendarRepository) + UserCalendarRepository userCalendarRepository, + CommandRepository commandRepository) { _dutyTimeRepository = dutyTimeRepository; _context = context; @@ -72,6 +77,7 @@ namespace BMA.EHR.Leave.Service.Controllers _userDutyTimeRepository = userDutyTimeRepository; _additionalCheckRequestRepository = additionalCheckRequestRepository; _userCalendarRepository = userCalendarRepository; + _commandRepository = commandRepository; } #endregion @@ -1086,6 +1092,7 @@ namespace BMA.EHR.Leave.Service.Controllers Description = req.Description, }; await _additionalCheckRequestRepository.AddAsync(request); + return Success(); } diff --git a/BMA.EHR.Leave.Service/Controllers/LeaveRequestController.cs b/BMA.EHR.Leave.Service/Controllers/LeaveRequestController.cs index 5caaac1b..3d8681cb 100644 --- a/BMA.EHR.Leave.Service/Controllers/LeaveRequestController.cs +++ b/BMA.EHR.Leave.Service/Controllers/LeaveRequestController.cs @@ -119,7 +119,7 @@ namespace BMA.EHR.Leave.Service.Controllers var isDuplicate = await _leaveRequestRepository.CheckDuplicateLeave(userId, req.LeaveStartDate.Date, req.LeaveEndDate.Date); if (isDuplicate) { - return Error("ไม่สามารถขอลาในช่วงเวลาเดียวกันได้"); + return Error("ไม่สามารถขอลาในช่วงเวลาเดียวกันได้ เนื่องจากมีการขอลาในช่วงเวลาดังกล่าวแล้ว"); } var thisYear = DateTime.Now.Year; @@ -980,13 +980,24 @@ namespace BMA.EHR.Leave.Service.Controllers public async Task> GetLeaveRequestForAdminAsync( [FromBody] GetLeaveRequestForAdminDto req) { - var rawData = await _leaveRequestRepository.GetLeaveRequestForAdminAsync(req.Year, req.Type, req.Status); + var rawData = await _leaveRequestRepository.GetLeaveRequestForAdminAsync(req.Year, req.Type, req.Status, req.StartDate, req.EndDate); var result = new List(); foreach (var item in rawData) { var profile = await _userProfileRepository.GetProfileByKeycloakIdAsync(item.KeycloakUserId); + + // Get Organization + var org = await _userProfileRepository.GetOrganizationById(profile.OcId ?? Guid.Empty); + + var agency_id = org == null ? Guid.Empty : org.OrganizationAgencyId ?? Guid.Empty; + var gov_agency_id = org == null ? Guid.Empty : org.OrganizationGovernmentAgencyId ?? Guid.Empty; + + var agency = await _userProfileRepository.GetOrgAgencyById(agency_id); + var gov_agency = await _userProfileRepository.GetOrgGovAgencyById(gov_agency_id); + + var res = new GetLeaveRequestForAdminResultDto { Id = item.Id, @@ -994,7 +1005,14 @@ namespace BMA.EHR.Leave.Service.Controllers LeaveTypeName = item.Type.Name, FullName = $"{profile.Prefix.Name}{profile.FirstName} {profile.LastName}", DateSendLeave = item.CreatedAt.Date, - Status = item.LeaveStatus + Status = item.LeaveStatus, + CitizenId = profile.CitizenId ?? "", + LeaveStartDate = item.LeaveStartDate, + LeaveEndDate = item.LeaveEndDate, + Position = profile.Position == null ? "" : profile.Position.Name, + Level = profile.PositionLevel == null ? "" : profile.PositionLevel.Name, + Agency = agency == null ? "" : agency.Name, + Org = gov_agency == null ? "" : gov_agency.Name }; result.Add(res); } @@ -1524,17 +1542,17 @@ namespace BMA.EHR.Leave.Service.Controllers /// เมื่อทำรายการสำเร็จ /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน - [HttpDelete("user/file/document/{id:guid}")] + [HttpDelete("user/file/document/{id:guid}/{docId:guid}")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] - public async Task> DeleteLeaveDocumentByIdAsync(Guid id, [FromBody] DeleteLeaveDocumentDto req) + public async Task> DeleteLeaveDocumentByIdAsync(Guid id, Guid docId) { var leaveReq = await _leaveRequestRepository.GetByIdAsync(id); if (leaveReq == null) return Error(GlobalMessages.DataNotFound); - var doc = leaveReq.LeaveDocument.Where(x => x.Document.Id == req.DocId).FirstOrDefault(); + var doc = leaveReq.LeaveDocument.Where(x => x.Document.Id == docId).FirstOrDefault(); if (doc != null) { await _minIOService.DeleteFileAsync(doc.Document.Id); diff --git a/BMA.EHR.Leave.Service/DTOs/LeaveRequest/GetLeaveRequestForAdminDto.cs b/BMA.EHR.Leave.Service/DTOs/LeaveRequest/GetLeaveRequestForAdminDto.cs index bc78293d..f1bb40a8 100644 --- a/BMA.EHR.Leave.Service/DTOs/LeaveRequest/GetLeaveRequestForAdminDto.cs +++ b/BMA.EHR.Leave.Service/DTOs/LeaveRequest/GetLeaveRequestForAdminDto.cs @@ -21,5 +21,9 @@ namespace BMA.EHR.Leave.Service.DTOs.LeaveRequest public int PageSize { get; set; } = 10; public string Keyword { get; set; } = string.Empty; + + public DateTime StartDate { get; set; } = DateTime.MinValue; + + public DateTime EndDate { get; set; } = DateTime.MinValue; } } diff --git a/BMA.EHR.Leave.Service/DTOs/LeaveRequest/GetLeaveRequestForAdminResultDto.cs b/BMA.EHR.Leave.Service/DTOs/LeaveRequest/GetLeaveRequestForAdminResultDto.cs index 998fdfac..b097c0ed 100644 --- a/BMA.EHR.Leave.Service/DTOs/LeaveRequest/GetLeaveRequestForAdminResultDto.cs +++ b/BMA.EHR.Leave.Service/DTOs/LeaveRequest/GetLeaveRequestForAdminResultDto.cs @@ -12,6 +12,20 @@ public DateTime DateSendLeave { get; set; } = DateTime.MinValue; - public string Status { get; set; } = string.Empty; + public string Status { get; set; } = string.Empty; + + public string CitizenId { get; set; } = string.Empty; + + public DateTime LeaveStartDate { get; set; } = DateTime.MinValue; + + public DateTime LeaveEndDate { get; set; } = DateTime.MinValue; + + public string Agency { get; set; } = string.Empty; + + public string Org { get; set; } = string.Empty; + + public string Position { get; set; } = string.Empty; + + public string Level { get; set; } = string.Empty; } }