diff --git a/functions/api/appeals/[id]/deny.ts b/functions/api/appeals/[id]/deny.ts index 4e958de..808667a 100644 --- a/functions/api/appeals/[id]/deny.ts +++ b/functions/api/appeals/[id]/deny.ts @@ -14,11 +14,12 @@ export async function onRequestPost(context: RequestContext) { fcm_token, ); - await context.env.D1.prepare( - "DELETE FROM push_notifications WHERE event_id = ? AND event_type = 'appeal';", - ) - .bind(appeal.id) - .run(); + await context.data.prisma.pushNotification.delete({ + where: { + event_id: appeal.id, + event_type: "appeal", + }, + }); } else { const emailResponse = await sendEmail( appeal.user.email, @@ -36,11 +37,8 @@ export async function onRequestPost(context: RequestContext) { } } - await context.env.D1.prepare( - "UPDATE appeals SET approved = 0, user = json_remove(user, '$.email') WHERE id = ?;", - ) - .bind(context.params.id) - .run(); + await context.data.prisma + .$executeRaw`UPDATE appeals SET approved = FALSE, user = json_remove(user, '$.id') WHERE id = ${appeal.id};`; const { current_user: currentUser } = context.data; diff --git a/functions/api/appeals/bans.ts b/functions/api/appeals/bans.ts index 14d84f6..b144d86 100644 --- a/functions/api/appeals/bans.ts +++ b/functions/api/appeals/bans.ts @@ -1,9 +1,11 @@ import { jsonResponse } from "../../common.js"; export async function onRequestGet(context: RequestContext) { - const { results } = await context.env.D1.prepare( - "SELECT * FROM appeal_bans ORDER BY created_by DESC;", - ).all(); + const results = await context.data.prisma.appealBan.findMany({ + orderBy: { + created_at: "desc", + }, + }); return jsonResponse(JSON.stringify(results)); } diff --git a/functions/api/me/items/[type]/[id].ts b/functions/api/me/items/[type]/[id].ts index c595b54..5147969 100644 --- a/functions/api/me/items/[type]/[id].ts +++ b/functions/api/me/items/[type]/[id].ts @@ -1,4 +1,8 @@ import { jsonError, jsonResponse } from "../../../../common.js"; +import { + type JsonArray, + type JsonObject, +} from "../../../../../generated/prisma/internal/prismaNamespace.js"; export async function onRequestGet(context: RequestContext) { const { id, type } = context.params; @@ -6,57 +10,65 @@ export async function onRequestGet(context: RequestContext) { if (!["appeal", "inactivity", "report"].includes(type as string)) return jsonError("Invalid type", 400); - const tables: { [k: string]: string } = { - appeal: "appeals", - inactivity: "inactivity_notices", - report: "reports", - }; + const { prisma } = context.data; + let item; - const data: Record | null = await context.env.D1.prepare( - `SELECT * - FROM ${tables[type as string]} - WHERE id = ?;`, + switch (type as string) { + case "appeal": + item = await prisma.appeal.findUnique({ + where: { + id: id as string, + }, + }); + + break; + + case "inactivity": + item = await prisma.inactivityNotice.findUnique({ + where: { + id: id as string, + }, + }); + + break; + case "report": + item = await prisma.report.findUnique({ + where: { + id: id as string, + }, + }); + + if (!item) break; + + const { AwsClient } = await import("aws4fetch"); + const aws = new AwsClient({ + accessKeyId: context.env.R2_ACCESS_KEY, + secretAccessKey: context.env.R2_SECRET_KEY, + }); + let urlPromises = []; + for (const attachment of item.attachments as JsonArray) { + urlPromises.push( + aws.sign( + `https://car-crushers.${context.env.R2_ZONE}.r2.cloudflarestorage.com/${attachment}?X-Amz-Expires=1800`, + ), + ); + } + const urls = (await Promise.all(urlPromises)).map((p) => p.url); + item = Object.defineProperty(item, "resolved_attachments", { + value: urls, + }); + + break; + + default: + break; + } + + if ( + !item || + (item.user as JsonObject | undefined)?.id !== context.data.current_user.id ) - .bind(id) - .first(); - - if (data?.user) data.user = JSON.parse(data.user); - - if (!data || data.user?.id !== context.data.current_user.id) return jsonError("Item does not exist", 404); - if (type === "inactivity") { - data.decisions = JSON.parse(data.decisions); - data.departments = JSON.parse(data.departments); - } - - if (type === "report") { - data.attachments = JSON.parse(data.attachments); - data.target_ids = JSON.parse(data.target_ids); - data.target_usernames = JSON.parse(data.target_usernames); - const { AwsClient } = await import("aws4fetch"); - const aws = new AwsClient({ - accessKeyId: context.env.R2_ACCESS_KEY, - secretAccessKey: context.env.R2_SECRET_KEY, - }); - - let urls = []; - - for (const attachment of data.attachments) { - const { url } = await aws.sign( - `https://car-crushers.${context.env.R2_ZONE}.r2.cloudflarestorage.com/${attachment}?X-Amz-Expires=1800`, - { - aws: { - signQuery: true, - }, - }, - ); - - urls.push(url); - } - - data.resolved_attachments = urls; - } - - return jsonResponse(JSON.stringify(data)); + return jsonResponse(JSON.stringify(item)); } diff --git a/functions/api/reports/[id]/action.ts b/functions/api/reports/[id]/action.ts index f0a12c9..7b7fb58 100644 --- a/functions/api/reports/[id]/action.ts +++ b/functions/api/reports/[id]/action.ts @@ -4,6 +4,7 @@ import sendEmail from "../../../email.js"; import { sendPushNotification } from "../../../gcloud.js"; export async function onRequestPost(context: RequestContext) { + const { prisma } = context.data; const reportId = context.params.id as string; const report: { [k: string]: any; @@ -83,12 +84,15 @@ export async function onRequestPost(context: RequestContext) { await setBanList(context, Object.assign(banList, newActions), etag); } - const pushNotificationData: Record | null = - await context.env.D1.prepare( - "SELECT token FROM push_notifications WHERE event_id = ? AND event_type = 'report';", - ) - .bind(reportId) - .first(); + const pushNotificationData = await prisma.pushNotification.findUnique({ + select: { + token: true, + }, + where: { + event_id: reportId, + event_type: "report", + }, + }); if (user?.email) await sendEmail( @@ -100,26 +104,33 @@ export async function onRequestPost(context: RequestContext) { username: user.username as string, }, ); - else if (pushNotificationData) + else if (pushNotificationData) { await sendPushNotification( context.env, "Report Processed", `Your report for ${JSON.parse(report.target_usernames).toString()} has been reviewed.`, pushNotificationData.token, ); + await prisma.pushNotification.delete({ + where: { + event_id: reportId, + event_type: "report", + }, + }); + } delete (report.user as { email?: string; id: string; username: string }) ?.email; - await context.env.D1.prepare("UPDATE reports SET open = 0 WHERE id = ?;") - .bind(reportId) - .run(); - - await context.env.D1.prepare( - "DELETE FROM push_notifications WHERE event_id = ? AND event_type = 'report';", - ) - .bind(reportId) - .run(); + await prisma.report.update({ + data: { + open: false, + user: report.user, + }, + where: { + id: reportId, + }, + }); return new Response(null, { status: 204,