Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
car-crushers-portal/functions/api/appeals/[id]/accept.ts
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
83 lines (73 sloc)
2.08 KB
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
import { jsonError } from "../../../common.js"; | |
import sendEmail from "../../../email.js"; | |
import { sendPushNotification } from "../../../gcloud.js"; | |
export async function onRequestPost(context: RequestContext) { | |
const { appeal } = context.data; | |
if (appeal.fcm_token) { | |
await sendPushNotification( | |
context.env, | |
"Appeal Accepted", | |
context.data.body.feedback || "No additional details to display", | |
appeal.fcm_token, | |
); | |
} else { | |
const emailResponse = await sendEmail( | |
appeal.user.email, | |
context.env.MAILGUN_API_KEY, | |
"Appeal Accepted", | |
"appeal_accepted", | |
{ | |
note: context.data.body.feedback || "No note provided.", | |
}, | |
); | |
if (!emailResponse.ok) { | |
console.log(await emailResponse.json()); | |
return jsonError("Failed to accept appeal", 500); | |
} | |
} | |
const { current_user: currentUser } = context.data; | |
await context.env.D1.prepare( | |
"UPDATE appeals SET approved = 1, open = 0 WHERE id = ?;", | |
) | |
.bind(context.params.id) | |
.run(); | |
delete appeal.fcm_token; | |
delete appeal.user.email; | |
await context.env.DATA.put(`appeal_${appeal.id}`, JSON.stringify(appeal), { | |
expirationTtl: 94608000, | |
}); | |
await fetch( | |
`https://discord.com/api/v10/guilds/242263977986359297/bans/${appeal.id}`, | |
{ | |
headers: { | |
authorization: `Bot ${context.env.BOT_TOKEN}`, | |
"x-audit-log-reason": `Appeal accepted by ${currentUser.username} (${currentUser.id})`, | |
}, | |
method: "DELETE", | |
}, | |
); | |
await fetch(context.env.APPEALS_WEBHOOK, { | |
body: JSON.stringify({ | |
embeds: [ | |
{ | |
title: "Appeal Accepted", | |
color: 0x00ff00, | |
description: `Appeal from user ${appeal.user.username} (${appeal.user.id}) was accepted.`, | |
fields: [ | |
{ | |
name: "Moderator", | |
value: `${currentUser.username} (${currentUser.id})`, | |
}, | |
], | |
}, | |
], | |
}), | |
headers: { | |
"content-type": "application/json", | |
}, | |
method: "POST", | |
}); | |
return new Response(null, { | |
status: 204, | |
}); | |
} |