Global code cleanup

This commit is contained in:
NaNoMelo 2024-10-15 21:41:18 +02:00
parent f09827339f
commit 57afc84fdd
4 changed files with 56 additions and 55 deletions

View File

@ -16,9 +16,10 @@
from ninja import Query from ninja import Query
from ninja_extra import ControllerBase, api_controller, paginate, route from ninja_extra import ControllerBase, api_controller, paginate, route
from ninja_extra.pagination import PageNumberPaginationExtra from ninja_extra.pagination import PageNumberPaginationExtra
from ninja_extra.permissions import IsAuthenticated
from ninja_extra.schemas import PaginatedResponseSchema from ninja_extra.schemas import PaginatedResponseSchema
from core.api_permissions import CanView, IsOldSubscriber, IsRoot from core.api_permissions import CanView, IsRoot
from counter.models import Counter, Permanency from counter.models import Counter, Permanency
from counter.schemas import CounterSchema, PermanencyFilterSchema, PermanencySchema from counter.schemas import CounterSchema, PermanencyFilterSchema, PermanencySchema
@ -46,7 +47,7 @@ class PermanencyController(ControllerBase):
@route.get( @route.get(
"", "",
response=PaginatedResponseSchema[PermanencySchema], response=PaginatedResponseSchema[PermanencySchema],
permissions=[IsOldSubscriber], permissions=[IsAuthenticated],
exclude_none=True, exclude_none=True,
) )
@paginate(PageNumberPaginationExtra, page_size=100) @paginate(PageNumberPaginationExtra, page_size=100)

View File

@ -1,14 +1,13 @@
import { paginated } from "#core:utils/api"; import { paginated } from "#core:utils/api";
import { exportToHtml } from "#core:utils/globals"; import { exportToHtml } from "#core:utils/globals";
import { Calendar } from "@fullcalendar/core";
import timeGridPlugin from "@fullcalendar/timegrid";
import { import {
type PermanencyFetchPermananciesData, type PermanencyFetchPermananciesData,
type PermanencySchema, type PermanencySchema,
permanencyFetchPermanancies, permanencyFetchPermanancies,
} from "#openapi"; } from "#openapi";
import { Calendar } from "@fullcalendar/core";
import timeGridPlugin from "@fullcalendar/timegrid";
interface ActivityChartConfig { interface ActivityChartConfig {
canvas: HTMLCanvasElement; canvas: HTMLCanvasElement;
startDate: Date; startDate: Date;
@ -20,6 +19,14 @@ interface OpeningTime {
end: Date; end: Date;
} }
interface EventInput {
start: Date;
end: Date;
backgroundColor: string;
}
const _15minutes = 15 * 60 * 1000;
exportToHtml("loadChart", loadChart); exportToHtml("loadChart", loadChart);
async function loadChart(options: ActivityChartConfig) { async function loadChart(options: ActivityChartConfig) {
@ -27,7 +34,7 @@ async function loadChart(options: ActivityChartConfig) {
query: { query: {
counter: [options.counterId], counter: [options.counterId],
// biome-ignore lint/style/useNamingConvention: backend API uses snake_case // biome-ignore lint/style/useNamingConvention: backend API uses snake_case
start_date: options.startDate.toString(), start_date: options.startDate.toISOString(),
}, },
} as PermanencyFetchPermananciesData); } as PermanencyFetchPermananciesData);
@ -37,30 +44,14 @@ async function loadChart(options: ActivityChartConfig) {
plugins: [timeGridPlugin], plugins: [timeGridPlugin],
initialView: "timeGridWeek", initialView: "timeGridWeek",
locale: "fr", locale: "fr",
slotLabelFormat: { slotLabelFormat: { hour: "2-digit", minute: "2-digit", hour12: false },
hour: "2-digit", dayHeaderFormat: { weekday: "long" },
minute: "2-digit",
hour12: false,
},
dayHeaderFormat: {
weekday: "long",
},
firstDay: 1, firstDay: 1,
views: { views: { timeGrid: { allDaySlot: false } },
timeGrid: {
allDaySlot: false,
},
},
scrollTime: "09:00:00", scrollTime: "09:00:00",
headerToolbar: { headerToolbar: { left: "", center: "", right: "" },
left: "",
center: "",
right: "",
},
//weekends: false,
events: events, events: events,
nowIndicator: true, nowIndicator: true,
//slotDuration: "00:15:00",
height: 600, height: 600,
}); });
calendar.render(); calendar.render();
@ -79,10 +70,10 @@ function getOpeningTimes(rawPermanancies: PermanencySchema[]) {
openingTimes.push(permanancy); openingTimes.push(permanancy);
} else { } else {
const lastPermanancy = openingTimes[openingTimes.length - 1]; const lastPermanancy = openingTimes[openingTimes.length - 1];
// if the new permanancy starts before the 15 minutes following the end of the last one, merge them
if ( if (
// if the new permanancy starts before the 15 minutes following the end of the last one, merge them new Date(permanancy.start).getTime() <
new Date(permanancy.start).setMinutes(permanancy.start.getMinutes() - 15) < lastPermanancy.end.getTime() + _15minutes
lastPermanancy.end.getTime()
) { ) {
lastPermanancy.end = new Date( lastPermanancy.end = new Date(
Math.max(lastPermanancy.end.getTime(), permanancy.end.getTime()), Math.max(lastPermanancy.end.getTime(), permanancy.end.getTime()),
@ -96,35 +87,45 @@ function getOpeningTimes(rawPermanancies: PermanencySchema[]) {
} }
function convertPermanancyToOpeningTime(permanancy: PermanencySchema): OpeningTime { function convertPermanancyToOpeningTime(permanancy: PermanencySchema): OpeningTime {
return { const start = new Date(permanancy.start);
start: new Date(permanancy.start), let end = new Date(permanancy.end);
end: permanancy.end ? new Date(permanancy.end) : new Date(), if (end.getTime() - start.getTime() < _15minutes) {
}; end = new Date(start.getTime() + _15minutes);
} else {
end = new Date(permanancy.end);
}
return { start: start, end: end };
} }
function getEvents(permanancies: PermanencySchema[]) { function getEvents(permanancies: PermanencySchema[]) {
const openingTimes = getOpeningTimes(permanancies); const openingTimes = getOpeningTimes(permanancies);
const events = []; const events: EventInput[] = [];
for (const openingTime of openingTimes) { for (const openingTime of openingTimes) {
const lastMonday: Date = new Date(); const lastMonday = getLastMonday();
lastMonday.setDate(new Date().getDate() - ((new Date().getDay() - 1) % 7)); const shift = openingTime.end < lastMonday;
lastMonday.setHours(0, 0, 0); // if permanancies took place last week (=before monday),
// -> display them in lightblue as part of the current week
// if permanancies took place before monday (last week), display them in lightblue as part of the current week events.push({
if (openingTime.end < lastMonday) { start: shift ? shiftDateByDays(openingTime.start, 7) : openingTime.start,
events.push({ end: shift ? shiftDateByDays(openingTime.end, 7) : openingTime.end,
start: new Date(openingTime.start).setDate(openingTime.start.getDate() + 7), backgroundColor: shift ? "lightblue" : "green",
end: new Date(openingTime.end).setDate(openingTime.end.getDate() + 7), });
backgroundColor: "lightblue",
});
} else {
events.push({
start: openingTime.start,
end: openingTime.end,
backgroundColor: "green",
});
}
} }
//const openingTimesByDay = splitByDay(openingTimes);
return events; return events;
} }
// Function to get last Monday at 00:00
function getLastMonday(): Date {
const now = new Date();
const dayOfWeek = now.getDay();
const lastMonday = new Date(now);
lastMonday.setDate(now.getDate() - ((dayOfWeek + 6) % 7)); // Adjust for Monday as day 1
lastMonday.setHours(0, 0, 0, 0);
return lastMonday;
}
function shiftDateByDays(date: Date, days: number): Date {
const newDate = new Date(date);
newDate.setDate(date.getDate() + days);
return newDate;
}

View File

@ -50,7 +50,7 @@
window.addEventListener("DOMContentLoaded", () => { window.addEventListener("DOMContentLoaded", () => {
loadChart({ loadChart({
canvas: document.getElementById("activityGraph"), canvas: document.getElementById("activityGraph"),
startDate: new Date().setDate(new Date().getDate() - 7), startDate: new Date(new Date().setDate(new Date().getDate() - 7)),
counterId: {{ counter.id }}, counterId: {{ counter.id }},
}); });
}); });

View File

@ -1,4 +1,3 @@
// biome-ignore lint/correctness/noUndeclaredDependencies: webpack works with commonjs
const BundleAnalyzerPlugin = require("webpack-bundle-analyzer").BundleAnalyzerPlugin; const BundleAnalyzerPlugin = require("webpack-bundle-analyzer").BundleAnalyzerPlugin;
const config = require("./webpack.config.js"); const config = require("./webpack.config.js");