Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store User_IsFromPublicDomain in Onyx #3460

Merged
merged 7 commits into from
Jun 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -120,6 +120,7 @@ class AuthScreens extends React.Component {
PersonalDetails.fetchPersonalDetails();
User.getUserDetails();
User.getBetas();
User.getPublicDomainInfo();
PersonalDetails.fetchCurrencyPreferences();
fetchAllReports(true, 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`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not E.cash knowledgable, so not a blocker, is there a reason why we do console.debug() in e.cash comparing to our Web code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well we can use this library for server logs. Those should show up in LogSearch I believe. I just didn't think we needed this in server logs, but we could put them there. What do you think?

But if you're asking why we used console.debug instead of console.log, it's mostly because we have some ESLint rule that bans console.log. I'm honestly not too sure why.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know honestly, we don't usually log errors in the console and just keep them in the server, that's why I am asking. But maybe since now the repo is public, this makes easier for contributors to debug, no idea haha

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well if this happens, we'll likely have server logs for the ClearbitCheckPublicEmail bedrock job that was just created, so let's just leave this for now. I think it'll maybe be useful for debugging, esp. since it's not obvious why this request might fail.

setTimeout(getPublicDomainInfo, RETRY_TIMEOUT);
}
});
}

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