2020-02-24 17:02:25 +00:00
|
|
|
|
2021-03-10 13:55:06 +00:00
|
|
|
from flask import Blueprint, abort, current_app, jsonify, request
|
2021-04-13 15:02:46 +01:00
|
|
|
from sqlalchemy.exc import IntegrityError
|
2018-02-08 14:54:08 +00:00
|
|
|
|
2019-07-09 11:59:33 +01:00
|
|
|
from app.config import QueueNames
|
2021-04-06 13:42:18 +01:00
|
|
|
from app.dao.annual_billing_dao import set_default_free_allowance_for_service
|
2021-04-13 15:02:46 +01:00
|
|
|
from app.dao.dao_utils import nested_transaction
|
2020-02-24 17:02:25 +00:00
|
|
|
from app.dao.fact_billing_dao import fetch_usage_year_for_organisation
|
2018-02-08 14:54:08 +00:00
|
|
|
from app.dao.organisation_dao import (
|
2021-03-10 13:55:06 +00:00
|
|
|
dao_add_service_to_organisation,
|
|
|
|
|
dao_add_user_to_organisation,
|
2018-02-08 14:54:08 +00:00
|
|
|
dao_create_organisation,
|
2019-04-04 10:13:19 +01:00
|
|
|
dao_get_organisation_by_email_address,
|
2021-03-10 13:55:06 +00:00
|
|
|
dao_get_organisation_by_id,
|
2018-02-10 01:37:17 +00:00
|
|
|
dao_get_organisation_services,
|
2021-03-10 13:55:06 +00:00
|
|
|
dao_get_organisations,
|
2018-02-20 17:09:16 +00:00
|
|
|
dao_get_users_for_organisation,
|
2021-03-10 13:55:06 +00:00
|
|
|
dao_update_organisation,
|
2018-02-08 14:54:08 +00:00
|
|
|
)
|
2018-02-10 01:37:17 +00:00
|
|
|
from app.dao.services_dao import dao_fetch_service_by_id
|
2021-03-10 13:55:06 +00:00
|
|
|
from app.dao.templates_dao import dao_get_template_by_id
|
|
|
|
|
from app.errors import InvalidRequest, register_errors
|
|
|
|
|
from app.models import KEY_TYPE_NORMAL, Organisation
|
|
|
|
|
from app.notifications.process_notifications import (
|
|
|
|
|
persist_notification,
|
|
|
|
|
send_notification_to_queue,
|
|
|
|
|
)
|
2018-02-08 14:54:08 +00:00
|
|
|
from app.organisation.organisation_schema import (
|
|
|
|
|
post_create_organisation_schema,
|
2018-02-10 01:37:17 +00:00
|
|
|
post_link_service_to_organisation_schema,
|
2021-03-10 13:55:06 +00:00
|
|
|
post_update_organisation_schema,
|
2018-02-08 14:54:08 +00:00
|
|
|
)
|
|
|
|
|
from app.schema_validation import validate
|
|
|
|
|
|
|
|
|
|
organisation_blueprint = Blueprint('organisation', __name__)
|
|
|
|
|
register_errors(organisation_blueprint)
|
|
|
|
|
|
|
|
|
|
|
2018-02-13 14:47:03 +00:00
|
|
|
@organisation_blueprint.errorhandler(IntegrityError)
|
|
|
|
|
def handle_integrity_error(exc):
|
|
|
|
|
"""
|
2018-02-19 14:33:44 +00:00
|
|
|
Handle integrity errors caused by the unique constraint on ix_organisation_name
|
2018-02-13 14:47:03 +00:00
|
|
|
"""
|
|
|
|
|
if 'ix_organisation_name' in str(exc):
|
2018-02-19 14:33:44 +00:00
|
|
|
return jsonify(result="error",
|
|
|
|
|
message="Organisation name already exists"), 400
|
2019-04-12 10:55:21 +01:00
|
|
|
if 'duplicate key value violates unique constraint "domain_pkey"' in str(exc):
|
2019-04-10 13:23:34 +01:00
|
|
|
return jsonify(result='error',
|
|
|
|
|
message='Domain already exists'), 400
|
2018-02-13 14:47:03 +00:00
|
|
|
|
2018-02-19 14:33:44 +00:00
|
|
|
current_app.logger.exception(exc)
|
|
|
|
|
return jsonify(result='error', message="Internal server error"), 500
|
2018-02-13 14:47:03 +00:00
|
|
|
|
|
|
|
|
|
2018-02-08 14:54:08 +00:00
|
|
|
@organisation_blueprint.route('', methods=['GET'])
|
|
|
|
|
def get_organisations():
|
|
|
|
|
organisations = [
|
2019-06-13 15:54:57 +01:00
|
|
|
org.serialize_for_list() for org in dao_get_organisations()
|
2018-02-08 14:54:08 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
return jsonify(organisations)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@organisation_blueprint.route('/<uuid:organisation_id>', methods=['GET'])
|
|
|
|
|
def get_organisation_by_id(organisation_id):
|
|
|
|
|
organisation = dao_get_organisation_by_id(organisation_id)
|
|
|
|
|
return jsonify(organisation.serialize())
|
|
|
|
|
|
|
|
|
|
|
2019-04-04 10:13:19 +01:00
|
|
|
@organisation_blueprint.route('/by-domain', methods=['GET'])
|
|
|
|
|
def get_organisation_by_domain():
|
|
|
|
|
|
|
|
|
|
domain = request.args.get('domain')
|
|
|
|
|
|
|
|
|
|
if not domain or '@' in domain:
|
|
|
|
|
abort(400)
|
|
|
|
|
|
|
|
|
|
organisation = dao_get_organisation_by_email_address(
|
|
|
|
|
'example@{}'.format(request.args.get('domain'))
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if not organisation:
|
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
|
|
return jsonify(organisation.serialize())
|
|
|
|
|
|
|
|
|
|
|
2018-02-08 14:54:08 +00:00
|
|
|
@organisation_blueprint.route('', methods=['POST'])
|
|
|
|
|
def create_organisation():
|
|
|
|
|
data = request.get_json()
|
|
|
|
|
|
|
|
|
|
validate(data, post_create_organisation_schema)
|
|
|
|
|
|
|
|
|
|
organisation = Organisation(**data)
|
|
|
|
|
dao_create_organisation(organisation)
|
|
|
|
|
return jsonify(organisation.serialize()), 201
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@organisation_blueprint.route('/<uuid:organisation_id>', methods=['POST'])
|
|
|
|
|
def update_organisation(organisation_id):
|
|
|
|
|
data = request.get_json()
|
|
|
|
|
validate(data, post_update_organisation_schema)
|
2018-02-09 11:17:13 +00:00
|
|
|
result = dao_update_organisation(organisation_id, **data)
|
2019-07-09 11:59:33 +01:00
|
|
|
|
|
|
|
|
if data.get('agreement_signed') is True:
|
2019-07-04 15:12:08 +01:00
|
|
|
# if a platform admin has manually adjusted the organisation, don't tell people
|
|
|
|
|
if data.get('agreement_signed_by_id'):
|
|
|
|
|
send_notifications_on_mou_signed(organisation_id)
|
2019-07-09 11:59:33 +01:00
|
|
|
|
2018-02-09 11:17:13 +00:00
|
|
|
if result:
|
|
|
|
|
return '', 204
|
|
|
|
|
else:
|
|
|
|
|
raise InvalidRequest("Organisation not found", 404)
|
2018-02-10 01:37:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@organisation_blueprint.route('/<uuid:organisation_id>/service', methods=['POST'])
|
|
|
|
|
def link_service_to_organisation(organisation_id):
|
|
|
|
|
data = request.get_json()
|
|
|
|
|
validate(data, post_link_service_to_organisation_schema)
|
|
|
|
|
service = dao_fetch_service_by_id(data['service_id'])
|
|
|
|
|
service.organisation = None
|
|
|
|
|
|
2021-04-13 15:02:46 +01:00
|
|
|
with nested_transaction():
|
2021-04-12 13:52:40 +01:00
|
|
|
dao_add_service_to_organisation(service, organisation_id)
|
2021-04-06 13:42:18 +01:00
|
|
|
set_default_free_allowance_for_service(service, year_start=None)
|
2018-02-10 01:37:17 +00:00
|
|
|
|
|
|
|
|
return '', 204
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@organisation_blueprint.route('/<uuid:organisation_id>/services', methods=['GET'])
|
|
|
|
|
def get_organisation_services(organisation_id):
|
|
|
|
|
services = dao_get_organisation_services(organisation_id)
|
2018-02-13 09:28:48 +00:00
|
|
|
sorted_services = sorted(services, key=lambda s: (-s.active, s.name))
|
|
|
|
|
return jsonify([s.serialize_for_org_dashboard() for s in sorted_services])
|
2018-02-20 17:09:16 +00:00
|
|
|
|
|
|
|
|
|
2020-02-17 16:34:17 +00:00
|
|
|
@organisation_blueprint.route('/<uuid:organisation_id>/services-with-usage', methods=['GET'])
|
|
|
|
|
def get_organisation_services_usage(organisation_id):
|
2020-02-25 17:34:03 +00:00
|
|
|
try:
|
2020-02-26 17:38:20 +00:00
|
|
|
year = int(request.args.get('year', 'none'))
|
2020-02-25 17:34:03 +00:00
|
|
|
except ValueError:
|
|
|
|
|
return jsonify(result='error', message='No valid year provided'), 400
|
|
|
|
|
services = fetch_usage_year_for_organisation(organisation_id, year)
|
|
|
|
|
list_services = services.values()
|
2021-01-12 09:44:35 +00:00
|
|
|
sorted_services = sorted(list_services, key=lambda s: (-s['active'], s['service_name'].lower()))
|
2020-02-25 17:34:03 +00:00
|
|
|
return jsonify(services=sorted_services)
|
2020-02-17 16:34:17 +00:00
|
|
|
|
|
|
|
|
|
2018-02-20 17:09:16 +00:00
|
|
|
@organisation_blueprint.route('/<uuid:organisation_id>/users/<uuid:user_id>', methods=['POST'])
|
|
|
|
|
def add_user_to_organisation(organisation_id, user_id):
|
|
|
|
|
new_org_user = dao_add_user_to_organisation(organisation_id, user_id)
|
2018-03-06 17:47:29 +00:00
|
|
|
return jsonify(data=new_org_user.serialize())
|
2018-02-20 17:09:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@organisation_blueprint.route('/<uuid:organisation_id>/users', methods=['GET'])
|
|
|
|
|
def get_organisation_users(organisation_id):
|
|
|
|
|
org_users = dao_get_users_for_organisation(organisation_id)
|
2018-03-06 17:47:29 +00:00
|
|
|
return jsonify(data=[x.serialize() for x in org_users])
|
2018-03-06 12:49:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@organisation_blueprint.route('/unique', methods=["GET"])
|
|
|
|
|
def is_organisation_name_unique():
|
|
|
|
|
organisation_id, name = check_request_args(request)
|
|
|
|
|
|
|
|
|
|
name_exists = Organisation.query.filter(Organisation.name.ilike(name)).first()
|
|
|
|
|
|
|
|
|
|
result = (not name_exists) or str(name_exists.id) == organisation_id
|
|
|
|
|
return jsonify(result=result), 200
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def check_request_args(request):
|
|
|
|
|
org_id = request.args.get('org_id')
|
|
|
|
|
name = request.args.get('name', None)
|
|
|
|
|
errors = []
|
|
|
|
|
if not org_id:
|
|
|
|
|
errors.append({'org_id': ["Can't be empty"]})
|
|
|
|
|
if not name:
|
|
|
|
|
errors.append({'name': ["Can't be empty"]})
|
|
|
|
|
if errors:
|
|
|
|
|
raise InvalidRequest(errors, status_code=400)
|
|
|
|
|
return org_id, name
|
2019-07-09 11:59:33 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def send_notifications_on_mou_signed(organisation_id):
|
2019-07-04 15:12:08 +01:00
|
|
|
organisation = dao_get_organisation_by_id(organisation_id)
|
2019-07-09 11:59:33 +01:00
|
|
|
notify_service = dao_fetch_service_by_id(current_app.config['NOTIFY_SERVICE_ID'])
|
|
|
|
|
|
|
|
|
|
def _send_notification(template_id, recipient, personalisation):
|
|
|
|
|
template = dao_get_template_by_id(template_id)
|
|
|
|
|
|
|
|
|
|
saved_notification = persist_notification(
|
|
|
|
|
template_id=template.id,
|
|
|
|
|
template_version=template.version,
|
|
|
|
|
recipient=recipient,
|
|
|
|
|
service=notify_service,
|
|
|
|
|
personalisation=personalisation,
|
|
|
|
|
notification_type=template.template_type,
|
|
|
|
|
api_key_id=None,
|
|
|
|
|
key_type=KEY_TYPE_NORMAL,
|
|
|
|
|
reply_to_text=notify_service.get_default_reply_to_email_address()
|
|
|
|
|
)
|
|
|
|
|
send_notification_to_queue(saved_notification, research_mode=False, queue=QueueNames.NOTIFY)
|
|
|
|
|
|
|
|
|
|
personalisation = {
|
|
|
|
|
'mou_link': '{}/agreement/{}.pdf'.format(
|
|
|
|
|
current_app.config['ADMIN_BASE_URL'],
|
|
|
|
|
'crown' if organisation.crown else 'non-crown'
|
|
|
|
|
),
|
|
|
|
|
'org_name': organisation.name,
|
|
|
|
|
'org_dashboard_link': '{}/organisations/{}'.format(
|
|
|
|
|
current_app.config['ADMIN_BASE_URL'],
|
|
|
|
|
organisation.id
|
|
|
|
|
),
|
|
|
|
|
'signed_by_name': organisation.agreement_signed_by.name,
|
|
|
|
|
'on_behalf_of_name': organisation.agreement_signed_on_behalf_of_name
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if not organisation.agreement_signed_on_behalf_of_email_address:
|
|
|
|
|
signer_template_id = 'MOU_SIGNER_RECEIPT_TEMPLATE_ID'
|
|
|
|
|
else:
|
|
|
|
|
signer_template_id = 'MOU_SIGNED_ON_BEHALF_SIGNER_RECEIPT_TEMPLATE_ID'
|
|
|
|
|
|
|
|
|
|
# let the person who has been signed on behalf of know.
|
|
|
|
|
_send_notification(
|
|
|
|
|
current_app.config['MOU_SIGNED_ON_BEHALF_ON_BEHALF_RECEIPT_TEMPLATE_ID'],
|
|
|
|
|
organisation.agreement_signed_on_behalf_of_email_address,
|
|
|
|
|
personalisation
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# let the person who signed know - the template is different depending on if they signed on behalf of someone
|
|
|
|
|
_send_notification(
|
|
|
|
|
current_app.config[signer_template_id],
|
|
|
|
|
organisation.agreement_signed_by.email_address,
|
|
|
|
|
personalisation
|
|
|
|
|
)
|