-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathPersonalDetails.js
353 lines (313 loc) · 12.5 KB
/
PersonalDetails.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
import _ from 'underscore';
import lodashGet from 'lodash/get';
import lodashMerge from 'lodash/merge';
import Onyx from 'react-native-onyx';
import Str from 'expensify-common/lib/str';
import ONYXKEYS from '../../ONYXKEYS';
import CONST from '../../CONST';
import NetworkConnection from '../NetworkConnection';
import * as API from '../API';
import NameValuePair from './NameValuePair';
import * as ReportUtils from '../reportUtils';
import * as OptionsListUtils from '../OptionsListUtils';
import Growl from '../Growl';
import * as Localize from '../Localize';
import Timing from './Timing';
let currentUserEmail = '';
Onyx.connect({
key: ONYXKEYS.SESSION,
callback: val => currentUserEmail = val ? val.email : '',
});
let personalDetails;
Onyx.connect({
key: ONYXKEYS.PERSONAL_DETAILS,
callback: val => personalDetails = val,
});
/**
* Returns the URL for a user's avatar and handles someone not having any avatar at all
*
* @param {Object} personalDetail
* @param {String} login
* @returns {String}
*/
function getAvatar(personalDetail, login) {
if (personalDetail && personalDetail.avatarThumbnail) {
return personalDetail.avatarThumbnail;
}
return OptionsListUtils.getDefaultAvatar(login);
}
/**
* Returns the displayName for a user
*
* @param {String} login
* @param {Object} [personalDetail]
* @returns {String}
*/
function getDisplayName(login, personalDetail) {
// If we have a number like +15857527441@expensify.sms then let's remove @expensify.sms
// so that the option looks cleaner in our UI.
const userLogin = Str.removeSMSDomain(login);
const userDetails = personalDetail || lodashGet(personalDetails, login);
if (!userDetails) {
return userLogin;
}
const firstName = userDetails.firstName || '';
const lastName = userDetails.lastName || '';
const fullName = (`${firstName} ${lastName}`).trim();
return fullName || userLogin;
}
/**
* Returns max character error text if true.
*
* @param {Boolean} isError
* @returns {String}
*/
function getMaxCharacterError(isError) {
return isError ? Localize.translateLocal('personalDetails.error.characterLimit', {limit: 50}) : '';
}
/**
* Format personal details
*
* @param {Object} personalDetailsList
* @return {Object}
*/
function formatPersonalDetails(personalDetailsList) {
Timing.start(CONST.TIMING.PERSONAL_DETAILS_FORMATTED);
const formattedResult = {};
// This method needs to be SUPER PERFORMANT because it can be called with a massive list of logins depending on the policies that someone belongs to
// eslint-disable-next-line rulesdir/prefer-underscore-method
Object.keys(personalDetailsList).forEach((login) => {
const personalDetailsResponse = personalDetailsList[login];
// Form the details into something that has all the data in an easy to use format.
const avatar = getAvatar(personalDetailsResponse, login);
const displayName = getDisplayName(login, personalDetailsResponse);
const pronouns = personalDetailsResponse.pronouns || '';
const timezone = personalDetailsResponse.timeZone || CONST.DEFAULT_TIME_ZONE;
const firstName = personalDetailsResponse.firstName || '';
const lastName = personalDetailsResponse.lastName || '';
const payPalMeAddress = personalDetailsResponse.expensify_payPalMeAddress || '';
const phoneNumber = personalDetailsResponse.phoneNumber || '';
formattedResult[login] = {
login,
avatar,
displayName,
firstName,
lastName,
pronouns,
timezone,
payPalMeAddress,
phoneNumber,
};
});
Timing.end(CONST.TIMING.PERSONAL_DETAILS_FORMATTED);
return formattedResult;
}
/**
* Get the personal details for our organization
* @returns {Promise}
*/
function fetchPersonalDetails() {
return API.Get({
returnValueList: 'personalDetailsList',
})
.then((data) => {
let myPersonalDetails = {};
// If personalDetailsList does not have the current user ensure we initialize their details with an empty
// object at least
const personalDetailsList = _.isEmpty(data.personalDetailsList) ? {} : data.personalDetailsList;
if (!personalDetailsList[currentUserEmail]) {
personalDetailsList[currentUserEmail] = {};
}
const allPersonalDetails = formatPersonalDetails(personalDetailsList);
Onyx.merge(ONYXKEYS.PERSONAL_DETAILS, allPersonalDetails);
myPersonalDetails = allPersonalDetails[currentUserEmail];
// Add the first and last name to the current user's MY_PERSONAL_DETAILS key
myPersonalDetails.firstName = lodashGet(data.personalDetailsList, [currentUserEmail, 'firstName'], '');
myPersonalDetails.lastName = lodashGet(data.personalDetailsList, [currentUserEmail, 'lastName'], '');
// Set my personal details so they can be easily accessed and subscribed to on their own key
Onyx.merge(ONYXKEYS.MY_PERSONAL_DETAILS, myPersonalDetails);
});
}
/**
* Get personal details from report participants.
*
* @param {Object} reports
*/
function getFromReportParticipants(reports) {
const participantEmails = _.chain(reports)
.pluck('participants')
.flatten()
.unique()
.value();
if (participantEmails.length === 0) {
return;
}
API.PersonalDetails_GetForEmails({emailList: participantEmails.join(',')})
.then((data) => {
const existingDetails = _.pick(data, participantEmails);
// Fallback to add logins that don't appear in the response
const details = _.chain(participantEmails)
.filter(login => !data[login])
.reduce((previousDetails, login) => ({
...previousDetails,
[login]: {}, // Simply just need the key to exist
}), existingDetails)
.value();
const formattedPersonalDetails = formatPersonalDetails(details);
Onyx.merge(ONYXKEYS.PERSONAL_DETAILS, formattedPersonalDetails);
// The personalDetails of the participants contain their avatar images. Here we'll go over each
// report and based on the participants we'll link up their avatars to report icons. This will
// skip over default rooms which aren't named by participants.
const reportsToUpdate = {};
_.each(reports, (report) => {
if (report.participants.length <= 0 && !ReportUtils.isChatRoom(report) && !ReportUtils.isPolicyExpenseChat(report)) {
return;
}
const avatars = OptionsListUtils.getReportIcons(report, details);
const reportName = (ReportUtils.isChatRoom(report) || ReportUtils.isPolicyExpenseChat(report))
? report.reportName
: _.chain(report.participants)
.filter(participant => participant !== currentUserEmail)
.map(participant => lodashGet(
formattedPersonalDetails,
[participant, 'displayName'],
participant,
))
.value()
.join(', ');
reportsToUpdate[`${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`] = {icons: avatars, reportName};
});
// We use mergeCollection such that it updates ONYXKEYS.COLLECTION.REPORT in one go.
// Any withOnyx subscribers to this key will also receive the complete updated props just once
// than updating props for each report and re-rendering had merge been used.
Onyx.mergeCollection(ONYXKEYS.COLLECTION.REPORT, reportsToUpdate);
});
}
/**
* Merges partial details object into the local store.
*
* @param {Object} details
* @private
*/
function mergeLocalPersonalDetails(details) {
// We are merging the partial details provided to this method with the existing details we have for the user so
// that we don't overwrite any values that may exist in storage.
const mergedDetails = lodashMerge(personalDetails[currentUserEmail], details);
// displayName is a generated field so we'll use the firstName and lastName + login to update it.
mergedDetails.displayName = getDisplayName(currentUserEmail, mergedDetails);
// Update the associated Onyx keys
Onyx.merge(ONYXKEYS.MY_PERSONAL_DETAILS, mergedDetails);
Onyx.merge(ONYXKEYS.PERSONAL_DETAILS, {[currentUserEmail]: mergedDetails});
}
/**
* Sets the personal details object for the current user
*
* @param {Object} details
* @param {boolean} shouldGrowl
*/
function setPersonalDetails(details, shouldGrowl) {
API.PersonalDetails_Update({details: JSON.stringify(details)})
.then((response) => {
if (response.jsonCode === 200) {
if (details.timezone) {
NameValuePair.set(CONST.NVP.TIMEZONE, details.timezone);
}
mergeLocalPersonalDetails(details);
if (shouldGrowl) {
Growl.show(Localize.translateLocal('profilePage.growlMessageOnSave'), CONST.GROWL.SUCCESS, 3000);
}
} else if (response.jsonCode === 400) {
Growl.error(Localize.translateLocal('personalDetails.error.firstNameLength'), 3000);
} else if (response.jsonCode === 401) {
Growl.error(Localize.translateLocal('personalDetails.error.lastNameLength'), 3000);
}
}).catch((error) => {
console.debug('Error while setting personal details', error);
});
}
/**
* Sets the onyx with the currency list from the network
* @returns {Object}
*/
function getCurrencyList() {
return API.GetCurrencyList()
.then((data) => {
const currencyListObject = JSON.parse(data.currencyList);
Onyx.merge(ONYXKEYS.CURRENCY_LIST, currencyListObject);
return currencyListObject;
});
}
/**
* Fetches the local currency based on location and sets currency code/symbol to local storage
*/
function fetchLocalCurrency() {
const coords = {};
let currency = '';
Onyx.merge(ONYXKEYS.IOU, {
isRetrievingCurrency: true,
});
API.GetLocalCurrency({...coords})
.then((data) => {
currency = data.currency;
})
.then(getCurrencyList)
.then(() => {
Onyx.merge(ONYXKEYS.MY_PERSONAL_DETAILS, {localCurrencyCode: currency});
})
.finally(() => {
Onyx.merge(ONYXKEYS.IOU, {
isRetrievingCurrency: false,
});
});
}
/**
* Sets the user's avatar image
*
* @param {File|Object} file
*/
function setAvatar(file) {
setPersonalDetails({avatarUploading: true});
API.User_UploadAvatar({file})
.then((response) => {
// Once we get the s3url back, update the personal details for the user with the new avatar URL
if (response.jsonCode !== 200) {
const error = new Error();
error.jsonCode = response.jsonCode;
throw error;
}
setPersonalDetails({avatar: response.s3url, avatarUploading: false});
})
.catch((error) => {
setPersonalDetails({avatarUploading: false});
if (error.jsonCode === 405 || error.jsonCode === 502) {
Growl.show(Localize.translateLocal('profilePage.invalidFileMessage'), CONST.GROWL.ERROR, 3000);
} else {
Growl.show(Localize.translateLocal('profilePage.avatarUploadFailureMessage'), CONST.GROWL.ERROR, 3000);
}
});
}
/**
* Replaces the user's avatar image with a default avatar
*
* @param {String} defaultAvatarURL
*/
function deleteAvatar(defaultAvatarURL) {
// We don't want to save the default avatar URL in the backend since we don't want to allow
// users the option of removing the default avatar, instead we'll save an empty string
API.PersonalDetails_Update({details: JSON.stringify({avatar: ''})});
mergeLocalPersonalDetails({avatar: defaultAvatarURL});
}
// When the app reconnects from being offline, fetch all of the personal details
NetworkConnection.onReconnect(fetchPersonalDetails);
export {
fetchPersonalDetails,
formatPersonalDetails,
getFromReportParticipants,
getDisplayName,
setPersonalDetails,
setAvatar,
deleteAvatar,
fetchLocalCurrency,
getCurrencyList,
getMaxCharacterError,
};