Raise error if multiple broadcasts found for reference

Because the `<reference>` field of a `cancel` message can contain an
arbitrary number of items it’s possible for it to reference more than
one current alert.

In this case it is ambiguous which alert should be cancelled, so we
should raise a custom error.

This will help people know that they have to manually go into Notify and
figure out which alert(s) to cancel there.
This commit is contained in:
Chris Hill-Scott
2022-02-17 11:46:24 +00:00
parent f691bc2a92
commit cc207ac11f
3 changed files with 53 additions and 4 deletions

View File

@@ -3,6 +3,7 @@ from itertools import chain
from flask import current_app, jsonify, request
from notifications_utils.polygons import Polygons
from notifications_utils.template import BroadcastMessageTemplate
from sqlalchemy.orm.exc import MultipleResultsFound
from app import api_user, authenticated_service
from app.broadcast_message.translators import cap_xml_to_dict
@@ -105,10 +106,17 @@ def create_broadcast():
def _cancel_or_reject_broadcast(references_to_original_broadcast, service_id):
broadcast_message = dao_get_broadcast_message_by_references_and_service_id(
references_to_original_broadcast,
service_id
)
try:
broadcast_message = dao_get_broadcast_message_by_references_and_service_id(
references_to_original_broadcast,
service_id
)
except MultipleResultsFound:
raise BadRequestError(
message='Multiple alerts found - unclear which one to cancel',
status_code=400,
)
if broadcast_message.status == BroadcastStatusType.PENDING_APPROVAL:
new_status = BroadcastStatusType.REJECTED
else: