Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Retrieve self-submitted reports endpoint
- Loading branch information
Showing
1 changed file
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
}; | ||
}), | ||
), | ||
); | ||
} |