2016-08-19 16:36:20 +01:00
|
|
|
|
import itertools
|
2017-03-29 11:05:31 +01:00
|
|
|
|
from datetime import datetime
|
2016-08-19 16:36:20 +01:00
|
|
|
|
|
2016-02-22 12:55:18 +00:00
|
|
|
|
from flask import (
|
|
|
|
|
|
jsonify,
|
2016-06-07 16:31:10 +01:00
|
|
|
|
request,
|
2017-05-24 16:27:12 +01:00
|
|
|
|
current_app,
|
|
|
|
|
|
Blueprint
|
2016-02-22 12:55:18 +00:00
|
|
|
|
)
|
2018-11-22 11:53:32 +00:00
|
|
|
|
from notifications_utils.letter_timings import letter_can_be_cancelled
|
|
|
|
|
|
from notifications_utils.timezones import convert_utc_to_bst
|
2018-02-19 14:33:44 +00:00
|
|
|
|
from sqlalchemy.exc import IntegrityError
|
2016-01-08 17:51:46 +00:00
|
|
|
|
from sqlalchemy.orm.exc import NoResultFound
|
2016-02-19 15:53:45 +00:00
|
|
|
|
|
2019-05-10 15:32:24 +01:00
|
|
|
|
from app.config import QueueNames
|
2019-07-19 10:57:14 +01:00
|
|
|
|
from app.dao import fact_notification_status_dao, notifications_dao
|
2016-09-23 12:21:00 +01:00
|
|
|
|
from app.dao.dao_utils import dao_rollback
|
2018-06-28 15:10:14 +01:00
|
|
|
|
from app.dao.date_util import get_financial_year
|
2016-02-25 12:11:51 +00:00
|
|
|
|
from app.dao.api_key_dao import (
|
|
|
|
|
|
save_model_api_key,
|
|
|
|
|
|
get_model_api_keys,
|
2016-06-22 15:27:28 +01:00
|
|
|
|
get_unsigned_secret,
|
|
|
|
|
|
expire_api_key)
|
2018-06-28 15:10:14 +01:00
|
|
|
|
from app.dao.fact_notification_status_dao import (
|
|
|
|
|
|
fetch_notification_status_for_service_by_month,
|
2018-11-06 13:30:37 +00:00
|
|
|
|
fetch_notification_status_for_service_for_day,
|
2019-01-04 16:45:39 +00:00
|
|
|
|
fetch_notification_status_for_service_for_today_and_7_previous_days,
|
2019-01-10 16:24:51 +00:00
|
|
|
|
fetch_stats_for_all_services_by_date_range, fetch_monthly_template_usage_for_service
|
|
|
|
|
|
)
|
2017-10-19 09:58:23 +01:00
|
|
|
|
from app.dao.inbound_numbers_dao import dao_allocate_number_for_service
|
2018-02-10 01:37:17 +00:00
|
|
|
|
from app.dao.organisation_dao import dao_get_organisation_by_service_id
|
2018-07-13 15:18:27 +01:00
|
|
|
|
from app.dao.service_data_retention_dao import (
|
|
|
|
|
|
fetch_service_data_retention,
|
|
|
|
|
|
fetch_service_data_retention_by_id,
|
2018-08-13 16:44:24 +01:00
|
|
|
|
fetch_service_data_retention_by_notification_type,
|
2018-07-13 15:18:27 +01:00
|
|
|
|
insert_service_data_retention,
|
|
|
|
|
|
update_service_data_retention,
|
|
|
|
|
|
)
|
2017-10-19 13:51:33 +01:00
|
|
|
|
from app.dao.service_sms_sender_dao import (
|
2018-04-26 09:05:17 +01:00
|
|
|
|
archive_sms_sender,
|
2017-10-19 13:51:33 +01:00
|
|
|
|
dao_add_sms_sender_for_service,
|
|
|
|
|
|
dao_update_service_sms_sender,
|
|
|
|
|
|
dao_get_service_sms_senders_by_id,
|
2017-10-25 11:58:54 +01:00
|
|
|
|
dao_get_sms_senders_by_service_id,
|
2017-11-07 14:26:18 +00:00
|
|
|
|
update_existing_sms_sender_with_inbound_number
|
|
|
|
|
|
)
|
2016-01-12 10:39:49 +00:00
|
|
|
|
from app.dao.services_dao import (
|
2017-11-14 14:32:34 +00:00
|
|
|
|
dao_add_user_to_service,
|
|
|
|
|
|
dao_archive_service,
|
2016-02-19 15:53:45 +00:00
|
|
|
|
dao_create_service,
|
2017-11-14 14:32:34 +00:00
|
|
|
|
dao_fetch_all_services,
|
2016-03-01 15:36:31 +00:00
|
|
|
|
dao_fetch_all_services_by_user,
|
2019-04-25 18:09:33 +01:00
|
|
|
|
dao_fetch_live_services_data,
|
2017-11-14 14:32:34 +00:00
|
|
|
|
dao_fetch_service_by_id,
|
2016-07-28 13:34:24 +01:00
|
|
|
|
dao_fetch_todays_stats_for_service,
|
2016-11-10 17:07:02 +00:00
|
|
|
|
dao_fetch_todays_stats_for_all_services,
|
2017-01-30 15:17:26 +00:00
|
|
|
|
dao_resume_service,
|
2017-11-14 14:32:34 +00:00
|
|
|
|
dao_remove_user_from_service,
|
|
|
|
|
|
dao_suspend_service,
|
|
|
|
|
|
dao_update_service,
|
2019-08-13 17:20:37 +01:00
|
|
|
|
get_services_by_partial_name,
|
2017-11-14 14:32:34 +00:00
|
|
|
|
)
|
2016-09-20 17:35:15 +01:00
|
|
|
|
from app.dao.service_whitelist_dao import (
|
|
|
|
|
|
dao_fetch_service_whitelist,
|
2016-09-22 17:17:34 +01:00
|
|
|
|
dao_add_and_commit_whitelisted_contacts,
|
|
|
|
|
|
dao_remove_service_whitelist
|
2016-09-20 17:35:15 +01:00
|
|
|
|
)
|
2017-09-25 16:01:24 +01:00
|
|
|
|
from app.dao.service_email_reply_to_dao import (
|
|
|
|
|
|
add_reply_to_email_address_for_service,
|
2018-04-25 16:34:36 +01:00
|
|
|
|
archive_reply_to_email_address,
|
2017-09-25 16:01:24 +01:00
|
|
|
|
dao_get_reply_to_by_id,
|
|
|
|
|
|
dao_get_reply_to_by_service_id,
|
|
|
|
|
|
update_reply_to_email_address
|
|
|
|
|
|
)
|
2017-10-04 12:26:38 +01:00
|
|
|
|
from app.dao.service_letter_contact_dao import (
|
2018-04-26 10:00:11 +01:00
|
|
|
|
archive_letter_contact,
|
2017-10-04 12:26:38 +01:00
|
|
|
|
dao_get_letter_contacts_by_service_id,
|
2017-10-04 14:41:36 +01:00
|
|
|
|
dao_get_letter_contact_by_id,
|
|
|
|
|
|
add_letter_contact_for_service,
|
2017-10-04 15:35:41 +01:00
|
|
|
|
update_letter_contact
|
2017-10-04 12:26:38 +01:00
|
|
|
|
)
|
2019-05-10 15:32:24 +01:00
|
|
|
|
from app.dao.templates_dao import dao_get_template_by_id
|
2016-11-07 17:42:23 +00:00
|
|
|
|
from app.dao.users_dao import get_user_by_id
|
2016-09-28 10:16:10 +01:00
|
|
|
|
from app.errors import (
|
2017-05-24 16:27:12 +01:00
|
|
|
|
InvalidRequest,
|
|
|
|
|
|
register_errors
|
|
|
|
|
|
)
|
2018-11-22 11:53:32 +00:00
|
|
|
|
from app.letters.utils import letter_print_day
|
2019-05-10 15:32:24 +01:00
|
|
|
|
from app.models import (
|
2019-07-16 14:56:04 +01:00
|
|
|
|
KEY_TYPE_NORMAL, LETTER_TYPE, NOTIFICATION_CANCELLED, Permission, Service,
|
2019-07-10 18:17:33 +01:00
|
|
|
|
EmailBranding, LetterBranding
|
2019-05-10 15:32:24 +01:00
|
|
|
|
)
|
|
|
|
|
|
from app.notifications.process_notifications import persist_notification, send_notification_to_queue
|
2017-06-15 11:32:51 +01:00
|
|
|
|
from app.schema_validation import validate
|
2016-09-28 10:16:10 +01:00
|
|
|
|
from app.service import statistics
|
2019-10-25 11:27:28 +01:00
|
|
|
|
from app.service.send_pdf_letter_schema import send_pdf_letter_request
|
2018-07-11 17:02:49 +01:00
|
|
|
|
from app.service.service_data_retention_schema import (
|
|
|
|
|
|
add_service_data_retention_request,
|
|
|
|
|
|
update_service_data_retention_request
|
|
|
|
|
|
)
|
2017-10-04 14:41:36 +01:00
|
|
|
|
from app.service.service_senders_schema import (
|
|
|
|
|
|
add_service_email_reply_to_request,
|
|
|
|
|
|
add_service_letter_contact_block_request,
|
2017-11-07 14:26:18 +00:00
|
|
|
|
add_service_sms_sender_request
|
|
|
|
|
|
)
|
2016-09-28 10:16:10 +01:00
|
|
|
|
from app.service.utils import get_whitelist_objects
|
2017-05-12 14:07:06 +01:00
|
|
|
|
from app.service.sender import send_notification_to_service_users
|
2019-09-06 09:02:49 +01:00
|
|
|
|
from app.service.send_notification import send_one_off_notification, send_pdf_letter_notification
|
2016-01-13 12:14:21 +00:00
|
|
|
|
from app.schemas import (
|
2016-02-19 15:53:45 +00:00
|
|
|
|
service_schema,
|
2016-02-26 12:00:16 +00:00
|
|
|
|
api_key_schema,
|
2016-07-26 12:34:39 +01:00
|
|
|
|
notification_with_template_schema,
|
2016-06-28 15:17:36 +01:00
|
|
|
|
notifications_filter_schema,
|
2019-05-10 15:32:24 +01:00
|
|
|
|
detailed_service_schema,
|
|
|
|
|
|
email_data_request_schema
|
2016-03-03 15:17:14 +00:00
|
|
|
|
)
|
2019-03-12 19:07:12 +00:00
|
|
|
|
from app.user.users_schema import post_set_permissions_schema
|
2018-11-22 11:53:32 +00:00
|
|
|
|
from app.utils import pagination_links
|
2017-03-15 16:52:44 +00:00
|
|
|
|
|
2016-08-19 17:30:25 +01:00
|
|
|
|
service_blueprint = Blueprint('service', __name__)
|
2017-03-15 16:52:44 +00:00
|
|
|
|
|
2016-08-19 17:30:25 +01:00
|
|
|
|
register_errors(service_blueprint)
|
2016-02-17 17:04:50 +00:00
|
|
|
|
|
2016-01-08 17:51:46 +00:00
|
|
|
|
|
2018-02-19 14:33:44 +00:00
|
|
|
|
@service_blueprint.errorhandler(IntegrityError)
|
|
|
|
|
|
def handle_integrity_error(exc):
|
|
|
|
|
|
"""
|
|
|
|
|
|
Handle integrity errors caused by the unique constraint on ix_organisation_name
|
|
|
|
|
|
"""
|
2019-03-14 12:09:29 +00:00
|
|
|
|
if any(
|
|
|
|
|
|
'duplicate key value violates unique constraint "{}"'.format(constraint) in str(exc)
|
|
|
|
|
|
for constraint in {'services_name_key', 'services_email_from_key'}
|
|
|
|
|
|
):
|
2018-02-19 14:33:44 +00:00
|
|
|
|
return jsonify(
|
|
|
|
|
|
result='error',
|
|
|
|
|
|
message={'name': ["Duplicate service name '{}'".format(
|
|
|
|
|
|
exc.params.get('name', exc.params.get('email_from', ''))
|
|
|
|
|
|
)]}
|
|
|
|
|
|
), 400
|
|
|
|
|
|
current_app.logger.exception(exc)
|
|
|
|
|
|
return jsonify(result='error', message="Internal server error"), 500
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-08-19 17:30:25 +01:00
|
|
|
|
@service_blueprint.route('', methods=['GET'])
|
2016-02-19 15:53:45 +00:00
|
|
|
|
def get_services():
|
2016-11-09 15:07:23 +00:00
|
|
|
|
only_active = request.args.get('only_active') == 'True'
|
|
|
|
|
|
detailed = request.args.get('detailed') == 'True'
|
2016-02-19 15:53:45 +00:00
|
|
|
|
user_id = request.args.get('user_id', None)
|
2016-12-02 17:40:12 +00:00
|
|
|
|
include_from_test_key = request.args.get('include_from_test_key', 'True') != 'False'
|
2017-01-31 16:12:46 +00:00
|
|
|
|
|
2016-12-29 13:50:41 +00:00
|
|
|
|
# If start and end date are not set, we are expecting today's stats.
|
2017-01-31 16:12:46 +00:00
|
|
|
|
today = str(datetime.utcnow().date())
|
|
|
|
|
|
|
|
|
|
|
|
start_date = datetime.strptime(request.args.get('start_date', today), '%Y-%m-%d').date()
|
|
|
|
|
|
end_date = datetime.strptime(request.args.get('end_date', today), '%Y-%m-%d').date()
|
2016-11-09 15:07:23 +00:00
|
|
|
|
|
2016-02-19 15:53:45 +00:00
|
|
|
|
if user_id:
|
2016-11-09 15:07:23 +00:00
|
|
|
|
services = dao_fetch_all_services_by_user(user_id, only_active)
|
|
|
|
|
|
elif detailed:
|
2017-09-15 14:40:56 +01:00
|
|
|
|
result = jsonify(data=get_detailed_services(start_date=start_date, end_date=end_date,
|
2017-09-15 15:00:13 +01:00
|
|
|
|
only_active=only_active,
|
2017-10-02 12:00:52 +01:00
|
|
|
|
include_from_test_key=include_from_test_key
|
2017-09-15 15:00:13 +01:00
|
|
|
|
))
|
2017-09-15 14:40:56 +01:00
|
|
|
|
return result
|
2016-02-19 15:53:45 +00:00
|
|
|
|
else:
|
2016-11-09 15:07:23 +00:00
|
|
|
|
services = dao_fetch_all_services(only_active)
|
2016-06-14 15:07:23 +01:00
|
|
|
|
data = service_schema.dump(services, many=True).data
|
2016-02-19 15:53:45 +00:00
|
|
|
|
return jsonify(data=data)
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-08-13 17:20:37 +01:00
|
|
|
|
@service_blueprint.route('/find-services-by-name', methods=['GET'])
|
|
|
|
|
|
def find_services_by_name():
|
|
|
|
|
|
service_name = request.args.get('service_name')
|
|
|
|
|
|
if not service_name:
|
|
|
|
|
|
errors = {'service_name': ['Missing data for required field.']}
|
|
|
|
|
|
raise InvalidRequest(errors, status_code=400)
|
|
|
|
|
|
fetched_services = get_services_by_partial_name(service_name)
|
2019-08-16 17:30:04 +01:00
|
|
|
|
data = [service.serialize_for_org_dashboard() for service in fetched_services]
|
2019-08-13 17:20:37 +01:00
|
|
|
|
return jsonify(data=data), 200
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-04-25 18:09:33 +01:00
|
|
|
|
@service_blueprint.route('/live-services-data', methods=['GET'])
|
|
|
|
|
|
def get_live_services_data():
|
|
|
|
|
|
data = dao_fetch_live_services_data()
|
|
|
|
|
|
return jsonify(data=data)
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-08-19 17:30:25 +01:00
|
|
|
|
@service_blueprint.route('/<uuid:service_id>', methods=['GET'])
|
2016-02-19 15:53:45 +00:00
|
|
|
|
def get_service_by_id(service_id):
|
2016-07-25 14:27:06 +01:00
|
|
|
|
if request.args.get('detailed') == 'True':
|
2016-08-19 17:36:31 +01:00
|
|
|
|
data = get_detailed_service(service_id, today_only=request.args.get('today_only') == 'True')
|
2016-07-18 12:03:44 +01:00
|
|
|
|
else:
|
|
|
|
|
|
fetched = dao_fetch_service_by_id(service_id)
|
2016-03-11 15:34:20 +00:00
|
|
|
|
|
2016-07-18 12:03:44 +01:00
|
|
|
|
data = service_schema.dump(fetched).data
|
2017-05-22 11:33:24 +01:00
|
|
|
|
return jsonify(data=data)
|
2016-02-19 15:53:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
2018-05-09 11:36:42 +01:00
|
|
|
|
@service_blueprint.route('/<uuid:service_id>/statistics')
|
|
|
|
|
|
def get_service_notification_statistics(service_id):
|
2018-08-13 16:34:04 +01:00
|
|
|
|
return jsonify(data=get_service_statistics(
|
|
|
|
|
|
service_id,
|
|
|
|
|
|
request.args.get('today_only') == 'True',
|
|
|
|
|
|
int(request.args.get('limit_days', 7))
|
|
|
|
|
|
))
|
2018-05-09 11:36:42 +01:00
|
|
|
|
|
|
|
|
|
|
|
2016-08-19 17:30:25 +01:00
|
|
|
|
@service_blueprint.route('', methods=['POST'])
|
2016-01-08 17:51:46 +00:00
|
|
|
|
def create_service():
|
2016-02-19 15:53:45 +00:00
|
|
|
|
data = request.get_json()
|
2017-12-06 11:01:18 +00:00
|
|
|
|
|
|
|
|
|
|
if not data.get('user_id'):
|
|
|
|
|
|
errors = {'user_id': ['Missing data for required field.']}
|
2016-06-14 15:07:23 +01:00
|
|
|
|
raise InvalidRequest(errors, status_code=400)
|
2019-04-05 16:26:52 +01:00
|
|
|
|
data.pop('service_domain', None)
|
|
|
|
|
|
|
2017-05-24 16:27:12 +01:00
|
|
|
|
# validate json with marshmallow
|
2017-12-01 16:31:21 +00:00
|
|
|
|
service_schema.load(data)
|
2017-05-24 16:27:12 +01:00
|
|
|
|
|
2017-12-06 11:01:18 +00:00
|
|
|
|
user = get_user_by_id(data.pop('user_id'))
|
2017-05-24 16:27:12 +01:00
|
|
|
|
|
|
|
|
|
|
# unpack valid json into service object
|
|
|
|
|
|
valid_service = Service.from_json(data)
|
|
|
|
|
|
|
2019-04-05 16:26:52 +01:00
|
|
|
|
dao_create_service(valid_service, user)
|
2019-01-22 17:27:00 +00:00
|
|
|
|
|
2016-02-19 15:53:45 +00:00
|
|
|
|
return jsonify(data=service_schema.dump(valid_service).data), 201
|
2016-01-08 17:51:46 +00:00
|
|
|
|
|
2016-02-19 15:53:45 +00:00
|
|
|
|
|
2016-08-19 17:30:25 +01:00
|
|
|
|
@service_blueprint.route('/<uuid:service_id>', methods=['POST'])
|
2016-01-08 17:51:46 +00:00
|
|
|
|
def update_service(service_id):
|
2017-09-08 12:09:45 +01:00
|
|
|
|
req_json = request.get_json()
|
2016-02-19 15:53:45 +00:00
|
|
|
|
fetched_service = dao_fetch_service_by_id(service_id)
|
2017-05-12 14:07:06 +01:00
|
|
|
|
# Capture the status change here as Marshmallow changes this later
|
2017-09-08 12:09:45 +01:00
|
|
|
|
service_going_live = fetched_service.restricted and not req_json.get('restricted', True)
|
2016-02-19 15:53:45 +00:00
|
|
|
|
current_data = dict(service_schema.dump(fetched_service).data.items())
|
|
|
|
|
|
current_data.update(request.get_json())
|
2017-12-04 16:07:26 +00:00
|
|
|
|
|
2018-02-01 17:16:48 +00:00
|
|
|
|
service = service_schema.load(current_data).data
|
|
|
|
|
|
|
2018-02-05 12:02:35 +00:00
|
|
|
|
if 'email_branding' in req_json:
|
|
|
|
|
|
email_branding_id = req_json['email_branding']
|
|
|
|
|
|
service.email_branding = None if not email_branding_id else EmailBranding.query.get(email_branding_id)
|
2019-01-22 17:27:00 +00:00
|
|
|
|
if 'letter_branding' in req_json:
|
|
|
|
|
|
letter_branding_id = req_json['letter_branding']
|
|
|
|
|
|
service.letter_branding = None if not letter_branding_id else LetterBranding.query.get(letter_branding_id)
|
2018-02-01 17:16:48 +00:00
|
|
|
|
dao_update_service(service)
|
2017-09-25 12:26:29 +01:00
|
|
|
|
|
2017-05-12 14:07:06 +01:00
|
|
|
|
if service_going_live:
|
|
|
|
|
|
send_notification_to_service_users(
|
|
|
|
|
|
service_id=service_id,
|
|
|
|
|
|
template_id=current_app.config['SERVICE_NOW_LIVE_TEMPLATE_ID'],
|
|
|
|
|
|
personalisation={
|
|
|
|
|
|
'service_name': current_data['name'],
|
2017-05-16 12:33:01 +01:00
|
|
|
|
'message_limit': '{:,}'.format(current_data['message_limit'])
|
2017-05-12 14:07:06 +01:00
|
|
|
|
},
|
|
|
|
|
|
include_user_fields=['name']
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2016-02-19 15:53:45 +00:00
|
|
|
|
return jsonify(data=service_schema.dump(fetched_service).data), 200
|
2016-01-13 11:04:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
2016-08-19 17:30:25 +01:00
|
|
|
|
@service_blueprint.route('/<uuid:service_id>/api-key', methods=['POST'])
|
2016-06-23 16:44:57 +01:00
|
|
|
|
def create_api_key(service_id=None):
|
2016-02-19 15:53:45 +00:00
|
|
|
|
fetched_service = dao_fetch_service_by_id(service_id=service_id)
|
2016-06-14 15:07:23 +01:00
|
|
|
|
valid_api_key = api_key_schema.load(request.get_json()).data
|
2016-04-20 17:25:20 +01:00
|
|
|
|
valid_api_key.service = fetched_service
|
|
|
|
|
|
save_model_api_key(valid_api_key)
|
|
|
|
|
|
unsigned_api_key = get_unsigned_secret(valid_api_key.id)
|
2016-01-19 12:07:00 +00:00
|
|
|
|
return jsonify(data=unsigned_api_key), 201
|
2016-01-13 14:05:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
2016-08-19 17:30:25 +01:00
|
|
|
|
@service_blueprint.route('/<uuid:service_id>/api-key/revoke/<uuid:api_key_id>', methods=['POST'])
|
2016-01-20 14:48:44 +00:00
|
|
|
|
def revoke_api_key(service_id, api_key_id):
|
2016-06-22 15:27:28 +01:00
|
|
|
|
expire_api_key(service_id=service_id, api_key_id=api_key_id)
|
2016-01-14 11:30:45 +00:00
|
|
|
|
return jsonify(), 202
|
2016-01-13 09:25:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
2016-08-19 17:30:25 +01:00
|
|
|
|
@service_blueprint.route('/<uuid:service_id>/api-keys', methods=['GET'])
|
|
|
|
|
|
@service_blueprint.route('/<uuid:service_id>/api-keys/<uuid:key_id>', methods=['GET'])
|
2016-01-21 12:13:17 +00:00
|
|
|
|
def get_api_keys(service_id, key_id=None):
|
2016-03-11 15:34:20 +00:00
|
|
|
|
dao_fetch_service_by_id(service_id=service_id)
|
|
|
|
|
|
|
2016-01-20 15:23:32 +00:00
|
|
|
|
try:
|
2016-01-21 12:13:17 +00:00
|
|
|
|
if key_id:
|
|
|
|
|
|
api_keys = [get_model_api_keys(service_id=service_id, id=key_id)]
|
|
|
|
|
|
else:
|
|
|
|
|
|
api_keys = get_model_api_keys(service_id=service_id)
|
|
|
|
|
|
except NoResultFound:
|
2016-06-14 15:07:23 +01:00
|
|
|
|
error = "API key not found for id: {}".format(service_id)
|
|
|
|
|
|
raise InvalidRequest(error, status_code=404)
|
2016-01-20 15:23:32 +00:00
|
|
|
|
|
2016-02-26 12:00:16 +00:00
|
|
|
|
return jsonify(apiKeys=api_key_schema.dump(api_keys, many=True).data), 200
|
2016-02-23 17:52:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
2016-08-19 17:30:25 +01:00
|
|
|
|
@service_blueprint.route('/<uuid:service_id>/users', methods=['GET'])
|
2016-02-23 17:52:55 +00:00
|
|
|
|
def get_users_for_service(service_id):
|
|
|
|
|
|
fetched = dao_fetch_service_by_id(service_id)
|
2018-03-06 17:47:29 +00:00
|
|
|
|
return jsonify(data=[x.serialize() for x in fetched.users])
|
2016-02-25 12:11:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
2016-08-19 17:30:25 +01:00
|
|
|
|
@service_blueprint.route('/<uuid:service_id>/users/<user_id>', methods=['POST'])
|
2016-02-29 17:38:02 +00:00
|
|
|
|
def add_user_to_service(service_id, user_id):
|
|
|
|
|
|
service = dao_fetch_service_by_id(service_id)
|
2016-11-07 17:42:23 +00:00
|
|
|
|
user = get_user_by_id(user_id=user_id)
|
2016-02-29 17:38:02 +00:00
|
|
|
|
|
|
|
|
|
|
if user in service.users:
|
2016-06-14 15:07:23 +01:00
|
|
|
|
error = 'User id: {} already part of service id: {}'.format(user_id, service_id)
|
|
|
|
|
|
raise InvalidRequest(error, status_code=400)
|
2016-02-29 17:38:02 +00:00
|
|
|
|
|
2019-03-12 16:46:15 +00:00
|
|
|
|
data = request.get_json()
|
2019-03-12 19:07:12 +00:00
|
|
|
|
validate(data, post_set_permissions_schema)
|
|
|
|
|
|
|
|
|
|
|
|
permissions = [
|
|
|
|
|
|
Permission(service_id=service_id, user_id=user_id, permission=p['permission'])
|
|
|
|
|
|
for p in data['permissions']
|
|
|
|
|
|
]
|
2019-03-14 16:55:48 +00:00
|
|
|
|
folder_permissions = data.get('folder_permissions', [])
|
2019-03-12 16:46:15 +00:00
|
|
|
|
|
2019-03-14 16:55:48 +00:00
|
|
|
|
dao_add_user_to_service(service, user, permissions, folder_permissions)
|
2016-06-14 15:07:23 +01:00
|
|
|
|
data = service_schema.dump(service).data
|
2016-02-29 17:38:02 +00:00
|
|
|
|
return jsonify(data=data), 201
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-08-19 17:30:25 +01:00
|
|
|
|
@service_blueprint.route('/<uuid:service_id>/users/<user_id>', methods=['DELETE'])
|
2016-03-22 13:14:23 +00:00
|
|
|
|
def remove_user_from_service(service_id, user_id):
|
|
|
|
|
|
service = dao_fetch_service_by_id(service_id)
|
2016-11-07 17:42:23 +00:00
|
|
|
|
user = get_user_by_id(user_id=user_id)
|
2016-03-22 13:14:23 +00:00
|
|
|
|
if user not in service.users:
|
2016-06-14 15:07:23 +01:00
|
|
|
|
error = 'User not found'
|
|
|
|
|
|
raise InvalidRequest(error, status_code=404)
|
|
|
|
|
|
|
2016-03-22 13:14:23 +00:00
|
|
|
|
elif len(service.users) == 1:
|
2016-06-14 15:07:23 +01:00
|
|
|
|
error = 'You cannot remove the only user for a service'
|
|
|
|
|
|
raise InvalidRequest(error, status_code=400)
|
|
|
|
|
|
|
2016-03-22 13:14:23 +00:00
|
|
|
|
dao_remove_user_from_service(service, user)
|
|
|
|
|
|
return jsonify({}), 204
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-04-21 16:32:20 +01:00
|
|
|
|
# This is placeholder get method until more thought
|
|
|
|
|
|
# goes into how we want to fetch and view various items in history
|
|
|
|
|
|
# tables. This is so product owner can pass stories as done
|
2016-08-19 17:30:25 +01:00
|
|
|
|
@service_blueprint.route('/<uuid:service_id>/history', methods=['GET'])
|
2016-04-25 10:38:37 +01:00
|
|
|
|
def get_service_history(service_id):
|
2017-12-08 15:01:19 +00:00
|
|
|
|
from app.models import (Service, ApiKey, TemplateHistory)
|
2016-04-25 10:38:37 +01:00
|
|
|
|
from app.schemas import (
|
|
|
|
|
|
service_history_schema,
|
|
|
|
|
|
api_key_history_schema,
|
2017-12-08 15:01:19 +00:00
|
|
|
|
template_history_schema
|
2016-04-25 10:38:37 +01:00
|
|
|
|
)
|
2016-04-21 16:32:20 +01:00
|
|
|
|
|
|
|
|
|
|
service_history = Service.get_history_model().query.filter_by(id=service_id).all()
|
2016-06-14 15:07:23 +01:00
|
|
|
|
service_data = service_history_schema.dump(service_history, many=True).data
|
2016-04-21 16:32:20 +01:00
|
|
|
|
api_key_history = ApiKey.get_history_model().query.filter_by(service_id=service_id).all()
|
2016-06-14 15:07:23 +01:00
|
|
|
|
api_keys_data = api_key_history_schema.dump(api_key_history, many=True).data
|
2016-04-21 16:32:20 +01:00
|
|
|
|
|
2016-08-02 16:23:14 +01:00
|
|
|
|
template_history = TemplateHistory.query.filter_by(service_id=service_id).all()
|
2016-04-25 10:38:37 +01:00
|
|
|
|
template_data, errors = template_history_schema.dump(template_history, many=True)
|
|
|
|
|
|
|
|
|
|
|
|
data = {
|
|
|
|
|
|
'service_history': service_data,
|
|
|
|
|
|
'api_key_history': api_keys_data,
|
2016-04-27 17:30:15 +01:00
|
|
|
|
'template_history': template_data,
|
2017-12-08 15:01:19 +00:00
|
|
|
|
'events': []}
|
2016-04-21 16:32:20 +01:00
|
|
|
|
|
|
|
|
|
|
return jsonify(data=data)
|
2016-06-28 15:17:36 +01:00
|
|
|
|
|
|
|
|
|
|
|
2016-08-19 17:30:25 +01:00
|
|
|
|
@service_blueprint.route('/<uuid:service_id>/notifications', methods=['GET'])
|
2016-06-28 15:17:36 +01:00
|
|
|
|
def get_all_notifications_for_service(service_id):
|
|
|
|
|
|
data = notifications_filter_schema.load(request.args).data
|
2017-05-24 14:25:38 +01:00
|
|
|
|
if data.get('to'):
|
2018-08-03 14:35:36 +01:00
|
|
|
|
notification_type = data.get('template_type')[0] if data.get('template_type') else None
|
2018-03-07 17:11:29 +00:00
|
|
|
|
return search_for_notification_by_to_field(service_id=service_id,
|
|
|
|
|
|
search_term=data['to'],
|
|
|
|
|
|
statuses=data.get('status'),
|
|
|
|
|
|
notification_type=notification_type)
|
2016-06-28 15:17:36 +01:00
|
|
|
|
page = data['page'] if 'page' in data else 1
|
|
|
|
|
|
page_size = data['page_size'] if 'page_size' in data else current_app.config.get('PAGE_SIZE')
|
2018-08-03 14:35:36 +01:00
|
|
|
|
limit_days = data.get('limit_days')
|
2016-09-21 09:46:34 +01:00
|
|
|
|
include_jobs = data.get('include_jobs', True)
|
2016-09-23 10:35:31 +01:00
|
|
|
|
include_from_test_key = data.get('include_from_test_key', False)
|
2018-07-18 10:54:20 +01:00
|
|
|
|
include_one_off = data.get('include_one_off', True)
|
2016-06-28 15:17:36 +01:00
|
|
|
|
|
2019-01-07 17:12:00 +00:00
|
|
|
|
count_pages = data.get('count_pages', True)
|
|
|
|
|
|
|
2016-06-28 15:17:36 +01:00
|
|
|
|
pagination = notifications_dao.get_notifications_for_service(
|
|
|
|
|
|
service_id,
|
|
|
|
|
|
filter_dict=data,
|
|
|
|
|
|
page=page,
|
|
|
|
|
|
page_size=page_size,
|
2019-01-07 17:12:00 +00:00
|
|
|
|
count_pages=count_pages,
|
2016-09-15 16:00:46 +01:00
|
|
|
|
limit_days=limit_days,
|
2016-09-23 10:35:31 +01:00
|
|
|
|
include_jobs=include_jobs,
|
2018-07-18 10:54:20 +01:00
|
|
|
|
include_from_test_key=include_from_test_key,
|
|
|
|
|
|
include_one_off=include_one_off
|
2016-09-23 10:35:31 +01:00
|
|
|
|
)
|
2019-01-07 17:12:00 +00:00
|
|
|
|
|
2016-06-28 15:17:36 +01:00
|
|
|
|
kwargs = request.args.to_dict()
|
|
|
|
|
|
kwargs['service_id'] = service_id
|
2018-06-21 17:04:49 +01:00
|
|
|
|
|
|
|
|
|
|
if data.get('format_for_csv'):
|
|
|
|
|
|
notifications = [notification.serialize_for_csv() for notification in pagination.items]
|
|
|
|
|
|
else:
|
|
|
|
|
|
notifications = notification_with_template_schema.dump(pagination.items, many=True).data
|
2016-06-28 15:17:36 +01:00
|
|
|
|
return jsonify(
|
2018-06-21 17:04:49 +01:00
|
|
|
|
notifications=notifications,
|
2016-06-28 15:17:36 +01:00
|
|
|
|
page_size=page_size,
|
|
|
|
|
|
total=pagination.total,
|
|
|
|
|
|
links=pagination_links(
|
|
|
|
|
|
pagination,
|
|
|
|
|
|
'.get_all_notifications_for_service',
|
|
|
|
|
|
**kwargs
|
|
|
|
|
|
)
|
|
|
|
|
|
), 200
|
2016-07-18 12:03:44 +01:00
|
|
|
|
|
|
|
|
|
|
|
2017-06-06 16:21:57 +01:00
|
|
|
|
@service_blueprint.route('/<uuid:service_id>/notifications/<uuid:notification_id>', methods=['GET'])
|
|
|
|
|
|
def get_notification_for_service(service_id, notification_id):
|
|
|
|
|
|
|
|
|
|
|
|
notification = notifications_dao.get_notification_with_personalisation(
|
|
|
|
|
|
service_id,
|
|
|
|
|
|
notification_id,
|
|
|
|
|
|
key_type=None,
|
|
|
|
|
|
)
|
|
|
|
|
|
return jsonify(
|
|
|
|
|
|
notification_with_template_schema.dump(notification).data,
|
|
|
|
|
|
), 200
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-11-22 11:53:32 +00:00
|
|
|
|
@service_blueprint.route('/<uuid:service_id>/notifications/<uuid:notification_id>/cancel', methods=['POST'])
|
|
|
|
|
|
def cancel_notification_for_service(service_id, notification_id):
|
|
|
|
|
|
notification = notifications_dao.get_notification_by_id(notification_id, service_id)
|
|
|
|
|
|
|
|
|
|
|
|
if not notification:
|
|
|
|
|
|
raise InvalidRequest('Notification not found', status_code=404)
|
|
|
|
|
|
elif notification.notification_type != LETTER_TYPE:
|
|
|
|
|
|
raise InvalidRequest('Notification cannot be cancelled - only letters can be cancelled', status_code=400)
|
|
|
|
|
|
elif not letter_can_be_cancelled(notification.status, notification.created_at):
|
|
|
|
|
|
print_day = letter_print_day(notification.created_at)
|
|
|
|
|
|
|
|
|
|
|
|
raise InvalidRequest(
|
|
|
|
|
|
"It’s too late to cancel this letter. Printing started {} at 5.30pm".format(print_day),
|
|
|
|
|
|
status_code=400)
|
|
|
|
|
|
|
|
|
|
|
|
updated_notification = notifications_dao.update_notification_status_by_id(
|
|
|
|
|
|
notification_id,
|
|
|
|
|
|
NOTIFICATION_CANCELLED,
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
return jsonify(
|
|
|
|
|
|
notification_with_template_schema.dump(updated_notification).data
|
|
|
|
|
|
), 200
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-03-07 17:11:29 +00:00
|
|
|
|
def search_for_notification_by_to_field(service_id, search_term, statuses, notification_type):
|
2018-03-07 18:13:40 +00:00
|
|
|
|
results = notifications_dao.dao_get_notifications_by_to_field(
|
|
|
|
|
|
service_id=service_id,
|
|
|
|
|
|
search_term=search_term,
|
|
|
|
|
|
statuses=statuses,
|
|
|
|
|
|
notification_type=notification_type
|
|
|
|
|
|
)
|
2017-05-05 14:12:50 +01:00
|
|
|
|
return jsonify(
|
2017-07-03 12:12:38 +01:00
|
|
|
|
notifications=notification_with_template_schema.dump(results, many=True).data
|
2017-05-24 14:25:38 +01:00
|
|
|
|
), 200
|
2017-05-05 14:12:50 +01:00
|
|
|
|
|
|
|
|
|
|
|
2017-01-30 15:17:26 +00:00
|
|
|
|
@service_blueprint.route('/<uuid:service_id>/notifications/monthly', methods=['GET'])
|
|
|
|
|
|
def get_monthly_notification_stats(service_id):
|
2018-06-28 15:10:14 +01:00
|
|
|
|
# check service_id validity
|
|
|
|
|
|
dao_fetch_service_by_id(service_id)
|
|
|
|
|
|
|
2017-01-30 15:17:26 +00:00
|
|
|
|
try:
|
2018-06-28 15:10:14 +01:00
|
|
|
|
year = int(request.args.get('year', 'NaN'))
|
2017-01-30 15:17:26 +00:00
|
|
|
|
except ValueError:
|
|
|
|
|
|
raise InvalidRequest('Year must be a number', status_code=400)
|
|
|
|
|
|
|
2018-06-28 15:10:14 +01:00
|
|
|
|
start_date, end_date = get_financial_year(year)
|
|
|
|
|
|
|
|
|
|
|
|
data = statistics.create_empty_monthly_notification_status_stats_dict(year)
|
|
|
|
|
|
|
|
|
|
|
|
stats = fetch_notification_status_for_service_by_month(start_date, end_date, service_id)
|
|
|
|
|
|
statistics.add_monthly_notification_status_stats(data, stats)
|
|
|
|
|
|
|
|
|
|
|
|
now = datetime.utcnow()
|
|
|
|
|
|
if end_date > now:
|
|
|
|
|
|
todays_deltas = fetch_notification_status_for_service_for_day(convert_utc_to_bst(now), service_id=service_id)
|
|
|
|
|
|
statistics.add_monthly_notification_status_stats(data, todays_deltas)
|
|
|
|
|
|
|
|
|
|
|
|
return jsonify(data=data)
|
|
|
|
|
|
|
2017-01-30 15:17:26 +00:00
|
|
|
|
|
2016-07-25 14:27:06 +01:00
|
|
|
|
def get_detailed_service(service_id, today_only=False):
|
2016-07-18 12:03:44 +01:00
|
|
|
|
service = dao_fetch_service_by_id(service_id)
|
|
|
|
|
|
|
2018-08-03 14:35:36 +01:00
|
|
|
|
service.statistics = get_service_statistics(service_id, today_only)
|
2016-08-19 17:36:31 +01:00
|
|
|
|
return detailed_service_schema.dump(service).data
|
2016-08-11 17:24:44 +01:00
|
|
|
|
|
|
|
|
|
|
|
2018-08-13 16:34:04 +01:00
|
|
|
|
def get_service_statistics(service_id, today_only, limit_days=7):
|
2018-08-03 14:35:36 +01:00
|
|
|
|
# today_only flag is used by the send page to work out if the service will exceed their daily usage by sending a job
|
2018-08-13 16:34:04 +01:00
|
|
|
|
if today_only:
|
|
|
|
|
|
stats = dao_fetch_todays_stats_for_service(service_id)
|
|
|
|
|
|
else:
|
2018-11-06 13:30:37 +00:00
|
|
|
|
stats = fetch_notification_status_for_service_for_today_and_7_previous_days(service_id, limit_days=limit_days)
|
2018-08-13 16:34:04 +01:00
|
|
|
|
|
2018-08-03 14:35:36 +01:00
|
|
|
|
return statistics.format_statistics(stats)
|
2018-05-09 11:36:42 +01:00
|
|
|
|
|
|
|
|
|
|
|
2017-10-02 12:00:52 +01:00
|
|
|
|
def get_detailed_services(start_date, end_date, only_active=False, include_from_test_key=True):
|
2016-12-29 13:50:41 +00:00
|
|
|
|
if start_date == datetime.utcnow().date():
|
2017-10-26 12:15:52 +01:00
|
|
|
|
stats = dao_fetch_todays_stats_for_all_services(include_from_test_key=include_from_test_key,
|
|
|
|
|
|
only_active=only_active)
|
2016-12-29 13:50:41 +00:00
|
|
|
|
else:
|
2017-01-27 15:57:25 +00:00
|
|
|
|
|
2019-01-04 16:45:39 +00:00
|
|
|
|
stats = fetch_stats_for_all_services_by_date_range(start_date=start_date,
|
2017-03-29 11:47:41 +01:00
|
|
|
|
end_date=end_date,
|
2017-10-26 12:15:52 +01:00
|
|
|
|
include_from_test_key=include_from_test_key,
|
2019-01-04 16:45:39 +00:00
|
|
|
|
)
|
2017-10-26 12:15:52 +01:00
|
|
|
|
results = []
|
2016-08-19 16:36:20 +01:00
|
|
|
|
for service_id, rows in itertools.groupby(stats, lambda x: x.service_id):
|
2017-10-26 12:15:52 +01:00
|
|
|
|
rows = list(rows)
|
2019-01-09 11:43:40 +00:00
|
|
|
|
s = statistics.format_statistics(rows)
|
2017-10-26 12:15:52 +01:00
|
|
|
|
results.append({
|
|
|
|
|
|
'id': str(rows[0].service_id),
|
|
|
|
|
|
'name': rows[0].name,
|
|
|
|
|
|
'notification_type': rows[0].notification_type,
|
|
|
|
|
|
'research_mode': rows[0].research_mode,
|
|
|
|
|
|
'restricted': rows[0].restricted,
|
|
|
|
|
|
'active': rows[0].active,
|
|
|
|
|
|
'created_at': rows[0].created_at,
|
|
|
|
|
|
'statistics': s
|
|
|
|
|
|
})
|
|
|
|
|
|
return results
|
2016-09-20 17:35:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@service_blueprint.route('/<uuid:service_id>/whitelist', methods=['GET'])
|
|
|
|
|
|
def get_whitelist(service_id):
|
2016-09-28 10:16:10 +01:00
|
|
|
|
from app.models import (EMAIL_TYPE, MOBILE_TYPE)
|
2016-09-27 13:47:53 +01:00
|
|
|
|
service = dao_fetch_service_by_id(service_id)
|
|
|
|
|
|
|
|
|
|
|
|
if not service:
|
|
|
|
|
|
raise InvalidRequest("Service does not exist", status_code=404)
|
2016-09-20 17:35:15 +01:00
|
|
|
|
|
2016-09-27 13:47:53 +01:00
|
|
|
|
whitelist = dao_fetch_service_whitelist(service.id)
|
2016-09-23 12:21:00 +01:00
|
|
|
|
return jsonify(
|
2016-09-27 13:47:53 +01:00
|
|
|
|
email_addresses=[item.recipient for item in whitelist
|
|
|
|
|
|
if item.recipient_type == EMAIL_TYPE],
|
|
|
|
|
|
phone_numbers=[item.recipient for item in whitelist
|
|
|
|
|
|
if item.recipient_type == MOBILE_TYPE]
|
2016-09-23 12:21:00 +01:00
|
|
|
|
)
|
2016-09-20 17:35:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
2016-09-23 12:21:00 +01:00
|
|
|
|
@service_blueprint.route('/<uuid:service_id>/whitelist', methods=['PUT'])
|
2016-09-20 17:35:15 +01:00
|
|
|
|
def update_whitelist(service_id):
|
2016-09-23 12:21:00 +01:00
|
|
|
|
# doesn't commit so if there are any errors, we preserve old values in db
|
2016-09-20 17:35:15 +01:00
|
|
|
|
dao_remove_service_whitelist(service_id)
|
2016-09-23 12:21:00 +01:00
|
|
|
|
try:
|
2016-09-28 10:16:10 +01:00
|
|
|
|
whitelist_objs = get_whitelist_objects(service_id, request.get_json())
|
2016-09-23 12:21:00 +01:00
|
|
|
|
except ValueError as e:
|
|
|
|
|
|
current_app.logger.exception(e)
|
|
|
|
|
|
dao_rollback()
|
|
|
|
|
|
msg = '{} is not a valid email address or phone number'.format(str(e))
|
2017-01-31 11:33:36 +00:00
|
|
|
|
raise InvalidRequest(msg, 400)
|
2016-09-23 12:21:00 +01:00
|
|
|
|
else:
|
|
|
|
|
|
dao_add_and_commit_whitelisted_contacts(whitelist_objs)
|
|
|
|
|
|
return '', 204
|
2016-09-30 19:44:13 +01:00
|
|
|
|
|
|
|
|
|
|
|
2017-01-30 16:32:44 +00:00
|
|
|
|
@service_blueprint.route('/<uuid:service_id>/archive', methods=['POST'])
|
|
|
|
|
|
def archive_service(service_id):
|
|
|
|
|
|
"""
|
|
|
|
|
|
When a service is archived the service is made inactive, templates are archived and api keys are revoked.
|
|
|
|
|
|
There is no coming back from this operation.
|
|
|
|
|
|
:param service_id:
|
|
|
|
|
|
:return:
|
|
|
|
|
|
"""
|
|
|
|
|
|
service = dao_fetch_service_by_id(service_id)
|
|
|
|
|
|
|
2017-01-31 11:33:36 +00:00
|
|
|
|
if service.active:
|
|
|
|
|
|
dao_archive_service(service.id)
|
2017-01-30 16:32:44 +00:00
|
|
|
|
|
|
|
|
|
|
return '', 204
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@service_blueprint.route('/<uuid:service_id>/suspend', methods=['POST'])
|
|
|
|
|
|
def suspend_service(service_id):
|
|
|
|
|
|
"""
|
|
|
|
|
|
Suspending a service will mark the service as inactive and revoke API keys.
|
|
|
|
|
|
:param service_id:
|
|
|
|
|
|
:return:
|
|
|
|
|
|
"""
|
|
|
|
|
|
service = dao_fetch_service_by_id(service_id)
|
|
|
|
|
|
|
2017-01-31 11:33:36 +00:00
|
|
|
|
if service.active:
|
|
|
|
|
|
dao_suspend_service(service.id)
|
2017-01-30 16:32:44 +00:00
|
|
|
|
|
|
|
|
|
|
return '', 204
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@service_blueprint.route('/<uuid:service_id>/resume', methods=['POST'])
|
|
|
|
|
|
def resume_service(service_id):
|
|
|
|
|
|
"""
|
|
|
|
|
|
Resuming a service that has been suspended will mark the service as active.
|
|
|
|
|
|
The service will need to re-create API keys
|
|
|
|
|
|
:param service_id:
|
|
|
|
|
|
:return:
|
|
|
|
|
|
"""
|
|
|
|
|
|
service = dao_fetch_service_by_id(service_id)
|
|
|
|
|
|
|
2017-01-31 11:33:36 +00:00
|
|
|
|
if not service.active:
|
|
|
|
|
|
dao_resume_service(service.id)
|
2016-11-01 16:09:55 +00:00
|
|
|
|
|
|
|
|
|
|
return '', 204
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-11-14 14:32:34 +00:00
|
|
|
|
@service_blueprint.route('/<uuid:service_id>/notifications/templates_usage/monthly', methods=['GET'])
|
|
|
|
|
|
def get_monthly_template_usage(service_id):
|
|
|
|
|
|
try:
|
2019-01-10 16:24:51 +00:00
|
|
|
|
start_date, end_date = get_financial_year(int(request.args.get('year', 'NaN')))
|
|
|
|
|
|
data = fetch_monthly_template_usage_for_service(
|
|
|
|
|
|
start_date=start_date,
|
|
|
|
|
|
end_date=end_date,
|
|
|
|
|
|
service_id=service_id
|
2017-11-15 15:55:00 +00:00
|
|
|
|
)
|
|
|
|
|
|
stats = list()
|
|
|
|
|
|
for i in data:
|
|
|
|
|
|
stats.append(
|
|
|
|
|
|
{
|
|
|
|
|
|
'template_id': str(i.template_id),
|
|
|
|
|
|
'name': i.name,
|
2017-11-16 14:41:07 +00:00
|
|
|
|
'type': i.template_type,
|
2017-11-15 15:55:00 +00:00
|
|
|
|
'month': i.month,
|
|
|
|
|
|
'year': i.year,
|
2018-03-05 13:38:41 +00:00
|
|
|
|
'count': i.count,
|
2018-03-07 23:02:38 +00:00
|
|
|
|
'is_precompiled_letter': i.is_precompiled_letter
|
2017-11-15 15:55:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
return jsonify(stats=stats), 200
|
2017-11-14 14:32:34 +00:00
|
|
|
|
except ValueError:
|
|
|
|
|
|
raise InvalidRequest('Year must be a number', status_code=400)
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-06-13 17:33:04 +01:00
|
|
|
|
@service_blueprint.route('/<uuid:service_id>/send-notification', methods=['POST'])
|
2017-06-15 13:40:38 +01:00
|
|
|
|
def create_one_off_notification(service_id):
|
2017-06-13 17:33:04 +01:00
|
|
|
|
resp = send_one_off_notification(service_id, request.get_json())
|
|
|
|
|
|
return jsonify(resp), 201
|
2017-08-09 15:12:52 +01:00
|
|
|
|
|
|
|
|
|
|
|
2019-09-06 09:02:49 +01:00
|
|
|
|
@service_blueprint.route('/<uuid:service_id>/send-pdf-letter', methods=['POST'])
|
|
|
|
|
|
def create_pdf_letter(service_id):
|
2019-10-25 11:27:28 +01:00
|
|
|
|
data = validate(request.get_json(), send_pdf_letter_request)
|
|
|
|
|
|
resp = send_pdf_letter_notification(service_id, data)
|
2019-09-06 09:02:49 +01:00
|
|
|
|
return jsonify(resp), 201
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-09-13 15:27:00 +01:00
|
|
|
|
@service_blueprint.route('/<uuid:service_id>/email-reply-to', methods=["GET"])
|
|
|
|
|
|
def get_email_reply_to_addresses(service_id):
|
|
|
|
|
|
result = dao_get_reply_to_by_service_id(service_id)
|
|
|
|
|
|
return jsonify([i.serialize() for i in result]), 200
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-09-22 15:52:11 +01:00
|
|
|
|
@service_blueprint.route('/<uuid:service_id>/email-reply-to/<uuid:reply_to_id>', methods=["GET"])
|
|
|
|
|
|
def get_email_reply_to_address(service_id, reply_to_id):
|
|
|
|
|
|
result = dao_get_reply_to_by_id(service_id=service_id, reply_to_id=reply_to_id)
|
|
|
|
|
|
return jsonify(result.serialize()), 200
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-05-15 15:23:27 +01:00
|
|
|
|
@service_blueprint.route('/<uuid:service_id>/email-reply-to/verify', methods=['POST'])
|
2019-05-22 16:07:27 +01:00
|
|
|
|
def verify_reply_to_email_address(service_id):
|
2019-05-10 15:32:24 +01:00
|
|
|
|
email_address, errors = email_data_request_schema.load(request.get_json())
|
2019-05-15 15:23:27 +01:00
|
|
|
|
check_if_reply_to_address_already_in_use(service_id, email_address["email"])
|
2019-05-10 15:32:24 +01:00
|
|
|
|
template = dao_get_template_by_id(current_app.config['REPLY_TO_EMAIL_ADDRESS_VERIFICATION_TEMPLATE_ID'])
|
2019-05-15 15:23:27 +01:00
|
|
|
|
notify_service = Service.query.get(current_app.config['NOTIFY_SERVICE_ID'])
|
2019-05-10 15:32:24 +01:00
|
|
|
|
saved_notification = persist_notification(
|
|
|
|
|
|
template_id=template.id,
|
|
|
|
|
|
template_version=template.version,
|
|
|
|
|
|
recipient=email_address["email"],
|
2019-05-15 15:23:27 +01:00
|
|
|
|
service=notify_service,
|
2019-05-10 15:32:24 +01:00
|
|
|
|
personalisation='',
|
|
|
|
|
|
notification_type=template.template_type,
|
|
|
|
|
|
api_key_id=None,
|
|
|
|
|
|
key_type=KEY_TYPE_NORMAL,
|
2019-05-15 15:23:27 +01:00
|
|
|
|
reply_to_text=notify_service.get_default_reply_to_email_address()
|
2019-05-10 15:32:24 +01:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
send_notification_to_queue(saved_notification, False, queue=QueueNames.NOTIFY)
|
|
|
|
|
|
|
|
|
|
|
|
return jsonify(data={"id": saved_notification.id}), 201
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-09-22 15:52:11 +01:00
|
|
|
|
@service_blueprint.route('/<uuid:service_id>/email-reply-to', methods=['POST'])
|
|
|
|
|
|
def add_service_reply_to_email_address(service_id):
|
|
|
|
|
|
# validate the service exists, throws ResultNotFound exception.
|
|
|
|
|
|
dao_fetch_service_by_id(service_id)
|
|
|
|
|
|
form = validate(request.get_json(), add_service_email_reply_to_request)
|
2019-05-15 15:23:27 +01:00
|
|
|
|
check_if_reply_to_address_already_in_use(service_id, form['email_address'])
|
2017-09-22 15:52:11 +01:00
|
|
|
|
new_reply_to = add_reply_to_email_address_for_service(service_id=service_id,
|
|
|
|
|
|
email_address=form['email_address'],
|
|
|
|
|
|
is_default=form.get('is_default', True))
|
|
|
|
|
|
return jsonify(data=new_reply_to.serialize()), 201
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@service_blueprint.route('/<uuid:service_id>/email-reply-to/<uuid:reply_to_email_id>', methods=['POST'])
|
|
|
|
|
|
def update_service_reply_to_email_address(service_id, reply_to_email_id):
|
|
|
|
|
|
# validate the service exists, throws ResultNotFound exception.
|
|
|
|
|
|
dao_fetch_service_by_id(service_id)
|
|
|
|
|
|
form = validate(request.get_json(), add_service_email_reply_to_request)
|
|
|
|
|
|
new_reply_to = update_reply_to_email_address(service_id=service_id,
|
|
|
|
|
|
reply_to_id=reply_to_email_id,
|
|
|
|
|
|
email_address=form['email_address'],
|
|
|
|
|
|
is_default=form.get('is_default', True))
|
|
|
|
|
|
return jsonify(data=new_reply_to.serialize()), 200
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-04-25 16:34:36 +01:00
|
|
|
|
@service_blueprint.route('/<uuid:service_id>/email-reply-to/<uuid:reply_to_email_id>/archive', methods=['POST'])
|
|
|
|
|
|
def delete_service_reply_to_email_address(service_id, reply_to_email_id):
|
|
|
|
|
|
archived_reply_to = archive_reply_to_email_address(service_id, reply_to_email_id)
|
|
|
|
|
|
|
|
|
|
|
|
return jsonify(data=archived_reply_to.serialize()), 200
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-10-04 11:26:27 +01:00
|
|
|
|
@service_blueprint.route('/<uuid:service_id>/letter-contact', methods=["GET"])
|
|
|
|
|
|
def get_letter_contacts(service_id):
|
|
|
|
|
|
result = dao_get_letter_contacts_by_service_id(service_id)
|
|
|
|
|
|
return jsonify([i.serialize() for i in result]), 200
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-10-04 12:26:38 +01:00
|
|
|
|
@service_blueprint.route('/<uuid:service_id>/letter-contact/<uuid:letter_contact_id>', methods=["GET"])
|
|
|
|
|
|
def get_letter_contact_by_id(service_id, letter_contact_id):
|
|
|
|
|
|
result = dao_get_letter_contact_by_id(service_id=service_id, letter_contact_id=letter_contact_id)
|
|
|
|
|
|
return jsonify(result.serialize()), 200
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-10-04 14:41:36 +01:00
|
|
|
|
@service_blueprint.route('/<uuid:service_id>/letter-contact', methods=['POST'])
|
|
|
|
|
|
def add_service_letter_contact(service_id):
|
|
|
|
|
|
# validate the service exists, throws ResultNotFound exception.
|
|
|
|
|
|
dao_fetch_service_by_id(service_id)
|
|
|
|
|
|
form = validate(request.get_json(), add_service_letter_contact_block_request)
|
|
|
|
|
|
new_letter_contact = add_letter_contact_for_service(service_id=service_id,
|
|
|
|
|
|
contact_block=form['contact_block'],
|
|
|
|
|
|
is_default=form.get('is_default', True))
|
|
|
|
|
|
return jsonify(data=new_letter_contact.serialize()), 201
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-10-04 15:35:41 +01:00
|
|
|
|
@service_blueprint.route('/<uuid:service_id>/letter-contact/<uuid:letter_contact_id>', methods=['POST'])
|
|
|
|
|
|
def update_service_letter_contact(service_id, letter_contact_id):
|
|
|
|
|
|
# validate the service exists, throws ResultNotFound exception.
|
|
|
|
|
|
dao_fetch_service_by_id(service_id)
|
|
|
|
|
|
form = validate(request.get_json(), add_service_letter_contact_block_request)
|
|
|
|
|
|
new_reply_to = update_letter_contact(service_id=service_id,
|
|
|
|
|
|
letter_contact_id=letter_contact_id,
|
|
|
|
|
|
contact_block=form['contact_block'],
|
|
|
|
|
|
is_default=form.get('is_default', True))
|
|
|
|
|
|
return jsonify(data=new_reply_to.serialize()), 200
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-04-26 10:00:11 +01:00
|
|
|
|
@service_blueprint.route('/<uuid:service_id>/letter-contact/<uuid:letter_contact_id>/archive', methods=['POST'])
|
|
|
|
|
|
def delete_service_letter_contact(service_id, letter_contact_id):
|
|
|
|
|
|
archived_letter_contact = archive_letter_contact(service_id, letter_contact_id)
|
|
|
|
|
|
|
|
|
|
|
|
return jsonify(data=archived_letter_contact.serialize()), 200
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-10-19 09:58:23 +01:00
|
|
|
|
@service_blueprint.route('/<uuid:service_id>/sms-sender', methods=['POST'])
|
|
|
|
|
|
def add_service_sms_sender(service_id):
|
|
|
|
|
|
dao_fetch_service_by_id(service_id)
|
|
|
|
|
|
form = validate(request.get_json(), add_service_sms_sender_request)
|
|
|
|
|
|
inbound_number_id = form.get('inbound_number_id', None)
|
2017-10-19 16:29:54 +01:00
|
|
|
|
sms_sender = form.get('sms_sender')
|
2017-10-25 11:58:54 +01:00
|
|
|
|
|
2017-10-19 09:58:23 +01:00
|
|
|
|
if inbound_number_id:
|
2017-10-19 16:29:54 +01:00
|
|
|
|
updated_number = dao_allocate_number_for_service(service_id=service_id, inbound_number_id=inbound_number_id)
|
2017-10-25 11:58:54 +01:00
|
|
|
|
# the sms_sender in the form is not set, use the inbound number
|
2017-10-19 16:29:54 +01:00
|
|
|
|
sms_sender = updated_number.number
|
2017-10-25 11:58:54 +01:00
|
|
|
|
existing_sms_sender = dao_get_sms_senders_by_service_id(service_id)
|
|
|
|
|
|
# we don't want to create a new sms sender for the service if we are allocating an inbound number.
|
|
|
|
|
|
if len(existing_sms_sender) == 1:
|
|
|
|
|
|
update_existing_sms_sender = existing_sms_sender[0]
|
|
|
|
|
|
new_sms_sender = update_existing_sms_sender_with_inbound_number(
|
|
|
|
|
|
service_sms_sender=update_existing_sms_sender,
|
|
|
|
|
|
sms_sender=sms_sender,
|
|
|
|
|
|
inbound_number_id=inbound_number_id)
|
|
|
|
|
|
|
|
|
|
|
|
return jsonify(new_sms_sender.serialize()), 201
|
|
|
|
|
|
|
2017-10-19 09:58:23 +01:00
|
|
|
|
new_sms_sender = dao_add_sms_sender_for_service(service_id=service_id,
|
2017-10-19 16:29:54 +01:00
|
|
|
|
sms_sender=sms_sender,
|
2017-10-19 09:58:23 +01:00
|
|
|
|
is_default=form['is_default'],
|
|
|
|
|
|
inbound_number_id=inbound_number_id
|
|
|
|
|
|
)
|
2017-10-19 13:59:22 +01:00
|
|
|
|
return jsonify(new_sms_sender.serialize()), 201
|
2017-10-19 09:58:23 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@service_blueprint.route('/<uuid:service_id>/sms-sender/<uuid:sms_sender_id>', methods=['POST'])
|
|
|
|
|
|
def update_service_sms_sender(service_id, sms_sender_id):
|
|
|
|
|
|
form = validate(request.get_json(), add_service_sms_sender_request)
|
|
|
|
|
|
|
|
|
|
|
|
sms_sender_to_update = dao_get_service_sms_senders_by_id(service_id=service_id,
|
|
|
|
|
|
service_sms_sender_id=sms_sender_id)
|
|
|
|
|
|
if sms_sender_to_update.inbound_number_id and form['sms_sender'] != sms_sender_to_update.sms_sender:
|
|
|
|
|
|
raise InvalidRequest("You can not change the inbound number for service {}".format(service_id),
|
|
|
|
|
|
status_code=400)
|
|
|
|
|
|
|
|
|
|
|
|
new_sms_sender = dao_update_service_sms_sender(service_id=service_id,
|
|
|
|
|
|
service_sms_sender_id=sms_sender_id,
|
|
|
|
|
|
is_default=form['is_default'],
|
|
|
|
|
|
sms_sender=form['sms_sender']
|
|
|
|
|
|
)
|
2017-10-19 13:59:22 +01:00
|
|
|
|
return jsonify(new_sms_sender.serialize()), 200
|
2017-10-19 09:58:23 +01:00
|
|
|
|
|
|
|
|
|
|
|
2018-04-26 09:05:17 +01:00
|
|
|
|
@service_blueprint.route('/<uuid:service_id>/sms-sender/<uuid:sms_sender_id>/archive', methods=['POST'])
|
|
|
|
|
|
def delete_service_sms_sender(service_id, sms_sender_id):
|
|
|
|
|
|
sms_sender = archive_sms_sender(service_id, sms_sender_id)
|
|
|
|
|
|
|
|
|
|
|
|
return jsonify(data=sms_sender.serialize()), 200
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-10-19 10:43:49 +01:00
|
|
|
|
@service_blueprint.route('/<uuid:service_id>/sms-sender/<uuid:sms_sender_id>', methods=['GET'])
|
|
|
|
|
|
def get_service_sms_sender_by_id(service_id, sms_sender_id):
|
|
|
|
|
|
sms_sender = dao_get_service_sms_senders_by_id(service_id=service_id,
|
|
|
|
|
|
service_sms_sender_id=sms_sender_id)
|
2017-10-19 13:59:22 +01:00
|
|
|
|
return jsonify(sms_sender.serialize()), 200
|
2017-10-19 10:43:49 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@service_blueprint.route('/<uuid:service_id>/sms-sender', methods=['GET'])
|
|
|
|
|
|
def get_service_sms_senders_for_service(service_id):
|
|
|
|
|
|
sms_senders = dao_get_sms_senders_by_service_id(service_id=service_id)
|
2017-10-19 13:59:22 +01:00
|
|
|
|
return jsonify([sms_sender.serialize() for sms_sender in sms_senders]), 200
|
2017-10-19 10:43:49 +01:00
|
|
|
|
|
|
|
|
|
|
|
2018-02-10 01:37:17 +00:00
|
|
|
|
@service_blueprint.route('/<uuid:service_id>/organisation', methods=['GET'])
|
|
|
|
|
|
def get_organisation_for_service(service_id):
|
|
|
|
|
|
organisation = dao_get_organisation_by_service_id(service_id=service_id)
|
|
|
|
|
|
return jsonify(organisation.serialize() if organisation else {}), 200
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-08-09 15:12:52 +01:00
|
|
|
|
@service_blueprint.route('/unique', methods=["GET"])
|
|
|
|
|
|
def is_service_name_unique():
|
2018-02-13 15:25:24 +00:00
|
|
|
|
service_id, name, email_from = check_request_args(request)
|
2017-08-09 15:12:52 +01:00
|
|
|
|
|
|
|
|
|
|
name_exists = Service.query.filter_by(name=name).first()
|
2018-02-13 15:25:24 +00:00
|
|
|
|
|
2018-02-14 14:47:03 +00:00
|
|
|
|
email_from_exists = Service.query.filter(
|
|
|
|
|
|
Service.email_from == email_from,
|
|
|
|
|
|
Service.id != service_id
|
|
|
|
|
|
).first()
|
2018-02-13 15:25:24 +00:00
|
|
|
|
|
2017-08-09 15:42:40 +01:00
|
|
|
|
result = not (name_exists or email_from_exists)
|
|
|
|
|
|
return jsonify(result=result), 200
|
2017-08-09 15:12:52 +01:00
|
|
|
|
|
|
|
|
|
|
|
2018-07-13 15:18:27 +01:00
|
|
|
|
@service_blueprint.route('/<uuid:service_id>/data-retention', methods=['GET'])
|
|
|
|
|
|
def get_data_retention_for_service(service_id):
|
|
|
|
|
|
data_retention_list = fetch_service_data_retention(service_id)
|
|
|
|
|
|
return jsonify([data_retention.serialize() for data_retention in data_retention_list]), 200
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-08-13 16:44:24 +01:00
|
|
|
|
@service_blueprint.route('/<uuid:service_id>/data-retention/notification-type/<notification_type>', methods=['GET'])
|
|
|
|
|
|
def get_data_retention_for_service_notification_type(service_id, notification_type):
|
|
|
|
|
|
data_retention = fetch_service_data_retention_by_notification_type(service_id, notification_type)
|
|
|
|
|
|
return jsonify(data_retention.serialize() if data_retention else {}), 200
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-07-13 15:18:27 +01:00
|
|
|
|
@service_blueprint.route('/<uuid:service_id>/data-retention/<uuid:data_retention_id>', methods=['GET'])
|
|
|
|
|
|
def get_data_retention_for_service_by_id(service_id, data_retention_id):
|
|
|
|
|
|
data_retention = fetch_service_data_retention_by_id(service_id, data_retention_id)
|
|
|
|
|
|
return jsonify(data_retention.serialize() if data_retention else {}), 200
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-07-11 17:02:49 +01:00
|
|
|
|
@service_blueprint.route('/<uuid:service_id>/data-retention', methods=['POST'])
|
|
|
|
|
|
def create_service_data_retention(service_id):
|
|
|
|
|
|
form = validate(request.get_json(), add_service_data_retention_request)
|
|
|
|
|
|
try:
|
|
|
|
|
|
new_data_retention = insert_service_data_retention(
|
|
|
|
|
|
service_id=service_id,
|
|
|
|
|
|
notification_type=form.get("notification_type"),
|
|
|
|
|
|
days_of_retention=form.get("days_of_retention")
|
|
|
|
|
|
)
|
|
|
|
|
|
except IntegrityError:
|
|
|
|
|
|
raise InvalidRequest(
|
|
|
|
|
|
message="Service already has data retention for {} notification type".format(form.get("notification_type")),
|
|
|
|
|
|
status_code=400
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
return jsonify(result=new_data_retention.serialize()), 201
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@service_blueprint.route('/<uuid:service_id>/data-retention/<uuid:data_retention_id>', methods=['POST'])
|
|
|
|
|
|
def modify_service_data_retention(service_id, data_retention_id):
|
|
|
|
|
|
form = validate(request.get_json(), update_service_data_retention_request)
|
|
|
|
|
|
|
|
|
|
|
|
update_count = update_service_data_retention(
|
|
|
|
|
|
service_data_retention_id=data_retention_id,
|
|
|
|
|
|
service_id=service_id,
|
|
|
|
|
|
days_of_retention=form.get("days_of_retention")
|
|
|
|
|
|
)
|
|
|
|
|
|
if update_count == 0:
|
|
|
|
|
|
raise InvalidRequest(
|
|
|
|
|
|
message="The service data retention for id: {} was not found for service: {}".format(data_retention_id,
|
|
|
|
|
|
service_id),
|
|
|
|
|
|
status_code=404)
|
|
|
|
|
|
|
|
|
|
|
|
return '', 204
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-07-19 10:57:14 +01:00
|
|
|
|
@service_blueprint.route('/monthly-data-by-service')
|
|
|
|
|
|
def get_monthly_notification_data_by_service():
|
|
|
|
|
|
start_date = request.args.get('start_date')
|
|
|
|
|
|
end_date = request.args.get('end_date')
|
|
|
|
|
|
|
|
|
|
|
|
result = fact_notification_status_dao.fetch_monthly_notification_statuses_per_service(start_date, end_date)
|
|
|
|
|
|
|
|
|
|
|
|
return jsonify(result)
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-08-09 15:12:52 +01:00
|
|
|
|
def check_request_args(request):
|
2018-02-13 15:25:24 +00:00
|
|
|
|
service_id = request.args.get('service_id')
|
2017-08-09 15:12:52 +01:00
|
|
|
|
name = request.args.get('name', None)
|
|
|
|
|
|
email_from = request.args.get('email_from', None)
|
|
|
|
|
|
errors = []
|
2018-02-14 14:47:03 +00:00
|
|
|
|
if not service_id:
|
|
|
|
|
|
errors.append({'service_id': ["Can't be empty"]})
|
2017-08-09 15:12:52 +01:00
|
|
|
|
if not name:
|
|
|
|
|
|
errors.append({'name': ["Can't be empty"]})
|
|
|
|
|
|
if not email_from:
|
|
|
|
|
|
errors.append({'email_from': ["Can't be empty"]})
|
|
|
|
|
|
if errors:
|
|
|
|
|
|
raise InvalidRequest(errors, status_code=400)
|
2018-02-13 15:25:24 +00:00
|
|
|
|
return service_id, name, email_from
|
2019-05-15 15:23:27 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def check_if_reply_to_address_already_in_use(service_id, email_address):
|
|
|
|
|
|
existing_reply_to_addresses = dao_get_reply_to_by_service_id(service_id)
|
|
|
|
|
|
if email_address in [i.email_address for i in existing_reply_to_addresses]:
|
|
|
|
|
|
raise InvalidRequest(
|
2019-05-22 16:07:27 +01:00
|
|
|
|
"Your service already uses ‘{}’ as an email reply-to address.".format(email_address), status_code=400
|
2019-05-15 15:23:27 +01:00
|
|
|
|
)
|