181 lines
4.3 KiB
JavaScript
181 lines
4.3 KiB
JavaScript
dayjs.locale("fr");
|
|
|
|
function getDatesInMonth(month) {
|
|
const year = dayjs().month(month).year();
|
|
const daysInMonth = dayjs(new Date(year, month, 0)).daysInMonth() + 1;
|
|
const dates = {};
|
|
|
|
for (let day = 1; day <= daysInMonth; day++) {
|
|
const currentDay = dayjs(new Date(year, month, day));
|
|
dates[parseInt(currentDay.format("DD"))] = [];
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
async function getMergedDates(monthNumb, dates) {
|
|
const month = dayjs().month(monthNumb).format("YYYY-MM");
|
|
const requestParams = {
|
|
month,
|
|
dates,
|
|
};
|
|
|
|
const requestOptions = {
|
|
method: "POST",
|
|
body: JSON.stringify(requestParams),
|
|
};
|
|
const response = await fetch("/month-dates.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");
|
|
}
|