Skip to content

Commit

Permalink
fix(ZMS-3253): show availability opening hour conflicts in the future…
Browse files Browse the repository at this point in the history
… not just for selectedDate
  • Loading branch information
Tom Fink authored and Tom Fink committed Nov 15, 2024
1 parent a16b2e4 commit 1bd40cb
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 16 deletions.
80 changes: 64 additions & 16 deletions zmsadmin/src/Zmsadmin/AvailabilityConflicts.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@
namespace BO\Zmsadmin;

use BO\Zmsentities\Availability;

use BO\Zmsentities\Collection\AvailabilityList;

/**
* Check if new Availability is in conflict with existing availability
*
*/
class AvailabilityConflicts extends BaseController
{
Expand All @@ -28,10 +26,7 @@ public function readResponse(
$validator = $request->getAttribute('validator');
$input = $validator->getInput()->isJson()->assertValid()->getValue();
$data = static::getAvailabilityData($input);
return \BO\Slim\Render::withJson(
$response,
$data
);
return \BO\Slim\Render::withJson($response, $data);
}

protected static function getAvailabilityData($input)
Expand All @@ -41,20 +36,25 @@ protected static function getAvailabilityData($input)
$conflictedList = [];

$selectedDateTime = (new \DateTimeImmutable($input['selectedDate']))->modify(\App::$now->format('H:i:s'));
$selectedAvailability = new Availability($input['selectedAvailability']);
$startDateTime = ($selectedAvailability->getStartDateTime() >= \App::$now) ?
$selectedAvailability->getStartDateTime() : $selectedDateTime;
$endDateTime = ($input['selectedAvailability']) ?
$selectedAvailability->getEndDateTime() : $selectedDateTime;

$scopeData = $input['availabilityList'][0]['scope'];
$scope = new \BO\Zmsentities\Scope($scopeData);
$futureAvailabilityList = self::getAvailabilityList($scope, $selectedDateTime);

foreach ($futureAvailabilityList as $futureAvailability) {
$availabilityList->addEntity($futureAvailability);
}

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

$availabilityList = $availabilityList->sortByCustomStringKey('endTime');
$conflictList = $availabilityList->getConflicts($startDateTime, $endDateTime);
$conflictList = $availabilityList->getConflicts($earliestStartDateTime, $latestEndDateTime);

foreach ($conflictList as $conflict) {
$availabilityId = ($conflict->getFirstAppointment()->getAvailability()->getId()) ?
$conflict->getFirstAppointment()->getAvailability()->getId() :
$conflict->getFirstAppointment()->getAvailability()->tempId;
if (! in_array($availabilityId, $conflictedList)) {
if (!in_array($availabilityId, $conflictedList)) {
$conflictedList[] = $availabilityId;
}
}
Expand All @@ -65,7 +65,13 @@ protected static function getAvailabilityData($input)
];
}

/*
/**
* Fetch availabilities for a given scope and date.
*
* @param \BO\Zmsentities\Scope $scope
* @param \DateTimeImmutable $dateTime
* @return AvailabilityList
*/
protected static function getAvailabilityList($scope, $dateTime)
{
try {
Expand All @@ -74,7 +80,7 @@ protected static function getAvailabilityList($scope, $dateTime)
'/scope/' . $scope->getId() . '/availability/',
[
'resolveReferences' => 0,
'startDate' => $dateTime->format('Y-m-d') //for skipping old availabilities
'startDate' => $dateTime->format('Y-m-d') // Only fetch availabilities from this date onward
]
)
->getCollection();
Expand All @@ -86,5 +92,47 @@ protected static function getAvailabilityList($scope, $dateTime)
}
return $availabilityList->withScope($scope);
}
*/


/**
* 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];
}

}
15 changes: 15 additions & 0 deletions zmsadmin/tests/Zmsadmin/AvailabilityConflictsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,21 @@ class AvailabilityConflictsTest extends Base

public function testRendering()
{

$this->setApiCalls(
[
[
'function' => 'readGetResult',
'url' => '/scope/141/availability/',
'parameters' => [
'resolveReferences' => 0,
'startDate' => '2016-04-04'
],
'response' => $this->readFixture("GET_availability_68985.json")
]
]
);

$response = $this->render([], [
'__body' => '{
"availabilityList": [
Expand Down

0 comments on commit 1bd40cb

Please sign in to comment.