From 2fa50925a76c266ac918d48e4e50c5579c8b0425 Mon Sep 17 00:00:00 2001 From: abdelmounaim lallouache Date: Fri, 17 Nov 2023 13:45:37 -0600 Subject: [PATCH] fix email sending for setup profile --- .../profileInitialSetupController.js | 27 ++++++++++++++----- src/utilities/emailSender.js | 10 ++++++- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/src/controllers/profileInitialSetupController.js b/src/controllers/profileInitialSetupController.js index 18cf7376c..3e51e5578 100644 --- a/src/controllers/profileInitialSetupController.js +++ b/src/controllers/profileInitialSetupController.js @@ -76,6 +76,23 @@ function informManagerMessage(user) { return message; } +const sendEmailWithAcknowledgment = (email, subject, message) => { + return new Promise((resolve, reject) => { + emailSender( + email, + subject, + message, + null, + null, + null, + (error,result) => { + if (result) resolve(result) + if (error) reject(result) + } + ); + }); +}; + const profileInitialSetupController = function ( ProfileInitialSetupToken, userProfile, @@ -114,15 +131,13 @@ const profileInitialSetupController = function ( const savedToken = await newToken.save(); const link = `${baseUrl}/ProfileInitialSetup/${savedToken.token}`; - emailSender( + const acknowledgment = await sendEmailWithAcknowledgment( email, "NEEDED: Complete your One Community profile setup", - sendLinkMessage(link), - null, - null + sendLinkMessage(link) ); - - res.status(200).send(link); + + res.status(200).send(acknowledgment); } } catch (error) { res.status(400).send(`Error: ${error}`); diff --git a/src/utilities/emailSender.js b/src/utilities/emailSender.js index b4864add6..b07f9a8c9 100644 --- a/src/utilities/emailSender.js +++ b/src/utilities/emailSender.js @@ -35,7 +35,7 @@ const closure = () => { if (!nextItem) return; const { - recipient, subject, message, cc, bcc, replyTo, + recipient, subject, message, cc, bcc, replyTo, acknowledgingReceipt } = nextItem; try { @@ -59,8 +59,14 @@ const closure = () => { }; const result = await transporter.sendMail(mailOptions); + if (typeof acknowledgingReceipt === 'function') { + acknowledgingReceipt(null,result); + } logger.logInfo(result); } catch (error) { + if (typeof acknowledgingReceipt === 'function') { + acknowledgingReceipt(error,null); + } logger.logException(error); } }, process.env.MAIL_QUEUE_INTERVAL || 1000); @@ -72,6 +78,7 @@ const closure = () => { cc = null, bcc = null, replyTo = null, + acknowledgingReceipt = null, ) { if (process.env.sendEmail) { queue.push({ @@ -81,6 +88,7 @@ const closure = () => { cc, bcc, replyTo, + acknowledgingReceipt }); } };