示例#1
0
        public async Task DeleteAsync(int id, int userId)
        {
            if (await _accountsRepository.IsMainAsync(id, userId))
            {
                throw new ArgumentException("Cannot delete main account.");
            }

            await _accountsRepository.DeleteAsync(id, userId);
        }
        private async Task Handle(MarkAccountsAsDeletedInternalCommand command, IEventPublisher publisher)
        {
            var executionInfo = await _executionInfoRepository.GetAsync <DeleteAccountsData>(OperationName,
                                                                                             command.OperationId);

            if (executionInfo == null || executionInfo.Data.State > DeleteAccountsState.MtCoreAccountsBlocked)
            {
                return;
            }
            if (executionInfo.Data.State < DeleteAccountsState.MtCoreAccountsBlocked)
            {
                throw new Exception($"{nameof(MarkAccountsAsDeletedInternalCommand)} have state {executionInfo.Data.State.ToString()}, but [{DeleteAccountsState.MtCoreAccountsBlocked}] was expected. Throwing to retry in {(long) _settings.Cqrs.RetryDelay.TotalMilliseconds}ms.");
            }

            var validationFailedAccountIds = await ValidateAccountsAsync(executionInfo.Data.AccountIds);

            foreach (var accountToDelete in executionInfo.Data.GetAccountsToDelete()
                     .Except(validationFailedAccountIds.Keys))
            {
                try
                {
                    var account = await _accountsRepository.DeleteAsync(accountToDelete);

                    publisher.PublishEvent(
                        new AccountChangedEvent(
                            account.ModificationTimestamp,
                            OperationName,
                            _convertService.Convert <IAccount, AccountContract>(account),
                            AccountChangedEventTypeContract.Deleted,
                            null,
                            command.OperationId,
                            null));
                }
                catch (Exception exception)
                {
                    await _log.WriteErrorAsync(nameof(DeleteAccountsCommandsHandler), nameof(MarkAccountsAsDeletedInternalCommand),
                                               $"OperationId: [{command.OperationId}]", exception);

                    validationFailedAccountIds.Add(accountToDelete, exception.Message);
                }
            }

            foreach (var failedAccountId in executionInfo.Data.FailedAccountIds.Keys
                     .Concat(validationFailedAccountIds.Keys))
            {
                try
                {
                    var account = (await _accountsRepository.GetAsync(failedAccountId))
                                  .RequiredNotNull(nameof(failedAccountId), $"Account {failedAccountId} does not exist.");

                    var result = await _accountsRepository.UpdateAccountAsync(
                        failedAccountId,
                        false,
                        false);

                    _eventSender.SendAccountChangedEvent(
                        nameof(MarkAccountsAsDeletedInternalCommand),
                        result,
                        AccountChangedEventTypeContract.Updated,
                        $"{command.OperationId}_{failedAccountId}",
                        previousSnapshot: account);
                }
                catch (Exception exception)
                {
                    await _log.WriteErrorAsync(nameof(DeleteAccountsCommandsHandler),
                                               nameof(MarkAccountsAsDeletedInternalCommand), exception);
                }
            }

            _chaosKitty.Meow($"{nameof(MarkAccountsAsDeletedInternalCommand)}: " +
                             "AccountsMarkedAsDeletedEvent: " +
                             $"{command.OperationId}");

            publisher.PublishEvent(new AccountsMarkedAsDeletedEvent(
                                       command.OperationId,
                                       _systemClock.UtcNow.UtcDateTime,
                                       validationFailedAccountIds
                                       ));
        }
示例#3
0
 public async Task DeleteAccountAsync(string skillId)
 {
     await _skillsRepository.DeleteAsync(skillId);
 }