redesign calendar api

This commit is contained in:
isUnknown 2024-09-11 15:17:44 +02:00
parent 4b6b61f762
commit 78d542cbac
3 changed files with 134 additions and 5 deletions

View file

@ -146,19 +146,48 @@ function getId(eventData) {
return id;
}
function createEmptyCalendar() {
const calendar = {};
for (let month = 9; month < 19; month++) {
const normalizedMonth = ((month - 1) % 12) + 1;
const daysInMonth = dayjs()
.month(normalizedMonth - 1)
.daysInMonth();
calendar[normalizedMonth] = {
name: dayjs()
.month(normalizedMonth - 1)
.format("MMMM"),
};
for (let day = 1; day <= daysInMonth; day++) {
calendar[normalizedMonth][day] = null;
}
}
return calendar;
}
async function getMergedDates(monthNumb, dates) {
const month = dayjs().month(monthNumb).format("YYYY-MM");
const calendar = createEmptyCalendar();
const requestParams = {
month,
dates,
calendar,
};
const requestOptions = {
method: "POST",
body: JSON.stringify(requestParams),
};
const response = await fetch("/month-dates.json", requestOptions);
const response = await fetch(
"/get-current-season-calendar.json",
requestOptions
);
const json = await response.json();
console.log("calendar", json);
return json;
}

View file

@ -34,7 +34,7 @@ return [
],
'routes' => [
require_once(__DIR__ . '/routes/mapado-api.php'),
require_once(__DIR__ . '/routes/month-dates.php'),
require_once(__DIR__ . '/routes/get-current-season-calendar.php'),
require_once(__DIR__ . '/routes/update-mapado-event.php'),
require_once(__DIR__ . '/routes/mapado-fetch.php'),
require_once(__DIR__ . '/routes/brevo-create-contact.php'),

View file

@ -0,0 +1,100 @@
<?php
function getCorrespondingSeasons($request) {
$year = explode('-', $request['month'])[0];
return page('programme')->children()->filter(
fn ($season) => str_contains($season->title()->value(), $year)
);
}
function getTimeDifference($start, $end) {
$start = new DateTime($start);
$end = new DateTime($end);
$difference = $end->diff($start);
return $difference->h . "h" . $difference->i;
}
return [
'pattern' => '/get-current-season-calendar.json',
'method' => 'POST',
'action' => function () {
$jsonRequest = file_get_contents("php://input");
$request = json_decode($jsonRequest, true);
$calendar = $request['calendar'];
$currentSeason = page('programme')->children()->first();
foreach ($currentSeason->children() as $event) {
$eventInfos = [
"title" => $event->title()->value(),
"place" => $event->place()->value(),
"authors" => $event->authors()->value()
];
if ($event->isMapadoEvent() == 'false') {
foreach ($event->sessions()->toStructure() as $session) {
$sessionMonth = $session->date()->toDate('M');
$sessionDay = intval($session->date()->toDate('d'));
$calendar[$sessionMonth][$sessionDay]['sessions'][] = array_merge($eventInfos, [
"color" => $event->color()->value(),
"day" => $sessionDay,
"time" => $session->timeComplement()->isEmpty() == 'true' ? $session->time()->value() : $session->time()->value() . ' ' . $session->timeComplement()->value(),
"place" => $event->place()->value(),
"duration" => $event->duration()->value(),
"eventUrl" => $event->url(),
]);
}
}
if ($event->isMapadoEvent() == 'true') {
$request = createMapadoEventRequest($event);
$mapadoEvent = fetchMapadoEvent($request);
$duration = null;
$sessionsToSave = [];
foreach ($mapadoEvent->eventDateList as $session) {
$sessionMonth = (int) substr($session->startDate, 5, 2);
$sessionDay = intval(substr($session->startDate, 8, 2));
$startTime = substr($session->startDate, 11, 5);
$eventDateId = explode('/', $session->{'@id'});
$eventDateId = $eventDateId[count($eventDateId) - 1];
$ticketingUrl = option('ticketingUrl') . 'event/' . $mapadoEvent->slug . '?eventDate=' . $eventDateId;
$bookableStock = $session->notInStockContingentBookableStock;
if (isset($session->endDate) && !$duration) {
$endTime = substr($session->endDate, 11 , 5);
$duration = getTimeDifference($startTime, $endTime);
}
$calendar[$sessionMonth][$sessionDay]['sessions'][] = array_merge($eventInfos, [
"color" => $event->color()->value(),
"day" => $sessionDay,
"time" => str_replace(':', 'h', $startTime),
"duration" => $duration,
"place" => $event->place()->value(),
"eventUrl" => $event->url(),
"ticketingUrl" => $ticketingUrl,
"bookableStock" => $bookableStock,
"totalStock" => (int) $event->totalStock()->value(),
]);
}
saveMapadoEvent($mapadoEvent, $event);
}
}
return json_encode($calendar);
}
];