From 1f0cdaeaf1e2f1d6cbc7ba3032ceff137937d5ed Mon Sep 17 00:00:00 2001 From: "Jason R. Stevens, CFA" Date: Thu, 3 Feb 2022 16:12:05 -0600 Subject: [PATCH 1/8] :recycle: :wrench: move api key setting to function [sc-755] --- lib/node-utils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node-utils.js b/lib/node-utils.js index b19e25d..b806096 100644 --- a/lib/node-utils.js +++ b/lib/node-utils.js @@ -1,7 +1,6 @@ import jwt from "jsonwebtoken"; import sgMail from '@sendgrid/mail'; -sgMail.setApiKey(process.env.EMAIL_API_KEY); /** * Encode (and sign) a given object as JWT. @@ -82,6 +81,7 @@ function resolveSafeTypeToFilename(safeType) { */ async function sendEmailNotification(toEmail, notificationMessage) { try { + sgMail.setApiKey(process.env.EMAIL_API_KEY); const msg = { to: toEmail, from: process.env.FROM_EMAIL, From e2a81a1d4b004a9a7b9bc6ffa4af107d286f1748 Mon Sep 17 00:00:00 2001 From: "Jason R. Stevens, CFA" Date: Thu, 3 Feb 2022 16:55:33 -0600 Subject: [PATCH 2/8] :fire: remove duplicate func --- lib/node-utils.js | 28 ++-------------------------- 1 file changed, 2 insertions(+), 26 deletions(-) diff --git a/lib/node-utils.js b/lib/node-utils.js index b806096..864cf77 100644 --- a/lib/node-utils.js +++ b/lib/node-utils.js @@ -1,5 +1,4 @@ import jwt from "jsonwebtoken"; -import sgMail from '@sendgrid/mail'; /** @@ -71,28 +70,5 @@ function resolveSafeTypeToFilename(safeType) { } return null; } -/* - * A function to email a notification to business development. - * - * @param string toEmail - * @param string notificationMessage - * - * @returns Boolean true if the message was sent, false otherwise. - */ -async function sendEmailNotification(toEmail, notificationMessage) { - try { - sgMail.setApiKey(process.env.EMAIL_API_KEY); - const msg = { - to: toEmail, - from: process.env.FROM_EMAIL, - subject: `Notification from Phund`, - text: notificationMessage, - }; - await sgMail.send(msg); - return true; - } catch (error) { - console.error(error); - return false; - } -} -export { getCsp, logApiError, encodeJwt, resolveSafeTypeToFilename, sendEmailNotification}; + +export { getCsp, logApiError, encodeJwt, resolveSafeTypeToFilename, }; From f517a1bc02d3d85fac4b54af11fd2a62ceee0ee8 Mon Sep 17 00:00:00 2001 From: "Jason R. Stevens, CFA" Date: Thu, 3 Feb 2022 16:55:36 -0600 Subject: [PATCH 3/8] :recycle: move func into api call --- pages/api/email-me.js | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/pages/api/email-me.js b/pages/api/email-me.js index 3e757b4..2b9e0c4 100644 --- a/pages/api/email-me.js +++ b/pages/api/email-me.js @@ -38,14 +38,18 @@ export default async (req, res) => { ], }; await sgMail.send(msg); - const notificatWasSent = await sendEmailNotification(businessUserEmail, notificationMessage + ` - Investor email: ${session?.user?.email}`); - if (notificatWasSent) { - res.status(200).json({message: `Email has been sent and the business notified.`}); + const notificationMsg = notificationMessage + ` - Investor email: ${session?.user?.email}`; + sgMail.setApiKey(process.env.EMAIL_API_KEY); + const businessMsg = { + to: businessUserEmail, + from: process.env.FROM_EMAIL, + subject: `Notification from Phund`, + text: notificationMsg, + }; + await sgMail.send(businessMsg); + + res.status(200).json({message: `Email has been sent and the business notified.`}); - } - else { - res.status(200).json({message: `Email has been sent`}); - } } catch (error) { console.error(error); res.status(500).json({error : "Error sending email"}); From 260586f3419c44116f870eff7d22a7238a32af56 Mon Sep 17 00:00:00 2001 From: "Jason R. Stevens, CFA" Date: Thu, 3 Feb 2022 16:57:02 -0600 Subject: [PATCH 4/8] :recycle: move api set into func body --- pages/api/email-me.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/pages/api/email-me.js b/pages/api/email-me.js index 2b9e0c4..a9b3e1d 100644 --- a/pages/api/email-me.js +++ b/pages/api/email-me.js @@ -3,11 +3,8 @@ import sgMail from "@sendgrid/mail"; import fs from "fs"; import {getSession} from "next-auth/react"; import path from "path"; -import {resolveSafeTypeToFilename, sendEmailNotification} from '../../lib/node-utils'; +import {resolveSafeTypeToFilename } from '../../lib/node-utils'; -sgMail.setApiKey(process.env.EMAIL_API_KEY); -const businessUserEmail = 'jason@tincre.com' -const notificationMessage = 'The following user emailed themselves our SAFE note. You should probably follow up tiger. ;-)\n\n'; export default async (req, res) => { if (req.method === "POST") { try { @@ -15,6 +12,10 @@ export default async (req, res) => { if (!session) { return res.status(403).json({error : "Not authorized"}); } + sgMail.setApiKey(process.env.EMAIL_API_KEY); + const businessUserEmail = 'jason@tincre.com' + const notificationMessage = 'The following user emailed themselves our SAFE note. You should probably follow up tiger. ;-)\n\n'; + const safeType = process.env.SAFE_TYPE; const filename = resolveSafeTypeToFilename(safeType); const dirRelativeToPublicFolder = "safes"; From 265666f49b64660902f8961f2b622250af44e41f Mon Sep 17 00:00:00 2001 From: "Jason R. Stevens, CFA" Date: Thu, 3 Feb 2022 17:03:28 -0600 Subject: [PATCH 5/8] :warning: :bug: add debugging output --- pages/api/email-me.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pages/api/email-me.js b/pages/api/email-me.js index a9b3e1d..3018f7b 100644 --- a/pages/api/email-me.js +++ b/pages/api/email-me.js @@ -1,5 +1,7 @@ /* eslint-disable import/no-anonymous-default-export */ import sgMail from "@sendgrid/mail"; +console.log(process.env.EMAIL_API_KEY) +sgMail.setApiKey(process.env.EMAIL_API_KEY); import fs from "fs"; import {getSession} from "next-auth/react"; import path from "path"; @@ -12,7 +14,6 @@ export default async (req, res) => { if (!session) { return res.status(403).json({error : "Not authorized"}); } - sgMail.setApiKey(process.env.EMAIL_API_KEY); const businessUserEmail = 'jason@tincre.com' const notificationMessage = 'The following user emailed themselves our SAFE note. You should probably follow up tiger. ;-)\n\n'; From 500f19f75ac7227daccf403bb5076c0aad923373 Mon Sep 17 00:00:00 2001 From: "Jason R. Stevens, CFA" Date: Thu, 3 Feb 2022 17:38:43 -0600 Subject: [PATCH 6/8] :construction: wip commit w/debugging --- pages/api/email-me.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/pages/api/email-me.js b/pages/api/email-me.js index 3018f7b..e96fe2e 100644 --- a/pages/api/email-me.js +++ b/pages/api/email-me.js @@ -1,6 +1,5 @@ /* eslint-disable import/no-anonymous-default-export */ import sgMail from "@sendgrid/mail"; -console.log(process.env.EMAIL_API_KEY) sgMail.setApiKey(process.env.EMAIL_API_KEY); import fs from "fs"; import {getSession} from "next-auth/react"; @@ -14,7 +13,9 @@ export default async (req, res) => { if (!session) { return res.status(403).json({error : "Not authorized"}); } - const businessUserEmail = 'jason@tincre.com' + console.log(`Sendgrid: ${process.env.EMAIL_API_KEY}`) + + const businessUserEmail = 'jason@musicfox.io' const notificationMessage = 'The following user emailed themselves our SAFE note. You should probably follow up tiger. ;-)\n\n'; const safeType = process.env.SAFE_TYPE; @@ -39,9 +40,9 @@ export default async (req, res) => { }, ], }; + console.log('Passed data/setup') await sgMail.send(msg); const notificationMsg = notificationMessage + ` - Investor email: ${session?.user?.email}`; - sgMail.setApiKey(process.env.EMAIL_API_KEY); const businessMsg = { to: businessUserEmail, from: process.env.FROM_EMAIL, @@ -53,7 +54,7 @@ export default async (req, res) => { res.status(200).json({message: `Email has been sent and the business notified.`}); } catch (error) { - console.error(error); + console.error(JSON.stringify(error)); res.status(500).json({error : "Error sending email"}); } } From 596165203240f4d26cedc8604d1ece9a991a6400 Mon Sep 17 00:00:00 2001 From: "Jason R. Stevens, CFA" Date: Thu, 3 Feb 2022 22:54:38 -0600 Subject: [PATCH 7/8] :recycle: change from email to hard coded string --- pages/api/email-me.js | 52 +++++++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/pages/api/email-me.js b/pages/api/email-me.js index e96fe2e..140e165 100644 --- a/pages/api/email-me.js +++ b/pages/api/email-me.js @@ -1,22 +1,23 @@ /* eslint-disable import/no-anonymous-default-export */ import sgMail from "@sendgrid/mail"; -sgMail.setApiKey(process.env.EMAIL_API_KEY); import fs from "fs"; -import {getSession} from "next-auth/react"; +import { getSession } from "next-auth/react"; import path from "path"; -import {resolveSafeTypeToFilename } from '../../lib/node-utils'; +import { resolveSafeTypeToFilename } from "../../lib/node-utils"; +sgMail.setApiKey(process.env.EMAIL_API_KEY); export default async (req, res) => { if (req.method === "POST") { try { - const session = await getSession({req}); + const session = await getSession({ req }); if (!session) { - return res.status(403).json({error : "Not authorized"}); + return res.status(403).json({ error: "Not authorized" }); } - console.log(`Sendgrid: ${process.env.EMAIL_API_KEY}`) + console.log(`Sendgrid: ${process.env.EMAIL_API_KEY}`); - const businessUserEmail = 'jason@musicfox.io' - const notificationMessage = 'The following user emailed themselves our SAFE note. You should probably follow up tiger. ;-)\n\n'; + const businessUserEmail = "jason@musicfox.io"; + const notificationMessage = + "The following user emailed themselves our SAFE note. You should probably follow up tiger. ;-)\n\n"; const safeType = process.env.SAFE_TYPE; const filename = resolveSafeTypeToFilename(safeType); @@ -25,37 +26,40 @@ export default async (req, res) => { const dir = path.resolve("./public", dirRelativeToPublicFolder); const pathToAttachment = `${dir}/${filename}`; const attachment = fs.readFileSync(pathToAttachment).toString("base64"); - const message = "Hi!\n\nWe're thrilled that you're interested in investing to help us continue our growth. We will reach out shortly.\n\nMeanwhile, please find your requested SAFE note attached."; + const message = + "Hi!\n\nWe're thrilled that you're interested in investing to help us continue our growth. We will reach out shortly.\n\nMeanwhile, please find your requested SAFE note attached."; const msg = { - to : session?.user?.email, - from : process.env.FROM_EMAIL, - subject : `Your requested SAFE note.`, - text : message, - attachments : [ + to: session?.user?.email, + from: "support@phund.xyz", + subject: `Your requested SAFE note.`, + text: message, + attachments: [ { - content : attachment, - filename : filename, - type : "application/pdf", - disposition : "attachment", + content: attachment, + filename: filename, + type: "application/pdf", + disposition: "attachment", }, ], }; - console.log('Passed data/setup') + console.log("Passed data/setup"); await sgMail.send(msg); - const notificationMsg = notificationMessage + ` - Investor email: ${session?.user?.email}`; + const notificationMsg = + notificationMessage + ` - Investor email: ${session?.user?.email}`; const businessMsg = { to: businessUserEmail, - from: process.env.FROM_EMAIL, + from: "support@phund.xyz", subject: `Notification from Phund`, text: notificationMsg, }; await sgMail.send(businessMsg); - res.status(200).json({message: `Email has been sent and the business notified.`}); - + res + .status(200) + .json({ message: `Email has been sent and the business notified.` }); } catch (error) { console.error(JSON.stringify(error)); - res.status(500).json({error : "Error sending email"}); + res.status(500).json({ error: "Error sending email" }); } } res.end(); From 8c7e79db98a3e15c5be53e3a60359110fefc76de Mon Sep 17 00:00:00 2001 From: "Jason R. Stevens, CFA" Date: Thu, 3 Feb 2022 23:14:29 -0600 Subject: [PATCH 8/8] :recycle: add automatic 'from' object parsing FROM_EMAIL --- pages/api/email-me.js | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/pages/api/email-me.js b/pages/api/email-me.js index 140e165..1d6e0b0 100644 --- a/pages/api/email-me.js +++ b/pages/api/email-me.js @@ -5,7 +5,13 @@ import { getSession } from "next-auth/react"; import path from "path"; import { resolveSafeTypeToFilename } from "../../lib/node-utils"; sgMail.setApiKey(process.env.EMAIL_API_KEY); - +function SplitFromEmailEnv(envVar) { + const first = envVar.indexOf("<"); + const last = envVar.indexOf(">"); + const email = envVar.substring(first + 1, last); + const name = envVar.substring(0, first).trim(); + return { name: name, email: email }; +} export default async (req, res) => { if (req.method === "POST") { try { @@ -13,8 +19,7 @@ export default async (req, res) => { if (!session) { return res.status(403).json({ error: "Not authorized" }); } - console.log(`Sendgrid: ${process.env.EMAIL_API_KEY}`); - + const from = SplitFromEmailEnv(process.env.FROM_EMAIL); const businessUserEmail = "jason@musicfox.io"; const notificationMessage = "The following user emailed themselves our SAFE note. You should probably follow up tiger. ;-)\n\n"; @@ -30,7 +35,7 @@ export default async (req, res) => { "Hi!\n\nWe're thrilled that you're interested in investing to help us continue our growth. We will reach out shortly.\n\nMeanwhile, please find your requested SAFE note attached."; const msg = { to: session?.user?.email, - from: "support@phund.xyz", + from: from, subject: `Your requested SAFE note.`, text: message, attachments: [ @@ -42,14 +47,13 @@ export default async (req, res) => { }, ], }; - console.log("Passed data/setup"); await sgMail.send(msg); const notificationMsg = notificationMessage + ` - Investor email: ${session?.user?.email}`; const businessMsg = { to: businessUserEmail, - from: "support@phund.xyz", - subject: `Notification from Phund`, + from: from, + subject: `Notification of user action`, text: notificationMsg, }; await sgMail.send(businessMsg);