Migrate mod queue list endpoint
Some checks failed
Test, Build, Deploy / Test, Build, and Deploy (push) Failing after 50s
Test, Build, Deploy / Create Sentry Release (push) Has been skipped

This commit is contained in:
2026-04-11 04:36:25 -04:00
parent 5c17f87f89
commit f184389ffd

View File

@@ -1,7 +1,10 @@
import { jsonError, jsonResponse } from "../../../common.js";
import { InactivityNotice } from "../../../../generated/prisma/client.js";
import { type JsonObject, raw } from "@prisma/client/runtime/client";
export async function onRequestGet(context: RequestContext): Promise<any> {
const type = context.params.type as string;
const { prisma } = context.data;
const { searchParams } = new URL(context.request.url);
const before = parseInt(searchParams.get("before") || `${Date.now()}`);
const showClosed = searchParams.get("showClosed") === "true";
@@ -26,73 +29,72 @@ export async function onRequestGet(context: RequestContext): Promise<any> {
if (isNaN(before)) return jsonError("Invalid `before` parameter", 400);
let rows: D1Result<Record<string, any>>;
let rows;
switch (type) {
case "appeal":
rows = await context.env.D1.prepare(
`SELECT *
FROM appeals
WHERE created_at < ?
AND approved ${showClosed ? "IS NOT" : "IS"} NULL
ORDER BY created_at DESC LIMIT 25;`,
)
.bind(before)
.all();
rows.results = rows.results.map((r) => {
r.user = JSON.parse(r.user);
delete r.user.email;
rows = await prisma.appeal.findMany({
orderBy: {
created_at: "desc",
},
take: 25,
where: {
created_at: {
lt: new Date(before),
},
approved: showClosed ? { not: null } : null,
},
});
rows.map((r) => {
delete (r.user as JsonObject).email;
return r;
});
break;
case "gma":
rows = await context.env.D1.prepare(
"SELECT * FROM game_appeals WHERE created_at < ? ORDER BY created_at DESC LIMIT 25;",
)
.bind(before)
.all();
rows = await prisma.gameAppeal.findMany({
orderBy: {
created_at: "desc",
},
take: 25,
where: {
created_at: {
lt: new Date(before),
},
},
});
break;
case "inactivity":
rows = await context.env.D1.prepare(
`SELECT *,
(SELECT COUNT(*) FROM json_each(decisions)) as decision_count
FROM inactivity_notices
WHERE created_at < ?
AND decision_count ${showClosed ? "=" : "!="} json_array_length(departments)`,
)
.bind(before)
.all();
rows = await prisma.$queryRaw<
InactivityNotice[] & { decision_count: number }[]
>`
SELECT *, (SELECT COUNT(*) FROM json_each(decisions)) AS decision_count FROM inactivity_notices WHERE created_at < datetime(${before} / 1000, 'unixepoch') AND decision_count ${showClosed ? raw("=") : raw("!=")} json_array_length(departments);`;
rows.results.map((r) => {
r.decisions = JSON.parse(r.decisions);
r.departments = JSON.parse(r.departments);
r.user = JSON.parse(r.user);
delete r.user.email;
rows.map((r) => {
delete (r.user as JsonObject).email;
return r;
});
break;
case "report":
rows = await context.env.D1.prepare(
"SELECT * FROM reports WHERE created_at < ? AND open = ? ORDER BY created_at DESC LIMIT 25;",
)
.bind(before, !Number(showClosed))
.all();
rows = await prisma.report.findMany({
orderBy: {
created_at: "desc",
},
take: 25,
where: {
created_at: {
lt: new Date(before),
},
open: !showClosed,
},
});
rows.results = rows.results.map((r) => {
r.attachments = JSON.parse(r.attachments);
r.target_ids = JSON.parse(r.target_ids);
r.target_usernames = JSON.parse(r.target_usernames);
if (r.user) {
r.user = JSON.parse(r.user);
delete r.user.email;
}
rows.map((r) => {
delete (r.user as JsonObject).email;
return r;
});
@@ -103,5 +105,5 @@ export async function onRequestGet(context: RequestContext): Promise<any> {
return jsonError("Unknown filter error", 500);
}
return jsonResponse(JSON.stringify(rows.results));
return jsonResponse(JSON.stringify(rows));
}