Exemplo n.º 1
0
        public async Task <bool> BankUserTransaction(TrasactionDTO trasactionDTO)
        {
            try
            {
                if (trasactionDTO != null && trasactionDTO.PersonId > 0 && trasactionDTO.TransactionAmount > 0)
                {
                    //var recentBankTransaction=await GetAccountStatus(trasactionDTO.PersonId);
                    var lastTransaction = await _lastTransactionService.GetBy(f => f.PersonId == trasactionDTO.PersonId);

                    if (lastTransaction != null)
                    {
                        if (lastTransaction.Balance < trasactionDTO.TransactionAmount && trasactionDTO.TransactionType == false)
                        {
                            throw new Exception("Insufficient account balance");
                        }
                        string        transctionType = trasactionDTO.TransactionType ? "Credit" : "Debit";
                        Bank_Activity bank_Activity  = new Bank_Activity();
                        //if the trasaction type is true(credit transaction) then we substract the transaction amount from the previous balance, else we sum the trasaction amount with the previous balance
                        bank_Activity.Balance          = trasactionDTO.TransactionType ? (lastTransaction.Balance + trasactionDTO.TransactionAmount) : (lastTransaction.Balance - trasactionDTO.TransactionAmount);
                        bank_Activity.Description      = trasactionDTO.Description;
                        bank_Activity.IsDeposit        = trasactionDTO.TransactionType ? true : false;
                        bank_Activity.IsOpeningBalance = false;
                        bank_Activity.PersonId         = trasactionDTO.PersonId;
                        bank_Activity.TrasactionAmount = trasactionDTO.TransactionAmount;
                        bank_Activity.TransactionDate  = DateTime.Now;
                        var iscreated = await Save(bank_Activity);

                        if (iscreated > 0)
                        {
                            lastTransaction.Balance     = bank_Activity.Balance;
                            lastTransaction.DateCreated = DateTime.Now;
                            await _lastTransactionService.Update(lastTransaction);

                            MailRequest mailRequest = new MailRequest();
                            mailRequest.Body    = $"{transctionType} of {trasactionDTO.TransactionAmount} was made on your account, for {trasactionDTO.Description} at {DateTime.Now}";
                            mailRequest.Subject = "Bank Transaction";
                            var user = await _userService.GetBy(f => f.PersonId == trasactionDTO.PersonId);

                            if (user != null && user.Id > 0)
                            {
                                mailRequest.ToEmail = user.Username;
                            }
                            await _mailService.SendEmailAsync(mailRequest);

                            return(true);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return(false);
        }
Exemplo n.º 2
0
 public async Task <bool> BankUserTransaction(TrasactionDTO trasactionDTO)
 {
     return(await _bankActivityService.BankUserTransaction(trasactionDTO));
 }
        public async Task <IActionResult> BankTrasaction(TrasactionDTO trasactionDTO)
        {
            var response = await _accountService.BankUserTransaction(trasactionDTO);

            return(Ok(response));
        }