2021-03-10 13:55:06 +00:00
|
|
|
from flask import Blueprint, jsonify, request
|
2017-11-23 16:46:39 +00:00
|
|
|
from notifications_utils.recipients import try_validate_and_format_phone_number
|
2017-05-31 14:49:14 +01:00
|
|
|
|
2017-06-06 17:12:21 +01:00
|
|
|
from app.dao.inbound_sms_dao import (
|
|
|
|
|
dao_count_inbound_sms_for_service,
|
2018-03-22 12:41:17 +00:00
|
|
|
dao_get_inbound_sms_by_id,
|
2021-03-10 13:55:06 +00:00
|
|
|
dao_get_inbound_sms_for_service,
|
|
|
|
|
dao_get_paginated_most_recent_inbound_sms_by_user_number_for_service,
|
|
|
|
|
)
|
|
|
|
|
from app.dao.service_data_retention_dao import (
|
|
|
|
|
fetch_service_data_retention_by_notification_type,
|
2017-06-06 17:12:21 +01:00
|
|
|
)
|
2017-05-31 14:49:14 +01:00
|
|
|
from app.errors import register_errors
|
2021-03-10 13:55:06 +00:00
|
|
|
from app.inbound_sms.inbound_sms_schemas import (
|
|
|
|
|
get_inbound_sms_for_service_schema,
|
|
|
|
|
)
|
2017-06-09 10:34:02 +01:00
|
|
|
from app.schema_validation import validate
|
|
|
|
|
|
2017-05-31 14:49:14 +01:00
|
|
|
inbound_sms = Blueprint(
|
|
|
|
|
'inbound_sms',
|
|
|
|
|
__name__,
|
2017-06-07 14:23:31 +01:00
|
|
|
url_prefix='/service/<uuid:service_id>/inbound-sms'
|
2017-05-31 14:49:14 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
register_errors(inbound_sms)
|
|
|
|
|
|
|
|
|
|
|
2017-11-23 16:46:39 +00:00
|
|
|
@inbound_sms.route('', methods=['POST'])
|
2019-03-29 15:18:25 +00:00
|
|
|
def post_inbound_sms_for_service(service_id):
|
2017-11-23 16:46:39 +00:00
|
|
|
form = validate(request.get_json(), get_inbound_sms_for_service_schema)
|
2019-03-29 15:18:25 +00:00
|
|
|
user_number = form.get('phone_number')
|
2017-06-09 10:34:02 +01:00
|
|
|
|
2017-11-23 16:46:39 +00:00
|
|
|
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)
|
|
|
|
|
|
2019-03-28 15:36:12 +00:00
|
|
|
inbound_data_retention = fetch_service_data_retention_by_notification_type(service_id, 'sms')
|
|
|
|
|
limit_days = inbound_data_retention.days_of_retention if inbound_data_retention else 7
|
|
|
|
|
|
|
|
|
|
results = dao_get_inbound_sms_for_service(service_id, user_number=user_number, limit_days=limit_days)
|
2018-04-04 16:59:48 +01:00
|
|
|
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)
|
|
|
|
|
|
2019-03-27 17:44:53 +00:00
|
|
|
inbound_data_retention = fetch_service_data_retention_by_notification_type(service_id, 'sms')
|
|
|
|
|
limit_days = inbound_data_retention.days_of_retention if inbound_data_retention else 7
|
|
|
|
|
|
|
|
|
|
# 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), limit_days)
|
2018-04-04 16:59:48 +01:00
|
|
|
return jsonify(
|
|
|
|
|
data=[row.serialize() for row in results.items],
|
|
|
|
|
has_next=results.has_next
|
|
|
|
|
)
|
2017-05-31 14:49:14 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@inbound_sms.route('/summary')
|
|
|
|
|
def get_inbound_sms_summary_for_service(service_id):
|
2019-03-27 17:44:53 +00:00
|
|
|
# this is for the dashboard, so always limit to 7 days, even if they have a longer data retention
|
|
|
|
|
count = dao_count_inbound_sms_for_service(service_id, limit_days=7)
|
2017-05-31 14:49:14 +01:00
|
|
|
most_recent = dao_get_inbound_sms_for_service(service_id, limit=1)
|
|
|
|
|
|
|
|
|
|
return jsonify(
|
|
|
|
|
count=count,
|
|
|
|
|
most_recent=most_recent[0].created_at.isoformat() if most_recent else None
|
|
|
|
|
)
|
2017-06-06 17:12:21 +01:00
|
|
|
|
|
|
|
|
|
2017-06-07 14:23:31 +01:00
|
|
|
@inbound_sms.route('/<uuid:inbound_sms_id>', methods=['GET'])
|
2017-06-06 17:12:21 +01:00
|
|
|
def get_inbound_by_id(service_id, inbound_sms_id):
|
2017-06-09 10:34:02 +01:00
|
|
|
message = dao_get_inbound_sms_by_id(service_id, inbound_sms_id)
|
2017-06-06 17:12:21 +01:00
|
|
|
|
2017-06-09 10:34:02 +01:00
|
|
|
return jsonify(message.serialize()), 200
|