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