From f7b5d3f8e072465d411203417306295a817065d9 Mon Sep 17 00:00:00 2001 From: davidteather <34144122+davidteather@users.noreply.github.com> Date: Mon, 2 Oct 2023 15:37:11 -0500 Subject: [PATCH] add rsvp confirmation --- lambda/index.js | 127 ++++++++++++++++++++++++++++-------------------- 1 file changed, 75 insertions(+), 52 deletions(-) diff --git a/lambda/index.js b/lambda/index.js index 6de9b83..780244f 100644 --- a/lambda/index.js +++ b/lambda/index.js @@ -1,67 +1,90 @@ -const AWS = require('aws-sdk'); +const AWS = require("aws-sdk"); const SES = new AWS.SES(); exports.handler = async (event) => { - const body = JSON.parse(event.body); - - var to_email = ""; - var first_name = ""; - var last_name = ""; - - // iterate over body.data.fields and get the values for the fields we want - for (var i = 0; i < body.data.fields.length; i++) { + // handle the RSVP confirmation emails + const body = JSON.parse(event.body); + var to_email = ""; + var first_name = ""; - if (body.data.fields[i].key == "question_7R91WA") { - to_email = body.data.fields[i].value; - } + var is_rsvp = false; + var is_rsvp_yes = false; - if (body.data.fields[i].key == "question_ja95yE") { - first_name = body.data.fields[i].value; - } + // iterate over body.data.fields and get the values for the fields we want + for (var i = 0; i < body.data.fields.length; i++) { + if ( + body.data.fields[i].key == "question_7R91WA" || + body.data.fields[i].key == "question_gDJEBP" + ) { + to_email = body.data.fields[i].value; + } - if (body.data.fields[i].key == "question_2Ex1eV") { - last_name = body.data.fields[i].value; - } + if ( + body.data.fields[i].key == "question_ja95yE" || + body.data.fields[i].key == "question_aQARbb" + ) { + first_name = body.data.fields[i].value; } - if (to_email == "" || first_name == "" || last_name == "") { - return { - statusCode: 500, - body: JSON.stringify({ error: "Missing required fields" }), - }; + if (body.data.fields[i].key == "question_7RaQlR") { + is_rsvp = true; + is_rsvp_yes = + body.data.fields[i].value[0] == "3b086c77-00f6-4cf9-87d1-346df95b19f1"; } + } + var subject = ""; + var body_text = ""; + if (is_rsvp) { + subject = "MadHacks RSVP Confirmation"; // is_rsvp_yes + if (is_rsvp_yes) { + body_text = "Hello " + first_name + "," + "\n\nWe're excited to see you at MadHacks Fall 2023! Please join our Discord server for more information https://discord.gg/VgrNq2XgMe .\n\nBest,\nMadHacks Team"; + } else { + body_text = "Hello " + first_name + "," + "\n\nWe're sorry to hear that you can't make it to MadHacks Fall 2023. We hope to see you at our next event!\n\nBest,\nMadHacks Team" + } + } else { + // confirmation email + subject = "Confirmation Of Application To MadHacks Fall 2023"; + body_text = + "Hello " + + first_name + + ",\n\nThank you for applying to MadHacks Fall 2023! We will be reviewing your application and will get back to you soon.\n\nBest,\nMadHacks Team"; + } - const subject = "Confirmation Of Application To MadHacks Fall 2023" - const body_text = "Hello " + first_name + ",\n\nThank you for applying to MadHacks Fall 2023! We will be reviewing your application and will get back to you soon.\n\nBest,\nMadHacks Team" + if (to_email == "" || first_name == "") { + return { + statusCode: 500, + body: JSON.stringify({ error: "Missing required fields" }), + }; + } - const params = { - Source: process.env.FROM_EMAIL, - Destination: { - ToAddresses: [to_email] + const params = { + Source: process.env.FROM_EMAIL, + Destination: { + ToAddresses: [to_email], + }, + Message: { + Subject: { + Data: subject, + }, + Body: { + Text: { + Data: body_text, }, - Message: { - Subject: { - Data: subject - }, - Body: { - Text: { - Data: body_text - } - } - } - }; + }, + }, + }; - try { - await SES.sendEmail(params).promise(); - return { - statusCode: 200, - body: JSON.stringify({ message: 'Email sent!' }), - }; - } catch (error) { - return { - statusCode: 500, - body: JSON.stringify({ error: error.message }), - }; - } + try { + await SES.sendEmail(params).promise(); + return { + statusCode: 200, + body: JSON.stringify({ message: "Email sent!" }), + }; + } catch (error) { + return { + statusCode: 500, + body: JSON.stringify({ error: error.message }), + }; + } };