コード例 #1
0
        public async Task Create(TransactionCreateModel newT, int user_id)
        {
            AccountsUserModel acc = new AccountsUserModel()
            {
                IdARS = _unitOfWork.Accounts.GetAccountId(user_id, "ARS")
            };


            if (acc.IdARS == null || acc.IdARS <= 0 || user_id <= 0)
            {
                throw new CustomException(404, "No se pudo obtener alguno de los datos del usuario");
            }

            var saldo = _accountBusiness.GetAccountBalance(user_id, "ARS");

            if ((newT.Type.ToLower() == "payment") && (saldo - newT.Amount < 0))
            {
                throw new CustomException(400, "No hay saldo suficiente para realizar la transacción");
            }

            Transactions transaction = _mapper.Map <Transactions>(newT);

            transaction.AccountId = (int)acc.IdARS;

            _unitOfWork.Transactions.Insert(transaction);
            await _unitOfWork.Complete();
        }
コード例 #2
0
        public Task <List <Transactions> > Filter(TransactionFilterModel transaction, int user_id)
        {
            AccountsUserModel acc = new AccountsUserModel()
            {
                IdARS = _unitOfWork.Accounts.GetAccountId(user_id, "ARS"),
                IdUSD = _unitOfWork.Accounts.GetAccountId(user_id, "USD")
            };

            if (!_unitOfWork.Accounts.ValidateAccounts(acc))
            {
                throw new CustomException(404, "No se encontró algunas de las cuentas del usuario");
            }

            //si el id de account es null o menor a 0 se asume que busca en pesos
            if (transaction.AccountId == null || transaction.AccountId <= 0)
            {
                transaction.AccountId = (int)acc.IdARS;
            }

            if (transaction.AccountId != acc.IdARS && transaction.AccountId != acc.IdUSD) //si el id de la account ingresado es distinta a alguna de la suyas, se asume que busca en pesos
            {
                transaction.AccountId = (int)acc.IdARS;
            }
            Task <List <Transactions> > List = _unitOfWork.Transactions.FilterTransaction(transaction, (int)acc.IdUSD, (int)acc.IdARS);

            return(List);
        }
コード例 #3
0
        public AccountsUserModel GetAccountsUsers(int user_id)
        {
            AccountsUserModel accounts = new AccountsUserModel();

            accounts.IdUSD = GetAccountId(user_id, "USD");
            accounts.IdARS = GetAccountId(user_id, "ARS");
            return(accounts);
        }
コード例 #4
0
        public IEnumerable <RefundRequest> GetAllByAccountsId(AccountsUserModel accounts)
        {
            var list = _context.RefundRequest.Where(e => e.SourceAccountId == accounts.IdARS || e.TargetAccountId == accounts.IdARS ||
                                                    e.SourceAccountId == accounts.IdUSD || e.TargetAccountId == accounts.IdUSD)
                       .ToList().OrderByDescending(e => e.Id);

            return(list);
        }
コード例 #5
0
 public bool ValidateAccounts(AccountsUserModel accounts)
 {
     //return false if any of the data is wrong
     if (accounts.IdUSD > 0 && accounts.IdUSD != null && accounts.IdARS > 0 && accounts.IdARS != null)
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
コード例 #6
0
        public IEnumerable <RefundRequestModel> GetAll(int?user_id)
        {
            if (user_id <= 0 || user_id == null)
            {
                throw new CustomException(404, "El id de usuario no es válido");
            }
            AccountsUserModel accounts = _unitOfWork.Accounts.GetAccountsUsers((int)user_id);
            var listDB = _unitOfWork.RefundRequest.GetAllByAccountsId(accounts);
            IEnumerable <RefundRequestModel> list = _mapper.Map <IEnumerable <RefundRequestModel> >(listDB);

            return(list);
        }
コード例 #7
0
        public async Task <TransactionDetailsModel> Details(int?id, int user_id)
        {
            if (user_id <= 0)
            {
                throw new CustomException(404, "Id de usario no válido");
            }
            AccountsUserModel acc = new AccountsUserModel
            {
                IdARS = _unitOfWork.Accounts.GetAccountId(user_id, "ARS"),
                IdUSD = _unitOfWork.Accounts.GetAccountId(user_id, "USD")
            };

            if (acc.IdARS != null && acc.IdUSD != null)
            {
                var transaction = _unitOfWork.Transactions.FindTransaction((int)id, (int)acc.IdUSD, (int)acc.IdARS);

                if (transaction != null)
                {
                    TransactionDetailsModel tdm = _mapper.Map <TransactionDetailsModel>(transaction);

                    // I ask if its editable to show the field
                    if ((bool)transaction.Category.Editable)
                    {
                        tdm.Editable = true;
                    }
                    else
                    {
                        tdm.Editable = false;
                    }

                    tdm.TransactionLog = _mapper.Map <List <TransactionLogModel> >(await _unitOfWork.TransactionLog.GetByTransactionId(transaction.Id));
                    return(tdm);
                }
                else
                {
                    throw new CustomException(400, "No se encontró la transacción");
                }
            }
            else
            {
                throw new CustomException(404, "No se encontró alguna de las cuentas del usuario");
            }
        }
コード例 #8
0
        public async Task <IEnumerable <TransactionModel> > GetAll(TransactionFilterModel tfm, int user_id, int page)
        {
            if (user_id <= 0)
            {
                throw new CustomException(400, "Id de usuario no válido");
            }
            IEnumerable <Transactions> listDB;

            //si busca con algún filtro
            if ((tfm.Type != "" && tfm.Type != null) ||
                (tfm.Concept != "" && tfm.Concept != null) ||
                (tfm.AccountId != null && tfm.AccountId >= 1))
            {
                listDB = await Filter(tfm, user_id);
            }
            //si busca sin filtros
            else
            {
                AccountsUserModel a = _unitOfWork.Accounts.GetAccountsUsers(user_id);
                if (_unitOfWork.Accounts.ValidateAccounts(a))
                {
                    listDB = await _unitOfWork.Transactions.GetTransactionsUser((int)a.IdARS, (int)a.IdUSD);
                }
                else
                {
                    throw new CustomException(404, "No se encontró algunas de las cuentas del usuario");
                }
            }

            IEnumerable <TransactionModel> list = _mapper.Map <IEnumerable <TransactionModel> >(listDB);

            //paginado con xpagelist
            if (page <= 0)
            {
                page = 1;
            }                                          //asigna la primer página
            int pageNumber = (int)page, pageSize = 10; //10 registros por página

            list = await list.ToPagedList(pageNumber, pageSize).ToListAsync();

            return(list);
        }
コード例 #9
0
        public async Task Edit(int?id, TransactionEditModel NewTransaction, int user_id)
        {
            AccountsUserModel acc = new AccountsUserModel
            {
                IdARS = _unitOfWork.Accounts.GetAccountId(user_id, "ARS"),
                IdUSD = _unitOfWork.Accounts.GetAccountId(user_id, "USD")
            };

            if (acc.IdUSD == null || acc.IdUSD <= 0 || acc.IdARS == null || acc.IdARS <= 0)
            {
                throw new CustomException(404, "No se encontró alguna de las cuentas del usuario");
            }

            var transaction_buscada = _unitOfWork.Transactions.FindTransaction((int)id, (int)acc.IdUSD, (int)acc.IdARS);

            if (transaction_buscada != null)
            {
                if ((bool)transaction_buscada.Category.Editable)
                {
                    var transactionLog = new TransactionLog
                    {
                        TransactionId = transaction_buscada.Id,
                        NewValue      = NewTransaction.Concept
                    };
                    transaction_buscada.Concept = NewTransaction.Concept;
                    _unitOfWork.TransactionLog.Insert(transactionLog); //inserto el nuevo cambio en transaction log
                    _unitOfWork.Transactions.Update(transaction_buscada);
                    await _unitOfWork.Complete();

                    return;
                }
                else
                {
                    throw new CustomException(400, "La transacción no es editable");
                }
            }
            else
            {
                throw new CustomException(400, "No se encontró la transacción");
            }
        }
コード例 #10
0
        public async Task Create(RefundRequestCreateModel refund, int?user_id)
        {
            if (user_id <= 0 || user_id == null)
            {
                throw new CustomException(404, "Id de usuario no válido");
            }

            //ceuntas del usuario que pide el reembolso
            AccountsUserModel acc_ori = _unitOfWork.Accounts.GetAccountsUsers((int)user_id);

            if (!_unitOfWork.Accounts.ValidateAccounts(acc_ori))
            {
                throw new CustomException(404, "No se encontró alguna de las cuentas del usuario");
            }

            var ori_transaction = _unitOfWork.Transactions.FindTransaction(refund.TransactionId, (int)acc_ori.IdUSD, (int)acc_ori.IdARS);

            if (ori_transaction != null)
            {
                if (_unitOfWork.RefundRequest.NotRefundable(ori_transaction.Id))
                {
                    throw new CustomException(400, "No se puede crear la solicitud");
                }

                var transfer = _unitOfWork.Transfers.GetTransfer(ori_transaction.Id);

                if (_unitOfWork.Transfers.ValidateTransfer(transfer))
                {
                    var des_transaction = _unitOfWork.Transactions.GetById(transfer.DestinationTransactionId);
                    if (des_transaction == null)
                    {
                        throw new CustomException(404, "No se encontró la transacción de destino");
                    }

                    if (ori_transaction.CategoryId == 4 || des_transaction.CategoryId == 4)
                    {
                        RefundRequest refundRequest = new RefundRequest()
                        {
                            TransactionId   = transfer.OriginTransactionId,
                            Status          = "Pending",
                            SourceAccountId = transfer.OriginTransaction.AccountId,
                            TargetAccountId = transfer.DestinationTransaction.AccountId
                        };
                        if (_unitOfWork.RefundRequest.ValidateRefundRequest(refundRequest))
                        {
                            _unitOfWork.RefundRequest.Insert(refundRequest);
                            await _unitOfWork.Complete();

                            //send email
                            int?des_user_id = _unitOfWork.Accounts.GetById(transfer.DestinationTransaction.AccountId).UserId;
                            if (des_user_id != null && des_user_id > 0)
                            {
                                var            origin_user   = _unitOfWork.Users.GetById((int)user_id);
                                string         email         = _unitOfWork.Users.GetById((int)des_user_id).Email;
                                string         origin_names  = origin_user.FirstName + " " + origin_user.LastName;
                                EmailTemplates emailTemplate = _unitOfWork.EmailTemplates.GetById((int)EmailTemplatesEnum.RefundRequestCreated);

                                if (emailTemplate != null)
                                {
                                    string title = emailTemplate.Title;
                                    string body  = string.Format(emailTemplate.Body, (int)user_id, origin_names, des_transaction.Amount, transfer.DestinationTransactionId);
                                    await _emailSender.SendEmailAsync(email, title, body);

                                    return;
                                }
                                return;
                            }
                            else
                            {
                                throw new CustomException(404, "Solicitud de reembolso creada, pero no se pudo enviar el email de solicitud de reembolso.");
                            }
                        }
                        else
                        {
                            throw new CustomException(400, "Reembolso no válido");
                        }
                    }
                    else
                    {
                        throw new CustomException(400, "No se puede pedir reembolso de la transacción");
                    }
                }
                else
                {
                    throw new CustomException(404, "El id de alguna de las transacciones no es válido");
                }
            }
            else
            {
                throw new CustomException(400, "No se encontró la transacción");
            }
        }