public async Task <Result> Delete(int id) { try { using (var transactionScope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { var globalCoordinator = await _dataContext.Users.FilterAvailable().FirstOrDefaultAsync(u => u.Id == id); if (globalCoordinator == null) { _loggerAdapter.Debug($"Global coordinator with id {id} was not found"); throw new ResultException(ResultKey.User.Common.UserNotFound); } await _deleteUserService.EnsureCanDeleteUser(id, Role.GlobalCoordinator); _dataContext.Users.Remove(globalCoordinator); await _dataContext.SaveChangesAsync(); await _identityUserRegistrationService.DeleteIdentityUser(globalCoordinator.IdentityUserId); transactionScope.Complete(); } return(Success()); } catch (ResultException e) { _loggerAdapter.Debug(e); return(e.Result); } }
public async Task <Result> Delete(int supervisorId) { try { await _deleteUserService.EnsureCanDeleteUser(supervisorId, Role.Supervisor); using var transactionScope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled); var supervisorUser = await GetSupervisorUser(supervisorId); await EnsureSupervisorHasNoDataCollectors(supervisorUser); AnonymizeSupervisor(supervisorUser); supervisorUser.DeletedAt = _dateTimeProvider.UtcNow; await _identityUserRegistrationService.DeleteIdentityUser(supervisorUser.IdentityUserId); supervisorUser.IdentityUserId = null; await _dataContext.SaveChangesAsync(); transactionScope.Complete(); return(Success()); } catch (ResultException e) { _loggerAdapter.Debug(e); return(e.Result); } async Task EnsureSupervisorHasNoDataCollectors(SupervisorUser supervisorUser) { var dataCollectorInfo = await _dataContext.DataCollectors .Where(dc => dc.Supervisor == supervisorUser) .Select(dc => new { dc, IsDeleted = dc.DeletedAt != null }) .GroupBy(dc => dc.IsDeleted) .Select(g => new { IsDeleted = g.Key, Count = g.Count() }) .ToListAsync(); var notDeletedDataCollectorCount = dataCollectorInfo.SingleOrDefault(dc => !dc.IsDeleted)?.Count; if (notDeletedDataCollectorCount > 0) { throw new ResultException(ResultKey.User.Deletion.CannotDeleteSupervisorWithDataCollectors); } } }
public async Task <Result> Delete(int nationalSocietyId, int technicalAdvisorId) { try { await _deleteUserService.EnsureCanDeleteUser(technicalAdvisorId, Role.TechnicalAdvisor); using var transactionScope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled); await DeleteFromNationalSociety(nationalSocietyId, technicalAdvisorId); await _dataContext.SaveChangesAsync(); transactionScope.Complete(); return(Success()); } catch (ResultException e) { _loggerAdapter.Debug(e); return(e.Result); } }
public async Task <Result> Delete(int managerId) { try { await _deleteUserService.EnsureCanDeleteUser(managerId, Role.Manager); using var transactionScope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled); await DeleteFromDb(managerId); await _dataContext.SaveChangesAsync(); transactionScope.Complete(); return(Success()); } catch (ResultException e) { _loggerAdapter.Debug(e); return(e.Result); } }
public async Task <Result> Delete(int nationalSocietyId, int dataConsumerId) { try { await _deleteService.EnsureCanDeleteUser(dataConsumerId, Role.DataConsumer); using var transactionScope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled); var dataConsumerUser = await _nationalSocietyUserService.GetNationalSocietyUserIncludingNationalSocieties <DataConsumerUser>(dataConsumerId); var userNationalSocieties = dataConsumerUser.UserNationalSocieties; var nationalSocietyReferenceToRemove = userNationalSocieties.SingleOrDefault(uns => uns.NationalSocietyId == nationalSocietyId); if (nationalSocietyReferenceToRemove == null) { return(Error(ResultKey.User.Registration.UserIsNotAssignedToThisNationalSociety)); } var isUsersLastNationalSociety = userNationalSocieties.Count == 1; _dataContext.UserNationalSocieties.Remove(nationalSocietyReferenceToRemove); if (isUsersLastNationalSociety) { _nationalSocietyUserService.DeleteNationalSocietyUser(dataConsumerUser); await _identityUserRegistrationService.DeleteIdentityUser(dataConsumerUser.IdentityUserId); } await _dataContext.SaveChangesAsync(); transactionScope.Complete(); return(Success()); } catch (ResultException e) { _loggerAdapter.Debug(e); return(e.Result); } }