public async Task TestCreateAsync_WithTestData_ShouldCreateWithdrawal() { var testUserId = "testUserId"; var testUserName = "******"; var withdrawalCreateInputModel = new WithdrawalCreateInputModel() { Amount = 15.45m, AdditionalInstructions = "NoneOfTheAbove", }; var user = new TrainConnectedUser() { Id = testUserId, Balance = 30.00m, UserName = testUserName, }; await this.usersRepository.AddAsync(user); await this.usersRepository.SaveChangesAsync(); await this.withdrawalsService.CreateAsync(withdrawalCreateInputModel, testUserId); var result = await this.withdrawalsRepository.All() .Where(u => u.TrainConnectedUserId == testUserId) .FirstOrDefaultAsync(); Assert.NotNull(result); Assert.True(result.Status == StatusCode.Initiated); }
public async Task CreateAsync(WithdrawalCreateInputModel withdrawalCreateInputModel, string userId) { var user = this.usersRepository.All() .FirstOrDefault(x => x.Id == userId); var pendingWithdrawals = await this.GetUserPendingWithdrawalsBalance(userId); var withdrawableAmount = user.Balance - pendingWithdrawals; if (withdrawableAmount >= withdrawalCreateInputModel.Amount && withdrawalCreateInputModel.Amount > 0) { var withdrawal = new Withdrawal { Amount = withdrawalCreateInputModel.Amount, AdditionalInstructions = withdrawalCreateInputModel.AdditionalInstructions, TrainConnectedUserId = user.Id, TrainConnectedUser = user, Status = StatusCode.Initiated, }; await this.withdrawalsRepository.AddAsync(withdrawal); await this.withdrawalsRepository.SaveChangesAsync(); } else { throw new InvalidOperationException(string.Format(ServiceConstants.Withdrawal.RequestedAmountGreaterThanWithdrawable)); } }
public async Task <IActionResult> Create(WithdrawalCreateInputModel withdrawalCreateInputModel) { var userId = this.User.FindFirst(ClaimTypes.NameIdentifier).Value; if (!this.ModelState.IsValid) { this.ViewData["userBalance"] = await this.withdrawalsService.GetUserBalanceAsync(userId); this.ViewData["pendingWithdrawals"] = await this.withdrawalsService.GetUserPendingWithdrawalsBalance(userId); return(this.View(withdrawalCreateInputModel)); } await this.withdrawalsService.CreateAsync(withdrawalCreateInputModel, userId); return(this.RedirectToAction(nameof(this.All))); }
public async Task TestCreateAsync_WithNegativeAmount_ShouldThrowInvOpEx() { var testUserId = "testUserId"; var testUserName = "******"; var withdrawalCreateInputModel = new WithdrawalCreateInputModel() { Amount = -15.45m, AdditionalInstructions = "NoneOfTheAbove", }; var user = new TrainConnectedUser() { Id = testUserId, Balance = 30.00m, UserName = testUserName, }; await this.usersRepository.AddAsync(user); await this.usersRepository.SaveChangesAsync(); await Assert.ThrowsAsync <InvalidOperationException>(async() => await this.withdrawalsService.CreateAsync(withdrawalCreateInputModel, testUserId)); }