public async Task CreateFixedTermDeposit(FixedTermDepositCreateModel fixedTermDeposit, int userId) { if (userId <= 0) { throw new CustomException(400, "Id inválido"); } // Must determine if there is enough balance in the account to afford the fixed term deposit opening // We need first the currency to call the stored procedure which calculates the balance var account = _unitOfWork.Accounts.GetById(fixedTermDeposit.AccountId); if (account == null) { throw new CustomException(404, "Cuenta inexistente"); } // Check if the account id belongs to the current user if (account.UserId != userId) { throw new CustomException(403, "La cuenta no le pertenece"); } string currency = account.Currency; // Execute the respective stored procedure to get the balance var balance = _accountBusiness.GetAccountBalance(userId, currency); if (balance - fixedTermDeposit.Amount < 0) { // If there isn't enough balance in the account, we cannot continue //throw new CustomException(400, "No hay suficiente dinero para realizar la operación."); throw new BusinessException(ErrorMessages.Not_Enough_Balance); } // We have enough balance. Lets create the fixed term deposit // First make a payment transaction Transactions newTransaction = new Transactions { AccountId = fixedTermDeposit.AccountId, Amount = fixedTermDeposit.Amount, Concept = "Plazo Fijo (Apertura)", Type = "Payment", CategoryId = 3 }; _unitOfWork.Transactions.Insert(newTransaction); // Having the transaction placed, it's time to make the fixed term deposit // Mapping the model received to entity model FixedTermDeposits newFixedTermDeposit = _mapper.Map <FixedTermDeposits>(fixedTermDeposit); // Insert the new fixed term deposit to unit of work _unitOfWork.FixedTermDeposits.Insert(newFixedTermDeposit); // Save changes to database await _unitOfWork.Complete(); }
public async Task <IActionResult> CreateFixedTermDeposit([FromBody] FixedTermDepositCreateModel fixedTermDeposit) { //try //{ // Get the current user's id logged to the API var userId = int.Parse(User.Claims.First(i => i.Type == "UserId").Value); // Delegate the logic to business await _fixedTermDepositBusiness.CreateFixedTermDeposit(fixedTermDeposit, userId); return(Ok()); /*} * catch * { * throw; * }*/ }
public async void Create_Fail_Not_Enough_Balance() { // Arrange FixedTermDepositCreateModel fixedTermDeposit = new FixedTermDepositCreateModel { AccountId = 1, Amount = 1000 }; // Act Func <Task> result = () => _controller.CreateFixedTermDeposit(fixedTermDeposit); // Assert var exception = await Assert.ThrowsAsync <CustomException>(result); Assert.Equal(400, exception.StatusCode); // TODO: Assert for equality of message error }
public async void Create_Fail_Not_Self_Account() { // Arrange // Create new user manually Users newUser = new Users() { Email = "*****@*****.**", FirstName = "pepe", LastName = "pompin", Password = "******" }; context.Users.Add(newUser); await _unitOfWork.Users.AddAccounts(newUser); // Create transactions Transactions firstTransaction = new Transactions { AccountId = 3, Amount = 100, CategoryId = 4, Type = "Topup" }; context.Transactions.Add(firstTransaction); context.SaveChanges(); // It should be a better way to arrange the data FixedTermDepositCreateModel fixedTermDeposit = new FixedTermDepositCreateModel { AccountId = 3, Amount = 10 }; // Act Func <Task> result = () => _controller.CreateFixedTermDeposit(fixedTermDeposit); // Assert var exception = await Assert.ThrowsAsync <CustomException>(result); Assert.Equal(403, exception.StatusCode); // TODO: Assert for equality of message error }
public async void Create_Ok() { // Arrange FixedTermDepositCreateModel fixedTermDeposit = new FixedTermDepositCreateModel { AccountId = 2, Amount = 10 }; // Act var result = await _controller.CreateFixedTermDeposit(fixedTermDeposit); // Assert Assert.IsType <OkResult>(result); // Get the saved entity from database (asuming there is the first deposit created in the database) FixedTermDeposits createdFixedTermDeposit = _unitOfWork.FixedTermDeposits.GetById(1); Assert.Equal(fixedTermDeposit.AccountId, createdFixedTermDeposit.AccountId); Assert.Equal(fixedTermDeposit.Amount, createdFixedTermDeposit.Amount); // Cannot assert anything about .CreationDate because the in-memory database does not save current datetime Assert.Null(createdFixedTermDeposit.ClosingDate); }