public async Task <Account> DeleteAccountAsync(string accountId) { if (accountId == null) { throw new ArgumentNullException(nameof(accountId)); } _dataProtectionService.Validate(); var account = await GetAccountByIdAsync(accountId); if (account == null) { throw new NotFoundException("Account not found"); } var employee = await GetEmployeeByIdAsync(account.EmployeeId); var isPrimary = employee.PrimaryAccountId == accountId; if (isPrimary) { employee.PrimaryAccountId = null; } account.Deleted = true; account.UpdatedAt = DateTime.UtcNow; account.Password = null; account.OtpSecret = null; List <HardwareVaultTask> tasks = new List <HardwareVaultTask>(); foreach (var vault in employee.HardwareVaults) { tasks.Add(_hardwareVaultTaskService.GetAccountDeleteTask(vault.Id, account.Id)); } using (TransactionScope transactionScope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { if (isPrimary) { await _employeeRepository.UpdateOnlyPropAsync(employee, new string[] { nameof(Employee.PrimaryAccountId) }); } await _accountService.UpdateOnlyPropAsync(account, new string[] { nameof(Account.Deleted), nameof(Account.UpdatedAt), nameof(Account.Password), nameof(Account.OtpSecret) }); await _hardwareVaultTaskService.AddRangeTasksAsync(tasks); await _hardwareVaultService.UpdateNeedSyncAsync(employee.HardwareVaults, true); transactionScope.Complete(); } return(account); }
public async Task <List <string> > DeleteSharedAccountAsync(string id) { if (id == null) { throw new ArgumentNullException(nameof(id)); } _dataProtectionService.Validate(); var sharedAccount = await _sharedAccountRepository.GetByIdAsync(id); if (sharedAccount == null) { throw new Exception("Shared Account not found"); } sharedAccount.Deleted = true; // Get all accounts where used this shared account var accounts = await _accountService .Query() .Include(x => x.Employee.HardwareVaults) .Where(x => x.SharedAccountId == sharedAccount.Id && x.Deleted == false) .AsNoTracking() .ToListAsync(); List <HardwareVaultTask> tasks = new List <HardwareVaultTask>(); foreach (var account in accounts) { account.Deleted = true; account.UpdatedAt = DateTime.UtcNow; foreach (var hardwareVault in account.Employee.HardwareVaults) { tasks.Add(_hardwareVaultTaskService.GetAccountDeleteTask(hardwareVault.Id, account.Id)); } } using (TransactionScope transactionScope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { await _sharedAccountRepository.UpdateOnlyPropAsync(sharedAccount, new string[] { nameof(SharedAccount.Deleted) }); await _accountService.UpdateOnlyPropAsync(accounts, new string[] { nameof(Account.Deleted), nameof(Account.UpdatedAt) }); await _hardwareVaultTaskService.AddRangeTasksAsync(tasks); transactionScope.Complete(); } return(accounts.SelectMany(x => x.Employee.HardwareVaults.Select(s => s.Id)).ToList()); }