Skip to content

Commit

Permalink
fix(ZMS-3253): refactor function logic getDateTimeRangeFromList to on…
Browse files Browse the repository at this point in the history
…e place
  • Loading branch information
Tom Fink authored and Tom Fink committed Nov 15, 2024
1 parent 4025319 commit 95dcef0
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 126 deletions.
1 change: 0 additions & 1 deletion zmsadmin/js/page/availabilityDay/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,6 @@ class AvailabilityPage extends Component {
selectedAvailability: this.state.selectedAvailability
}))
};
console.log("here");
const url = `${this.props.links.includeurl}/availability/conflicts/`;
fetch(url, requestOptions)
.then(res => res.json())
Expand Down
41 changes: 1 addition & 40 deletions zmsadmin/src/Zmsadmin/AvailabilityConflicts.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected static function getAvailabilityData($input)
$availabilityList->addEntity($futureAvailability);
}

[$earliestStartDateTime, $latestEndDateTime] = self::getDateTimeRangeFromList($availabilityList, $selectedDateTime);
[$earliestStartDateTime, $latestEndDateTime] = $availabilityList->getDateTimeRangeFromList( $selectedDateTime);

$availabilityList = $availabilityList->sortByCustomStringKey('endTime');
$conflictList = $availabilityList->getConflicts($earliestStartDateTime, $latestEndDateTime);
Expand Down Expand Up @@ -94,45 +94,6 @@ protected static function getAvailabilityList($scope, $dateTime)
}


/**
* Get the earliest startDateTime and latest endDateTime from an AvailabilityList
* If the start date of any availability is before the selected date, use the selected date instead.
*
* @param AvailabilityList $availabilityList
* @param \DateTimeImmutable $selectedDate
* @return array
*/
protected static function getDateTimeRangeFromList(AvailabilityList $availabilityList, \DateTimeImmutable $selectedDate): array
{
$earliestStartDateTime = null;
$latestEndDateTime = null;

foreach ($availabilityList as $availability) {
// Convert Unix timestamp to date strings
$startDate = (new \DateTimeImmutable())->setTimestamp($availability->startDate)->format('Y-m-d');
$endDate = (new \DateTimeImmutable())->setTimestamp($availability->endDate)->format('Y-m-d');

// Combine date and time for start and end
$startDateTime = new \DateTimeImmutable("{$startDate} {$availability->startTime}");
$endDateTime = new \DateTimeImmutable("{$endDate} {$availability->endTime}");

// Adjust the startDateTime if it's before the selected date
if ($startDateTime < $selectedDate) {
$startDateTime = $selectedDate->setTime(0, 0);
}

// Determine the earliest start time
if (is_null($earliestStartDateTime) || $startDateTime < $earliestStartDateTime) {
$earliestStartDateTime = $startDateTime;
}

// Determine the latest end time
if (is_null($latestEndDateTime) || $endDateTime > $latestEndDateTime) {
$latestEndDateTime = $endDateTime;
}
}

return [$earliestStartDateTime, $latestEndDateTime];
}

}
44 changes: 1 addition & 43 deletions zmsapi/src/Zmsapi/AvailabilityAdd.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function readResponse(
throw new AvailabilityUpdateFailed();
}

[$earliestStartDateTime, $latestEndDateTime] = $this->getDateTimeRangeFromCollection($mergedCollection, \DateTimeImmutable::createFromFormat('Y-m-d H:i:s', $input['selectedDate'] . ' 00:00:00'));
[$earliestStartDateTime, $latestEndDateTime] = $mergedCollection->getDateTimeRangeFromList(\DateTimeImmutable::createFromFormat('Y-m-d H:i:s', $input['selectedDate'] . ' 00:00:00'));
$conflicts = $mergedCollection->getConflicts($earliestStartDateTime, $latestEndDateTime);
if ($conflicts->count() > 0) {
//error_log(json_encode($conflicts));
Expand All @@ -109,48 +109,6 @@ public function readResponse(
$response = Render::withLastModified($response, time(), '0');
return Render::withJson($response, $message->setUpdatedMetaData(), $message->getStatuscode());
}


/**
* Get the earliest startDateTime and latest endDateTime from a Collection
* If the start date of any availability is before the selected date,
* use the selected date instead.
*
* @param Collection $collection
* @param \DateTimeImmutable $selectedDate
* @return array
*/
private function getDateTimeRangeFromCollection(Collection $collection, \DateTimeImmutable $selectedDate): array
{
$earliestStartDateTime = null;
$latestEndDateTime = null;

foreach ($collection as $availability) {
// Convert Unix timestamp to a date string before concatenating with the time
$startDate = (new \DateTimeImmutable())->setTimestamp($availability->startDate)->format('Y-m-d');
$endDate = (new \DateTimeImmutable())->setTimestamp($availability->endDate)->format('Y-m-d');

// Combine date and time for start and end
$startDateTime = new \DateTimeImmutable("{$startDate} {$availability->startTime}");
$endDateTime = new \DateTimeImmutable("{$endDate} {$availability->endTime}");

// If startDate is before the selectedDate, use the selectedDate as the start
if ($startDateTime < $selectedDate) {
$startDateTime = $selectedDate->setTime(0, 0);
}

// Determine the earliest start and latest end times
if (is_null($earliestStartDateTime) || $startDateTime < $earliestStartDateTime) {
$earliestStartDateTime = $startDateTime;
}
if (is_null($latestEndDateTime) || $endDateTime > $latestEndDateTime) {
$latestEndDateTime = $endDateTime;
}
}

return [$earliestStartDateTime, $latestEndDateTime];
}


protected function writeEntityUpdate($entity, $resolveReferences): Entity
{
Expand Down
42 changes: 1 addition & 41 deletions zmsapi/src/Zmsapi/AvailabilityUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function readResponse(
throw new AvailabilityUpdateFailed();
}

[$earliestStartDateTime, $latestEndDateTime] = $this->getDateTimeRangeFromCollection($mergedCollection, \DateTimeImmutable::createFromFormat('Y-m-d H:i:s', $input['selectedDate'] . ' 00:00:00'));
[$earliestStartDateTime, $latestEndDateTime] = $mergedCollection->getDateTimeRangeFromList(\DateTimeImmutable::createFromFormat('Y-m-d H:i:s', $input['selectedDate'] . ' 00:00:00'));
$conflicts = $mergedCollection->getConflicts($earliestStartDateTime, $latestEndDateTime);
if ($conflicts->count() > 0) {
//error_log(json_encode($conflicts));
Expand All @@ -117,46 +117,6 @@ public function readResponse(
return Render::withJson($response, $message->setUpdatedMetaData(), $message->getStatuscode());
}

/**
* Get the earliest startDateTime and latest endDateTime from a Collection
* If the start date of any availability is before the selected date,
* use the selected date instead.
*
* @param Collection $collection
* @param \DateTimeImmutable $selectedDate
* @return array
*/
private function getDateTimeRangeFromCollection(Collection $collection, \DateTimeImmutable $selectedDate): array
{
$earliestStartDateTime = null;
$latestEndDateTime = null;

foreach ($collection as $availability) {
// Convert Unix timestamp to a date string before concatenating with the time
$startDate = (new \DateTimeImmutable())->setTimestamp($availability->startDate)->format('Y-m-d');
$endDate = (new \DateTimeImmutable())->setTimestamp($availability->endDate)->format('Y-m-d');

// Combine date and time for start and end
$startDateTime = new \DateTimeImmutable("{$startDate} {$availability->startTime}");
$endDateTime = new \DateTimeImmutable("{$endDate} {$availability->endTime}");

// If startDate is before the selectedDate, use the selectedDate as the start
if ($startDateTime < $selectedDate) {
$startDateTime = $selectedDate->setTime(0, 0);
}

// Determine the earliest start and latest end times
if (is_null($earliestStartDateTime) || $startDateTime < $earliestStartDateTime) {
$earliestStartDateTime = $startDateTime;
}
if (is_null($latestEndDateTime) || $endDateTime > $latestEndDateTime) {
$latestEndDateTime = $endDateTime;
}
}

return [$earliestStartDateTime, $latestEndDateTime];
}

protected function writeEntityUpdate($entity, $resolveReferences): Entity
{
$repository = new AvailabilityRepository();
Expand Down
41 changes: 40 additions & 1 deletion zmsentities/src/Zmsentities/Collection/AvailabilityList.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,46 @@ public function validateInputs(\DateTimeImmutable $startDate, \DateTimeImmutable
return $errorList;
}


/**
* Get the earliest startDateTime and latest endDateTime from an AvailabilityList
* If the start date of any availability is before the selected date, use the selected date instead.
*
* @param AvailabilityList $availabilityList
* @param \DateTimeImmutable $selectedDate
* @return array
*/
public function getDateTimeRangeFromList(\DateTimeImmutable $selectedDate): array
{
$earliestStartDateTime = null;
$latestEndDateTime = null;

foreach ($this as $availability) {
// Convert Unix timestamp to date strings
$startDate = (new \DateTimeImmutable())->setTimestamp($availability->startDate)->format('Y-m-d');
$endDate = (new \DateTimeImmutable())->setTimestamp($availability->endDate)->format('Y-m-d');

// Combine date and time for start and end
$startDateTime = new \DateTimeImmutable("{$startDate} {$availability->startTime}");
$endDateTime = new \DateTimeImmutable("{$endDate} {$availability->endTime}");

// Adjust the startDateTime if it's before the selected date
if ($startDateTime < $selectedDate) {
$startDateTime = $selectedDate->setTime(0, 0);
}

// Determine the earliest start time
if (is_null($earliestStartDateTime) || $startDateTime < $earliestStartDateTime) {
$earliestStartDateTime = $startDateTime;
}

// Determine the latest end time
if (is_null($latestEndDateTime) || $endDateTime > $latestEndDateTime) {
$latestEndDateTime = $endDateTime;
}
}

return [$earliestStartDateTime, $latestEndDateTime];
}

public function getConflicts($startDate, $endDate)
{
Expand Down

0 comments on commit 95dcef0

Please sign in to comment.