From 094f4d4298b609b9e1583310bbf3306a173102c5 Mon Sep 17 00:00:00 2001 From: Philip Wafula Date: Wed, 23 Jun 2021 06:44:01 +0000 Subject: [PATCH] Refactors handling of phone number inputs during transactions --- apps/cic-ussd/cic_ussd/phone_number.py | 4 ++++ .../cic_ussd/runnable/daemons/cic_user_ussd_server.py | 5 +++-- apps/cic-ussd/cic_ussd/state_machine/logic/transaction.py | 5 +++-- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/apps/cic-ussd/cic_ussd/phone_number.py b/apps/cic-ussd/cic_ussd/phone_number.py index 2c476fa0..34bd3655 100644 --- a/apps/cic-ussd/cic_ussd/phone_number.py +++ b/apps/cic-ussd/cic_ussd/phone_number.py @@ -8,6 +8,10 @@ import phonenumbers from cic_ussd.db.models.account import Account +class E164Format: + region = None + + def process_phone_number(phone_number: str, region: str): """This function parses any phone number for the provided region :param phone_number: A string with a phone number. diff --git a/apps/cic-ussd/cic_ussd/runnable/daemons/cic_user_ussd_server.py b/apps/cic-ussd/cic_ussd/runnable/daemons/cic_user_ussd_server.py index 6aad181f..171951ef 100644 --- a/apps/cic-ussd/cic_ussd/runnable/daemons/cic_user_ussd_server.py +++ b/apps/cic-ussd/cic_ussd/runnable/daemons/cic_user_ussd_server.py @@ -26,7 +26,7 @@ from cic_ussd.metadata.base import Metadata from cic_ussd.operations import (define_response_with_content, process_menu_interaction_requests, 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.redis import cache_data, create_cached_data_key, InMemoryStore from cic_ussd.requests import (get_request_endpoint, @@ -126,6 +126,7 @@ else: 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') @@ -168,7 +169,7 @@ def application(env, start_response): # add validation for 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 if not check_ip(config=config, env=env): diff --git a/apps/cic-ussd/cic_ussd/state_machine/logic/transaction.py b/apps/cic-ussd/cic_ussd/state_machine/logic/transaction.py index 84c866ee..b8894103 100644 --- a/apps/cic-ussd/cic_ussd/state_machine/logic/transaction.py +++ b/apps/cic-ussd/cic_ussd/state_machine/logic/transaction.py @@ -11,7 +11,7 @@ from cic_ussd.balance import BalanceManager, compute_operational_balance from cic_ussd.chain import Chain from cic_ussd.db.models.account import AccountStatus, Account 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.redis import create_cached_data_key, get_cached_data from cic_ussd.transactions import OutgoingTransactionProcessor @@ -29,8 +29,9 @@ def is_valid_recipient(state_machine_data: Tuple[str, dict, Account]) -> bool: :rtype: bool """ 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) - 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 return is_not_initiator and has_active_account_status and recipient is not None