98 lines
3.7 KiB
C#
98 lines
3.7 KiB
C#
using LCU.API.Internal.Operations.IncentiveApi.Models.DataTransfer;
|
|
using LCU.API.Internal.Operations.IncentiveApi.Models.ExternalActivities;
|
|
using LCU.API.Internal.Operations.IncentiveApiLib.Services;
|
|
using LCU.API.Internal.Operations.IncentiveApiLib.Repositories;
|
|
using LCU.Library.Internal.IncentiveApiModels.Entities;
|
|
using LCU.Library.Internal.IncentiveApiModels.Enums;
|
|
using LCU.Library.Internal.IncentiveApiModels.Requests;
|
|
|
|
namespace LCU.API.Internal.Operations.IncentiveApi.Services.ExternalActivity;
|
|
|
|
public class NewDebtProtectionEligibleLoanStrategy : IExternalActivityStrategy
|
|
{
|
|
private readonly IUserService _userService;
|
|
private readonly IDepartmentRepository _departmentRepository;
|
|
|
|
public NewDebtProtectionEligibleLoanStrategy(IUserService userService, IDepartmentRepository departmentRepository)
|
|
{
|
|
_userService = userService;
|
|
_departmentRepository = departmentRepository;
|
|
}
|
|
|
|
public bool CanMapActivity(IExternalActivity activity)
|
|
{
|
|
return activity.GetActivityType() == ActivityType.Sale && activity.GetProductType() == Product.DebtProtectionEligibleLoan;
|
|
}
|
|
|
|
public async Task<Activity> MapToActivityManagerActivity(IExternalActivity activity)
|
|
{
|
|
AppUser appUser = null;
|
|
Department department = null;
|
|
try
|
|
{
|
|
if (activity is NewLoanSaleActivity newDpEligibleLoanSale)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(newDpEligibleLoanSale.EmployeeEmail))
|
|
{
|
|
appUser = await _userService.TryFindSingleUserByName(new UserNameSearchRequest()
|
|
{
|
|
FirstName = newDpEligibleLoanSale.AssociateFirst,
|
|
LastName = newDpEligibleLoanSale.AssociateLast,
|
|
MiddleInit = newDpEligibleLoanSale.AssociateMiddleInit,
|
|
DnaOrgNbr = newDpEligibleLoanSale.BranchOrgNbr.ToString()
|
|
|
|
});
|
|
}
|
|
else
|
|
{
|
|
appUser = await _userService.GetUserByAdEmailAsync(newDpEligibleLoanSale.EmployeeEmail);
|
|
}
|
|
|
|
if (appUser == null)
|
|
{
|
|
return activity.MapToActivity();
|
|
}
|
|
|
|
department = await _departmentRepository.FindUserHomeDepartment(appUser);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex,
|
|
$"Failed to get data for NewDPEligibleLoanSaleActivity with sourceId: {activity.MapToActivity().SourceId}. Falling back to basic mapping.");
|
|
}
|
|
|
|
var mappedActivity = activity.MapToActivity();
|
|
mappedActivity.AppUserId = appUser?.Id ?? Guid.Empty;
|
|
mappedActivity.EmployeeId = appUser?.EmployeeId;
|
|
mappedActivity.DepartmentId = department?.Id ?? -1;
|
|
return mappedActivity;
|
|
}
|
|
|
|
public async Task<AppUser> SearchForUser(UserSearchRequest userSearchRequest)
|
|
{
|
|
var foundUsers = await _userService.GetAllUsersAsync(userSearchRequest);
|
|
|
|
if (foundUsers.Count < 1 && !string.IsNullOrWhiteSpace(userSearchRequest.MiddleInit))
|
|
{
|
|
foundUsers = await _userService.GetAllUsersAsync(new UserSearchRequest()
|
|
{
|
|
FirstName = userSearchRequest.FirstName,
|
|
LastName = userSearchRequest.LastName,
|
|
DnaOrgNbr = userSearchRequest.DnaOrgNbr
|
|
});
|
|
}
|
|
|
|
if (foundUsers.Count < 1)
|
|
{
|
|
foundUsers = await _userService.GetAllUsersAsync(new UserSearchRequest()
|
|
{
|
|
FirstName = userSearchRequest.FirstName,
|
|
LastName = userSearchRequest.LastName,
|
|
});
|
|
}
|
|
|
|
return foundUsers.Single();
|
|
}
|
|
}
|