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

Fix pay someone flow for unvalidated accounts #50835

Merged
merged 9 commits into from
Nov 12, 2024
4 changes: 2 additions & 2 deletions src/libs/Network/SequentialQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ function flush() {
return;
}

if (PersistedRequests.getAll().length === 0) {
Log.info('[SequentialQueue] Unable to flush. No requests to process.');
if (PersistedRequests.getAll().length === 0 && QueuedOnyxUpdates.isEmpty()) {
Log.info('[SequentialQueue] Unable to flush. No requests or queued Onyx updates to process.');
return;
}

Expand Down
33 changes: 31 additions & 2 deletions src/libs/actions/OnyxUpdateManager/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import type {OnyxEntry} from 'react-native-onyx';
import type {OnyxEntry, OnyxUpdate} from 'react-native-onyx';
import Onyx from 'react-native-onyx';
import * as ActiveClientManager from '@libs/ActiveClientManager';
import Log from '@libs/Log';
import * as NetworkStore from '@libs/Network/NetworkStore';
import * as SequentialQueue from '@libs/Network/SequentialQueue';
import * as App from '@userActions/App';
import updateSessionAuthTokens from '@userActions/Session/updateSessionAuthTokens';
import ONYXKEYS from '@src/ONYXKEYS';
import type {OnyxUpdatesFromServer} from '@src/types/onyx';
import type {OnyxUpdatesFromServer, Session} from '@src/types/onyx';
import {isValidOnyxUpdateFromServer} from '@src/types/onyx/OnyxUpdatesFromServer';
import * as OnyxUpdateManagerUtils from './utils';
import * as DeferredOnyxUpdates from './utils/DeferredOnyxUpdates';
Expand Down Expand Up @@ -90,6 +92,10 @@ function handleOnyxUpdateGap(onyxUpdatesFromServer: OnyxEntry<OnyxUpdatesFromSer
return;
}

// Check if one of these onyx updates is for the authToken. If it is, let's update our authToken now because our
// current authToken is probably invalid.
updateAuthTokenIfNecessary(onyxUpdatesFromServer);

const updateParams = onyxUpdatesFromServer;
const lastUpdateIDFromServer = onyxUpdatesFromServer.lastUpdateID;
const previousUpdateIDFromServer = onyxUpdatesFromServer.previousUpdateID;
Expand Down Expand Up @@ -144,6 +150,29 @@ function handleOnyxUpdateGap(onyxUpdatesFromServer: OnyxEntry<OnyxUpdatesFromSer
DeferredOnyxUpdates.getMissingOnyxUpdatesQueryPromise()?.finally(finalizeUpdatesAndResumeQueue);
}

function updateAuthTokenIfNecessary(onyxUpdatesFromServer: OnyxEntry<OnyxUpdatesFromServer>): void {
// Consolidate all of the given Onyx updates
const onyxUpdates: OnyxUpdate[] = [];
onyxUpdatesFromServer?.updates?.forEach((updateEvent) => onyxUpdates.push(...updateEvent.data));
onyxUpdates.push(...(onyxUpdatesFromServer?.response?.onyxData ?? []));

// Find any session updates
const sessionUpdates = onyxUpdates?.filter((onyxUpdate) => onyxUpdate.key === ONYXKEYS.SESSION);

// If any of the updates changes the authToken, let's update it now
sessionUpdates?.forEach((sessionUpdate) => {
const session = (sessionUpdate.value ?? {}) as Session;
const newAuthToken = session.authToken ?? '';
if (!newAuthToken) {
return;
}

Log.info('[OnyxUpdateManager] Found an authToken update while handling an Onyx update gap. Updating the authToken.');
updateSessionAuthTokens(newAuthToken);
NetworkStore.setAuthToken(newAuthToken);
});
}

export default () => {
console.debug('[OnyxUpdateManager] Listening for updates from the server');
Onyx.connect({
Expand Down
6 changes: 5 additions & 1 deletion src/libs/actions/QueuedOnyxUpdates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@ function flushQueue(): Promise<void> {
});
}

export {queueOnyxUpdates, flushQueue};
function isEmpty() {
return queuedOnyxUpdates.length === 0;
}

export {queueOnyxUpdates, flushQueue, isEmpty};
12 changes: 11 additions & 1 deletion src/libs/actions/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,16 @@ function validateLogin(accountID: number, validateCode: string) {
Onyx.merge(ONYXKEYS.ACCOUNT, {...CONST.DEFAULT_ACCOUNT_DATA, isLoading: true});

const optimisticData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.ACCOUNT,
value: {
isLoading: true,
},
},
];

const finallyData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.ACCOUNT,
Expand All @@ -566,7 +576,7 @@ function validateLogin(accountID: number, validateCode: string) {

const parameters: ValidateLoginParams = {accountID, validateCode};

API.write(WRITE_COMMANDS.VALIDATE_LOGIN, parameters, {optimisticData});
API.write(WRITE_COMMANDS.VALIDATE_LOGIN, parameters, {optimisticData, finallyData});
Navigation.navigate(ROUTES.HOME);
}

Expand Down
8 changes: 5 additions & 3 deletions src/pages/settings/Wallet/VerifyAccountPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,19 @@ function VerifyAccountPage({route}: VerifyAccountPageProps) {
const loginData = loginList?.[contactMethod];
const validateLoginError = ErrorUtils.getEarliestErrorField(loginData, 'validateLogin');
const [isUserValidated] = useOnyx(ONYXKEYS.USER, {selector: (user) => !!user?.validated});
const [accountID] = useOnyx(ONYXKEYS.SESSION, {selector: (session) => session?.accountID ?? 0});

const [isValidateCodeActionModalVisible, setIsValidateCodeActionModalVisible] = useState(true);

const navigateBackTo = route?.params?.backTo;

useEffect(() => () => User.clearUnvalidatedNewContactMethodAction(), []);

const handleSubmitForm = useCallback(
(submitCode: string) => {
User.validateSecondaryLogin(loginList, contactMethod ?? '', submitCode);
(validateCode: string) => {
User.validateLogin(accountID ?? 0, validateCode);
},
[loginList, contactMethod],
[accountID],
);

const clearError = useCallback(() => {
Expand Down
Loading