-
Notifications
You must be signed in to change notification settings - Fork 141
/
loadUser.js
186 lines (174 loc) · 5.58 KB
/
loadUser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import _ from 'lodash'
import {
ACCOUNTS_APP_CONNECTOR_URL,
LOAD_USER_SUCCESS,
LOAD_USER_FAILURE,
LOAD_ORG_CONFIG_SUCCESS,
LOAD_ORG_CONFIG_FAILURE,
ROLE_ADMINISTRATOR,
ROLE_CONNECT_COPILOT,
ROLE_TOPCODER_USER,
ROLE_CONNECT_MANAGER,
ROLE_CONNECT_ADMIN,
ROLE_ACCOUNT_EXECUTIVE,
ROLE_BUSINESS_DEVELOPMENT_REPRESENTATIVE,
ROLE_PROGRAM_MANAGER,
ROLE_PRESALES, ROLE_PROJECT_MANAGER, ROLE_SOLUTION_ARCHITECT
} from '../config/constants'
import { getFreshToken, configureConnector, decodeToken } from 'tc-accounts'
import { getUserProfile } from '../api/users'
import { getUserGroups } from '../api/groups'
import { getOrgConfig } from '../api/orgConfig'
import { EventTypes } from 'redux-segment'
configureConnector({
connectorUrl: ACCOUNTS_APP_CONNECTOR_URL,
frameId: 'tc-accounts-iframe'
})
export function loadUser() {
return ((dispatch, getState) => {
const state = getState()
const user = state.user
if (user) {
dispatch({ type: LOAD_USER_SUCCESS, user })
}
return getFreshToken()
.then((token) => {
return loadUserSuccess(dispatch, token)
})
.catch((err) => {
console.log(err)
return loadUserFailure(dispatch)
})
})
}
export function loadUserSuccess(dispatch, token) {
const decodedToken = decodeToken( token )
let currentUser = null
if (decodedToken.userId) {
currentUser = decodedToken
currentUser.id = currentUser.userId
currentUser.token = token
}
// uncomment to add admin role to the user for testing admin functionality locally
// currentUser.roles.push('administrator')
if (currentUser) {
getUserProfile(currentUser.handle).then((profile) => {
currentUser = _.assign(currentUser, profile)
// determine user role
let userRole
if (_.indexOf(currentUser.roles, ROLE_BUSINESS_DEVELOPMENT_REPRESENTATIVE) > -1) {
userRole = ROLE_BUSINESS_DEVELOPMENT_REPRESENTATIVE
} else if (_.indexOf(currentUser.roles, ROLE_PRESALES) > -1) {
userRole = ROLE_PRESALES
} else if (_.indexOf(currentUser.roles, ROLE_ACCOUNT_EXECUTIVE) > -1) {
userRole = ROLE_ACCOUNT_EXECUTIVE
} else if (_.indexOf(currentUser.roles, ROLE_PROGRAM_MANAGER) > -1) {
userRole = ROLE_PROGRAM_MANAGER
} else if (_.indexOf(currentUser.roles, ROLE_PROJECT_MANAGER) > -1) {
userRole = ROLE_PROJECT_MANAGER
} else if (_.indexOf(currentUser.roles, ROLE_SOLUTION_ARCHITECT) > -1) {
userRole = ROLE_SOLUTION_ARCHITECT
} else if (_.indexOf(currentUser.roles, ROLE_ADMINISTRATOR) > -1) {
userRole = ROLE_ADMINISTRATOR
} else if (_.indexOf(currentUser.roles, ROLE_CONNECT_ADMIN) > -1) {
userRole = ROLE_CONNECT_ADMIN
} else if (_.indexOf(currentUser.roles, ROLE_CONNECT_MANAGER) > -1) {
userRole = ROLE_CONNECT_MANAGER
} else if (_.indexOf(currentUser.roles, ROLE_CONNECT_COPILOT) > -1) {
userRole = ROLE_CONNECT_COPILOT
} else {
userRole = ROLE_TOPCODER_USER
}
const analyticsEvents = [{
eventType: EventTypes.identify,
eventPayload: {
userId: currentUser.id,
traits: {
id: currentUser.id,
role: userRole,
username: currentUser.handle,
firstName: currentUser.firstName,
lastName: currentUser.lastName,
email: currentUser.email,
createdAt: currentUser.createdAt,
avatar: currentUser.photoURL
}
}
}]
if (window.analytics && window.analytics.user && window.analytics.user()) {
const anonymousId = window.analytics.user().anonymousId()
if (anonymousId) {
analyticsEvents.push({
eventType: EventTypes.alias,
eventPayload: {
userId: currentUser.id,
previousId: anonymousId
}
})
}
}
dispatch({
type: LOAD_USER_SUCCESS,
user : currentUser,
meta: {
analytics: analyticsEvents
}
})
loadGroups(dispatch, currentUser.userId)
})
.catch((err) => {
// if we fail to load user's profile, still dispatch user load success
// ideally it shouldn't happen, but if it is, we can render the page
// without profile information
console.log(err)
dispatch({ type: LOAD_USER_SUCCESS, user : currentUser })
})
}
}
export function loadUserFailure(dispatch) {
dispatch({ type: LOAD_USER_FAILURE })
}
/**
* Load groups for the user.
*
* @param {Object} dispatch dispatch
* @param {Number} userId user id
*/
function loadGroups(dispatch, userId) {
if (userId) {
getUserGroups(userId, 'user').then((groups) => {
const groupIds = _.compact(_.uniq([
// get old and new ids as organizations may refer to any of them
..._.map(groups, 'oldId'),
..._.map(groups, 'id')
]))
if (groupIds.length > 0) {
loadOrganizationConfigSuccess(dispatch, _.join(groupIds, ','))
}
})
.catch((err) => {
// if we fail to load groups
console.log(err)
})
}
}
/**
* Load organization configurations for the groups.
*
* @param {Object} dispatch dispatch
* @param {String} groupIds group ids
*/
function loadOrganizationConfigSuccess(dispatch, groupIds) {
getOrgConfig(groupIds)
.then((orgConfigs) => {
dispatch({
type: LOAD_ORG_CONFIG_SUCCESS,
orgConfig : orgConfigs
})
})
.catch((err) => {
// if we fail to load organization configs
console.log(err)
dispatch({ type: LOAD_ORG_CONFIG_FAILURE })
})
}