@@ -214,6 +214,74 @@ export async function handlesendReturnEmail(req, res) {
214214}
215215
216216
217+ export async function handlesendDueEmails ( req , res ) {
218+ if ( req . method !== "POST" ) {
219+ return res . status ( 405 ) . json ( { error : "Method Not Allowed" } ) ;
220+ }
221+
222+ try {
223+ const result = await query ( `
224+ SELECT borrower_name, borrower_email, due_date, json_agg(item_name) AS items
225+ FROM borrowed_items
226+ WHERE due_date < CURRENT_DATE + INTERVAL '5 days'
227+ GROUP BY borrower_name, borrower_email, due_date
228+ ` ) ;
229+
230+ for ( const { borrower_name, borrower_email, due_date, items } of result ) {
231+ const isOverdue = new Date ( due_date ) < new Date ( ) ; // JS comparison
232+ const itemList = items . map ( ( item ) => `<li>${ item } </li>` ) . join ( "" ) ;
233+
234+ const subject = isOverdue
235+ ? "Overdue Notice: Your Borrowed Items Are Past Due"
236+ : "Reminder: Your Borrowed Items Are Due Soon" ;
237+
238+ const HTMLPart = isOverdue
239+ ? `
240+ <p>Hi ${ borrower_name } !</p>
241+ <p>This email is to remind you that the item(s) listed below are <b>OVERDUE</b>:</p>
242+ <ul>${ itemList } </ul>
243+ <p>Please contact <a href="mailto:[email protected] ">[email protected] </a> to arrange a return.</p> 244+ `
245+ : `
246+ <p>Hi ${ borrower_name } !</p>
247+ <p>This email is to remind you that the following item(s) should be returned by <b>${ due_date } </b>:</p>
248+ <ul>${ itemList } </ul>
249+ <p>Please contact <a href="mailto:[email protected] ">[email protected] </a> to arrange your return.</p> 250+ <p><b>Guidelines:</b></p>
251+ <p>
252+ Please return your items in the same or better condition than when you received them.
253+ If something is damaged or missing (e.g., buttons, hooks), let us know and return what you can.
254+ </p>
255+ <p>
256+ White cotton items (e.g., socks, shirts) should be washed on delicate with fragrance-free detergent,
257+ dried on low heat, and hung promptly to reduce wrinkling.
258+ </p>
259+ <p>
260+ Don’t forget accessories like belts, pins, jewelry, etc. Thank you!
261+ </p>
262+ ` ;
263+
264+ await mailjet . post ( "send" , { version : "v3.1" } ) . request ( {
265+ Messages : [
266+ {
267+ From : { Email : sendEmailAddr , Name : "Somerville Museum" } ,
268+ To : [ { Email : borrower_email , Name : borrower_name } ] ,
269+ Subject : subject ,
270+ HTMLPart : HTMLPart ,
271+ } ,
272+ ] ,
273+ } ) ;
274+ }
275+
276+ res . status ( 200 ) . json ( { success : true , message : "Due and overdue emails sent." } ) ;
277+
278+ } catch ( error ) {
279+ console . error ( "Error sending due emails:" , error ) ;
280+ res . status ( 500 ) . json ( { success : false , error : error . message } ) ;
281+ }
282+ }
283+
284+
217285/*---------------------------functions below not from elias + massimo----------*/
218286
219287
@@ -312,6 +380,8 @@ export default async function handler(req, res) {
312380 return handlesendOverdueEmail ( req , res ) ;
313381 case 'sendReturnEmail' :
314382 return handlesendReturnEmail ( req , res ) ;
383+ case 'sendDueEmails' :
384+ return handlesendDueEmails ( req , res ) ;
315385 default :
316386 return res . status ( 400 ) . json ( { error : "Invalid email type." } ) ;
317387 }
0 commit comments