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.Extensions; 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; using Microsoft.Extensions.Configuration; namespace BMA.EHR.Application.Repositories.Leaves.TimeAttendants { public class AdditionalCheckRequestRepository : 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; private readonly IApplicationDBContext _appContext; private readonly CommandRepository _commandRepository; #endregion #region " Constructor and Destuctor " public AdditionalCheckRequestRepository(ILeaveDbContext dbContext, IHttpContextAccessor httpContextAccessor, OrganizationCommonRepository organizationCommonRepository, UserProfileRepository userProfileRepository, IConfiguration configuration, EmailSenderService emailSenderService, IApplicationDBContext appContext, CommandRepository commandRepository) : base(dbContext, httpContextAccessor) { _dbContext = dbContext; _httpContextAccessor = httpContextAccessor; _organizationCommonRepository = organizationCommonRepository; _userProfileRepository = userProfileRepository; _configuration = configuration; _emailSenderService = emailSenderService; _appContext = appContext; _commandRepository = commandRepository; } #endregion #region " Properties " protected Guid UserOrganizationId { get { if (UserId != null || UserId != "") return _userProfileRepository.GetUserOCId(Guid.Parse(UserId!)); else return Guid.Empty; } } #endregion #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, AccessToken ?? ""); 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}{profile.FirstName} {profile.LastName} ในวันที่ {entity.CheckDate.Date.ToThaiShortDate2()}"; 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 == Guid.Empty ? Guid.Parse("08dbca3a-8b6a-4a4e-8b23-1f62e4f30ef6") : appr.Id, Payload = "", }; _appContext.Set().Add(inbox); var noti = new Notification { Body = body_str, ReceiverUserId = appr.Id == Guid.Empty ? Guid.Parse("08dbca3a-8b6a-4a4e-8b23-1f62e4f30ef6") : appr.Id, Type = "", Payload = "", }; _appContext.Set().Add(noti); await _appContext.SaveChangesAsync(); } return entity; } public async Task> GetAdditionalCheckRequestsByUserId(Guid keycloakId, int year, int month) { try { var data = await _dbContext.Set().AsQueryable() .Where(x => x.KeycloakUserId == keycloakId) .Where(x => (x.CheckDate.Year == year && x.CheckDate.Month == month)) .OrderByDescending(x => x.CheckDate.Date) // add sort for fix defect .ToListAsync(); return data; } catch { throw; } } public async Task> GetAdditionalCheckRequests(int year, int month) { try { var data = await _dbContext.Set().AsQueryable() .Where(x => (x.CheckDate.Year == year && x.CheckDate.Month == month)) .OrderByDescending(x => x.CreatedAt.Date) .ToListAsync(); return data; } catch { throw; } } #endregion } }