示例#1
0
        private async Task Withdraw(AccountId accountId, Money withdrawAmount)
        {
            var customerId = _customerService.GetCurrentCustomerId();

            IAccount account = await _accountRepository.Find(accountId, customerId);

            if (account is Account withdrawAccount)
            {
                Money localCurrencyAmount = await _currencyExchanger.Convert(withdrawAmount, withdrawAccount.Currency);

                Debit debit = _accountFactory.NewDebit(withdrawAccount, localCurrencyAmount, DateTime.UtcNow);

                var currentBalance = withdrawAccount.GetCurrentBalance();
                if (currentBalance.Subtract(debit.Amount).Amount < 0)
                {
                    _logger.LogWarning($"{nameof(Withdraw)} Account {accountId} balance insufficient");
                    _outputPort?.OutOfFunds();
                    return;
                }

                await Withdraw(withdrawAccount, debit);

                _outputPort?.Ok(debit, withdrawAccount);
                return;
            }

            _logger.LogWarning($"{nameof(Withdraw)} Account {accountId} not found for customer {customerId}");
            _outputPort?.NotFound();
        }