@@ -8,21 +8,38 @@ function (user, context, callback) {
88 }
99
1010 const _ = require ( 'lodash' ) ;
11+ const moment = require ( 'moment' ) ;
1112
1213 let handle = _ . get ( user , "handle" , null ) ;
1314 const provider = _ . get ( user , "identities[0].provider" , null ) ;
1415 if ( ! handle && provider === "auth0" ) {
1516 handle = _ . get ( user , "nickname" , null ) ;
1617 }
1718
18- let createdAt = _ . get ( user , "created_at" , null ) ;
19- console . log ( 'rule:onboarding-checklist: user created at' , createdAt ) ;
19+ console . log ( "rule:onboarding-checklist: fetch onboarding_checklist for email/handle: " , user . email , handle , provider ) ;
20+
21+ const createdAt = _ . get ( user , "created_at" , null ) ;
22+ const thresholdDate = moment ( configuration . PROFILE_CREATION_DATE_THRESHOLD , "YYYY-MM-DD" ) ;
23+ console . log ( 'rule:onboarding-checklist: PROFILE_CREATION_DATE_THRESHOLD' , thresholdDate ) ;
24+
25+ try {
26+ // For users created before thresholdDate, we don't want to check onboarding_checklist
27+ // This is because older profiles might not have onboarding_checklist data and they don't need to see the onboarding_wizard
28+ if ( createdAt && ! thresholdDate . isBefore ( moment ( createdAt ) ) ) {
29+ console . log ( "rule:onboarding-checklist: user created before threshold date. Not checking onboarding_checklist." ) ;
30+ return callback ( null , user , context ) ;
31+ }
32+ } catch ( err ) {
33+ console . log ( "rule:onboarding-checklist: failed to compare userCreationDate" , createdAt , " with threshold. Error" , err ) ;
34+ }
2035
36+ /**
37+ * Returns M2M token needed to fetch onboarding_checklist
38+ */
2139 const getToken = function ( callback ) {
2240 if ( global . M2MToken ) {
2341 console . log ( 'rule:onboarding-checklist:M2M token is available' ) ;
24- const jwt = require ( 'jsonwebtoken' ) ;
25- const moment = require ( 'moment' ) ;
42+ const jwt = require ( 'jsonwebtoken' ) ;
2643 const decoded = jwt . decode ( global . M2MToken ) ;
2744 const exp = moment . unix ( decoded . exp ) ;
2845
@@ -59,25 +76,26 @@ function (user, context, callback) {
5976 console . log ( 'rule:onboarding-checklist:failed to fetch M2M token.' ) ;
6077 return callback ( null , user , context ) ;
6178 }
62-
63- console . log ( "rule:onboarding-checklist: fetch onboarding_checklist for email/handle: " , user . email , handle , provider ) ;
6479 global . AUTH0_CLAIM_NAMESPACE = "https://" + configuration . DOMAIN + "/" ;
65- try {
66- const axios = require ( 'axios@0.19.2' ) ;
80+ const axios = require ( 'axios@0.19.2' ) ;
81+
82+ const redirectUrl = `https://platform.${ configuration . DOMAIN } /onboard` ;
83+ const options = {
84+ method : 'GET' ,
85+ url : `https://api.${ configuration . DOMAIN } /v5/members/${ handle } /traits?traitIds=onboarding_checklist` ,
86+ headers : {
87+ Authorization : `Bearer ${ token } `
88+ }
89+ } ;
6790
68- const redirectUrl = `https://platform.${ configuration . DOMAIN } /onboard` ;
69- const options = {
70- method : 'GET' ,
71- url : `https://api.${ configuration . DOMAIN } /v5/members/${ handle } /traits?traitIds=onboarding_checklist` ,
72- headers : {
73- Authorization : `Bearer ${ token } `
74- }
75- } ;
76-
77- axios ( options ) . then ( result => {
78- const data = result . data ;
91+ // Fetch onboarding_checklist using v5 member Api.
92+ axios ( options )
93+ . then ( result => {
94+ try {
95+ const data = result . data ;
7996
80- if ( data . length === 0 ) {
97+ if ( data . length === 0 ) {
98+ // User doesn't have any traits with traitId onboarding_checklist and should be shown the onboarding wizard
8199 context . redirect = {
82100 url : redirectUrl
83101 } ;
@@ -89,9 +107,9 @@ function (user, context, callback) {
89107
90108 for ( let checklistTrait of onboardingChecklistTrait . data ) {
91109 if (
92- checklistTrait . onboarding_wizard !== null &&
93- ( checklistTrait . onboarding_wizard . status !== null ||
94- checklistTrait . onboarding_wizard . skip )
110+ checklistTrait . onboarding_wizard != null &&
111+ ( checklistTrait . onboarding_wizard . status != null || // any valid status indicates user has already seen onboarding wizard and needn't be shown again.
112+ checklistTrait . onboarding_wizard . skip ) // for certain signup routes skip is set to true, and thus onboarding wizard needn't be shown
95113 ) {
96114 return callback ( null , user , context ) ;
97115 }
@@ -110,21 +128,21 @@ function (user, context, callback) {
110128 }
111129 }
112130 }
113-
131+
132+ // All checks failed - indicating user newly registered and needs to be shown the onboarding wizard
114133 context . redirect = {
115134 url : redirectUrl
116135 } ;
117136 console . log ( 'rule:onboarding-checklist:Setting redirectUrl' , redirectUrl ) ;
118137 return callback ( null , user , context ) ;
119- } ) . catch ( requestError => {
120- console . log ( "rule:onboarding-checklist:Failed fetching onboarding_checklist with error " , requestError . response . status ) ;
138+ } catch ( e ) {
139+ console . log ( "rule:onboarding-checklist:Error in fetching onboarding_checklist" , e ) ;
121140 return callback ( null , user , context ) ;
122- } ) ;
123-
124- } catch ( e ) {
125- console . log ( "rule:onboarding-checklist:Error in fetching onboarding_checklist" , + e ) ;
141+ }
142+ } ) . catch ( requestError => {
143+ console . log ( "rule:onboarding-checklist:Failed fetching onboarding_checklist with error" , requestError . response . status ) ;
126144 return callback ( null , user , context ) ;
127- }
145+ } ) ;
128146 } ) ;
129147 } else {
130148 return callback ( null , user , context ) ;
0 commit comments