public async Task TestProcessAsync_WithTestData_ShouldProcessWithdrawal() { var testUserId = "testUserId"; var testAdminId = "testAdminId"; var testWithdrawalId = "testWithdrawalId"; var user = new TrainConnectedUser() { Id = testUserId, Balance = 100.00m, }; var adminUser = new TrainConnectedUser() { Id = testAdminId, }; await this.usersRepository.AddAsync(user); await this.usersRepository.AddAsync(adminUser); await this.usersRepository.SaveChangesAsync(); var withdrawal = new Withdrawal() { Amount = 10.05m, Id = testWithdrawalId, CreatedOn = DateTime.Now, TrainConnectedUserId = testUserId, }; await this.withdrawalsRepository.AddAsync(withdrawal); await this.withdrawalsRepository.SaveChangesAsync(); var inputModel = new WithdrawalProcessInputModel() { Amount = 10.05m, Id = testWithdrawalId, Status = true, TrainConnectedUserId = testUserId, }; await this.withdrawalsService.ProcessAsync(inputModel, testAdminId); var expectedWithdrawalStatus = "Approved"; var actualWithdrawalStatus = await this.withdrawalsRepository.All() .Where(w => w.Id == testWithdrawalId) .Select(s => s.Status.ToString()) .FirstOrDefaultAsync(); Assert.Equal(expectedWithdrawalStatus, actualWithdrawalStatus); }
public async Task TestProcessAsync_WithIncorrectUser_ShouldThrowNullRefEx() { var testUserId = "testUserId"; var incorrectTestUserId = "incorrectTestUserId"; var testAdminId = "testAdminId"; var testWithdrawalId = "testWithdrawalId"; var user = new TrainConnectedUser() { Id = testUserId, Balance = 100.00m, }; var adminUser = new TrainConnectedUser() { Id = testAdminId, }; await this.usersRepository.AddAsync(user); await this.usersRepository.AddAsync(adminUser); await this.usersRepository.SaveChangesAsync(); var withdrawal = new Withdrawal() { Amount = 10.05m, Id = testWithdrawalId, CreatedOn = DateTime.Now, TrainConnectedUserId = incorrectTestUserId, }; await this.withdrawalsRepository.AddAsync(withdrawal); await this.withdrawalsRepository.SaveChangesAsync(); var inputModel = new WithdrawalProcessInputModel() { Amount = 10.05m, Id = testWithdrawalId, Status = true, TrainConnectedUserId = incorrectTestUserId, }; await Assert.ThrowsAsync <NullReferenceException>(async() => await this.withdrawalsService.ProcessAsync(inputModel, testAdminId)); }
public async Task <IActionResult> Process(string id, WithdrawalProcessInputModel withdrawalProcessInputModel) { if (id != withdrawalProcessInputModel.Id) { return(this.BadRequest()); } if (!this.ModelState.IsValid) { return(this.View(withdrawalProcessInputModel)); } var processedByUserId = this.User.FindFirst(ClaimTypes.NameIdentifier).Value; await this.withdrawalsService.ProcessAsync(withdrawalProcessInputModel, processedByUserId); return(this.RedirectToAction(nameof(this.All))); }
public async Task ProcessAsync(WithdrawalProcessInputModel withdrawalProcessInputModel, string processedByUserId) { var withdrawal = await this.withdrawalsRepository.All() .FirstOrDefaultAsync(x => x.Id == withdrawalProcessInputModel.Id); if (withdrawal == null) { throw new NullReferenceException(string.Format(ServiceConstants.Withdrawal.NullReferenceWithdrawalId, withdrawalProcessInputModel.Id)); } var user = await this.usersRepository.All() .FirstOrDefaultAsync(u => u.Id == withdrawalProcessInputModel.TrainConnectedUserId); if (user == null) { throw new NullReferenceException(string.Format(ServiceConstants.User.NullReferenceUserId, withdrawalProcessInputModel.TrainConnectedUserId)); } var processedByUser = await this.usersRepository.All() .FirstOrDefaultAsync(u => u.Id == processedByUserId); if (withdrawalProcessInputModel.Status == true) { withdrawal.Status = StatusCode.Approved; user.Balance -= withdrawalProcessInputModel.Amount; this.usersRepository.Update(user); await this.usersRepository.SaveChangesAsync(); } else { withdrawal.Status = StatusCode.Rejected; } withdrawal.CompletedOn = DateTime.UtcNow; withdrawal.ProcessedByUser = processedByUser; withdrawal.ProcessedByUserId = processedByUser.Id; withdrawal.ResolutionNotes = withdrawalProcessInputModel.ResolutionNotes; this.withdrawalsRepository.Update(withdrawal); await this.withdrawalsRepository.SaveChangesAsync(); }