mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-02 08:45:16 -05:00
Merge pull request #3120 from alphagov/update-service-broadcast-settings
Update service broadcast settings
This commit is contained in:
@@ -384,6 +384,9 @@ class Config(object):
|
||||
|
||||
ENABLED_CBCS = {BroadcastProvider.EE, BroadcastProvider.THREE, BroadcastProvider.O2, BroadcastProvider.VODAFONE}
|
||||
|
||||
# as defined in api db migration 0331_add_broadcast_org.py
|
||||
BROADCAST_ORGANISATION_ID = '38e4bf69-93b0-445d-acee-53ea53fe02df'
|
||||
|
||||
|
||||
######################
|
||||
# Config overrides ###
|
||||
|
||||
65
app/dao/broadcast_service_dao.py
Normal file
65
app/dao/broadcast_service_dao.py
Normal file
@@ -0,0 +1,65 @@
|
||||
from datetime import datetime
|
||||
|
||||
from flask import current_app
|
||||
|
||||
from app import db
|
||||
from app.models import ServiceBroadcastSettings, ServicePermission, Organisation, BROADCAST_TYPE
|
||||
from app.dao.dao_utils import transactional
|
||||
|
||||
|
||||
@transactional
|
||||
def set_broadcast_service_type(service, service_mode, broadcast_channel, provider_restriction):
|
||||
insert_or_update_service_broadcast_settings(
|
||||
service, channel=broadcast_channel, provider_restriction=provider_restriction
|
||||
)
|
||||
|
||||
# Remove all permissions and add broadcast permission
|
||||
if not service.has_permission(BROADCAST_TYPE):
|
||||
service_permission = ServicePermission(service_id=service.id, permission=BROADCAST_TYPE)
|
||||
db.session.add(service_permission)
|
||||
|
||||
ServicePermission.query.filter(
|
||||
ServicePermission.service_id == service.id,
|
||||
ServicePermission.permission != BROADCAST_TYPE
|
||||
).delete()
|
||||
|
||||
# Refresh the service object as it has references to the service permissions but we don't yet
|
||||
# want to commit the permission changes incase all of this needs to rollback
|
||||
db.session.refresh(service)
|
||||
|
||||
# Set service count as live false always
|
||||
service.count_as_live = False
|
||||
|
||||
# Set service into training mode or live mode
|
||||
if service_mode == "live":
|
||||
if service.restricted:
|
||||
# Only update the go live at timestamp if this if moving from training mode
|
||||
# to live mode, not if it's moving from one type of live mode service to another
|
||||
service.go_live_at = datetime.utcnow()
|
||||
service.restricted = False
|
||||
else:
|
||||
service.restricted = True
|
||||
service.go_live_at = None
|
||||
|
||||
# Add service to organisation
|
||||
organisation = Organisation.query.filter_by(
|
||||
id=current_app.config['BROADCAST_ORGANISATION_ID']
|
||||
).one()
|
||||
service.organisation_id = organisation.id
|
||||
service.organisation_type = organisation.organisation_type
|
||||
service.crown = organisation.crown
|
||||
|
||||
db.session.add(service)
|
||||
|
||||
|
||||
def insert_or_update_service_broadcast_settings(service, channel, provider_restriction=None):
|
||||
if not service.service_broadcast_settings:
|
||||
settings = ServiceBroadcastSettings()
|
||||
settings.service = service
|
||||
settings.channel = channel
|
||||
settings.provider = provider_restriction
|
||||
db.session.add(settings)
|
||||
else:
|
||||
service.service_broadcast_settings.channel = channel
|
||||
service.service_broadcast_settings.provider = provider_restriction
|
||||
db.session.add(service.service_broadcast_settings)
|
||||
@@ -25,7 +25,7 @@ from notifications_utils.recipients import (
|
||||
|
||||
from app import ma
|
||||
from app import models
|
||||
from app.models import ServicePermission
|
||||
from app.models import ServicePermission, BROADCAST_TYPE
|
||||
from app.dao.permissions_dao import permission_dao
|
||||
from app.utils import DATETIME_FORMAT_NO_TIMEZONE, get_template_instance
|
||||
|
||||
@@ -236,10 +236,21 @@ class ServiceSchema(BaseSchema, UUIDsAsStringsMixin):
|
||||
override_flag = False
|
||||
go_live_at = field_for(models.Service, 'go_live_at', format=DATETIME_FORMAT_NO_TIMEZONE)
|
||||
allowed_broadcast_provider = fields.Method(dump_only=True, serialize='_get_allowed_broadcast_provider')
|
||||
broadcast_channel = fields.Method(dump_only=True, serialize='_get_broadcast_channel')
|
||||
|
||||
def _get_allowed_broadcast_provider(self, service):
|
||||
return service.allowed_broadcast_provider
|
||||
|
||||
def _get_broadcast_channel(self, service):
|
||||
# TODO: Once we've migrated data so that all broadcast services have `service.broadcast_channel`
|
||||
# set then we can remove this logic and related tests and instead just return
|
||||
# `service.broadcast_channel`. For the moment though, as we have some services with the broadcast
|
||||
# permission that do not have a row in the service_broadcast_settings table, we need to hardcode
|
||||
# this in here to give them a default that the admin app can use
|
||||
if BROADCAST_TYPE in self.service_permissions(service):
|
||||
return service.broadcast_channel if service.broadcast_channel else "test"
|
||||
return None
|
||||
|
||||
def get_letter_logo_filename(self, service):
|
||||
return service.letter_branding and service.letter_branding.filename
|
||||
|
||||
|
||||
@@ -29,7 +29,9 @@ from app.dao.fact_notification_status_dao import (
|
||||
fetch_stats_for_all_services_by_date_range, fetch_monthly_template_usage_for_service
|
||||
)
|
||||
from app.dao.inbound_numbers_dao import dao_allocate_number_for_service
|
||||
from app.dao.organisation_dao import dao_get_organisation_by_service_id
|
||||
from app.dao.organisation_dao import (
|
||||
dao_get_organisation_by_service_id,
|
||||
)
|
||||
from app.dao.returned_letters_dao import (
|
||||
fetch_most_recent_returned_letter,
|
||||
fetch_recent_returned_letter_count,
|
||||
@@ -42,6 +44,7 @@ from app.dao.service_contact_list_dao import (
|
||||
dao_get_contact_list_by_id,
|
||||
save_service_contact_list,
|
||||
)
|
||||
from app.dao.broadcast_service_dao import set_broadcast_service_type
|
||||
from app.dao.service_data_retention_dao import (
|
||||
fetch_service_data_retention,
|
||||
fetch_service_data_retention_by_id,
|
||||
@@ -100,8 +103,13 @@ from app.errors import (
|
||||
)
|
||||
from app.letters.utils import letter_print_day
|
||||
from app.models import (
|
||||
KEY_TYPE_NORMAL, LETTER_TYPE, NOTIFICATION_CANCELLED, Permission, Service,
|
||||
EmailBranding, LetterBranding,
|
||||
KEY_TYPE_NORMAL,
|
||||
LETTER_TYPE,
|
||||
NOTIFICATION_CANCELLED,
|
||||
Permission,
|
||||
Service,
|
||||
EmailBranding,
|
||||
LetterBranding,
|
||||
ServiceContactList
|
||||
)
|
||||
from app.notifications.process_notifications import persist_notification, send_notification_to_queue
|
||||
@@ -118,6 +126,7 @@ from app.service.service_senders_schema import (
|
||||
add_service_letter_contact_block_request,
|
||||
add_service_sms_sender_request
|
||||
)
|
||||
from app.service.service_broadcast_settings_schema import service_broadcast_settings_schema
|
||||
from app.service.utils import get_guest_list_objects
|
||||
from app.service.sender import send_notification_to_service_users
|
||||
from app.service.send_notification import send_one_off_notification, send_pdf_letter_notification
|
||||
@@ -1070,3 +1079,27 @@ def create_contact_list(service_id):
|
||||
save_service_contact_list(list_to_save)
|
||||
|
||||
return jsonify(list_to_save.serialize()), 201
|
||||
|
||||
|
||||
@service_blueprint.route('/<uuid:service_id>/set-as-broadcast-service', methods=['POST'])
|
||||
def set_as_broadcast_service(service_id):
|
||||
"""
|
||||
This route does the following
|
||||
- adds a service broadcast settings to define which channel broadcasts should go out on
|
||||
- removes all current service permissions and adds the broadcast service permission
|
||||
- sets the services `count_as_live` to false
|
||||
- adds the service to the broadcast organisation
|
||||
- puts the service into training mode or live mode
|
||||
"""
|
||||
data = validate(request.get_json(), service_broadcast_settings_schema)
|
||||
service = dao_fetch_service_by_id(service_id)
|
||||
|
||||
set_broadcast_service_type(
|
||||
service,
|
||||
service_mode=data["service_mode"],
|
||||
broadcast_channel=data["broadcast_channel"],
|
||||
provider_restriction=data["provider_restriction"]
|
||||
)
|
||||
|
||||
data = service_schema.dump(service).data
|
||||
return jsonify(data=data)
|
||||
|
||||
12
app/service/service_broadcast_settings_schema.py
Normal file
12
app/service/service_broadcast_settings_schema.py
Normal file
@@ -0,0 +1,12 @@
|
||||
service_broadcast_settings_schema = {
|
||||
"$schema": "http://json-schema.org/draft-04/schema#",
|
||||
"description": "Set a services broadcast settings",
|
||||
"type": "object",
|
||||
"title": "Set a services broadcast settings",
|
||||
"properties": {
|
||||
"broadcast_channel": {"enum": ["test", "severe"]},
|
||||
"service_mode": {"enum": ["training", "live"]},
|
||||
"provider_restriction": {"enum": [None, "three", "o2", "vodafone", "ee"]}
|
||||
},
|
||||
"required": ["broadcast_channel", "service_mode", "provider_restriction"]
|
||||
}
|
||||
Reference in New Issue
Block a user