Files
drop/server/server/api/v1/admin/game/[id]/age-ratings.patch.ts
T
AgentScrubblesandRobert Clabough 97c6f2c8a6 Add age ratings (#451)
* Base age rating system

* Cleanup enum leftovers

* Missed files

* GB importer

* Linter settings

* Translations

* Prettier and linting

---------

Co-authored-by: Robert Clabough <robert@clabough.tech>
2026-07-26 11:42:16 +10:00

66 lines
1.9 KiB
TypeScript

import { type } from "arktype";
import type { AgeRatingOrganization } from "~/prisma/client/enums";
import { readDropValidatedBody, throwingArktype } from "~/server/arktype";
import aclManager from "~/server/internal/acls";
import prisma from "~/server/internal/db/database";
import {
getAvailableRatings,
RATINGS_FOR_ORGANIZATION,
} from "~/utils/ageRatings";
const PatchAgeRatings = type({
ageRatings: type({
organization: "string",
rating: "string",
}).array(),
}).configure(throwingArktype);
export default defineEventHandler(async (h3) => {
const allowed = await aclManager.allowSystemACL(h3, ["game:update"]);
if (!allowed) throw createError({ statusCode: 403 });
const body = await readDropValidatedBody(h3, PatchAgeRatings);
const id = getRouterParam(h3, "id")!;
for (const ar of body.ageRatings) {
if (!(ar.organization in RATINGS_FOR_ORGANIZATION)) {
throw createError({
statusCode: 400,
message: `Invalid organization: ${ar.organization}`,
});
}
const validRatings = getAvailableRatings(
ar.organization as AgeRatingOrganization,
);
if (!validRatings.includes(ar.rating)) {
throw createError({
statusCode: 400,
message: `Invalid rating "${ar.rating}" for ${ar.organization}`,
});
}
}
const game = await prisma.game.findUnique({
where: { id },
select: { id: true },
});
if (!game) throw createError({ statusCode: 404, message: "Game not found" });
await prisma.$transaction([
prisma.gameAgeRating.deleteMany({
where: { gameId: id },
}),
prisma.gameAgeRating.createMany({
data: body.ageRatings.map((ar) => ({
gameId: id,
organization: ar.organization as AgeRatingOrganization,
rating: ar.rating,
})),
}),
]);
return await prisma.gameAgeRating.findMany({
where: { gameId: id },
});
});