From 676a486878d407a83c506a6eb1fd1f3d98c4517d Mon Sep 17 00:00:00 2001 From: Leon Xu Date: Sat, 30 Dec 2023 17:02:04 -0800 Subject: [PATCH] fix; updated the nodemailer --- lib/nodemailer/index.ts | 107 +++++++++++++++++++++------------------- 1 file changed, 56 insertions(+), 51 deletions(-) diff --git a/lib/nodemailer/index.ts b/lib/nodemailer/index.ts index 4dffb16..dcc3b96 100644 --- a/lib/nodemailer/index.ts +++ b/lib/nodemailer/index.ts @@ -1,28 +1,33 @@ 'use server' -import { EmailContent, EmailProductInfo } from '@/types'; +import { EmailContent, EmailProductInfo, NotificationType } from '@/types'; import nodemailer from 'nodemailer'; export const Notification = { - WELCOME: 'WELCOME', - CHANGE_OF_STOCK: 'CHANGE_OF_STOCK', - LOWEST_PRICE: 'LOWEST_PRICE', - THRESHOLD_MET: 'THRESHOLD_MET' + WELCOME: 'WELCOME', + CHANGE_OF_STOCK: 'CHANGE_OF_STOCK', + LOWEST_PRICE: 'LOWEST_PRICE', + THRESHOLD_MET: 'THRESHOLD_MET' } -export const generateEmailBody(product: EmailProductInfo, type: NotificationType) => { - const THRESHOLD_PERCENTAGE = 40; +export async function generateEmailBody( + product: EmailProductInfo, + type: NotificationType +) { + const THRESHOLD_PERCENTAGE = 40; + // Shorten the product title + const shortenedTitle = + product.title.length > 20 + ? `${product.title.substring(0, 20)}...` + : product.title; - const shortenTitle = product.title.length > 20 ? `${product.title.substring(0, 20)}...` : product.title; + let subject = ""; + let body = ""; - let subject = '' - - let body = '' - - switch (type) { - case Notification.WELCOME: - subject = `Welcome to Price Tracking for ${shortenTitle}`; - body = ` + switch (type) { + case Notification.WELCOME: + subject = `Welcome to Price Tracking for ${shortenedTitle}`; + body = `

Welcome to PriceWise 🚀

You are now tracking ${product.title}.

@@ -36,67 +41,67 @@ export const generateEmailBody(product: EmailProductInfo, type: NotificationType

Stay tuned for more updates on ${product.title} and other products you're tracking.

`; - break; + break; - case Notification.CHANGE_OF_STOCK: - subject = `${shortenedTitle} is now back in stock!`; - body = ` + case Notification.CHANGE_OF_STOCK: + subject = `${shortenedTitle} is now back in stock!`; + body = `

Hey, ${product.title} is now restocked! Grab yours before they run out again!

See the product here.

`; - break; + break; - case Notification.LOWEST_PRICE: - subject = `Lowest Price Alert for ${shortenTitle}`; - body = ` + case Notification.LOWEST_PRICE: + subject = `Lowest Price Alert for ${shortenedTitle}`; + body = `

Hey, ${product.title} has reached its lowest price ever!!

Grab the product here now.

`; - break; + break; - case Notification.THRESHOLD_MET: - subject = `Discount Alert for ${shortenTitle}`; - body = ` + case Notification.THRESHOLD_MET: + subject = `Discount Alert for ${shortenedTitle}`; + body = `

Hey, ${product.title} is now available at a discount more than ${THRESHOLD_PERCENTAGE}%!

Grab it right away from here.

`; - break; + break; - default: - throw new Error("Invalid notification type."); - } + default: + throw new Error("Invalid notification type."); + } - return { subject, body }; + return { subject, body }; } const transporter = nodemailer.createTransport({ - pool: true, - service: 'hotmail', - port: 2525, - auth: { - user: 'javascriptmastery@outlook.com', - pass: process.env.EMAIL_PASSWORD, - }, - maxConnections: 1 + pool: true, + service: 'hotmail', + port: 2525, + auth: { + user: 'javascriptmastery@outlook.com', + pass: process.env.EMAIL_PASSWORD, + }, + maxConnections: 1 }) export const sendEmail = async (emailContent: EmailContent, sendTo: string[]) => { - const mailOptions = { - from: 'javascriptmastery@outlook.com', - to: sendTo, - html: emailContent.body, - subject: emailContent.subject, - } + const mailOptions = { + from: 'javascriptmastery@outlook.com', + to: sendTo, + html: emailContent.body, + subject: emailContent.subject, + } - transporter.sendMail(mailOptions, (error: any, info: any) => { - if (error) return console.log(error); + transporter.sendMail(mailOptions, (error: any, info: any) => { + if (error) return console.log(error); - console.log('Email sent: ', info); - }) + console.log('Email sent: ', info); + }) } \ No newline at end of file