Exemplo n.º 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();
        }
Exemplo n.º 2
0
        public IDebit Withdraw(IAccountFactory entityFactory, PositiveMoney amountToWithdraw)
        {
            if (GetCurrentBalance().LessThan(amountToWithdraw))
            {
                throw new MoneyShouldBePositiveException("Account has not enough funds.");
            }

            var debit = entityFactory.NewDebit(this, amountToWithdraw, DateTime.UtcNow);

            Debits.Add(debit);
            return(debit);
        }
Exemplo n.º 3
0
        /// <inheritdoc />
        public IDebit Withdraw(IAccountFactory entityFactory, PositiveMoney amountToWithdraw)
        {
            if (entityFactory is null)
            {
                throw new ArgumentNullException(nameof(entityFactory));
            }

            if (this.GetCurrentBalance().LessThan(amountToWithdraw))
            {
                throw new MoneyShouldBePositiveException(Messages.AccountHasNotEnoughFunds);
            }

            var debit = entityFactory.NewDebit(this, amountToWithdraw, DateTime.UtcNow);

            this.Debits.Add(debit);
            return(debit);
        }