394 lines
15 KiB
C#
394 lines
15 KiB
C#
using LCU.API.Internal.Operations.IncentiveApiLib.Persistence;
|
|
using LCU.API.Internal.Operations.IncentiveApi.Services;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.FeatureManagement.Mvc;
|
|
using LCU.API.Internal.Operations.IncentiveApiLib.Services;
|
|
using LCU.API.Internal.Operations.IncentiveApi.Services.Allied;
|
|
using LCU.API.Internal.Operations.IncentiveApi.Services.Action;
|
|
using LCU.API.Internal.Operations.IncentiveApi.Models.Action;
|
|
using LCU.Library.Internal.IncentiveApiModels.Common;
|
|
using LCU.Library.Internal.IncentiveApiModels.Requests;
|
|
using LCU.API.Internal.Operations.IncentiveApi.Configuration;
|
|
using LCU.API.Internal.Operations.IncentiveApi.Models.Allied;
|
|
using LCU.API.Internal.Operations.IncentiveApi.Services.QueueServices;
|
|
using LCU.Library.Internal.Exceptions;
|
|
using System.Threading;
|
|
|
|
namespace LCU.API.Internal.Operations.IncentiveApi.Controllers;
|
|
|
|
[FeatureGate(FeatureFlags.EnableControllers)]
|
|
[ApiController]
|
|
[Route("api/[controller]")]
|
|
public class ProcessingController : Controller
|
|
{
|
|
private readonly IActionCreditCardImportService _actionCreditCardImportService;
|
|
private readonly IActionVehicleImportService _actionVehicleImportService;
|
|
private readonly IAlliedGapMbpTransactionImportService _alliedTransactionImportService;
|
|
private readonly IApplicationDbIntegrityService _applicationDbIntegrityService;
|
|
private readonly IDnaActivitySyncService _dnaActivitySyncService;
|
|
private readonly IUserService _userService;
|
|
private readonly ICurrentUserService _currentUser;
|
|
private readonly IGapInsuranceActivityService _gapInsuranceActivityService;
|
|
private readonly IQueueIncentiveProcessesService _queueIncentiveProcessService;
|
|
private readonly IQueueNightlyIncentiveProcessesService _queueNightlyIncentiveProcessService;
|
|
private readonly IQueueReportingService _queueReportingService;
|
|
private readonly IQueuePayoutIncentiveService _queuePayoutIncentiveService;
|
|
private readonly IQueueGoalDispersalService _queueGoalDispersalService;
|
|
private readonly IQueueDayforceSyncService _queueDayforceSyncService;
|
|
|
|
public ProcessingController(
|
|
IActionCreditCardImportService actionCreditCardImportService,
|
|
IActionVehicleImportService actionVehicleImportService,
|
|
IAlliedGapMbpTransactionImportService alliedTransactionImportService,
|
|
IApplicationDbIntegrityService applicationDbIntegrityService,
|
|
ICurrentUserService currentUserService,
|
|
IDnaActivitySyncService dnaActivitySyncService,
|
|
IUserService userService,
|
|
IGapInsuranceActivityService gapInsuranceActivityService,
|
|
IQueueIncentiveProcessesService queueIncentiveProcessService,
|
|
IQueueNightlyIncentiveProcessesService queueNightlyIncentiveProcessService,
|
|
IQueueReportingService queueReportingService, IQueuePayoutIncentiveService queuePayoutIncentiveService,
|
|
IQueueGoalDispersalService queueGoalDispersalService, IQueueDayforceSyncService queueDayforceSyncService
|
|
)
|
|
{
|
|
_actionCreditCardImportService = actionCreditCardImportService;
|
|
_actionVehicleImportService = actionVehicleImportService;
|
|
_alliedTransactionImportService = alliedTransactionImportService;
|
|
_applicationDbIntegrityService = applicationDbIntegrityService;
|
|
_currentUser = currentUserService;
|
|
_dnaActivitySyncService = dnaActivitySyncService;
|
|
_userService = userService;
|
|
_gapInsuranceActivityService = gapInsuranceActivityService;
|
|
_queueIncentiveProcessService = queueIncentiveProcessService;
|
|
_queueNightlyIncentiveProcessService = queueNightlyIncentiveProcessService;
|
|
_queueReportingService = queueReportingService;
|
|
_queuePayoutIncentiveService = queuePayoutIncentiveService;
|
|
_queueGoalDispersalService = queueGoalDispersalService;
|
|
_queueDayforceSyncService = queueDayforceSyncService;
|
|
}
|
|
|
|
[HttpPost("QueueIncentiveProcess")]
|
|
[ProducesResponseType(202)]
|
|
[ProducesResponseType(400)]
|
|
[ProducesResponseType(500)]
|
|
public async Task<IActionResult> QueueIncentiveProcess([FromBody] IncentiveProcessRequest request)
|
|
{
|
|
try
|
|
{
|
|
await _queueIncentiveProcessService.QueueIncentiveProcess(request);
|
|
return Accepted();
|
|
}
|
|
catch (ServiceException exception)
|
|
{
|
|
return StatusCode(StatusCodes.Status500InternalServerError, exception.Message);
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
Log.Error(exception, "Incentive Process Unexpected Exception");
|
|
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
}
|
|
}
|
|
|
|
[HttpPost("QueueNightlyIncentiveProcess")]
|
|
[ProducesResponseType(202)]
|
|
[ProducesResponseType(400)]
|
|
[ProducesResponseType(500)]
|
|
public async Task<IActionResult> QueueNightlyIncentiveProcess([FromBody] NightlyIncentiveProcessRequest request)
|
|
{
|
|
try
|
|
{
|
|
await _queueNightlyIncentiveProcessService.QueueNightlyIncentiveProcess(request);
|
|
return Accepted();
|
|
}
|
|
catch (ServiceException exception)
|
|
{
|
|
return StatusCode(StatusCodes.Status500InternalServerError, exception.Message);
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
Log.Error(exception, "Nightly Incentive Process Unexpected Exception");
|
|
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
}
|
|
}
|
|
|
|
[HttpPost("QueuePayoutProcess")]
|
|
[ProducesResponseType(202)]
|
|
[ProducesResponseType(400)]
|
|
[ProducesResponseType(500)]
|
|
public async Task<IActionResult> QueuePayoutProcess([FromBody] PayoutIncentiveRequest request)
|
|
{
|
|
try
|
|
{
|
|
await _queuePayoutIncentiveService.QueuePayoutIncentiveProcess(request);
|
|
return Accepted();
|
|
}
|
|
catch (ServiceException exception)
|
|
{
|
|
return StatusCode(StatusCodes.Status500InternalServerError, exception.Message);
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
Log.Error(exception, "Unexpected Exception");
|
|
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
}
|
|
}
|
|
|
|
[HttpPost("QueueReportingRequest")]
|
|
[ProducesResponseType(202)]
|
|
[ProducesResponseType(400)]
|
|
[ProducesResponseType(500)]
|
|
public async Task<IActionResult> QueueReportingRequest([FromBody] ReportingRequest request)
|
|
{
|
|
try
|
|
{
|
|
await _queueReportingService.QueueReportingProcess(request);
|
|
return Accepted();
|
|
}
|
|
catch (ServiceException exception)
|
|
{
|
|
return StatusCode(StatusCodes.Status500InternalServerError, exception.Message);
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
Log.Error(exception, "Unexpected Exception");
|
|
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
}
|
|
}
|
|
|
|
[HttpPost("QueueGoalDispersalSyncProcess")]
|
|
[ProducesResponseType(202)]
|
|
[ProducesResponseType(400)]
|
|
[ProducesResponseType(500)]
|
|
public async Task<IActionResult> QueueGoalDispersalSyncProcess([FromBody] GoalDispersalRequest request)
|
|
{
|
|
try
|
|
{
|
|
await _queueGoalDispersalService.QueueGoalDispersalProcess(request);
|
|
return Accepted();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Information("Queue Goal Dispersal Process has failed");
|
|
Log.Error(ex, $"Failure in {GetType().Name}");
|
|
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
|
|
}
|
|
}
|
|
|
|
[HttpPost("QueueDayforceSyncProcess")]
|
|
[ProducesResponseType(202)]
|
|
[ProducesResponseType(400)]
|
|
[ProducesResponseType(500)]
|
|
public async Task<IActionResult> QueueDayforceSyncProcess([FromBody] DayforceProcessRequest request)
|
|
{
|
|
try
|
|
{
|
|
await _queueDayforceSyncService.QueueDayforceSyncProcess(request);
|
|
return Accepted();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Information("DayForce forced Sync has failed");
|
|
Log.Error(ex, $"Failure in {GetType().Name}");
|
|
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
|
|
}
|
|
}
|
|
|
|
[HttpGet("RunAlliedImport")]
|
|
[Obsolete("Replaced with ActivityImport process, please use endpoint ActivityImport/QueueAlliedGapMbpRequest Date: 1/1/2025")]
|
|
public async Task<IActionResult> RunAlliedImport([FromBody] AlliedGapMbpRequest request)
|
|
{
|
|
try
|
|
{
|
|
var appUser = _currentUser.GetCurrentUser();
|
|
|
|
if (appUser?.Email == null)
|
|
{
|
|
return Unauthorized("No AD User Found");
|
|
}
|
|
|
|
var employee = await _userService.GetUserByAdEmailAsync(appUser.Email);
|
|
if (employee == null)
|
|
{
|
|
Log.Information("No AppUser found");
|
|
return NoContent();
|
|
}
|
|
|
|
Log.Information($"Importing transactions from Allied, kicked off manually");
|
|
var imported = await _alliedTransactionImportService.ImportTransactions<AlliedSaleRow>(request);
|
|
Log.Information($"Imported {imported.Count} transactions from Allied");
|
|
|
|
return Ok();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, $"Failure in {GetType().Name}");
|
|
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
}
|
|
}
|
|
|
|
[HttpGet("RunCreditCardImport")]
|
|
[Obsolete("Replaced with ActivityImport process, please use endpoint ActivityImport/QueueActionCreditCardRequest Date: 1/1/2025")]
|
|
public async Task<IActionResult> RunCreditCardImport(ActionCreditCardRequest request)
|
|
{
|
|
try
|
|
{
|
|
var appUser = _currentUser.GetCurrentUser();
|
|
|
|
if (appUser?.Email == null)
|
|
{
|
|
return Unauthorized("No AD User Found");
|
|
}
|
|
|
|
var employee = await _userService.GetUserByAdEmailAsync(appUser.Email);
|
|
if (employee == null)
|
|
{
|
|
return NoContent();
|
|
}
|
|
|
|
Log.Information($"Importing credit cards, kicked off manually");
|
|
var imported = await _actionCreditCardImportService.ImportTransactions<ActionCreditCardPersonalRow>("personal", request);
|
|
Log.Information($"Imported {imported.Count} credit cards - personal");
|
|
imported = await _actionCreditCardImportService.ImportTransactions<ActionCreditCardBusinessRow>("business", request);
|
|
Log.Information($"Imported {imported.Count} credit cards - business");
|
|
|
|
return Ok();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, $"Failure in {GetType().Name}");
|
|
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
}
|
|
}
|
|
|
|
[HttpGet("RunVehicleImport")]
|
|
[Obsolete("Replaced with ActivityImport process, please use endpoint ActivityImport/QueueActionVehicleRequest Date: 1/1/2025")]
|
|
public async Task<IActionResult> RunVehicleImport([FromBody] ActionVehicleRequest request)
|
|
{
|
|
try
|
|
{
|
|
var appUser = _currentUser.GetCurrentUser();
|
|
|
|
if (appUser?.Email == null)
|
|
{
|
|
return Unauthorized("No AD User Found");
|
|
}
|
|
|
|
var employee = await _userService.GetUserByAdEmailAsync(appUser.Email);
|
|
if (employee == null)
|
|
{
|
|
return NoContent();
|
|
}
|
|
|
|
Log.Information($"Importing vehicles, kicked off manually");
|
|
var imported = await _actionVehicleImportService.ImportTransactions<ActionSaleRow>(request);
|
|
Log.Information($"Imported {imported.Count} vehicles");
|
|
|
|
return Ok();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, $"Failure in {GetType().Name}");
|
|
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
}
|
|
}
|
|
|
|
[HttpGet("RunDnaSyncQuery")]
|
|
[Obsolete("Replaced with Incentive Processes, please use QueueIncentiveProcesses. Date: 1/1/2025")]
|
|
public async Task<IActionResult> RunDnaSyncQuery()
|
|
{
|
|
try
|
|
{
|
|
var appUser = _currentUser.GetCurrentUser();
|
|
|
|
if (appUser?.Email == null)
|
|
{
|
|
return Unauthorized("No AD User Found");
|
|
}
|
|
|
|
var employee = await _userService.GetUserByAdEmailAsync(appUser.Email);
|
|
if (employee == null)
|
|
{
|
|
return NoContent();
|
|
}
|
|
|
|
Log.Information("DNA Sync was forced");
|
|
await _dnaActivitySyncService.SyncLatestActivitiesFromDna();
|
|
return Ok();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, $"Failure in {GetType().Name}");
|
|
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
}
|
|
}
|
|
|
|
[HttpGet("MatchOrRemoveGapAccessories")]
|
|
[Obsolete("Moved to Nightly Processes Controller. Please use QueueNightlyIncentiveProcesses. Date: 1/1/2025")]
|
|
public async Task<IActionResult> MatchOrRemoveGapAccessories(
|
|
[FromQuery] int fromMonth,
|
|
[FromQuery] int fromYear,
|
|
[FromQuery] int toMonth,
|
|
[FromQuery] int toYear
|
|
)
|
|
{
|
|
try
|
|
{
|
|
var appUser = _currentUser.GetCurrentUser();
|
|
|
|
if (appUser?.Email == null)
|
|
{
|
|
return Unauthorized("No AD User Found");
|
|
}
|
|
|
|
var employee = await _userService.GetUserByAdEmailAsync(appUser.Email);
|
|
if (employee == null)
|
|
{
|
|
return NoContent();
|
|
}
|
|
|
|
var monthRange = new MonthRange(fromYear, fromMonth, toYear, toMonth);
|
|
Log.Information("MatchOrRemoveGapAccessories was forced");
|
|
var updatedCount = await _gapInsuranceActivityService.MatchOrRemoveGapAccessories(monthRange);
|
|
Log.Information($"MatchOrRemoveGapAccessories updated {updatedCount} records");
|
|
return Ok();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, $"Failure in {GetType().Name}");
|
|
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
}
|
|
}
|
|
|
|
[HttpGet("RunAppUserIdIntegrityMaintenance")]
|
|
[Obsolete("Moved to Nightly Processes Controller. Please use QueueNightlyIncentiveProcesses. Date: 1/1/2025")]
|
|
public async Task<IActionResult> RunAppUserIdIntegrityMaintenance()
|
|
{
|
|
try
|
|
{
|
|
var appUser = _currentUser.GetCurrentUser();
|
|
|
|
if (appUser?.Email == null)
|
|
{
|
|
return Unauthorized("No AD User Found");
|
|
}
|
|
|
|
var employee = await _userService.GetUserByAdEmailAsync(appUser.Email);
|
|
if (employee == null)
|
|
{
|
|
return NoContent();
|
|
}
|
|
|
|
Log.Information("AppUserId Integrity Maintenance was forced");
|
|
var token = new CancellationToken();
|
|
await _applicationDbIntegrityService.AppUserIdIntegrityMaintenanceSucceeded(token);
|
|
return Ok();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, $"Failure in {GetType().Name}");
|
|
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
}
|
|
}
|
|
}
|