แก้ defect

add send noti when create ลงเวลากรณีพิเศษ
This commit is contained in:
Suphonchai Phoonsawat 2024-01-17 12:42:19 +07:00
parent bb637bca36
commit ee4ef41547
8 changed files with 144 additions and 12 deletions

View file

@ -1,6 +1,9 @@
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;
@ -18,6 +21,8 @@ namespace BMA.EHR.Application.Repositories.Leaves.TimeAttendants
private readonly UserProfileRepository _userProfileRepository;
private readonly IConfiguration _configuration;
private readonly EmailSenderService _emailSenderService;
private readonly IApplicationDBContext _appContext;
private readonly CommandRepository _commandRepository;
#endregion
@ -28,7 +33,9 @@ namespace BMA.EHR.Application.Repositories.Leaves.TimeAttendants
OrganizationCommonRepository organizationCommonRepository,
UserProfileRepository userProfileRepository,
IConfiguration configuration,
EmailSenderService emailSenderService) : base(dbContext, httpContextAccessor)
EmailSenderService emailSenderService,
IApplicationDBContext appContext,
CommandRepository commandRepository) : base(dbContext, httpContextAccessor)
{
_dbContext = dbContext;
_httpContextAccessor = httpContextAccessor;
@ -36,6 +43,8 @@ namespace BMA.EHR.Application.Repositories.Leaves.TimeAttendants
_userProfileRepository = userProfileRepository;
_configuration = configuration;
_emailSenderService = emailSenderService;
_appContext = appContext;
_commandRepository = commandRepository;
}
#endregion
@ -57,6 +66,57 @@ namespace BMA.EHR.Application.Repositories.Leaves.TimeAttendants
#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