cic-internal-integration/apps/cic-ussd/cic_ussd/tasks/notifications.py

71 lines
3.0 KiB
Python
Raw Permalink Normal View History

2021-07-20 18:18:27 +02:00
# standard imports
import datetime
import logging
# third-party imports
import celery
# local imports
2021-11-29 16:04:50 +01:00
from cic_ussd.account.tokens import get_cached_token_data
2021-08-06 18:29:01 +02:00
from cic_ussd.account.transaction import from_wei
2021-07-20 18:18:27 +02:00
from cic_ussd.notifications import Notifier
from cic_ussd.phone_number import Support
celery_app = celery.current_app
logg = logging.getLogger(__file__)
notifier = Notifier()
@celery_app.task
2021-08-06 18:29:01 +02:00
def transaction(notification_data: dict):
2021-07-20 18:18:27 +02:00
"""
:param notification_data:
:type notification_data:
:return:
:rtype:
"""
2021-08-06 18:29:01 +02:00
role = notification_data.get('role')
token_value = notification_data.get('token_value')
2021-11-29 16:04:50 +01:00
token_symbol = notification_data.get('token_symbol')
blockchain_address = notification_data.get('blockchain_address')
token_data = get_cached_token_data(blockchain_address, token_symbol)
decimals = token_data.get('decimals')
amount = token_value if token_value == 0 else from_wei(decimals, token_value)
2021-08-06 18:29:01 +02:00
balance = notification_data.get('available_balance')
2021-07-20 18:18:27 +02:00
phone_number = notification_data.get('phone_number')
preferred_language = notification_data.get('preferred_language')
2021-11-29 16:04:50 +01:00
2021-08-25 12:33:35 +02:00
alt_metadata_id = notification_data.get('alt_metadata_id')
metadata_id = notification_data.get('metadata_id')
2021-07-20 18:18:27 +02:00
transaction_type = notification_data.get('transaction_type')
timestamp = datetime.datetime.now().strftime('%d-%m-%y, %H:%M %p')
if transaction_type == 'tokengift':
notifier.send_sms_notification(
key='sms.account_successfully_created',
phone_number=phone_number,
preferred_language=preferred_language,
2021-08-06 18:29:01 +02:00
support_phone=Support.phone_number)
2021-07-20 18:18:27 +02:00
if transaction_type == 'transfer':
2021-08-06 18:29:01 +02:00
if role == 'recipient':
notifier.send_sms_notification('sms.received_tokens',
phone_number=phone_number,
preferred_language=preferred_language,
amount=amount,
token_symbol=token_symbol,
2021-08-25 12:33:35 +02:00
tx_recipient_information=metadata_id,
tx_sender_information=alt_metadata_id,
2021-08-06 18:29:01 +02:00
timestamp=timestamp,
balance=balance)
if role == 'sender':
notifier.send_sms_notification('sms.sent_tokens',
phone_number=phone_number,
preferred_language=preferred_language,
amount=amount,
token_symbol=token_symbol,
2021-08-25 12:33:35 +02:00
tx_recipient_information=alt_metadata_id,
tx_sender_information=metadata_id,
2021-08-06 18:29:01 +02:00
timestamp=timestamp,
balance=balance)