public async Task <Transaction> Handle(WithdrawCommand command)
        {
            Account account = await accountReadOnlyRepository.GetAccount(command.AccountId);

            if (account == null)
            {
                throw new AccountNotFoundException($"The account {command.AccountId} does not exists or is already closed.");
            }

            Transaction transaction = Debit.Create(command.CustomerId, Amount.Create(command.Amount));

            account.Withdraw(transaction);

            var domainEvents = account.GetEvents();
            await bus.Publish(domainEvents, command.Header);

            bool received = false;

            for (int i = 0; i < 5; i++)
            {
                received = await accountReadOnlyRepository.CheckTransactionReceived(account.Id, transaction.Id);

                if (received)
                {
                    break;
                }
            }

            //if (!received)
            //{
            //}

            return(transaction);
        }
        public void Withdraw_100()
        {
            //
            // Arrange
            Account account = Account.Create(
                Guid.NewGuid(),
                Amount.Create(1000.0));

            Customer customer = Customer.Create(
                PIN.Create("08724050601"),
                Name.Create("Ivan Paulovich"));

            customer.Register(account);

            Account sut = customer.GetAccounts().First();

            Transaction transaction = Debit.Create(
                customer.Id, Amount.Create(100.0));

            //
            // Act
            sut.Withdraw(transaction);

            //
            // Assert
            var domainEvents = sut.GetEvents();
            var deposited    = domainEvents.Where(e => e is WithdrewDomainEvent).First() as WithdrewDomainEvent;

            Assert.Equal(deposited.Amount.Value, 100.0);
            Assert.Equal(sut.GetCurrentBalance().Value, 900);
        }
        public void Close_Account()
        {
            //
            // Arrange
            Account account = Account.Create(
                Guid.NewGuid(),
                Amount.Create(1000.0));

            Customer customer = Customer.Create(
                PIN.Create("08724050601"),
                Name.Create("Ivan Paulovich"));

            customer.Register(account);

            Account     sut         = customer.GetAccounts().First();
            Transaction transaction = Debit.Create(
                customer.Id, Amount.Create(1000.0));

            sut.Withdraw(transaction);

            //
            // Act
            sut.Close();

            //
            // Assert
            var domainEvents = sut.GetEvents();
            var closed       = domainEvents.Where(e => e is ClosedDomainEvent).Count();

            Assert.NotEqual(closed, 0);
        }
示例#4
0
        public void Withdraw_100()
        {
            //
            // Arrange
            Customer customer = Customer.Create(
                PIN.Create("08724050601"),
                Name.Create("Ivan Paulovich"));

            Account account = Account.Create(
                customer,
                Amount.Create(1000.0));

            customer.Register(account);

            Account sut = customer.GetAccounts().First();

            Transaction transaction = Debit.Create(
                customer.Id, Amount.Create(100.0));

            //
            // Act
            sut.Withdraw(transaction);

            //
            // Assert
            var transactions = sut.GetTransactions();
            var deposited    = transactions.Where(e => e.Id == transaction.Id).First();

            Assert.Equal(deposited.GetAmount().Value, 100.0);
            Assert.Equal(sut.GetCurrentBalance().Value, 900);
        }
示例#5
0
        public void Account_With_200_Balance_Should_Not_Allow_50000_Withdraw()
        {
            //
            // Arrange
            Account sut    = Substitute.For <Account>();
            Credit  credit = Credit.Create(Amount.Create(200));

            sut.Deposit(credit);

            Debit debit = Debit.Create(Amount.Create(5000.0));

            //
            // Act and Assert
            Assert.Throws <InsuficientFundsException>(
                () => sut.Withdraw(debit));
        }
示例#6
0
        public async Task <Debit> Handle(WithdrawMessage command)
        {
            Account account = await accountReadOnlyRepository.Get(command.AccountId);

            if (account == null)
            {
                throw new AccountNotFoundException($"The account {command.AccountId} does not exists or is already closed.");
            }

            Debit debit = Debit.Create(Amount.Create(command.Amount));

            account.Withdraw(debit);

            await accountWriteOnlyRepository.Update(account);

            return(debit);
        }
示例#7
0
        public async Task <Transaction> Handle(WithdrawCommand command)
        {
            Account account = await accountReadOnlyRepository.GetAccount(command.AccountId);

            if (account == null)
            {
                throw new AccountNotFoundException($"The account {command.AccountId} does not exists or is already closed.");
            }

            Transaction transaction = Debit.Create(command.CustomerId, Amount.Create(command.Amount));

            account.Withdraw(transaction);

            var domainEvents = account.GetEvents();
            await bus.Publish(domainEvents, command.Header);

            return(transaction);
        }
示例#8
0
        public void New_Account_With_1000_Balance_Should_Have_900_After_Withdraw()
        {
            //
            // Arrange
            Account sut    = Substitute.For <Account>();
            Credit  credit = Credit.Create(Amount.Create(1000.0));

            sut.Deposit(credit);

            Debit transaction = Debit.Create(Amount.Create(100.0));

            //
            // Act
            sut.Withdraw(transaction);

            //
            // Assert
            Assert.Equal(900, sut.CurrentBalance.Value);
        }
        public void Withdraw_More_Than_Current_Balance()
        {
            //
            // Arrange
            Account account = Account.Create(
                Guid.NewGuid(),
                Amount.Create(1000.0));

            Customer customer = Customer.Create(
                PIN.Create("08724050601"),
                Name.Create("Ivan Paulovich"));

            customer.Register(account);

            Account sut = customer.GetAccounts().First();

            Transaction transaction = Debit.Create(
                customer.Id, Amount.Create(5000.0));

            //
            // Act and Assert
            Assert.Throws <InsuficientFundsException>(
                () => sut.Withdraw(transaction));
        }