Exemplo n.º 1
0
        public async Task RemoveHardwareVaultAsync(string vaultId, VaultStatusReason reason, bool isNeedBackup = false)
        {
            if (vaultId == null)
            {
                throw new ArgumentNullException(nameof(vaultId));
            }

            _dataProtectionService.Validate();

            var vault = await _hardwareVaultService.GetVaultByIdAsync(vaultId);

            if (vault == null)
            {
                throw new Exception($"Vault {vaultId} not found.");
            }

            if (vault.Status != VaultStatus.Reserved && vault.Status != VaultStatus.Active &&
                vault.Status != VaultStatus.Locked && vault.Status != VaultStatus.Suspended)
            {
                throw new Exception($"Vault {vaultId} in a status that does not allow to remove.");
            }

            using (TransactionScope transactionScope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                await _hardwareVaultTaskService.DeleteTasksByVaultIdAsync(vaultId);

                await _workstationService.DeleteProximityByVaultIdAsync(vaultId);

                if (vault.Status == VaultStatus.Reserved && !vault.IsStatusApplied)
                {
                    await _hardwareVaultService.SetReadyStatusAsync(vault);
                }
                else
                {
                    var employeeVaultsCount = vault.Employee.HardwareVaults.Count();

                    if (employeeVaultsCount == 1)
                    {
                        await RemovePrimaryAccountIdAsync(vault.EmployeeId);

                        await _accountService.DeleteAccountsByEmployeeIdAsync(vault.EmployeeId);
                    }

                    vault.StatusReason = reason;
                    await _hardwareVaultService.SetDeactivatedStatusAsync(vault);
                }

                transactionScope.Complete();
            }
        }