Skip to content

Commit

Permalink
fix(ZMSKVR-140): fix invalid appointment timestamps array
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Fink authored and Thomas Fink committed Feb 21, 2025
1 parent 6b8b1ca commit c82eeda
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
class AvailableAppointments extends Entity implements JsonSerializable
{
public static $schema = 'citizenapi/availableAppointments.json';
/** @var array */
/** @var array */
public array $appointmentTimestamps = [];
public function __construct(array $appointmentTimestamps = [])
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -482,40 +482,39 @@ public static function getAvailableAppointments(string $date, array $officeIds,
return new AvailableAppointments(array_values($timestamps)[0]);
}

private static function processFreeSlots(ProcessList $freeSlots): array
{
private static function processFreeSlots(ProcessList $freeSlots): array {
$errors = ValidationService::validateGetProcessFreeSlots($freeSlots);
if (is_array($errors) && !empty($errors['errors'])) {
return $errors;
}

$currentTimestamp = time();
$appointmentTimestamps = array_reduce(iterator_to_array($freeSlots), function ($timestamps, $slot) use ($currentTimestamp) {

$allTimestamps = [];

// Collect all timestamps
foreach ($freeSlots as $slot) {
if (isset($slot->appointments) && is_iterable($slot->appointments)) {
$providerId = (int) $slot->scope->provider->id;
foreach ($slot->appointments as $appointment) {
if (isset($appointment->date)) {
$timestamp = (int) $appointment->date;
if ($timestamp > $currentTimestamp) {
$timestamps[$providerId][$timestamp] = true;
$allTimestamps[] = $timestamp;
}
}
}
}
return $timestamps;
}, []);
foreach ($appointmentTimestamps as $providerId => &$timestamps) {
$timestamps = array_keys($timestamps);
asort($timestamps);
}

$errors = ValidationService::validateGetProcessByIdTimestamps($appointmentTimestamps);

// Final deduplication and sorting
$uniqueTimestamps = array_values(array_unique($allTimestamps));
sort($uniqueTimestamps);

$errors = ValidationService::validateGetProcessByIdTimestamps($uniqueTimestamps);
if (is_array($errors) && !empty($errors['errors'])) {
return $errors;
}

return $appointmentTimestamps;
return ['appointmentTimestamps' => $uniqueTimestamps];
}

public static function reserveTimeslot(Process $appointmentProcess, array $serviceIds, array $serviceCounts): ThinnedProcess|array
Expand Down
14 changes: 9 additions & 5 deletions zmsentities/schema/citizenapi/availableAppointments.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
{
"title": "AvailableAppointments",
"type": ["array", "object", "null"],
"type": ["array", "object"],
"properties": {
"appointmentTimestamps": {
"type": ["array", "null"],
"type": "array",
"description": "Array of available appointment timestamps in seconds since epoch",
"items": {
"type": "integer",
"description": "Timestamp in seconds since epoch"
}
"description": "Timestamp in seconds since epoch",
"minimum": 0
},
"uniqueItems": true,
"minItems": 0
}
},
"required": ["appointmentTimestamps"],
"additionalProperties": false,
"description": "Schema defining the available appointments for a specific date, office, and service"
}
}
12 changes: 8 additions & 4 deletions zmsentities/schema/citizenapi/availableAppointmentsByOffice.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@
"description": "Office ID"
},
"appointmentTimestamps": {
"type": ["array", "null"],
"description": "Numeric timestamps (seconds) for each available slot.",
"type": "array",
"description": "Array of available appointment timestamps in seconds since epoch",
"items": {
"type": "integer"
}
"type": "integer",
"description": "Timestamp in seconds since epoch",
"minimum": 0
},
"uniqueItems": true,
"minItems": 0
}
},
"required": ["offices"]
Expand Down

0 comments on commit c82eeda

Please sign in to comment.