Filter and report bad tournament

This commit is contained in:
Timothy Armes
2023-10-22 14:31:53 +02:00
parent 3a50d34aaf
commit 30da984a97
2 changed files with 60 additions and 20 deletions
+41 -5
View File
@@ -3,7 +3,7 @@ import { groupBy } from "lodash";
import clientPromise from "@/lib/mongodb"; import clientPromise from "@/lib/mongodb";
import { TournamentData } from "@/types"; import { TournamentData } from "@/types";
import { TimeControl, Tournament, tcMap } from "@/types"; import { TimeControl, Tournament, tcMap, tournamentDataSchema } from "@/types";
import { errorLog } from "@/utils/logger"; import { errorLog } from "@/utils/logger";
import TournamentsDisplay from "./TournamentsDisplay"; import TournamentsDisplay from "./TournamentsDisplay";
@@ -43,9 +43,45 @@ const getTournaments = async () => {
]) ])
.toArray(); .toArray();
const bad = data.filter((t) => {
const result = tournamentDataSchema.safeParse(t);
if (result.success === false) {
console.log(JSON.stringify(result, null, 2));
console.log(JSON.stringify(t, null, 2));
if (typeof process.env.DISCORD_WEBHOOK_ERROR_LOGS_URL === "string") {
fetch(process.env.DISCORD_WEBHOOK_ERROR_LOGS_URL as string, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
embeds: [
{
title: "Error parsing tournaments",
fields: [
{
name: "parseError",
value: JSON.stringify(result, null, 2),
},
{ name: "tournament", value: JSON.stringify(t, null, 2) },
],
},
],
}),
});
}
}
return result.success === false;
});
const badIds = bad.map((t) => t._id.toString());
const goodData = data.filter((t) => !badIds.includes(t._id.toString()));
// Group the tournaments by their location // Group the tournaments by their location
const groupedByLocation = groupBy( const groupedByLocation = groupBy(
data, goodData,
(t) => `${t.coordinates[0]}_${t.coordinates[1]}`, (t) => `${t.coordinates[0]}_${t.coordinates[1]}`,
); );
@@ -73,7 +109,7 @@ const getTournaments = async () => {
dateRangesByLocation[location] = dateRanges; dateRangesByLocation[location] = dateRanges;
} }
return data.map<Tournament>((t) => { return goodData.map<Tournament>((t) => {
const location = `${t.coordinates[0]}_${t.coordinates[1]}`; const location = `${t.coordinates[0]}_${t.coordinates[1]}`;
const date = parse(t.date, "dd/MM/yyyy", new Date()); const date = parse(t.date, "dd/MM/yyyy", new Date());
const dateRanges = dateRangesByLocation[location]; const dateRanges = dateRangesByLocation[location];
@@ -97,8 +133,8 @@ const getTournaments = async () => {
url: t.url, url: t.url,
timeControl, timeControl,
latLng: { lat: t.coordinates[0], lng: t.coordinates[1] }, latLng: { lat: t.coordinates[0], lng: t.coordinates[1] },
norm: t.norm_tournament, norm: t.norm_tournament ?? false,
pending: t.pending, pending: t.pending ?? false,
status: t.status, status: t.status,
}; };
}); });
+19 -15
View File
@@ -1,23 +1,27 @@
import { LatLngLiteral } from "leaflet"; import { LatLngLiteral } from "leaflet";
import { ObjectId } from "mongodb"; import { ObjectId } from "mongodb";
import { z } from "zod";
export type Status = "scheduled" | "ongoing" | "finished"; export type Status = "scheduled" | "ongoing" | "finished" | "in-play";
export type TournamentData = { export const tournamentDataSchema = z.object({
tournament_id: z.string(),
town: z.string(),
department: z.string(),
country: z.string(),
tournament: z.string(),
url: z.string(),
time_control: z.string(),
norm_tournament: z.boolean().optional(),
date: z.string(),
coordinates: z.array(z.number()).min(2).max(2),
entry_method: z.enum(["manual", "auto"]),
pending: z.boolean().optional(),
status: z.enum(["scheduled", "ongoing", "finished", "in-play"]),
});
export type TournamentData = z.infer<typeof tournamentDataSchema> & {
_id: ObjectId; _id: ObjectId;
tournament_id: string;
town: string;
department: string;
country: string;
tournament: string;
url: string;
time_control: "Cadence Lente" | "Rapide" | "Blitz" | string;
norm_tournament: boolean;
date: string;
coordinates: [number, number];
entry_method: "manual" | "auto";
pending: boolean;
status: Status;
}; };
export type ClubData = { export type ClubData = {