Skip to content

Commit

Permalink
add intl number detection to api
Browse files Browse the repository at this point in the history
  • Loading branch information
abelsonlive committed Nov 16, 2024
1 parent 1cb1bfe commit 57934f4
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 17 deletions.
8 changes: 7 additions & 1 deletion app/bam_app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
from fastapi import FastAPI, Body, Depends, HTTPException, status
from fastapi.security import OAuth2PasswordBearer

from bam_core.utils.phone import format_phone_number
from bam_core.utils.phone import (
format_phone_number,
is_international_phone_number,
)
from bam_core.utils.email import format_email
from bam_core.utils.geo import format_address

Expand Down Expand Up @@ -45,12 +48,15 @@ def clean_record(
if not valid_phone:
response["phone"] = phone
response["phone_is_invalid"] = True
response["phone_is_intl"] = False
else:
response["phone"] = valid_phone
response["phone_is_invalid"] = False
response["phone_is_intl"] = is_international_phone_number(phone)
else:
response["phone"] = ""
response["phone_is_invalid"] = True
response["phone_is_intl"] = False

# validate email address
if email and email != "null":
Expand Down
34 changes: 19 additions & 15 deletions app/tests/test_clean_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,21 @@ def test_clean_record_with_valid_input():
"email_error": "",
"phone": "(626) 420-6969",
"phone_is_invalid": False,
"phone_is_intl": False,
}


def test_clean_record_with_valid_intl_number():
response = client.get(
f"/clean-record?apikey={APIKEY}&phone=%2b443476669193&[email protected]"
)
assert response.status_code == 200
assert response.json() == {
"email": "[email protected]",
"email_error": "",
"phone": "+44 347 666 9193",
"phone_is_invalid": False,
"phone_is_intl": True,
}


Expand All @@ -32,6 +47,7 @@ def test_clean_record_with_null_input():
"phone": "",
"email_error": "No email address provided",
"phone_is_invalid": True,
"phone_is_intl": False,
}


Expand All @@ -43,6 +59,7 @@ def test_clean_record_with_missing_input():
"phone": "",
"email_error": "No email address provided",
"phone_is_invalid": True,
"phone_is_intl": False,
}


Expand All @@ -56,6 +73,7 @@ def test_clean_record_with_invalid_email():
"phone": "",
"email_error": "The email address is not valid. It must have exactly one @-sign.",
"phone_is_invalid": True,
"phone_is_intl": False,
}


Expand All @@ -64,24 +82,10 @@ def test_clean_record_with_reformatted_email():
f"/clean-record?apikey={APIKEY}&phone=&email=foo @gmail .com"
)
assert response.status_code == 200
print(response.json())
assert response.json() == {
"email": "[email protected]",
"phone": "",
"email_error": "",
"phone_is_invalid": True,
}


def test_clean_record_with_reformatted_email():
response = client.get(
f"/clean-record?apikey={APIKEY}&phone=&email=foo @gmail .com"
)
assert response.status_code == 200
print(response.json())
assert response.json() == {
"email": "[email protected]",
"phone": "",
"email_error": "",
"phone_is_invalid": True,
"phone_is_intl": False,
}
21 changes: 21 additions & 0 deletions core/bam_core/utils/phone.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,27 @@
log = logging.getLogger(__name__)


def is_international_phone_number(phone_number: str) -> bool:
"""
Check if a phone number is international
:param phone_number: The phone number to check
:return: True if the phone number is international, False otherwise
"""
try:
parsed_phone_number = phonenumbers.parse(phone_number, "US")
if not phonenumbers.is_valid_number(parsed_phone_number):
return False
return (
parsed_phone_number.country_code is not None
and parsed_phone_number.country_code != 1
)
except Exception as e:
log.warning(
f"Error checking if phone number {phone_number} is international because of {e}"
)
return False


def format_phone_number(phone_number: str) -> Optional[str]:
"""
Format a phone number to the US standard
Expand Down
15 changes: 14 additions & 1 deletion core/tests/test_phone.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
from bam_core.utils.phone import format_phone_number, extract_phone_numbers
from bam_core.utils.phone import (
format_phone_number,
extract_phone_numbers,
is_international_phone_number,
)


def test_format_phone_number():
Expand Down Expand Up @@ -32,3 +36,12 @@ def test_extract_phone_numbers():
text = "dsilfkhlae (917) 555-5555 jfidalfks(347) 555 5555"
numbers = extract_phone_numbers(text)
assert numbers == ["(917) 555-5555", "(347) 555-5555"]


def test_is_international_phone_number():
valid_intl_number = "+44 347 208 6666"
assert is_international_phone_number(valid_intl_number) is True
us_number_no_country_core = "9294206969"
assert is_international_phone_number(us_number_no_country_core) is False
invalid_intl_number = "+44 666 666 6666"
assert is_international_phone_number(invalid_intl_number) is False

0 comments on commit 57934f4

Please sign in to comment.