Refactors handling of phone number inputs during transactions

This commit is contained in:
Philip Wafula 2021-06-23 06:44:01 +00:00
parent 28de7a4eac
commit 094f4d4298
3 changed files with 10 additions and 4 deletions

View File

@ -8,6 +8,10 @@ import phonenumbers
from cic_ussd.db.models.account import Account from cic_ussd.db.models.account import Account
class E164Format:
region = None
def process_phone_number(phone_number: str, region: str): def process_phone_number(phone_number: str, region: str):
"""This function parses any phone number for the provided region """This function parses any phone number for the provided region
:param phone_number: A string with a phone number. :param phone_number: A string with a phone number.

View File

@ -26,7 +26,7 @@ from cic_ussd.metadata.base import Metadata
from cic_ussd.operations import (define_response_with_content, from cic_ussd.operations import (define_response_with_content,
process_menu_interaction_requests, process_menu_interaction_requests,
define_multilingual_responses) define_multilingual_responses)
from cic_ussd.phone_number import process_phone_number, Support from cic_ussd.phone_number import process_phone_number, Support, E164Format
from cic_ussd.processor import get_default_token_data from cic_ussd.processor import get_default_token_data
from cic_ussd.redis import cache_data, create_cached_data_key, InMemoryStore from cic_ussd.redis import cache_data, create_cached_data_key, InMemoryStore
from cic_ussd.requests import (get_request_endpoint, from cic_ussd.requests import (get_request_endpoint,
@ -126,6 +126,7 @@ else:
valid_service_codes = config.get('APP_SERVICE_CODE').split(",") valid_service_codes = config.get('APP_SERVICE_CODE').split(",")
E164Format.region = config.get('PHONE_NUMBER_REGION')
Support.phone_number = config.get('APP_SUPPORT_PHONE_NUMBER') Support.phone_number = config.get('APP_SUPPORT_PHONE_NUMBER')
@ -168,7 +169,7 @@ def application(env, start_response):
# add validation for phone number # add validation for phone number
if phone_number: if phone_number:
phone_number = process_phone_number(phone_number=phone_number, region=config.get('PHONE_NUMBER_REGION')) phone_number = process_phone_number(phone_number=phone_number, region=E164Format.region)
# validate ip address # validate ip address
if not check_ip(config=config, env=env): if not check_ip(config=config, env=env):

View File

@ -11,7 +11,7 @@ from cic_ussd.balance import BalanceManager, compute_operational_balance
from cic_ussd.chain import Chain from cic_ussd.chain import Chain
from cic_ussd.db.models.account import AccountStatus, Account from cic_ussd.db.models.account import AccountStatus, Account
from cic_ussd.operations import save_to_in_memory_ussd_session_data from cic_ussd.operations import save_to_in_memory_ussd_session_data
from cic_ussd.phone_number import get_user_by_phone_number, process_phone_number from cic_ussd.phone_number import get_user_by_phone_number, process_phone_number, E164Format
from cic_ussd.processor import retrieve_token_symbol from cic_ussd.processor import retrieve_token_symbol
from cic_ussd.redis import create_cached_data_key, get_cached_data from cic_ussd.redis import create_cached_data_key, get_cached_data
from cic_ussd.transactions import OutgoingTransactionProcessor from cic_ussd.transactions import OutgoingTransactionProcessor
@ -29,8 +29,9 @@ def is_valid_recipient(state_machine_data: Tuple[str, dict, Account]) -> bool:
:rtype: bool :rtype: bool
""" """
user_input, ussd_session, user = state_machine_data user_input, ussd_session, user = state_machine_data
phone_number = process_phone_number(user_input, E164Format.region)
recipient = get_user_by_phone_number(phone_number=user_input) recipient = get_user_by_phone_number(phone_number=user_input)
is_not_initiator = process_phone_number(user_input, 'KE') != user.phone_number is_not_initiator = phone_number != user.phone_number
has_active_account_status = user.get_account_status() == AccountStatus.ACTIVE.name has_active_account_status = user.get_account_status() == AccountStatus.ACTIVE.name
return is_not_initiator and has_active_account_status and recipient is not None return is_not_initiator and has_active_account_status and recipient is not None