hrms-api-backend/BMA.EHR.Application/Repositories/Leaves/TimeAttendants/AdditionalCheckRequestRepository.cs
Suphonchai Phoonsawat ee4ef41547 แก้ defect
add send noti when create ลงเวลากรณีพิเศษ
2024-01-17 12:42:19 +07:00

157 lines
6 KiB
C#

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.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<Guid, AdditionalCheckRequest>
{
#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<AdditionalCheckRequest> AddAsync(AdditionalCheckRequest entity)
{
await base.AddAsync(entity);
var userId = UserId != null ? Guid.Parse(UserId) : Guid.Empty;
var profile = await _userProfileRepository.GetProfileByKeycloakIdAsync(userId);
var profile_id = profile == null ? Guid.Empty : profile.Id;
var rootOc = _userProfileRepository.GetRootOcId(profile_id);
var approver = string.Empty;
var list = new List<OrganizationApproverResponse>();
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.Name}{profile.FirstName} {profile.LastName}";
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,
Payload = "",
};
_appContext.Set<Inbox>().Add(inbox);
var noti = new Notification
{
Body = body_str,
ReceiverUserId = appr.Id,
Type = "",
Payload = "",
};
_appContext.Set<Notification>().Add(noti);
await _appContext.SaveChangesAsync();
}
return entity;
}
public async Task<List<AdditionalCheckRequest>> GetAdditionalCheckRequestsByUserId(Guid keycloakId, int year, int month)
{
try
{
var data = await _dbContext.Set<AdditionalCheckRequest>().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<List<AdditionalCheckRequest>> GetAdditionalCheckRequests(int year, int month)
{
try
{
var data = await _dbContext.Set<AdditionalCheckRequest>().AsQueryable()
.Where(x => (x.CheckDate.Year == year && x.CheckDate.Month == month))
.OrderByDescending(x => x.CreatedAt.Date)
.ToListAsync();
return data;
}
catch
{
throw;
}
}
#endregion
}
}