public async Task <IActionResult> Create() { var userId = this.GetCurrentUserId(); var userAccounts = await this.GetAllAccountsAsync(userId); if (!userAccounts.Any()) { this.ShowErrorMessage(NotificationMessages.NoAccountsError); return(this.RedirectToHome()); } var model = new InternalMoneyTransferCreateBindingModel { OwnAccounts = userAccounts }; return(this.View(model)); }
public async Task <IActionResult> Create(InternalMoneyTransferCreateBindingModel model) { var userId = await this.userService.GetUserIdByUsernameAsync(this.User.Identity.Name); if (!this.ModelState.IsValid) { model.OwnAccounts = await this.GetAllAccountsAsync(userId); return(this.View(model)); } var account = await this.bankAccountService.GetByIdAsync <BankAccountDetailsServiceModel>(model.AccountId); if (account == null || account.UserUserName != this.User.Identity.Name) { return(this.Forbid()); } if (string.Equals(account.UniqueId, model.DestinationBankAccountUniqueId, StringComparison.InvariantCulture)) { this.ShowErrorMessage(NotificationMessages.SameAccountsError); model.OwnAccounts = await this.GetAllAccountsAsync(userId); return(this.View(model)); } if (account.Balance < model.Amount) { this.ShowErrorMessage(NotificationMessages.InsufficientFunds); model.OwnAccounts = await this.GetAllAccountsAsync(userId); return(this.View(model)); } var destinationAccount = await this.bankAccountService.GetByUniqueIdAsync <BankAccountConciseServiceModel>( model.DestinationBankAccountUniqueId); if (destinationAccount == null) { this.ShowErrorMessage(NotificationMessages.DestinationBankAccountDoesNotExist); model.OwnAccounts = await this.GetAllAccountsAsync(userId); return(this.View(model)); } var referenceNumber = ReferenceNumberGenerator.GenerateReferenceNumber(); var sourceServiceModel = Mapper.Map <MoneyTransferCreateServiceModel>(model); sourceServiceModel.Source = account.UniqueId; sourceServiceModel.Amount *= -1; sourceServiceModel.SenderName = account.UserFullName; sourceServiceModel.RecipientName = destinationAccount.UserFullName; sourceServiceModel.ReferenceNumber = referenceNumber; if (!await this.moneyTransferService.CreateMoneyTransferAsync(sourceServiceModel)) { this.ShowErrorMessage(NotificationMessages.TryAgainLaterError); model.OwnAccounts = await this.GetAllAccountsAsync(userId); return(this.View(model)); } var destinationServiceModel = Mapper.Map <MoneyTransferCreateServiceModel>(model); destinationServiceModel.Source = account.UniqueId; destinationServiceModel.AccountId = destinationAccount.Id; destinationServiceModel.SenderName = account.UserFullName; destinationServiceModel.RecipientName = destinationAccount.UserFullName; destinationServiceModel.ReferenceNumber = referenceNumber; if (!await this.moneyTransferService.CreateMoneyTransferAsync(destinationServiceModel)) { this.ShowErrorMessage(NotificationMessages.TryAgainLaterError); model.OwnAccounts = await this.GetAllAccountsAsync(userId); return(this.View(model)); } this.ShowSuccessMessage(NotificationMessages.SuccessfulMoneyTransfer); return(this.RedirectToHome()); }