Add New API and Correct Reopen Issue

This commit is contained in:
Suphonchai Phoonsawat 2023-12-08 10:44:42 +07:00
parent 84e656b0c8
commit 3b054f21cc
13 changed files with 2678 additions and 4 deletions

View file

@ -3,6 +3,8 @@ using BMA.EHR.Application.Common.Interfaces;
using BMA.EHR.Application.Messaging;
using BMA.EHR.Domain.Models.Leave.Commons;
using BMA.EHR.Domain.Models.Leave.Requests;
using BMA.EHR.Domain.Models.Notifications;
using BMA.EHR.Domain.Shared;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Query;
@ -194,6 +196,240 @@ namespace BMA.EHR.Application.Repositories.Leaves.LeaveRequests
return await rawData.ToListAsync();
}
public async Task ApproveCancelLeaveRequestAsync(Guid id, string Reason)
{
var rawData = await GetByIdAsync(id);
if (rawData == null)
{
throw new Exception(GlobalMessages.DataNotFound);
}
var profile = await _userProfileRepository.GetProfileByKeycloakIdAsync(rawData.KeycloakUserId);
if (profile == null)
{
throw new Exception(GlobalMessages.DataNotFound);
}
rawData.LeaveCancelStatus = "APPROVE";
rawData.LeaveCancelComment = Reason;
await UpdateAsync(rawData);
// TODO: remove วันลา
// Send Noti
var noti = new Notification
{
Body = $"การขอยกเลิกใบลาของคุณได้รับการอนุมัติ",
ReceiverUserId = profile.Id,
Type = "",
Payload = "",
};
_dbContext.Set<Notification>().Add(noti);
await _dbContext.SaveChangesAsync();
}
public async Task RejectCancelLeaveRequestAsync(Guid id, string Reason)
{
var rawData = await GetByIdAsync(id);
if (rawData == null)
{
throw new Exception(GlobalMessages.DataNotFound);
}
var profile = await _userProfileRepository.GetProfileByKeycloakIdAsync(rawData.KeycloakUserId);
if (profile == null)
{
throw new Exception(GlobalMessages.DataNotFound);
}
rawData.LeaveCancelStatus = "REJECT";
rawData.LeaveCancelComment = Reason;
await UpdateAsync(rawData);
// TODO: remove วันลา
// Send Noti
var noti = new Notification
{
Body = $"การขอยกเลิกใบลาของคุณไม่ได้รับการอนุมัติ \r\nเนืองจาก {Reason}",
ReceiverUserId = profile.Id,
Type = "",
Payload = "",
};
_dbContext.Set<Notification>().Add(noti);
await _dbContext.SaveChangesAsync();
}
public async Task OfficerApproveLeaveRequest(Guid id)
{
var rawData = await GetByIdAsync(id);
if (rawData == null)
{
throw new Exception(GlobalMessages.DataNotFound);
}
rawData.LeaveStatus = "PENDING";
rawData.ApproveStep = "st1";
await UpdateAsync(rawData);
}
public async Task CommanderApproveLeaveRequest(Guid id, string reason)
{
var rawData = await GetByIdAsync(id);
if (rawData == null)
{
throw new Exception(GlobalMessages.DataNotFound);
}
if (rawData.ApproveStep != "st1")
{
throw new Exception("คำขอนี้ยังไม่ได้รับการอนุมัติจากเจ้าหน้าที่ ไม่สามารถทำรายการได้");
}
rawData.LeaveStatus = "PENDING";
rawData.LeaveComment = reason;
rawData.ApproveStep = "st2";
await UpdateAsync(rawData);
}
public async Task ApproveLeaveRequest(Guid id, string reason)
{
var rawData = await GetByIdAsync(id);
if (rawData == null)
{
throw new Exception(GlobalMessages.DataNotFound);
}
if (rawData.ApproveStep != "st2")
{
throw new Exception("คำขอนี้ยังไม่ได้รับการอนุมัติจากผู้บังคับบัญชา ไม่สามารถทำรายการได้");
}
var profile = await _userProfileRepository.GetProfileByKeycloakIdAsync(rawData.KeycloakUserId);
if (profile == null)
{
throw new Exception(GlobalMessages.DataNotFound);
}
rawData.LeaveStatus = "APPROVE";
rawData.LeaveDirectorComment = reason;
rawData.ApproveStep = "st2";
await UpdateAsync(rawData);
// Send Noti
var noti = new Notification
{
Body = $"การขอลาของคุณได้รับการอนุมัติ",
ReceiverUserId = profile.Id,
Type = "",
Payload = "",
};
_dbContext.Set<Notification>().Add(noti);
await _dbContext.SaveChangesAsync();
}
public async Task RejectLeaveRequest(Guid id, string reason)
{
var rawData = await GetByIdAsync(id);
if (rawData == null)
{
throw new Exception(GlobalMessages.DataNotFound);
}
if (rawData.ApproveStep != "st2")
{
throw new Exception("คำขอนี้ยังไม่ได้รับการอนุมัติจากผู้บังคับบัญชา ไม่สามารถทำรายการได้");
}
var profile = await _userProfileRepository.GetProfileByKeycloakIdAsync(rawData.KeycloakUserId);
if (profile == null)
{
throw new Exception(GlobalMessages.DataNotFound);
}
rawData.LeaveStatus = "REJECT";
rawData.LeaveDirectorComment = reason;
rawData.ApproveStep = "st2";
await UpdateAsync(rawData);
// Send Noti
var noti = new Notification
{
Body = $"การขอลาของคุณไม่ได้รับการอนุมัติ \r\nเนื่องจาก{reason}",
ReceiverUserId = profile.Id,
Type = "",
Payload = "",
};
_dbContext.Set<Notification>().Add(noti);
await _dbContext.SaveChangesAsync();
}
public async Task<int> GetSumSendLeaveByTypeForUserAsync(Guid keycloakUserId, Guid leaveTypeId, int year)
{
var data = await _dbContext.Set<LeaveRequest>().AsQueryable()
.Include(x => x.Type)
.Where(x => x.KeycloakUserId == keycloakUserId)
.Where(x => x.Type.Id == leaveTypeId)
.Where(x => x.LeaveStartDate.Year == year)
.ToListAsync();
return data.Sum(x => x.LeaveTotal);
}
public async Task<int> GetSumApproveLeaveByTypeForUserAsync(Guid keycloakUserId, Guid leaveTypeId, int year)
{
var data = await _dbContext.Set<LeaveRequest>().AsQueryable()
.Include(x => x.Type)
.Where(x => x.KeycloakUserId == keycloakUserId)
.Where(x => x.Type.Id == leaveTypeId)
.Where(x => x.LeaveStartDate.Year == year)
.Where(x => x.LeaveStatus == "APPROVE")
.ToListAsync();
if (data.Count > 0)
return data.Sum(x => x.LeaveTotal);
else
return 0;
}
public async Task<int> GetSumRejectLeaveByTypeForUserAsync(Guid keycloakUserId, Guid leaveTypeId, int year)
{
var data = await _dbContext.Set<LeaveRequest>().AsQueryable()
.Include(x => x.Type)
.Where(x => x.KeycloakUserId == keycloakUserId)
.Where(x => x.Type.Id == leaveTypeId)
.Where(x => x.LeaveStartDate.Year == year)
.Where(x => x.LeaveStatus == "REJECT")
.ToListAsync();
if (data.Count > 0)
return data.Sum(x => x.LeaveTotal);
else
return 0;
}
public async Task<int> GetSumDeleteLeaveByTypeForUserAsync(Guid keycloakUserId, Guid leaveTypeId, int year)
{
var data = await _dbContext.Set<LeaveRequest>().AsQueryable()
.Include(x => x.Type)
.Where(x => x.KeycloakUserId == keycloakUserId)
.Where(x => x.Type.Id == leaveTypeId)
.Where(x => x.LeaveStartDate.Year == year)
.Where(x => x.LeaveStatus == "DELETE")
.ToListAsync();
if (data.Count > 0)
return data.Sum(x => x.LeaveTotal);
else
return 0;
}
#endregion
}
}