Skip to content

Commit

Permalink
debug
Browse files Browse the repository at this point in the history
  • Loading branch information
freelancing-solutions committed Jan 13, 2022
1 parent 85944b9 commit b7c907d
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 114 deletions.
229 changes: 115 additions & 114 deletions _cron/jobs/transactions_jobs.py
Original file line number Diff line number Diff line change
@@ -1,114 +1,115 @@
import asyncio
from typing import List, Coroutine, Optional
from flask import current_app
from google.api_core.exceptions import RetryError
from google.cloud import ndb
from google.cloud.ndb import toplevel, tasklet, Future, wait_all

from config.use_context import use_context
from database.mixins import AmountMixin
from database.wallet import WalletTransactionsModel, WalletModel


class TransactionsJobs:
"""
**TransactionsJobs**
withdrawals and deposits cron jobs
"""

def __init__(self):
self._max_retries = current_app.config.get('DATASTORE_RETRIES')
self._max_timeout = current_app.config.get('DATASTORE_TIMEOUT')

@use_context
@toplevel
def run(self):
approved_withdrawals = yield self.send_approved_withdrawals_to_paypal_wallets()
approved_deposits = yield self.add_approved_deposits_to_wallet()

@tasklet
def do_send_to_client_paypal(self, transaction: WalletTransactionsModel) -> Future:
"""
**do_send_to_client_paypal**
send withdrawal amount to the client's paypal account
:param transaction: contains a record of the transaction
:return: bool indicating success or failure
"""
# TODO use paypal SDK to send transactions to paypal here
# TODO then update transaction to reflect that transaction was sent
# NOTE: Could also listen to an _ipn to find out if transaction succeeded on paypal side
wallet_instance: WalletModel = WalletModel.query(
WalletModel.organization_id == transaction.organization_id, WalletModel.uid == transaction.uid).get_async().get_result()

if wallet_instance.is_verified:
paypal_address = wallet_instance.paypal_address
amount_to_send: AmountMixin = transaction.amount
# TODO send amount to paypal using paypal address from wallet and amount from transactions
transaction.is_settled = True
tran_key: Optional[ndb.Key] = transaction.put_async(retries=self._max_retries,
timeout=self._max_timeout).get_result()
yield bool(tran_key)
yield False

@tasklet
def send_approved_withdrawals_to_paypal_wallets(self) -> Optional[List[Future]]:
"""
**send_approved_withdrawals_to_paypal_wallets**
fetches all processed and approved withdrawals and schedule them for sending to the client
paypal wallet address
:return: None
"""
try:
wallet_transactions: List[WalletTransactionsModel] = WalletTransactionsModel.query(
WalletTransactionsModel.is_verified == True, WalletTransactionsModel.is_settled == False).fetch_async().get_result()

return [self.do_send_to_client_paypal(transaction=transaction) for transaction in wallet_transactions
if transaction.transaction_type == 'withdrawal']
except RetryError as e:
# TODO Log this error
return None

# Note below methods deals with client deposits
@tasklet
def do_send_to_client_wallet(self, transaction: WalletTransactionsModel) -> Future:
"""
**do_send_to_client_wallet**
send the deposit to client wallet
:param transaction: contains the deposit amount
:return: bool indicating success or failure
"""
# requesting the user wallet
wallet_instance: WalletModel = WalletModel.query(
WalletModel.organization_id == transaction.organization_id, WalletModel.uid == transaction.uid).get_async().get_result()

is_currency_valid: bool = wallet_instance.available_funds.currency == transaction.amount.currency
if isinstance(wallet_instance, WalletModel) and is_currency_valid:
wallet_instance.available_funds.amount_cents += transaction.amount.amount_cents
key: Optional[ndb.Key] = wallet_instance.put_async(
retries=self._max_retries, timeout=self._max_timeout).get_result()
if bool(key):
transaction.is_settled = True
tran_key: Optional[ndb.Key] = transaction.put_async(retries=self._max_retries,
timeout=self._max_timeout).get_result()
yield bool(tran_key)
yield False

@tasklet
def add_approved_deposits_to_wallet(self) -> Optional[List[Future]]:
"""
**add_approved_deposits_to_wallet**
fetches all processed deposits which are not yet settled and then adding them to the client wallet
:return: None
"""
try:
wallet_transactions: List[WalletTransactionsModel] = WalletTransactionsModel.query(
WalletTransactionsModel.is_verified == True, WalletTransactionsModel.is_settled == False).fetch_async().get_result()

return [self.do_send_to_client_wallet(transaction=transaction) for transaction in wallet_transactions
if transaction.transaction_type == 'deposit']
except RetryError as e:
# TODO log this errors
return None
import asyncio
from typing import List, Coroutine, Optional
from flask import current_app
from google.api_core.exceptions import RetryError
from google.cloud import ndb
from google.cloud.ndb import toplevel, tasklet, Future, wait_all

from config.use_context import use_context
from database.mixins import AmountMixin
from database.wallet import WalletTransactionsModel, WalletModel


class TransactionsJobs:
"""
**TransactionsJobs**
withdrawals and deposits cron jobs
"""

def __init__(self):
self._max_retries = current_app.config.get('DATASTORE_RETRIES')
self._max_timeout = current_app.config.get('DATASTORE_TIMEOUT')

@use_context
@toplevel
def run(self):
"""runs transactions cron jobs"""
approved_withdrawals = yield self.send_approved_withdrawals_to_paypal_wallets()
approved_deposits = yield self.add_approved_deposits_to_wallet()

@tasklet
def do_send_to_client_paypal(self, transaction: WalletTransactionsModel) -> Future:
"""
**do_send_to_client_paypal**
send withdrawal amount to the client's paypal account
:param transaction: contains a record of the transaction
:return: bool indicating success or failure
"""
# TODO use paypal SDK to send transactions to paypal here
# TODO then update transaction to reflect that transaction was sent
# NOTE: Could also listen to an _ipn to find out if transaction succeeded on paypal side
wallet_instance: WalletModel = WalletModel.query(
WalletModel.organization_id == transaction.organization_id, WalletModel.uid == transaction.uid).get_async().get_result()

if wallet_instance.is_verified:
paypal_address = wallet_instance.paypal_address
amount_to_send: AmountMixin = transaction.amount
# TODO send amount to paypal using paypal address from wallet and amount from transactions
transaction.is_settled = True
tran_key: Optional[ndb.Key] = transaction.put_async(retries=self._max_retries,
timeout=self._max_timeout).get_result()
yield bool(tran_key)
yield False

@tasklet
def send_approved_withdrawals_to_paypal_wallets(self) -> Optional[List[Future]]:
"""
**send_approved_withdrawals_to_paypal_wallets**
fetches all processed and approved withdrawals and schedule them for sending to the client
paypal wallet address
:return: None
"""
try:
wallet_transactions: List[WalletTransactionsModel] = WalletTransactionsModel.query(
WalletTransactionsModel.is_verified == True, WalletTransactionsModel.is_settled == False).fetch_async().get_result()

return [self.do_send_to_client_paypal(transaction=transaction) for transaction in wallet_transactions
if transaction.transaction_type == 'withdrawal']
except RetryError as e:
# TODO Log this error
return None

# Note below methods deals with client deposits
@tasklet
def do_send_to_client_wallet(self, transaction: WalletTransactionsModel) -> Future:
"""
**do_send_to_client_wallet**
send the deposit to client wallet
:param transaction: contains the deposit amount
:return: bool indicating success or failure
"""
# requesting the user wallet
wallet_instance: WalletModel = WalletModel.query(
WalletModel.organization_id == transaction.organization_id, WalletModel.uid == transaction.uid).get_async().get_result()

is_currency_valid: bool = wallet_instance.available_funds.currency == transaction.amount.currency
if isinstance(wallet_instance, WalletModel) and is_currency_valid:
wallet_instance.available_funds.amount_cents += transaction.amount.amount_cents
key: Optional[ndb.Key] = wallet_instance.put_async(
retries=self._max_retries, timeout=self._max_timeout).get_result()
if bool(key):
transaction.is_settled = True
tran_key: Optional[ndb.Key] = transaction.put_async(retries=self._max_retries,
timeout=self._max_timeout).get_result()
yield bool(tran_key)
yield False

@tasklet
def add_approved_deposits_to_wallet(self) -> Optional[List[Future]]:
"""
**add_approved_deposits_to_wallet**
fetches all processed deposits which are not yet settled and then adding them to the client wallet
:return: None
"""
try:
wallet_transactions: List[WalletTransactionsModel] = WalletTransactionsModel.query(
WalletTransactionsModel.is_verified == True, WalletTransactionsModel.is_settled == False).fetch_async().get_result()

return [self.do_send_to_client_wallet(transaction=transaction) for transaction in wallet_transactions
if transaction.transaction_type == 'deposit']
except RetryError as e:
# TODO log this errors
return None
1 change: 1 addition & 0 deletions security/api_authenticator.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def handle_api_auth(func):
# noinspection DuplicatedCode
@functools.wraps(func)
def auth_wrapper(*args, **kwargs):
"""authenticates api calls"""
api_key: Optional[str] = request.headers.get('x-api-key')
secret_token: Optional[str] = request.headers.get('x-secret-token')
domain: Optional[str] = request.base_url
Expand Down

0 comments on commit b7c907d

Please sign in to comment.