The template type should be passed in when doing a search by recipent.

It is possible to search for a phone number when from the email notification page and get a SMS message in return.
This also helps to optimise the query.
This commit is contained in:
Rebecca Law
2018-03-07 17:11:29 +00:00
committed by Chris Hill-Scott
parent faaea75a99
commit 5f25fc0db4
4 changed files with 66 additions and 4 deletions

View File

@@ -435,7 +435,7 @@ def dao_update_notifications_by_reference(references, update_dict):
@statsd(namespace="dao")
def dao_get_notifications_by_to_field(service_id, search_term, statuses=None):
def dao_get_notifications_by_to_field(service_id, search_term, statuses=None, notification_type=None):
try:
normalised = validate_and_format_phone_number(search_term)
except InvalidPhoneError:
@@ -457,6 +457,8 @@ def dao_get_notifications_by_to_field(service_id, search_term, statuses=None):
if statuses:
filters.append(Notification.status.in_(statuses))
if notification_type:
filters.append(Notification.notification_type == notification_type)
results = db.session.query(Notification).filter(*filters).order_by(desc(Notification.created_at)).all()
return results

View File

@@ -337,7 +337,11 @@ def get_service_history(service_id):
def get_all_notifications_for_service(service_id):
data = notifications_filter_schema.load(request.args).data
if data.get('to'):
return search_for_notification_by_to_field(service_id, data['to'], statuses=data.get('status'))
notification_type = data.get('template_type')[0] if data.get('template_type') else None
return search_for_notification_by_to_field(service_id=service_id,
search_term=data['to'],
statuses=data.get('status'),
notification_type=notification_type)
page = data['page'] if 'page' in data else 1
page_size = data['page_size'] if 'page_size' in data else current_app.config.get('PAGE_SIZE')
limit_days = data.get('limit_days')
@@ -380,8 +384,8 @@ def get_notification_for_service(service_id, notification_id):
), 200
def search_for_notification_by_to_field(service_id, search_term, statuses):
results = notifications_dao.dao_get_notifications_by_to_field(service_id, search_term, statuses)
def search_for_notification_by_to_field(service_id, search_term, statuses, notification_type):
results = notifications_dao.dao_get_notifications_by_to_field(service_id, search_term, statuses, notification_type)
return jsonify(
notifications=notification_with_template_schema.dump(results, many=True).data
), 200

View File

@@ -1838,6 +1838,26 @@ 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
):
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')
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")
assert len(results) == 2
results = dao_get_notifications_by_to_field(service.id, "077", 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')
assert len(results) == 1
assert results[0].id == email.id
def test_dao_created_scheduled_notification(sample_notification):
scheduled_notification = ScheduledNotification(notification_id=sample_notification.id,

View File

@@ -2238,6 +2238,42 @@ def test_search_for_notification_by_to_field_returns_personlisation(
assert notifications[0]['personalisation']['name'] == 'Foo'
def test_search_for_notification_by_to_field_returns_notifications_by_type(
client,
notify_db,
notify_db_session,
sample_template,
sample_email_template
):
sms_notification = create_sample_notification(
notify_db,
notify_db_session,
to_field='+447700900855',
normalised_to='447700900855',
template=sample_template
)
create_sample_notification(
notify_db,
notify_db_session,
to_field='44770@gamil.com',
normalised_to='44770@gamil.com',
template=sample_email_template
)
response = client.get(
'/service/{}/notifications?to={}&template_type={}'.format(
sms_notification.service_id, '0770', 'sms'
),
headers=[create_authorization_header()]
)
notifications = json.loads(response.get_data(as_text=True))['notifications']
assert response.status_code == 200
assert len(notifications) == 1
assert notifications[0]['id'] == str(sms_notification.id)
def test_is_service_name_unique_returns_200_if_unique(admin_request, notify_db, notify_db_session):
service = create_service(service_name='unique', email_from='unique')