From e5e0d055c664f6a18887983f90654fc5aeedebb1 Mon Sep 17 00:00:00 2001 From: Soren Zaiser Date: Sun, 15 Sep 2024 03:00:32 -0400 Subject: [PATCH] final updates for this branch --- back-end/api/src/controllers/Match.ts | 11 ++++++-- back-end/api/src/util/Errors.ts | 5 ++++ .../apps/scorekeeper/hooks/use-prestart.ts | 27 ++++++++++++++++--- 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/back-end/api/src/controllers/Match.ts b/back-end/api/src/controllers/Match.ts index d5dd396a..ae0769ee 100644 --- a/back-end/api/src/controllers/Match.ts +++ b/back-end/api/src/controllers/Match.ts @@ -10,7 +10,7 @@ import { } from '@toa-lib/models'; import { NextFunction, Response, Request, Router } from 'express'; import { validateBodyZ } from '../middleware/BodyValidator.js'; -import { DataNotFoundError } from '../util/Errors.js'; +import { DataNotFoundError, InvalidDataError } from '../util/Errors.js'; import { join } from 'path'; import { writeFile } from 'fs/promises'; import { @@ -159,7 +159,6 @@ router.get( res.send(match); } catch (e) { - console.log(e); return next(e); } } @@ -245,6 +244,14 @@ router.patch( const funcs = getFunctionsBySeasonKey( eventKey.split('-')[0].toLowerCase() ); + // Ensure they are actually updating what they say they're updating + if ( + req.body.eventKey !== eventKey || + req.body.tournamentKey !== tournamentKey || + String(req.body.id) !== id + ) { + return next(InvalidDataError); + } const data = funcs?.detailsToJson ? funcs.detailsToJson(req.body) : req.body; diff --git a/back-end/api/src/util/Errors.ts b/back-end/api/src/util/Errors.ts index e2a45393..7318546a 100644 --- a/back-end/api/src/util/Errors.ts +++ b/back-end/api/src/util/Errors.ts @@ -41,6 +41,11 @@ export const DataNotFoundError: ApiError = { message: 'Data requested was not found' }; +export const InvalidDataError: ApiError = { + code: 400, + message: 'The body sent does not match the route parameters. Please ensure the body matches the route.' +}; + export const RouteNotFound: ApiError = { code: 404, message: 'Route not found.' diff --git a/front-end/src/apps/scorekeeper/hooks/use-prestart.ts b/front-end/src/apps/scorekeeper/hooks/use-prestart.ts index 025766db..e4f8ec82 100644 --- a/front-end/src/apps/scorekeeper/hooks/use-prestart.ts +++ b/front-end/src/apps/scorekeeper/hooks/use-prestart.ts @@ -1,17 +1,23 @@ import { useRecoilCallback } from 'recoil'; import { useMatchControl } from './use-match-control'; -import { MatchSocketEvent, MatchState } from '@toa-lib/models'; +import { + getDefaultMatchDetailsBySeasonKey, + getFunctionsBySeasonKey, + MatchSocketEvent, + MatchState +} from '@toa-lib/models'; import { matchOccurringAtom, socketConnectedAtom } from 'src/stores/recoil'; import { patchMatch, patchMatchParticipants } from 'src/api/use-match-data'; import { DateTime } from 'luxon'; import { once, sendPrestart, sendUpdate } from 'src/api/use-socket'; import { useSeasonFieldControl } from 'src/hooks/use-season-components'; +import { c } from 'vite/dist/node/types.d-aGj9QkWt'; export const usePrestartCallback = () => { const { canPrestart, setState } = useMatchControl(); const fieldControl = useSeasonFieldControl(); return useRecoilCallback( - ({ snapshot }) => + ({ snapshot, set }) => async () => { const match = await snapshot.getPromise(matchOccurringAtom); const socketConnected = await snapshot.getPromise(socketConnectedAtom); @@ -36,9 +42,24 @@ export const usePrestartCallback = () => { { eventKey, tournamentKey, id }, match.participants ); + let currentMatch = match; + if (!match.details) { + currentMatch = { + ...match, + details: { + ...getDefaultMatchDetailsBySeasonKey( + match.eventKey.split('-')[0].toLowerCase() + ), + tournamentKey: match.tournamentKey, + eventKey: match.eventKey, + id: match.id + } + }; + set(matchOccurringAtom, currentMatch); + } fieldControl?.prestartField?.(); // Once we recieve the prestart response, immediately send update to load socket with match - once(MatchSocketEvent.PRESTART, () => sendUpdate(match)); + once(MatchSocketEvent.PRESTART, () => sendUpdate(currentMatch)); // Send prestart to server sendPrestart({ eventKey, tournamentKey, id }); setState(MatchState.PRESTART_COMPLETE);