Adding
This commit is contained in:
+36
@@ -0,0 +1,36 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using LCU.API.Internal.Operations.IncentiveApiLib.Persistence;
|
||||
using LCU.Library.Internal.IncentiveApiModels.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace LCU.API.Internal.Operations.IncentiveApiLib.Repositories;
|
||||
|
||||
public class AccountingRepository : BaseRepository, IAccountingRepository
|
||||
{
|
||||
public AccountingRepository(IApplicationDbContext context) : base(context)
|
||||
{
|
||||
}
|
||||
|
||||
public async Task<AccountingSummary> SaveAccountingSummaryAsync(AccountingSummary summary)
|
||||
{
|
||||
await _context.DepartmentLineItems.AddRangeAsync(summary.DepartmentPayouts);
|
||||
return summary;
|
||||
}
|
||||
|
||||
public async Task<AccountingSummary> GetAccountingSummaryAsync(DateTime payoutPeriod)
|
||||
{
|
||||
var rows = await _context.DepartmentLineItems.Where(x => x.PayoutMonth.Date == payoutPeriod.Date).ToListAsync();
|
||||
|
||||
if (rows != null && rows.Any())
|
||||
{
|
||||
var summary = new AccountingSummary(payoutPeriod);
|
||||
summary.DepartmentPayouts = rows;
|
||||
summary.Total = summary.DepartmentPayouts.Sum(x => x.PayoutTotal);
|
||||
return summary;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using LCU.API.Internal.Operations.IncentiveApiLib.Persistence;
|
||||
using LCU.Library.Internal.IncentiveApiModels.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace LCU.API.Internal.Operations.IncentiveApiLib.Repositories;
|
||||
|
||||
public class ActivityChangeRepository : BaseRepository, IActivityChangeRepository
|
||||
{
|
||||
public ActivityChangeRepository(ApplicationDbContext context) : base(context)
|
||||
{
|
||||
}
|
||||
|
||||
public async Task<ActivityStateChange> SaveActivityStateChange(ActivityStateChange stateChange)
|
||||
{
|
||||
await _context.ActivityStateChanges.AddAsync(stateChange);
|
||||
return stateChange;
|
||||
}
|
||||
|
||||
public async Task<List<ActivityStateChange>> GetActivityChangesByActivityId(int activityId)
|
||||
{
|
||||
return await _context.ActivityStateChanges.Where(x => x.ActivityId == activityId).ToListAsync();
|
||||
}
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using LCU.API.Internal.Operations.IncentiveApiLib.Persistence;
|
||||
using LCU.Library.Internal.IncentiveApiModels.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace LCU.API.Internal.Operations.IncentiveApiLib.Repositories;
|
||||
|
||||
public class ActivityDetailUpdateRepository : BaseRepository, IActivityDetailUpdateRepository
|
||||
{
|
||||
public ActivityDetailUpdateRepository(ApplicationDbContext context) : base(context)
|
||||
{
|
||||
}
|
||||
|
||||
public async Task<ActivityDetail> GetActivityDetailById(int activityDetailId)
|
||||
{
|
||||
return await _context.ActivityDetails.FindAsync(activityDetailId);
|
||||
}
|
||||
|
||||
public async Task<ActivityDetailUpdate> SaveActivityDetailUpdate(ActivityDetailUpdate activityDetailUpdate)
|
||||
{
|
||||
await _context.ActivityDetailUpdates.AddAsync(activityDetailUpdate);
|
||||
return activityDetailUpdate;
|
||||
}
|
||||
|
||||
public async Task<List<ActivityDetailUpdate>> GetActivityDetailUpdatesByActivityDetailId(int activityDetailId)
|
||||
{
|
||||
return await _context.ActivityDetailUpdates.Where(x => x.ActivityDetailId == activityDetailId).ToListAsync();
|
||||
}
|
||||
}
|
||||
+566
@@ -0,0 +1,566 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading.Tasks;
|
||||
using LCU.API.Internal.Operations.IncentiveApiLib.Extensions;
|
||||
using LCU.API.Internal.Operations.IncentiveApiLib.Persistence;
|
||||
using LCU.Library.Internal.IncentiveApiModels.Common;
|
||||
using LCU.Library.Internal.IncentiveApiModels.Entities;
|
||||
using LCU.Library.Internal.IncentiveApiModels.Enums;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace LCU.API.Internal.Operations.IncentiveApiLib.Repositories;
|
||||
|
||||
public class ActivityRepository : BaseRepository, IActivityRepository
|
||||
{
|
||||
public ActivityRepository(IApplicationDbContext context) : base(context)
|
||||
{
|
||||
}
|
||||
|
||||
public async Task<int> SaveActivityAsync(Activity activity)
|
||||
{
|
||||
await _context.Activities.AddAsync(activity);
|
||||
return activity.Id;
|
||||
}
|
||||
|
||||
public async Task SaveActivityRangeAsync(List<Activity> activities)
|
||||
{
|
||||
await _context.Activities.AddRangeAsync(activities);
|
||||
}
|
||||
|
||||
public void UpdateActivityRangeAsync(List<Activity> activities)
|
||||
{
|
||||
var existing = activities.Where(x => _context.Activities.Contains(x));
|
||||
_context.Activities.UpdateRange(activities);
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyCollection<Activity>> GetActivitiesAsync()
|
||||
{
|
||||
return await _context.Activities
|
||||
.Include(activity => activity.Detail)
|
||||
.Include(activity => activity.MetaData)
|
||||
.OrderBy(t => t.Id)
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<PagedResult<Activity>> GetActivitiesAsync(int page, int limit)
|
||||
{
|
||||
var results = new PagedResult<Activity>();
|
||||
var skip = (page - 1) * limit;
|
||||
var activities = _context.Activities
|
||||
.Include(activity => activity.Detail)
|
||||
.Include(activity => activity.MetaData)
|
||||
.OrderByDescending(t => t.Created);
|
||||
|
||||
|
||||
results.Total = activities.Count();
|
||||
results.Items = await activities.Skip(skip).Take(limit).ToListAsync();
|
||||
return results;
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyCollection<Activity>> FindUnmatchedReferrals()
|
||||
{
|
||||
var activities = _context.Activities.Include(activity => activity.Detail).Include(activity => activity.MetaData);
|
||||
IQueryable<Activity> querySet = activities
|
||||
.Where(x => x.Type == ActivityType.Referral
|
||||
&& x.State == ActivityState.PendingReferral)
|
||||
.OrderByDescending(t => t.LastUpdated);
|
||||
|
||||
return await querySet.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyCollection<Activity>> FindPendingEStatementsReferrals()
|
||||
{
|
||||
var activities = _context.Activities.Include(activity => activity.Detail).Include(activity => activity.MetaData);
|
||||
IQueryable<Activity> querySet = activities
|
||||
.Where(x => x.Type == ActivityType.Referral
|
||||
&& x.State == ActivityState.PendingReferral
|
||||
&& x.Product == Product.eStatement
|
||||
&& string.IsNullOrEmpty(x.Detail.AccountNumber))
|
||||
.OrderByDescending(t => t.LastUpdated);
|
||||
|
||||
return await querySet.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<Activity> FindUnmatchedSaleForReferral(Activity referral)
|
||||
{
|
||||
var activities = _context.Activities
|
||||
.Include(activity => activity.Detail)
|
||||
.Include(activity => activity.MetaData);
|
||||
|
||||
var matchOnMetaData = string.IsNullOrEmpty(referral.Detail.SubjectOrgNbr) && string.IsNullOrEmpty(referral.Detail.SubjectPersonNbr);
|
||||
|
||||
var matchedActivities = matchOnMetaData
|
||||
? await activities.Where(MetaDataMatchPredicate(referral)).ToListAsync()
|
||||
: await activities.Where(PersonDataMatchPredicate(referral)).ToListAsync();
|
||||
|
||||
if (matchOnMetaData && matchedActivities.Any())
|
||||
{
|
||||
matchedActivities = matchedActivities.Where(x =>
|
||||
ListsContainMatchingItem(
|
||||
x.MetaData.Where(e => e.MetaDataType == MetaDataType.Email).Select(e => e.MetaData).ToList(),
|
||||
referral.MetaData.Where(r => r.MetaDataType == MetaDataType.Email).Select(e => e.MetaData).ToList()
|
||||
)
|
||||
||
|
||||
ListsContainMatchingItem(
|
||||
x.MetaData.Where(e => e.MetaDataType == MetaDataType.Phone).Select(e => e.MetaData).ToList(),
|
||||
referral.MetaData.Where(r => r.MetaDataType == MetaDataType.Phone).Select(e => e.MetaData).ToList()
|
||||
)
|
||||
)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
var matchingSaleActivityIds = matchedActivities.Select(x => x.Id);
|
||||
var activityIDsAssociatedToStateChange = _context.ActivityStateChanges
|
||||
.Where(change => matchingSaleActivityIds.Contains(change.ActivityMatchId))
|
||||
.Select(x => x.ActivityMatchId);
|
||||
var activityIDsNotAssociatedToStateChange = matchingSaleActivityIds.Except(activityIDsAssociatedToStateChange);
|
||||
return matchedActivities.Where(x => activityIDsNotAssociatedToStateChange.Contains(x.Id))
|
||||
.OrderByDescending(t => t.LastUpdated)
|
||||
.FirstOrDefault();
|
||||
}
|
||||
|
||||
public EStatementDeliveryMethod FindDeliveryMethodForEStatement(Activity referral, List<EStatementDeliveryMethod> deliveryMethods)
|
||||
{
|
||||
var matchedMethods = new List<EStatementDeliveryMethod>();
|
||||
var nullReferralOrgNumber = "0";
|
||||
|
||||
var matchableDeliveryMethods = deliveryMethods.Where(x => x.ActivityDateTime.Date >= referral.SourceCreated.Date).ToList();
|
||||
|
||||
matchedMethods.AddRange(matchableDeliveryMethods.Where(x => x.PersonNumber == referral.Detail.SubjectPersonNbr
|
||||
|| x.OrganizationNumber == (referral.Detail.SubjectOrgNbr ?? nullReferralOrgNumber)
|
||||
&& x.OrganizationNumber != nullReferralOrgNumber
|
||||
&& referral.Detail.SubjectOrgNbr != nullReferralOrgNumber));
|
||||
|
||||
matchedMethods.AddRange(matchableDeliveryMethods.Where(x =>
|
||||
ListsContainMatchingItem(
|
||||
x.MetaData.Where(e => e.MetaDataType == MetaDataType.Email).Select(e => e.MetaData).ToList(),
|
||||
referral.MetaData.Where(r => r.MetaDataType == MetaDataType.Email).Select(e => e.MetaData).ToList()
|
||||
)
|
||||
||
|
||||
ListsContainMatchingItem(
|
||||
x.MetaData.Where(e => e.MetaDataType == MetaDataType.Phone).Select(e => e.MetaData).ToList(),
|
||||
referral.MetaData.Where(r => r.MetaDataType == MetaDataType.Phone).Select(e => e.MetaData).ToList()
|
||||
)
|
||||
)
|
||||
.ToList());
|
||||
|
||||
var matchedMethod = matchedMethods.OrderByDescending(a => a.ActivityDateTime).FirstOrDefault();
|
||||
|
||||
if (matchedMethod != null)
|
||||
{
|
||||
deliveryMethods.Remove(matchedMethod);
|
||||
}
|
||||
|
||||
return matchedMethod;
|
||||
}
|
||||
|
||||
private static Expression<Func<Activity, bool>> MetaDataMatchPredicate(Activity referral)
|
||||
{
|
||||
return x => (x.Type == ActivityType.Sale || x.Type == ActivityType.Other)
|
||||
&& x.Product == referral.Product
|
||||
&& x.MetaData != null && referral.MetaData != null
|
||||
&& x.State == ActivityState.Accepted
|
||||
&& x.SourceCreated.Date >= referral.SourceCreated.Date;
|
||||
}
|
||||
|
||||
private static bool ListsContainMatchingItem(IReadOnlyList<string> list1, IReadOnlyList<string> list2)
|
||||
{
|
||||
if ((list1?.Any() ?? false) && (list2?.Any() ?? false))
|
||||
{
|
||||
return list1.Any(x => list2.Any(t => string.Equals(t, x, StringComparison.CurrentCultureIgnoreCase)));
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static Expression<Func<Activity, bool>> PersonDataMatchPredicate(Activity referral)
|
||||
{
|
||||
return x => (x.Type == ActivityType.Sale || x.Type == ActivityType.Other)
|
||||
&& x.Product == referral.Product
|
||||
&& x.Detail.SubjectPersonNbr == referral.Detail.SubjectPersonNbr
|
||||
&& x.Detail.SubjectOrgNbr == referral.Detail.SubjectOrgNbr
|
||||
&& x.State == ActivityState.Accepted
|
||||
&& x.SourceCreated.Date >= referral.SourceCreated.Date;
|
||||
}
|
||||
|
||||
public async Task<Activity> FindDuplicateSaleAsync(Activity queryActivity)
|
||||
{
|
||||
if (queryActivity.Detail == null || queryActivity.Type != ActivityType.Sale)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var activities = _context.Activities
|
||||
.Include(activity => activity.Detail)
|
||||
.Include(activity => activity.MetaData);
|
||||
|
||||
IQueryable<Activity> querySet = activities
|
||||
.Where(x => x.Id != queryActivity.Id
|
||||
&& x.Product == queryActivity.Product
|
||||
&& x.Type == queryActivity.Type
|
||||
&& x.Detail.AccountNumber == queryActivity.Detail.AccountNumber
|
||||
&& x.Detail.SubAccountNumber == queryActivity.Detail.SubAccountNumber
|
||||
&& x.Detail.SubjectOrgNbr == queryActivity.Detail.SubjectOrgNbr
|
||||
&& x.Detail.SubjectPersonNbr == queryActivity.Detail.SubjectPersonNbr
|
||||
&& (
|
||||
queryActivity.SourceId == null
|
||||
|| x.SourceId == queryActivity.SourceId)
|
||||
)
|
||||
.OrderByDescending(t => t.LastUpdated);
|
||||
|
||||
return await querySet.FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
public async Task<Activity> FindDuplicateReferralAsync(Activity queryActivity)
|
||||
{
|
||||
if (queryActivity.Detail == null || queryActivity.Type != ActivityType.Referral)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var activities = _context.Activities
|
||||
.Include(activity => activity.Detail);
|
||||
|
||||
IQueryable<Activity> querySet = activities
|
||||
.Where(x => x.Id != queryActivity.Id
|
||||
&& x.Type == queryActivity.Type && x.Product == queryActivity.Product
|
||||
&& x.Detail.SubjectPersonNbr != null && queryActivity.Detail.SubjectPersonNbr != null
|
||||
&& x.Detail.SubjectOrgNbr != null && queryActivity.Detail.SubjectOrgNbr != null
|
||||
&& x.Detail.SubjectPersonNbr == queryActivity.Detail.SubjectPersonNbr
|
||||
&& x.Detail.SubjectOrgNbr == queryActivity.Detail.SubjectOrgNbr
|
||||
)
|
||||
.OrderByDescending(t => t.LastUpdated);
|
||||
|
||||
return await querySet.FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
public async Task<Activity> FindActivityForStateUpdate(Activity queryActivity)
|
||||
{
|
||||
if (queryActivity.Detail == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var activities = _context.Activities
|
||||
.Include(activity => activity.Detail)
|
||||
.Include(activity => activity.MetaData);
|
||||
|
||||
IQueryable<Activity> querySet = activities
|
||||
.Where(x => x.EmployeeEmail == queryActivity.EmployeeEmail
|
||||
&& x.EmployeeId == queryActivity.EmployeeId
|
||||
&& x.DepartmentId == queryActivity.DepartmentId
|
||||
&& x.Product == queryActivity.Product
|
||||
&& x.SourceCreated.Date == queryActivity.SourceCreated.Date
|
||||
&& x.SourceSystem == queryActivity.SourceSystem
|
||||
&& x.Type == queryActivity.Type
|
||||
&& x.Detail.AccountNumber == queryActivity.Detail.AccountNumber
|
||||
&& x.Detail.SubAccountNumber == queryActivity.Detail.SubAccountNumber
|
||||
)
|
||||
.OrderByDescending(t => t.LastUpdated);
|
||||
|
||||
var returnedItems = await querySet.ToListAsync();
|
||||
Func<Activity, bool> activityStateCondition = x => x.State != queryActivity.State;
|
||||
var returnItem = returnedItems.Count(activityStateCondition) > 1
|
||||
? returnedItems.FirstOrDefault(activityStateCondition)
|
||||
: returnedItems.FirstOrDefault();
|
||||
|
||||
return returnItem;
|
||||
}
|
||||
|
||||
public async Task<Activity> FindActivityForDetailUpdate(Activity queryActivity)
|
||||
{
|
||||
if (queryActivity.Detail == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var returnedItems = await GetActivityForDetailUpdate(queryActivity);
|
||||
|
||||
if (returnedItems.Count == 0)
|
||||
{
|
||||
returnedItems = await GetActivityForDetailUpdate(queryActivity, false);
|
||||
}
|
||||
|
||||
if (returnedItems.Count == 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
Func<Activity, bool> activityStatePredicate = x => x.State == ActivityState.Accepted;
|
||||
var returnItem = returnedItems.Count(activityStatePredicate) >= 1
|
||||
? returnedItems.FirstOrDefault(activityStatePredicate)
|
||||
: returnedItems.FirstOrDefault();
|
||||
|
||||
return returnItem;
|
||||
}
|
||||
|
||||
private async Task<List<Activity>> GetActivityForDetailUpdate(Activity queryActivity, bool filterOnSubAccountNumber = true)
|
||||
{
|
||||
var activities = _context.Activities
|
||||
.Include(activity => activity.Detail)
|
||||
.Include(activity => activity.MetaData);
|
||||
|
||||
IQueryable<Activity> querySet = activities
|
||||
.Where(x => x.EmployeeEmail == queryActivity.EmployeeEmail
|
||||
&& x.EmployeeId == queryActivity.EmployeeId
|
||||
&& x.DepartmentId == queryActivity.DepartmentId
|
||||
&& x.Product == queryActivity.Product
|
||||
&& x.SourceSystem == queryActivity.SourceSystem
|
||||
&& x.Type == queryActivity.Type
|
||||
&& x.Detail.AccountNumber == queryActivity.Detail.AccountNumber
|
||||
)
|
||||
.OrderByDescending(t => t.LastUpdated);
|
||||
|
||||
if (filterOnSubAccountNumber)
|
||||
{
|
||||
return await querySet.Where(x => x.Detail.SubAccountNumber == queryActivity.Detail.SubAccountNumber).ToListAsync();
|
||||
}
|
||||
|
||||
return await querySet.Where(x => x.State == ActivityState.Accepted).ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<PagedResult<Activity>> GetPagedFilteredActivities(ActivityFilter filter)
|
||||
{
|
||||
var results = new PagedResult<Activity>();
|
||||
var skip = (filter.Page - 1) * filter.Limit;
|
||||
var activities = _context.Activities.Include(activity => activity.Detail).Include(activity => activity.MetaData);
|
||||
IQueryable<Activity> querySet = activities;
|
||||
|
||||
if (filter.Product != null && filter.Product.Any())
|
||||
{
|
||||
var products = filter.Product;
|
||||
querySet = querySet.Where(x => products.Contains(x.Product));
|
||||
}
|
||||
|
||||
if (filter.Type != null && filter.Type.Any())
|
||||
{
|
||||
var types = filter.Type;
|
||||
querySet = querySet.Where(x => types.Contains(x.Type));
|
||||
}
|
||||
|
||||
if (filter.State != null && filter.State.Any())
|
||||
{
|
||||
var states = filter.State;
|
||||
querySet = querySet.Where(x => states.Contains(x.State));
|
||||
}
|
||||
|
||||
if (filter.DateLow != null)
|
||||
{
|
||||
querySet = querySet.Where(x => x.Accepted.Date >= filter.DateLow);
|
||||
}
|
||||
|
||||
if (filter.DateHigh != null)
|
||||
{
|
||||
querySet = querySet.Where(x => x.Accepted.Date <= filter.DateHigh);
|
||||
}
|
||||
|
||||
if (filter.DepartmentId != 0)
|
||||
{
|
||||
querySet = querySet.Where(x => x.DepartmentId == filter.DepartmentId);
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(filter.EmployeeId))
|
||||
{
|
||||
querySet = querySet.Where(x => x.EmployeeId == filter.EmployeeId);
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(filter.Query))
|
||||
{
|
||||
var query = filter.Query.ToLower().Trim();
|
||||
querySet = querySet.Where(x =>
|
||||
x.Detail.MemberName.ToLower().Contains(query)
|
||||
|| x.Detail.AccountNumber.ToLower().Contains(query)
|
||||
|| x.Detail.SubjectPersonNbr.ToLower().Contains(query)
|
||||
|| x.Detail.SubjectOrgNbr.ToLower().Contains(query)
|
||||
|| x.SourceId.ToLower() == query
|
||||
|| query == x.Id.ToString());
|
||||
}
|
||||
|
||||
querySet = SortQuerySet(querySet, filter);
|
||||
results.Total = querySet.Count();
|
||||
results.Items = await querySet.Skip(skip).Take(filter.Limit).ToListAsync();
|
||||
return results;
|
||||
}
|
||||
|
||||
public async Task<List<Activity>> GetAllActivitiesWithPayoutsForEmployee(string employeeId)
|
||||
{
|
||||
return await _context.Activities.Include(activity => activity.ActivityPayouts).Where(x => x.EmployeeId == employeeId).ToListAsync();
|
||||
}
|
||||
|
||||
private IQueryable<Activity> SortQuerySet(IQueryable<Activity> querySet, ActivityFilter filter)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(filter.SortBy))
|
||||
{
|
||||
return querySet
|
||||
.OrderByDescending(t => t.Accepted);
|
||||
}
|
||||
|
||||
if (filter.SortDescending.HasValue && filter.SortDescending.Value)
|
||||
{
|
||||
return (IQueryable<Activity>)querySet.OrderByDescending<Activity>(filter.SortBy);
|
||||
}
|
||||
|
||||
return (IQueryable<Activity>)querySet.OrderBy<Activity>(filter.SortBy);
|
||||
}
|
||||
|
||||
public async Task<Activity> GetActivityById(int activityId)
|
||||
{
|
||||
return await _context.Activities.FindAsync(activityId);
|
||||
}
|
||||
|
||||
public async Task<ICollection<Activity>> FindEmployeeActivityByMonthRange(AppUser appUser, MonthRange monthRange)
|
||||
{
|
||||
return await _context.Activities
|
||||
.Include(activity => activity.Detail)
|
||||
.Include(activity => activity.MetaData)
|
||||
.Where(x => x.Accepted.Date >= monthRange.StartTime.Date &&
|
||||
x.Accepted.Date <= monthRange.EndTime.Date &&
|
||||
appUser.EmployeeId == x.EmployeeId)
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<ICollection<Activity>> FindDepartmentActivityByMonthRange(int departmentId, MonthRange monthRange)
|
||||
{
|
||||
return await _context.Activities
|
||||
.Include(activity => activity.Detail)
|
||||
.Include(activity => activity.MetaData)
|
||||
.Where(x => x.Accepted.Date >= monthRange.StartTime.Date &&
|
||||
x.Accepted.Date <= monthRange.EndTime.Date &&
|
||||
x.DepartmentId == departmentId)
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<Activity> GetActivityIncludingPayouts(int activityId)
|
||||
{
|
||||
return await _context.Activities
|
||||
.Include(activity => activity.Detail)
|
||||
.Include(activity => activity.MetaData)
|
||||
.Include(activity => activity.ActivityPayouts)
|
||||
.ThenInclude(payouts => payouts.PayoutLineItem)
|
||||
.FirstOrDefaultAsync(x => x.Id == activityId);
|
||||
}
|
||||
|
||||
public async Task<List<Activity>> GetActivitiesBySourceId(List<string> sourceIds)
|
||||
{
|
||||
return await _context.Activities.Where(x => sourceIds.Contains(x.SourceId)).ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<Activity> FindDuplicateReferralBySourceIdAsync(Activity queryActivity)
|
||||
{
|
||||
if (queryActivity.Detail == null || queryActivity.Type != ActivityType.Referral)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var activities = _context.Activities
|
||||
.Include(activity => activity.Detail)
|
||||
.Include(activity => activity.MetaData);
|
||||
|
||||
IQueryable<Activity> querySet = activities
|
||||
.Where(x => x.Id != queryActivity.Id
|
||||
&& x.Type == queryActivity.Type
|
||||
&& x.Product == queryActivity.Product
|
||||
&& x.SourceId == queryActivity.SourceId)
|
||||
.OrderByDescending(t => t.LastUpdated);
|
||||
|
||||
return await querySet.FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
public async Task<List<Activity>> FindById(
|
||||
List<int> ids,
|
||||
bool withDetail = true,
|
||||
bool withTracking = true
|
||||
)
|
||||
{
|
||||
if (!ids.Any())
|
||||
{
|
||||
return new List<Activity>();
|
||||
}
|
||||
|
||||
var query = withTracking
|
||||
? _context.Activities
|
||||
: _context.Activities.AsNoTracking();
|
||||
|
||||
if (withDetail)
|
||||
{
|
||||
query = query.Include(activity => activity.Detail);
|
||||
}
|
||||
|
||||
query = query.Where(activity => ids.Contains(activity.Id));
|
||||
|
||||
return await query.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<List<Activity>> FindLatestBySourceId(
|
||||
List<string> sourceIds,
|
||||
bool withDetail = true,
|
||||
bool withTracking = true
|
||||
)
|
||||
{
|
||||
if (!sourceIds.Any())
|
||||
{
|
||||
return new List<Activity>();
|
||||
}
|
||||
|
||||
var query = withTracking
|
||||
? _context.Activities
|
||||
: _context.Activities.AsNoTracking();
|
||||
|
||||
if (withDetail)
|
||||
{
|
||||
query = query.Include(activity => activity.Detail);
|
||||
}
|
||||
|
||||
return (await query
|
||||
.Where(activity => sourceIds.Contains(activity.SourceId))
|
||||
.GroupBy(activity => activity.SourceId)
|
||||
.Select(group => group.OrderBy(activity => activity.Created).FirstOrDefault())
|
||||
.ToListAsync())
|
||||
.Where(activity => activity != null)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public async Task<List<Activity>> FindGapInsuranceEligible(
|
||||
MonthRange monthRange,
|
||||
bool withTracking = true
|
||||
)
|
||||
{
|
||||
var query = withTracking
|
||||
? _context.Activities
|
||||
: _context.Activities.AsNoTracking();
|
||||
|
||||
return await query
|
||||
.Include(activity => activity.Detail)
|
||||
.Where(activity =>
|
||||
activity.Detail != null &&
|
||||
activity.Detail.IsGapInsuranceEligible &&
|
||||
activity.Accepted.Date >= monthRange.StartTime.Date &&
|
||||
activity.Accepted.Date <= monthRange.EndTime.Date)
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<List<Activity>> GetFilteredActivities(
|
||||
MonthRange monthRange,
|
||||
Product product,
|
||||
ActivityState activityState,
|
||||
bool withTracking = true
|
||||
)
|
||||
{
|
||||
var querySet = withTracking
|
||||
? _context.Activities
|
||||
: _context.Activities.AsNoTracking();
|
||||
|
||||
return await querySet
|
||||
.Include(activity => activity.Detail)
|
||||
.Where(
|
||||
activity =>
|
||||
activity.Accepted.Date >= monthRange.StartTime.Date &&
|
||||
activity.Accepted.Date <= monthRange.EndTime.Date &&
|
||||
activity.Product == product &&
|
||||
activity.State == activityState
|
||||
)
|
||||
.ToListAsync();
|
||||
}
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using LCU.API.Internal.Operations.IncentiveApiLib.Persistence;
|
||||
using LCU.Library.Internal.IncentiveApiModels.Entities;
|
||||
using LCU.Library.Internal.IncentiveApiModels.Enums;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace LCU.API.Internal.Operations.IncentiveApiLib.Repositories;
|
||||
|
||||
public class ActivityTransferActionRepository : BaseRepository, IActivityTransferActionRepository
|
||||
{
|
||||
public ActivityTransferActionRepository(IApplicationDbContext context) : base(context)
|
||||
{
|
||||
}
|
||||
|
||||
public async Task<int> SaveTransferAsync(ActivityTransferAction activityTransferAction)
|
||||
{
|
||||
await _context.ActivityTransferActions.AddAsync(activityTransferAction);
|
||||
return activityTransferAction.Id;
|
||||
}
|
||||
|
||||
public async Task<ActivityTransferAction> GetTransferActionById(int id)
|
||||
{
|
||||
return await _context.ActivityTransferActions
|
||||
.Include(x => x.RequestedActivity)
|
||||
.FirstOrDefaultAsync(x => x.Id == id);
|
||||
}
|
||||
|
||||
public async Task<ActivityTransferAction> GetRequestedTransferActionByActivityId(int requestedActivityId)
|
||||
{
|
||||
return await _context.ActivityTransferActions
|
||||
.FirstOrDefaultAsync(x => x.RequestedActivityId == requestedActivityId
|
||||
&& x.State == ActivityTransferState.Requested);
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyCollection<ActivityTransferAction>> GetActiveTransfersForStateAsync(
|
||||
ActivityState state)
|
||||
{
|
||||
return await _context.ActivityTransferActions
|
||||
.Include(x => x.RequestedActivity)
|
||||
.Where(x => x.ActivityStateBeforeTransfer == state
|
||||
&& x.State == ActivityTransferState.Requested)
|
||||
.ToListAsync();
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
using LCU.API.Internal.Operations.IncentiveApiLib.Persistence;
|
||||
|
||||
namespace LCU.API.Internal.Operations.IncentiveApiLib.Repositories;
|
||||
|
||||
public abstract class BaseRepository
|
||||
{
|
||||
protected readonly IApplicationDbContext _context;
|
||||
|
||||
protected BaseRepository(IApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using LCU.Library.Internal.IncentiveApiModels.Entities;
|
||||
using LCU.Library.Internal.IncentiveApiModels.Enums;
|
||||
using LCU.API.Internal.Operations.IncentiveApiLib.Persistence;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace LCU.API.Internal.Operations.IncentiveApiLib.Repositories;
|
||||
|
||||
public class CommissionRepository : BaseRepository, ICommissionRepository
|
||||
{
|
||||
public CommissionRepository(IApplicationDbContext context) : base(context)
|
||||
{
|
||||
}
|
||||
|
||||
public async Task<Commission> FindExistingCommission(int id)
|
||||
{
|
||||
return await _context.Commissions.FindAsync(id);
|
||||
}
|
||||
|
||||
public async Task<Commission> FindExistingCommission(Role role, Product product)
|
||||
{
|
||||
return await _context.Commissions.FirstOrDefaultAsync(x => x.Role == role &&
|
||||
x.Product == product);
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyCollection<Commission>> GetCommissionsAsync()
|
||||
{
|
||||
return await _context.Commissions.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task SaveCommission(Commission commission)
|
||||
{
|
||||
await _context.Commissions.AddAsync(commission);
|
||||
}
|
||||
|
||||
public async Task<IList<Commission>> FindProductCommissions(Role employeeRole)
|
||||
{
|
||||
return await _context.Commissions.AsNoTracking().Where(x => x.Role == employeeRole).ToListAsync();
|
||||
}
|
||||
}
|
||||
+142
@@ -0,0 +1,142 @@
|
||||
using LCU.API.Internal.Operations.IncentiveApiLib.Extensions;
|
||||
using LCU.API.Internal.Operations.IncentiveApiLib.Persistence;
|
||||
using LCU.Library.Internal.IncentiveApiModels.Common;
|
||||
using LCU.Library.Internal.IncentiveApiModels.Entities;
|
||||
using LCU.Library.Internal.IncentiveApiModels.Enums;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace LCU.API.Internal.Operations.IncentiveApiLib.Repositories;
|
||||
|
||||
public class DeadActivityRepository : BaseRepository, IDeadActivityRepository
|
||||
{
|
||||
public DeadActivityRepository(IApplicationDbContext context) : base(context)
|
||||
{
|
||||
}
|
||||
|
||||
public async Task<DeadActivity> SaveDeadActivity(DeadActivity newDeadActivity)
|
||||
{
|
||||
await _context.DeadActivities.AddAsync(newDeadActivity);
|
||||
return newDeadActivity;
|
||||
}
|
||||
|
||||
public async Task<List<DeadActivity>> SaveDeadActivityRange(List<DeadActivity> newActivities)
|
||||
{
|
||||
await _context.DeadActivities.AddRangeAsync(newActivities);
|
||||
return newActivities;
|
||||
}
|
||||
|
||||
public async Task<List<DeadActivity>> GetDeadActivitiesBySourceIds(List<string> sourceIds)
|
||||
{
|
||||
return await _context.DeadActivities.Include(x => x.DeadDetail).Where(x => sourceIds.Contains(x.SourceId)).ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<List<DeadActivity>> GetAllDeadActivities()
|
||||
{
|
||||
return await _context.DeadActivities.Include(x => x.DeadDetail).ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<DeadActivity> GetDeadActivityById(int activityId)
|
||||
{
|
||||
return await _context.DeadActivities
|
||||
.Include(x => x.DeadDetail)
|
||||
.FirstOrDefaultAsync(x => x.Id == activityId);
|
||||
}
|
||||
|
||||
public async Task<PagedResult<DeadActivity>> GetPagedDeadActivities(int page, int limit)
|
||||
{
|
||||
var results = new PagedResult<DeadActivity>();
|
||||
var skip = (page - 1) * limit;
|
||||
var deadActivities = _context.DeadActivities
|
||||
.Include(activity => activity.DeadDetail)
|
||||
.OrderByDescending(t => t.Created);
|
||||
|
||||
results.Total = deadActivities.Count();
|
||||
results.Items = await deadActivities.Skip(skip).Take(limit).ToListAsync();
|
||||
return results;
|
||||
}
|
||||
|
||||
public async Task<PagedResult<DeadActivity>> GetFilteredDeadActivities(DeadActivityFilter filter)
|
||||
{
|
||||
var results = new PagedResult<DeadActivity>();
|
||||
var skip = (filter.Page - 1) * filter.Limit;
|
||||
var activities = _context
|
||||
.DeadActivities.Include(activity => activity.DeadDetail)
|
||||
.Where(activity => activity.LivingActivityId == null);
|
||||
IQueryable<DeadActivity> querySet = activities;
|
||||
|
||||
if (filter.Product != null && filter.Product.Any())
|
||||
{
|
||||
var products = filter.Product;
|
||||
querySet = querySet.Where(x => products.Contains((Product)x.Product));
|
||||
}
|
||||
|
||||
if (filter.Type != null && filter.Type.Any())
|
||||
{
|
||||
var types = filter.Type;
|
||||
querySet = querySet.Where(x => types.Contains((ActivityType)x.Type));
|
||||
}
|
||||
|
||||
if (filter.State != null && filter.State.Any())
|
||||
{
|
||||
var states = filter.State;
|
||||
querySet = querySet.Where(x => states.Contains((ActivityState)x.State));
|
||||
}
|
||||
|
||||
if (filter.DateLow != null)
|
||||
{
|
||||
querySet = querySet.Where(x => x.Accepted >= filter.DateLow);
|
||||
}
|
||||
|
||||
if (filter.DateHigh != null)
|
||||
{
|
||||
querySet = querySet.Where(x => x.Accepted <= filter.DateHigh);
|
||||
}
|
||||
|
||||
if (filter.DepartmentId != 0)
|
||||
{
|
||||
querySet = querySet.Where(x => x.DepartmentId == filter.DepartmentId);
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(filter.EmployeeId))
|
||||
{
|
||||
querySet = querySet.Where(x => x.EmployeeId == filter.EmployeeId);
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(filter.Query))
|
||||
{
|
||||
var query = filter.Query.ToLower().Trim();
|
||||
querySet = querySet.Where(x =>
|
||||
(x.DeadDetail.MemberName ?? "").ToLower().Contains(query)
|
||||
|| (x.DeadDetail.AccountNumber ?? "").ToLower().Contains(query)
|
||||
|| (x.DeadDetail.SubAccountNumber ?? "").ToLower().Contains(query)
|
||||
|| (x.DeadDetail.SubjectPersonNbr ?? "").ToLower().Contains(query)
|
||||
|| (x.DeadDetail.SubjectOrgNbr ?? "").ToLower().Contains(query)
|
||||
|| (x.SourceId ?? "").ToLower() == query
|
||||
|| query == x.Id.ToString());
|
||||
}
|
||||
|
||||
querySet = SortQuerySet(querySet, filter);
|
||||
results.Total = querySet.Count();
|
||||
results.Items = await querySet.Skip(skip).Take(filter.Limit).ToListAsync();
|
||||
return results;
|
||||
}
|
||||
|
||||
private IQueryable<DeadActivity> SortQuerySet(IQueryable<DeadActivity> querySet, ActivityFilter filter)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(filter.SortBy))
|
||||
{
|
||||
return querySet
|
||||
.OrderByDescending(t => t.Accepted);
|
||||
}
|
||||
|
||||
if (filter.SortDescending.HasValue && filter.SortDescending.Value)
|
||||
{
|
||||
return (IQueryable<DeadActivity>)querySet.OrderByDescending<DeadActivity>(filter.SortBy);
|
||||
}
|
||||
|
||||
return (IQueryable<DeadActivity>)querySet.OrderBy<DeadActivity>(filter.SortBy);
|
||||
}
|
||||
}
|
||||
+173
@@ -0,0 +1,173 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using LCU.Library.Internal.IncentiveApiModels.Entities;
|
||||
using LCU.Library.Internal.IncentiveApiModels.Enums;
|
||||
using LCU.API.Internal.Operations.IncentiveApiLib.Persistence;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Serilog;
|
||||
using LCU.Library.Internal.IncentiveApiModels.Common;
|
||||
using LCU.Library.Internal.IncentiveApiModels.Results;
|
||||
using LCU.Library.Internal.IncentiveApiModels.Requests;
|
||||
|
||||
namespace LCU.API.Internal.Operations.IncentiveApiLib.Repositories;
|
||||
|
||||
public class DepartmentRepository : BaseRepository, IDepartmentRepository
|
||||
{
|
||||
private const string BranchStr = "branch";
|
||||
|
||||
public DepartmentRepository(IApplicationDbContext context) : base(context)
|
||||
{
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyCollection<Department>> GetAllDepartmentsAsync()
|
||||
{
|
||||
return await _context.Departments.OrderBy(x => x.DepartmentName).ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<Department> FindDepartmentByIdAsync(int id)
|
||||
{
|
||||
return await _context.Departments.FindAsync(id);
|
||||
}
|
||||
|
||||
public async Task<Department> SaveDepartmentAsync(Department department)
|
||||
{
|
||||
await _context.Departments.AddAsync(department);
|
||||
return department;
|
||||
}
|
||||
|
||||
public async Task<Department> FindOrCreateDepartment(ActiveEmployeeResult employee)
|
||||
{
|
||||
var departmentName = DetermineDepartmentName(employee.OrgUnit);
|
||||
var orgCode = employee.OrgCode.ToLower();
|
||||
var businessLine = BusinessLine.TryLookup(employee.OrgUnit);
|
||||
var department = await _context.Departments.FirstOrDefaultAsync(x =>
|
||||
x.DayForceGlCode.ToLower() == orgCode || x.DepartmentName.ToLower() == departmentName.ToLower());
|
||||
|
||||
if (department == null)
|
||||
{
|
||||
department = new Department()
|
||||
{
|
||||
BusinessLineCode = businessLine.Code,
|
||||
State = DepartmentState.Active,
|
||||
DepartmentName = departmentName,
|
||||
DayforceDepartmentName = employee.OrgUnit,
|
||||
DayForceGlCode = employee.OrgCode,
|
||||
DnaOrgNbr = employee.GetDnaOrgCode()
|
||||
};
|
||||
await SaveDepartmentAsync(department);
|
||||
}
|
||||
|
||||
department.DepartmentName = departmentName;
|
||||
return department;
|
||||
}
|
||||
|
||||
public static string DetermineDepartmentName(string employeeOrgUnit)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(employeeOrgUnit))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (employeeOrgUnit.Contains("-"))
|
||||
{
|
||||
var unitNames = employeeOrgUnit.Split("-");
|
||||
var leftHalf = unitNames.First().Trim();
|
||||
var rightHalf = unitNames.ElementAt(1).Trim();
|
||||
|
||||
if (string.Equals(rightHalf, BranchStr, StringComparison.CurrentCultureIgnoreCase))
|
||||
{
|
||||
return leftHalf;
|
||||
}
|
||||
|
||||
return rightHalf;
|
||||
}
|
||||
|
||||
return employeeOrgUnit.Trim();
|
||||
}
|
||||
|
||||
public async Task<List<AppUser>> FindDepartmentAssignments(DepartmentPersonnelSearchRequest searchRequest)
|
||||
{
|
||||
var userDepartments = await _context.DepartmentAssignments
|
||||
.Include(x => x.Employee)
|
||||
.ThenInclude(x => x.AdditionalPermissions)
|
||||
.Where(x => x.DepartmentId == searchRequest.DepartmentId)
|
||||
.ToListAsync();
|
||||
|
||||
var range = new MonthRange(searchRequest.Year, searchRequest.Month);
|
||||
var usersInRange = userDepartments
|
||||
.Where(x =>
|
||||
x.StartDate.Date <= range.EndTime.Date &&
|
||||
(x.EndDate == null || x.EndDate.Value.Date >= range.StartTime.Date));
|
||||
|
||||
var users = usersInRange.Where(x => searchRequest.IncludeAllPersonnel ||
|
||||
x.Role.Permissions.Any(p =>
|
||||
p == Permission.HaveSalesGoals) ||
|
||||
x.Employee.AdditionalPermissions.Any(p =>
|
||||
p.Permission == Permission.HaveSalesGoals));
|
||||
|
||||
return users.Select(x => x.Employee).ToList();
|
||||
}
|
||||
|
||||
public async Task<Department> FindUserHomeDepartment(AppUser appUser)
|
||||
{
|
||||
try
|
||||
{
|
||||
var userDepartment = await _context.DepartmentAssignments.Include(x => x.Department)
|
||||
.Where(x => x.IsHome == true && x.EndDate == null
|
||||
&& x.Employee.EmployeeId.Equals(appUser.EmployeeId)).SingleOrDefaultAsync();
|
||||
return userDepartment?.Department;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex,
|
||||
$"Error Finding User Home Department. User with EmployeeId {appUser.EmployeeId} may have no or multiple home assignments.");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Department> FindDepartmentByBranchName(string name)
|
||||
{
|
||||
return await _context.Departments.SingleOrDefaultAsync(x => x.DepartmentName.ToLower() == name.ToLower());
|
||||
}
|
||||
|
||||
public async Task<Department> GetDepartmentByDayforceGlCode(int glCode)
|
||||
{
|
||||
return await _context.Departments.AsNoTracking().FirstOrDefaultAsync(x => x.DayForceGlCode == glCode.ToString());
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyCollection<Department>> FindDepartmentsByRegionAsync(Region region)
|
||||
{
|
||||
return await _context.Departments
|
||||
.Where(x => x.RegionCode == region.Code)
|
||||
.OrderBy(x => x.DepartmentName)
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyCollection<Department>> FindDepartmentsByBusinessLineAsync(BusinessLine businessLine)
|
||||
{
|
||||
return await _context.Departments
|
||||
.Where(x => x.BusinessLineCode == businessLine.Code)
|
||||
.OrderBy(x => x.DepartmentName)
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyCollection<UserDepartment>> FindCurrentDepartmentAssignments(int departmentId)
|
||||
{
|
||||
return await FindDepartmentAssignmentsForRange(departmentId, new MonthRange());
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyCollection<UserDepartment>> FindDepartmentAssignmentsForRange(int departmentId,
|
||||
MonthRange monthRange)
|
||||
{
|
||||
return await _context.DepartmentAssignments
|
||||
.AsNoTracking()
|
||||
.Include(assignment => assignment.Employee)
|
||||
.Where(assignment => assignment.DepartmentId == departmentId &&
|
||||
assignment.StartDate.Date <= monthRange.EndTime.Date &&
|
||||
(assignment.EndDate == null ||
|
||||
assignment.EndDate.Value.Date >= monthRange.StartTime.Date))
|
||||
.ToListAsync();
|
||||
}
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using LCU.Library.Internal.IncentiveApiModels.Entities;
|
||||
using LCU.API.Internal.Operations.IncentiveApiLib.Persistence;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace LCU.API.Internal.Operations.IncentiveApiLib.Repositories;
|
||||
|
||||
public class GoalChangeRepository : BaseRepository, IGoalChangeRepository
|
||||
{
|
||||
public GoalChangeRepository(IApplicationDbContext context) : base(context)
|
||||
{
|
||||
}
|
||||
|
||||
public async Task<int> SaveGoalChangeAsync(GoalChange goalChange)
|
||||
{
|
||||
await _context.GoalChanges.AddAsync(goalChange);
|
||||
return goalChange.Id;
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyCollection<GoalChange>> GetGoalChangesAsync()
|
||||
{
|
||||
return await _context.GoalChanges
|
||||
.OrderBy(t => t.Id)
|
||||
.ToListAsync();
|
||||
}
|
||||
}
|
||||
+130
@@ -0,0 +1,130 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using LCU.Library.Internal.IncentiveApiModels.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using LCU.API.Internal.Operations.IncentiveApiLib.Persistence;
|
||||
using LCU.Library.Internal.IncentiveApiModels.Common;
|
||||
using LCU.Library.Internal.IncentiveApiModels.Requests;
|
||||
|
||||
namespace LCU.API.Internal.Operations.IncentiveApiLib.Repositories;
|
||||
|
||||
public class GoalRepository : BaseRepository, IGoalRepository
|
||||
{
|
||||
public GoalRepository(IApplicationDbContext context) : base(context)
|
||||
{
|
||||
}
|
||||
|
||||
public async Task<int> SaveGoalAsync(Goal goalChange)
|
||||
{
|
||||
await _context.Goals.AddAsync(goalChange);
|
||||
return goalChange.Id;
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyCollection<Goal>> GetGoalsAsync(GoalSearchRequest goalSearchRequest)
|
||||
{
|
||||
var result = _context.Goals.AsQueryable();
|
||||
|
||||
if (goalSearchRequest != null)
|
||||
{
|
||||
if (goalSearchRequest.departmentId.HasValue)
|
||||
{
|
||||
result = result.Where(x => x.DepartmentId == goalSearchRequest.departmentId);
|
||||
}
|
||||
|
||||
if (goalSearchRequest.DateRange != null && goalSearchRequest.DateRange.ValidFromRange)
|
||||
{
|
||||
var fromDate = goalSearchRequest.DateRange.StartTime;
|
||||
result = result.Where(x => x.StartDate.Date >= fromDate.Date);
|
||||
}
|
||||
|
||||
if (goalSearchRequest.DateRange != null && goalSearchRequest.DateRange.ValidToRange)
|
||||
{
|
||||
var toDate = goalSearchRequest.DateRange.EndTime;
|
||||
result = result.Where(x => x.EndDate.Date <= toDate.Date);
|
||||
}
|
||||
|
||||
if (goalSearchRequest.IncludePersonnel)
|
||||
{
|
||||
result = result.Where(x => x.EmployeeId != null);
|
||||
}
|
||||
else
|
||||
{
|
||||
result = result.Where(x => x.EmployeeId == null);
|
||||
}
|
||||
}
|
||||
|
||||
return await result.OrderBy(t => t.Id).ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<Goal> FindExistingGoal(GoalChange goalChange)
|
||||
{
|
||||
return await _context.Goals.FirstOrDefaultAsync(x => x.Product == goalChange.Product &&
|
||||
x.StartDate.Date == goalChange.StartDate.Date &&
|
||||
x.EndDate.Date == goalChange.EndDate.Date &&
|
||||
x.DepartmentId == goalChange.DepartmentId &&
|
||||
x.EmployeeId == goalChange.PersonId);
|
||||
}
|
||||
|
||||
public async Task<ICollection<Goal>> FindDepartmentGoals(int departmentId, DateTime start, DateTime end)
|
||||
{
|
||||
return await _context.Goals.Where(x => x.StartDate.Date >= start.Date &&
|
||||
x.EndDate.Date <= end.Date &&
|
||||
x.EmployeeId == null &&
|
||||
x.DepartmentId == departmentId).ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<ICollection<Goal>> FindAnnualDepartmentGoals(int departmentId, int year)
|
||||
{
|
||||
return await _context.Goals.Where(x => x.StartDate.Year == year &&
|
||||
x.EndDate.Year == year &&
|
||||
x.EmployeeId == null &&
|
||||
x.DepartmentId == departmentId).ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<ICollection<Goal>> FindPersonnelGoals(int departmentId, List<AppUser> users, DateTime start,
|
||||
DateTime end)
|
||||
{
|
||||
var employeeIds = users.Select(user => user.EmployeeId).ToList();
|
||||
return await _context.Goals.Where(x => x.StartDate.Date == start.Date &&
|
||||
x.EndDate.Date == end.Date &&
|
||||
x.DepartmentId == departmentId &&
|
||||
employeeIds.Contains(x.EmployeeId)).ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<ICollection<Goal>> FindMonthlyPersonnelGoals(int departmentId, List<AppUser> users, int year,
|
||||
int month)
|
||||
{
|
||||
var employeeIds = users.Select(user => user.EmployeeId).ToList();
|
||||
return await _context.Goals.Where(x => x.StartDate.Year == year && x.StartDate.Month == month &&
|
||||
x.EndDate.Year == year && x.EndDate.Month == month &&
|
||||
x.DepartmentId == departmentId &&
|
||||
employeeIds.Contains(x.EmployeeId)).ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<ICollection<Goal>> FindPersonnelGoalsByMonthRange(int departmentId, List<AppUser> users, MonthRange monthRange)
|
||||
{
|
||||
var employeeIds = users.Select(user => user.EmployeeId).ToList();
|
||||
return await _context.Goals.AsNoTracking().Where(x => x.StartDate.Date >= monthRange.StartTime.Date &&
|
||||
x.EndDate.Date <= monthRange.EndTime.Date &&
|
||||
x.DepartmentId == departmentId &&
|
||||
employeeIds.Contains(x.EmployeeId)).ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<ICollection<Goal>> FindPersonnelGoalsByMonthRange(int departmentId, List<string> employeeIds, MonthRange monthRange)
|
||||
{
|
||||
return await _context.Goals.Where(x => x.StartDate.Date >= monthRange.StartTime.Date &&
|
||||
x.EndDate.Date <= monthRange.EndTime.Date &&
|
||||
x.DepartmentId == departmentId &&
|
||||
employeeIds.Contains(x.EmployeeId)).ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<ICollection<Goal>> FindDepartmentGoalsByMonthRange(int departmentId, MonthRange monthRange)
|
||||
{
|
||||
return await _context.Goals.AsNoTracking().Where(x => x.StartDate.Date >= monthRange.StartTime.Date &&
|
||||
x.EndDate.Date <= monthRange.EndTime.Date &&
|
||||
x.EmployeeId == null &&
|
||||
x.DepartmentId == departmentId).ToListAsync();
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using LCU.Library.Internal.IncentiveApiModels.Entities;
|
||||
|
||||
namespace LCU.API.Internal.Operations.IncentiveApiLib.Repositories;
|
||||
|
||||
public interface IAccountingRepository
|
||||
{
|
||||
Task<AccountingSummary> SaveAccountingSummaryAsync(AccountingSummary summary);
|
||||
Task<AccountingSummary> GetAccountingSummaryAsync(DateTime payoutPeriod);
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
using LCU.Library.Internal.IncentiveApiModels.Entities;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace LCU.API.Internal.Operations.IncentiveApiLib.Repositories;
|
||||
|
||||
public interface IActivityChangeRepository
|
||||
{
|
||||
public Task<ActivityStateChange> SaveActivityStateChange(ActivityStateChange stateChange);
|
||||
public Task<List<ActivityStateChange>> GetActivityChangesByActivityId(int activityId);
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
using LCU.Library.Internal.IncentiveApiModels.Entities;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace LCU.API.Internal.Operations.IncentiveApiLib.Repositories;
|
||||
|
||||
public interface IActivityDetailUpdateRepository
|
||||
{
|
||||
Task<ActivityDetailUpdate> SaveActivityDetailUpdate(ActivityDetailUpdate activityDetailUpdate);
|
||||
Task<List<ActivityDetailUpdate>> GetActivityDetailUpdatesByActivityDetailId(int activityDetailId);
|
||||
Task<ActivityDetail> GetActivityDetailById(int activityDetailId);
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
using LCU.Library.Internal.IncentiveApiModels.Common;
|
||||
using LCU.Library.Internal.IncentiveApiModels.Entities;
|
||||
using LCU.Library.Internal.IncentiveApiModels.Enums;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace LCU.API.Internal.Operations.IncentiveApiLib.Repositories;
|
||||
|
||||
public interface IActivityRepository
|
||||
{
|
||||
Task<int> SaveActivityAsync(Activity activity);
|
||||
Task SaveActivityRangeAsync(List<Activity> activities);
|
||||
Task<IReadOnlyCollection<Activity>> GetActivitiesAsync();
|
||||
Task<PagedResult<Activity>> GetActivitiesAsync(int page, int limit);
|
||||
Task<PagedResult<Activity>> GetPagedFilteredActivities(ActivityFilter filter);
|
||||
Task<Activity> GetActivityById(int activityId);
|
||||
Task<ICollection<Activity>> FindEmployeeActivityByMonthRange(AppUser appUser, MonthRange monthRange);
|
||||
Task<ICollection<Activity>> FindDepartmentActivityByMonthRange(int departmentId, MonthRange monthRange);
|
||||
Task<Activity> GetActivityIncludingPayouts(int activityId);
|
||||
Task<IReadOnlyCollection<Activity>> FindUnmatchedReferrals();
|
||||
Task<IReadOnlyCollection<Activity>> FindPendingEStatementsReferrals();
|
||||
Task<Activity> FindUnmatchedSaleForReferral(Activity referral);
|
||||
EStatementDeliveryMethod FindDeliveryMethodForEStatement(Activity referral, List<EStatementDeliveryMethod> eStatementActivities);
|
||||
Task<Activity> FindDuplicateSaleAsync(Activity activity);
|
||||
Task<Activity> FindDuplicateReferralAsync(Activity activity);
|
||||
Task<Activity> FindActivityForStateUpdate(Activity activity);
|
||||
Task<Activity> FindActivityForDetailUpdate(Activity activity);
|
||||
public void UpdateActivityRangeAsync(List<Activity> activities);
|
||||
Task<List<Activity>> GetActivitiesBySourceId(List<string> sourceId);
|
||||
Task<List<Activity>> GetAllActivitiesWithPayoutsForEmployee(string employeeId);
|
||||
Task<Activity> FindDuplicateReferralBySourceIdAsync(Activity activity);
|
||||
Task<List<Activity>> FindById(List<int> ids, bool withDetail = true, bool withTracking = true);
|
||||
Task<List<Activity>> FindLatestBySourceId(List<string> sourceIds, bool withDetail = true, bool withTracking = true);
|
||||
Task<List<Activity>> FindGapInsuranceEligible(MonthRange monthRange, bool withTracking = true);
|
||||
Task<List<Activity>> GetFilteredActivities(MonthRange monthRange, Product product, ActivityState activityState, bool withTracking = true);
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using LCU.Library.Internal.IncentiveApiModels.Entities;
|
||||
using LCU.Library.Internal.IncentiveApiModels.Enums;
|
||||
|
||||
namespace LCU.API.Internal.Operations.IncentiveApiLib.Repositories;
|
||||
|
||||
public interface IActivityTransferActionRepository
|
||||
{
|
||||
Task<int> SaveTransferAsync(ActivityTransferAction activityTransferAction);
|
||||
Task<ActivityTransferAction> GetTransferActionById(int id);
|
||||
Task<ActivityTransferAction> GetRequestedTransferActionByActivityId(int requestedActivityId);
|
||||
Task<IReadOnlyCollection<ActivityTransferAction>> GetActiveTransfersForStateAsync(ActivityState state);
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using LCU.Library.Internal.IncentiveApiModels.Entities;
|
||||
using LCU.Library.Internal.IncentiveApiModels.Enums;
|
||||
|
||||
namespace LCU.API.Internal.Operations.IncentiveApiLib.Repositories;
|
||||
|
||||
public interface ICommissionRepository
|
||||
{
|
||||
Task<Commission> FindExistingCommission(int id);
|
||||
Task<Commission> FindExistingCommission(Role role, Product product);
|
||||
Task<IReadOnlyCollection<Commission>> GetCommissionsAsync();
|
||||
Task SaveCommission(Commission commission);
|
||||
Task<IList<Commission>> FindProductCommissions(Role employeeRole);
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using LCU.Library.Internal.IncentiveApiModels.Entities;
|
||||
using LCU.Library.Internal.IncentiveApiModels.Common;
|
||||
|
||||
namespace LCU.API.Internal.Operations.IncentiveApiLib.Repositories;
|
||||
|
||||
public interface IDeadActivityRepository
|
||||
{
|
||||
public Task<DeadActivity> SaveDeadActivity(DeadActivity newActivity);
|
||||
public Task<List<DeadActivity>> SaveDeadActivityRange(List<DeadActivity> newActivities);
|
||||
public Task<List<DeadActivity>> GetAllDeadActivities();
|
||||
public Task<DeadActivity> GetDeadActivityById(int activityId);
|
||||
public Task<PagedResult<DeadActivity>> GetPagedDeadActivities(int page, int limit);
|
||||
public Task<List<DeadActivity>> GetDeadActivitiesBySourceIds(List<string> sourceIds);
|
||||
public Task<PagedResult<DeadActivity>> GetFilteredDeadActivities(DeadActivityFilter filter);
|
||||
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using LCU.Library.Internal.IncentiveApiModels.Entities;
|
||||
using LCU.Library.Internal.IncentiveApiModels.Common;
|
||||
using LCU.Library.Internal.IncentiveApiModels.Results;
|
||||
using LCU.Library.Internal.IncentiveApiModels.Requests;
|
||||
|
||||
namespace LCU.API.Internal.Operations.IncentiveApiLib.Repositories;
|
||||
|
||||
public interface IDepartmentRepository
|
||||
{
|
||||
Task<IReadOnlyCollection<Department>> GetAllDepartmentsAsync();
|
||||
Task<Department> FindDepartmentByIdAsync(int id);
|
||||
Task<Department> SaveDepartmentAsync(Department department);
|
||||
Task<Department> FindOrCreateDepartment(ActiveEmployeeResult employee);
|
||||
Task<List<AppUser>> FindDepartmentAssignments(DepartmentPersonnelSearchRequest searchRequest);
|
||||
Task<Department> FindUserHomeDepartment(AppUser appUser);
|
||||
Task<Department> FindDepartmentByBranchName(string name);
|
||||
Task<IReadOnlyCollection<Department>> FindDepartmentsByRegionAsync(Region region);
|
||||
Task<IReadOnlyCollection<UserDepartment>> FindCurrentDepartmentAssignments(int departmentId);
|
||||
Task<Department> GetDepartmentByDayforceGlCode(int glCode);
|
||||
Task<IReadOnlyCollection<UserDepartment>> FindDepartmentAssignmentsForRange(int departmentId, MonthRange monthRange);
|
||||
Task<IReadOnlyCollection<Department>> FindDepartmentsByBusinessLineAsync(BusinessLine businessLine);
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using LCU.Library.Internal.IncentiveApiModels.Entities;
|
||||
|
||||
namespace LCU.API.Internal.Operations.IncentiveApiLib.Repositories;
|
||||
|
||||
public interface IGoalChangeRepository
|
||||
{
|
||||
Task<int> SaveGoalChangeAsync(GoalChange goalChange);
|
||||
Task<IReadOnlyCollection<GoalChange>> GetGoalChangesAsync();
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using LCU.Library.Internal.IncentiveApiModels.Common;
|
||||
using LCU.Library.Internal.IncentiveApiModels.Entities;
|
||||
using LCU.Library.Internal.IncentiveApiModels.Requests;
|
||||
|
||||
namespace LCU.API.Internal.Operations.IncentiveApiLib.Repositories;
|
||||
|
||||
public interface IGoalRepository
|
||||
{
|
||||
Task<int> SaveGoalAsync(Goal goalChange);
|
||||
Task<IReadOnlyCollection<Goal>> GetGoalsAsync(GoalSearchRequest goalSearchRequest);
|
||||
Task<Goal> FindExistingGoal(GoalChange goalChange);
|
||||
Task<ICollection<Goal>> FindDepartmentGoals(int departmentId, DateTime start, DateTime end);
|
||||
Task<ICollection<Goal>> FindAnnualDepartmentGoals(int departmentId, int year);
|
||||
Task<ICollection<Goal>> FindPersonnelGoals(int departmentId, List<AppUser> users, DateTime start, DateTime end);
|
||||
Task<ICollection<Goal>> FindMonthlyPersonnelGoals(int departmentId, List<AppUser> users, int year, int month);
|
||||
Task<ICollection<Goal>> FindPersonnelGoalsByMonthRange(int departmentId, List<AppUser> users, MonthRange monthRange);
|
||||
Task<ICollection<Goal>> FindPersonnelGoalsByMonthRange(int departmentId, List<string> employeeIds, MonthRange monthRange);
|
||||
Task<ICollection<Goal>> FindDepartmentGoalsByMonthRange(int departmentId, MonthRange monthRange);
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using LCU.Library.Internal.IncentiveApiModels.Entities;
|
||||
|
||||
namespace LCU.API.Internal.Operations.IncentiveApiLib.Repositories;
|
||||
|
||||
public interface IPayoutRepository
|
||||
{
|
||||
Task<int> SavePayoutLineItemAsync(PayoutLineItem lineItem);
|
||||
Task SavePayoutLineItemRangeAsync(IReadOnlyCollection<PayoutLineItem> lineItems);
|
||||
Task<IReadOnlyCollection<PayoutLineItem>> GetPayoutLineItemsForPeriodAsync(DateTime forPeriod);
|
||||
Task<PayoutLineItem> GetPayoutLineItemById(int payoutId);
|
||||
Task<IReadOnlyCollection<PayoutLineItem>> GetEmployeePayoutsForPeriod(AppUser appUser, DateTime forPeriod);
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using LCU.Library.Internal.IncentiveApiModels.Entities;
|
||||
using LCU.Library.Internal.IncentiveApiModels.Enums;
|
||||
|
||||
namespace LCU.API.Internal.Operations.IncentiveApiLib.Repositories;
|
||||
|
||||
public interface IProcessorHistoryRepository
|
||||
{
|
||||
public Task<Process> SaveProcessorRunHistory(ProcessorHistoryRecord newHistoryRecord);
|
||||
public Task<ProcessorHistoryRecord> GetProcessorRunHistory(Process process);
|
||||
public Task<List<ProcessorHistoryRecord>> GetAllProcessorRunHistory();
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using LCU.Library.Internal.IncentiveApiModels.Entities;
|
||||
|
||||
namespace LCU.API.Internal.Operations.IncentiveApiLib.Repositories;
|
||||
|
||||
public interface IRoleMappingRepository
|
||||
{
|
||||
Task<ICollection<RoleMapping>> GetAllRoleMappingsAsync();
|
||||
Task<RoleMapping> GetRoleMappingByIdAsync(int id);
|
||||
Task<int> UpdateRoleMapping(RoleMapping roleMapping);
|
||||
Task<int> SaveRoleMappingAsync(RoleMapping roleMapping);
|
||||
Task SaveRoleMappingRangeAsync(IEnumerable<RoleMapping> newRoleMappings);
|
||||
Task SaveNewRoleMappingRangeAsync(IEnumerable<RoleMapping> newRoleMappings);
|
||||
Role FindMatchingRoleFromBusinessRole(string queryRole);
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace LCU.API.Internal.Operations.IncentiveApiLib.Repositories;
|
||||
|
||||
public interface IUnitOfWork
|
||||
{
|
||||
Task CompleteAsync();
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using LCU.Library.Internal.IncentiveApiModels.Entities;
|
||||
using LCU.Library.Internal.IncentiveApiModels.Enums;
|
||||
|
||||
namespace LCU.API.Internal.Operations.IncentiveApiLib.Repositories;
|
||||
|
||||
public interface IUserAssignmentRepository
|
||||
{
|
||||
Task<ICollection<UserAssignment>> GetAllUserAssignmentsAsync();
|
||||
Task<UserAssignment> FindExistingAssignmentAsync(AssignmentType assignmentType, string key);
|
||||
Task<UserAssignment> IsUserAssigned(AssignmentType assignmentType, AppUser appUser);
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
using LCU.Library.Internal.IncentiveApiModels.Entities;
|
||||
using LCU.Library.Internal.IncentiveApiModels.Requests;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace LCU.API.Internal.Operations.IncentiveApiLib.Repositories;
|
||||
|
||||
public interface IUserDepartmentHistoryRepository
|
||||
{
|
||||
Task<List<UserDepartmentHistory>> SaveUserDepartmentUpdate(UserDepartment currentUserDepartment, DepartmentUpdateRequest departmentUpdateRequest);
|
||||
Task<List<UserPermissionHistory>> GetUserDepartmentHistory(int employeeId);
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using LCU.Library.Internal.IncentiveApiModels.Entities;
|
||||
|
||||
namespace LCU.API.Internal.Operations.IncentiveApiLib.Repositories;
|
||||
|
||||
public interface IUserPermissionHistoryRepository
|
||||
{
|
||||
Task<List<UserPermissionHistory>> SaveUserPermissionsInsertion(IEnumerable<UserPermission> newPermissions);
|
||||
Task<List<UserPermissionHistory>> SaveUserPermissionsDeletion(IEnumerable<UserPermission> newPermissions);
|
||||
Task<List<UserPermissionHistory>> GetUserPermissionHistory(int employeeId);
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using LCU.Library.Internal.IncentiveApiModels.Entities;
|
||||
using LCU.Library.Internal.IncentiveApiModels.Common;
|
||||
using LCU.Library.Internal.IncentiveApiModels.Requests;
|
||||
|
||||
namespace LCU.API.Internal.Operations.IncentiveApiLib.Repositories;
|
||||
|
||||
public interface IUserRepository
|
||||
{
|
||||
Task<ICollection<AppUser>> GetAllUsersAsync();
|
||||
Task<ICollection<AppUser>> GetAllUsersToPayoutAsync(MonthRange range);
|
||||
Task<AppUser> FindUserByEmployeeIdAsync(string employeeId);
|
||||
Task<AppUser> FindUserByAdEmailAsync(string adEmail);
|
||||
Task<AppUser> FindActiveUserByEmployeeIdAsync(string employeeId);
|
||||
Task<AppUser> FindActiveUserByAdEmailAsync(string adEmail);
|
||||
Task<AppUser> FindUserAsync(string employeeId, string adEmail);
|
||||
Task<ICollection<AppUser>> FindUsersByHomeDepartmentsAsync(List<string> homeDepartments);
|
||||
Task<ICollection<AppUser>> FindUsersByBaseRolesAsync(List<string> baseRoles);
|
||||
Task<AppUser> SaveUserAsync(AppUser newAppUser);
|
||||
Task<ICollection<AppUser>> SaveUserRangeAsync(List<AppUser> newUsers);
|
||||
Task<ICollection<AppUser>> SearchUsersAsync(UserSearchRequest userSearchRequest);
|
||||
Task<IReadOnlyCollection<UserSummary>> GetAllUserSummariesAsync();
|
||||
IReadOnlyCollection<UserSummary> GetTerminatedUsersAsync(MonthRange monthRange);
|
||||
}
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using LCU.Library.Internal.IncentiveApiModels.Entities;
|
||||
using LCU.API.Internal.Operations.IncentiveApiLib.Persistence;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace LCU.API.Internal.Operations.IncentiveApiLib.Repositories;
|
||||
|
||||
public class PayoutRepository : BaseRepository, IPayoutRepository
|
||||
{
|
||||
public PayoutRepository(IApplicationDbContext context) : base(context)
|
||||
{
|
||||
}
|
||||
|
||||
public async Task<int> SavePayoutLineItemAsync(PayoutLineItem lineItem)
|
||||
{
|
||||
await _context.PayoutLineItems.AddAsync(lineItem);
|
||||
return lineItem.Id;
|
||||
}
|
||||
|
||||
public async Task SavePayoutLineItemRangeAsync(IReadOnlyCollection<PayoutLineItem> lineItems)
|
||||
{
|
||||
await _context.PayoutLineItems.AddRangeAsync(lineItems);
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyCollection<PayoutLineItem>> GetPayoutLineItemsForPeriodAsync(DateTime forPeriod)
|
||||
{
|
||||
var checkDate = new DateTime(forPeriod.Year, forPeriod.Month, 1);
|
||||
return await _context.PayoutLineItems
|
||||
.Where(p => p.ActivityPeriod.Date == checkDate.Date)
|
||||
.OrderBy(t => t.Id)
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<PayoutLineItem> GetPayoutLineItemById(int payoutId)
|
||||
{
|
||||
return await _context.PayoutLineItems.FindAsync(payoutId);
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyCollection<PayoutLineItem>> GetEmployeePayoutsForPeriod(AppUser appUser, DateTime forPeriod)
|
||||
{
|
||||
var checkDate = new DateTime(forPeriod.Year, forPeriod.Month, 1);
|
||||
return await _context.PayoutLineItems.AsNoTracking()
|
||||
.Include(x => x.ActivityPayouts).ThenInclude(y => y.Activity)
|
||||
.Where(p => p.ActivityPeriod.Date == checkDate.Date &&
|
||||
p.EmployeeId == appUser.EmployeeId)
|
||||
.OrderBy(t => t.Id)
|
||||
.ToListAsync();
|
||||
}
|
||||
}
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using LCU.Library.Internal.IncentiveApiModels.Entities;
|
||||
using LCU.Library.Internal.IncentiveApiModels.Enums;
|
||||
using LCU.API.Internal.Operations.IncentiveApiLib.Persistence;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Serilog;
|
||||
|
||||
namespace LCU.API.Internal.Operations.IncentiveApiLib.Repositories;
|
||||
|
||||
public class ProcessorHistoryRepository : BaseRepository, IProcessorHistoryRepository
|
||||
{
|
||||
public ProcessorHistoryRepository(IApplicationDbContext context) : base(context)
|
||||
{
|
||||
}
|
||||
|
||||
public async Task<Process> SaveProcessorRunHistory(ProcessorHistoryRecord newHistoryRecord)
|
||||
{
|
||||
var existingProcess = await _context.ProcessorRunHistory.FindAsync(newHistoryRecord.Process);
|
||||
if (existingProcess == null)
|
||||
{
|
||||
await _context.ProcessorRunHistory.AddAsync(newHistoryRecord);
|
||||
return newHistoryRecord.Process;
|
||||
}
|
||||
|
||||
if (newHistoryRecord.LastSuccessfulRunTime < existingProcess.LastSuccessfulRunTime)
|
||||
{
|
||||
Log.Warning(
|
||||
$"ProcessorHistoryRepository Saved over a record with older data." +
|
||||
$"Old LastSuccess {existingProcess.LastSuccessfulRunTime} New LastSuccess: {newHistoryRecord.LastSuccessfulRunTime}");
|
||||
}
|
||||
|
||||
if (newHistoryRecord.LastProcessedRecordTimestamp < existingProcess.LastProcessedRecordTimestamp)
|
||||
{
|
||||
Log.Warning(
|
||||
$"ProcessorHistoryRepository Saved over a record with older data." +
|
||||
$"Old LastProcessed {existingProcess.LastProcessedRecordTimestamp} New LastProcessed: {newHistoryRecord.LastProcessedRecordTimestamp}");
|
||||
}
|
||||
|
||||
if (newHistoryRecord.LastProcessedRecordId < existingProcess.LastProcessedRecordId)
|
||||
{
|
||||
Log.Warning(
|
||||
$"ProcessorHistoryRepository Saved over a record with older data." +
|
||||
$"Old ID {existingProcess.LastProcessedRecordId} New ID: {newHistoryRecord.LastProcessedRecordId}");
|
||||
}
|
||||
|
||||
existingProcess.LastProcessedRecordId = newHistoryRecord.LastProcessedRecordId;
|
||||
existingProcess.LastProcessedRecordTimestamp = newHistoryRecord.LastProcessedRecordTimestamp;
|
||||
existingProcess.LastSuccessfulRunTime = newHistoryRecord.LastSuccessfulRunTime;
|
||||
return existingProcess.Process;
|
||||
}
|
||||
|
||||
public async Task<ProcessorHistoryRecord> GetProcessorRunHistory(Process process)
|
||||
{
|
||||
return await _context.ProcessorRunHistory.FindAsync(process);
|
||||
}
|
||||
|
||||
public async Task<List<ProcessorHistoryRecord>> GetAllProcessorRunHistory()
|
||||
{
|
||||
return await _context.ProcessorRunHistory.ToListAsync();
|
||||
}
|
||||
}
|
||||
+65
@@ -0,0 +1,65 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using LCU.Library.Internal.IncentiveApiModels.Entities;
|
||||
using LCU.API.Internal.Operations.IncentiveApiLib.Persistence;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace LCU.API.Internal.Operations.IncentiveApiLib.Repositories;
|
||||
|
||||
public class RoleMappingRepository : BaseRepository, IRoleMappingRepository
|
||||
{
|
||||
public RoleMappingRepository(IApplicationDbContext context) : base(context)
|
||||
{
|
||||
}
|
||||
|
||||
public async Task<ICollection<RoleMapping>> GetAllRoleMappingsAsync()
|
||||
{
|
||||
return await _context.RoleMappings.OrderBy(x => x.BusinessRole).ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<RoleMapping> GetRoleMappingByIdAsync(int id)
|
||||
{
|
||||
return await _context.RoleMappings.FirstOrDefaultAsync(x => x.Id == id);
|
||||
}
|
||||
|
||||
public async Task<int> UpdateRoleMapping(RoleMapping roleMapping)
|
||||
{
|
||||
_context.RoleMappings.Update(roleMapping);
|
||||
return roleMapping.Id;
|
||||
}
|
||||
|
||||
public async Task<int> SaveRoleMappingAsync(RoleMapping roleMapping)
|
||||
{
|
||||
await _context.RoleMappings.AddAsync(roleMapping);
|
||||
return roleMapping.Id;
|
||||
}
|
||||
|
||||
public async Task SaveRoleMappingRangeAsync(IEnumerable<RoleMapping> roleMappings)
|
||||
{
|
||||
await _context.RoleMappings.AddRangeAsync(roleMappings);
|
||||
}
|
||||
|
||||
public async Task SaveNewRoleMappingRangeAsync(IEnumerable<RoleMapping> roleMappings)
|
||||
{
|
||||
await _context.RoleMappings.AddRangeAsync(roleMappings);
|
||||
}
|
||||
|
||||
public Role FindMatchingRoleFromBusinessRole(string queryRole)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(queryRole))
|
||||
{
|
||||
return Role.MISSING_ROLE;
|
||||
}
|
||||
|
||||
var returnRole = _context.RoleMappings.Where(x => x.BusinessRole.ToUpper() == queryRole.ToUpper()).FirstOrDefault()
|
||||
?.ApplicationRole;
|
||||
|
||||
if (returnRole == null)
|
||||
{
|
||||
returnRole = Role.MISSING_ROLE;
|
||||
}
|
||||
|
||||
return returnRole;
|
||||
}
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
using System.Threading.Tasks;
|
||||
using LCU.API.Internal.Operations.IncentiveApiLib.Persistence;
|
||||
|
||||
namespace LCU.API.Internal.Operations.IncentiveApiLib.Repositories;
|
||||
|
||||
public class UnitOfWork : IUnitOfWork
|
||||
{
|
||||
private readonly ApplicationDbContext _context;
|
||||
|
||||
public UnitOfWork(ApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task CompleteAsync()
|
||||
{
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using LCU.Library.Internal.IncentiveApiModels.Entities;
|
||||
using LCU.Library.Internal.IncentiveApiModels.Enums;
|
||||
using LCU.API.Internal.Operations.IncentiveApiLib.Persistence;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace LCU.API.Internal.Operations.IncentiveApiLib.Repositories;
|
||||
|
||||
public class UserAssignmentRepository : BaseRepository, IUserAssignmentRepository
|
||||
{
|
||||
public UserAssignmentRepository(IApplicationDbContext context) : base(context)
|
||||
{
|
||||
}
|
||||
|
||||
public async Task<ICollection<UserAssignment>> GetAllUserAssignmentsAsync()
|
||||
{
|
||||
return await _context.UserAssignments.Include(x => x.AppUser).ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<UserAssignment> FindExistingAssignmentAsync(AssignmentType assignmentType, string key)
|
||||
{
|
||||
return await _context.UserAssignments
|
||||
.Include(x => x.AppUser)
|
||||
.FirstOrDefaultAsync(x => x.Type == assignmentType && x.Key == key);
|
||||
}
|
||||
|
||||
public async Task<UserAssignment> IsUserAssigned(AssignmentType assignmentType, AppUser appUser)
|
||||
{
|
||||
return await _context.UserAssignments
|
||||
.Include(x => x.AppUser)
|
||||
.FirstOrDefaultAsync(x => x.Type == assignmentType
|
||||
&& x.AppUser.EmployeeId == appUser.EmployeeId);
|
||||
}
|
||||
}
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using LCU.API.Internal.Operations.IncentiveApiLib.Persistence;
|
||||
using LCU.Library.Internal.IncentiveApiModels.Entities;
|
||||
using LCU.Library.Internal.IncentiveApiModels.Requests;
|
||||
|
||||
namespace LCU.API.Internal.Operations.IncentiveApiLib.Repositories;
|
||||
|
||||
public class UserDepartmentHistoryRepository : BaseRepository, IUserDepartmentHistoryRepository
|
||||
{
|
||||
public UserDepartmentHistoryRepository(IApplicationDbContext context) : base(context)
|
||||
{
|
||||
}
|
||||
|
||||
public async Task<List<UserDepartmentHistory>> SaveUserDepartmentUpdate(UserDepartment currentUserDepartment, DepartmentUpdateRequest departmentUpdateRequest)
|
||||
{
|
||||
var userDepartmentHistoryUpdates = new List<UserDepartmentHistory>();
|
||||
|
||||
if (currentUserDepartment.Role.Name != departmentUpdateRequest.Role)
|
||||
{
|
||||
userDepartmentHistoryUpdates.Add(new UserDepartmentHistory
|
||||
{
|
||||
UserDepartmentId = currentUserDepartment.Id,
|
||||
FieldName = "Role",
|
||||
NewValue = departmentUpdateRequest.Role,
|
||||
OldValue = currentUserDepartment.Role.Name
|
||||
});
|
||||
}
|
||||
|
||||
if (currentUserDepartment.SlidingPayout != departmentUpdateRequest.SlidingPayout)
|
||||
{
|
||||
userDepartmentHistoryUpdates.Add(new UserDepartmentHistory
|
||||
{
|
||||
UserDepartmentId = currentUserDepartment.Id,
|
||||
FieldName = "SlidingPayout",
|
||||
NewValue = departmentUpdateRequest.SlidingPayout.ToString(CultureInfo.InvariantCulture),
|
||||
OldValue = currentUserDepartment.SlidingPayout.ToString(CultureInfo.InvariantCulture)
|
||||
});
|
||||
}
|
||||
|
||||
if (currentUserDepartment.EndDate?.Date != departmentUpdateRequest.EndDate?.Date)
|
||||
{
|
||||
userDepartmentHistoryUpdates.Add(new UserDepartmentHistory
|
||||
{
|
||||
UserDepartmentId = currentUserDepartment.Id,
|
||||
FieldName = "EndDate",
|
||||
NewValue = departmentUpdateRequest.EndDate.ToString(),
|
||||
OldValue = currentUserDepartment.EndDate.ToString()
|
||||
});
|
||||
}
|
||||
|
||||
await _context.UserDepartmentHistories.AddRangeAsync(userDepartmentHistoryUpdates);
|
||||
return userDepartmentHistoryUpdates;
|
||||
}
|
||||
|
||||
public async Task<List<UserPermissionHistory>> GetUserDepartmentHistory(int employeeId)
|
||||
{
|
||||
return await _context.UserPermissionHistories.Where(history => history.EmployeeId == employeeId).ToListAsync();
|
||||
}
|
||||
}
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using LCU.Library.Internal.IncentiveApiModels.Entities;
|
||||
using LCU.API.Internal.Operations.IncentiveApiLib.Persistence;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace LCU.API.Internal.Operations.IncentiveApiLib.Repositories;
|
||||
|
||||
public class UserPermissionHistoryRepository : BaseRepository, IUserPermissionHistoryRepository
|
||||
{
|
||||
public UserPermissionHistoryRepository(IApplicationDbContext context) : base(context)
|
||||
{
|
||||
}
|
||||
|
||||
public async Task<List<UserPermissionHistory>> SaveUserPermissionsInsertion(IEnumerable<UserPermission> newPermissions)
|
||||
{
|
||||
var newPermissionHistory = newPermissions.Select(userPermission => new UserPermissionHistory
|
||||
{
|
||||
Id = 0,
|
||||
Source = "Manual Assignment",
|
||||
EmployeeId = Convert.ToInt32(userPermission.AppUser.EmployeeId),
|
||||
Permission = userPermission.Permission.Code,
|
||||
PermissionAdded = true
|
||||
})
|
||||
.ToList();
|
||||
|
||||
await _context.UserPermissionHistories.AddRangeAsync(newPermissionHistory);
|
||||
return newPermissionHistory;
|
||||
}
|
||||
|
||||
public async Task<List<UserPermissionHistory>> SaveUserPermissionsDeletion(IEnumerable<UserPermission> newPermissions)
|
||||
{
|
||||
var newPermissionHistory = newPermissions.Select(userPermission => new UserPermissionHistory
|
||||
{
|
||||
Id = 0,
|
||||
Source = "Manual Assignment",
|
||||
EmployeeId = Convert.ToInt32(userPermission.AppUser.EmployeeId),
|
||||
Permission = userPermission.Permission.Code,
|
||||
PermissionRemoved = true
|
||||
})
|
||||
.ToList();
|
||||
|
||||
await _context.UserPermissionHistories.AddRangeAsync(newPermissionHistory);
|
||||
return newPermissionHistory;
|
||||
}
|
||||
|
||||
public async Task<List<UserPermissionHistory>> GetUserPermissionHistory(int employeeId)
|
||||
{
|
||||
return await _context.UserPermissionHistories.Where(history => history.EmployeeId == employeeId).ToListAsync();
|
||||
}
|
||||
}
|
||||
+223
@@ -0,0 +1,223 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using LCU.API.Internal.Operations.IncentiveApiLib.Persistence;
|
||||
using LCU.Library.Internal.IncentiveApiModels.Common;
|
||||
using LCU.Library.Internal.IncentiveApiModels.Entities;
|
||||
using LCU.Library.Internal.IncentiveApiModels.Enums;
|
||||
using LCU.Library.Internal.IncentiveApiModels.Requests;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace LCU.API.Internal.Operations.IncentiveApiLib.Repositories;
|
||||
|
||||
public class UserRepository : BaseRepository, IUserRepository
|
||||
{
|
||||
public UserRepository(IApplicationDbContext context) : base(context)
|
||||
{
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyCollection<UserSummary>> GetAllUserSummariesAsync()
|
||||
{
|
||||
return await _context.AppUsers
|
||||
.Select(x => new UserSummary()
|
||||
{
|
||||
EmployeeId = x.EmployeeId,
|
||||
AdEmail = x.AdEmail,
|
||||
FullName = x.FullName,
|
||||
ManagerKey = x.ManagerKey,
|
||||
Terminated = x.Terminated
|
||||
})
|
||||
.OrderBy(x => x.EmployeeId)
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public IReadOnlyCollection<UserSummary> GetTerminatedUsersAsync(MonthRange monthRange)
|
||||
{
|
||||
if (!monthRange.IsSingleMonth)
|
||||
{
|
||||
throw new ArgumentException("Can only request terminated employees for a single month");
|
||||
}
|
||||
|
||||
return _context.AppUsers
|
||||
.Where(x => x.Terminated != null
|
||||
&& x.Terminated >= monthRange.StartTime
|
||||
&& x.Terminated <= monthRange.EndTime)
|
||||
.ToList()
|
||||
.GroupJoin(_context.PayoutLineItems,
|
||||
user => user.EmployeeId,
|
||||
pli => pli.EmployeeId,
|
||||
(x, pli) => new UserSummary()
|
||||
{
|
||||
EmployeeId = x.EmployeeId,
|
||||
AdEmail = x.AdEmail,
|
||||
FullName = x.FullName,
|
||||
ManagerKey = x.ManagerKey,
|
||||
Terminated = x.Terminated,
|
||||
AssociatedPayouts = pli.ToList()
|
||||
})
|
||||
.OrderBy(x => x.EmployeeId)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public async Task<ICollection<AppUser>> GetAllUsersAsync()
|
||||
{
|
||||
return await _context.AppUsers
|
||||
.Include(user => user.Departments)
|
||||
.ThenInclude(userDept => userDept.Department)
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<ICollection<AppUser>> GetAllUsersToPayoutAsync(MonthRange range)
|
||||
{
|
||||
return await _context.AppUsers
|
||||
.AsNoTracking()
|
||||
.Include(user => user.Departments)
|
||||
.ThenInclude(userDept => userDept.Department)
|
||||
.Where(x => x.Terminated == null
|
||||
&& x.Departments.Any(y => y.Department.State == DepartmentState.Active
|
||||
&& y.StartDate.Date <= range.StartTime.Date
|
||||
&& (y.EndDate == null || y.EndDate >= range.EndTime.Date)))
|
||||
.Distinct()
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<AppUser> FindUserAsync(string employeeId, string adEmail)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(employeeId))
|
||||
{
|
||||
return await FindUserByEmployeeIdAsync(employeeId);
|
||||
}
|
||||
|
||||
return await FindUserByAdEmailAsync(adEmail);
|
||||
}
|
||||
|
||||
public async Task<AppUser> FindUserByEmployeeIdAsync(string employeeId)
|
||||
{
|
||||
return await _context.AppUsers
|
||||
.Include(user => user.AdditionalPermissions)
|
||||
.Include(user => user.Departments)
|
||||
.ThenInclude(userDept => userDept.Department)
|
||||
.Include(user => user.Assignments)
|
||||
.Where(user => user.EmployeeId == employeeId)
|
||||
.OrderByDescending(user => user.Terminated)
|
||||
.FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
public async Task<AppUser> FindUserByAdEmailAsync(string adEmail)
|
||||
{
|
||||
return await _context.AppUsers
|
||||
.Include(user => user.AdditionalPermissions)
|
||||
.Include(user => user.Departments)
|
||||
.ThenInclude(userDept => userDept.Department)
|
||||
.Include(user => user.Assignments)
|
||||
.Where(user => user.AdEmail.ToLower() == adEmail.ToLower())
|
||||
.OrderByDescending(user => user.Terminated)
|
||||
.FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
public async Task<AppUser> FindActiveUserByEmployeeIdAsync(string employeeId)
|
||||
{
|
||||
return await _context.AppUsers
|
||||
.Include(user => user.AdditionalPermissions)
|
||||
.Include(user => user.Departments)
|
||||
.ThenInclude(userDept => userDept.Department)
|
||||
.Include(user => user.Assignments)
|
||||
.Where(user => user.EmployeeId == employeeId && user.Terminated == null)
|
||||
.FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
public async Task<AppUser> FindActiveUserByAdEmailAsync(string adEmail)
|
||||
{
|
||||
return await _context.AppUsers
|
||||
.Include(user => user.AdditionalPermissions)
|
||||
.Include(user => user.Departments)
|
||||
.ThenInclude(userDept => userDept.Department)
|
||||
.Include(user => user.Assignments)
|
||||
.Where(user => user.AdEmail.ToLower() == adEmail.ToLower() && user.Terminated == null)
|
||||
.FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
public async Task<ICollection<AppUser>> FindUsersByHomeDepartmentsAsync(List<string> homeDepartments)
|
||||
{
|
||||
return await _context.AppUsers.Where(user => homeDepartments.Contains(user.HomeDepartment)).ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<ICollection<AppUser>> FindUsersByBaseRolesAsync(List<string> baseRoles)
|
||||
{
|
||||
return await _context.AppUsers.Where(user => baseRoles.Contains(user.BaseRole)).ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<AppUser> SaveUserAsync(AppUser newAppUser)
|
||||
{
|
||||
await _context.AppUsers.AddAsync(newAppUser);
|
||||
return newAppUser;
|
||||
}
|
||||
|
||||
public async Task<ICollection<AppUser>> SaveUserRangeAsync(List<AppUser> newUsers)
|
||||
{
|
||||
await _context.AppUsers.AddRangeAsync(newUsers);
|
||||
return newUsers;
|
||||
}
|
||||
|
||||
public async Task<ICollection<AppUser>> SearchUsersAsync(UserSearchRequest userSearchRequest)
|
||||
{
|
||||
var users = _context.AppUsers.Include(user => user.Departments).AsQueryable();
|
||||
if (!string.IsNullOrWhiteSpace(userSearchRequest.Query))
|
||||
{
|
||||
var cleanQuery = userSearchRequest.Query.ToLower().Trim();
|
||||
var emailQuery = BuildEmailQuery(cleanQuery);
|
||||
|
||||
users = users.Where(user => user.AdEmail.ToLower().Contains(cleanQuery) ||
|
||||
user.AdEmail.ToLower().Contains(emailQuery) ||
|
||||
user.FullName.ToLower().Contains(cleanQuery) ||
|
||||
user.EmployeeId.ToLower().Contains(cleanQuery));
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(userSearchRequest.FirstName))
|
||||
{
|
||||
users = users.Where(user => user.FullName.ToLower().Contains(userSearchRequest.FirstName.ToLower()));
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(userSearchRequest.LastName))
|
||||
{
|
||||
users = users.Where(user => user.FullName.ToLower().Contains(userSearchRequest.LastName.ToLower()));
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(userSearchRequest.MiddleInit))
|
||||
{
|
||||
users = users.Where(user => user.FullName.ToLower().EndsWith($" {userSearchRequest.MiddleInit.ToLower()}"));
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(userSearchRequest.Role))
|
||||
{
|
||||
var foundRoles = Role.SearchRoles(userSearchRequest.Role);
|
||||
users = users.Where(user => user.Departments.Any(dept => foundRoles.Contains(dept.Role)));
|
||||
}
|
||||
|
||||
if (userSearchRequest.DnaOrgNbr != null)
|
||||
{
|
||||
users = users.Where(user => user.Departments.Any(dept => dept.Department.DnaOrgNbr == userSearchRequest.DnaOrgNbr));
|
||||
}
|
||||
|
||||
return await users.ToListAsync();
|
||||
}
|
||||
|
||||
private string BuildEmailQuery(string cleanQuery)
|
||||
{
|
||||
if (cleanQuery.Contains(","))
|
||||
{
|
||||
var transform = cleanQuery.Split(",");
|
||||
if (transform?.Length == 2)
|
||||
{
|
||||
return transform[1].Trim() + transform[0].Trim();
|
||||
}
|
||||
}
|
||||
else if (cleanQuery.Contains(" "))
|
||||
{
|
||||
return cleanQuery.Replace(" ", "");
|
||||
}
|
||||
|
||||
return cleanQuery;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user