HRM-10/HRM.UI/Controllers/Ess/EssController.cs

336 lines
12 KiB
C#
Raw Permalink Normal View History

2026-11-03 13:06:00 +06:00
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using HRM.BO;
using HRM.BO.ess;
using Microsoft.AspNetCore.Authorization;
using static iTextSharp.text.pdf.AcroFields;
using HRM.DA.Fund;
using System.Data;
using HRM.Report;
using Ease.Core.DataAccess;
using Ease.Core.Utility;
namespace HRM.UI.Controllers.Ess
{
[Route("api/Ess")]
[ApiController]
[Authorize]
public class EssController : ControllerBase
{
private readonly IProfileUpdateRequestService _profileUpdateRequestService;
private readonly IInstitutionService _institutionService;
private readonly ISearchEmployeeService _searchEmployeeService;
private readonly IWebHostEnvironment _env;
private readonly ICacheManager _cashManager;
public EssController(IProfileUpdateRequestService profileUpdateRequestService,
IInstitutionService institutionService,
ISearchEmployeeService searchEmployeeService,
IWebHostEnvironment env,
ICacheManager cashManager)
{
this._profileUpdateRequestService = profileUpdateRequestService;
_institutionService = institutionService;
_searchEmployeeService = searchEmployeeService;
_env = env;
_cashManager = cashManager;
}
[HttpPost]
[Route("savePUR")]
public ActionResult SaveEmployee(ProfileUpdateRequest item)
{
int ans;
CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User);
item.RequestEmpID = currentUser.EmployeeID.GetValueOrDefault();
try
{
if (item.NomineeNominationPurposeId != null)
{
int Percentage = _profileUpdateRequestService.GetNomineePercent(item.NomineeNominationPurposeId ?? 0, 1);
if (Percentage + item.NomineePercent > 100)
{
return BadRequest("Percentage is exceed the limit");
}
}
if(item.PURequestType == EnumPURequestType.Education)
{
if(item.AcademicInstitute == "")
{
item.AcademicInstitute = _institutionService.Get(item.AcademicUniversityBoardID ?? 0).Name;
}
}
ans = _profileUpdateRequestService.Save(item);
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
return Ok(ans);
}
[HttpPost]
[Route("updateEmployee")]
public ActionResult UpdateEmployee(ProfileUpdateRequest item)
{
try
{
CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User);
item.currentUserId = currentUser.UserID;
item.CreatedBy = currentUser.UserID;
if (item.NomineeNominationPurposeId != null)
{
int Percentage = _profileUpdateRequestService.GetNomineePercent(item.NomineeNominationPurposeId ?? 0, 1);
if ((Percentage + item.NomineePercent) > 100)
{
return BadRequest("Percentage is exceed the limit");
}
}
ApproveRequest(item);
ClearDashboardCache(item.RequestEmpID);
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
return Ok();
}
[HttpPost]
[Route("updateEmployeeRequestBulk")]
public ActionResult UpdateEmployeeRequestBulk(List<ProfileUpdateRequest> items)
{
try
{
CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User);
items.ForEach(x =>
{
x.currentUserId = currentUser.UserID;
ApproveRequest(x);
});
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
return Ok();
}
[HttpPost]
[Route("rejectEmployeeRequestBulk")]
public ActionResult RejectEmployeeRequestBulk(List<ProfileUpdateRequest> items)
{
try
{
CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User);
int currentUserId = currentUser.UserID;
_profileUpdateRequestService.RejectEmployeeRequestBulk(items, currentUserId, DateTime.Now);
items.ForEach(DiscardProfilePicture);
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
return Ok();
}
[HttpGet("getAllPURByEmpId")]
public ActionResult GetAllPURByEmpId()
{
List<ProfileUpdateRequest> items = new List<ProfileUpdateRequest>();
CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User);
int empId = currentUser.EmployeeID.GetValueOrDefault();
try
{
items = _profileUpdateRequestService.GetAllByEmpId(empId);
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
return Ok(items);
}
[HttpGet("getPURByEmpIdAndStatus/{status}")]
public ActionResult GetPURByEmpIdAndStatus(EnumPURequestStatus status)
{
CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User);
int empId = currentUser.EmployeeID.GetValueOrDefault();
int ans;
try
{
ans = _profileUpdateRequestService.GetPURCountEmpIdAndStatus(empId, status);
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
return Ok(ans);
}
[HttpGet("getAllPUR")]
public ActionResult GetAllPUR()
{
List<ProfileUpdateRequest> items = new List<ProfileUpdateRequest>();
try
{
items = _profileUpdateRequestService.GetAll();
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
return Ok(items);
}
[HttpPost]
[Route("updateStatusToRejected")]
public ActionResult UpdateStatusToRejected(ProfileUpdateRequest item)
{
try
{
CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User);
item.currentUserId = currentUser.UserID;
_profileUpdateRequestService.UpdateStatusToRejected(item);
DiscardProfilePicture(item);
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
return Ok();
}
/// <summary>
/// Employee/getEmployeeForDashboard caches each employee's dashboard row (incl.
/// profile-completion percent). Approving a request changes that data, so drop the
/// entry and let the next dashboard load re-read it from the DB.
/// </summary>
private void ClearDashboardCache(int employeeId)
{
_cashManager.Remove(enumMemoryCashType.LandingPageEmployee, enumMemoryCashNature.EmployeeWise, employeeId);
}
/// <summary>
/// Approves one request. Everything that touches the database — the request's own
/// changes, its status, the profile percentage, the notification and, for a picture
/// request, the photo row — goes in through a single service call, so it all commits
/// together or not at all. The photo FILE is moved only after that call returns,
/// because a file move cannot be rolled back with the transaction.
/// </summary>
private void ApproveRequest(ProfileUpdateRequest item)
{
string employeeNo;
empFileupload approvedPicture = ReadSubmittedPicture(item, out employeeNo);
_profileUpdateRequestService.UpdateEmployee(item, approvedPicture);
if (approvedPicture != null)
MoveApprovedPictureIntoPlace(employeeNo);
}
/// <summary>
/// The picture an employee submitted, read out of the pending folder so it can be saved
/// inside the approval's transaction. Returns null for anything that is not a picture
/// request, and for picture requests raised before pictures moved to the folder — those
/// still have their bytes in empFileuploadUpdateRequest and the service promotes them.
///
/// It carries the original upload rather than the 100px thumbnail: this row is what the
/// thumbnail is rebuilt from whenever the photo folder is empty (a fresh deploy, a new
/// environment).
///
/// referenceID is the employee id, which is what every photo reader asks for
/// (GetuploadEmpFile(empId, empId, Profile_Picture)) and what the core-HR upload writes.
/// The older approval path used 0 here, which no screen ever looked up.
/// </summary>
private empFileupload ReadSubmittedPicture(ProfileUpdateRequest item, out string employeeNo)
{
employeeNo = null;
if (item == null || item.PURequestType != EnumPURequestType.Picture)
return null;
SearchEmployee employee = _searchEmployeeService.get(item.RequestEmpID);
if (employee == null || string.IsNullOrWhiteSpace(employee.EmployeeNo))
return null;
var pending = new EmployeePhotoService(_env).GetPending(employee.EmployeeNo);
if (pending == null)
return null;
employeeNo = employee.EmployeeNo;
string extension = Path.GetExtension(pending.Value.FileName);
if (string.IsNullOrWhiteSpace(extension))
extension = ".jpg";
return new empFileupload
{
employeeID = item.RequestEmpID,
referenceID = item.RequestEmpID,
filetype = enumEmpFileUploadType.photo,
FileFormat = EnumFileFormat.Picture,
fileName = "Profile Picture" + extension,
fileData = pending.Value.Bytes
};
}
/// <summary>
/// Moves the approved picture from the pending folder into the one every read path
/// serves from. Runs after the transaction has committed, so a failure here cannot undo
/// the approval — and does not need to: the photo is already on the employee's record,
/// and the next read of it rebuilds the thumbnail from there.
/// </summary>
private void MoveApprovedPictureIntoPlace(string employeeNo)
{
if (string.IsNullOrWhiteSpace(employeeNo))
return;
var photoService = new EmployeePhotoService(_env);
try
{
photoService.ApprovePending(employeeNo);
}
catch (Exception e)
{
ExceptionLog.Write(e);
// Don't leave it sitting in the queue as though it were still awaiting approval.
photoService.DeletePending(employeeNo);
}
}
/// <summary>
/// Discards a rejected profile picture so it is not left behind in the pending folder.
/// </summary>
private void DiscardProfilePicture(ProfileUpdateRequest item)
{
if (item == null || item.PURequestType != EnumPURequestType.Picture)
return;
SearchEmployee employee = _searchEmployeeService.get(item.RequestEmpID);
if (employee == null || string.IsNullOrWhiteSpace(employee.EmployeeNo))
return;
new EmployeePhotoService(_env).DeletePending(employee.EmployeeNo);
}
}
}