KV to D1 migration (this is totally gonna break something)
This commit is contained in:
@ -9,5 +9,13 @@ export async function onRequestGet(context: RequestContext) {
|
||||
|
||||
if (!success) return jsonError("Unable to retrieve appeals", 500);
|
||||
|
||||
return jsonResponse(JSON.stringify(results));
|
||||
return jsonResponse(
|
||||
JSON.stringify(
|
||||
results.map((result) => {
|
||||
result.user = JSON.parse(result.user as string);
|
||||
|
||||
return result;
|
||||
}),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
@ -6,19 +6,27 @@ export async function onRequestGet(context: RequestContext) {
|
||||
if (!["appeal", "inactivity", "report"].includes(type as string))
|
||||
return jsonError("Invalid type", 400);
|
||||
|
||||
const data = (await context.env.DATA.get(`${type}_${id}`, {
|
||||
type: "json",
|
||||
})) as {
|
||||
created_at: number;
|
||||
id: string;
|
||||
open: boolean;
|
||||
user?: { id: string; username: string };
|
||||
} & { [k: string]: any };
|
||||
const tables: { [k: string]: string } = {
|
||||
appeal: "appeals",
|
||||
inactivity: "inactivity_notices",
|
||||
report: "reports",
|
||||
};
|
||||
|
||||
if (data?.user?.id !== context.data.current_user.id)
|
||||
const data: Record<string, any> | null = await context.env.D1.prepare(
|
||||
`SELECT *
|
||||
FROM ${tables[type as string]}
|
||||
WHERE 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 === "report") {
|
||||
data.attachments = JSON.parse(data.attachments);
|
||||
const { AwsClient } = await import("aws4fetch");
|
||||
const aws = new AwsClient({
|
||||
accessKeyId: context.env.R2_ACCESS_KEY,
|
||||
|
@ -8,42 +8,12 @@ export async function onRequestGet(context: RequestContext) {
|
||||
results: { id: string }[];
|
||||
success: boolean;
|
||||
} = await context.env.D1.prepare(
|
||||
"SELECT id FROM reports WHERE user = ? ORDER BY created_at LIMIT 50;",
|
||||
"SELECT created_at, id, open, target_usernames FROM reports WHERE json_extract(user, '$.id') = ? ORDER BY created_at LIMIT 50;",
|
||||
)
|
||||
.bind(context.data.current_user.id)
|
||||
.all();
|
||||
|
||||
if (!success) return jsonError("Failed to retrieve reports", 500);
|
||||
|
||||
const ids: string[] = [];
|
||||
results.map((v) => ids.push(v.id));
|
||||
|
||||
const kvDataPromises = [];
|
||||
|
||||
for (const id of ids)
|
||||
kvDataPromises.push(context.env.DATA.get(`report_${id}`, { type: "json" }));
|
||||
|
||||
let settledKvPromises;
|
||||
|
||||
try {
|
||||
settledKvPromises = (await Promise.all(
|
||||
kvDataPromises,
|
||||
)) as ReportCardProps[];
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
return jsonError("Failed to resolve reports", 500);
|
||||
}
|
||||
|
||||
return jsonResponse(
|
||||
JSON.stringify(
|
||||
settledKvPromises.map((r) => {
|
||||
return {
|
||||
created_at: r.created_at,
|
||||
id: r.id,
|
||||
open: r.open,
|
||||
target_usernames: r.target_usernames,
|
||||
};
|
||||
}),
|
||||
),
|
||||
);
|
||||
return jsonResponse(JSON.stringify(results));
|
||||
}
|
||||
|
Reference in New Issue
Block a user