Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Move most email sending to common email function
  • Loading branch information
regalijan committed Oct 19, 2023
1 parent 86109eb commit b5a92db
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 31 deletions.
31 changes: 15 additions & 16 deletions functions/api/appeals/[id]/accept.ts
@@ -1,27 +1,20 @@
import { jsonError } from "../../../common.js";
import sendEmail from "../../../email.js";

export async function onRequestPost(context: RequestContext) {
const { appeal } = context.data;
const body = new FormData();
body.append("from", "noreply@mail.carcrushers.cc");
body.append("to", appeal.user.email);
body.append("subject", "Appeal Accepted");
body.append("template", "appeal_accepted");
body.append("v:note", context.data.body.feedback || "No note provided.");

const emailReq = await fetch(
"https://api.mailgun.net/v3/mail.carcrushers.cc/messages",
const emailResponse = await sendEmail(
appeal.user.email,
context.env.MAILGUN_API_KEY,
"Appeal Accepted",
"appeal_accepted",
{
body,
headers: {
authorization: `Basic ${btoa("api:" + context.env.MAILGUN_API_KEY)}`,
},
method: "POST",
note: context.data.body.feedback || "No note provided.",
},
);

if (!emailReq.ok) {
console.log(await emailReq.json());
if (!emailResponse.ok) {
console.log(await emailResponse.json());
return jsonError("Failed to accept appeal", 500);
}

Expand All @@ -31,6 +24,12 @@ export async function onRequestPost(context: RequestContext) {
.bind(context.params.id)
.run();

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}`,
{
Expand Down
24 changes: 9 additions & 15 deletions functions/api/appeals/[id]/deny.ts
@@ -1,27 +1,21 @@
import { jsonError } from "../../../common.js";
import sendEmail from "../../../email.js";

export async function onRequestPost(context: RequestContext) {
const { appeal } = context.data;
const body = new FormData();
body.append("from", "noreply@mail.carcrushers.cc");
body.append("to", appeal.user.email);
body.append("subject", "Appeal Denied");
body.append("template", "appeal_denied");
body.append("v:note", context.data.body.feedback || "No note provided.");

const emailReq = await fetch(
"https://api.mailgun.net/v3/mail.carcrushers.cc/messages",
const emailResponse = await sendEmail(
appeal.user.email,
context.env.MAILGUN_API_KEY,
"Appeal Denied",
"appeal_denied",
{
body,
headers: {
authorization: `Basic ${btoa("api:" + context.env.MAILGUN_API_KEY)}`,
},
method: "POST",
note: context.data.body.feedback || "No note provided.",
},
);

if (!emailReq.ok) {
console.log(await emailReq.json());
if (!emailResponse.ok) {
console.log(await emailResponse.json());
return jsonError("Failed to deny appeal", 500);
}

Expand Down
13 changes: 13 additions & 0 deletions functions/api/reports/[id]/action.ts
@@ -1,6 +1,7 @@
import { getBanList, setBanList } from "../../../roblox-open-cloud.js";
import { GetAccessToken, insertLogs } from "../../../gcloud.js";
import { jsonError } from "../../../common.js";
import sendEmail from "../../../email.js";

export async function onRequestPost(context: RequestContext) {
const reportId = context.params.id as string;
Expand All @@ -14,6 +15,18 @@ export async function onRequestPost(context: RequestContext) {
const actionMap = context.data.body;
const newActions: { [k: string]: { BanType: number } } = {};
const logMap: { [k: string]: number } = {};
const { user } = reportData as ReportCardProps & { user?: { email: string } };

if (user?.email)
await sendEmail(
user.email,
context.env.MAILGUN_API_KEY,
"Report Processed",
"report_processed",
{
username: reportData.user?.username as string,
},
);

if (!Object.values(actionMap).find((action) => action !== 0)) {
await context.env.DATA.delete(`report_${reportId}`);
Expand Down

0 comments on commit b5a92db

Please sign in to comment.