diff --git a/BMA.EHR.Application/Repositories/Leaves/GenericLeaveRepository.cs b/BMA.EHR.Application/Repositories/Leaves/GenericLeaveRepository.cs index 0e407725..5fbbb8a1 100644 --- a/BMA.EHR.Application/Repositories/Leaves/GenericLeaveRepository.cs +++ b/BMA.EHR.Application/Repositories/Leaves/GenericLeaveRepository.cs @@ -77,7 +77,7 @@ namespace BMA.EHR.Application.Repositories.Leaves (entity as EntityBase).LastUpdatedAt = DateTime.Now; } - _dbContext.Detach(entity); + //_dbContext.Detach(entity); _dbSet.Update(entity); await _dbContext.SaveChangesAsync(); diff --git a/BMA.EHR.Application/Repositories/Leaves/LeaveRequests/LeaveRequestRepository.cs b/BMA.EHR.Application/Repositories/Leaves/LeaveRequests/LeaveRequestRepository.cs index fe892ad2..de0f7e9a 100644 --- a/BMA.EHR.Application/Repositories/Leaves/LeaveRequests/LeaveRequestRepository.cs +++ b/BMA.EHR.Application/Repositories/Leaves/LeaveRequests/LeaveRequestRepository.cs @@ -70,7 +70,7 @@ namespace BMA.EHR.Application.Repositories.Leaves.LeaveRequests #region " Overrides " - public override async Task GetByIdAsync(Guid id) + public async Task GetByIdWithTrackingAsync(Guid id) { var data = await _dbContext.Set().AsQueryable() //.AsNoTracking() @@ -84,6 +84,20 @@ namespace BMA.EHR.Application.Repositories.Leaves.LeaveRequests return data; } + public override async Task GetByIdAsync(Guid id) + { + var data = await _dbContext.Set().AsQueryable() + .AsNoTracking() + .Include(x => x.LeaveDocument) + .ThenInclude(x => x.Document) + .Include(x => x.LeaveDraftDocument) + .Include(x => x.LeaveCancelDocument) + .Include(x => x.Type) + .FirstOrDefaultAsync(x => x.Id == id); + + return data; + } + public override async Task AddAsync(LeaveRequest entity) { if (entity.LeaveCancelDocument != null) @@ -108,7 +122,7 @@ namespace BMA.EHR.Application.Repositories.Leaves.LeaveRequests return await base.AddAsync(entity); } - public override async Task UpdateAsync(LeaveRequest entity) + public async Task UpdateWithTrackingAsync(LeaveRequest entity) { // detach //_dbContext.Detach(entity); @@ -137,6 +151,35 @@ namespace BMA.EHR.Application.Repositories.Leaves.LeaveRequests return await base.UpdateAsync(entity); } + public override async Task UpdateAsync(LeaveRequest entity) + { + // detach + //_dbContext.Detach(entity); + + if (entity.LeaveCancelDocument != null) + _dbContext.Attatch(entity.LeaveCancelDocument); + + if (entity.LeaveDraftDocument != null) + _dbContext.Attatch(entity.LeaveDraftDocument); + + if (entity.LeaveDocument != null) + { + foreach (var d in entity.LeaveDocument) + { + _dbContext.Attatch(d); + } + } + + if (entity.Type != null) + { + _dbContext.Attatch(entity.Type); + //_dbContext.Detach(entity.Type); + } + + + return await base.UpdateAsync(entity); + } + #endregion @@ -275,6 +318,41 @@ namespace BMA.EHR.Application.Repositories.Leaves.LeaveRequests return await rawData.ToListAsync(); } + public async Task ApproveCancelLeaveRequestAsync(LeaveRequest data,string Reason) + { + try + { + var profile = await _userProfileRepository.GetProfileByKeycloakIdAsync(data.KeycloakUserId, AccessToken ?? ""); + if (profile == null) + { + throw new Exception(GlobalMessages.DataNotFound); + } + + + data.LeaveCancelStatus = "APPROVE"; + data.LeaveCancelComment = Reason; + + // Send Noti + var noti = new Notification + { + Body = $"การขอยกเลิกใบลาของคุณได้รับการอนุมัติ", + ReceiverUserId = profile.Id, + Type = "", + Payload = "", + }; + _appDbContext.Set().Add(noti); + await _appDbContext.SaveChangesAsync(); + + return data; + } + catch + { + throw; + } + + } + + public async Task ApproveCancelLeaveRequestAsync(Guid id, string Reason) { var rawData = await GetByIdAsync(id); diff --git a/BMA.EHR.Leave/Controllers/LeaveRequestController.cs b/BMA.EHR.Leave/Controllers/LeaveRequestController.cs index 2a52e45b..3bba1d70 100644 --- a/BMA.EHR.Leave/Controllers/LeaveRequestController.cs +++ b/BMA.EHR.Leave/Controllers/LeaveRequestController.cs @@ -1151,7 +1151,7 @@ namespace BMA.EHR.Leave.Service.Controllers public async Task> CancelLeaveRequestAsync([FromForm] CancelLeaveRequestDto req, Guid id) { - var data = await _leaveRequestRepository.GetByIdAsync(id); + var data = await _leaveRequestRepository.GetByIdWithTrackingAsync(id); if (data == null) { //return Success(new List()); @@ -1173,7 +1173,7 @@ namespace BMA.EHR.Leave.Service.Controllers } else { - await _leaveRequestRepository.ApproveCancelLeaveRequestAsync(data.Id, "อนุมัติการขอยกเลิกการลา โดยระบบ"); + data = await _leaveRequestRepository.ApproveCancelLeaveRequestAsync(data, "อนุมัติการขอยกเลิกการลา โดยระบบ"); } // upload leave cancel document @@ -1187,7 +1187,7 @@ namespace BMA.EHR.Leave.Service.Controllers } // save to database - await _leaveRequestRepository.UpdateAsync(data); + await _leaveRequestRepository.UpdateWithTrackingAsync(data); return Success(); }