diff --git a/web/modules/custom/dpl_event/src/Services/EventRestMapper.php b/web/modules/custom/dpl_event/src/Services/EventRestMapper.php index b23b095f1..d342b1d12 100644 --- a/web/modules/custom/dpl_event/src/Services/EventRestMapper.php +++ b/web/modules/custom/dpl_event/src/Services/EventRestMapper.php @@ -66,7 +66,7 @@ public function getResponse(EventInstance $event_instance): EventsGET200Response 'ticketCapacity' => $this->getValue('event_ticket_capacity'), 'ticketCategories' => $this->getTicketCategories(), 'createdAt' => $this->getDateField('created'), - 'updatedAt' => $this->getDateField('changed'), + 'updatedAt' => $this->getUpdatedDate(), 'dateTime' => $this->getDate(), 'externalData' => $this->getExternalData(), 'series' => new EventsGET200ResponseInnerSeries([ @@ -75,6 +75,37 @@ public function getResponse(EventInstance $event_instance): EventsGET200Response ]); } + /** + * Getting relevant updated date - either the series or instance. + * + * As we use inheritance, we want an updated series to also reflect in the + * updatedAt API property. + * We could implement this, by programmatically saving all instances when + * the series is saved, but this may have unforseen consequences, as it is + * running against the Drupal system. + * Instead, we'll look up the instance and series changed dates, and take + * which ever is newer. + */ + private function getUpdatedDate(): ?DateTime { + $series = $this->event->getEventSeries(); + + $changed_instance = $this->event->getChangedTime(); + $changed_series = $series->getChangedTime(); + + // Setting the timestamp to whichever is the larger. + $timestamp = ($changed_instance > $changed_series) ? + $changed_instance : $changed_series; + + if (empty($timestamp)) { + return NULL; + } + + $date = new DateTime(); + $date->setTimestamp(intval($timestamp)); + + return $date; + } + /** * Getting associated branches. *