Remove submodule cic ussd
This commit is contained in:
15
apps/cic-ussd/cic_ussd/tasks/__init__.py
Normal file
15
apps/cic-ussd/cic_ussd/tasks/__init__.py
Normal file
@@ -0,0 +1,15 @@
|
||||
# standard import
|
||||
import os
|
||||
import logging
|
||||
import urllib
|
||||
import json
|
||||
|
||||
# third-party imports
|
||||
# this must be included for the package to be recognized as a tasks package
|
||||
import celery
|
||||
|
||||
celery_app = celery.current_app
|
||||
# export external celery task modules
|
||||
from .foo import log_it_plz
|
||||
from .ussd import persist_session_to_db
|
||||
from .callback_handler import process_account_creation_callback
|
||||
114
apps/cic-ussd/cic_ussd/tasks/callback_handler.py
Normal file
114
apps/cic-ussd/cic_ussd/tasks/callback_handler.py
Normal file
@@ -0,0 +1,114 @@
|
||||
# standard imports
|
||||
import json
|
||||
import logging
|
||||
from datetime import timedelta
|
||||
|
||||
# third-party imports
|
||||
import celery
|
||||
|
||||
# local imports
|
||||
from cic_ussd.db.models.base import SessionBase
|
||||
from cic_ussd.db.models.user import User
|
||||
from cic_ussd.error import ActionDataNotFoundError
|
||||
from cic_ussd.redis import InMemoryStore
|
||||
from cic_ussd.transactions import IncomingTransactionProcessor
|
||||
|
||||
logg = logging.getLogger(__file__)
|
||||
celery_app = celery.current_app
|
||||
|
||||
|
||||
@celery_app.task(bind=True)
|
||||
def process_account_creation_callback(self, result: str, url: str, status_code: int):
|
||||
"""This function defines a task that creates a user and
|
||||
:param result: The blockchain address for the created account
|
||||
:type result: str
|
||||
:param url: URL provided to callback task in cic-eth should http be used for callback.
|
||||
:type url: str
|
||||
:param status_code: The status of the task to create an account
|
||||
:type status_code: int
|
||||
"""
|
||||
session = SessionBase.create_session()
|
||||
cache = InMemoryStore.cache
|
||||
task_id = self.request.root_id
|
||||
|
||||
# get account creation status
|
||||
account_creation_data = cache.get(task_id)
|
||||
|
||||
# check status
|
||||
if account_creation_data:
|
||||
account_creation_data = json.loads(account_creation_data)
|
||||
if status_code == 0:
|
||||
# update redis data
|
||||
account_creation_data['status'] = 'CREATED'
|
||||
cache.set(name=task_id, value=json.dumps(account_creation_data))
|
||||
cache.persist(task_id)
|
||||
|
||||
phone_number = account_creation_data.get('phone_number')
|
||||
|
||||
# create user
|
||||
user = User(blockchain_address=result, phone_number=phone_number)
|
||||
session.add(user)
|
||||
session.commit()
|
||||
|
||||
# expire cache
|
||||
cache.expire(task_id, timedelta(seconds=30))
|
||||
session.close()
|
||||
|
||||
else:
|
||||
cache.expire(task_id, timedelta(seconds=30))
|
||||
session.close()
|
||||
|
||||
else:
|
||||
session.close()
|
||||
raise ActionDataNotFoundError(f'Account creation task: {task_id}, returned unexpected response: {status_code}')
|
||||
|
||||
|
||||
@celery_app.task
|
||||
def process_incoming_transfer_callback(result: dict, param: str, status_code: int):
|
||||
logg.debug(f'PARAM: {param}, RESULT: {result}, STATUS_CODE: {status_code}')
|
||||
session = SessionBase.create_session()
|
||||
if result and status_code == 0:
|
||||
|
||||
# collect result data
|
||||
recipient_blockchain_address = result.get('recipient')
|
||||
sender_blockchain_address = result.get('sender')
|
||||
token_symbol = result.get('token_symbol')
|
||||
value = result.get('destination_value')
|
||||
|
||||
# try to find users in system
|
||||
recipient_user = session.query(User).filter_by(blockchain_address=recipient_blockchain_address).first()
|
||||
sender_user = session.query(User).filter_by(blockchain_address=sender_blockchain_address).first()
|
||||
|
||||
# check whether recipient is in the system
|
||||
if not recipient_user:
|
||||
session.close()
|
||||
raise ValueError(
|
||||
f'Tx for recipient: {recipient_blockchain_address} was received but has no matching user in the system.'
|
||||
)
|
||||
|
||||
# process incoming transactions
|
||||
incoming_tx_processor = IncomingTransactionProcessor(phone_number=recipient_user.phone_number,
|
||||
preferred_language=recipient_user.preferred_language,
|
||||
token_symbol=token_symbol,
|
||||
value=value)
|
||||
|
||||
if param == 'tokengift':
|
||||
logg.debug('Name information would require integration with cic meta.')
|
||||
incoming_tx_processor.process_token_gift_incoming_transactions(first_name="")
|
||||
elif param == 'transfer':
|
||||
logg.debug('Name information would require integration with cic meta.')
|
||||
if sender_user:
|
||||
sender_information = f'{sender_user.phone_number}, {""}, {""}'
|
||||
incoming_tx_processor.process_transfer_incoming_transaction(sender_information=sender_information)
|
||||
else:
|
||||
logg.warning(
|
||||
f'Tx with sender: {sender_blockchain_address} was received but has no matching user in the system.'
|
||||
)
|
||||
incoming_tx_processor.process_transfer_incoming_transaction(
|
||||
sender_information=sender_blockchain_address)
|
||||
else:
|
||||
session.close()
|
||||
raise ValueError(f'Unexpected transaction param: {param}.')
|
||||
else:
|
||||
session.close()
|
||||
raise ValueError(f'Unexpected status code: {status_code}.')
|
||||
11
apps/cic-ussd/cic_ussd/tasks/foo.py
Normal file
11
apps/cic-ussd/cic_ussd/tasks/foo.py
Normal file
@@ -0,0 +1,11 @@
|
||||
# third-party imports
|
||||
import celery
|
||||
import logging
|
||||
|
||||
celery_app = celery.current_app
|
||||
logg = logging.getLogger()
|
||||
|
||||
|
||||
@celery_app.task()
|
||||
def log_it_plz(whatever):
|
||||
logg.info('logged it plz: {}'.format(whatever))
|
||||
72
apps/cic-ussd/cic_ussd/tasks/ussd.py
Normal file
72
apps/cic-ussd/cic_ussd/tasks/ussd.py
Normal file
@@ -0,0 +1,72 @@
|
||||
# standard imports
|
||||
import json
|
||||
from datetime import timedelta
|
||||
|
||||
# third party imports
|
||||
import celery
|
||||
from celery.utils.log import get_logger
|
||||
|
||||
# local imports
|
||||
from cic_ussd.db.models.base import SessionBase
|
||||
from cic_ussd.db.models.ussd_session import UssdSession
|
||||
from cic_ussd.error import SessionNotFoundError
|
||||
from cic_ussd.session.ussd_session import UssdSession as InMemoryUssdSession
|
||||
|
||||
celery_app = celery.current_app
|
||||
logg = get_logger(__file__)
|
||||
|
||||
|
||||
@celery_app.task
|
||||
def persist_session_to_db(external_session_id: str):
|
||||
"""
|
||||
This task initiates the saving of the session object to the database and it's removal from the in-memory storage.
|
||||
:param external_session_id: The session id of the session to be saved.
|
||||
:type external_session_id: str.
|
||||
:return: The representation of the newly created database object or en error message if session is not found.
|
||||
:rtype: str.
|
||||
:raises SessionNotFoundError: If the session object is not found in memory.
|
||||
:raises VersionTooLowError: If the session's version doesn't match the latest version.
|
||||
"""
|
||||
# create session
|
||||
session = SessionBase.create_session()
|
||||
|
||||
# get ussd session in redis cache
|
||||
in_memory_session = InMemoryUssdSession.redis_cache.get(external_session_id)
|
||||
|
||||
# process persistence to db
|
||||
if in_memory_session:
|
||||
in_memory_session = json.loads(in_memory_session)
|
||||
in_db_ussd_session = session.query(UssdSession).filter_by(external_session_id=external_session_id).first()
|
||||
if in_db_ussd_session:
|
||||
in_db_ussd_session.update(
|
||||
session=session,
|
||||
user_input=in_memory_session.get('user_input'),
|
||||
state=in_memory_session.get('state'),
|
||||
version=in_memory_session.get('version'),
|
||||
)
|
||||
else:
|
||||
in_db_ussd_session = UssdSession(
|
||||
external_session_id=external_session_id,
|
||||
service_code=in_memory_session.get('service_code'),
|
||||
msisdn=in_memory_session.get('msisdn'),
|
||||
user_input=in_memory_session.get('user_input'),
|
||||
state=in_memory_session.get('state'),
|
||||
version=in_memory_session.get('version'),
|
||||
)
|
||||
|
||||
# handle the updating of session data for persistence to db
|
||||
session_data = in_memory_session.get('session_data')
|
||||
|
||||
if session_data:
|
||||
for key, value in session_data.items():
|
||||
in_db_ussd_session.set_data(key=key, value=value, session=session)
|
||||
|
||||
session.add(in_db_ussd_session)
|
||||
InMemoryUssdSession.redis_cache.expire(external_session_id, timedelta(minutes=1))
|
||||
else:
|
||||
session.close()
|
||||
raise SessionNotFoundError('Session does not exist!')
|
||||
|
||||
session.commit()
|
||||
session.close()
|
||||
|
||||
Reference in New Issue
Block a user