Files
car-crushers-portal/functions/api/appeals/[id]/deny.ts
Regalijan 5457898ff9
All checks were successful
Test, Build, Deploy / Test, Build, and Deploy (push) Successful in 55s
Test, Build, Deploy / Create Sentry Release (push) Successful in 6s
Get almost everything else
2026-04-11 04:49:02 -04:00

71 lines
1.8 KiB
TypeScript

import { jsonError } from "../../../common.js";
import sendEmail from "../../../email.js";
import { sendPushNotification } from "../../../gcloud.js";
export async function onRequestPost(context: RequestContext) {
const { appeal, fcm_token } = context.data;
if (fcm_token) {
await sendPushNotification(
context.env,
"Appeal Denied",
`Unfortunately, we have decided to deny your appeal for the following reason: ${
context.data.body.feedback || "No additional details"
}`,
fcm_token,
);
await context.data.prisma.pushNotification.delete({
where: {
event_id: appeal.id,
event_type: "appeal",
},
});
} else {
const emailResponse = await sendEmail(
appeal.user.email,
context.env.MAILGUN_API_KEY,
"Appeal Denied",
"appeal_denied",
{
note: context.data.body.feedback || "No note provided.",
},
);
if (!emailResponse.ok) {
console.log(await emailResponse.json());
return jsonError("Failed to deny appeal", 500);
}
}
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;
await fetch(context.env.APPEALS_WEBHOOK, {
body: JSON.stringify({
embeds: [
{
title: "Appeal Denied",
color: 16711680,
description: `Appeal from user ${appeal.user.username} (${appeal.user.id}) was denied.`,
fields: [
{
name: "Moderator",
value: `${currentUser.username} (${currentUser.id})`,
},
],
},
],
}),
headers: {
"content-type": "application/json",
},
method: "POST",
});
return new Response(null, {
status: 204,
});
}