Skip to content

Commit

Permalink
Update event API, editing series should reflect in changed date. DDFH…
Browse files Browse the repository at this point in the history
…ER-132

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.
  • Loading branch information
rasben committed Nov 19, 2024
1 parent 8349c76 commit f15fc70
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion web/modules/custom/dpl_event/src/Services/EventRestMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand All @@ -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.
*
Expand Down

0 comments on commit f15fc70

Please sign in to comment.