Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions apps/www/src/@types/sane-email-validation.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare module "sane-email-validation" {
function isEmail(email: string): boolean;
export = isEmail;
}
31 changes: 27 additions & 4 deletions apps/www/src/pages/api/applyAsVolunteer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Airtable from "airtable";
import { checkBotId } from "botid/server";
import { NextApiRequest, NextApiResponse } from "next";
import { ServerClient } from "postmark";
import isEmail from "sane-email-validation";

import {
renderBannedVolunteer,
Expand All @@ -17,16 +18,36 @@ import { ApplyAsVolunteerQuery } from "./applyAsVolunteer.gql";
const postmark = new ServerClient(process.env.POSTMARK_SERVER_TOKEN!);
const base = new Airtable({ apiKey: process.env.AIRTABLE_TOKEN }).base(process.env.AIRTABLE_BASE!);

const escapeAirtableFormulaValue = (value: string) => value.replace(/[\\"]/g, (c) => `\\${c}`);

async function ApplyAsVolunteer(req: NextApiRequest, res: NextApiResponse) {
if (req.method !== "POST") {
res.setHeader("Allow", "POST");
return res.status(405).json({ error: "Method not allowed" });
}

const verification = await checkBotId();

if (verification.isBot) {
return res.status(403).json({ error: "Access denied" });
}

const { email, firstName, lastName, linkedin, region, isOrganize, background } = JSON.parse(
req.body,
);
let body;
if (typeof req.body === "string") {
try {
body = JSON.parse(req.body);
} catch {
return res.status(400).json({ error: "Invalid JSON" });
}
} else {
body = req.body ?? {};
}

const { email, firstName, lastName, linkedin, region, isOrganize, background } = body;
if (!email || !isEmail(String(email))) {
return res.status(400).json({ error: "Invalid email" });
}

let banned = false;

try {
Expand All @@ -35,14 +56,15 @@ async function ApplyAsVolunteer(req: NextApiRequest, res: NextApiResponse) {
maxRecords: 100,
fields: ["Flags"],

filterByFormula: `TRIM(LOWER({Email})) = "${email.toString().toLowerCase().trim()}"`,
filterByFormula: `TRIM(LOWER({Email})) = "${escapeAirtableFormulaValue(email.toString().toLowerCase().trim())}"`,
})
.firstPage();
airtableRes.forEach((record: any) => {
if ((record.get("Flags") || []).includes("Banned")) banned = true;
});
} catch (ex) {
console.error(ex);
return res.status(502).json({ error: "Failed to look up volunteer record" });
}
try {
await base("Volunteers").create([
Expand All @@ -58,6 +80,7 @@ async function ApplyAsVolunteer(req: NextApiRequest, res: NextApiResponse) {
]);
} catch (ex) {
console.error(ex);
return res.status(502).json({ error: "Failed to create volunteer record" });
}
let emailText: string | undefined;
// if(background === 'industry') {
Expand Down