diff --git a/app/features/img-upload/ImageRepository.server.ts b/app/features/img-upload/ImageRepository.server.ts new file mode 100644 index 000000000..f65b4c7c5 --- /dev/null +++ b/app/features/img-upload/ImageRepository.server.ts @@ -0,0 +1,14 @@ +import { db } from "~/db/sql"; + +export function findById(id: number) { + return db + .selectFrom("UnvalidatedUserSubmittedImage") + .leftJoin( + "CalendarEvent", + "CalendarEvent.avatarImgId", + "UnvalidatedUserSubmittedImage.id", + ) + .select(["CalendarEvent.tournamentId"]) + .where("UnvalidatedUserSubmittedImage.id", "=", id) + .executeTakeFirst(); +} diff --git a/app/features/img-upload/routes/upload.admin.tsx b/app/features/img-upload/routes/upload.admin.tsx index 8baadacfc..ceeaf7734 100644 --- a/app/features/img-upload/routes/upload.admin.tsx +++ b/app/features/img-upload/routes/upload.admin.tsx @@ -3,9 +3,16 @@ import { Form, useLoaderData } from "@remix-run/react"; import { Main } from "~/components/Main"; import { SubmitButton } from "~/components/SubmitButton"; import { requireUserId } from "~/features/auth/core/user.server"; +import { clearTournamentDataCache } from "~/features/tournament-bracket/core/Tournament.server"; import { isMod } from "~/permissions"; -import { notFoundIfFalsy, parseRequestFormData, validate } from "~/utils/remix"; +import { + badRequestIfFalsy, + notFoundIfFalsy, + parseRequestFormData, + validate, +} from "~/utils/remix"; import { userSubmittedImage } from "~/utils/urls"; +import * as ImageRepository from "../ImageRepository.server"; import { countAllUnvalidatedImg } from "../queries/countAllUnvalidatedImg.server"; import { oneUnvalidatedImage } from "../queries/oneUnvalidatedImage"; import { validateImage } from "../queries/validateImage"; @@ -20,8 +27,14 @@ export const action: ActionFunction = async ({ request }) => { validate(isMod(user), "Only admins can validate images"); + const image = badRequestIfFalsy(await ImageRepository.findById(data.imageId)); + validateImage(data.imageId); + if (image.tournamentId) { + clearTournamentDataCache(image.tournamentId); + } + return null; };