added new paginated inbound endpoint

This commit is contained in:
chrisw
2018-04-04 16:59:48 +01:00
parent 730eff7e97
commit 0f2ddd8cfa
4 changed files with 136 additions and 56 deletions

View File

@@ -4,7 +4,8 @@ from datetime import (
)
from flask import current_app
from notifications_utils.statsd_decorators import statsd
from sqlalchemy import desc
from sqlalchemy import desc, and_
from sqlalchemy.orm import aliased
from app import db
from app.dao.dao_utils import transactional
@@ -32,22 +33,6 @@ 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(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,
@@ -93,3 +78,48 @@ def dao_get_inbound_sms_by_id(service_id, inbound_id):
id=inbound_id,
service_id=service_id
).one()
def dao_get_paginated_most_recent_inbound_sms_by_user_number_for_service(
service_id,
page
):
"""
This query starts from inbound_sms and joins on to itself to find the most recent row for each user_number
Equivalent sql:
SELECT t1.*
FROM inbound_sms t1
LEFT OUTER JOIN inbound_sms AS t2 ON (
-- identifying
t1.user_number = t2.user_number AND
t1.service_id = t2.service_id AND
-- ordering
t1.created_at < t2.created_at
)
WHERE t2.id IS NULL AND t1.service_id = :service_id
ORDER BY t1.created_at DESC;
LIMIT 50 OFFSET :page
"""
t2 = aliased(InboundSms)
q = db.session.query(
InboundSms
).outerjoin(
t2,
and_(
InboundSms.user_number == t2.user_number,
InboundSms.service_id == t2.service_id,
InboundSms.created_at < t2.created_at
)
).filter(
t2.id == None, # noqa
InboundSms.service_id == service_id
).order_by(
InboundSms.created_at.desc()
)
return q.paginate(
page=page,
per_page=current_app.config['PAGE_SIZE']
)

View File

@@ -10,7 +10,7 @@ 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_paginated_inbound_sms_for_service
dao_get_paginated_most_recent_inbound_sms_by_user_number_for_service
)
from app.errors import register_errors
from app.schema_validation import validate
@@ -41,23 +41,27 @@ 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)
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
)
results = dao_get_inbound_sms_for_service(service_id, user_number=user_number)
return jsonify(data=[row.serialize() for row in results])
@inbound_sms.route('/most-recent', methods=['GET'])
def get_most_recent_inbound_sms_for_service(service_id):
# used on the service inbox page
page = request.args.get('page', 1)
# get most recent message for each user for service
results = dao_get_paginated_most_recent_inbound_sms_by_user_number_for_service(service_id, int(page))
return jsonify(
data=[row.serialize() for row in results.items],
has_next=results.has_next
)
@inbound_sms.route('/summary')