mirror of
https://github.com/TheRealOwenRees/echecsfrance.git
synced 2026-07-23 04:26:57 +00:00
Global filters (#40)
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user