mirror of
https://github.com/GSA/notifications-api.git
synced 2026-08-26 17:23:50 -04:00
Add endpoint to set broadcast service channel
This commit is contained in:
15
app/dao/service_broadcast_settings_dao.py
Normal file
15
app/dao/service_broadcast_settings_dao.py
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
from app import db
|
||||||
|
from app.models import ServiceBroadcastSettings
|
||||||
|
from app.dao.dao_utils import transactional
|
||||||
|
|
||||||
|
|
||||||
|
@transactional
|
||||||
|
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
|
||||||
|
db.session.add(settings)
|
||||||
|
else:
|
||||||
|
service.service_broadcast_settings.channel = channel
|
||||||
|
db.session.add(service.service_broadcast_settings)
|
||||||
@@ -73,6 +73,9 @@ from app.dao.services_dao import (
|
|||||||
dao_update_service,
|
dao_update_service,
|
||||||
get_services_by_partial_name,
|
get_services_by_partial_name,
|
||||||
)
|
)
|
||||||
|
from app.dao.service_broadcast_settings_dao import (
|
||||||
|
insert_or_update_service_broadcast_settings
|
||||||
|
)
|
||||||
from app.dao.service_guest_list_dao import (
|
from app.dao.service_guest_list_dao import (
|
||||||
dao_fetch_service_guest_list,
|
dao_fetch_service_guest_list,
|
||||||
dao_add_and_commit_guest_list_contacts,
|
dao_add_and_commit_guest_list_contacts,
|
||||||
@@ -100,9 +103,15 @@ from app.errors import (
|
|||||||
)
|
)
|
||||||
from app.letters.utils import letter_print_day
|
from app.letters.utils import letter_print_day
|
||||||
from app.models import (
|
from app.models import (
|
||||||
KEY_TYPE_NORMAL, LETTER_TYPE, NOTIFICATION_CANCELLED, Permission, Service,
|
KEY_TYPE_NORMAL,
|
||||||
EmailBranding, LetterBranding,
|
LETTER_TYPE,
|
||||||
ServiceContactList
|
NOTIFICATION_CANCELLED,
|
||||||
|
Permission,
|
||||||
|
Service,
|
||||||
|
EmailBranding,
|
||||||
|
LetterBranding,
|
||||||
|
ServiceContactList,
|
||||||
|
ServiceBroadcastSettings,
|
||||||
)
|
)
|
||||||
from app.notifications.process_notifications import persist_notification, send_notification_to_queue
|
from app.notifications.process_notifications import persist_notification, send_notification_to_queue
|
||||||
from app.schema_validation import validate
|
from app.schema_validation import validate
|
||||||
@@ -118,6 +127,7 @@ from app.service.service_senders_schema import (
|
|||||||
add_service_letter_contact_block_request,
|
add_service_letter_contact_block_request,
|
||||||
add_service_sms_sender_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.utils import get_guest_list_objects
|
||||||
from app.service.sender import send_notification_to_service_users
|
from app.service.sender import send_notification_to_service_users
|
||||||
from app.service.send_notification import send_one_off_notification, send_pdf_letter_notification
|
from app.service.send_notification import send_one_off_notification, send_pdf_letter_notification
|
||||||
@@ -1070,3 +1080,12 @@ def create_contact_list(service_id):
|
|||||||
save_service_contact_list(list_to_save)
|
save_service_contact_list(list_to_save)
|
||||||
|
|
||||||
return jsonify(list_to_save.serialize()), 201
|
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):
|
||||||
|
data = validate(request.get_json(), service_broadcast_settings_schema)
|
||||||
|
service = dao_fetch_service_by_id(service_id)
|
||||||
|
insert_or_update_service_broadcast_settings(service, channel=data["broadcast_channel"])
|
||||||
|
data = service_schema.dump(service).data
|
||||||
|
return jsonify(data=data)
|
||||||
|
|||||||
10
app/service/service_broadcast_settings_schema.py
Normal file
10
app/service/service_broadcast_settings_schema.py
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
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"]}
|
||||||
|
},
|
||||||
|
"required": ["broadcast_channel"]
|
||||||
|
}
|
||||||
@@ -31,11 +31,13 @@ from app.models import (
|
|||||||
EMAIL_TYPE,
|
EMAIL_TYPE,
|
||||||
SMS_TYPE,
|
SMS_TYPE,
|
||||||
LETTER_TYPE,
|
LETTER_TYPE,
|
||||||
|
BROADCAST_TYPE,
|
||||||
INTERNATIONAL_LETTERS,
|
INTERNATIONAL_LETTERS,
|
||||||
INTERNATIONAL_SMS_TYPE,
|
INTERNATIONAL_SMS_TYPE,
|
||||||
INBOUND_SMS_TYPE,
|
INBOUND_SMS_TYPE,
|
||||||
NOTIFICATION_RETURNED_LETTER,
|
NOTIFICATION_RETURNED_LETTER,
|
||||||
UPLOAD_LETTERS,
|
UPLOAD_LETTERS,
|
||||||
|
ServiceBroadcastSettings,
|
||||||
)
|
)
|
||||||
from tests import create_authorization_header
|
from tests import create_authorization_header
|
||||||
from tests.app.db import (
|
from tests.app.db import (
|
||||||
@@ -3647,3 +3649,75 @@ def test_get_returned_letter(admin_request, sample_letter_template):
|
|||||||
assert not response[4]['original_file_name']
|
assert not response[4]['original_file_name']
|
||||||
assert not response[4]['job_row_number']
|
assert not response[4]['job_row_number']
|
||||||
assert response[4]['uploaded_letter_file_name'] == 'filename.pdf'
|
assert response[4]['uploaded_letter_file_name'] == 'filename.pdf'
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('channel', ["test", "severe"])
|
||||||
|
def test_set_as_broadcast_service_sets_broadcast_channel(client, notify_db, sample_service, channel):
|
||||||
|
assert sample_service.service_broadcast_settings is None
|
||||||
|
data = {
|
||||||
|
'broadcast_channel': channel,
|
||||||
|
}
|
||||||
|
|
||||||
|
resp = client.post(
|
||||||
|
'/service/{}/set-as-broadcast-service'.format(sample_service.id),
|
||||||
|
data=json.dumps(data),
|
||||||
|
headers=[('Content-Type', 'application/json'), create_authorization_header()]
|
||||||
|
)
|
||||||
|
result = resp.json
|
||||||
|
assert resp.status_code == 200
|
||||||
|
assert result['data']['name'] == 'Sample service'
|
||||||
|
assert result['data']['broadcast_channel'] == channel
|
||||||
|
|
||||||
|
records = ServiceBroadcastSettings.query.filter_by(service_id=sample_service.id).all()
|
||||||
|
assert len(records) == 1
|
||||||
|
assert records[0].service_id == sample_service.id
|
||||||
|
assert records[0].channel == channel
|
||||||
|
|
||||||
|
|
||||||
|
def test_set_as_broadcast_service_updates_channel(client, notify_db, sample_service):
|
||||||
|
settings = ServiceBroadcastSettings(channel="test", service=sample_service)
|
||||||
|
notify_db.session.add(settings)
|
||||||
|
notify_db.session.commit()
|
||||||
|
assert sample_service.broadcast_channel == "test"
|
||||||
|
|
||||||
|
resp = client.post(
|
||||||
|
'/service/{}/set-as-broadcast-service'.format(sample_service.id),
|
||||||
|
data=json.dumps({
|
||||||
|
'broadcast_channel': "severe",
|
||||||
|
}),
|
||||||
|
headers=[('Content-Type', 'application/json'), create_authorization_header()]
|
||||||
|
)
|
||||||
|
result = resp.json
|
||||||
|
assert resp.status_code == 200
|
||||||
|
assert result['data']['name'] == 'Sample service'
|
||||||
|
assert result['data']['broadcast_channel'] == "severe"
|
||||||
|
|
||||||
|
records = ServiceBroadcastSettings.query.filter_by(service_id=sample_service.id).all()
|
||||||
|
assert len(records) == 1
|
||||||
|
assert records[0].service_id == sample_service.id
|
||||||
|
assert records[0].channel == "severe"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('channel', ["government", "extreme", "exercise", "random", ""])
|
||||||
|
def test_set_as_broadcast_service_rejects_unknown_channels(client, notify_db, sample_service, channel):
|
||||||
|
data = {
|
||||||
|
'broadcast_channel': channel,
|
||||||
|
}
|
||||||
|
|
||||||
|
resp = client.post(
|
||||||
|
'/service/{}/set-as-broadcast-service'.format(sample_service.id),
|
||||||
|
data=json.dumps(data),
|
||||||
|
headers=[('Content-Type', 'application/json'), create_authorization_header()]
|
||||||
|
)
|
||||||
|
assert resp.status_code == 400
|
||||||
|
|
||||||
|
|
||||||
|
def test_set_as_broadcast_service_rejects_if_no_channel(client, notify_db, sample_service):
|
||||||
|
data = {}
|
||||||
|
|
||||||
|
resp = client.post(
|
||||||
|
'/service/{}/set-as-broadcast-service'.format(sample_service.id),
|
||||||
|
data=json.dumps(data),
|
||||||
|
headers=[('Content-Type', 'application/json'), create_authorization_header()]
|
||||||
|
)
|
||||||
|
assert resp.status_code == 400
|
||||||
|
|||||||
Reference in New Issue
Block a user