From 31bbdd8c1aa6e78e5cff66e2a051c38cc9886045 Mon Sep 17 00:00:00 2001 From: regalijan Date: Thu, 19 Oct 2023 16:50:55 -0400 Subject: [PATCH] Delete all data if all users are ignored --- functions/api/reports/[id]/action.ts | 36 +++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/functions/api/reports/[id]/action.ts b/functions/api/reports/[id]/action.ts index f9fe11c..ad96ea7 100644 --- a/functions/api/reports/[id]/action.ts +++ b/functions/api/reports/[id]/action.ts @@ -1,12 +1,46 @@ import { getBanList, setBanList } from "../../../roblox-open-cloud.js"; -import { insertLogs } from "../../../gcloud.js"; +import { GetAccessToken, insertLogs } from "../../../gcloud.js"; import { jsonError } from "../../../common.js"; export async function onRequestPost(context: RequestContext) { + const reportId = context.params.id as string; + const reportData: ReportCardProps | null = await context.env.DATA.get( + `report_${reportId}`, + { type: "json" }, + ); + + if (!reportData) return jsonError("Report does not exist", 404); + const actionMap = context.data.body; const newActions: { [k: string]: { BanType: number } } = {}; const logMap: { [k: string]: number } = {}; + if (!Object.values(actionMap).find((action) => action !== 0)) { + await context.env.DATA.delete(`report_${reportId}`); + await context.env.D1.prepare("DELETE FROM reports WHERE id = ?;") + .bind(reportId) + .run(); + const gcloudAccessToken = await GetAccessToken(context.env); + + for (const attachment of reportData.attachments) { + await fetch( + `https://storage.googleapis.com/storage/v1/b/portal-carcrushers-cc/o/${encodeURIComponent( + attachment, + )}`, + { + headers: { + authorization: `Bearer ${gcloudAccessToken}`, + }, + method: "DELETE", + }, + ); + } + + return new Response(null, { + status: 204, + }); + } + for (const [user, action] of Object.entries(actionMap)) { if ( isNaN(parseInt(user)) ||