/// <summary>
        /// Retrieve customer by IBAN
        /// </summary>
        /// <param name="iban"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task <ApiResponse <CustomerResponse> > GetCustomerByIBANAsync(string iban, CancellationToken cancellationToken = default)
        {
            var responseModel = new ApiResponse <CustomerResponse>();

            var bankAccount = await _bankAccountRepo.FindByIBANAsync(iban);

            if (bankAccount == null)
            {
                responseModel.AddError(ExceptionCreator.CreateNotFoundError(nameof(bankAccount), $"bank Account of IBAN: {iban} not found"));
                return(responseModel);
            }

            var customer = await _customerRepo.FindByIBANAsync(iban);

            if (customer == null)
            {
                responseModel.AddError(ExceptionCreator.CreateNotFoundError(nameof(customer), $"bank Account's holder of IBAN: {iban} not found"));
                return(responseModel);
            }

            var address = await _addressRepo.FindByIdAsync(customer.AddressId);

            responseModel.Data = CreateCustomerResponse(customer, address);

            return(responseModel);
        }
Beispiel #2
0
        /// <summary>
        /// Retrieve bank account for the specified iban
        /// </summary>
        /// <param name="iban"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task <ApiResponse <BankAccountResponse> > GetBankAccountByIBANAsync(string iban, CancellationToken cancellationToken = default)
        {
            var responseModel = new ApiResponse <BankAccountResponse>();

            var bankAccount = await _bankAccountRepo.FindByIBANAsync(iban);


            if (bankAccount == null)
            {
                responseModel.AddError(ExceptionCreator.CreateNotFoundError(nameof(bankAccount), $"IBAN: {iban} Not found"));
                return(responseModel);
            }

            var accountOwner    = CreateBankAccountOwner(bankAccount);
            var lastTransaction = await _cashTransactionsRepo.GetLastAsync(bankAccount.IBAN);

            responseModel.Data = CreateBankAccountResponse(bankAccount, accountOwner, lastTransaction.CreatedAt);

            return(responseModel);
        }
        /// <summary>
        /// Create a deposit transaction in db
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task <Response> MakeDepositAsync(CreateCashTransactionRequest request, CancellationToken cancellationToken = default)
        {
            var responseModel   = new ApiResponse <CashTransactionResponse>();
            var amountToDeposit = request.Amount;

            var dbContextTransaction = await _dbContext.Database.BeginTransactionAsync(cancellationToken);

            try
            {
                var toAccount = await _bankAccountRepo.FindByIBANAsync(request.To);

                if (toAccount == null)
                {
                    responseModel.AddError(ExceptionCreator.CreateNotFoundError(nameof(toAccount)));
                    return(responseModel);
                }

                //Add amount to recipient balance
                toAccount.Balance             += amountToDeposit;
                toAccount.AllowedBalanceToUse += amountToDeposit;

                //Update bank account
                await _bankAccountRepo.UpdateAsync(_dbContext, toAccount);

                //Add transaction to db & save changes
                await _cashTransactionsRepo.AddAsync(_dbContext, CreateCashTransaction(request, 0, toAccount.Balance));

                await dbContextTransaction.CommitAsync(cancellationToken);

                return(responseModel);
            }

            catch (Exception ex)
            {
                await dbContextTransaction.RollbackAsync(cancellationToken);

                responseModel.AddError(ExceptionCreator.CreateInternalServerError(ex.ToString()));

                return(responseModel);
            }
        }