fix issue

This commit is contained in:
Suphonchai Phoonsawat 2025-04-28 12:02:29 +07:00
parent 28544df284
commit ae2112f23a
3 changed files with 365 additions and 237 deletions

View file

@ -1,6 +1,4 @@
using System.Drawing;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using Amazon.S3.Model;
using BMA.EHR.Application.Common.Interfaces;
using BMA.EHR.Application.Messaging;
using BMA.EHR.Application.Responses.Leaves;
@ -12,6 +10,10 @@ using BMA.EHR.Domain.Shared;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Nest;
using System.Drawing;
using System.Net.Http.Headers;
using System.Net.Http.Json;
namespace BMA.EHR.Application.Repositories.Leaves.LeaveRequests
{
@ -27,6 +29,7 @@ namespace BMA.EHR.Application.Repositories.Leaves.LeaveRequests
private readonly EmailSenderService _emailSenderService;
private readonly IApplicationDBContext _appDbContext;
private readonly MinIOLeaveService _minIOService;
private readonly string URL = string.Empty;
@ -40,7 +43,8 @@ namespace BMA.EHR.Application.Repositories.Leaves.LeaveRequests
UserProfileRepository userProfileRepository,
IConfiguration configuration,
EmailSenderService emailSenderService,
IApplicationDBContext appDbContext) : base(dbContext, httpContextAccessor)
IApplicationDBContext appDbContext,
MinIOLeaveService minIOService) : base(dbContext, httpContextAccessor)
{
_dbContext = dbContext;
_httpContextAccessor = httpContextAccessor;
@ -51,6 +55,7 @@ namespace BMA.EHR.Application.Repositories.Leaves.LeaveRequests
_appDbContext = appDbContext;
URL = (_configuration["API"]).Replace("/api/v1", "");
_minIOService = minIOService;
}
#endregion
@ -197,6 +202,53 @@ namespace BMA.EHR.Application.Repositories.Leaves.LeaveRequests
#endregion
public async Task AddLeaveDocumentAsync(Guid id, LeaveDocument doc)
{
try
{
var req = await _dbContext.Set<LeaveRequest>().FirstOrDefaultAsync(x => x.Id == id);
req!.LeaveDocument.Add(doc);
await _dbContext.SaveChangesAsync();
}
catch
{
throw;
}
}
public async Task AddApproversAsync(Guid id, List<LeaveRequestApprover> approvers)
{
try
{
var req = await _dbContext.Set<LeaveRequest>().FirstOrDefaultAsync(x => x.Id == id);
req!.Approvers.AddRange(approvers);
await _dbContext.SaveChangesAsync();
}
catch
{
throw;
}
}
public async Task RemoveApproversAsync(Guid id, string type)
{
try
{
var data = await _dbContext.Set<LeaveRequestApprover>()
.Where(x => x.LeaveRequest.Id == id && x.ApproveType.ToUpper() == type.ToUpper())
.ToListAsync();
_dbContext.Set<LeaveRequestApprover>().RemoveRange(data);
await _dbContext.SaveChangesAsync();
}
catch
{
throw;
}
}
public async Task<List<LeaveRequest>> GetLeaveRequestByYearAsync(int year)
{
var data = await _dbContext.Set<LeaveRequest>().AsQueryable().AsNoTracking()

View file

@ -134,11 +134,15 @@ namespace BMA.EHR.Leave.Service.Controllers
return Error(GlobalMessages.DataNotFound, StatusCodes.Status404NotFound);
}
var delete = leaveReq.Approvers.RemoveAll(x => x.ApproveType == type.Trim().ToUpper());
await _leaveRequestRepository.RemoveApproversAsync(id, type);
//var delete = leaveReq.Approvers.RemoveAll(x => x.ApproveType.ToUpper() == type.Trim().ToUpper());
//await _leaveRequestRepository.UpdateAsync(leaveReq);
var addList = new List<LeaveRequestApprover>();
foreach (var r in req)
{
leaveReq.Approvers.Add(new LeaveRequestApprover
addList.Add(new LeaveRequestApprover
{
Seq = r.Seq,
Prefix = r.Prefix,
@ -152,7 +156,7 @@ namespace BMA.EHR.Leave.Service.Controllers
});
}
await _leaveRequestRepository.UpdateAsync(leaveReq);
await _leaveRequestRepository.AddApproversAsync(id, addList);
return Success();
@ -502,6 +506,8 @@ namespace BMA.EHR.Leave.Service.Controllers
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<ActionResult<ResponseObject>> EditLeaveRequestAsync(Guid id, [FromForm] CreateLeaveRequestDto req)
{
try
{
var oldData = await _leaveRequestRepository.GetByIdAsync(id);
if (oldData == null)
@ -591,31 +597,7 @@ namespace BMA.EHR.Leave.Service.Controllers
**/
// upload document
if (req.LeaveDocument != null)
{
foreach (var d in req.LeaveDocument)
{
var doc = await _minIOService.UploadFileAsync(d);
if (doc != null)
{
oldData.LeaveDocument.Add(new LeaveDocument { Document = doc });
var a = oldData.LeaveDocument.Last();
_context.Entry(a).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
}
}
}
// upload draft document
if (req.LeaveDraftDocument != null)
{
var doc = await _minIOService.UploadFileAsync(req.LeaveDraftDocument);
if (doc != null)
{
oldData.LeaveDraftDocument = doc;
_context.Entry(oldData.LeaveDraftDocument).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
}
}
// switch from leave type
switch (leaveType.Code.Trim().ToUpper())
@ -741,8 +723,46 @@ namespace BMA.EHR.Leave.Service.Controllers
// save to database
await _leaveRequestRepository.UpdateAsync(oldData);
// upload document
if (req.LeaveDocument != null)
{
foreach (var d in req.LeaveDocument)
{
var doc = await _minIOService.UploadFileAsync(d);
if (doc != null)
{
// add new record
await _leaveRequestRepository.AddLeaveDocumentAsync(id, new LeaveDocument { Document = doc });
//oldData.LeaveDocument.Add(new LeaveDocument { Document = doc });
//var a = oldData.LeaveDocument.Last();
//_context.Entry(a).State = Microsoft.EntityFrameworkCore.EntityState.Added;
}
}
}
// upload draft document
if (req.LeaveDraftDocument != null)
{
var doc = await _minIOService.UploadFileAsync(req.LeaveDraftDocument);
if (doc != null)
{
oldData.LeaveDraftDocument = doc;
_context.Entry(oldData.LeaveDraftDocument).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
}
// save to database
await _leaveRequestRepository.UpdateAsync(oldData);
}
return Success(new { id = oldData.Id });
}
catch (Exception ex)
{
return Error(ex);
}
}
/// <summary>
@ -2249,6 +2269,37 @@ namespace BMA.EHR.Leave.Service.Controllers
}
}
var commanders = rawData.Approvers.Where(x => x.ApproveType.ToUpper() == "COMMANDER")
.Select(x => new GetLeaveApproverDto
{
Seq = x.Seq,
Prefix = x.Prefix,
FirstName = x.FirstName,
LastName = x.LastName,
PositionName = x.PositionName,
ApproveStatus = x.ApproveStatus,
Comment = x.Comment,
ProfileId = x.ProfileId,
KeycloakId = x.KeycloakId
}).ToList();
var approvers = rawData.Approvers.Where(x => x.ApproveType.ToUpper() == "APPROVER")
.Select(x => new GetLeaveApproverDto
{
Seq = x.Seq,
Prefix = x.Prefix,
FirstName = x.FirstName,
LastName = x.LastName,
PositionName = x.PositionName,
ApproveStatus = x.ApproveStatus,
Comment = x.Comment,
ProfileId = x.ProfileId,
KeycloakId = x.KeycloakId
}).ToList();
result.Approvers.AddRange(approvers);
result.Commanders.AddRange(commanders);
return Success(result);
}

View file

@ -141,5 +141,30 @@ namespace BMA.EHR.Leave.Service.DTOs.LeaveRequest
public string? ProfileType { get; set; }
public string? CommanderPosition { get; set; } = string.Empty;
public List<GetLeaveApproverDto> Commanders { get; set; } = new();
public List<GetLeaveApproverDto> Approvers { get; set; } = new();
}
public class GetLeaveApproverDto
{
public int Seq { get; set; } = 0;
public string Prefix { get; set; } = string.Empty;
public string FirstName { get; set; } = string.Empty;
public string LastName { get; set; } = string.Empty;
public string PositionName { get; set; } = string.Empty;
public Guid ProfileId { get; set; } = Guid.Empty;
public Guid KeycloakId { get; set; } = Guid.Empty;
public string ApproveStatus { get; set; } = string.Empty;
public string Comment { get; set; } = string.Empty;
}
}