mirror of
https://github.com/Drop-OSS/drop.git
synced 2026-08-24 15:22:05 +10:00
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>
This commit is contained in:
co-authored by
Robert Clabough
parent
8e75a0bf56
commit
97c6f2c8a6
@@ -0,0 +1,65 @@
|
||||
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 },
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user