public void WhenGettingOpenBatchWithNoOpenBatchesNewBatchIsCreated()
        {
            ApplicationService applicationService = new ApplicationService(_ctx);
            MessageServices messageService = new MessageServices(_ctx);
            UserService userService = new UserService(_ctx);
            PaymentAccountService paymentAccountService = new PaymentAccountService(_ctx);
            TransactionBatchService transactionBatchService = new TransactionBatchService(_ctx, _logger);

            var transactionBatchGuid = Guid.NewGuid();

            _ctx.TransactionBatches.Add(new TransactionBatch()
            {
                CreateDate = System.DateTime.Now,
                Id = transactionBatchGuid,
                IsClosed = true,
                TotalDepositAmount = 0,
                TotalNumberOfDeposits = 0,
                TotalWithdrawalAmount = 0,
                TotalNumberOfWithdrawals = 0,
                Transactions = new List<Transaction>()
            });

            var transactionBatch = transactionBatchService.GetOpenBatch();

            Assert.AreNotEqual(transactionBatchGuid, transactionBatch.Id);
        }
        public void WhenBatching3TransactionsWithOpenBatchTransactionsInBatchIs3()
        {
            ApplicationService applicationService = new ApplicationService(_ctx);
            MessageServices messageService = new MessageServices(_ctx);
            UserService userService = new UserService(_ctx);
            PaymentAccountService paymentAccountService = new PaymentAccountService(_ctx);
            TransactionBatchService transactionBatchService = new TransactionBatchService(_ctx, _logger);

            var transactionBatchGuid = Guid.NewGuid();
            var transactionAmount = 2.75;

            _ctx.TransactionBatches.Add(new TransactionBatch()
            {
                CreateDate = System.DateTime.Now,
                Id = transactionBatchGuid,
                IsClosed = true,
                TotalDepositAmount = 0,
                TotalNumberOfDeposits = 0,
                TotalWithdrawalAmount = 0,
                TotalNumberOfWithdrawals = 0,
                Transactions = new List<Transaction>()
            });
            _ctx.SaveChanges();

            var application = applicationService.AddApplication("Test", "http://www.test.com", true);
            var sender = userService.AddUser(application.ApiKey, "*****@*****.**", "pdthx123",
                "*****@*****.**", "1234");
            sender.SecurityPin = "2589";
            userService.UpdateUser(sender);

            var senderPaymentAccount = paymentAccountService.AddPaymentAccount(sender.UserId.ToString(), "Sender PaidThx",
                "053000219", "1234123412", "Checking");

            sender.PaymentAccounts = new System.Collections.ObjectModel.Collection<PaymentAccount>();
            sender.PaymentAccounts.Add(senderPaymentAccount);

            var transaction1 = new Domain.Transaction()
            {
                Amount = 1,
                Category = TransactionCategory.Payment,
                FromAccount = senderPaymentAccount,
                CreateDate = System.DateTime.Now,
                Id = Guid.NewGuid(),
                PaymentChannelType = PaymentChannelType.Single,
                StandardEntryClass = StandardEntryClass.Web,
                Status = TransactionStatus.Submitted,
                Type = TransactionType.Withdrawal,
                User = sender
            };
            var transaction2 = new Domain.Transaction()
            {
                Amount = 2,
                Category = TransactionCategory.Payment,
                FromAccount = senderPaymentAccount,
                CreateDate = System.DateTime.Now,
                Id = Guid.NewGuid(),
                PaymentChannelType = PaymentChannelType.Single,
                StandardEntryClass = StandardEntryClass.Web,
                Status = TransactionStatus.Submitted,
                Type = TransactionType.Withdrawal,
                User = sender
            };
            var transaction3 = new Domain.Transaction()
            {
                Amount = 3,
                Category = TransactionCategory.Payment,
                FromAccount = senderPaymentAccount,
                CreateDate = System.DateTime.Now,
                Id = Guid.NewGuid(),
                PaymentChannelType = PaymentChannelType.Single,
                StandardEntryClass = StandardEntryClass.Web,
                Status = TransactionStatus.Submitted,
                Type = TransactionType.Deposit,
                User = sender
            };

            transactionBatchService.BatchTransactions(new List<Transaction>()
            {
                transaction1,
                transaction2,
                transaction3
            });

            var transactionBatch = transactionBatchService.GetOpenBatch();

            Assert.AreEqual(3, transactionBatch.Transactions.Count);
            Assert.AreEqual(1, transactionBatch.TotalNumberOfDeposits);
            Assert.AreEqual(2, transactionBatch.TotalNumberOfWithdrawals);
        }
        public void WhenBatchingMessageWithOpenBatchWithWithdrawAndDepositTransactionThenTransactionsInBatchIs2()
        {
            ApplicationService applicationService = new ApplicationService(_ctx);
            MessageServices messageService = new MessageServices(_ctx);
            UserService userService = new UserService(_ctx);
            PaymentAccountService paymentAccountService = new PaymentAccountService(_ctx);
            TransactionBatchService transactionBatchService = new TransactionBatchService(_ctx, _logger);

            var transactionBatchGuid = Guid.NewGuid();
            var transactionAmount = 2.75;

            _ctx.TransactionBatches.Add(new TransactionBatch()
            {
                CreateDate = System.DateTime.Now,
                Id = transactionBatchGuid,
                IsClosed = false,
                TotalDepositAmount = 0,
                TotalNumberOfDeposits = 0,
                TotalWithdrawalAmount = 0,
                TotalNumberOfWithdrawals = 0,
                Transactions = new List<Transaction>()
            });
            _ctx.SaveChanges();

            var application = applicationService.AddApplication("Test", "http://www.test.com", true);
            var sender = userService.AddUser(application.ApiKey, "*****@*****.**", "pdthx123",
                "*****@*****.**", "1234");
            sender.SecurityPin = "2589";
            userService.UpdateUser(sender);

            var senderPaymentAccount = paymentAccountService.AddPaymentAccount(sender.UserId.ToString(), "Sender PaidThx",
                "053000219", "1234123412", "Checking");

            sender.PaymentAccounts = new System.Collections.ObjectModel.Collection<PaymentAccount>();
            sender.PaymentAccounts.Add(senderPaymentAccount);

            var recipient = userService.AddUser(application.ApiKey, "*****@*****.**", "pdthx123",
                "*****@*****.**", "1234");

            var recipientPaymentAccount = paymentAccountService.AddPaymentAccount(sender.UserId.ToString(), "Recipient PaidThx",
            "053000219", "1234123412", "Savings");

            recipient.PaymentAccounts = new System.Collections.ObjectModel.Collection<PaymentAccount>();
            recipient.PaymentAccounts.Add(recipientPaymentAccount);

            var message = messageService.AddMessage(application.ApiKey.ToString(), sender.EmailAddress, recipient.EmailAddress,
                senderPaymentAccount.Id.ToString(), transactionAmount, "Test Payment", "Payment", "2589");

            message.Recipient = userService.FindUserByEmailAddress(recipient.EmailAddress);

            transactionBatchService.BatchTransactions(message);

            var transactionBatch = transactionBatchService.GetOpenBatch();

            Assert.AreEqual(transactionBatchGuid, transactionBatch.Id);
            Assert.AreEqual(2, transactionBatch.Transactions.Count);
            Assert.AreEqual(1, transactionBatch.TotalNumberOfDeposits);
            Assert.AreEqual(1, transactionBatch.TotalNumberOfWithdrawals);
        }