Exemplo n.º 1
0
        public async Task DepositWithdrawTHBFromAccount1234()
        {
            //Deposit THB 1000
            AccountTransactionRequest request = new AccountTransactionRequest
            {
                AccountNumber = 1234,
                Currency      = "THB",
                Amount        = 1000
            };

            var result = await _accountRepository.Deposit(request);

            Assert.AreEqual(true, result.Successful, "Deposit THB 1000 to account number 1234 should be successful");

            //Withdraw THB 6000 afterward
            request = new AccountTransactionRequest
            {
                AccountNumber = 1234,
                Currency      = "THB",
                Amount        = 6000
            };

            result = await _accountRepository.Withdraw(request);

            Assert.AreEqual(true, result.Successful, "Withdraw THB 6000 after deposit THb 1000 to account number 1234 should be successful");
            Assert.AreEqual(1234, result.AccountNumber, "Withdraw THB 6000 after deposit THb 1000 to account number 1234 should return AccuntNumber: 1234");
            Assert.AreEqual("THB", result.Currency, "Withdraw THB 6000 after deposit THb 1000 to account number 1234 should successfully deposit to currency THB");
            Assert.AreEqual(0, result.Balance, "Withdraw THB 6000 after deposit THb 1000 to account number 1234, balance should be 0");

            Assert.AreEqual(null, result.AccountBalances, "Withdraw should not return Account Balances items");

            var resultPost = await _accountRepository.Balance(1234);

            Assert.AreEqual(2, resultPost.AccountBalances.Count, "Withdraw THB 6000 after deposit THb 1000 should not add new row to account balances (remain 2 rows)");
        }
Exemplo n.º 2
0
        public async Task DepositUSDandSGDToAccount3456()
        {
            AccountTransactionRequest request = new AccountTransactionRequest
            {
                AccountNumber = 3456,
                Currency      = "USD",
                Amount        = 372
            };

            var result = await _accountRepository.Deposit(request);

            Assert.AreEqual(true, result.Successful, "Deposit USD 372 to account number 3456 should be successful");
            Assert.AreEqual(3456, result.AccountNumber, "Deposit USD 372 to account number 3456 should return AccuntNumber: 1234");
            Assert.AreEqual("USD", result.Currency, "Deposit USD 372 to account number 3456 should successfully deposit to currency USD");
            Assert.AreEqual(372, result.Balance, "Deposit USD 372 to account number 3456, balance should be 372");

            Assert.AreEqual(null, result.AccountBalances, "Deposit should not return Account Balances items");

            var resultPost = await _accountRepository.Balance(3456);

            Assert.AreEqual(2, resultPost.AccountBalances.Count, "Deposit USD 372 should add new row to account balances (becomes 2 rows)");

            //deposit SGD
            request = new AccountTransactionRequest
            {
                AccountNumber = 3456,
                Currency      = "SGD",
                Amount        = 317
            };

            result = await _accountRepository.Deposit(request);

            Assert.AreEqual(true, result.Successful, "Deposit SGD 317 to account number 3456 should be successful");
            Assert.AreEqual(3456, result.AccountNumber, "Deposit SGD 317 to account number 3456 should return AccuntNumber: 1234");
            Assert.AreEqual("SGD", result.Currency, "Deposit SGD 317 to account number 3456 should successfully deposit to currency SGD");
            Assert.AreEqual(317, result.Balance, "Deposit SGD 317 to account number 3456, balance should be 317");

            //Negative
            Assert.AreEqual(null, result.AccountBalances, "Deposit should not return Account Balances items");

            resultPost = await _accountRepository.Balance(3456);

            Assert.AreEqual(3, resultPost.AccountBalances.Count, "Deposit SGD 165 should add new row to account balances (become 3 rows)");
        }
Exemplo n.º 3
0
        public async Task FailedDepositTHBToAccount1224()
        {
            AccountTransactionRequest request = new AccountTransactionRequest
            {
                AccountNumber = 1224,
                Currency      = "THB",
                Amount        = 1000
            };

            var result = await _accountRepository.Deposit(request);

            Assert.AreEqual(false, result.Successful, "Deposit THB 1000 to account number 1224 should be NOT successful");
            Assert.AreEqual(1224, result.AccountNumber, "Deposit THB 1000 to account number 1224 should return AccuntNumber: 1234");
            Assert.AreNotEqual("Successful", result.Message, "Deposit THB 1000 to account number 1224 should return error message");

            Assert.AreEqual(null, result.Currency, "Deposit THB 1000 to account number 1224 should return currency null (not successful)");
            Assert.AreEqual(null, result.Balance, "Deposit THB 1000 to account number 1224, should return null balance");

            Assert.AreEqual(null, result.AccountBalances, "Deposit should not return Account Balances items");
        }
Exemplo n.º 4
0
        public async Task DepositTHBToAccount3456()
        {
            AccountTransactionRequest request = new AccountTransactionRequest
            {
                AccountNumber = 3456,
                Currency      = "THB",
                Amount        = 1200
            };

            var result = await _accountRepository.Deposit(request);

            Assert.AreEqual(true, result.Successful, "Deposit THB 1200 to account number 3456 should be successful");
            Assert.AreEqual(3456, result.AccountNumber, "Deposit THB 1200 to account number 3456 should return AccuntNumber: 1234");
            Assert.AreEqual("THB", result.Currency, "Deposit THB 1200 to account number 3456 should successfully deposit to currency THB");
            Assert.AreEqual(23700, result.Balance, "Deposit THB 1200 to account number 3456, balance should be 23700");

            Assert.AreEqual(null, result.AccountBalances, "Deposit should not return Account Balances items");

            var resultPost = await _accountRepository.Balance(3456);

            Assert.AreEqual(1, resultPost.AccountBalances.Count, "Deposit THB 1200 should not add new row to account balances (remain 1 rows)");
        }
Exemplo n.º 5
0
        public async Task DepositUSDToAccount1234()
        {
            AccountTransactionRequest request = new AccountTransactionRequest
            {
                AccountNumber = 1234,
                Currency      = "USD",
                Amount        = 125
            };

            var result = await _accountRepository.Deposit(request);

            Assert.AreEqual(true, result.Successful, "Deposit USD 125 to account number 1234 should be successful");
            Assert.AreEqual(1234, result.AccountNumber, "Deposit USD 125 to account number 1234 should return AccuntNumber: 1234");
            Assert.AreEqual("USD", result.Currency, "Deposit USD 125 to account number 1234 should successfully deposit to currency USD");
            Assert.AreEqual(375, result.Balance, "Deposit USD 125 to account number 1234, balance should be 375");

            Assert.AreEqual(null, result.AccountBalances, "Deposit should not return Account Balances items");

            var resultPost = await _accountRepository.Balance(1234);

            Assert.AreEqual(2, resultPost.AccountBalances.Count, "Deposit USD 125 should not add new row to account balances (remain 2 rows)");
        }
Exemplo n.º 6
0
        public async Task DepositSGDToAccount1234()
        {
            AccountTransactionRequest request = new AccountTransactionRequest
            {
                AccountNumber = 1234,
                Currency      = "SGD",
                Amount        = 165
            };

            var result = await _accountRepository.Deposit(request);

            Assert.AreEqual(true, result.Successful, "Deposit SGD 165 to account number 1234 should be successful");
            Assert.AreEqual(1234, result.AccountNumber, "Deposit SGD 165 to account number 1234 should return AccuntNumber: 1234");
            Assert.AreEqual("SGD", result.Currency, "Deposit SGD 165 to account number 1234 should successfully deposit to currency SGD");
            Assert.AreEqual(165, result.Balance, "Deposit SGD 165 to account number 1234, balance should be 165");

            //Negative
            Assert.AreEqual(null, result.AccountBalances, "Deposit should not return Account Balances items");

            var resultPost = await _accountRepository.Balance(1234);

            Assert.AreEqual(3, resultPost.AccountBalances.Count, "Deposit SGD 165 should add new row to account balances (become 3 rows)");
        }
Exemplo n.º 7
0
        public async Task FailedWithdrawTHB7000FromAccount1234_InsufficienceBalance()
        {
            AccountTransactionRequest request = new AccountTransactionRequest
            {
                AccountNumber = 1234,
                Currency      = "THB",
                Amount        = 7000
            };

            var result = await _accountRepository.Withdraw(request);

            Assert.AreEqual(false, result.Successful, "Withdraw THB 7000 to account number 1234 should be successful");
            Assert.AreEqual(1234, result.AccountNumber, "Withdraw THB 7000 to account number 1234 should return AccuntNumber: 1234");
            Assert.AreEqual("Insufficient balance", result.Message, "Withdraw THB 7000 to account number 1234 should not return successful message");
            Assert.AreEqual(null, result.Currency, "Withdraw THB 7000 to account number 1234 should return null as currency");
            Assert.AreEqual(null, result.Balance, "Withdraw THB 7000 to account number 1234, should return null as balance");

            Assert.AreEqual(null, result.AccountBalances, "Withdraw should not return Account Balances items");

            var resultPost = await _accountRepository.Balance(1234);

            Assert.AreEqual(2, resultPost.AccountBalances.Count, "Withdraw THB 7000 should not add new row to account balances (remain 2 rows)");
        }
Exemplo n.º 8
0
        public async Task FailedWithdrawSGDToAccount1234()
        {
            AccountTransactionRequest request = new AccountTransactionRequest
            {
                AccountNumber = 1234,
                Currency      = "SGD",
                Amount        = 165
            };

            var result = await _accountRepository.Withdraw(request);

            Assert.AreEqual(false, result.Successful, "Withdraw SGD 165 to account number 1234 should be successful");
            Assert.AreEqual(1234, result.AccountNumber, "Withdraw SGD 165 to account number 1234 should return AccuntNumber: 1234");
            Assert.AreNotEqual("Successful", result.Message, "Withdraw SGD 165 to account number 1234 should not return successful message");
            Assert.AreEqual(null, result.Currency, "Withdraw SGD 165 to account number 1234 should return null as currency");
            Assert.AreEqual(null, result.Balance, "Withdraw SGD 165 to account number 1234, should return null as balance");

            //Negative
            Assert.AreEqual(null, result.AccountBalances, "Withdraw should not return Account Balances items");

            var resultPost = await _accountRepository.Balance(1234);

            Assert.AreEqual(2, resultPost.AccountBalances.Count, "Withdraw SGD 165 should not add new row to account balances (remain 2 rows)");
        }
Exemplo n.º 9
0
 public async Task <AccountTransactionResponse> Withdraww([FromBody] AccountTransactionRequest request)
 {
     return(await _AccountRep.Withdraw(request));
 }
Exemplo n.º 10
0
 public async Task <AccountTransactionResponse> Deposit([FromBody] AccountTransactionRequest request)
 {
     return(await _AccountRep.Deposit(request));
 }
Exemplo n.º 11
0
        public async Task <AccountTransactionResponse> Deposit(AccountTransactionRequest request, int maxRetry = 10, int retry = 1)
        {
            try
            {
                _logger.LogInformation($"Deposit amount to account number: {request.AccountNumber}");

                var account = await _db.Accounts
                              .Include(a => a.Balances)
                              .FirstOrDefaultAsync(acc => acc.AccountNumber == request.AccountNumber);

                if (account == null)
                {
                    return(ErrorResponse(request.AccountNumber, $"Invalid Account Number: {request.AccountNumber}"));
                }

                var balanceWithCurr = account.Balances.FirstOrDefault(b => b.Currency == request.Currency);

                if (balanceWithCurr == null)
                {
                    account.Balances.Add(new Core.Models.AccountBalance
                    {
                        Currency = request.Currency,
                        Balance  = request.Amount
                    });
                }
                else
                {
                    balanceWithCurr.Balance += request.Amount;
                }

                _db.TransactionHistories.Add(new TransactionHistory
                {
                    AccountNumber   = request.AccountNumber,
                    TransactionType = TransactionType.Deposit,
                    Currency        = request.Currency,
                    Amount          = request.Amount,
                    TransactionTime = DateTime.Now
                });

                await _db.SaveChangesAsync();

                account = await _db.Accounts
                          .Include(a => a.Balances)
                          .FirstOrDefaultAsync(acc => acc.AccountNumber == request.AccountNumber);

                return(ConvertToResponse(account, request.Currency));
            }
            catch (Exception ex)
            {
                if (maxRetry >= retry)
                {
                    _db = new ChilindoContext(_db._options, _db._logger);
                    await Task.Delay(200);

                    retry += 1;
                    return(await Deposit(request, maxRetry, retry));
                }
                else
                {
                    return(ErrorResponse(request.AccountNumber, ex.Message));
                }
            }
        }
Exemplo n.º 12
0
        public async Task <AccountTransactionResponse> Withdraw(AccountTransactionRequest request, int maxRetry = 10, int retry = 1)
        {
            try
            {
                _logger.LogInformation($"Withdraw amount to account number: {request.AccountNumber}");

                var account = await _db.Accounts
                              .Include(a => a.Balances)
                              .FirstOrDefaultAsync(acc => acc.AccountNumber == request.AccountNumber);

                if (account == null)
                {
                    return(ErrorResponse(request.AccountNumber, $"Invalid Account Number: {request.AccountNumber}"));
                }

                var balanceWithCurr = account.Balances.FirstOrDefault(b => b.Currency == request.Currency);

                if (balanceWithCurr == null || balanceWithCurr.Balance < request.Amount)
                {
                    return(new AccountTransactionResponse
                    {
                        AccountNumber = request.AccountNumber,
                        Successful = false,
                        Message = $"Insufficient balance",
                        Currency = request.Currency,
                        Balance = balanceWithCurr.Balance
                    });
                }

                balanceWithCurr.Balance -= request.Amount;

                _db.TransactionHistories.Add(new TransactionHistory
                {
                    AccountNumber   = request.AccountNumber,
                    TransactionType = TransactionType.Withdraw,
                    Currency        = request.Currency,
                    Amount          = request.Amount,
                    TransactionTime = DateTime.Now
                });
                await _db.SaveChangesAsync();

                account = await _db.Accounts
                          .Include(a => a.Balances)
                          .FirstOrDefaultAsync(acc => acc.AccountNumber == request.AccountNumber);

                return(ConvertToResponse(account, request.Currency));
            }
            catch (Exception ex)
            {
                if (maxRetry >= retry)
                {
                    _db = new ChilindoContext(_db._options, _db._logger);
                    await Task.Delay(200);

                    retry += 1;
                    return(await Withdraw(request, maxRetry, retry));
                }
                else
                {
                    return(ErrorResponse(request.AccountNumber, ex.Message));
                }
            }
        }