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]/deny.ts
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
69 lines (62 sloc)
1.74 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 Denied", | |
`Unfortunately, we have decided to deny your appeal for the following reason: ${ | |
context.data.body.feedback || "No additional details" | |
}`, | |
appeal.fcm_token, | |
); | |
} 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.env.D1.prepare( | |
"UPDATE appeals SET approved = 0, open = 0 WHERE id = ?;", | |
) | |
.bind(context.params.id) | |
.run(); | |
const { current_user: currentUser } = context.data; | |
delete appeal.user.email; | |
delete appeal.fcm_token; | |
await fetch(context.env.APPEALS_WEBHOOK, { | |
body: JSON.stringify({ | |
embeds: [ | |
{ | |
title: "Appeal Denied", | |
color: 0xff0000, | |
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, | |
}); | |
} |