diff --git a/app/dao/inbound_sms_dao.py b/app/dao/inbound_sms_dao.py index 4bd2a417d..53c62f5af 100644 --- a/app/dao/inbound_sms_dao.py +++ b/app/dao/inbound_sms_dao.py @@ -32,7 +32,23 @@ def dao_get_inbound_sms_for_service(service_id, limit=None, user_number=None): return q.all() -def dao_get_paginated_inbound_sms_for_service( +def dao_get_paginated_inbound_sms_for_service(service_id, user_number=None, page=1): + q = InboundSms.query.filter( + InboundSms.service_id == service_id + ).order_by( + InboundSms.created_at.desc() + ) + + if user_number: + q = q.filter(InboundSms.user_number == user_number) + + return q.paginate( + page=page, + per_page=current_app.config['PAGE_SIZE'] + ) + + +def dao_get_paginated_inbound_sms_for_service_for_public_api( service_id, older_than=None, page_size=None diff --git a/app/inbound_sms/rest.py b/app/inbound_sms/rest.py index 3e67e4ee1..e60625486 100644 --- a/app/inbound_sms/rest.py +++ b/app/inbound_sms/rest.py @@ -9,7 +9,8 @@ from notifications_utils.recipients import try_validate_and_format_phone_number from app.dao.inbound_sms_dao import ( dao_get_inbound_sms_for_service, dao_count_inbound_sms_for_service, - dao_get_inbound_sms_by_id + dao_get_inbound_sms_by_id, + dao_get_paginated_inbound_sms_for_service ) from app.errors import register_errors from app.schema_validation import validate @@ -41,15 +42,22 @@ def post_query_inbound_sms_for_service(service_id): @inbound_sms.route('', methods=['GET']) def get_inbound_sms_for_service(service_id): limit = request.args.get('limit') + page = request.args.get('page') user_number = request.args.get('user_number') if user_number: # we use this to normalise to an international phone number - but this may fail if it's an alphanumeric user_number = try_validate_and_format_phone_number(user_number, international=True) - results = dao_get_inbound_sms_for_service(service_id, limit, user_number) - - return jsonify(data=[row.serialize() for row in results]) + if not page: + results = dao_get_inbound_sms_for_service(service_id, limit, user_number) + return jsonify(data=[row.serialize() for row in results]) + else: + results = dao_get_paginated_inbound_sms_for_service(service_id, user_number, int(page)) + return jsonify( + data=[row.serialize() for row in results.items], + has_next=results.has_next + ) @inbound_sms.route('/summary') diff --git a/app/v2/inbound_sms/get_inbound_sms.py b/app/v2/inbound_sms/get_inbound_sms.py index f4225a011..24594a610 100644 --- a/app/v2/inbound_sms/get_inbound_sms.py +++ b/app/v2/inbound_sms/get_inbound_sms.py @@ -11,7 +11,7 @@ from app.v2.inbound_sms.inbound_sms_schemas import get_inbound_sms_request def get_inbound_sms(): data = validate(request.args.to_dict(), get_inbound_sms_request) - paginated_inbound_sms = inbound_sms_dao.dao_get_paginated_inbound_sms_for_service( + paginated_inbound_sms = inbound_sms_dao.dao_get_paginated_inbound_sms_for_service_for_public_api( authenticated_service.id, older_than=data.get('older_than', None), page_size=current_app.config.get('API_PAGE_SIZE') diff --git a/tests/app/dao/test_inbound_sms_dao.py b/tests/app/dao/test_inbound_sms_dao.py index c74abb1b5..5c3ad989e 100644 --- a/tests/app/dao/test_inbound_sms_dao.py +++ b/tests/app/dao/test_inbound_sms_dao.py @@ -7,6 +7,7 @@ from app.dao.inbound_sms_dao import ( dao_count_inbound_sms_for_service, delete_inbound_sms_created_more_than_a_week_ago, dao_get_inbound_sms_by_id, + dao_get_paginated_inbound_sms_for_service_for_public_api, dao_get_paginated_inbound_sms_for_service ) from tests.app.db import create_inbound_sms, create_service @@ -22,6 +23,16 @@ def test_get_all_inbound_sms(sample_service): assert res[0] == inbound +def test_get_all_inbound_sms_by_page(sample_service): + inbound = create_inbound_sms(sample_service) + + res = dao_get_paginated_inbound_sms_for_service(sample_service.id) + assert len(res.items) == 1 + assert res.has_next is False + assert res.per_page == 50 + assert res.items[0] == inbound + + def test_get_all_inbound_sms_when_none_exist(sample_service): res = dao_get_inbound_sms_for_service(sample_service.id) assert len(res) == 0 @@ -97,31 +108,31 @@ def test_get_inbound_sms_by_id_returns(sample_service): assert inbound_sms == inbound_from_db -def test_dao_get_paginated_inbound_sms_for_service(sample_service): +def test_dao_get_paginated_inbound_sms_for_service_for_public_api(sample_service): inbound_sms = create_inbound_sms(service=sample_service) - inbound_from_db = dao_get_paginated_inbound_sms_for_service(inbound_sms.service.id) + inbound_from_db = dao_get_paginated_inbound_sms_for_service_for_public_api(inbound_sms.service.id) assert inbound_sms == inbound_from_db[0] -def test_dao_get_paginated_inbound_sms_for_service_return_only_for_service(sample_service): +def test_dao_get_paginated_inbound_sms_for_service_for_public_api_return_only_for_service(sample_service): inbound_sms = create_inbound_sms(service=sample_service) another_service = create_service(service_name='another service') another_inbound_sms = create_inbound_sms(another_service) - inbound_from_db = dao_get_paginated_inbound_sms_for_service(inbound_sms.service.id) + inbound_from_db = dao_get_paginated_inbound_sms_for_service_for_public_api(inbound_sms.service.id) assert inbound_sms in inbound_from_db assert another_inbound_sms not in inbound_from_db -def test_dao_get_paginated_inbound_sms_for_service_no_inbound_sms_returns_empty_list(sample_service): - inbound_from_db = dao_get_paginated_inbound_sms_for_service(sample_service.id) +def test_dao_get_paginated_inbound_sms_for_service_for_public_api_no_inbound_sms_returns_empty_list(sample_service): + inbound_from_db = dao_get_paginated_inbound_sms_for_service_for_public_api(sample_service.id) assert inbound_from_db == [] -def test_dao_get_paginated_inbound_sms_for_service_page_size_returns_correct_size(sample_service): +def test_dao_get_paginated_inbound_sms_for_service_for_public_api_page_size_returns_correct_size(sample_service): inbound_sms_list = [ create_inbound_sms(sample_service), create_inbound_sms(sample_service), @@ -130,7 +141,7 @@ def test_dao_get_paginated_inbound_sms_for_service_page_size_returns_correct_siz ] reversed_inbound_sms = sorted(inbound_sms_list, key=lambda sms: sms.created_at, reverse=True) - inbound_from_db = dao_get_paginated_inbound_sms_for_service( + inbound_from_db = dao_get_paginated_inbound_sms_for_service_for_public_api( sample_service.id, older_than=reversed_inbound_sms[1].id, page_size=2 @@ -139,7 +150,7 @@ def test_dao_get_paginated_inbound_sms_for_service_page_size_returns_correct_siz assert len(inbound_from_db) == 2 -def test_dao_get_paginated_inbound_sms_for_service_older_than_returns_correct_list(sample_service): +def test_dao_get_paginated_inbound_sms_for_service_for_public_api_older_than_returns_correct_list(sample_service): inbound_sms_list = [ create_inbound_sms(sample_service), create_inbound_sms(sample_service), @@ -148,7 +159,7 @@ def test_dao_get_paginated_inbound_sms_for_service_older_than_returns_correct_li ] reversed_inbound_sms = sorted(inbound_sms_list, key=lambda sms: sms.created_at, reverse=True) - inbound_from_db = dao_get_paginated_inbound_sms_for_service( + inbound_from_db = dao_get_paginated_inbound_sms_for_service_for_public_api( sample_service.id, older_than=reversed_inbound_sms[1].id, page_size=2 @@ -159,14 +170,14 @@ def test_dao_get_paginated_inbound_sms_for_service_older_than_returns_correct_li assert expected_inbound_sms == inbound_from_db -def test_dao_get_paginated_inbound_sms_for_service_older_than_end_returns_empty_list(sample_service): +def test_dao_get_paginated_inbound_sms_for_service_for_public_api_older_than_end_returns_empty_list(sample_service): inbound_sms_list = [ create_inbound_sms(sample_service), create_inbound_sms(sample_service), ] reversed_inbound_sms = sorted(inbound_sms_list, key=lambda sms: sms.created_at, reverse=True) - inbound_from_db = dao_get_paginated_inbound_sms_for_service( + inbound_from_db = dao_get_paginated_inbound_sms_for_service_for_public_api( sample_service.id, older_than=reversed_inbound_sms[1].id, page_size=2