1414 * limitations under the License. 
1515 */ 
1616
17- import  { NotificationSettingsRepository }  from  "../../repository/notificationSettingsRepository" ; 
1817import  { EventRepository }  from  "../../repository/eventsRepository" ; 
1918import  { NotificationTemplatesRepository ,  WebhookConfigRepository }  from  "../../repository/templatesRepository" ; 
2019import  { NotificationTemplates }  from  "../../entities/notificationTemplates" ; 
@@ -30,20 +29,17 @@ import {CustomError, CustomResponse} from "../../entities/events";
3029
3130export  interface  Handler  { 
3231    handle ( event : Event ,  templates : ( NotificationTemplates [ ]  |  WebhookConfig [ ] ) ,  setting : NotificationSettings ,  configMap : Map < string ,  boolean > ,  destinationMap : Map < string ,  boolean > ) : Promise < boolean > 
33- 
3432    sendNotification ( event : Event ,  sdk : any ,  template : string ) 
3533} 
3634
3735class  NotificationService  { 
3836    private  eventRepository : EventRepository 
39-     private  notificationSettingsRepository : NotificationSettingsRepository 
4037    private  templatesRepository : NotificationTemplatesRepository 
4138    private  readonly  handlers : Handler [ ] 
4239    private  logger : any 
4340
44-     constructor ( eventRepository : EventRepository ,  notificationSettingsRepository :  NotificationSettingsRepository ,   templatesRepository : NotificationTemplatesRepository ,  handlers : Handler [ ] ,  logger : any )  { 
41+     constructor ( eventRepository : EventRepository ,  templatesRepository : NotificationTemplatesRepository ,  handlers : Handler [ ] ,  logger : any )  { 
4542        this . eventRepository  =  eventRepository 
46-         this . notificationSettingsRepository  =  notificationSettingsRepository 
4743        this . handlers  =  handlers 
4844        this . templatesRepository  =  templatesRepository 
4945        this . logger  =  logger 
@@ -54,7 +50,6 @@ class NotificationService {
5450                throw  new  CustomError ( "Event is not valid for approval " ,  400 ) 
5551            } 
5652
57-             this . logger . info ( 'notificationSettingsRepository.findByEventSource' ) 
5853            if  ( ! event . payload . providers  ||  event . payload . providers  ==  0 )  { 
5954                this . logger . info ( "no notification settings found for event "  +  event . correlationId ) ; 
6055                throw  new  CustomError ( "no notification settings found for event" ,  400 ) 
@@ -137,47 +132,7 @@ class NotificationService {
137132            } 
138133        } 
139134
140-     /** 
141-      * Main function to send notifications based on event type 
142-      * @param  event The event to send notifications for 
143-      * @returns  CustomResponse with status and message 
144-      */ 
145-     public  async  sendNotification ( event : Event ) : Promise < CustomResponse >  { 
146-         try  { 
147-             this . logger . info ( `Processing notification for event type: ${ event . eventTypeId } ${ event . correlationId }  ) ; 
148- 
149-             // Handle approval notifications 
150-             if  ( event . payload . providers  &&  event . payload . providers . length  >  0 )  { 
151-                 this . logger . info ( `Processing approval notification with ${ event . payload . providers . length }  ) ; 
152-                 await  this . sendApprovalNotification ( event ) ; 
153-                 this . logger . info ( `Approval notification sent successfully` ) ; 
154-                 return  new  CustomResponse ( "notification sent" ,  200 ) ; 
155-             } 
156- 
157-             // Handle scoop notification events 
158-             if  ( event . eventTypeId  ==  EVENT_TYPE . ScoopNotification )  { 
159-                 this . logger . info ( `Processing scoop notification event` ) ; 
160-                 return  await  this . handleScoopNotification ( event ) ; 
161-             } 
162135
163-             // Handle regular notifications 
164-             this . logger . info ( `Processing regular notification event` ) ; 
165-             return  await  this . handleRegularNotification ( event ) ; 
166-         }  catch  ( error : any )  { 
167-             const  errorMessage  =  error . message  ||  'Unknown error' ; 
168-             const  errorStack  =  error . stack  ||  '' ; 
169-             this . logger . error ( `Error in sendNotification: ${ errorMessage } ${ errorStack }  ) ; 
170- 
171-             if  ( error  instanceof  CustomError )  { 
172-                 this . logger . error ( `CustomError with status code: ${ error . statusCode }  ) ; 
173-                 return  new  CustomResponse ( "" ,  0 ,  error ) ; 
174-             }  else  { 
175-                 const  customError  =  new  CustomError ( errorMessage ,  400 ) ; 
176-                 this . logger . error ( `Converted to CustomError with status code: 400` ) ; 
177-                 return  new  CustomResponse ( "" ,  0 ,  customError ) ; 
178-             } 
179-         } 
180-     } 
181136
182137    /** 
183138     * Enhanced function to send notifications with pre-provided notification settings 
@@ -295,104 +250,6 @@ class NotificationService {
295250        } 
296251    } 
297252
298-     /** 
299-      * Handle regular notification events 
300-      * @param  event The regular notification event 
301-      * @returns  CustomResponse with status and message 
302-      */ 
303-     private  async  handleRegularNotification ( event : Event ) : Promise < CustomResponse >  { 
304-         try  { 
305-             this . logger . info ( `Handling regular notification for event ID: ${ event . correlationId } ${ event . eventTypeId }  ) ; 
306- 
307-             // Validate event 
308-             if  ( ! this . isValidEvent ( event ) )  { 
309-                 this . logger . error ( `Invalid event: ${ JSON . stringify ( {  
310-                     eventTypeId : event . eventTypeId ,  
311-                     pipelineType : event . pipelineType ,  
312-                     correlationId : event . correlationId ,  
313-                     hasPayload : ! ! event . payload ,  
314-                     hasBaseUrl : ! ! event . baseUrl  
315-                 } ) }  `) ; 
316-                 throw  new  CustomError ( "Event is not valid" ,  400 ) ; 
317-             } 
318-             this . logger . info ( `Event validation passed` ) ; 
319- 
320-             // Get notification settings 
321-             this . logger . info ( `Finding notification settings for event: ${ event . correlationId }  ) ; 
322-             const  settingsResults  =  await  this . findNotificationSettings ( event ) ; 
323-             if  ( ! settingsResults  ||  settingsResults . length  ==  0 )  { 
324-                 this . logger . warn ( `No notification settings found for event ${ event . correlationId }  ) ; 
325-                 return  new  CustomResponse ( "" ,  0 ,  new  CustomError ( "no notification settings found for event" ,  404 ) ) ; 
326-             } 
327-             this . logger . info ( `Found ${ settingsResults . length }  ) ; 
328- 
329-             // Process notification settings 
330-             this . logger . info ( `Preparing notification maps` ) ; 
331-             const  {  destinationMap,  configsMap }  =  this . prepareNotificationMaps ( settingsResults ) ; 
332- 
333-             // Process each setting 
334-             this . logger . info ( `Processing ${ settingsResults . length }  ) ; 
335-             for  ( let  i  =  0 ;  i  <  settingsResults . length ;  i ++ )  { 
336-                 const  setting  =  settingsResults [ i ] ; 
337-                 this . logger . info ( `Processing notification setting ${ i + 1 } ${ settingsResults . length } ${ setting . id }  ) ; 
338-                 const  result  =  await  this . processNotificationSetting ( event ,  setting ,  configsMap ,  destinationMap ) ; 
339-                 if  ( result . status  ===  0 )  { 
340-                     this . logger . error ( `Error processing notification setting: ${ result . error ?. message }  ) ; 
341-                     return  result ;  // Return error if any 
342-                 } 
343-             } 
344- 
345-             this . logger . info ( `All notifications processed successfully` ) ; 
346-             return  new  CustomResponse ( "notification sent" ,  200 ) ; 
347-         }  catch  ( error : any )  { 
348-             const  errorMessage  =  error . message  ||  'Unknown error' ; 
349-             this . logger . error ( `Error in handleRegularNotification: ${ errorMessage }  ) ; 
350-             if  ( error . stack )  { 
351-                 this . logger . error ( `Stack trace: ${ error . stack }  ) ; 
352-             } 
353-             throw  error ;  // Let the parent function handle the error 
354-         } 
355-     } 
356- 
357-     /** 
358-      * Find notification settings for an event 
359-      * @param  event The event to find settings for 
360-      * @returns  Array of notification settings 
361-      */ 
362-     private  async  findNotificationSettings ( event : Event ) : Promise < NotificationSettings [ ] >  { 
363-         try  { 
364-             this . logger . info ( `Finding notification settings for event ID: ${ event . correlationId }  ) ; 
365-             this . logger . info ( `Search parameters: pipelineType=${ event . pipelineType } ${ event . pipelineId }   + 
366-                 `eventTypeId=${ event . eventTypeId } ${ event . appId } ${ event . envId }   + 
367-                 `teamId=${ event . teamId } ${ event . clusterId } ${ event . isProdEnv }  ) ; 
368- 
369-             if  ( event . envIdsForCiPipeline  &&  event . envIdsForCiPipeline . length  >  0 )  { 
370-                 this . logger . info ( `Additional envIdsForCiPipeline: ${ event . envIdsForCiPipeline . join ( ', ' ) }  ) ; 
371-             } 
372- 
373-             const  settings  =  await  this . notificationSettingsRepository . findByEventSource ( 
374-                 event . pipelineType , 
375-                 event . pipelineId , 
376-                 event . eventTypeId , 
377-                 event . appId , 
378-                 event . envId , 
379-                 event . teamId , 
380-                 event . clusterId , 
381-                 event . isProdEnv , 
382-                 event . envIdsForCiPipeline 
383-             ) ; 
384- 
385-             this . logger . info ( `Found ${ settings  ? settings . length  : 0 }  ) ; 
386-             return  settings ; 
387-         }  catch  ( error : any )  { 
388-             const  errorMessage  =  error . message  ||  'Unknown error' ; 
389-             this . logger . error ( `Error in findNotificationSettings: ${ errorMessage }  ) ; 
390-             if  ( error . stack )  { 
391-                 this . logger . error ( `Stack trace: ${ error . stack }  ) ; 
392-             } 
393-             throw  error ;  // Let the parent function handle the error 
394-         } 
395-     } 
396253
397254    /** 
398255     * Prepare notification maps for tracking destinations and configs 
0 commit comments