mirror of
https://github.com/GSA/notifications-api.git
synced 2026-01-31 15:15:38 -05:00
Merge pull request #951 from alphagov/imdad-feat-auto-send-email-live-service
Send a welcome email to newly live services
This commit is contained in:
@@ -89,6 +89,7 @@ class Config(object):
|
||||
PASSWORD_RESET_TEMPLATE_ID = '474e9242-823b-4f99-813d-ed392e7f1201'
|
||||
ALREADY_REGISTERED_EMAIL_TEMPLATE_ID = '0880fbb1-a0c6-46f0-9a8e-36c986381ceb'
|
||||
CHANGE_EMAIL_CONFIRMATION_TEMPLATE_ID = 'eb4d9930-87ab-4aef-9bce-786762687884'
|
||||
SERVICE_NOW_LIVE_TEMPLATE_ID = '618185c6-3636-49cd-b7d2-6f6f5eb3bdde'
|
||||
|
||||
BROKER_URL = 'sqs://'
|
||||
BROKER_TRANSPORT_OPTIONS = {
|
||||
|
||||
@@ -384,3 +384,12 @@ def dao_suspend_service(service_id):
|
||||
def dao_resume_service(service_id):
|
||||
service = Service.query.get(service_id)
|
||||
service.active = True
|
||||
|
||||
|
||||
def dao_fetch_active_users_for_service(service_id):
|
||||
query = User.query.filter(
|
||||
User.user_to_service.any(id=service_id),
|
||||
User.state == 'active'
|
||||
)
|
||||
|
||||
return query.all()
|
||||
|
||||
@@ -46,6 +46,7 @@ from app.errors import (
|
||||
InvalidRequest, register_errors)
|
||||
from app.service import statistics
|
||||
from app.service.utils import get_whitelist_objects
|
||||
from app.service.sender import send_notification_to_service_users
|
||||
from app.schemas import (
|
||||
service_schema,
|
||||
api_key_schema,
|
||||
@@ -117,10 +118,25 @@ def create_service():
|
||||
@service_blueprint.route('/<uuid:service_id>', methods=['POST'])
|
||||
def update_service(service_id):
|
||||
fetched_service = dao_fetch_service_by_id(service_id)
|
||||
# Capture the status change here as Marshmallow changes this later
|
||||
service_going_live = fetched_service.restricted and not request.get_json().get('restricted', True)
|
||||
|
||||
current_data = dict(service_schema.dump(fetched_service).data.items())
|
||||
current_data.update(request.get_json())
|
||||
update_dict = service_schema.load(current_data).data
|
||||
dao_update_service(update_dict)
|
||||
|
||||
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'],
|
||||
'message_limit': current_data['message_limit']
|
||||
},
|
||||
include_user_fields=['name']
|
||||
)
|
||||
|
||||
return jsonify(data=service_schema.dump(fetched_service).data), 200
|
||||
|
||||
|
||||
|
||||
33
app/service/sender.py
Normal file
33
app/service/sender.py
Normal file
@@ -0,0 +1,33 @@
|
||||
from flask import current_app
|
||||
|
||||
from app.dao.services_dao import dao_fetch_service_by_id, dao_fetch_active_users_for_service
|
||||
from app.dao.templates_dao import dao_get_template_by_id
|
||||
from app.models import EMAIL_TYPE, KEY_TYPE_NORMAL
|
||||
from app.notifications.process_notifications import persist_notification, send_notification_to_queue
|
||||
|
||||
|
||||
def send_notification_to_service_users(service_id, template_id, personalisation={}, include_user_fields=[]):
|
||||
template = dao_get_template_by_id(template_id)
|
||||
service = dao_fetch_service_by_id(service_id)
|
||||
active_users = dao_fetch_active_users_for_service(service.id)
|
||||
notify_service = dao_fetch_service_by_id(current_app.config['NOTIFY_SERVICE_ID'])
|
||||
|
||||
for user in active_users:
|
||||
personalisation = _add_user_fields(user, personalisation, include_user_fields)
|
||||
notification = persist_notification(
|
||||
template_id=template.id,
|
||||
template_version=template.version,
|
||||
recipient=user.email_address if template.template_type == EMAIL_TYPE else user.mobile_number,
|
||||
service=notify_service,
|
||||
personalisation=personalisation,
|
||||
notification_type=template.template_type,
|
||||
api_key_id=None,
|
||||
key_type=KEY_TYPE_NORMAL
|
||||
)
|
||||
send_notification_to_queue(notification, False, queue='notify')
|
||||
|
||||
|
||||
def _add_user_fields(user, personalisation, fields):
|
||||
for field in fields:
|
||||
personalisation[field] = getattr(user, field)
|
||||
return personalisation
|
||||
Reference in New Issue
Block a user