Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Retrieve self-submitted reports endpoint
  • Loading branch information
regalijan committed Oct 19, 2023
1 parent ac56a62 commit 5c65424
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions functions/api/me/reports.ts
@@ -0,0 +1,49 @@
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,
};
}),
),
);
}

0 comments on commit 5c65424

Please sign in to comment.