diff --git a/BMA.EHR.Application/ApplicationServicesRegistration.cs b/BMA.EHR.Application/ApplicationServicesRegistration.cs index bf6dc6df..0b99a7b6 100644 --- a/BMA.EHR.Application/ApplicationServicesRegistration.cs +++ b/BMA.EHR.Application/ApplicationServicesRegistration.cs @@ -61,6 +61,8 @@ namespace BMA.EHR.Application services.AddTransient(); + services.AddTransient(); + return services; } diff --git a/BMA.EHR.Application/Repositories/Leaves/TimeAttendants/LeaveProcessJobStatusRepository.cs b/BMA.EHR.Application/Repositories/Leaves/TimeAttendants/LeaveProcessJobStatusRepository.cs new file mode 100644 index 00000000..114087b5 --- /dev/null +++ b/BMA.EHR.Application/Repositories/Leaves/TimeAttendants/LeaveProcessJobStatusRepository.cs @@ -0,0 +1,119 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using BMA.EHR.Application.Common.Interfaces; +using BMA.EHR.Domain.Models.Leave.TimeAttendants; +using Microsoft.AspNetCore.Http; +using Microsoft.EntityFrameworkCore; + +namespace BMA.EHR.Application.Repositories.Leaves.TimeAttendants +{ + public class LeaveProcessJobStatusRepository: GenericLeaveRepository + { + #region " Fields " + + private readonly ILeaveDbContext _dbContext; + + #endregion + + #region " Constructor and Destructor " + + public LeaveProcessJobStatusRepository(ILeaveDbContext dbContext, + IHttpContextAccessor httpContextAccessor) : base(dbContext, httpContextAccessor) + { + _dbContext = dbContext; + } + + #endregion + + #region " Methods " + + /// + /// ดึงข้อมูล Job Status จาก TaskId + /// + public async Task GetByTaskIdAsync(Guid id) + { + var data = await _dbContext.Set() + .Where(x => x.Id == id) + .FirstOrDefaultAsync(); + + return data; + } + + /// + /// ดึงข้อมูล Job Status จาก UserId และสถานะ + /// + public async Task> GetByUserIdAndStatusAsync(Guid userId, string status) + { + var data = await _dbContext.Set() + .Where(x => x.CreatedUserId == userId.ToString("D") && x.Status == status) + .OrderByDescending(x => x.CreatedDate) + .ToListAsync(); + + return data; + } + + /// + /// ดึงข้อมูล Job Status ที่ยัง pending หรือ processing + /// + public async Task> GetPendingOrProcessingJobsAsync(Guid userId) + { + var data = await _dbContext.Set() + .Where(x => x.CreatedUserId == userId.ToString("D") && + (x.Status == "PENDING" || x.Status == "PROCESSING")) + //.OrderByDescending(x => x.CreatedDate) + .ToListAsync(); + + return data; + } + + /// + /// อัปเดตสถานะเป็น Processing + /// + public async Task UpdateToProcessingAsync(Guid id) + { + var job = await GetByTaskIdAsync(id); + if (job != null) + { + job.Status = "PROCESSING"; + job.ProcessingDate = DateTime.Now; + await UpdateAsync(job); + } + return job!; + } + + /// + /// อัปเดตสถานะเป็น Completed + /// + public async Task UpdateToCompletedAsync(Guid id, string? additionalData = null) + { + var job = await GetByTaskIdAsync(id); + if (job != null) + { + job.Status = "COMPLETED"; + job.CompletedDate = DateTime.Now; + await UpdateAsync(job); + } + return job!; + } + + /// + /// อัปเดตสถานะเป็น Failed + /// + public async Task UpdateToFailedAsync(Guid id, string errorMessage) + { + var job = await GetByTaskIdAsync(id); + if (job != null) + { + job.Status = "FAILED"; + job.CompletedDate = DateTime.Now; + job.ErrorMessage = errorMessage; + await UpdateAsync(job); + } + return job!; + } + + #endregion + } +} \ No newline at end of file