Skip to content

Commit

Permalink
Merge pull request #3460 from Expensify/Rory-IsUserPublicDomain
Browse files Browse the repository at this point in the history
Store User_IsFromPublicDomain in Onyx
  • Loading branch information
Amal Nazeem authored Jun 14, 2021
2 parents 35d0848 + 0bb46ed commit 53b1596
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/libs/API.js
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,21 @@ function User_GetBetas() {
return Network.post('User_GetBetas');
}

/**
* @param {Object} parameters
* @param {String} parameters.email
* @param {Boolean} [parameters.requireCertainty]
* @returns {Promise}
*/
function User_IsFromPublicDomain(parameters) {
const commandName = 'User_IsFromPublicDomain';
requireParameters(['email'], parameters, commandName);
return Network.post(commandName, {
...{requireCertainty: true},
...parameters,
});
}

/**
* @param {Object} parameters
* @param {String} parameters.email
Expand Down Expand Up @@ -880,6 +895,7 @@ export {
UpdateAccount,
User_SignUp,
User_GetBetas,
User_IsFromPublicDomain,
User_ReopenAccount,
User_SecondaryLogin_Send,
User_UploadAvatar,
Expand Down
1 change: 1 addition & 0 deletions src/libs/Navigation/AppNavigator/AuthScreens.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ class AuthScreens extends React.Component {
PersonalDetails.fetchPersonalDetails();
User.getUserDetails();
User.getBetas();
User.getPublicDomainInfo();
PersonalDetails.fetchCurrencyPreferences();
fetchAllReports(true, true);
fetchCountryCodeByRequestIP();
Expand Down
41 changes: 40 additions & 1 deletion src/libs/actions/User.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import _ from 'underscore';
import lodashGet from 'lodash/get';
import Onyx from 'react-native-onyx';
import Str from 'expensify-common/lib/str';
import {PUBLIC_DOMAINS as COMMON_PUBLIC_DOMAINS} from 'expensify-common/lib/CONST';
import ONYXKEYS from '../../ONYXKEYS';
import * as API from '../API';
import CONST from '../../CONST';
import Navigation from '../Navigation/Navigation';
import ROUTES from '../../ROUTES';

let sessionAuthToken = '';
let sessionEmail = '';
Onyx.connect({
key: ONYXKEYS.SESSION,
callback: val => sessionAuthToken = val ? val.authToken : '',
callback: (val) => {
sessionAuthToken = lodashGet(val, 'authToken', '');
sessionEmail = lodashGet(val, 'email', '');
},
});

let currentlyViewedReportID = '';
Expand Down Expand Up @@ -168,6 +174,38 @@ function validateLogin(accountID, validateCode) {
});
}

/**
* Fetch the public domain info for the current user.
*
* This API is a bit weird in that it sometimes depends on information being cached in bedrock.
* If the info for the domain is not in bedrock, then it creates an asynchronous bedrock job to gather domain info.
* If that happens, this function will automatically retry itself in 10 minutes.
*/
function getPublicDomainInfo() {
// If this command fails, we'll retry again in 10 minutes,
// arbitrarily chosen giving Bedrock time to resolve the ClearbitCheckPublicEmail job for this email.
const RETRY_TIMEOUT = 600000;

// First check list of common public domains
if (_.contains(COMMON_PUBLIC_DOMAINS, sessionEmail)) {
Onyx.merge(ONYXKEYS.USER, {isFromPublicDomain: true});
return;
}

// If it is not a common public domain, check the API
API.User_IsFromPublicDomain({email: sessionEmail})
.then((response) => {
if (response.jsonCode === 200) {
const {isFromPublicDomain} = response;
Onyx.merge(ONYXKEYS.USER, {isFromPublicDomain});
} else {
// eslint-disable-next-line max-len
console.debug(`Command User_IsFromPublicDomain returned error code: ${response.jsonCode}. Most likely, this means that the domain ${Str.extractEmail(sessionEmail)} is not in the bedrock cache. Retrying in ${RETRY_TIMEOUT / 1000 / 60} minutes`);
setTimeout(getPublicDomainInfo, RETRY_TIMEOUT);
}
});
}

export {
changePassword,
getBetas,
Expand All @@ -176,4 +214,5 @@ export {
setExpensifyNewsStatus,
setSecondaryLogin,
validateLogin,
getPublicDomainInfo,
};

0 comments on commit 53b1596

Please sign in to comment.