Global filters (#40)

This commit is contained in:
Timothy Armes
2023-07-05 17:27:53 +02:00
parent caaad4e229
commit 15ead0b877
11 changed files with 168 additions and 71 deletions
+24 -4
View File
@@ -1,19 +1,38 @@
import clientPromise from "@/lib/mongodb";
import { errorLog } from "@/utils/logger";
import { Tournament } from "@/types";
import { Tournament, TimeControl } from "@/types";
import TournamentsDisplay from "./TournamentsDisplay";
import { ObjectId } from "mongodb";
export const revalidate = 3600; // Revalidate cache every 6 hours
export interface TournamentData {
_id: ObjectId;
town: string;
department: string;
tournament: string;
url: string;
time_control: "Cadence Lente" | "Rapide" | "Blitz" | "1h KO";
date: string;
coordinates: [number, number];
}
const tcMap: Record<TournamentData["time_control"], TimeControl> = {
"Cadence Lente": TimeControl.Classic,
Rapide: TimeControl.Rapid,
Blitz: TimeControl.Blitz,
"1h KO": TimeControl.KO,
};
const getTournaments = async () => {
try {
const client = await clientPromise;
const db = client.db("tournamentsFranceDB");
const data = await db
.collection("tournaments")
.aggregate<Tournament>([
.aggregate<TournamentData>([
{
$addFields: {
dateParts: {
@@ -35,10 +54,11 @@ const getTournaments = async () => {
])
.toArray();
// We map the ObjectId to a string so that it can be serialized and sent to a client component
return data.map((t) => ({
return data.map<Tournament>((t) => ({
...t,
_id: t._id.toString(),
timeControl: tcMap[t.time_control],
latLng: { lat: t.coordinates[0], lng: t.coordinates[1] },
}));
} catch (error) {
errorLog(error);