11function ( user , context , callback ) {
22 if ( context . clientID === configuration . CLIENT_ACCOUNTS_LOGIN ) {
33 console . log ( "rule:onboarding-checklist:enter" ) ;
4+ console . log ( "rule:onboarding-checklist:context.request" , context . request ) ;
45
56 if ( context . redirect ) {
67 console . log ( "rule:onboarding-checklist:exiting due to context being a redirect" ) ;
78 return callback ( null , user , context ) ;
89 }
910
1011 const _ = require ( 'lodash' ) ;
12+ const moment = require ( 'moment' ) ;
1113
1214 let handle = _ . get ( user , "handle" , null ) ;
1315 const provider = _ . get ( user , "identities[0].provider" , null ) ;
1416 if ( ! handle && provider === "auth0" ) {
1517 handle = _ . get ( user , "nickname" , null ) ;
1618 }
1719
18- let createdAt = _ . get ( user , "created_at" , null ) ;
19- console . log ( 'rule:onboarding-checklist: user created at' , createdAt ) ;
20+ console . log ( "rule:onboarding-checklist: fetch onboarding_checklist for email/handle: " , user . email , handle , provider ) ;
21+
22+ const createdAt = _ . get ( user , "created_at" , null ) ;
23+ const thresholdDate = moment ( configuration . PROFILE_CREATION_DATE_THRESHOLD , "YYYY-MM-DD" ) ;
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,39 +76,37 @@ 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 options = {
83+ method : 'GET' ,
84+ url : `https://api.${ configuration . DOMAIN } /v5/members/${ handle } /traits?traitIds=onboarding_checklist` ,
85+ headers : {
86+ Authorization : `Bearer ${ token } `
87+ }
88+ } ;
6789
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 ;
90+ // Fetch onboarding_checklist using v5 member Api.
91+ axios ( options )
92+ . then ( result => {
93+ try {
94+ const data = result . data ;
7995
80- if ( data . length === 0 ) {
81- context . redirect = {
82- url : redirectUrl
83- } ;
84- console . log ( 'rule:onboarding-checklist:Setting redirectUrl' , redirectUrl ) ;
96+ if ( data . length === 0 ) {
97+ // User doesn't have any traits with traitId onboarding_checklist and should be shown the onboarding wizard
98+ user . show_onboarding_wizard = true ;
99+ console . log ( 'rule:onboarding-checklist:Setting show_onboarding_wizard to true' , user ) ;
85100 return callback ( null , user , context ) ;
86101 }
87102
88103 const onboardingChecklistTrait = data . filter ( ( item ) => item . traitId === 'onboarding_checklist' ) [ 0 ] . traits ;
89104
90105 for ( let checklistTrait of onboardingChecklistTrait . data ) {
91106 if (
92- checklistTrait . onboarding_wizard !== null &&
93- ( checklistTrait . onboarding_wizard . status !== null ||
94- checklistTrait . onboarding_wizard . skip )
107+ checklistTrait . onboarding_wizard != null &&
108+ ( checklistTrait . onboarding_wizard . status != null || // any valid status indicates user has already seen onboarding wizard and needn't be shown again.
109+ checklistTrait . onboarding_wizard . skip ) // for certain signup routes skip is set to true, and thus onboarding wizard needn't be shown
95110 ) {
96111 return callback ( null , user , context ) ;
97112 }
@@ -110,21 +125,19 @@ function (user, context, callback) {
110125 }
111126 }
112127 }
113-
114- context . redirect = {
115- url : redirectUrl
116- } ;
117- console . log ( 'rule:onboarding-checklist:Setting redirectUrl' , redirectUrl ) ;
128+
129+ // All checks failed - indicating user newly registered and needs to be shown the onboarding wizard
130+ console . log ( 'rule:onboarding-checklist: set show_onboarding_wizard' , user ) ;
131+ user . show_onboarding_wizard = true ;
118132 return callback ( null , user , context ) ;
119- } ) . catch ( requestError => {
120- console . log ( "rule:onboarding-checklist:Failed fetching onboarding_checklist with error " , requestError . response . status ) ;
133+ } catch ( e ) {
134+ console . log ( "rule:onboarding-checklist:Error in fetching onboarding_checklist" , e ) ;
121135 return callback ( null , user , context ) ;
122- } ) ;
123-
124- } catch ( e ) {
125- console . log ( "rule:onboarding-checklist:Error in fetching onboarding_checklist" , + e ) ;
136+ }
137+ } ) . catch ( requestError => {
138+ console . log ( "rule:onboarding-checklist:Failed fetching onboarding_checklist with error" , requestError . response . status ) ;
126139 return callback ( null , user , context ) ;
127- }
140+ } ) ;
128141 } ) ;
129142 } else {
130143 return callback ( null , user , context ) ;
0 commit comments