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; using Serilog; using System.Diagnostics; 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!), AccessToken); 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 = await _userProfileRepository.GetProfileByKeycloakIdNew2Async(userId, AccessToken ?? ""); // fix issue : SIT ระบบบันทึกเวลาปฏิบัติงาน>>ลงเวลากรณีพิเศษ (ไม่มีแจ้งเตือนไปยังผู้บังคับบัญชา) #969 // send noti + inbox + mail // 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 = profile.CommanderId == Guid.Empty || profile.CommanderId == null ? Guid.Parse("08dbca3a-8b6a-4a4e-8b23-1f62e4f30ef6") : profile.CommanderId.Value, Payload = "", }; _appContext.Set().Add(inbox); var noti = new Notification { Body = body_str, ReceiverUserId = profile.CommanderId == Guid.Empty || profile.CommanderId == null ? Guid.Parse("08dbca3a-8b6a-4a4e-8b23-1f62e4f30ef6") : profile.CommanderId.Value, 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; } } public async Task> GetAdditionalCheckRequestsByAdminRole(int year, int month, string role, string nodeId, int? node, string? keyword) { try { var data = await _dbContext.Set().AsQueryable() .Where(x => (x.CheckDate.Year == year && x.CheckDate.Month == month)) .OrderByDescending(x => x.CreatedAt.Date) .ToListAsync(); if (!string.IsNullOrEmpty(keyword)) { data = data.Where(x => ( (x.Prefix ?? "") + (x.FirstName ?? "") + " " + (x.LastName ?? "")).Contains(keyword) || x.Description.Contains(keyword) ).ToList(); } if (role == "OWNER") { node = null; } if (role == "OWNER" || role == "CHILD") { data = data .Where(x => node == 4 ? x.Child4DnaId == Guid.Parse(nodeId!) : (node == 3 ? x.Child3DnaId == Guid.Parse(nodeId!) : (node == 2 ? x.Child2DnaId == Guid.Parse(nodeId!) : (node == 1 ? x.Child1DnaId == Guid.Parse(nodeId!) : (node == 0 ? x.RootDnaId == Guid.Parse(nodeId!) : (node == null ? true : true)))))) .ToList(); } else if (role == "BROTHER") { data = data .Where(x => node == 4 ? x.Child3DnaId == Guid.Parse(nodeId!) : (node == 3 ? x.Child2DnaId == Guid.Parse(nodeId!) : (node == 2 ? x.Child1DnaId == Guid.Parse(nodeId!) : (node == 1 || node == 0 ? x.RootDnaId == Guid.Parse(nodeId!) : (node == null ? true : true))))) .ToList(); } else if (role == "ROOT") { data = data .Where(x => x.RootDnaId == Guid.Parse(nodeId!)).ToList(); } else if (role == "PARENT") { data = data .Where(x => x.RootDnaId == Guid.Parse(nodeId!) && x.Child1DnaId != null && x.Child1DnaId != Guid.Empty).ToList(); } else if (role == "NORMAL") { data = data.Where(x => node == 0 ? x.RootDnaId == Guid.Parse(nodeId!) && (x.Child1DnaId == Guid.Empty || x.Child1DnaId == null) : node == 1 ? x.Child1DnaId == Guid.Parse(nodeId!) && (x.Child2DnaId == Guid.Empty || x.Child2DnaId == null) : node == 2 ? x.Child2DnaId == Guid.Parse(nodeId!) && (x.Child3DnaId == Guid.Empty || x.Child3DnaId == null) : node == 3 ? x.Child3DnaId == Guid.Parse(nodeId!) && (x.Child4DnaId == Guid.Empty || x.Child4DnaId == null) : node == 4 ? x.Child4DnaId == Guid.Parse(nodeId!) : true ).ToList(); } return data; } catch { throw; } } #endregion } }