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(); }
public async Task CreateFixedTermDeposit(FixedTermDepositCreateModel fixedTermDeposit, int userId) { if (userId <= 0) { throw new CustomException(400, "Id inválido"); } // Must determine if there is enough balance in the account to afford the fixed term deposit opening // We need first the currency to call the stored procedure which calculates the balance var account = _unitOfWork.Accounts.GetById(fixedTermDeposit.AccountId); if (account == null) { throw new CustomException(404, "Cuenta inexistente"); } // Check if the account id belongs to the current user if (account.UserId != userId) { throw new CustomException(403, "La cuenta no le pertenece"); } string currency = account.Currency; // Execute the respective stored procedure to get the balance var balance = _accountBusiness.GetAccountBalance(userId, currency); if (balance - fixedTermDeposit.Amount < 0) { // If there isn't enough balance in the account, we cannot continue //throw new CustomException(400, "No hay suficiente dinero para realizar la operación."); throw new BusinessException(ErrorMessages.Not_Enough_Balance); } // We have enough balance. Lets create the fixed term deposit // First make a payment transaction Transactions newTransaction = new Transactions { AccountId = fixedTermDeposit.AccountId, Amount = fixedTermDeposit.Amount, Concept = "Plazo Fijo (Apertura)", Type = "Payment", CategoryId = 3 }; _unitOfWork.Transactions.Insert(newTransaction); // Having the transaction placed, it's time to make the fixed term deposit // Mapping the model received to entity model FixedTermDeposits newFixedTermDeposit = _mapper.Map <FixedTermDeposits>(fixedTermDeposit); // Insert the new fixed term deposit to unit of work _unitOfWork.FixedTermDeposits.Insert(newFixedTermDeposit); // Save changes to database await _unitOfWork.Complete(); }
public async Task Accept(int userId, int refundRequestId) { //Check if request exists and is pending RefundRequest refundRequest = _unitOfWork.RefundRequest.GetByIdExtended(refundRequestId, m => m.SourceAccount.User, m => m.TargetAccount.User); if (refundRequest == null) { throw new CustomException(404, "Solicitud de reembolso no existente"); } if (refundRequest.Status != "Pending") { throw new CustomException(400, "Esta solicitud ya ha sido procesada"); } //Check if the target account is owned by the current user if (refundRequest.TargetAccount.UserId != userId) { throw new CustomException(403, "Solo puede aceptar reembolsos de transacciones realizadas a una cuenta propia"); } //Check if the account has enough money to accept the refund var transaction = _unitOfWork.Transactions.GetById(refundRequest.TransactionId); var balance = _accountBusiness.GetAccountBalance(refundRequest.TargetAccount.UserId, refundRequest.TargetAccount.Currency); if (balance < transaction.Amount) { throw new CustomException(400, "No posee saldo suficiente para aceptar el reembolso"); } //Accept the refund //Change the status to accepted refundRequest.Status = "Accepted"; _unitOfWork.RefundRequest.Update(refundRequest); //Create inverse transactions Transactions transferTopup = new Transactions { Amount = transaction.Amount, Concept = "Reembolso de una transacción", Type = "Topup", AccountId = refundRequest.SourceAccountId, CategoryId = 4 }; Transactions transferPayment = new Transactions { Amount = transaction.Amount, Concept = "Reembolso de una transacción", Type = "Payment", AccountId = refundRequest.TargetAccountId, CategoryId = 4 }; _unitOfWork.Transactions.Insert(transferTopup); _unitOfWork.Transactions.Insert(transferPayment); await _unitOfWork.Complete(); //Add to transfers table Transfers transfer = new Transfers() { OriginTransactionId = transferPayment.Id, DestinationTransactionId = transferTopup.AccountId }; _unitOfWork.Transfers.Insert(transfer); //Save all changes await _unitOfWork.Complete(); //Send email await EmailStatusChange(refundRequest.TargetAccount.User, "aceptado", refundRequest.TransactionId, refundRequest.SourceAccount.User); }