mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-02 09:26:08 -05:00
Merge pull request #1790 from alphagov/update-paginated-inbound-sms-method
Update inbound sms method
This commit is contained in:
@@ -32,7 +32,23 @@ def dao_get_inbound_sms_for_service(service_id, limit=None, user_number=None):
|
|||||||
return q.all()
|
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,
|
service_id,
|
||||||
older_than=None,
|
older_than=None,
|
||||||
page_size=None
|
page_size=None
|
||||||
|
|||||||
@@ -9,7 +9,8 @@ from notifications_utils.recipients import try_validate_and_format_phone_number
|
|||||||
from app.dao.inbound_sms_dao import (
|
from app.dao.inbound_sms_dao import (
|
||||||
dao_get_inbound_sms_for_service,
|
dao_get_inbound_sms_for_service,
|
||||||
dao_count_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.errors import register_errors
|
||||||
from app.schema_validation import validate
|
from app.schema_validation import validate
|
||||||
@@ -41,15 +42,22 @@ def post_query_inbound_sms_for_service(service_id):
|
|||||||
@inbound_sms.route('', methods=['GET'])
|
@inbound_sms.route('', methods=['GET'])
|
||||||
def get_inbound_sms_for_service(service_id):
|
def get_inbound_sms_for_service(service_id):
|
||||||
limit = request.args.get('limit')
|
limit = request.args.get('limit')
|
||||||
|
page = request.args.get('page')
|
||||||
user_number = request.args.get('user_number')
|
user_number = request.args.get('user_number')
|
||||||
|
|
||||||
if user_number:
|
if user_number:
|
||||||
# we use this to normalise to an international phone number - but this may fail if it's an alphanumeric
|
# 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)
|
user_number = try_validate_and_format_phone_number(user_number, international=True)
|
||||||
|
|
||||||
results = dao_get_inbound_sms_for_service(service_id, limit, user_number)
|
if not page:
|
||||||
|
results = dao_get_inbound_sms_for_service(service_id, limit, user_number)
|
||||||
return jsonify(data=[row.serialize() for row in results])
|
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')
|
@inbound_sms.route('/summary')
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ from app.v2.inbound_sms.inbound_sms_schemas import get_inbound_sms_request
|
|||||||
def get_inbound_sms():
|
def get_inbound_sms():
|
||||||
data = validate(request.args.to_dict(), get_inbound_sms_request)
|
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,
|
authenticated_service.id,
|
||||||
older_than=data.get('older_than', None),
|
older_than=data.get('older_than', None),
|
||||||
page_size=current_app.config.get('API_PAGE_SIZE')
|
page_size=current_app.config.get('API_PAGE_SIZE')
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ from app.dao.inbound_sms_dao import (
|
|||||||
dao_count_inbound_sms_for_service,
|
dao_count_inbound_sms_for_service,
|
||||||
delete_inbound_sms_created_more_than_a_week_ago,
|
delete_inbound_sms_created_more_than_a_week_ago,
|
||||||
dao_get_inbound_sms_by_id,
|
dao_get_inbound_sms_by_id,
|
||||||
|
dao_get_paginated_inbound_sms_for_service_for_public_api,
|
||||||
dao_get_paginated_inbound_sms_for_service
|
dao_get_paginated_inbound_sms_for_service
|
||||||
)
|
)
|
||||||
from tests.app.db import create_inbound_sms, create_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
|
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):
|
def test_get_all_inbound_sms_when_none_exist(sample_service):
|
||||||
res = dao_get_inbound_sms_for_service(sample_service.id)
|
res = dao_get_inbound_sms_for_service(sample_service.id)
|
||||||
assert len(res) == 0
|
assert len(res) == 0
|
||||||
@@ -97,31 +108,31 @@ def test_get_inbound_sms_by_id_returns(sample_service):
|
|||||||
assert inbound_sms == inbound_from_db
|
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_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]
|
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)
|
inbound_sms = create_inbound_sms(service=sample_service)
|
||||||
another_service = create_service(service_name='another service')
|
another_service = create_service(service_name='another service')
|
||||||
another_inbound_sms = create_inbound_sms(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 inbound_sms in inbound_from_db
|
||||||
assert another_inbound_sms not 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):
|
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(sample_service.id)
|
inbound_from_db = dao_get_paginated_inbound_sms_for_service_for_public_api(sample_service.id)
|
||||||
|
|
||||||
assert inbound_from_db == []
|
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 = [
|
inbound_sms_list = [
|
||||||
create_inbound_sms(sample_service),
|
create_inbound_sms(sample_service),
|
||||||
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)
|
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,
|
sample_service.id,
|
||||||
older_than=reversed_inbound_sms[1].id,
|
older_than=reversed_inbound_sms[1].id,
|
||||||
page_size=2
|
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
|
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 = [
|
inbound_sms_list = [
|
||||||
create_inbound_sms(sample_service),
|
create_inbound_sms(sample_service),
|
||||||
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)
|
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,
|
sample_service.id,
|
||||||
older_than=reversed_inbound_sms[1].id,
|
older_than=reversed_inbound_sms[1].id,
|
||||||
page_size=2
|
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
|
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 = [
|
inbound_sms_list = [
|
||||||
create_inbound_sms(sample_service),
|
create_inbound_sms(sample_service),
|
||||||
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)
|
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,
|
sample_service.id,
|
||||||
older_than=reversed_inbound_sms[1].id,
|
older_than=reversed_inbound_sms[1].id,
|
||||||
page_size=2
|
page_size=2
|
||||||
|
|||||||
Reference in New Issue
Block a user