Skip to content
Permalink
75402b3315
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
49 lines (41 sloc) 1.12 KB
import { jsonError, jsonResponse } from "../../common.js";
export async function onRequestGet(context: RequestContext) {
const {
results,
success,
}: {
results: { id: string }[];
success: boolean;
} = await context.env.D1.prepare(
"SELECT id FROM reports WHERE user = ? 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,
};
}),
),
);
}