mirror of
https://github.com/GSA/notifications-api.git
synced 2026-05-27 09:28:03 -04:00
Search notification against normalised recipient with filter for status
This commit is contained in:
@@ -5,6 +5,12 @@ from datetime import (
|
||||
date)
|
||||
|
||||
from flask import current_app
|
||||
|
||||
from notifications_utils.recipients import (
|
||||
validate_and_format_phone_number,
|
||||
validate_and_format_email_address,
|
||||
InvalidPhoneError
|
||||
)
|
||||
from werkzeug.datastructures import MultiDict
|
||||
from sqlalchemy import (desc, func, or_, and_, asc)
|
||||
from sqlalchemy.orm import joinedload
|
||||
@@ -467,10 +473,22 @@ def dao_update_notifications_sent_to_dvla(job_id, provider):
|
||||
|
||||
|
||||
@statsd(namespace="dao")
|
||||
def dao_get_notifications_by_to_field(service_id, search_term):
|
||||
return Notification.query.filter(
|
||||
def dao_get_notifications_by_to_field(service_id, search_term, statuses=None):
|
||||
try:
|
||||
normalised = validate_and_format_phone_number(search_term)
|
||||
except InvalidPhoneError:
|
||||
normalised = validate_and_format_email_address(search_term)
|
||||
|
||||
filters = [
|
||||
Notification.service_id == service_id,
|
||||
func.replace(func.lower(Notification.to), " ", "") == search_term.lower().replace(" ", "")).all()
|
||||
Notification.normalised_to == normalised
|
||||
]
|
||||
|
||||
if statuses:
|
||||
filters.append(Notification.status.in_(statuses))
|
||||
|
||||
results = db.session.query(Notification).filter(*filters).all()
|
||||
return results
|
||||
|
||||
|
||||
@statsd(namespace="dao")
|
||||
|
||||
@@ -1746,33 +1746,53 @@ def test_dao_update_notifications_sent_to_dvla_does_update_history_if_test_key(
|
||||
|
||||
|
||||
def test_dao_get_notifications_by_to_field(sample_template):
|
||||
notification1 = create_notification(template=sample_template, to_field='+447700900855')
|
||||
notification2 = create_notification(template=sample_template, to_field='jack@gmail.com')
|
||||
notification3 = create_notification(template=sample_template, to_field='jane@gmail.com')
|
||||
notification1 = create_notification(
|
||||
template=sample_template, to_field='+447700900855', normalised_to='447700900855'
|
||||
)
|
||||
create_notification(
|
||||
template=sample_template, to_field='jack@gmail.com', normalised_to='jack@gmail.com'
|
||||
)
|
||||
create_notification(
|
||||
template=sample_template, to_field='jane@gmail.com', normalised_to='jane@gmail.com'
|
||||
)
|
||||
results = dao_get_notifications_by_to_field(notification1.service_id, "+447700900855")
|
||||
|
||||
assert len(results) == 1
|
||||
assert results[0].id == notification1.id
|
||||
assert notification1.id == results[0].id
|
||||
|
||||
|
||||
def test_dao_get_notifications_by_to_field_search_is_not_case_sensitive(sample_template):
|
||||
notification1 = create_notification(template=sample_template, to_field='+447700900855')
|
||||
notification2 = create_notification(template=sample_template, to_field='jack@gmail.com')
|
||||
notification3 = create_notification(template=sample_template, to_field='jane@gmail.com')
|
||||
results = dao_get_notifications_by_to_field(notification1.service_id, 'JACK@gmail.com')
|
||||
notification = create_notification(
|
||||
template=sample_template, to_field='jack@gmail.com', normalised_to='jack@gmail.com'
|
||||
)
|
||||
results = dao_get_notifications_by_to_field(notification.service_id, 'JACK@gmail.com')
|
||||
notification_ids = [notification.id for notification in results]
|
||||
|
||||
assert len(results) == 1
|
||||
assert results[0].id == notification2.id
|
||||
assert notification.id in notification_ids
|
||||
|
||||
|
||||
def test_dao_get_notifications_by_to_field_search_ignores_spaces(sample_template):
|
||||
notification1 = create_notification(template=sample_template, to_field='+447700900855')
|
||||
notification2 = create_notification(template=sample_template, to_field='+44 77 00900 855')
|
||||
notification3 = create_notification(template=sample_template, to_field=' +4477009 00 855 ')
|
||||
notification4 = create_notification(template=sample_template, to_field='jack@gmail.com')
|
||||
notification1 = create_notification(
|
||||
template=sample_template, to_field='+447700900855', normalised_to='447700900855'
|
||||
)
|
||||
notification2 = create_notification(
|
||||
template=sample_template, to_field='+44 77 00900 855', normalised_to='447700900855'
|
||||
)
|
||||
notification3 = create_notification(
|
||||
template=sample_template, to_field=' +4477009 00 855 ', normalised_to='447700900855'
|
||||
)
|
||||
create_notification(
|
||||
template=sample_template, to_field='jaCK@gmail.com', normalised_to='jack@gmail.com'
|
||||
)
|
||||
|
||||
results = dao_get_notifications_by_to_field(notification1.service_id, '+447700900855')
|
||||
notification_ids = [notification.id for notification in results]
|
||||
|
||||
assert len(results) == 3
|
||||
assert notification1.id in [r.id for r in results]
|
||||
assert notification2.id in [r.id for r in results]
|
||||
assert notification3.id in [r.id for r in results]
|
||||
assert notification1.id in notification_ids
|
||||
assert notification2.id in notification_ids
|
||||
assert notification3.id in notification_ids
|
||||
|
||||
|
||||
def test_dao_created_scheduled_notification(sample_notification):
|
||||
@@ -1813,3 +1833,59 @@ def test_set_scheduled_notification_to_processed(notify_db, notify_db_session, s
|
||||
set_scheduled_notification_to_processed(notification_1.id)
|
||||
scheduled_notifications = dao_get_scheduled_notifications()
|
||||
assert not scheduled_notifications
|
||||
|
||||
|
||||
def test_dao_get_notifications_by_to_field_filters_status(sample_template):
|
||||
notification = create_notification(
|
||||
template=sample_template, to_field='+447700900855',
|
||||
normalised_to='447700900855', status='delivered'
|
||||
)
|
||||
create_notification(
|
||||
template=sample_template, to_field='+447700900855',
|
||||
normalised_to='447700900855', status='temporary-failure'
|
||||
)
|
||||
|
||||
notifications = dao_get_notifications_by_to_field(notification.service_id, "+447700900855", statuses=['delivered'])
|
||||
|
||||
assert len(notifications) == 1
|
||||
assert notification.id == notifications[0].id
|
||||
|
||||
|
||||
def test_dao_get_notifications_by_to_field_filters_multiple_statuses(sample_template):
|
||||
notification1 = create_notification(
|
||||
template=sample_template, to_field='+447700900855',
|
||||
normalised_to='447700900855', status='delivered'
|
||||
)
|
||||
notification2 = create_notification(
|
||||
template=sample_template, to_field='+447700900855',
|
||||
normalised_to='447700900855', status='sending'
|
||||
)
|
||||
|
||||
notifications = dao_get_notifications_by_to_field(
|
||||
notification1.service_id, "+447700900855", statuses=['delivered', 'sending']
|
||||
)
|
||||
notification_ids = [notification.id for notification in notifications]
|
||||
|
||||
assert len(notifications) == 2
|
||||
assert notification1.id in notification_ids
|
||||
assert notification2.id in notification_ids
|
||||
|
||||
|
||||
def test_dao_get_notifications_by_to_field_returns_all_if_no_status_filter(sample_template):
|
||||
notification1 = create_notification(
|
||||
template=sample_template, to_field='+447700900855',
|
||||
normalised_to='447700900855', status='delivered'
|
||||
)
|
||||
notification2 = create_notification(
|
||||
template=sample_template, to_field='+447700900855',
|
||||
normalised_to='447700900855', status='temporary-failure'
|
||||
)
|
||||
|
||||
notifications = dao_get_notifications_by_to_field(
|
||||
notification1.service_id, "+447700900855"
|
||||
)
|
||||
notification_ids = [notification.id for notification in notifications]
|
||||
|
||||
assert len(notifications) == 2
|
||||
assert notification1.id in notification_ids
|
||||
assert notification2.id in notification_ids
|
||||
|
||||
@@ -1,9 +1,20 @@
|
||||
from datetime import datetime
|
||||
import uuid
|
||||
|
||||
|
||||
from app.dao.jobs_dao import dao_create_job
|
||||
from app.models import (Service, User, Template, Notification, EMAIL_TYPE, LETTER_TYPE,
|
||||
SMS_TYPE, KEY_TYPE_NORMAL, Job, ServicePermission, ScheduledNotification)
|
||||
from app.models import (
|
||||
Service,
|
||||
User,
|
||||
Template,
|
||||
Notification,
|
||||
ScheduledNotification,
|
||||
ServicePermission,
|
||||
Job,
|
||||
EMAIL_TYPE,
|
||||
SMS_TYPE,
|
||||
KEY_TYPE_NORMAL,
|
||||
)
|
||||
from app.dao.users_dao import save_model_user
|
||||
from app.dao.notifications_dao import dao_create_notification, dao_created_scheduled_notification
|
||||
from app.dao.templates_dao import dao_create_template
|
||||
@@ -81,7 +92,8 @@ def create_notification(
|
||||
rate_multiplier=None,
|
||||
international=False,
|
||||
phone_prefix=None,
|
||||
scheduled_for=None
|
||||
scheduled_for=None,
|
||||
normalised_to=None
|
||||
):
|
||||
if created_at is None:
|
||||
created_at = datetime.utcnow()
|
||||
@@ -115,7 +127,8 @@ def create_notification(
|
||||
'job_row_number': job_row_number,
|
||||
'rate_multiplier': rate_multiplier,
|
||||
'international': international,
|
||||
'phone_prefix': phone_prefix
|
||||
'phone_prefix': phone_prefix,
|
||||
'normalised_to': normalised_to
|
||||
}
|
||||
notification = Notification(**data)
|
||||
dao_create_notification(notification)
|
||||
|
||||
@@ -383,7 +383,6 @@ def test_persist_sms_notification_stores_normalised_number(
|
||||
|
||||
assert persisted_notification.to == recipient
|
||||
assert persisted_notification.normalised_to == expected_recipient_normalised
|
||||
<<<<<<< HEAD
|
||||
|
||||
|
||||
@pytest.mark.parametrize('recipient, expected_recipient_normalised', [
|
||||
@@ -413,5 +412,3 @@ def test_persist_email_notification_stores_normalised_email(
|
||||
|
||||
assert persisted_notification.to == recipient
|
||||
assert persisted_notification.normalised_to == expected_recipient_normalised
|
||||
=======
|
||||
>>>>>>> Store the normalised number on the notification
|
||||
|
||||
Reference in New Issue
Block a user