Files
monorepo/LCU.API.Operations.IncentiveAPI/LCU.API.Internal.Operations.IncentiveApi/Controllers/SettingsController.cs
T
2026-03-17 09:53:09 -05:00

134 lines
4.7 KiB
C#

using LCU.API.Internal.Operations.IncentiveApi.Services;
using LCU.API.Internal.Operations.IncentiveApi.Models.DataTransfer;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.FeatureManagement.Mvc;
using LCU.API.Internal.Operations.IncentiveApi.Configuration;
using LCU.API.Internal.Operations.IncentiveApiLib.Services;
using LCU.API.Internal.Operations.IncentiveApi.Models;
using LCU.Library.Internal.IncentiveApiModels.Enums;
using LCU.API.Internal.Operations.IncentiveApiLib.Persistence;
namespace LCU.API.Internal.Operations.IncentiveApi.Controllers;
[ApiController]
[FeatureGate(FeatureFlags.EnableControllers)]
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
[Route("api/[controller]")]
public class SettingsController : ControllerBase
{
private readonly ICurrentUserService _currentUserService;
private readonly IDateTime _dateTimeService;
private readonly IDepartmentService _departmentService;
private readonly ISettingsService _settingsService;
private readonly IUserService _userService;
public SettingsController(
ICurrentUserService currentUserService,
IDateTime dateTimeService,
IDepartmentService departmentService,
ISettingsService settingsService,
IUserService userService
)
{
_currentUserService = currentUserService;
_dateTimeService = dateTimeService;
_departmentService = departmentService;
_settingsService = settingsService;
_userService = userService;
}
[HttpGet("")]
[ProducesResponseType(200)]
public async Task<IActionResult> Get()
{
try
{
var settings = new Settings();
settings.Departments = await _departmentService.GetAllDepartmentsAsync();
settings.Employees = await _userService.GetAllUserSummariesAsync();
settings.NextBonusPeriod = _dateTimeService.getNextBonusPeriod(DateTime.Today);
var currentUser = _currentUserService.GetCurrentUser();
if (currentUser?.Email != null && currentUser.UserType() != UserType.System)
{
var appUser = await _userService.GetUserByAdEmailAsync(currentUser.Email);
settings.AuthorizedReports = await _settingsService.GetAuthorizedReportSettings(appUser);
Log.Debug($"Returning Settings For AppUser:{appUser.FullName}");
return Ok(settings);
}
Log.Warning("No Authorized AppUser Found, Returning Base Settings");
return Ok(settings);
}
catch (Exception ex)
{
Log.Error(ex, $"Failure in {GetType().Name}");
return StatusCode((int)System.Net.HttpStatusCode.InternalServerError);
}
}
[HttpGet("GetAllRoleMappings", Name = "GetAllRoleMappings")]
public async Task<IActionResult> GetAllRoleMappings()
{
try
{
var result = await _settingsService.GetRoleMappingsAsync();
return Ok(result);
}
catch (Exception ex)
{
Log.Error(ex, $"Failure in {GetType().Name}");
return StatusCode(StatusCodes.Status500InternalServerError);
}
}
[HttpPost("UpdateRoleMapping", Name = "UpdateRoleMapping")]
public async Task<IActionResult> UpdateRoleMapping([FromBody] RoleMappingUpdateRequest request)
{
try
{
var currentUser = _currentUserService.GetCurrentUser();
if (currentUser?.Email == null)
{
return Unauthorized("No AD User Found.");
}
await _settingsService.SaveRoleMappingAsync(request);
return (Ok());
}
catch (Exception ex)
{
Log.Error(ex, $"Failure in {GetType().Name}");
return StatusCode(StatusCodes.Status500InternalServerError);
}
}
[HttpGet("GetAuthorizedReportSettings", Name = "GetAuthorizedReportSettings")]
public async Task<IActionResult> GetAuthorizedReports()
{
try
{
var currentUser = _currentUserService.GetCurrentUser();
if (currentUser?.Email == null)
{
return Unauthorized("No AD User Found.");
}
var appUser = await _userService.GetUserByAdEmailAsync(currentUser.Email);
var result = await _settingsService.GetAuthorizedReportSettings(appUser);
return Ok(result);
}
catch (Exception ex)
{
Log.Error(ex, $"Failure in {GetType().Name}");
return StatusCode(StatusCodes.Status500InternalServerError);
}
}
}