222 lines
5.3 KiB
JavaScript
222 lines
5.3 KiB
JavaScript
dayjs.locale("fr");
|
|
|
|
function getDatesInMonth(month) {
|
|
const year = dayjs().month(month).year();
|
|
const daysInMonth = dayjs(new Date(year, month, 0)).daysInMonth();
|
|
const dates = {};
|
|
|
|
for (let day = 1; day <= daysInMonth; day++) {
|
|
const currentDay = dayjs(new Date(year, month, day));
|
|
const dayInitial = currentDay.format("dd")[0].toUpperCase(); // Get the initial of the day in French
|
|
dates[parseInt(currentDay.format("DD"))] = {
|
|
sessions: [],
|
|
full: currentDay.format("DD-MM-YYYY"),
|
|
initial: dayInitial,
|
|
};
|
|
}
|
|
|
|
return dates;
|
|
}
|
|
|
|
async function getMapadoEvent(id) {
|
|
const requestEndPoint = "ticketings/" + id;
|
|
|
|
const requestParams = [];
|
|
|
|
const requestFields = [
|
|
{ name: "startDate" },
|
|
{ name: "bookableStock" },
|
|
{
|
|
name: "eventDateList",
|
|
subfields: [{ name: "@id" }, { name: "bookableStock" }],
|
|
},
|
|
];
|
|
|
|
const requestOptions = {
|
|
method: "POST",
|
|
body: JSON.stringify({ requestEndPoint, requestParams, requestFields }),
|
|
};
|
|
|
|
const response = await fetch("/mapado-api.json", requestOptions);
|
|
const json = await response.json();
|
|
|
|
const event = json["hydra:member"];
|
|
|
|
console.log(`Événement Mapado`, event);
|
|
}
|
|
|
|
async function getMapadoDates(monthNumb) {
|
|
const contractId = "1941";
|
|
|
|
const firstDayOfMonth = dayjs()
|
|
.month(monthNumb)
|
|
.startOf("month")
|
|
.format("YYYY-MM-DD");
|
|
const firstDayOfNextMonth = dayjs(firstDayOfMonth)
|
|
.add(1, "month")
|
|
.format("YYYY-MM-DD");
|
|
|
|
const requestEndPoint = "event_dates";
|
|
const requestParams = [
|
|
{ name: "itemsPerPage", value: 100 },
|
|
{ name: "contract", value: contractId },
|
|
{ name: "after", value: firstDayOfMonth },
|
|
{ name: "before", value: firstDayOfNextMonth },
|
|
{ name: "order", value: "asc" },
|
|
];
|
|
|
|
const requestFields = [
|
|
{ name: "startDate" },
|
|
{ name: "bookableStock" },
|
|
{
|
|
name: "ticketing",
|
|
subfields: [
|
|
{ name: "@id" },
|
|
{ name: "title" },
|
|
{ name: "slug" },
|
|
{
|
|
name: "venue",
|
|
subfields: [
|
|
{ name: "@id" },
|
|
{ name: "address" },
|
|
{ name: "zipCode" },
|
|
{ name: "city" },
|
|
{ name: "countryCode" },
|
|
{ name: "timezone" },
|
|
],
|
|
},
|
|
],
|
|
},
|
|
];
|
|
|
|
const requestOptions = {
|
|
method: "POST",
|
|
body: JSON.stringify({ requestEndPoint, requestParams, requestFields }),
|
|
};
|
|
|
|
const response = await fetch("/mapado-api.json", requestOptions);
|
|
const json = await response.json();
|
|
|
|
const eventDates = json["hydra:member"];
|
|
|
|
console.log(
|
|
`Événements Mapado de ${dayjs(firstDayOfMonth).format("MMMM")} ${dayjs(
|
|
firstDayOfMonth
|
|
).format("YYYY")}`,
|
|
eventDates
|
|
);
|
|
|
|
const mapadoDates = eventDates.map((eventDate) => {
|
|
return {
|
|
day: getDay(eventDate),
|
|
title: eventDate.ticketing.title,
|
|
startTime: getStartTime(eventDate),
|
|
ticketingUrl: getTicketingUrl(eventDate),
|
|
id: getId(eventDate),
|
|
};
|
|
});
|
|
|
|
return mapadoDates;
|
|
}
|
|
|
|
function getDay(eventDate) {
|
|
const date = eventDate.startDate.slice(0, 10);
|
|
const splittedDate = date.split("-");
|
|
const day = splittedDate[2];
|
|
return day;
|
|
}
|
|
|
|
function getStartTime(eventDate) {
|
|
const rawTime = eventDate.startDate.slice(11, 19);
|
|
const time = dayjs(`2000-01-01T${rawTime}`);
|
|
const formattedTime = time.format("HH[h]mm");
|
|
return formattedTime;
|
|
}
|
|
|
|
function getTicketingUrl(eventDate) {
|
|
const baseUrl = "https://cdn-besancon.mapado.com/event/";
|
|
return baseUrl + eventDate.ticketing.slug;
|
|
}
|
|
|
|
function getId(eventData) {
|
|
const endpoint = eventData.ticketing["@id"];
|
|
const splitString = endpoint.split("/");
|
|
const id = parseInt(splitString[3]);
|
|
|
|
return id;
|
|
}
|
|
|
|
function createEmptyCalendar() {
|
|
const calendar = {};
|
|
|
|
for (let month = 9; month < 21; month++) {
|
|
const normalizedMonth = ((month - 1) % 12) + 1;
|
|
|
|
const daysInMonth = dayjs()
|
|
.month(normalizedMonth - 1)
|
|
.daysInMonth();
|
|
|
|
calendar[normalizedMonth] = {};
|
|
|
|
for (let day = 1; day <= daysInMonth; day++) {
|
|
const currentDay = dayjs()
|
|
.month(normalizedMonth - 1)
|
|
.date(day);
|
|
|
|
const dayInitial = currentDay.format("dd")[0].toUpperCase();
|
|
|
|
calendar[normalizedMonth][day] = {
|
|
sessions: [],
|
|
full: currentDay.format("DD-MM-YYYY"),
|
|
initial: dayInitial,
|
|
index: parseInt(currentDay.format("DD")),
|
|
indexInWeek: currentDay.day() === 0 ? 7 : currentDay.day(),
|
|
};
|
|
}
|
|
}
|
|
|
|
return calendar;
|
|
}
|
|
|
|
async function getCurrentSeasonCalendar(monthNumb, dates) {
|
|
const calendar = createEmptyCalendar();
|
|
|
|
const requestParams = {
|
|
calendar,
|
|
};
|
|
|
|
const requestOptions = {
|
|
method: "POST",
|
|
body: JSON.stringify(requestParams),
|
|
};
|
|
|
|
const response = await fetch(
|
|
"/get-current-season-calendar.json",
|
|
requestOptions
|
|
);
|
|
const json = await response.json();
|
|
return json;
|
|
}
|
|
|
|
async function updateMapadoEvent(pageUri) {
|
|
const headers = new Headers();
|
|
|
|
const requestOptions = {
|
|
method: "POST",
|
|
headers: headers,
|
|
redirect: "follow",
|
|
body: JSON.stringify({
|
|
pageUri,
|
|
}),
|
|
};
|
|
|
|
const response = await fetch("/update-mapado-event.json", requestOptions);
|
|
const json = await response.json();
|
|
console.log("Séance récupérées de Mapado", json);
|
|
return json;
|
|
}
|
|
|
|
function dateToFrench(date) {
|
|
date = dayjs(date);
|
|
return date.format("D MMMM YYYY");
|
|
}
|