Use internally generated types for jsonobject and raw
This commit is contained in:
@@ -1,68 +1,71 @@
|
||||
import { jsonError, jsonResponse } from "../../../common.js";
|
||||
import { JsonObject } from "@prisma/client/runtime/client";
|
||||
|
||||
export async function onRequestGet(context: RequestContext) {
|
||||
const types: {
|
||||
[k: string]: { permissions: number[]; table: string };
|
||||
} = {
|
||||
appeal: {
|
||||
permissions: [1 << 0, 1 << 11],
|
||||
table: "appeals",
|
||||
},
|
||||
gma: {
|
||||
permissions: [1 << 5],
|
||||
table: "game_appeals",
|
||||
},
|
||||
inactivity: {
|
||||
permissions: [1 << 0, 1 << 4, 1 << 6, 1 << 7, 1 << 11],
|
||||
table: "inactivity_notices",
|
||||
},
|
||||
report: {
|
||||
permissions: [1 << 5],
|
||||
table: "reports",
|
||||
},
|
||||
const types: { [k: string]: number[] } = {
|
||||
appeal: [1 << 0, 1 << 11],
|
||||
gma: [1 << 5],
|
||||
inactivity: [1 << 0, 1 << 4, 1 << 6, 1 << 7, 1 << 11],
|
||||
report: [1 << 5],
|
||||
};
|
||||
|
||||
const type = context.params.type as string;
|
||||
const itemId = context.params.id as string;
|
||||
|
||||
if (
|
||||
!types[type]?.permissions.find(
|
||||
(p) => context.data.current_user.permissions & p,
|
||||
)
|
||||
)
|
||||
if (!types[type]?.find((p) => context.data.current_user.permissions & p))
|
||||
return jsonError("You cannot use this filter", 403);
|
||||
|
||||
const item: Record<string, any> | null = await context.env.D1.prepare(
|
||||
`SELECT *
|
||||
FROM ${types[type].table}
|
||||
WHERE id = ?;`,
|
||||
)
|
||||
.bind(itemId)
|
||||
.first();
|
||||
let item;
|
||||
|
||||
switch (type) {
|
||||
case "appeal":
|
||||
item = await context.data.prisma.appeal.findUnique({
|
||||
where: {
|
||||
id: itemId,
|
||||
},
|
||||
});
|
||||
|
||||
if (item) delete (item.user as JsonObject).email;
|
||||
break;
|
||||
|
||||
case "gma":
|
||||
item = await context.data.prisma.gameAppeal.findUnique({
|
||||
where: {
|
||||
id: itemId,
|
||||
},
|
||||
});
|
||||
break;
|
||||
|
||||
case "inactivity":
|
||||
item = await context.data.prisma.inactivityNotice.findUnique({
|
||||
where: {
|
||||
id: itemId,
|
||||
},
|
||||
});
|
||||
|
||||
if (item) delete (item.user as JsonObject).email;
|
||||
break;
|
||||
|
||||
case "report":
|
||||
item = await context.data.prisma.report.findUnique({
|
||||
where: {
|
||||
id: itemId,
|
||||
},
|
||||
});
|
||||
|
||||
if (item) delete (item.user as JsonObject).email;
|
||||
break;
|
||||
|
||||
default:
|
||||
return jsonError("Unknown filter", 400);
|
||||
}
|
||||
|
||||
if (!item) return jsonError("Item not found", 404);
|
||||
|
||||
if (type === "report") {
|
||||
if (await context.env.DATA.get(`reportprocessing_${itemId}`))
|
||||
return jsonError("Report is processing", 409);
|
||||
|
||||
item.attachments = JSON.parse(item.attachments);
|
||||
item.target_ids = JSON.parse(item.target_ids);
|
||||
item.target_usernames = JSON.parse(item.target_usernames);
|
||||
}
|
||||
|
||||
if (item.user) {
|
||||
item.user = JSON.parse(item.user);
|
||||
|
||||
delete item.user.email;
|
||||
}
|
||||
|
||||
if (type === "inactivity") {
|
||||
item.decisions = JSON.parse(item.decisions);
|
||||
item.departments = JSON.parse(item.departments);
|
||||
}
|
||||
|
||||
return item
|
||||
? jsonResponse(JSON.stringify(item))
|
||||
: jsonError("Not found", 404);
|
||||
return jsonResponse(JSON.stringify(item));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user