@@ -320,20 +320,23 @@ export const createTransporter = ({ ses }) => {
320320 * Send email
321321 */
322322export const sendEmail = async ( {
323- toEmails,
323+ toEmails = [ ] ,
324324 fromEmail,
325325 title,
326326 template = { } ,
327+ modifier,
327328} : {
328329 toEmails ?: string [ ] ;
329330 fromEmail ?: string ;
330331 title ?: string ;
331332 template ?: { name ?: string ; data ?: any ; isCustom ?: boolean } ;
333+ modifier ?: ( data : any , email : string ) => void ;
332334} ) => {
333335 const NODE_ENV = getEnv ( { name : 'NODE_ENV' } ) ;
334336 const DEFAULT_EMAIL_SERVICE = getEnv ( { name : 'DEFAULT_EMAIL_SERVICE' , defaultValue : '' } ) || 'SES' ;
335337 const COMPANY_EMAIL_FROM = getEnv ( { name : 'COMPANY_EMAIL_FROM' } ) ;
336338 const AWS_SES_CONFIG_SET = getEnv ( { name : 'AWS_SES_CONFIG_SET' , defaultValue : '' } ) ;
339+ const DOMAIN = getEnv ( { name : 'DOMAIN' } ) ;
337340
338341 // do not send email it is running in test mode
339342 if ( NODE_ENV === 'test' ) {
@@ -351,14 +354,21 @@ export const sendEmail = async ({
351354
352355 const { isCustom, data, name } = template ;
353356
354- // generate email content by given template
355- let html = await applyTemplate ( data , name || '' ) ;
357+ // for unsubscribe url
358+ data . domain = DOMAIN ;
356359
357- if ( ! isCustom ) {
358- html = await applyTemplate ( { content : html } , 'base' ) ;
359- }
360+ for ( const toEmail of toEmails ) {
361+ if ( modifier ) {
362+ modifier ( data , toEmail ) ;
363+ }
364+
365+ // generate email content by given template
366+ let html = await applyTemplate ( data , name || '' ) ;
367+
368+ if ( ! isCustom ) {
369+ html = await applyTemplate ( { content : html } , 'base' ) ;
370+ }
360371
361- return ( toEmails || [ ] ) . map ( toEmail => {
362372 const mailOptions = {
363373 from : fromEmail || COMPANY_EMAIL_FROM ,
364374 to : toEmail ,
@@ -373,7 +383,7 @@ export const sendEmail = async ({
373383 debugEmail ( error ) ;
374384 debugEmail ( info ) ;
375385 } ) ;
376- } ) ;
386+ }
377387} ;
378388
379389/**
@@ -406,7 +416,7 @@ export const sendNotification = async (doc: ISendNotification) => {
406416 const receiverIds = [ ...new Set ( receivers ) ] ;
407417
408418 // collecting emails
409- const recipients = await Users . find ( { _id : { $in : receiverIds } } ) ;
419+ const recipients = await Users . find ( { _id : { $in : receiverIds } , isActive : true , doNotDisturb : { $ne : 'Yes' } } ) ;
410420
411421 // collect recipient emails
412422 const toEmails : string [ ] = [ ] ;
@@ -439,12 +449,21 @@ export const sendNotification = async (doc: ISendNotification) => {
439449 throw e ;
440450 }
441451 }
442- }
452+ } // end receiverIds loop
443453
444454 const MAIN_APP_DOMAIN = getEnv ( { name : 'MAIN_APP_DOMAIN' } ) ;
445455
446456 link = `${ MAIN_APP_DOMAIN } ${ link } ` ;
447457
458+ // for controlling email template data filling
459+ const modifier = ( data : any , email : string ) => {
460+ const user = recipients . find ( item => item . email === email ) ;
461+
462+ if ( user ) {
463+ data . uid = user . _id ;
464+ }
465+ } ;
466+
448467 await sendEmail ( {
449468 toEmails,
450469 title : 'Notification' ,
@@ -456,6 +475,7 @@ export const sendNotification = async (doc: ISendNotification) => {
456475 userName : getUserDetail ( createdUser ) ,
457476 } ,
458477 } ,
478+ modifier,
459479 } ) ;
460480
461481 return true ;
@@ -806,8 +826,13 @@ export const getToday = (date: Date): Date => {
806826
807827export const getNextMonth = ( date : Date ) : { start : number ; end : number } => {
808828 const today = getToday ( date ) ;
829+ const currentMonth = new Date ( ) . getMonth ( ) ;
809830
810- const month = ( new Date ( ) . getMonth ( ) + 1 ) % 12 ;
831+ if ( currentMonth === 11 ) {
832+ today . setFullYear ( today . getFullYear ( ) + 1 ) ;
833+ }
834+
835+ const month = ( currentMonth + 1 ) % 12 ;
811836 const start = today . setMonth ( month , 1 ) ;
812837 const end = today . setMonth ( month + 1 , 0 ) ;
813838
@@ -856,3 +881,20 @@ export const regexSearchText = (searchValue: string) => {
856881
857882 return { $and : result } ;
858883} ;
884+
885+ /*
886+ * Handle engage unsubscribe request
887+ */
888+ export const handleUnsubscription = async ( query : { cid : string ; uid : string } ) => {
889+ const { cid, uid } = query ;
890+
891+ if ( cid ) {
892+ await Customers . updateOne ( { _id : cid } , { $set : { doNotDisturb : 'Yes' } } ) ;
893+ }
894+
895+ if ( uid ) {
896+ await Users . updateOne ( { _id : uid } , { $set : { doNotDisturb : 'Yes' } } ) ;
897+ }
898+
899+ return true ;
900+ } ;
0 commit comments