/// <summary> /// Withdraw amount for account /// </summary> /// <param name="withdrawAmountRequestDto"></param> /// <returns></returns> public async Task <float> WithdrawAmount(WithdrawAmountRequestDto withdrawAmountRequestDto) { //check account type from account Id var account = await _accountRepository.GetAccountSetting(withdrawAmountRequestDto.AccountId); if (account != null && account.AccountType == (int)AccountType.SavingsAccount) { //allow withdraw if account type is savings account return(await _accountRepository.WithdrawAmount(withdrawAmountRequestDto)); } else if (account != null && account.AccountType == (int)AccountType.DepositAccount) { //get days diff- deposit period in days and created date for current account : Assuming Deposit is on the day of account creation var days = (DateTime.Now - account.AccountCreatedDate).Days; if (days >= account.DepositPeriodInDays) { //allow withdrawing if maturity day has arrived for deposit account return(await _accountRepository.WithdrawAmount(withdrawAmountRequestDto)); } else { throw new Exception("Deposit hasn't reached maturity"); } } throw new Exception("Account doesn't exist"); }
public async Task WithdrawAmount_FromDepositAccount_WhenAccountIsNotMatured_ShouldThrowException() { //Arrange Mock <IAccountRepository> sut = new Mock <IAccountRepository>(); _accountRepo = sut.Object; var withdrawAmountRequest = new WithdrawAmountRequestDto { AccountId = 1, Amount = 20 }; var savingsAccountSetting = new AccountTypeDetailDto { AccountId = 1, AnnualInterestRate = 2, //2% interest rate AccountType = 2, //Deposit account DepositPeriodInDays = 30, InterestPayingFrequency = 1, //Daily, AccountCreatedDate = DateTime.Now.AddDays(-2) }; sut.Setup(x => x.GetAccountSetting(1)).Returns(async() => savingsAccountSetting); var accountService = new AccountService(_accountRepo); //Act and Assert var ex = Assert.ThrowsAsync <Exception>(() => accountService.WithdrawAmount(withdrawAmountRequest)); Assert.AreEqual("Deposit hasn't reached maturity", ex.Message); }
public async Task WithdrawAmount_FromSavingsAccount_ShouldUpdate_Balance() { //Arrange Mock <IAccountRepository> sut = new Mock <IAccountRepository>(); _accountRepo = sut.Object; var withdrawAmountRequest = new WithdrawAmountRequestDto { AccountId = 1, Amount = 20 }; var expected = 10; var savingsAccountSetting = new AccountTypeDetailDto { AccountId = 1, AnnualInterestRate = 2, //2% interest rate AccountType = 1, //Savings account DepositPeriodInDays = 0, InterestPayingFrequency = 0 }; sut.Setup(x => x.GetAccountSetting(1)).Returns(async() => savingsAccountSetting); sut.Setup(x => x.WithdrawAmount(withdrawAmountRequest)).Returns(async() => expected); var accountService = new AccountService(_accountRepo); //Act var res = await accountService.WithdrawAmount(withdrawAmountRequest); //Act Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(expected, res); }
public async Task WithdrawAmount_FromAccountId_ShouldUpdate_Balance() { var options = new DbContextOptionsBuilder <BankContext>() .UseInMemoryDatabase(databaseName: "OBS") .Options; using (var context = new BankContext(options)) { context.Accounts.Add(new Account { Id = 1, AccountNumber = 222616, Balance = 10050, ClientId = 1, BankId = 1, CreatedDate = DateTime.Now }); context.SaveChanges(); } var mockMapper = new Mock <IMapper>(); var dto = new WithdrawAmountRequestDto { AccountId = 1, Amount = 10 }; using (var context = new BankContext(options)) { AccountRepository accountRepository = new AccountRepository(context, mockMapper.Object); var amt = await accountRepository.WithdrawAmount(dto); Assert.AreEqual(10040, amt); context.Database.EnsureDeleted(); } }
/// <summary> /// Withdraw amount from given accountId and return final balance /// </summary> /// <param name="withdrawAmountRequestDto"></param> /// <returns></returns> public async Task <float> WithdrawAmount(WithdrawAmountRequestDto withdrawAmountRequestDto) { //withdraw amount from account var account = _bankContext.Accounts.FirstOrDefault(x => x.Id == withdrawAmountRequestDto.AccountId); if (account != null && account.Balance >= withdrawAmountRequestDto.Amount) { account.Balance -= withdrawAmountRequestDto.Amount; _bankContext.SaveChanges(); return(account.Balance); } //exception if requested withdraw is more than available balance throw new Exception("Insufficient Balance"); }
public async Task <float> WithdrawAmount(WithdrawAmountRequestDto withdrawAmountRequestDto) { var balance = await _accountService.WithdrawAmount(withdrawAmountRequestDto); return(balance); }