773 lines
35 KiB
C#
773 lines
35 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using LCU.Library.Internal.IncentiveApiModels.Entities;
|
|
using LCU.Library.Internal.IncentiveApiModels.Results;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Serilog;
|
|
|
|
namespace LCU.API.Internal.Operations.IncentiveApiLib.Persistence;
|
|
|
|
public class ApplicationDbIntegrityService : IApplicationDbIntegrityService
|
|
{
|
|
private readonly ApplicationDbContext _applicationDbContext;
|
|
private const string DataIntegrityFailureMessage = "AppUserId maintenance was required, the system was unable to carry out all of this maintenance in an automated fashion and this must be looked into immediately";
|
|
private const string DataIntegrityPartialSuccessMessage = "AppUserId maintenance was required, it was carried out in an automated fashion but this should not have been required and needs to be looked into";
|
|
private const string DataIntegritySuccessMessage = "Data integrity audit finished successfully, no maintenance was required";
|
|
|
|
public ApplicationDbIntegrityService(ApplicationDbContext applicationDbContext)
|
|
{
|
|
_applicationDbContext = applicationDbContext;
|
|
}
|
|
|
|
public async Task<bool> AppUserIdIntegrityMaintenanceSucceeded(CancellationToken cancellationToken)
|
|
{
|
|
try
|
|
{
|
|
EnsureAuditCreateAndUpdateAppUserIds();
|
|
|
|
Log.Information("ApplicationDbIntegrity: RunDataIntegrityAudit method Started");
|
|
|
|
var allMaintenanceResults = new List<DataIntegrityAuditResult>
|
|
{
|
|
await DetermineActivityMaintenanceAuditResult(),
|
|
await DetermineActivityTransferActionMaintenanceAuditResult(),
|
|
await DetermineDeadActivityMaintenanceAuditResult(),
|
|
await DetermineGoalMaintenanceAuditResult(),
|
|
await DetermineGoalChangeMaintenanceAuditResult()
|
|
};
|
|
|
|
Log.Information("ApplicationDbIntegrity: RunDataIntegrityAudit method Ended");
|
|
|
|
var overallMaintenanceResult = await DetermineOverallDataIntegrityAuditResult(allMaintenanceResults);
|
|
|
|
if (!overallMaintenanceResult.MaintenanceAutomationFailed && !overallMaintenanceResult.MaintenanceWasRequired)
|
|
{
|
|
Log.Information(DataIntegritySuccessMessage);
|
|
return true;
|
|
}
|
|
|
|
if (!overallMaintenanceResult.MaintenanceAutomationFailed && overallMaintenanceResult.MaintenanceWasRequired)
|
|
{
|
|
Log.Information(DataIntegrityPartialSuccessMessage);
|
|
throw new Exception(DataIntegrityPartialSuccessMessage);
|
|
}
|
|
|
|
if (!overallMaintenanceResult.MaintenanceAutomationFailed && overallMaintenanceResult.MaintenanceWasRequired)
|
|
{
|
|
Log.Information(DataIntegrityFailureMessage);
|
|
throw new Exception(DataIntegrityFailureMessage);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, $"Failed to execute AppUserId integrity checks, Error: {ex.Message}");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
private async Task<DataIntegrityAuditResult> DetermineOverallDataIntegrityAuditResult(List<DataIntegrityAuditResult> dataIntegrityAuditResults)
|
|
{
|
|
return await Task.FromResult(
|
|
new DataIntegrityAuditResult
|
|
{
|
|
MaintenanceAutomationFailed = dataIntegrityAuditResults.Any(x => x.MaintenanceAutomationFailed),
|
|
MaintenanceWasRequired = dataIntegrityAuditResults.Any(x => x.MaintenanceWasRequired)
|
|
});
|
|
}
|
|
|
|
private async Task<DataIntegrityAuditResult> DetermineActivityMaintenanceAuditResult()
|
|
{
|
|
var dataIntegrityAuditResult = new DataIntegrityAuditResult();
|
|
var targetEmployeeIds = _applicationDbContext.Activities.Where(x => x.EmployeeId != null && x.AppUserId == Guid.Empty)
|
|
.Select(x => x.EmployeeId)
|
|
.Distinct();
|
|
|
|
if (!targetEmployeeIds.Any())
|
|
{
|
|
return dataIntegrityAuditResult;
|
|
}
|
|
|
|
dataIntegrityAuditResult.MaintenanceWasRequired = true;
|
|
|
|
Log.Warning("Found Activity records with empty AppUserIds, this should not happen and needs to be investigated");
|
|
|
|
foreach (var targetEmployeeId in targetEmployeeIds)
|
|
{
|
|
try
|
|
{
|
|
var appUsers = _applicationDbContext.AppUsers.Where(x => x.EmployeeId == targetEmployeeId);
|
|
|
|
if (appUsers.Count() != 1)
|
|
{
|
|
Log.Information($"{appUsers.Count()} returned from AppUser lookup on {targetEmployeeId} during DetermineActivityMaintenanceAuditResult, " +
|
|
$"unable to perform automatic maintenance this needs to be addressed manually");
|
|
dataIntegrityAuditResult.MaintenanceAutomationFailed = true;
|
|
continue;
|
|
}
|
|
|
|
var appUser = await appUsers.FirstAsync();
|
|
foreach (var item in _applicationDbContext.Activities.Where(x => x.EmployeeId == appUser.EmployeeId
|
|
&& x.AppUserId == Guid.Empty))
|
|
{
|
|
item.AppUserId = appUser.Id;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
dataIntegrityAuditResult.MaintenanceAutomationFailed = true;
|
|
Log.Error(ex, ex.Message);
|
|
}
|
|
}
|
|
|
|
return dataIntegrityAuditResult;
|
|
}
|
|
|
|
private async Task<DataIntegrityAuditResult> DetermineActivityTransferActionMaintenanceAuditResult()
|
|
{
|
|
var dataIntegrityAuditResult = new DataIntegrityAuditResult();
|
|
var targetEmployeeIds = _applicationDbContext.ActivityTransferActions.Where(x => x.NewActivityEmployeeId != null && x.NewAppUserId == Guid.Empty)
|
|
.Select(x => x.NewActivityEmployeeId)
|
|
.Distinct();
|
|
|
|
if (!targetEmployeeIds.Any())
|
|
{
|
|
return dataIntegrityAuditResult;
|
|
}
|
|
|
|
dataIntegrityAuditResult.MaintenanceWasRequired = true;
|
|
|
|
Log.Information("Found ActivityTransferAction records with empty AppUserIds, this should not happen and needs to be investigated");
|
|
|
|
foreach (var targetEmployeeId in targetEmployeeIds)
|
|
{
|
|
try
|
|
{
|
|
var appUsers = _applicationDbContext.AppUsers.Where(x => x.EmployeeId == targetEmployeeId);
|
|
|
|
if (appUsers.Count() != 1)
|
|
{
|
|
Log.Information($"{appUsers.Count()} returned from AppUser lookup on {targetEmployeeId} during DetermineActivityTransferActionMaintenanceAuditResult, " +
|
|
$"unable to perform automatic maintenance this needs to be addressed manually");
|
|
dataIntegrityAuditResult.MaintenanceAutomationFailed = true;
|
|
continue;
|
|
}
|
|
|
|
var appUser = await appUsers.FirstAsync();
|
|
foreach (var item in _applicationDbContext.ActivityTransferActions.Where(x => x.NewActivityEmployeeId == appUser.EmployeeId
|
|
&& x.NewAppUserId == Guid.Empty))
|
|
{
|
|
item.NewAppUserId = appUser.Id;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
dataIntegrityAuditResult.MaintenanceAutomationFailed = true;
|
|
Log.Error(ex, ex.Message);
|
|
}
|
|
}
|
|
|
|
return dataIntegrityAuditResult;
|
|
}
|
|
|
|
private void EnsureAuditCreateAndUpdateAppUserIds()
|
|
{
|
|
Log.Information("ApplicationDbIntegrity: EnsureAuditCreateAndUpdateAppUserIds method Started");
|
|
|
|
foreach (var appUser in _applicationDbContext.AppUsers)
|
|
{
|
|
try
|
|
{
|
|
SyncIdsWithDb(appUser, true).GetAwaiter().GetResult();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, ex.Message);
|
|
}
|
|
}
|
|
|
|
Log.Information("ApplicationDbIntegrity: EnsureAuditCreateAndUpdateAppUserIds method Ended");
|
|
}
|
|
|
|
private async Task<DataIntegrityAuditResult> DetermineDeadActivityMaintenanceAuditResult()
|
|
{
|
|
var dataIntegrityAuditResult = new DataIntegrityAuditResult();
|
|
var targetEmployeeIds = _applicationDbContext.DeadActivities.Where(x => x.EmployeeId != null && x.AppUserId == Guid.Empty)
|
|
.Select(x => x.EmployeeId)
|
|
.Distinct();
|
|
|
|
if (!targetEmployeeIds.Any())
|
|
{
|
|
return dataIntegrityAuditResult;
|
|
}
|
|
|
|
dataIntegrityAuditResult.MaintenanceWasRequired = true;
|
|
|
|
Log.Information("Found DeadActivity records with empty AppUserIds, this should not happen and needs to be investigated");
|
|
|
|
foreach (var targetEmployeeId in targetEmployeeIds)
|
|
{
|
|
try
|
|
{
|
|
var appUsers = _applicationDbContext.AppUsers.Where(x => x.EmployeeId == targetEmployeeId);
|
|
|
|
if (appUsers.Count() != 1)
|
|
{
|
|
Log.Information($"{appUsers.Count()} returned from AppUser lookup on {targetEmployeeId} during DetermineDeadActivityMaintenanceAuditResult, " +
|
|
$"unable to perform automatic maintenance this needs to be addressed manually");
|
|
dataIntegrityAuditResult.MaintenanceAutomationFailed = true;
|
|
continue;
|
|
}
|
|
|
|
var appUser = await appUsers.FirstAsync();
|
|
foreach (var item in _applicationDbContext.DeadActivities.Where(x => x.EmployeeId == appUser.EmployeeId
|
|
&& x.AppUserId == Guid.Empty))
|
|
{
|
|
item.AppUserId = appUser.Id;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
dataIntegrityAuditResult.MaintenanceAutomationFailed = true;
|
|
Log.Error(ex, ex.Message);
|
|
}
|
|
}
|
|
|
|
return dataIntegrityAuditResult;
|
|
}
|
|
|
|
private async Task<DataIntegrityAuditResult> DetermineGoalMaintenanceAuditResult()
|
|
{
|
|
var dataIntegrityAuditResult = new DataIntegrityAuditResult();
|
|
var targetEmployeeIds = _applicationDbContext.Goals.Where(x => x.EmployeeId != null && x.AppUserId == Guid.Empty).Select(x => x.EmployeeId).Distinct();
|
|
|
|
if (!targetEmployeeIds.Any())
|
|
{
|
|
return dataIntegrityAuditResult;
|
|
}
|
|
|
|
dataIntegrityAuditResult.MaintenanceWasRequired = true;
|
|
|
|
Log.Information("Found Goal records with empty AppUserIds, this should not happen and needs to be investigated");
|
|
|
|
foreach (var targetEmployeeId in targetEmployeeIds)
|
|
{
|
|
try
|
|
{
|
|
var appUsers = _applicationDbContext.AppUsers.Where(x => x.EmployeeId == targetEmployeeId);
|
|
|
|
if (appUsers.Count() != 1)
|
|
{
|
|
Log.Information($"{appUsers.Count()} returned from AppUser lookup on {targetEmployeeId} during DetermineGoalMaintenanceAuditResult, " +
|
|
$"unable to perform automatic maintenance this needs to be addressed manually");
|
|
dataIntegrityAuditResult.MaintenanceAutomationFailed = true;
|
|
continue;
|
|
}
|
|
|
|
var appUser = await appUsers.FirstAsync();
|
|
foreach (var item in _applicationDbContext.Goals.Where(x => x.EmployeeId == appUser.EmployeeId
|
|
&& x.AppUserId == Guid.Empty))
|
|
{
|
|
item.AppUserId = appUser.Id;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
dataIntegrityAuditResult.MaintenanceAutomationFailed = true;
|
|
Log.Error(ex, ex.Message);
|
|
}
|
|
}
|
|
|
|
return dataIntegrityAuditResult;
|
|
}
|
|
|
|
private async Task<DataIntegrityAuditResult> DetermineGoalChangeMaintenanceAuditResult()
|
|
{
|
|
var dataIntegrityAuditResult = new DataIntegrityAuditResult();
|
|
var targetEmployeeIds = _applicationDbContext.GoalChanges.Where(x => x.PersonId != null && x.AppUserId == Guid.Empty)
|
|
.Select(x => x.PersonId)
|
|
.Distinct();
|
|
|
|
if (!targetEmployeeIds.Any())
|
|
{
|
|
return dataIntegrityAuditResult;
|
|
}
|
|
|
|
dataIntegrityAuditResult.MaintenanceWasRequired = true;
|
|
|
|
Log.Information("Found GoalChange records with empty AppUserIds, this should not happen and needs to be investigated");
|
|
|
|
foreach (var targetEmployeeId in targetEmployeeIds)
|
|
{
|
|
try
|
|
{
|
|
var appUsers = _applicationDbContext.AppUsers.Where(x => x.EmployeeId == targetEmployeeId);
|
|
|
|
if (appUsers.Count() != 1)
|
|
{
|
|
Log.Information($"{appUsers.Count()} returned from AppUser lookup on {targetEmployeeId} during DetermineGoalChangeMaintenanceAuditResult, " +
|
|
$"unable to perform automatic maintenance this needs to be addressed manually");
|
|
dataIntegrityAuditResult.MaintenanceAutomationFailed = true;
|
|
continue;
|
|
}
|
|
|
|
var appUser = await appUsers.FirstAsync();
|
|
foreach (var item in _applicationDbContext.GoalChanges.Where(x => x.PersonId == appUser.EmployeeId
|
|
&& x.AppUserId == Guid.Empty))
|
|
{
|
|
item.AppUserId = appUser.Id;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
dataIntegrityAuditResult.MaintenanceAutomationFailed = true;
|
|
Log.Error(ex, ex.Message);
|
|
}
|
|
}
|
|
|
|
return dataIntegrityAuditResult;
|
|
}
|
|
|
|
public async Task<bool> SyncAppUserUpdate(AppUser appUser)
|
|
{
|
|
try
|
|
{
|
|
foreach (var item in _applicationDbContext.Activities.Where(x => x.CreatedByAppUserId == appUser.Id))
|
|
{
|
|
item.CreatedBy = appUser.AppUserFullNameInFromContextFormat();
|
|
}
|
|
|
|
foreach (var item in _applicationDbContext.Activities.Where(x => x.LastUpdatedByAppUserId == appUser.Id))
|
|
{
|
|
item.LastUpdatedBy = appUser.AppUserFullNameInFromContextFormat();
|
|
}
|
|
|
|
foreach (var item in _applicationDbContext.Activities.Where(x => x.AppUserId == appUser.Id))
|
|
{
|
|
item.EmployeeEmail = appUser.AdEmail;
|
|
item.EmployeeId = appUser.EmployeeId;
|
|
}
|
|
|
|
foreach (var item in _applicationDbContext.ActivityDetailUpdates.Where(x => x.CreatedByAppUserId == appUser.Id))
|
|
{
|
|
item.CreatedBy = appUser.AppUserFullNameInFromContextFormat();
|
|
}
|
|
|
|
foreach (var item in _applicationDbContext.ActivityStateChanges.Where(x => x.CreatedByAppUserId == appUser.Id))
|
|
{
|
|
item.CreatedBy = appUser.AppUserFullNameInFromContextFormat();
|
|
}
|
|
|
|
foreach (var item in _applicationDbContext.ActivityTransferActions.Where(x => x.CreatedByAppUserId == appUser.Id))
|
|
{
|
|
item.CreatedBy = appUser.AppUserFullNameInFromContextFormat();
|
|
}
|
|
|
|
foreach (var item in _applicationDbContext.ActivityTransferActions.Where(x => x.LastUpdatedByAppUserId == appUser.Id))
|
|
{
|
|
item.LastUpdatedBy = appUser.AppUserFullNameInFromContextFormat();
|
|
}
|
|
|
|
foreach (var item in _applicationDbContext.AppUsers.Where(x => x.CreatedByAppUserId == appUser.Id))
|
|
{
|
|
item.CreatedBy = appUser.AppUserFullNameInFromContextFormat();
|
|
}
|
|
|
|
foreach (var item in _applicationDbContext.AppUsers.Where(x => x.LastUpdatedByAppUserId == appUser.Id))
|
|
{
|
|
item.LastUpdatedBy = appUser.AppUserFullNameInFromContextFormat();
|
|
}
|
|
|
|
foreach (var item in _applicationDbContext.Commissions.Where(x => x.CreatedByAppUserId == appUser.Id))
|
|
{
|
|
item.CreatedBy = appUser.AppUserFullNameInFromContextFormat();
|
|
}
|
|
|
|
foreach (var item in _applicationDbContext.Commissions.Where(x => x.LastUpdatedByAppUserId == appUser.Id))
|
|
{
|
|
item.LastUpdatedBy = appUser.AppUserFullNameInFromContextFormat();
|
|
}
|
|
|
|
foreach (var item in _applicationDbContext.DeadActivities.Where(x => x.CreatedByAppUserId == appUser.Id))
|
|
{
|
|
item.CreatedBy = appUser.AppUserFullNameInFromContextFormat();
|
|
}
|
|
|
|
foreach (var item in _applicationDbContext.DeadActivities.Where(x => x.LastUpdatedByAppUserId == appUser.Id))
|
|
{
|
|
item.LastUpdatedBy = appUser.AppUserFullNameInFromContextFormat();
|
|
}
|
|
|
|
foreach (var item in _applicationDbContext.DeadActivities.Where(x => x.AppUserId == appUser.Id))
|
|
{
|
|
item.EmployeeEmail = appUser.AdEmail;
|
|
item.EmployeeId = appUser.EmployeeId;
|
|
}
|
|
|
|
foreach (var item in _applicationDbContext.DepartmentAssignments.Where(x => x.CreatedByAppUserId == appUser.Id))
|
|
{
|
|
item.CreatedBy = appUser.AppUserFullNameInFromContextFormat();
|
|
}
|
|
|
|
foreach (var item in _applicationDbContext.DepartmentAssignments.Where(x => x.LastUpdatedByAppUserId == appUser.Id))
|
|
{
|
|
item.LastUpdatedBy = appUser.AppUserFullNameInFromContextFormat();
|
|
}
|
|
|
|
foreach (var item in _applicationDbContext.DepartmentLineItems.Where(x => x.CreatedByAppUserId == appUser.Id))
|
|
{
|
|
item.CreatedBy = appUser.AppUserFullNameInFromContextFormat();
|
|
}
|
|
|
|
foreach (var item in _applicationDbContext.DepartmentLineItems.Where(x => x.LastUpdatedByAppUserId == appUser.Id))
|
|
{
|
|
item.LastUpdatedBy = appUser.AppUserFullNameInFromContextFormat();
|
|
}
|
|
|
|
foreach (var item in _applicationDbContext.Departments.Where(x => x.CreatedByAppUserId == appUser.Id))
|
|
{
|
|
item.CreatedBy = appUser.AppUserFullNameInFromContextFormat();
|
|
}
|
|
|
|
foreach (var item in _applicationDbContext.Departments.Where(x => x.LastUpdatedByAppUserId == appUser.Id))
|
|
{
|
|
item.LastUpdatedBy = appUser.AppUserFullNameInFromContextFormat();
|
|
}
|
|
|
|
foreach (var item in _applicationDbContext.GoalChanges.Where(x => x.CreatedByAppUserId == appUser.Id))
|
|
{
|
|
item.CreatedBy = appUser.AppUserFullNameInFromContextFormat();
|
|
}
|
|
|
|
foreach (var item in _applicationDbContext.GoalChanges.Where(x => x.AppUserId == appUser.Id))
|
|
{
|
|
item.PersonId = appUser.EmployeeId;
|
|
}
|
|
|
|
foreach (var item in _applicationDbContext.Goals.Where(x => x.CreatedByAppUserId == appUser.Id))
|
|
{
|
|
item.CreatedBy = appUser.AppUserFullNameInFromContextFormat();
|
|
}
|
|
|
|
foreach (var item in _applicationDbContext.Goals.Where(x => x.LastUpdatedByAppUserId == appUser.Id))
|
|
{
|
|
item.LastUpdatedBy = appUser.AppUserFullNameInFromContextFormat();
|
|
}
|
|
|
|
foreach (var item in _applicationDbContext.Goals.Where(x => x.AppUserId == appUser.Id))
|
|
{
|
|
item.EmployeeId = appUser.EmployeeId;
|
|
}
|
|
|
|
foreach (var item in _applicationDbContext.PayoutLineItems.Where(x => x.CreatedByAppUserId == appUser.Id))
|
|
{
|
|
item.CreatedBy = appUser.AppUserFullNameInFromContextFormat();
|
|
}
|
|
|
|
foreach (var item in _applicationDbContext.PayoutLineItems.Where(x => x.AppUserId == appUser.Id))
|
|
{
|
|
item.EmployeeId = appUser.EmployeeId;
|
|
}
|
|
|
|
foreach (var item in _applicationDbContext.RoleMappings.Where(x => x.CreatedByAppUserId == appUser.Id))
|
|
{
|
|
item.CreatedBy = appUser.AppUserFullNameInFromContextFormat();
|
|
}
|
|
|
|
foreach (var item in _applicationDbContext.RoleMappings.Where(x => x.LastUpdatedByAppUserId == appUser.Id))
|
|
{
|
|
item.LastUpdatedBy = appUser.AppUserFullNameInFromContextFormat();
|
|
}
|
|
|
|
foreach (var item in _applicationDbContext.UserDepartmentHistories.Where(x => x.CreatedByAppUserId == appUser.Id)
|
|
)
|
|
{
|
|
item.CreatedBy = appUser.AppUserFullNameInFromContextFormat();
|
|
}
|
|
|
|
foreach (var item in _applicationDbContext.UserPermissionHistories.Where(x => x.CreatedByAppUserId == appUser.Id))
|
|
{
|
|
item.CreatedBy = appUser.AppUserFullNameInFromContextFormat();
|
|
}
|
|
|
|
foreach (var item in _applicationDbContext.UserPermissionHistories.Where(x => x.AppUserId == appUser.Id))
|
|
{
|
|
item.EmployeeId = int.Parse(appUser.EmployeeId);
|
|
}
|
|
|
|
foreach (var item in _applicationDbContext.UserPermissions.Where(x => x.CreatedByAppUserId == appUser.Id))
|
|
{
|
|
item.CreatedBy = appUser.AppUserFullNameInFromContextFormat();
|
|
}
|
|
|
|
await _applicationDbContext.SaveChangesAsync();
|
|
|
|
return true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, ex.Message);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public async Task<bool> SyncIdsWithDb(AppUser appUser, bool limitScopeToAuditCreateAndUpdate = false)
|
|
{
|
|
try
|
|
{
|
|
Log.Information($"ApplicationDbIntegrity: SyncIdsWithDb for User {appUser.FullName} Started");
|
|
foreach (var item in _applicationDbContext.Activities.Where(x => x.CreatedBy == appUser.AppUserFullNameInFromContextFormat()
|
|
&& x.LastUpdatedByAppUserId == Guid.Empty))
|
|
{
|
|
item.CreatedByAppUserId = appUser.Id;
|
|
}
|
|
|
|
foreach (var item in _applicationDbContext.Activities.Where(x => x.LastUpdatedBy == appUser.AppUserFullNameInFromContextFormat()
|
|
&& x.LastUpdatedByAppUserId == Guid.Empty))
|
|
{
|
|
item.LastUpdatedByAppUserId = appUser.Id;
|
|
}
|
|
|
|
if (!limitScopeToAuditCreateAndUpdate)
|
|
{
|
|
foreach (var item in _applicationDbContext.Activities.Where(x => x.EmployeeEmail == appUser.AdEmail
|
|
&& x.EmployeeId == appUser.EmployeeId
|
|
&& x.AppUserId == Guid.Empty))
|
|
{
|
|
item.AppUserId = appUser.Id;
|
|
}
|
|
}
|
|
|
|
foreach (var item in _applicationDbContext.ActivityDetailUpdates.Where(x => x.CreatedBy == appUser.AppUserFullNameInFromContextFormat()
|
|
&& x.CreatedByAppUserId == Guid.Empty))
|
|
{
|
|
item.CreatedByAppUserId = appUser.Id;
|
|
}
|
|
|
|
foreach (var item in _applicationDbContext.ActivityStateChanges.Where(x => x.CreatedBy == appUser.AppUserFullNameInFromContextFormat()
|
|
&& x.CreatedByAppUserId == Guid.Empty))
|
|
{
|
|
item.CreatedByAppUserId = appUser.Id;
|
|
}
|
|
|
|
foreach (var item in _applicationDbContext.ActivityTransferActions.Where(x => x.CreatedBy == appUser.AppUserFullNameInFromContextFormat()
|
|
&& x.CreatedByAppUserId == Guid.Empty))
|
|
{
|
|
item.CreatedByAppUserId = appUser.Id;
|
|
}
|
|
|
|
foreach (var item in _applicationDbContext.ActivityTransferActions.Where(x => x.LastUpdatedBy == appUser.AppUserFullNameInFromContextFormat()
|
|
&& x.LastUpdatedByAppUserId == Guid.Empty))
|
|
{
|
|
item.LastUpdatedByAppUserId = appUser.Id;
|
|
}
|
|
|
|
foreach (var item in _applicationDbContext.AppUsers.Where(x => x.CreatedBy == appUser.AppUserFullNameInFromContextFormat()
|
|
&& x.CreatedByAppUserId == Guid.Empty))
|
|
{
|
|
item.CreatedByAppUserId = appUser.Id;
|
|
}
|
|
|
|
foreach (var item in _applicationDbContext.AppUsers.Where(x => x.LastUpdatedBy == appUser.AppUserFullNameInFromContextFormat()
|
|
&& x.LastUpdatedByAppUserId == Guid.Empty))
|
|
{
|
|
item.LastUpdatedByAppUserId = appUser.Id;
|
|
}
|
|
|
|
foreach (var item in _applicationDbContext.Commissions.Where(x => x.CreatedBy == appUser.AppUserFullNameInFromContextFormat()
|
|
&& x.CreatedByAppUserId == Guid.Empty))
|
|
{
|
|
item.CreatedByAppUserId = appUser.Id;
|
|
}
|
|
|
|
foreach (var item in _applicationDbContext.Commissions.Where(x => x.LastUpdatedBy == appUser.AppUserFullNameInFromContextFormat()
|
|
&& x.LastUpdatedByAppUserId == Guid.Empty))
|
|
{
|
|
item.LastUpdatedByAppUserId = appUser.Id;
|
|
}
|
|
|
|
foreach (var item in _applicationDbContext.DeadActivities.Where(x => x.CreatedBy == appUser.AppUserFullNameInFromContextFormat()
|
|
&& x.CreatedByAppUserId == Guid.Empty))
|
|
{
|
|
item.CreatedByAppUserId = appUser.Id;
|
|
}
|
|
|
|
foreach (var item in _applicationDbContext.DeadActivities.Where(x => x.LastUpdatedBy == appUser.AppUserFullNameInFromContextFormat()
|
|
&& x.LastUpdatedByAppUserId == Guid.Empty))
|
|
{
|
|
item.LastUpdatedByAppUserId = appUser.Id;
|
|
}
|
|
|
|
if (!limitScopeToAuditCreateAndUpdate)
|
|
{
|
|
foreach (var item in _applicationDbContext.DeadActivities.Where(x => x.EmployeeEmail == appUser.AdEmail
|
|
&& x.EmployeeId == appUser.EmployeeId
|
|
&& x.AppUserId == Guid.Empty))
|
|
{
|
|
item.AppUserId = appUser.Id;
|
|
}
|
|
}
|
|
|
|
foreach (var item in _applicationDbContext.DepartmentAssignments.Where(x => x.CreatedBy == appUser.AppUserFullNameInFromContextFormat()
|
|
&& x.CreatedByAppUserId == Guid.Empty))
|
|
{
|
|
item.CreatedByAppUserId = appUser.Id;
|
|
}
|
|
|
|
foreach (var item in _applicationDbContext.DepartmentAssignments.Where(x => x.LastUpdatedBy == appUser.AppUserFullNameInFromContextFormat()
|
|
&& x.LastUpdatedByAppUserId == Guid.Empty))
|
|
{
|
|
item.LastUpdatedByAppUserId = appUser.Id;
|
|
}
|
|
|
|
if (!limitScopeToAuditCreateAndUpdate)
|
|
{
|
|
foreach (var item in _applicationDbContext.DepartmentAssignments.Where(x => x.Employee.AdEmail == appUser.AdEmail
|
|
&& x.Employee.EmployeeId == appUser.EmployeeId
|
|
&& x.AppUserId == Guid.Empty))
|
|
{
|
|
item.AppUserId = appUser.Id;
|
|
}
|
|
}
|
|
|
|
foreach (var item in _applicationDbContext.DepartmentLineItems.Where(x => x.CreatedBy == appUser.AppUserFullNameInFromContextFormat()
|
|
&& x.CreatedByAppUserId == Guid.Empty))
|
|
{
|
|
item.CreatedByAppUserId = appUser.Id;
|
|
}
|
|
|
|
foreach (var item in _applicationDbContext.DepartmentLineItems.Where(x => x.LastUpdatedBy == appUser.AppUserFullNameInFromContextFormat()
|
|
&& x.LastUpdatedByAppUserId == Guid.Empty))
|
|
{
|
|
item.LastUpdatedByAppUserId = appUser.Id;
|
|
}
|
|
|
|
foreach (var item in _applicationDbContext.Departments.Where(x => x.CreatedBy == appUser.AppUserFullNameInFromContextFormat()
|
|
&& x.CreatedByAppUserId == Guid.Empty))
|
|
{
|
|
item.CreatedByAppUserId = appUser.Id;
|
|
}
|
|
|
|
foreach (var item in _applicationDbContext.Departments.Where(x => x.LastUpdatedBy == appUser.AppUserFullNameInFromContextFormat()
|
|
&& x.LastUpdatedByAppUserId == Guid.Empty))
|
|
{
|
|
item.LastUpdatedByAppUserId = appUser.Id;
|
|
}
|
|
|
|
foreach (var item in _applicationDbContext.GoalChanges.Where(x => x.CreatedBy == appUser.AppUserFullNameInFromContextFormat()
|
|
&& x.CreatedByAppUserId == Guid.Empty))
|
|
{
|
|
item.CreatedByAppUserId = appUser.Id;
|
|
}
|
|
|
|
if (!limitScopeToAuditCreateAndUpdate)
|
|
{
|
|
foreach (var item in _applicationDbContext.GoalChanges.Where(x => x.PersonId == appUser.EmployeeId
|
|
&& x.AppUserId == Guid.Empty))
|
|
{
|
|
item.AppUserId = appUser.Id;
|
|
}
|
|
}
|
|
|
|
foreach (var item in _applicationDbContext.Goals.Where(x => x.CreatedBy == appUser.AppUserFullNameInFromContextFormat()
|
|
&& x.CreatedByAppUserId == Guid.Empty))
|
|
{
|
|
item.CreatedByAppUserId = appUser.Id;
|
|
}
|
|
|
|
foreach (var item in _applicationDbContext.Goals.Where(x => x.LastUpdatedBy == appUser.AppUserFullNameInFromContextFormat()
|
|
&& x.LastUpdatedByAppUserId == Guid.Empty))
|
|
{
|
|
item.LastUpdatedByAppUserId = appUser.Id;
|
|
}
|
|
|
|
if (!limitScopeToAuditCreateAndUpdate)
|
|
{
|
|
foreach (var item in _applicationDbContext.Goals.Where(x => x.EmployeeId == appUser.EmployeeId
|
|
&& x.AppUserId == Guid.Empty))
|
|
{
|
|
item.AppUserId = appUser.Id;
|
|
}
|
|
}
|
|
|
|
foreach (var item in _applicationDbContext.PayoutLineItems.Where(x => x.CreatedBy == appUser.AppUserFullNameInFromContextFormat()
|
|
&& x.CreatedByAppUserId == Guid.Empty))
|
|
{
|
|
item.CreatedByAppUserId = appUser.Id;
|
|
}
|
|
|
|
if (!limitScopeToAuditCreateAndUpdate)
|
|
{
|
|
foreach (var item in _applicationDbContext.PayoutLineItems.Where(x => x.EmployeeId == appUser.EmployeeId
|
|
&& x.AppUserId == Guid.Empty))
|
|
{
|
|
item.AppUserId = appUser.Id;
|
|
}
|
|
}
|
|
|
|
foreach (var item in _applicationDbContext.RoleMappings.Where(x => x.CreatedBy == appUser.AppUserFullNameInFromContextFormat()
|
|
&& x.CreatedByAppUserId == Guid.Empty))
|
|
{
|
|
item.CreatedByAppUserId = appUser.Id;
|
|
}
|
|
|
|
foreach (var item in _applicationDbContext.RoleMappings.Where(x => x.LastUpdatedBy == appUser.AppUserFullNameInFromContextFormat()
|
|
&& x.LastUpdatedByAppUserId == Guid.Empty))
|
|
{
|
|
item.LastUpdatedByAppUserId = appUser.Id;
|
|
}
|
|
|
|
foreach (var item in _applicationDbContext.UserDepartmentHistories.Where(x => x.CreatedBy == appUser.AppUserFullNameInFromContextFormat()
|
|
&& x.CreatedByAppUserId == Guid.Empty))
|
|
{
|
|
item.CreatedByAppUserId = appUser.Id;
|
|
}
|
|
|
|
foreach (var item in _applicationDbContext.UserPermissionHistories.Where(x => x.CreatedBy == appUser.AppUserFullNameInFromContextFormat()
|
|
&& x.CreatedByAppUserId == Guid.Empty))
|
|
{
|
|
item.CreatedByAppUserId = appUser.Id;
|
|
}
|
|
|
|
if (!limitScopeToAuditCreateAndUpdate)
|
|
{
|
|
foreach (var item in _applicationDbContext.UserPermissionHistories.Where(x => x.EmployeeId.ToString() == appUser.EmployeeId
|
|
&& x.AppUserId == Guid.Empty))
|
|
{
|
|
item.AppUserId = appUser.Id;
|
|
}
|
|
}
|
|
|
|
foreach (var item in _applicationDbContext.UserPermissions.Where(x => x.CreatedBy == appUser.AppUserFullNameInFromContextFormat()
|
|
&& x.CreatedByAppUserId == Guid.Empty))
|
|
{
|
|
item.CreatedByAppUserId = appUser.Id;
|
|
}
|
|
|
|
if (!limitScopeToAuditCreateAndUpdate)
|
|
{
|
|
foreach (var item in _applicationDbContext.UserPermissions.Where(x => x.AppUser.AdEmail == appUser.AdEmail
|
|
&& x.AppUser.EmployeeId == appUser.EmployeeId
|
|
&& x.AppUserId == Guid.Empty))
|
|
{
|
|
item.AppUserId = appUser.Id;
|
|
}
|
|
}
|
|
|
|
await _applicationDbContext.SaveChangesAsync();
|
|
return true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, ex.Message);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|