78 lines
3.1 KiB
C#
78 lines
3.1 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;
|
|
|
|
namespace LCU.API.Internal.Operations.IncentiveApi.Services.ExternalActivity;
|
|
|
|
public class NewVehicleActivityStrategy : IExternalActivityStrategy
|
|
{
|
|
private readonly IUserService _userService;
|
|
private readonly IDepartmentRepository _departmentRepository;
|
|
|
|
public NewVehicleActivityStrategy(IUserService userService, IDepartmentRepository departmentRepository)
|
|
{
|
|
_userService = userService;
|
|
_departmentRepository = departmentRepository;
|
|
}
|
|
|
|
public bool CanMapActivity(IExternalActivity activity)
|
|
{
|
|
return activity.GetActivityType() == ActivityType.Sale && activity.GetProductType() == Product.Vehicle;
|
|
}
|
|
|
|
public async Task<Activity> MapToActivityManagerActivity(IExternalActivity activity)
|
|
{
|
|
AppUser appUser = null;
|
|
Department department = null;
|
|
try
|
|
{
|
|
if (activity is NewLoanSaleActivity newVehicleActivity)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(newVehicleActivity.EmployeeEmail))
|
|
{
|
|
try
|
|
{
|
|
appUser = await _userService.TryFindSingleUserByName(new UserNameSearchRequest()
|
|
{
|
|
FirstName = newVehicleActivity.AssociateFirst,
|
|
LastName = newVehicleActivity.AssociateLast,
|
|
MiddleInit = newVehicleActivity.AssociateMiddleInit,
|
|
DnaOrgNbr = newVehicleActivity.BranchOrgNbr.ToString()
|
|
});
|
|
}
|
|
catch (InvalidOperationException ioe)
|
|
{
|
|
Log.Error(ioe,
|
|
$"More than one user found for name query {newVehicleActivity.AssociateFirst}, {newVehicleActivity.AssociateLast}, will try employee email.");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
appUser = await _userService.GetUserByAdEmailAsync(newVehicleActivity.EmployeeEmail);
|
|
}
|
|
|
|
if (appUser == null)
|
|
{
|
|
return activity.MapToActivity();
|
|
}
|
|
|
|
department = await _departmentRepository.FindUserHomeDepartment(appUser);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex,
|
|
$"Failed to get data for NewVehicleActivityStrategy 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;
|
|
}
|
|
}
|