@@ -64,7 +64,10 @@ export class SMTPService implements Handler {
6464 const providersSet = new Set ( providerObjects ) ;
6565 this . smtpConfig = null
6666 for ( const element of providersSet ) {
67- if ( element [ 'dest' ] === "smtp" ) {
67+ if ( ( element [ 'dest' ] === "smtp" ) && ( element [ 'configId' ] != 0 ) ) {
68+ await this . getConfigById ( element [ 'configId' ] , providersSet , event , sesTemplate , setting , destinationMap , configsMap )
69+ break
70+ } else if ( element [ 'dest' ] === "smtp" ) {
6871 await this . getDefaultConfig ( providersSet , event , sesTemplate , setting , destinationMap , configsMap )
6972 break
7073 }
@@ -85,20 +88,48 @@ export class SMTPService implements Handler {
8588 if ( this . smtpConfig && this . smtpConfig . from_email ) {
8689 for ( const p of providersSet ) {
8790 if ( p [ 'dest' ] == "smtp" ) {
88- let userId = p [ 'configId' ]
8991 let recipient = p [ 'recipient' ]
9092 let configKey = '' ;
9193 if ( recipient ) {
9294 configKey = p [ 'dest' ] + '-' + recipient
93- } else {
94- configKey = p [ 'dest' ] + '-' + userId
9595 }
9696 if ( ! configsMap . get ( configKey ) ) {
97- await this . processNotification ( userId , recipient , event , sesTemplate , setting , p , emailMap )
97+ await this . processNotification ( recipient , event , sesTemplate , setting , p , emailMap )
9898 configsMap . set ( configKey , true )
9999 }
100100 }
101- } ;
101+ }
102+ }
103+ } catch ( error ) {
104+ this . logger . error ( 'getDefaultConfig' , error )
105+ throw new CustomError ( "Unable to send SMTP notification" , 500 ) ;
106+ }
107+ }
108+
109+ private async getConfigById ( id , providersSet , event : Event , sesTemplate : NotificationTemplates , setting : NotificationSettings , emailMap : Map < string , boolean > , configsMap : Map < string , boolean > ) {
110+ try {
111+ const config = await this . smtpConfigRepository . findBySMTPConfigId ( id )
112+ this . smtpConfig = {
113+ port : config [ 'port' ] ,
114+ host : config [ 'host' ] ,
115+ auth_user : config [ 'auth_user' ] ,
116+ auth_password : config [ 'auth_password' ] ,
117+ from_email : config [ 'from_email' ]
118+ }
119+ if ( this . smtpConfig && this . smtpConfig . from_email ) {
120+ for ( const p of providersSet ) {
121+ if ( p [ 'dest' ] == "smtp" ) {
122+ let recipient = p [ 'recipient' ]
123+ let configKey = '' ;
124+ if ( recipient ) {
125+ configKey = p [ 'dest' ] + '-' + recipient
126+ }
127+ if ( ! configsMap . get ( configKey ) ) {
128+ await this . processNotification ( recipient , event , sesTemplate , setting , p , emailMap )
129+ configsMap . set ( configKey , true )
130+ }
131+ }
132+ }
102133 }
103134 } catch ( error ) {
104135 this . logger . error ( 'getDefaultConfig' , error )
@@ -150,34 +181,24 @@ export class SMTPService implements Handler {
150181 catch ( error : any ) {
151182 this . logger . error ( error . message ) ;
152183 await this . saveNotificationEventFailureLog ( event , p , setting ) ;
153- } ;
184+ }
154185 } else {
155186 try {
156187 const result = this . sendNotification ( event , sdk , smtpTemplate . template_payload )
157188 await this . saveNotificationEventSuccessLog ( result , event , p , setting ) ; }
158189 catch ( error : any ) {
159190 this . logger . error ( error . message ) ;
160191 await this . saveNotificationEventFailureLog ( event , p , setting ) ;
161- } ;
192+ }
162193 }
163194 }
164195
165- private async processNotification ( userId : number , recipient : string , event : Event , smtpTemplate : NotificationTemplates , setting : NotificationSettings , p : string , emailMap : Map < string , boolean > ) {
166- if ( userId ) {
167- const user = await this . usersRepository . findByUserId ( userId )
168- if ( ! user ) {
169- this . logger . info ( 'no user found for id - ' + userId )
170- this . logger . info ( event . correlationId )
171- return
172- }
173- await this . sendEmailIfNotDuplicate ( user [ 'email_id' ] , event , smtpTemplate , setting , p , emailMap )
174- } else {
175- if ( ! recipient ) {
176- this . logger . error ( 'recipient is blank' )
177- return
178- }
179- await this . sendEmailIfNotDuplicate ( recipient , event , smtpTemplate , setting , p , emailMap )
196+ private async processNotification ( recipient : string , event : Event , smtpTemplate : NotificationTemplates , setting : NotificationSettings , p : string , emailMap : Map < string , boolean > ) {
197+ if ( ! recipient ) {
198+ this . logger . error ( 'recipient is blank' )
199+ return
180200 }
201+ await this . sendEmailIfNotDuplicate ( recipient , event , smtpTemplate , setting , p , emailMap )
181202 }
182203
183204 private async sendEmailIfNotDuplicate ( recipient : string , event : Event , smtpTemplate : NotificationTemplates , setting : NotificationSettings , p : string , emailMap : Map < string , boolean > ) {
0 commit comments