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 void Close_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); // It should be a better way to arrange the data FixedTermDeposits fixedTermDeposit = new FixedTermDeposits { AccountId = 3, Amount = 10, CreationDate = DateTime.Now.AddDays(-1), }; context.FixedTermDeposits.Add(fixedTermDeposit); context.SaveChanges(); // Act Func <Task> result = () => _controller.CloseFixedTermDeposit(fixedTermDeposit.Id); // Assert var exception = await Assert.ThrowsAsync <CustomException>(result); Assert.Equal(403, exception.StatusCode); // TODO: Assert for equality of message error }
public async void Close_Ok() { // Arrange FixedTermDeposits fixedTermDeposit = new FixedTermDeposits { AccountId = 2, Amount = 10, CreationDate = DateTime.Now.AddDays(-1), }; context.FixedTermDeposits.Add(fixedTermDeposit); context.SaveChanges(); // Act var result = await _controller.CloseFixedTermDeposit(fixedTermDeposit.Id); // Assert Assert.IsType <OkResult>(result); Assert.NotNull(fixedTermDeposit.ClosingDate); }
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); }
public async void Close_Fail_Already_Closed() { // Arrange FixedTermDeposits fixedTermDeposit = new FixedTermDeposits { AccountId = 2, Amount = 10, CreationDate = DateTime.Now.AddDays(-1), ClosingDate = DateTime.Now }; context.FixedTermDeposits.Add(fixedTermDeposit); context.SaveChanges(); // Act Func <Task> result = () => _controller.CloseFixedTermDeposit(fixedTermDeposit.Id); // Assert var exception = await Assert.ThrowsAsync <CustomException>(result); Assert.Equal(400, exception.StatusCode); // TODO: Assert for equality of message error }