From 07bb0f0332e1ff271f7db889a801bb8eaa01f919 Mon Sep 17 00:00:00 2001 From: Leo Hemsted Date: Tue, 9 Jul 2019 11:59:33 +0100 Subject: [PATCH] send emails when MOU is signed we build up one personalisation dict, and then pass it in to all the different templates - so be careful editing things. also of note, we check if the agreement_signed_on_behalf_of is set, and send a different template with slightly different wording to the person who clicked the confirm button. --- app/config.py | 4 +++ app/models.py | 1 + app/organisation/rest.py | 73 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 77 insertions(+), 1 deletion(-) diff --git a/app/config.py b/app/config.py index 2c5ee54ae..0b074e8c6 100644 --- a/app/config.py +++ b/app/config.py @@ -151,6 +151,10 @@ class Config(object): TEAM_MEMBER_EDIT_EMAIL_TEMPLATE_ID = 'c73f1d71-4049-46d5-a647-d013bdeca3f0' TEAM_MEMBER_EDIT_MOBILE_TEMPLATE_ID = '8a31520f-4751-4789-8ea1-fe54496725eb' REPLY_TO_EMAIL_ADDRESS_VERIFICATION_TEMPLATE_ID = 'a42f1d17-9404-46d5-a647-d013bdfca3e1' + MOU_SIGNER_RECEIPT_TEMPLATE_ID = '4fd2e43c-309b-4e50-8fb8-1955852d9d71' + MOU_SIGNED_ON_BEHALF_SIGNER_RECEIPT_TEMPLATE_ID = 'c20206d5-bf03-4002-9a90-37d5032d9e84' + MOU_SIGNED_ON_BEHALF_ON_BEHALF_RECEIPT_TEMPLATE_ID = '522b6657-5ca5-4368-a294-6b527703bd0b' + MOU_NOTIFY_TEAM_ALERT_TEMPLATE_ID = 'd0e66c4c-0c50-43f0-94f5-f85b613202d4' BROKER_URL = 'sqs://' BROKER_TRANSPORT_OPTIONS = { diff --git a/app/models.py b/app/models.py index a46c51980..3db41ff6d 100644 --- a/app/models.py +++ b/app/models.py @@ -346,6 +346,7 @@ class Organisation(db.Model): db.ForeignKey('users.id'), nullable=True, ) + agreement_signed_by = db.relationship('User') agreement_signed_on_behalf_of_name = db.Column(db.String(255), nullable=True) agreement_signed_on_behalf_of_email_address = db.Column(db.String(255), nullable=True) agreement_signed_version = db.Column(db.Float, nullable=True) diff --git a/app/organisation/rest.py b/app/organisation/rest.py index 6296e63dd..fd63698cc 100644 --- a/app/organisation/rest.py +++ b/app/organisation/rest.py @@ -1,6 +1,7 @@ from flask import abort, Blueprint, jsonify, request, current_app from sqlalchemy.exc import IntegrityError +from app.config import QueueNames from app.dao.organisation_dao import ( dao_create_organisation, dao_get_organisations, @@ -12,9 +13,11 @@ from app.dao.organisation_dao import ( dao_get_users_for_organisation, dao_add_user_to_organisation ) +from app.dao.templates_dao import dao_get_template_by_id from app.dao.services_dao import dao_fetch_service_by_id from app.errors import register_errors, InvalidRequest -from app.models import Organisation +from app.models import Organisation, KEY_TYPE_NORMAL +from app.notifications.process_notifications import persist_notification, send_notification_to_queue from app.organisation.organisation_schema import ( post_create_organisation_schema, post_update_organisation_schema, @@ -91,6 +94,10 @@ def update_organisation(organisation_id): data = request.get_json() validate(data, post_update_organisation_schema) result = dao_update_organisation(organisation_id, **data) + + if data.get('agreement_signed') is True: + send_notifications_on_mou_signed(organisation_id) + if result: return '', 204 else: @@ -149,3 +156,67 @@ def check_request_args(request): if errors: raise InvalidRequest(errors, status_code=400) return org_id, name + + +def send_notifications_on_mou_signed(organisation_id): + 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) + + organisation = dao_get_organisation_by_id(organisation_id) + 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 + } + + # let notify team know something's happened + _send_notification( + current_app.config['MOU_NOTIFY_TEAM_ALERT_TEMPLATE_ID'], + + # TODO: decide should we do this to distinguish emails from different envs? + 'notify-support+{}@digital.cabinet-office.gov.uk'.format(current_app.config['NOTIFY_ENVIRONMENT']), + personalisation + ) + + 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 + )