From 9c9a1012d200da3c631c435fc01d87abc31166bb Mon Sep 17 00:00:00 2001 From: MarkLark86 Date: Mon, 8 Jul 2024 09:19:02 +1000 Subject: [PATCH] [STT-15] backend: Support AddAsEvent when Planning already has an Event linked (#2015) --- server/features/events.feature | 78 ++++++++++++++++++++++++++++ server/features/planning.feature | 40 ++++++++++++++ server/planning/events/events.py | 10 +++- server/planning/history.py | 1 + server/planning/planning/planning.py | 3 ++ 5 files changed, 130 insertions(+), 2 deletions(-) diff --git a/server/features/events.feature b/server/features/events.feature index b5036603a..bff343d72 100644 --- a/server/features/events.feature +++ b/server/features/events.feature @@ -719,6 +719,84 @@ Feature: Events } """ + @auth + Scenario: Link new Event as secondary to a Planning item + Given "planning" + """ + [{ + "_id": "plan1", + "guid": "plan1", + "slugline": "TestEvent", + "state": "draft", + "lock_user": "#CONTEXT_USER_ID#", + "lock_session": "#SESSION_ID#", + "lock_action": "add_as_event", + "lock_time": "#DATE#", + "planning_date": "2016-01-02" + }] + """ + When we post to "events" + """ + { + "guid": "event_1", + "name": "Primary Event 1", + "slugline": "event-1", + "_planning_item": "plan1", + "dates": { + "start": "2029-11-21T12:00:00.000Z", + "end": "2029-11-21T14:00:00.000Z", + "tz": "Australia/Sydney" + } + } + """ + Then we get OK response + When we get "/planning/plan1" + Then we get existing resource + """ + {"related_events": [{"_id": "#events._id#", "link_type": "primary"}]} + """ + When we post to "events" + """ + { + "guid": "event_2", + "name": "Primary Event 2", + "slugline": "event-2", + "_planning_item": "plan1", + "dates": { + "start": "2029-11-21T12:00:00.000Z", + "end": "2029-11-21T14:00:00.000Z", + "tz": "Australia/Sydney" + } + } + """ + Then we get OK response + When we get "/planning/plan1" + Then we get existing resource + """ + {"related_events": [ + {"_id": "event_1", "link_type": "primary"}, + {"_id": "event_2", "link_type": "secondary"} + ]} + """ + + @auth + Scenario: Attemps to link Event to non existing Planning fails + When we post to "events" + """ + { + "guid": "event_1", + "name": "Primary Event 1", + "slugline": "event-1", + "_planning_item": "plan1", + "dates": { + "start": "2029-11-21T12:00:00.000Z", + "end": "2029-11-21T14:00:00.000Z", + "tz": "Australia/Sydney" + } + } + """ + Then we get error 400 + @auth Scenario: Fails to link a new Event to a Planning Item if another use holds the Planning lock Given we have sessions "/sessions" diff --git a/server/features/planning.feature b/server/features/planning.feature index 3d7c5df3c..f30a9acf7 100644 --- a/server/features/planning.feature +++ b/server/features/planning.feature @@ -4323,3 +4323,43 @@ Feature: Planning } """ Then we get OK response + + @auth + Scenario: Planning can only have 1 primary linked event + Given "events" + """ + [{ + "guid": "event_1", + "name": "Primary Event 1", + "slugline": "event-1", + "dates": { + "start": "2029-11-21T12:00:00.000Z", + "end": "2029-11-21T14:00:00.000Z", + "tz": "Australia/Sydney" + } + }, { + "guid": "event_2", + "name": "Primary Event 2", + "slugline": "event-2", + "dates": { + "start": "2029-11-21T12:00:00.000Z", + "end": "2029-11-21T14:00:00.000Z", + "tz": "Australia/Sydney" + } + }] + """ + When we post to "planning" + """ + { + "_id": "plan1", + "guid": "plan1", + "slugline": "TestEvent", + "state": "draft", + "planning_date": "2016-01-02". + "related_events": [ + {"_id": "event_1", "link_type": "primary"}, + {"_id": "event_2", "link_type": "primary"} + ] + } + """ + Then we get error 400 diff --git a/server/planning/events/events.py b/server/planning/events/events.py index 264820725..6d3b4e788 100644 --- a/server/planning/events/events.py +++ b/server/planning/events/events.py @@ -75,7 +75,7 @@ update_ingest_on_patch, TEMP_ID_PREFIX, ) -from planning.utils import get_related_planning_for_events +from planning.utils import get_related_planning_for_events, get_related_event_ids_for_planning from .events_base_service import EventsBaseService from .events_schema import events_schema from .events_sync import sync_event_metadata_with_planning_items @@ -735,7 +735,7 @@ def get_recurring_timeline(self, selected, spiked=False): return events_base_service.get_recurring_timeline(selected, postponed=True, spiked=spiked) @staticmethod - def _link_to_planning(event: Event, link_type: PLANNING_RELATED_EVENT_LINK_TYPE = "primary"): + def _link_to_planning(event: Event): """ Links an Event to an existing Planning Item @@ -747,7 +747,13 @@ def _link_to_planning(event: Event, link_type: PLANNING_RELATED_EVENT_LINK_TYPE event_id = event[config.ID_FIELD] planning_item = planning_service.find_one(req=None, _id=plan_id) + if not planning_item: + raise SuperdeskApiError.badRequestError("Planning item not found") + updates = {"related_events": planning_item.get("related_events") or []} + link_type: PLANNING_RELATED_EVENT_LINK_TYPE = ( + "primary" if not len(get_related_event_ids_for_planning(planning_item, "primary")) else "secondary" + ) related_planning = PlanningRelatedEventLink(_id=event_id, link_type=link_type) updates["related_events"].append(related_planning) diff --git a/server/planning/history.py b/server/planning/history.py index ed903a416..e9eb30cfb 100644 --- a/server/planning/history.py +++ b/server/planning/history.py @@ -115,6 +115,7 @@ def _remove_unwanted_fields(self, update): update_copy.pop(field, None) return update_copy + return update def _save_history(self, item, update, operation): raise NotImplementedError() diff --git a/server/planning/planning/planning.py b/server/planning/planning/planning.py index e497d9aa2..343e3f153 100644 --- a/server/planning/planning/planning.py +++ b/server/planning/planning/planning.py @@ -319,6 +319,9 @@ def validate_planning(self, updates, original=None): sanitize_input_data(updates) + if len(get_related_event_ids_for_planning(updates, "primary")) > 1: + raise SuperdeskApiError.badRequestError("Only 1 primary linked event is allowed") + # Validate if agendas being added are enabled agendas agenda_service = get_resource_service("agenda") for agenda_id in updates.get("agendas", []):