From b3fdd8b52c4f310a5f91cce55856787ecf10cbb8 Mon Sep 17 00:00:00 2001
From: PhilipWafula <philipwafula2@gmail.com>
Date: Mon, 22 Nov 2021 13:16:19 +0300
Subject: [PATCH] Adds menu logic for token handling.

---
 .../cic_ussd/state_machine/logic/tokens.py    | 69 +++++++++++++++++++
 1 file changed, 69 insertions(+)
 create mode 100644 apps/cic-ussd/cic_ussd/state_machine/logic/tokens.py

diff --git a/apps/cic-ussd/cic_ussd/state_machine/logic/tokens.py b/apps/cic-ussd/cic_ussd/state_machine/logic/tokens.py
new file mode 100644
index 00000000..7f4c2398
--- /dev/null
+++ b/apps/cic-ussd/cic_ussd/state_machine/logic/tokens.py
@@ -0,0 +1,69 @@
+# standard imports
+from typing import Tuple
+
+# external imports
+from sqlalchemy.orm.session import Session
+
+# local imports
+from cic_ussd.account.tokens import set_active_token
+from cic_ussd.db.models.account import Account
+from cic_ussd.processor.util import wait_for_session_data
+from cic_ussd.session.ussd_session import save_session_data
+
+
+def is_valid_token_selection(state_machine_data: Tuple[str, dict, Account, Session]):
+    """
+    :param state_machine_data:
+    :type state_machine_data:
+    :return:
+    :rtype:
+    """
+    user_input, ussd_session, account, session = state_machine_data
+    session_data = ussd_session.get('data')
+    account_tokens_list = session_data.get('account_tokens_list')
+    if not account_tokens_list:
+        wait_for_session_data('Account token list', session_data_key='account_tokens_list', ussd_session=ussd_session)
+    if user_input not in ['00', '22']:
+        try:
+            user_input = int(user_input)
+            return user_input <= len(account_tokens_list)
+        except ValueError:
+            user_input = user_input.upper()
+            return any(token_data['symbol'] == user_input for token_data in account_tokens_list)
+
+
+def process_token_selection(state_machine_data: Tuple[str, dict, Account, Session]):
+    """
+    :param state_machine_data:
+    :type state_machine_data:
+    :return:
+    :rtype:
+    """
+    user_input, ussd_session, account, session = state_machine_data
+    account_tokens_list = ussd_session.get('data').get('account_tokens_list')
+    try:
+        user_input = int(user_input)
+        selected_token = account_tokens_list[user_input-1]
+    except ValueError:
+        user_input = user_input.upper()
+        selected_token = next(token_data for token_data in account_tokens_list if token_data['symbol'] == user_input)
+    data = {
+        'selected_token': selected_token
+    }
+    save_session_data(queue='cic-ussd', session=session, data=data, ussd_session=ussd_session)
+
+
+def set_selected_active_token(state_machine_data: Tuple[str, dict, Account, Session]):
+    """
+    :param state_machine_data:
+    :type state_machine_data:
+    :return:
+    :rtype:
+    """
+    user_input, ussd_session, account, session = state_machine_data
+    wait_for_session_data(resource_name='Selected token', session_data_key='selected_token', ussd_session=ussd_session)
+    selected_token = ussd_session.get('data').get('selected_token')
+    token_symbol = selected_token.get('symbol')
+    set_active_token(blockchain_address=account.blockchain_address, token_symbol=token_symbol)
+
+