public async Task <ServiceResult> GetAccountBalance()
        {
            try
            {
                if (_accountId <= 0)
                {
                    return(new ServiceResult {
                        StatusCode = StatusCodes.Status400BadRequest, ContentResult = JsonConvert.SerializeObject(new ErrorResponseDto {
                            Message = Messages.Account_InvalidAccountId
                        })
                    });
                }

                Account account = await _accountRepo.GetAsync(_accountId);

                if (account == null)
                {
                    return(new ServiceResult {
                        StatusCode = StatusCodes.Status404NotFound, ContentResult = JsonConvert.SerializeObject(new ErrorResponseDto {
                            Message = Messages.Account_AccountNotFound
                        })
                    });
                }

                IEnumerable <Transaction> payments = await _transRepo.GetAllAsync(t => t.AccountId == _accountId && t.TransactionType == TransactionTypeEnum.Withdrawal, orderBy : q => q.OrderByDescending(t => t.Date));

                IEnumerable <Transaction> deposits = await _transRepo.GetAllAsync(t => t.AccountId == _accountId && t.TransactionType == TransactionTypeEnum.Deposit, orderBy : q => q.OrderByDescending(t => t.Date));

                AccountBalanceResultDto result = new AccountBalanceResultDto
                {
                    AccountId                = _accountId,
                    OpeningBalance           = await _accountRepo.GetAccountOpeningBalanceAsync(_accountId),
                    ProcessedPaymentsBalance = await _accountRepo.GetAccountProcessedBalanceAsync(_accountId),
                    ClosingBalance           = await _accountRepo.GetAccountBalanceAsync(_accountId),
                    PendingdPaymentsBalance  = await _accountRepo.GetAccountPendingBalanceAsync(_accountId),
                    Payments = _mapper.Map <List <PaymentTransactionResultDto> >(payments),
                    Deposits = _mapper.Map <List <DepositTransactionResultDto> >(deposits),
                };
                return(new ServiceResult {
                    StatusCode = StatusCodes.Status200OK, ContentResult = JsonConvert.SerializeObject(result, new DecimalFormatConverter())
                });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message, $"An error has occurred executing GetAccountBalance for Account {_accountId}");
                return(new ServiceResult {
                    StatusCode = StatusCodes.Status500InternalServerError, ContentResult = JsonConvert.SerializeObject(new ErrorResponseDto {
                        Message = ex.Message
                    })
                });
            }
        }
Example #2
0
        public async Task <ServiceResult> UpdateTransaction()
        {
            try
            {
                Account account = await _accountRepo.GetAsync(_accountId);

                if (account == null)
                {
                    return(new ServiceResult {
                        StatusCode = StatusCodes.Status404NotFound, ContentResult = JsonConvert.SerializeObject(new ErrorResponseDto {
                            Message = Messages.Account_AccountNotFound
                        })
                    });
                }

                Transaction paymentFromDb = await _transRepo.GetFirstOrDefaultAsync(t => t.Id == _transactionId && t.TransactionType == TransactionTypeEnum.Withdrawal && t.AccountId == _accountId);

                if (paymentFromDb == null)
                {
                    return(new ServiceResult {
                        StatusCode = StatusCodes.Status404NotFound, ContentResult = JsonConvert.SerializeObject(new ErrorResponseDto {
                            Message = Messages.Payment_NotFound
                        })
                    });
                }

                if (paymentFromDb.TransactionStatus == TransactionStatusEnum.Processed)
                {
                    return(new ServiceResult {
                        StatusCode = StatusCodes.Status400BadRequest, ContentResult = JsonConvert.SerializeObject(new ErrorResponseDto {
                            Message = Messages.Payment_StatusIsProcessed
                        })
                    });
                }

                if (paymentFromDb.TransactionStatus == TransactionStatusEnum.Closed)
                {
                    return(new ServiceResult {
                        StatusCode = StatusCodes.Status400BadRequest, ContentResult = JsonConvert.SerializeObject(new ErrorResponseDto {
                            Message = Messages.Payment_StatusIsClosed
                        })
                    });
                }

                paymentFromDb.TransactionStatus = _status;
                paymentFromDb.LastUpdateDate    = DateTime.Now;;
                if (_closedComment != null)
                {
                    paymentFromDb.ClosedReason = _closedComment;
                }

                bool result = await _transRepo.UpdateAsync(paymentFromDb);

                if (!result)
                {
                    return(new ServiceResult {
                        StatusCode = StatusCodes.Status500InternalServerError, ContentResult = JsonConvert.SerializeObject(new ErrorResponseDto {
                            Message = _server500Error
                        })
                    });
                }
                return(new ServiceResult {
                    StatusCode = StatusCodes.Status200OK, ContentResult = JsonConvert.SerializeObject(_mapper.Map <TransactionResultDto>(paymentFromDb), new DecimalFormatConverter())
                });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message, $"An error has occurred executing UpdateTransaction for Account {_accountId}, TransactionId {_transactionId}");
                return(new ServiceResult {
                    StatusCode = StatusCodes.Status500InternalServerError, ContentResult = JsonConvert.SerializeObject(new ErrorResponseDto {
                        Message = ex.Message
                    })
                });
            }
        }
        public async Task <ServiceResult> CreateTransaction()
        {
            try
            {
                Account account = await _accountRepo.GetAsync(_accountId);

                if (account == null)
                {
                    return(new ServiceResult {
                        StatusCode = StatusCodes.Status404NotFound, ContentResult = JsonConvert.SerializeObject(new ErrorResponseDto {
                            Message = Messages.Account_AccountNotFound
                        })
                    });
                }

                DateTime creationDate        = DateTime.Now;
                bool     insufficientBalance = false;

                Transaction newTransaction = new Transaction
                {
                    TransactionStatus = _status,
                    TransactionType   = _type,
                    Date           = _date,
                    CreationDate   = creationDate,
                    LastUpdateDate = creationDate,
                    AccountId      = _accountId,
                    Amount         = _amount
                };

                if (_type == TransactionTypeEnum.Withdrawal)
                {
                    decimal accountAvailableBalance = await _accountRepo.GetAccountBalanceAsync(account.Id);

                    insufficientBalance = accountAvailableBalance < _amount;
                    if (insufficientBalance)
                    {
                        newTransaction.TransactionStatus = TransactionStatusEnum.Closed;
                        newTransaction.ClosedReason      = Messages.Payment_NotEnoughFundsReason;
                    }
                }

                bool result = await _transRepo.AddAsync(newTransaction);

                if (!result)
                {
                    return(new ServiceResult {
                        StatusCode = StatusCodes.Status500InternalServerError, ContentResult = JsonConvert.SerializeObject(new ErrorResponseDto {
                            Message = _server500Error
                        })
                    });
                }
                return(new ServiceResult {
                    StatusCode = StatusCodes.Status201Created, ContentResult = JsonConvert.SerializeObject(_mapper.Map <TransactionResultDto>(newTransaction), new DecimalFormatConverter())
                });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message, $"An error has occurred executing CreateTransaction for Account {_accountId}, Amount {_amount}, Type {_type.ToString()}, Date {_date.ToShortDateString()}");
                return(new ServiceResult {
                    StatusCode = StatusCodes.Status500InternalServerError, ContentResult = JsonConvert.SerializeObject(new ErrorResponseDto {
                        Message = ex.Message
                    })
                });
            }
        }