Merge pull request #2814 from alphagov/search-letters

Let users search for letters
This commit is contained in:
Chris Hill-Scott
2020-04-27 15:06:32 +01:00
committed by GitHub
7 changed files with 108 additions and 26 deletions

View File

@@ -26,7 +26,6 @@ from werkzeug.datastructures import MultiDict
from app import db, create_uuid
from app.aws.s3 import remove_s3_object, get_s3_bucket_objects
from app.dao.dao_utils import transactional
from app.errors import InvalidRequest
from app.letters.utils import get_letter_pdf_filename
from app.models import (
FactNotificationStatus,
@@ -603,11 +602,19 @@ def dao_get_notifications_by_recipient_or_reference(service_id, search_term, not
except InvalidEmailError:
normalised = search_term.lower()
elif notification_type == LETTER_TYPE:
raise InvalidRequest("Only email and SMS can use search by recipient", 400)
elif notification_type in {LETTER_TYPE, None}:
# For letters, we store the address without spaces, so we need
# to removes spaces from the search term to match. We also do
# this when a notification type isnt provided (this will
# happen if a user doesnt have permission to see the dashboard)
# because email addresses and phone numbers will never be stored
# with spaces either.
normalised = ''.join(search_term.split()).lower()
else:
normalised = search_term.lower()
raise TypeError(
f'Notification type must be {EMAIL_TYPE}, {SMS_TYPE}, {LETTER_TYPE} or None'
)
normalised = escape_special_characters(normalised)
search_term = escape_special_characters(search_term)
@@ -615,6 +622,7 @@ def dao_get_notifications_by_recipient_or_reference(service_id, search_term, not
filters = [
Notification.service_id == service_id,
or_(
Notification.to.ilike("%{}%".format(search_term)),
Notification.normalised_to.like("%{}%".format(normalised)),
Notification.client_reference.ilike("%{}%".format(search_term)),
),