Get almost everything else
This commit is contained in:
@@ -1,4 +1,8 @@
|
||||
import { jsonError, jsonResponse } from "../../../../common.js";
|
||||
import {
|
||||
type JsonArray,
|
||||
type JsonObject,
|
||||
} from "../../../../../generated/prisma/internal/prismaNamespace.js";
|
||||
|
||||
export async function onRequestGet(context: RequestContext) {
|
||||
const { id, type } = context.params;
|
||||
@@ -6,57 +10,65 @@ export async function onRequestGet(context: RequestContext) {
|
||||
if (!["appeal", "inactivity", "report"].includes(type as string))
|
||||
return jsonError("Invalid type", 400);
|
||||
|
||||
const tables: { [k: string]: string } = {
|
||||
appeal: "appeals",
|
||||
inactivity: "inactivity_notices",
|
||||
report: "reports",
|
||||
};
|
||||
const { prisma } = context.data;
|
||||
let item;
|
||||
|
||||
const data: Record<string, any> | null = await context.env.D1.prepare(
|
||||
`SELECT *
|
||||
FROM ${tables[type as string]}
|
||||
WHERE id = ?;`,
|
||||
switch (type as string) {
|
||||
case "appeal":
|
||||
item = await prisma.appeal.findUnique({
|
||||
where: {
|
||||
id: id as string,
|
||||
},
|
||||
});
|
||||
|
||||
break;
|
||||
|
||||
case "inactivity":
|
||||
item = await prisma.inactivityNotice.findUnique({
|
||||
where: {
|
||||
id: id as string,
|
||||
},
|
||||
});
|
||||
|
||||
break;
|
||||
case "report":
|
||||
item = await prisma.report.findUnique({
|
||||
where: {
|
||||
id: id as string,
|
||||
},
|
||||
});
|
||||
|
||||
if (!item) break;
|
||||
|
||||
const { AwsClient } = await import("aws4fetch");
|
||||
const aws = new AwsClient({
|
||||
accessKeyId: context.env.R2_ACCESS_KEY,
|
||||
secretAccessKey: context.env.R2_SECRET_KEY,
|
||||
});
|
||||
let urlPromises = [];
|
||||
for (const attachment of item.attachments as JsonArray) {
|
||||
urlPromises.push(
|
||||
aws.sign(
|
||||
`https://car-crushers.${context.env.R2_ZONE}.r2.cloudflarestorage.com/${attachment}?X-Amz-Expires=1800`,
|
||||
),
|
||||
);
|
||||
}
|
||||
const urls = (await Promise.all(urlPromises)).map((p) => p.url);
|
||||
item = Object.defineProperty(item, "resolved_attachments", {
|
||||
value: urls,
|
||||
});
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if (
|
||||
!item ||
|
||||
(item.user as JsonObject | undefined)?.id !== context.data.current_user.id
|
||||
)
|
||||
.bind(id)
|
||||
.first();
|
||||
|
||||
if (data?.user) data.user = JSON.parse(data.user);
|
||||
|
||||
if (!data || data.user?.id !== context.data.current_user.id)
|
||||
return jsonError("Item does not exist", 404);
|
||||
|
||||
if (type === "inactivity") {
|
||||
data.decisions = JSON.parse(data.decisions);
|
||||
data.departments = JSON.parse(data.departments);
|
||||
}
|
||||
|
||||
if (type === "report") {
|
||||
data.attachments = JSON.parse(data.attachments);
|
||||
data.target_ids = JSON.parse(data.target_ids);
|
||||
data.target_usernames = JSON.parse(data.target_usernames);
|
||||
const { AwsClient } = await import("aws4fetch");
|
||||
const aws = new AwsClient({
|
||||
accessKeyId: context.env.R2_ACCESS_KEY,
|
||||
secretAccessKey: context.env.R2_SECRET_KEY,
|
||||
});
|
||||
|
||||
let urls = [];
|
||||
|
||||
for (const attachment of data.attachments) {
|
||||
const { url } = await aws.sign(
|
||||
`https://car-crushers.${context.env.R2_ZONE}.r2.cloudflarestorage.com/${attachment}?X-Amz-Expires=1800`,
|
||||
{
|
||||
aws: {
|
||||
signQuery: true,
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
urls.push(url);
|
||||
}
|
||||
|
||||
data.resolved_attachments = urls;
|
||||
}
|
||||
|
||||
return jsonResponse(JSON.stringify(data));
|
||||
return jsonResponse(JSON.stringify(item));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user