Retrieve self-submitted reports endpoint
This commit is contained in:
parent
ac56a62a5d
commit
5c65424a04
49
functions/api/me/reports.ts
Normal file
49
functions/api/me/reports.ts
Normal file
@ -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,
|
||||||
|
};
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user