Stop guessing notification type

Before the search term was either:
- an email address (or partial email address)
- a phone number (or partial phone number)

Now it can also be:
- a reference (or partial reference)

We can take a pretty good guess, by looking at the search term, whether
the thing the user is searching by email address or phone number. This
helps us:
- only show relevant notifications
- normalise the search term to give the best chance of matching what we
  store in the `normalised_to` field

However we can’t look at a search term and guess whether it’s a
reference, because a reference could take any format. Therefore if the
user hasn’t told us what kind of thing their search term is, we should
stop trying to guess.
This commit is contained in:
Chris Hill-Scott
2019-12-16 10:27:55 +00:00
parent 8cb6907828
commit c573209d7e
3 changed files with 25 additions and 21 deletions

View File

@@ -1,5 +1,4 @@
import functools
import string
from itertools import groupby
from operator import attrgetter
from datetime import (
@@ -570,8 +569,6 @@ def dao_update_notifications_by_reference(references, update_dict):
@statsd(namespace="dao")
def dao_get_notifications_by_recipient_or_reference(service_id, search_term, notification_type=None, statuses=None):
if notification_type is None:
notification_type = guess_notification_type(search_term)
if notification_type == SMS_TYPE:
normalised = try_validate_and_format_phone_number(search_term)
@@ -587,9 +584,12 @@ def dao_get_notifications_by_recipient_or_reference(service_id, search_term, not
except InvalidEmailError:
normalised = search_term.lower()
else:
elif notification_type == LETTER_TYPE:
raise InvalidRequest("Only email and SMS can use search by recipient", 400)
else:
normalised = search_term.lower()
normalised = escape_special_characters(normalised)
search_term = escape_special_characters(search_term)
@@ -765,13 +765,6 @@ def dao_precompiled_letters_still_pending_virus_check():
return notifications
def guess_notification_type(search_term):
if set(search_term) & set(string.ascii_letters + '@'):
return EMAIL_TYPE
else:
return SMS_TYPE
def _duplicate_update_warning(notification, status):
current_app.logger.info(
(

View File

@@ -455,7 +455,7 @@ def cancel_notification_for_service(service_id, notification_id):
def search_for_notification_by_to_field(service_id, search_term, statuses, notification_type):
results = notifications_dao.dao_get_notifications_by_to_field(
results = notifications_dao.dao_get_notifications_by_recipient_or_reference(
service_id=service_id,
search_term=search_term,
statuses=statuses,

View File

@@ -1272,18 +1272,23 @@ def test_dao_get_notifications_by_recipient_searches_across_notification_types(
email = create_notification(
template=email_template, to_field='077@example.com', normalised_to='077@example.com'
)
results = dao_get_notifications_by_recipient_or_reference(service.id, phone_search, notification_type='sms')
results = dao_get_notifications_by_recipient_or_reference(
service.id, phone_search, notification_type='sms'
)
assert len(results) == 1
assert results[0].id == sms.id
results = dao_get_notifications_by_recipient_or_reference(service.id, phone_search) # should assume SMS
assert len(results) == 1
assert results[0].id == sms.id
results = dao_get_notifications_by_recipient_or_reference(service.id, '077', notification_type='email')
results = dao_get_notifications_by_recipient_or_reference(
service.id, email_search, notification_type='email'
)
assert len(results) == 1
assert results[0].id == email.id
results = dao_get_notifications_by_recipient_or_reference(service.id, email_search) # should assume email
assert len(results) == 1
results = dao_get_notifications_by_recipient_or_reference(service.id, '77')
assert len(results) == 2
assert results[0].id == email.id
assert results[1].id == sms.id
def test_dao_get_notifications_by_reference(
@@ -1307,10 +1312,11 @@ def test_dao_get_notifications_by_reference(
results = dao_get_notifications_by_recipient_or_reference(service.id, '77')
assert len(results) == 2
assert results[0].id == sms.id
assert results[0].id == email.id
assert results[1].id == sms.id
# If notification_type isnt specified then we cant normalise the phone number
# to 4477… so this query will only find the email
# to 4477… so this query will only find the email sent to 077@example.com
results = dao_get_notifications_by_recipient_or_reference(service.id, '077')
assert len(results) == 1
assert results[0].id == email.id
@@ -1318,21 +1324,26 @@ def test_dao_get_notifications_by_reference(
results = dao_get_notifications_by_recipient_or_reference(service.id, '077', notification_type='sms')
assert len(results) == 1
assert results[0].id == sms.id
results = dao_get_notifications_by_recipient_or_reference(service.id, '77', notification_type='sms')
assert len(results) == 1
assert results[0].id == sms.id
results = dao_get_notifications_by_recipient_or_reference(service.id, 'Aa', notification_type='sms')
assert len(results) == 1
assert results[0].id == sms.id
results = dao_get_notifications_by_recipient_or_reference(service.id, 'bB', notification_type='sms')
assert len(results) == 0
results = dao_get_notifications_by_recipient_or_reference(service.id, '77', notification_type='email')
assert len(results) == 1
assert results[0].id == email.id
results = dao_get_notifications_by_recipient_or_reference(service.id, 'Bb', notification_type='email')
assert len(results) == 1
assert results[0].id == email.id
results = dao_get_notifications_by_recipient_or_reference(service.id, 'aA', notification_type='email')
assert len(results) == 0