diff --git a/app/dao/notifications_dao.py b/app/dao/notifications_dao.py index feaad7fea..f531a15c6 100644 --- a/app/dao/notifications_dao.py +++ b/app/dao/notifications_dao.py @@ -1,4 +1,5 @@ import functools +import string from datetime import ( datetime, timedelta, @@ -425,7 +426,10 @@ def dao_update_notifications_by_reference(references, update_dict): @statsd(namespace="dao") -def dao_get_notifications_by_to_field(service_id, search_term, notification_type, statuses=None): +def dao_get_notifications_by_to_field(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) @@ -440,6 +444,7 @@ def dao_get_notifications_by_to_field(service_id, search_term, notification_type normalised = validate_and_format_email_address(search_term) except InvalidEmailError: normalised = search_term.lower() + else: raise InvalidRequest("Only email and SMS can use search by recipient", 400) @@ -597,3 +602,10 @@ def notifications_not_yet_sent(should_be_sending_after_seconds, notification_typ Notification.status == NOTIFICATION_CREATED ).all() return notifications + + +def guess_notification_type(search_term): + if set(search_term) & set(string.ascii_letters + '@'): + return EMAIL_TYPE + else: + return SMS_TYPE diff --git a/tests/app/dao/notification_dao/test_notification_dao.py b/tests/app/dao/notification_dao/test_notification_dao.py index 4c9e3bca2..775598015 100644 --- a/tests/app/dao/notification_dao/test_notification_dao.py +++ b/tests/app/dao/notification_dao/test_notification_dao.py @@ -1553,20 +1553,34 @@ def test_dao_get_notifications_by_to_field_search_ignores_spaces(sample_template assert notification3.id in notification_ids -def test_dao_get_notifications_by_to_field_only_searches_for_notification_type( - notify_db_session +@pytest.mark.parametrize('phone_search', ( + '077', '7-7', '+44(0)7711 111111' +)) +@pytest.mark.parametrize('email_search', ( + 'example', 'eXaMpLe', +)) +def test_dao_get_notifications_by_to_field_only_searches_one_notification_type( + notify_db_session, + phone_search, + email_search, ): service = create_service() sms_template = create_template(service=service) email_template = create_template(service=service, template_type='email') - sms = create_notification(template=sms_template, to_field='0771111111', normalised_to='0771111111') + sms = create_notification(template=sms_template, to_field='07711111111', normalised_to='447711111111') email = create_notification( template=email_template, to_field='077@example.com', normalised_to='077@example.com' ) - results = dao_get_notifications_by_to_field(service.id, "077", notification_type='sms') + results = dao_get_notifications_by_to_field(service.id, phone_search, notification_type='sms') assert len(results) == 1 assert results[0].id == sms.id - results = dao_get_notifications_by_to_field(service.id, "077", notification_type='email') + results = dao_get_notifications_by_to_field(service.id, phone_search) # should assume SMS + assert len(results) == 1 + assert results[0].id == sms.id + results = dao_get_notifications_by_to_field(service.id, '077', notification_type='email') + assert len(results) == 1 + assert results[0].id == email.id + results = dao_get_notifications_by_to_field(service.id, email_search) # should assume email assert len(results) == 1 assert results[0].id == email.id