11const jsforce = require ( 'jsforce' ) ;
22const { messages } = require ( 'elasticio-node' ) ;
33const { callJSForceMethod } = require ( '../helpers/wrapper' ) ;
4- const { getCredentials , refreshToken } = require ( '../helpers/oauth2Helper ' ) ;
5- const { REFRESH_TOKEN_RETRIES , SALESFORCE_API_VERSION } = require ( '../common.js' ) . globalConsts ;
4+ const { getSecret , refreshToken } = require ( '../util ' ) ;
5+ const { SALESFORCE_API_VERSION } = require ( '../common.js' ) . globalConsts ;
66
7+ let fayeClient ;
78/**
89 * This method will be called from elastic.io platform providing following data
910 *
@@ -12,15 +13,13 @@ const { REFRESH_TOKEN_RETRIES, SALESFORCE_API_VERSION } = require('../common.js'
1213 */
1314async function processTrigger ( msg , configuration ) {
1415 this . logger . info ( 'Starting Subscribe to platform events Trigger' ) ;
15- let iteration = REFRESH_TOKEN_RETRIES ;
16- let refreshTokenSuccess = false ;
1716 const { secretId } = configuration ;
1817 if ( ! secretId ) {
1918 this . logger . error ( 'secretId is missing in configuration, credentials cannot be fetched' ) ;
2019 throw new Error ( 'secretId is missing in configuration, credentials cannot be fetched' ) ;
2120 }
2221 this . logger . debug ( 'Fetching credentials by secretId' ) ;
23- const credentials = await getCredentials ( this , secretId ) ;
22+ const { credentials } = await getSecret ( this , secretId ) ;
2423 const accessToken = credentials . access_token ;
2524 const instanceUrl = credentials . undefined_params . instance_url ;
2625 this . logger . trace ( 'AccessToken = %s' , accessToken ) ;
@@ -33,52 +32,46 @@ async function processTrigger(msg, configuration) {
3332 const topic = `/event/${ configuration . object } ` ;
3433 const replayId = - 1 ;
3534 this . logger . debug ( 'Creating streaming client' ) ;
36- const fayeClient = connection . streaming . createClient ( [
37- new jsforce . StreamingExtension . Replay ( topic , replayId ) ,
38- new jsforce . StreamingExtension . AuthFailure ( async ( err ) => {
39- this . logger . trace ( 'AuthFailure: %j' , err ) ;
40- // eslint-disable-next-line max-len
41- // if (err.ext && err.ext.sfdc && err.ext.sfdc.failureReason && (err.ext.sfdc.failureReason === '401::Authentication invalid') && (iteration > 0)) {
42- this . logger . trace ( 'err.ext.sfdc.failureReason: %s' , err . ext . sfdc . failureReason ) ;
43- if ( err . ext . sfdc . failureReason === '401::Authentication invalid' ) {
44- do {
45- iteration -= 1 ;
35+ if ( ! fayeClient ) {
36+ fayeClient = connection . streaming . createClient ( [
37+ new jsforce . StreamingExtension . Replay ( topic , replayId ) ,
38+ new jsforce . StreamingExtension . AuthFailure ( async ( err ) => {
39+ this . logger . trace ( 'AuthFailure: %j' , err ) ;
40+ if ( err . ext && err . ext . sfdc && err . ext . sfdc . failureReason && ( err . ext . sfdc . failureReason === '401::Authentication invalid' ) ) {
4641 try {
47- this . logger . debug ( 'Session is expired, trying to refresh token, iteration: %s' , REFRESH_TOKEN_RETRIES - iteration ) ;
48- refreshTokenSuccess = await refreshToken ( this , secretId ) ;
49- this . logger . debug ( 'Token is successfully refreshed, iteration: %s' , REFRESH_TOKEN_RETRIES - iteration ) ;
50- break ;
42+ this . logger . debug ( 'Session is expired, trying to refresh token' ) ;
43+ await refreshToken ( this , secretId ) ;
44+ this . logger . debug ( 'Token is successfully refreshed' ) ;
5145 } catch ( error ) {
52- this . logger . error ( 'Failed to refresh token, iteration: %s' , REFRESH_TOKEN_RETRIES - iteration ) ;
46+ this . logger . trace ( 'Refresh token error: %j' , error ) ;
47+ this . logger . error ( 'Failed to fetch and/or refresh token' ) ;
48+ throw new Error ( 'Failed to fetch and/or refresh token' ) ;
5349 }
54- } while ( iteration > 0 ) ;
55- if ( ! refreshTokenSuccess ) {
56- this . logger . error ( 'Failed to fetch and/or refresh token, retries exceeded' ) ;
57- throw new Error ( 'Failed to fetch and/or refresh token, retries exceeded' ) ;
50+ fayeClient = undefined ;
51+ this . logger . info ( 'Lets call processTrigger one more time' ) ;
52+ await processTrigger . call ( this , msg , configuration ) ;
53+ } else {
54+ this . logger . error ( 'AuthFailure extension error occurred' ) ;
55+ throw err ;
5856 }
59- this . logger . info ( 'Lets call processTrigger one more time' ) ;
60- await processTrigger . call ( this , msg , configuration ) ;
61- } else {
62- this . logger . error ( 'AuthFailure extension error occurred' ) ;
63- throw err ;
64- }
65- } ) ,
66- ] ) ;
57+ } ) ,
58+ ] ) ;
6759
68- fayeClient . subscribe ( topic , async ( message ) => {
69- this . logger . info ( 'Incoming message found, going to emit...' ) ;
70- this . logger . trace ( 'Incoming Message: %j' , message ) ;
71- await this . emit ( 'data' , messages . newMessageWithBody ( message ) ) ;
72- } )
73- . then ( ( ) => {
74- this . logger . info ( 'Subscribed to PushTopic successfully' ) ;
75- this . logger . trace ( `Subscribed to PushTopic: ${ topic } ` ) ;
76- } ,
77- ( err ) => {
78- this . logger . error ( 'Subscriber error occurred' ) ;
79- throw err ;
80- } ) ;
81- this . logger . info ( 'Streaming client created and ready' ) ;
60+ fayeClient . subscribe ( topic , async ( message ) => {
61+ this . logger . info ( 'Incoming message found, going to emit...' ) ;
62+ this . logger . trace ( 'Incoming Message: %j' , message ) ;
63+ await this . emit ( 'data' , messages . newMessageWithBody ( message ) ) ;
64+ } )
65+ . then ( ( ) => {
66+ this . logger . info ( 'Subscribed to PushTopic successfully' ) ;
67+ this . logger . trace ( `Subscribed to PushTopic: ${ topic } ` ) ;
68+ } ,
69+ ( err ) => {
70+ this . logger . error ( 'Subscriber error occurred' ) ;
71+ throw err ;
72+ } ) ;
73+ this . logger . info ( 'Streaming client created and ready' ) ;
74+ }
8275}
8376
8477/**
0 commit comments