2017-05-31 14:49:14 +01:00
|
|
|
from flask import (
|
|
|
|
|
Blueprint,
|
|
|
|
|
jsonify,
|
2017-06-09 10:34:02 +01:00
|
|
|
request,
|
|
|
|
|
current_app, json)
|
|
|
|
|
from jsonschema import ValidationError
|
2017-06-06 17:12:21 +01:00
|
|
|
|
2017-06-02 12:57:28 +01:00
|
|
|
from notifications_utils.recipients import 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_get_inbound_sms_for_service,
|
|
|
|
|
dao_count_inbound_sms_for_service,
|
|
|
|
|
dao_get_inbound_sms_by_id
|
|
|
|
|
)
|
2017-05-31 14:49:14 +01:00
|
|
|
from app.errors import register_errors
|
2017-06-09 10:34:02 +01:00
|
|
|
from app.schema_validation import validate
|
|
|
|
|
|
|
|
|
|
from app.inbound_sms.inbound_sms_schemas import get_inbound_sms_for_service_schema
|
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-06-09 10:34:02 +01:00
|
|
|
@inbound_sms.route('', methods=['POST', 'GET'])
|
2017-05-31 14:49:14 +01:00
|
|
|
def get_inbound_sms_for_service(service_id):
|
|
|
|
|
|
2017-06-09 10:34:02 +01:00
|
|
|
if request.method == 'GET':
|
|
|
|
|
limit = request.args.get('limit')
|
|
|
|
|
user_number = request.args.get('user_number')
|
|
|
|
|
|
|
|
|
|
if user_number:
|
|
|
|
|
# we use this to normalise to an international phone number
|
|
|
|
|
user_number = validate_and_format_phone_number(user_number, international=True)
|
|
|
|
|
|
|
|
|
|
results = dao_get_inbound_sms_for_service(service_id, limit, user_number)
|
2017-05-31 14:49:14 +01:00
|
|
|
|
2017-06-09 10:34:02 +01:00
|
|
|
return jsonify(data=[row.serialize() for row in results])
|
|
|
|
|
else:
|
|
|
|
|
form = validate(request.get_json(), get_inbound_sms_for_service_schema)
|
|
|
|
|
results = dao_get_inbound_sms_for_service(service_id, form.get('limit'), form.get('phone_number'))
|
2017-05-31 14:49:14 +01:00
|
|
|
|
2017-06-09 10:34:02 +01:00
|
|
|
return jsonify(data=[row.serialize() for row in results])
|
2017-05-31 14:49:14 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@inbound_sms.route('/summary')
|
|
|
|
|
def get_inbound_sms_summary_for_service(service_id):
|
|
|
|
|
count = dao_count_inbound_sms_for_service(service_id)
|
|
|
|
|
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
|