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