From 28ce455f4c1f41fff16541bced7057f9aa744fbc Mon Sep 17 00:00:00 2001 From: Kalle <38327916+Sendouc@users.noreply.github.com> Date: Sun, 21 Jul 2024 11:00:43 +0300 Subject: [PATCH] Clear tournament cache when avatar is approved --- app/features/img-upload/ImageRepository.server.ts | 14 ++++++++++++++ app/features/img-upload/routes/upload.admin.tsx | 15 ++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 app/features/img-upload/ImageRepository.server.ts 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; };