From 62a6c3a1f17e06a8a5113c7df72108441b6bce0f Mon Sep 17 00:00:00 2001 From: regalijan Date: Thu, 19 Oct 2023 16:49:46 -0400 Subject: [PATCH] Create user history endpoint --- functions/api/game-bans/[user]/history.ts | 53 +++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 functions/api/game-bans/[user]/history.ts diff --git a/functions/api/game-bans/[user]/history.ts b/functions/api/game-bans/[user]/history.ts new file mode 100644 index 0000000..d3bd765 --- /dev/null +++ b/functions/api/game-bans/[user]/history.ts @@ -0,0 +1,53 @@ +import { queryLogs } from "../../../gcloud.js"; + +export async function onRequestGet(context: RequestContext) { + const robloxUserReq = await fetch( + "https://users.roblox.com/v1/usernames/users", + { + body: JSON.stringify({ + excludeBannedUsers: false, + usernames: [context.params.user as string], + }), + headers: { + "content-type": "application/json", + }, + } + ); + + if (!robloxUserReq.ok) { + console.log(await robloxUserReq.json()); + return new Response('{"error":"Failed to resolve username"}', { + headers: { + "content-type": "application/json", + }, + status: 500, + }); + } + + const { data: users }: { data: { [k: string]: any }[] } = + await robloxUserReq.json(); + + if (!users.length) + return new Response('{"error":"No user found with that name"}', { + headers: { + "content-type": "application/json", + }, + status: 400, + }); + + return new Response( + JSON.stringify( + (await queryLogs(users[0].id, context)).sort((a, b) => + a.entity.properties.executed_at.integerValue > + b.entity.properties.executed_at.integerValue + ? 1 + : -1 + ) + ), + { + headers: { + "content-type": "application/json", + }, + } + ); +}