diff --git a/lib/node-utils.js b/lib/node-utils.js index b19e25d..864cf77 100644 --- a/lib/node-utils.js +++ b/lib/node-utils.js @@ -1,7 +1,5 @@ import jwt from "jsonwebtoken"; -import sgMail from '@sendgrid/mail'; -sgMail.setApiKey(process.env.EMAIL_API_KEY); /** * Encode (and sign) a given object as JWT. @@ -72,27 +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 { - 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, }; diff --git a/pages/api/email-me.js b/pages/api/email-me.js index 3e757b4..1d6e0b0 100644 --- a/pages/api/email-me.js +++ b/pages/api/email-me.js @@ -1,20 +1,29 @@ /* eslint-disable import/no-anonymous-default-export */ import sgMail from "@sendgrid/mail"; import fs from "fs"; -import {getSession} from "next-auth/react"; +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'; +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 { - 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" }); } + 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"; + const safeType = process.env.SAFE_TYPE; const filename = resolveSafeTypeToFilename(safeType); const dirRelativeToPublicFolder = "safes"; @@ -22,33 +31,39 @@ 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: from, + 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", }, ], }; 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.`}); - - } - else { - res.status(200).json({message: `Email has been sent`}); - } + const notificationMsg = + notificationMessage + ` - Investor email: ${session?.user?.email}`; + const businessMsg = { + to: businessUserEmail, + from: from, + subject: `Notification of user action`, + text: notificationMsg, + }; + await sgMail.send(businessMsg); + + res + .status(200) + .json({ message: `Email has been sent and the business notified.` }); } catch (error) { - console.error(error); - res.status(500).json({error : "Error sending email"}); + console.error(JSON.stringify(error)); + res.status(500).json({ error: "Error sending email" }); } } res.end();