@@ -7,14 +7,13 @@ const { FdkWebhookProcessError, FdkWebhookHandlerNotFound, FdkWebhookRegistratio
77const logger = require ( "./logger" ) ;
88const { RetryManger } = require ( "./retry_manager" ) ;
99
10- let retryManager = new RetryManger ( ) ;
11-
1210let eventConfig = { }
1311class WebhookRegistry {
14- constructor ( ) {
12+ constructor ( retryManager ) {
1513 this . _handlerMap = null ;
1614 this . _config = null ;
1715 this . _fdkConfig = null ;
16+ this . _retryManager = retryManager ;
1817 }
1918
2019 async initialize ( config , fdkConfig ) {
@@ -322,19 +321,19 @@ class WebhookRegistry {
322321 async registerSubscriberConfig ( platformClient , subscriberConfig ) {
323322 const uniqueKey = `registerSubscriberToEvent_${ platformClient . config . companyId } _${ this . _fdkConfig . api_key } ` ;
324323
325- const retryInfo = retryManager . retryInfoMap . get ( uniqueKey ) ;
324+ const retryInfo = this . _retryManager . retryInfoMap . get ( uniqueKey ) ;
326325 if ( retryInfo && ! retryInfo . isRetry ) {
327- retryManager . resetRetryState ( uniqueKey ) ;
326+ this . _retryManager . resetRetryState ( uniqueKey ) ;
328327 }
329328
330329 try {
331330 return await platformClient . webhook . registerSubscriberToEvent ( { body : subscriberConfig } ) ;
332331 } catch ( err ) {
333332 if (
334333 RetryManger . shouldRetryOnError ( err )
335- && ! retryManager . retryInfoMap . get ( uniqueKey ) ?. isRetryInProgress
334+ && ! this . _retryManager . isRetryInProgress ( uniqueKey )
336335 ) {
337- return await retryManager . retry (
336+ return await this . _retryManager . retry (
338337 uniqueKey ,
339338 this . registerSubscriberConfig . bind ( this ) ,
340339 platformClient ,
@@ -348,19 +347,19 @@ class WebhookRegistry {
348347 async updateSubscriberConfig ( platformClient , subscriberConfig ) {
349348 const uniqueKey = `updateSubscriberConfig_${ platformClient . config . companyId } _${ this . _fdkConfig . api_key } ` ;
350349
351- const retryInfo = retryManager . retryInfoMap . get ( uniqueKey ) ;
350+ const retryInfo = this . _retryManager . retryInfoMap . get ( uniqueKey ) ;
352351 if ( retryInfo && ! retryInfo . isRetry ) {
353- retryManager . resetRetryState ( uniqueKey ) ;
352+ this . _retryManager . resetRetryState ( uniqueKey ) ;
354353 }
355354
356355 try {
357356 return await platformClient . webhook . updateSubscriberConfig ( { body : subscriberConfig } ) ;
358357 } catch ( err ) {
359358 if (
360359 RetryManger . shouldRetryOnError ( err )
361- && ! retryManager . retryInfoMap . get ( uniqueKey ) ?. isRetryInProgress
360+ && ! this . _retryManager . isRetryInProgress ( uniqueKey )
362361 ) {
363- return await retryManager . retry (
362+ return await this . _retryManager . retry (
364363 uniqueKey ,
365364 this . updateSubscriberConfig . bind ( this ) ,
366365 platformClient ,
@@ -374,9 +373,9 @@ class WebhookRegistry {
374373 async getSubscriberConfig ( platformClient ) {
375374 const uniqueKey = `getSubscribersByExtensionId_${ platformClient . config . companyId } _${ this . _fdkConfig . api_key } ` ;
376375
377- const retryInfo = retryManager . retryInfoMap . get ( uniqueKey ) ;
376+ const retryInfo = this . _retryManager . retryInfoMap . get ( uniqueKey ) ;
378377 if ( retryInfo && ! retryInfo . isRetry ) {
379- retryManager . resetRetryState ( uniqueKey ) ;
378+ this . _retryManager . resetRetryState ( uniqueKey ) ;
380379 }
381380
382381 try {
@@ -386,9 +385,9 @@ class WebhookRegistry {
386385 catch ( err ) {
387386 if (
388387 RetryManger . shouldRetryOnError ( err )
389- && ! retryManager . retryInfoMap . get ( uniqueKey ) ?. isRetryInProgress
388+ && ! this . _retryManager . isRetryInProgress ( uniqueKey )
390389 ) {
391- return await retryManager . retry (
390+ return await this . _retryManager . retry (
392391 uniqueKey ,
393392 this . getSubscriberConfig . bind ( this ) ,
394393 platformClient
@@ -402,10 +401,10 @@ class WebhookRegistry {
402401 let url = `${ this . _fdkConfig . cluster } /service/common/webhook/v1.0/events/query-event-details` ;
403402 const uniqueKey = `${ url } _${ this . _fdkConfig . api_key } ` ;
404403
405- const retryInfo = retryManager . retryInfoMap . get ( uniqueKey ) ;
404+ const retryInfo = this . _retryManager . retryInfoMap . get ( uniqueKey ) ;
406405
407406 if ( retryInfo && ! retryInfo . isRetry ) {
408- retryManager . resetRetryState ( uniqueKey ) ;
407+ this . _retryManager . resetRetryState ( uniqueKey ) ;
409408 }
410409
411410 try {
@@ -440,9 +439,9 @@ class WebhookRegistry {
440439
441440 if (
442441 RetryManger . shouldRetryOnError ( err )
443- && ! retryManager . retryInfoMap . get ( uniqueKey ) ?. isRetryInProgress
442+ && ! this . _retryManager . isRetryInProgress ( uniqueKey )
444443 ) {
445- return await retryManager . retry ( uniqueKey , this . getEventConfig . bind ( this ) , handlerConfig ) ;
444+ return await this . _retryManager . retry ( uniqueKey , this . getEventConfig . bind ( this ) , handlerConfig ) ;
446445 }
447446
448447 throw new FdkInvalidWebhookConfig ( `Error while fetching webhook events configuration, Reason: ${ err . message } ` )
0 commit comments