using Amazon.S3.Model; using BMA.EHR.Application.Common.Interfaces; using BMA.EHR.Application.Messaging; using BMA.EHR.Domain.Extensions; using BMA.EHR.Domain.Models.Leave.Commons; using BMA.EHR.Domain.Models.Leave.Requests; using BMA.EHR.Domain.Shared; using Microsoft.AspNetCore.Http; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; namespace BMA.EHR.Application.Repositories.Leaves.LeaveRequests { public class LeaveBeginningRepository : GenericLeaveRepository { #region " Fields " private readonly ILeaveDbContext _dbContext; private readonly IHttpContextAccessor _httpContextAccessor; private readonly OrganizationCommonRepository _organizationCommonRepository; private readonly UserProfileRepository _userProfileRepository; private readonly IConfiguration _configuration; private readonly EmailSenderService _emailSenderService; #endregion #region " Constructor and Destuctor " public LeaveBeginningRepository(ILeaveDbContext dbContext, IHttpContextAccessor httpContextAccessor, OrganizationCommonRepository organizationCommonRepository, UserProfileRepository userProfileRepository, IConfiguration configuration, EmailSenderService emailSenderService) : base(dbContext, httpContextAccessor) { _dbContext = dbContext; _httpContextAccessor = httpContextAccessor; _organizationCommonRepository = organizationCommonRepository; _userProfileRepository = userProfileRepository; _configuration = configuration; _emailSenderService = emailSenderService; } #endregion #region " Properties " protected Guid UserOrganizationId { get { if (UserId != null || UserId != "") return _userProfileRepository.GetUserOCId(Guid.Parse(UserId!), AccessToken); else return Guid.Empty; } } #endregion public async Task> GetAllByYearAsync(int year) { return await _dbContext.Set() .Include(x => x.LeaveType) .Where(x => x.LeaveYear == year) .ToListAsync(); } public async Task GetByYearAndTypeIdAsync(int year, Guid typeId) { var data = await _dbContext.Set() .Include(x => x.LeaveType) .FirstOrDefaultAsync(x => x.LeaveYear == year && x.LeaveTypeId == typeId); return data; } public async Task UpdateLeaveUsageAsync(int year, Guid typeId, Guid userId, double day) { var pf = await _userProfileRepository.GetProfileByKeycloakIdAsync(userId, AccessToken); if (pf == null) { throw new Exception(GlobalMessages.DataNotFound); } var data = await _dbContext.Set() .Include(x => x.LeaveType) .FirstOrDefaultAsync(x => x.LeaveYear == year && x.LeaveTypeId == typeId && x.ProfileId == pf.Id); if(data == null) { throw new Exception(GlobalMessages.DataNotFound); } data.LeaveDaysUsed += day; await _dbContext.SaveChangesAsync(); } public async Task GetByYearAndTypeIdForUserAsync(int year, Guid typeId, Guid userId) { var pf = await _userProfileRepository.GetProfileByKeycloakIdAsync(userId, AccessToken); if (pf == null) { throw new Exception(GlobalMessages.DataNotFound); } var govAge = (pf?.DateStart?.Date ?? DateTime.Now.Date).DiffDay(DateTime.Now.Date); var leaveType = await _dbContext.Set().FirstOrDefaultAsync(x => x.Id == typeId); var data = await _dbContext.Set() .Include(x => x.LeaveType) .FirstOrDefaultAsync(x => x.LeaveYear == year && x.LeaveTypeId == typeId && x.ProfileId == pf.Id); if (data == null) { var limit = 0.0; var prev = await _dbContext.Set() .Include(x => x.LeaveType) .FirstOrDefaultAsync(x => x.LeaveYear == year - 1 && x.LeaveTypeId == typeId && x.ProfileId == pf.Id); var prevRemain = 0.0; if (prev != null) { prevRemain = prev.LeaveDays - prev.LeaveDaysUsed; } if (govAge >= 180) { if (govAge >= 3650) { limit = 10 + prevRemain; if (limit > 30) limit = 30; } else { limit = 10 + prevRemain; if (limit > 20) limit = 20; } } else { limit = 0.0; } data = new LeaveBeginning { LeaveYear = year, LeaveTypeId = typeId, ProfileId = pf.Id, Prefix = pf.Prefix, FirstName = pf.FirstName, LastName = pf.LastName, LeaveDaysUsed = 0, LeaveDays = leaveType?.Code == "LV-005" ? limit : 0 }; _dbContext.Set().Add(data); await _dbContext.SaveChangesAsync(); } return data; } } }