1
- function ( user , context , callback ) {
1
+ function OnboardingChecklist ( user , context , callback ) {
2
2
if ( context . clientID === configuration . CLIENT_ACCOUNTS_LOGIN ) {
3
3
console . log ( "rule:onboarding-checklist:enter" ) ;
4
-
5
- if ( context . redirect ) {
4
+
5
+ if ( context . redirect ) {
6
6
console . log ( "rule:onboarding-checklist:exiting due to context being a redirect" ) ;
7
7
return callback ( null , user , context ) ;
8
8
}
@@ -24,15 +24,15 @@ function (user, context, callback) {
24
24
const handle = context . idToken [ global . AUTH0_CLAIM_NAMESPACE + 'handle' ] ;
25
25
console . log ( "rule:onboarding-checklist: fetch onboarding_checklist for email/handle: " , user . email , handle ) ;
26
26
27
- const roles = context . idToken [ global . AUTH0_CLAIM_NAMESPACE + 'roles' ]
28
-
29
- if ( roles && roles . includes ( 'Topcoder Customer' ) ) {
30
- console . log ( "rule:onboarding-checklist:exiting due to user being a customer." ) ;
27
+ if ( handle === null ) {
28
+ console . log ( "rule:onboarding-checklist: exiting due to handle being null." ) ;
31
29
return callback ( null , user , context ) ;
32
30
}
31
+
32
+ const roles = context . idToken [ global . AUTH0_CLAIM_NAMESPACE + 'roles' ] ;
33
33
34
- if ( handle == null ) {
35
- console . log ( "rule:onboarding-checklist: exiting due to handle being null ." ) ;
34
+ if ( roles && roles . includes ( 'Topcoder Customer' ) ) {
35
+ console . log ( "rule:onboarding-checklist:exiting due to user being a customer ." ) ;
36
36
return callback ( null , user , context ) ;
37
37
}
38
38
@@ -49,14 +49,14 @@ function (user, context, callback) {
49
49
} catch ( err ) {
50
50
console . log ( "rule:onboarding-checklist: failed to compare userCreationDate" , createdAt , " with threshold. Error" , err ) ;
51
51
}
52
-
52
+
53
53
/**
54
54
* Returns M2M token needed to fetch onboarding_checklist
55
55
*/
56
- const getToken = function ( callback ) {
56
+ const getToken = function ( callback ) {
57
57
if ( global . M2MToken ) {
58
58
console . log ( 'rule:onboarding-checklist:M2M token is available' ) ;
59
- const jwt = require ( 'jsonwebtoken' ) ;
59
+ const jwt = require ( 'jsonwebtoken' ) ;
60
60
const decoded = jwt . decode ( global . M2MToken ) ;
61
61
const exp = moment . unix ( decoded . exp ) ;
62
62
@@ -79,7 +79,7 @@ function (user, context, callback) {
79
79
}
80
80
} , function ( err , response , body ) {
81
81
if ( err ) {
82
- return callback ( err ) ;
82
+ return callback ( err ) ;
83
83
}
84
84
85
85
global . M2MToken = body . access_token ;
@@ -88,83 +88,82 @@ function (user, context, callback) {
88
88
} ) ;
89
89
} ;
90
90
91
- getToken ( function ( err , token ) {
91
+ getToken ( function ( err , token ) {
92
92
if ( err ) {
93
93
console . log ( 'rule:onboarding-checklist:failed to fetch M2M token.' ) ;
94
94
return callback ( null , user , context ) ;
95
95
}
96
96
global . AUTH0_CLAIM_NAMESPACE = "https://" + configuration . DOMAIN + "/" ;
97
97
const axios = require ( '[email protected] ' ) ;
98
-
98
+
99
99
const options = {
100
100
method : 'GET' ,
101
101
url : `https://api.${ configuration . DOMAIN } /v5/members/${ handle } /traits?traitIds=onboarding_checklist` ,
102
102
headers : {
103
103
Authorization : `Bearer ${ token } `
104
104
}
105
105
} ;
106
-
106
+
107
107
// Fetch onboarding_checklist using v5 member Api.
108
108
axios ( options )
109
- . then ( result => {
110
- try {
111
- const data = result . data ;
112
-
113
- if ( data . length === 0 ) {
114
- // User doesn't have any traits with traitId onboarding_checklist and should be shown the onboarding wizard
115
- context . idToken [ global . AUTH0_CLAIM_NAMESPACE + 'onboarding_wizard' ] = 'show' ;
116
- console . log ( 'rule:onboarding-checklist:Setting onboarding_wizard to show' ) ;
117
- return callback ( null , user , context ) ;
118
- }
109
+ . then ( result => {
110
+ try {
111
+ const data = result . data ;
112
+
113
+ if ( data . length === 0 ) {
114
+ // User doesn't have any traits with traitId onboarding_checklist and should be shown the onboarding wizard
115
+ context . idToken [ global . AUTH0_CLAIM_NAMESPACE + 'onboarding_wizard' ] = 'show' ;
116
+ console . log ( 'rule:onboarding-checklist:Setting onboarding_wizard to show' ) ;
117
+ return callback ( null , user , context ) ;
118
+ }
119
119
120
- const onboardingChecklistTrait = data . filter ( ( item ) => item . traitId === 'onboarding_checklist' ) [ 0 ] . traits ;
121
- let override = 'show' ;
122
-
123
- for ( let checklistTrait of onboardingChecklistTrait . data ) {
124
- if ( checklistTrait . onboarding_wizard != null ) {
125
- if ( checklistTrait . onboarding_wizard . status !== 'pending_at_user' || // any non pending_at_user status indicates OB was either seen or completed and can be skipped
126
- checklistTrait . onboarding_wizard . skip || // for certain signup routes skip is set to true, and thus onboarding wizard needn't be shown
127
- checklistTrait . onboarding_wizard . override === 'skip' )
128
- {
129
- return callback ( null , user , context ) ;
130
- } else if ( checklistTrait . onboarding_wizard . override === 'useRetUrl' ) {
131
- override = 'useRetUrl' ;
120
+ const onboardingChecklistTrait = data . filter ( ( item ) => item . traitId === 'onboarding_checklist' ) [ 0 ] . traits ;
121
+ let override = 'show' ;
122
+
123
+ for ( let checklistTrait of onboardingChecklistTrait . data ) {
124
+ if ( checklistTrait . onboarding_wizard != = null ) {
125
+ if ( checklistTrait . onboarding_wizard . status !== 'pending_at_user' || // any non pending_at_user status indicates OB was either seen or completed and can be skipped
126
+ checklistTrait . onboarding_wizard . skip || // for certain signup routes skip is set to true, and thus onboarding wizard needn't be shown
127
+ checklistTrait . onboarding_wizard . override === 'skip' ) {
128
+ return callback ( null , user , context ) ;
129
+ } else if ( checklistTrait . onboarding_wizard . override === 'useRetUrl' ) {
130
+ override = 'useRetUrl' ;
131
+ }
132
132
}
133
133
}
134
- }
135
134
136
- const profileCompletedData = onboardingChecklistTrait . data . length > 0 ? onboardingChecklistTrait . data [ 0 ] . profile_completed : null ;
137
-
138
- if ( profileCompletedData ) {
139
- if ( profileCompletedData . status === "completed" ) {
140
- return callback ( null , user , context ) ;
141
- }
142
-
143
- for ( const item in profileCompletedData . metadata ) {
144
- if ( profileCompletedData . metadata [ item ] ) {
135
+ const profileCompletedData = onboardingChecklistTrait . data . length > 0 ? onboardingChecklistTrait . data [ 0 ] . profile_completed : null ;
136
+
137
+ if ( profileCompletedData ) {
138
+ if ( profileCompletedData . status === "completed" ) {
145
139
return callback ( null , user , context ) ;
146
140
}
141
+
142
+ for ( const item in profileCompletedData . metadata ) {
143
+ if ( profileCompletedData . metadata [ item ] ) {
144
+ return callback ( null , user , context ) ;
145
+ }
146
+ }
147
147
}
148
- }
149
-
150
- // All checks failed - indicating user newly registered and needs to be shown the onboarding wizard
151
- console . log ( 'rule:onboarding-checklist: set onboarding_wizard ' + override ) ;
152
-
153
- context . idToken [ global . AUTH0_CLAIM_NAMESPACE + 'onboarding_wizard' ] = override ;
154
148
149
+ // All checks failed - indicating user newly registered and needs to be shown the onboarding wizard
150
+ console . log ( 'rule:onboarding-checklist: set onboarding_wizard ' + override ) ;
155
151
152
+ context . idToken [ global . AUTH0_CLAIM_NAMESPACE + 'onboarding_wizard' ] = override ;
156
153
154
+
155
+
156
+ return callback ( null , user , context ) ;
157
+ } catch ( e ) {
158
+ console . log ( "rule:onboarding-checklist:Error in fetching onboarding_checklist" , e ) ;
159
+ return callback ( null , user , context ) ;
160
+ }
161
+ } ) . catch ( requestError => {
162
+ console . log ( "rule:onboarding-checklist:Failed fetching onboarding_checklist with error" , requestError . response . status ) ;
157
163
return callback ( null , user , context ) ;
158
- } catch ( e ) {
159
- console . log ( "rule:onboarding-checklist:Error in fetching onboarding_checklist" , e ) ;
160
- return callback ( null , user , context ) ;
161
- }
162
- } ) . catch ( requestError => {
163
- console . log ( "rule:onboarding-checklist:Failed fetching onboarding_checklist with error" , requestError . response . status ) ;
164
- return callback ( null , user , context ) ;
165
- } ) ;
164
+ } ) ;
166
165
} ) ;
167
166
} else {
168
167
return callback ( null , user , context ) ;
169
168
}
170
- }
169
+ }
0 commit comments