Exemplo n.º 1
0
        public InsertTransactionResponse InsertTransaction(InsertTransactionRequest req)
        {
            InsertTransactionResponse res = new InsertTransactionResponse();

            try
            {
                DateTime registrationDate = DateTime.Now;
                long?    transactionId    = null;
                Business business         = null;
                User     user             = null;

                if (req.Transaction.Amount <= 0)
                {
                    res.IsSuccessful = false;
                    res.Errors.Add(new Error()
                    {
                        Code = ((int)ErrorCodes.AmountInvalid).ToString(), Message = "The amount is invalid"
                    });
                    return(res);
                }

                using (DABusiness da = new DABusiness())
                {
                    business = da.GetBusiness(req.Transaction.BusinessId.Value);
                }

                if (business == null)
                {
                    res.IsSuccessful = false;
                    res.Errors.Add(new Error()
                    {
                        Code = ((int)ErrorCodes.BusinessNotFound).ToString(), Message = "The business not found"
                    });
                    return(res);
                }

                using (DATransaction da = new DATransaction())
                {
                    transactionId = da.InsertTransaction(req.Transaction.BusinessId, User.Id, req.Transaction.Amount, registrationDate, req.Transaction.Description, (int)TransactionStatus.Registered);

                    using (DAUser daUser = new DAUser())
                    {
                        user = daUser.GetUser(User.Id);
                    }

                    DateTime processingDate = DateTime.Now;
                    if (user.AvailableBalance >= req.Transaction.Amount)
                    {
                        da.UpdateTransaction(transactionId, (int)TransactionStatus.Processed, processingDate, req.Transaction.Amount,
                                             business, user);
                    }
                    else
                    {
                        da.UpdateTransaction(transactionId, (int)TransactionStatus.Rejected, processingDate);
                        res.IsSuccessful = false;
                        res.Errors.Add(new Error()
                        {
                            Code = ((int)ErrorCodes.AvailableBalanceIsEnough).ToString(), Message = "The available balance is not enough to make the transaction"
                        });
                        return(res);
                    }

                    res.Transaction = da.GetTransaction(transactionId);
                }

                res.IsSuccessful = true;
                return(res);
            }
            catch (Exception ex)
            {
                res.IsSuccessful = false;
                res.Errors.Add(new Error()
                {
                    Code = ((int)ErrorCodes.ErrorNotSpecific).ToString(), Message = "There was a problem. Please try again later"
                });
            }
            return(res);
        }
        public async Task <InsertTransactionResponse> AddAsync(InsertTransactionRequest request, ClaimsPrincipal claimsPrincipal)
        {
            InsertTransactionResponse response = new InsertTransactionResponse();

            try
            {
                //TODO: Validate if a the user can add transactions

                if (request.Transaction.Amount <= 0)
                {
                    response.IsSuccessful = false;
                    response.Errors.Add(new Error()
                    {
                        Code = ((int)ErrorCodes.InvalidAmount).ToString(), Message = "The amount is invalid"
                    });
                    return(response);
                }

                var fromAccount = await this.accountRepository.GetAsync(request.Transaction.FromAccountId).ConfigureAwait(false);


                if (fromAccount.IsNull())
                {
                    response.IsSuccessful = false;
                    response.Errors.Add(new Error()
                    {
                        Code = ((int)ErrorCodes.NotFound).ToString(), Message = $"Account {request.Transaction.FromAccountId} was not found"
                    });
                    return(response);
                }

                var toAccount = await this.accountRepository.GetAsync(request.Transaction.ToAccountId).ConfigureAwait(false);


                if (toAccount.IsNull())
                {
                    response.IsSuccessful = false;
                    response.Errors.Add(new Error()
                    {
                        Code = ((int)ErrorCodes.NotFound).ToString(), Message = $"Account {request.Transaction.ToAccountId} was not found"
                    });
                    return(response);
                }



                var user = await this.userRepository.GetByIdAsync(request.Transaction.UserId);

                //TODO: Check if user can register transactions

                var transactionID = await this.transactionRepository.AddAsync(CreateTransaction(request));

                if (fromAccount.Balance.Available >= request.Transaction.Amount)
                {
                    var transaction = await this.GetTransactionAsync(transactionID);

                    transaction.ChangeStatus(TransactionStatus.Processed)
                    .UpdateAmount(request.Transaction.Amount)
                    .SetProcessingDate(SystemTime.Now());

                    await this.transactionRepository.UpdateAsync(transaction, fromAccount, toAccount);
                }
                else
                {
                    var transaction = await this.GetTransactionAsync(transactionID);

                    transaction.ChangeStatus(TransactionStatus.Rejected)
                    .UpdateAmount(null)
                    .SetProcessingDate(SystemTime.Now());

                    await this.transactionRepository.UpdateAsync(transaction);

                    response.IsSuccessful = false;
                    response.Errors.Add(new Error()
                    {
                        Code = ((int)ErrorCodes.InsufficientFunds).ToString(), Message = "The available balance is not enough to make the transaction"
                    });
                    return(response);
                }

                response.Transaction = await this.GetTransactionAsync(transactionID);
            }
            catch (Exception)
            {
                response.IsSuccessful = false;
                response.Errors.Add(new Error()
                {
                    Code = ((int)ErrorCodes.Unknown).ToString(), Message = "There was a problem. Please try again later"
                });
            }

            return(response);
        }