public async Task <TransactionModel> Withdraw(DepositWithdrawalModel model) { if (model.Amount <= 0) { throw new AppException("Invalid amount"); } var account = await _repo.GetById <BankAccount>(model.SenderAccountId); if (account == null) { throw new AppException("Unavailable bank account"); } if (string.Equals(account.BankAccountStatus.ToString(), "frozen", StringComparison.InvariantCultureIgnoreCase)) { throw new AppException("Your bank account is frozen!"); } if (account.Balance < model.Amount) { throw new AppException("Cannot withdraw more than available balance"); } account.Balance -= model.Amount; var transactionModel = _mapper.Map <TransactionModel>(model); return(transactionModel); }
public async Task <IActionResult> Withdraw([FromBodyAttribute] DepositWithdrawalModel model) { var transactionModel = await _serv.Withdraw(model); transactionModel.TransactionType = Enum.GetName(typeof(TransactionType), TransactionType.Withdrawal); var transaction = await _serv.CreateTransaction(transactionModel); return(Ok()); }
public async Task UpdateCreditScore(DepositWithdrawalModel model) { var account = await _repo.GetByIdWithInclude <BankAccount>(model.SenderAccountId, o => o.Customer); var customer = account.Customer; var minAmountToPay = (account.Balance / account.Period) * 1 + account.InterestRate; if (model.Amount >= minAmountToPay) { customer.CreditScore += 3; } else { customer.CreditScore -= 3; } await _repo.Add(new CustomerCreditScore { CreditScore = customer.CreditScore, CustomerId = customer.Id }); }