115 lines
4.3 KiB
C#
115 lines
4.3 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;
|
|
using LCU.API.Internal.Operations.IncentiveApi.Models.Constants;
|
|
|
|
namespace LCU.API.Internal.Operations.IncentiveApi.Services.ExternalActivity;
|
|
|
|
public class NewMortgageActivityStrategy : IExternalActivityStrategy
|
|
{
|
|
private readonly IUserService _userService;
|
|
private readonly IDepartmentRepository _departmentRepository;
|
|
|
|
public NewMortgageActivityStrategy(IUserService userService, IDepartmentRepository departmentRepository)
|
|
{
|
|
_userService = userService;
|
|
_departmentRepository = departmentRepository;
|
|
}
|
|
|
|
public bool CanMapActivity(IExternalActivity activity)
|
|
{
|
|
return
|
|
activity.GetActivityType() == ActivityType.Sale
|
|
&& (activity.GetProductType() == Product.Mortgage || activity.GetProductType() == Product.FirstMortgageReferral);
|
|
}
|
|
|
|
public async Task<Activity> MapToActivityManagerActivity(IExternalActivity activity)
|
|
{
|
|
AppUser appUser = null;
|
|
Department department = null;
|
|
|
|
try
|
|
{
|
|
if (activity is NewLoanSaleActivity newMortgageActivity)
|
|
{
|
|
if (IsFirstMortgageSale(newMortgageActivity))
|
|
{
|
|
appUser = await _userService.GetUserByAdEmailAsync(SystemAppUserConstants.FirstMortgageEmail);
|
|
}
|
|
else if (string.IsNullOrWhiteSpace(newMortgageActivity.EmployeeEmail))
|
|
{
|
|
appUser = await _userService.TryFindSingleUserByName(new UserNameSearchRequest()
|
|
{
|
|
FirstName = newMortgageActivity.AssociateFirst,
|
|
LastName = newMortgageActivity.AssociateLast,
|
|
MiddleInit = newMortgageActivity.AssociateMiddleInit,
|
|
DnaOrgNbr = newMortgageActivity.BranchOrgNbr.ToString()
|
|
});
|
|
}
|
|
else
|
|
{
|
|
appUser = await _userService.GetUserByAdEmailAsync(newMortgageActivity.EmployeeEmail);
|
|
}
|
|
|
|
if (appUser == null)
|
|
{
|
|
return activity.MapToActivity();
|
|
}
|
|
|
|
department = await _departmentRepository.FindUserHomeDepartment(appUser);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex,
|
|
$"Failed to get data for NewMortgageSaleActivity 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;
|
|
}
|
|
|
|
private bool IsFirstMortgageSale(NewLoanSaleActivity newMortgageActivity)
|
|
{
|
|
if (newMortgageActivity.GetProductType() == Product.FirstMortgageReferral
|
|
&& newMortgageActivity.GetActivityType() == ActivityType.Sale)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
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();
|
|
}
|
|
}
|