Skip to content

Commit 963f92d

Browse files
authored
Merge pull request #50835 from Expensify/arosiclair-in-product-validation
Fix pay someone flow for unvalidated accounts
2 parents 967e8f2 + ee38879 commit 963f92d

File tree

5 files changed

+54
-9
lines changed

5 files changed

+54
-9
lines changed

src/libs/Network/SequentialQueue.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@ function flush() {
138138
return;
139139
}
140140

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

src/libs/actions/OnyxUpdateManager/index.ts

+31-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
import type {OnyxEntry} from 'react-native-onyx';
1+
import type {OnyxEntry, OnyxUpdate} from 'react-native-onyx';
22
import Onyx from 'react-native-onyx';
33
import * as ActiveClientManager from '@libs/ActiveClientManager';
44
import Log from '@libs/Log';
5+
import * as NetworkStore from '@libs/Network/NetworkStore';
56
import * as SequentialQueue from '@libs/Network/SequentialQueue';
67
import * as App from '@userActions/App';
8+
import updateSessionAuthTokens from '@userActions/Session/updateSessionAuthTokens';
79
import ONYXKEYS from '@src/ONYXKEYS';
8-
import type {OnyxUpdatesFromServer} from '@src/types/onyx';
10+
import type {OnyxUpdatesFromServer, Session} from '@src/types/onyx';
911
import {isValidOnyxUpdateFromServer} from '@src/types/onyx/OnyxUpdatesFromServer';
1012
import * as OnyxUpdateManagerUtils from './utils';
1113
import * as DeferredOnyxUpdates from './utils/DeferredOnyxUpdates';
@@ -90,6 +92,10 @@ function handleOnyxUpdateGap(onyxUpdatesFromServer: OnyxEntry<OnyxUpdatesFromSer
9092
return;
9193
}
9294

95+
// Check if one of these onyx updates is for the authToken. If it is, let's update our authToken now because our
96+
// current authToken is probably invalid.
97+
updateAuthTokenIfNecessary(onyxUpdatesFromServer);
98+
9399
const updateParams = onyxUpdatesFromServer;
94100
const lastUpdateIDFromServer = onyxUpdatesFromServer.lastUpdateID;
95101
const previousUpdateIDFromServer = onyxUpdatesFromServer.previousUpdateID;
@@ -144,6 +150,29 @@ function handleOnyxUpdateGap(onyxUpdatesFromServer: OnyxEntry<OnyxUpdatesFromSer
144150
DeferredOnyxUpdates.getMissingOnyxUpdatesQueryPromise()?.finally(finalizeUpdatesAndResumeQueue);
145151
}
146152

153+
function updateAuthTokenIfNecessary(onyxUpdatesFromServer: OnyxEntry<OnyxUpdatesFromServer>): void {
154+
// Consolidate all of the given Onyx updates
155+
const onyxUpdates: OnyxUpdate[] = [];
156+
onyxUpdatesFromServer?.updates?.forEach((updateEvent) => onyxUpdates.push(...updateEvent.data));
157+
onyxUpdates.push(...(onyxUpdatesFromServer?.response?.onyxData ?? []));
158+
159+
// Find any session updates
160+
const sessionUpdates = onyxUpdates?.filter((onyxUpdate) => onyxUpdate.key === ONYXKEYS.SESSION);
161+
162+
// If any of the updates changes the authToken, let's update it now
163+
sessionUpdates?.forEach((sessionUpdate) => {
164+
const session = (sessionUpdate.value ?? {}) as Session;
165+
const newAuthToken = session.authToken ?? '';
166+
if (!newAuthToken) {
167+
return;
168+
}
169+
170+
Log.info('[OnyxUpdateManager] Found an authToken update while handling an Onyx update gap. Updating the authToken.');
171+
updateSessionAuthTokens(newAuthToken);
172+
NetworkStore.setAuthToken(newAuthToken);
173+
});
174+
}
175+
147176
export default () => {
148177
console.debug('[OnyxUpdateManager] Listening for updates from the server');
149178
Onyx.connect({

src/libs/actions/QueuedOnyxUpdates.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,8 @@ function flushQueue(): Promise<void> {
1919
});
2020
}
2121

22-
export {queueOnyxUpdates, flushQueue};
22+
function isEmpty() {
23+
return queuedOnyxUpdates.length === 0;
24+
}
25+
26+
export {queueOnyxUpdates, flushQueue, isEmpty};

src/libs/actions/User.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,16 @@ function validateLogin(accountID: number, validateCode: string) {
555555
Onyx.merge(ONYXKEYS.ACCOUNT, {...CONST.DEFAULT_ACCOUNT_DATA, isLoading: true});
556556

557557
const optimisticData: OnyxUpdate[] = [
558+
{
559+
onyxMethod: Onyx.METHOD.MERGE,
560+
key: ONYXKEYS.ACCOUNT,
561+
value: {
562+
isLoading: true,
563+
},
564+
},
565+
];
566+
567+
const finallyData: OnyxUpdate[] = [
558568
{
559569
onyxMethod: Onyx.METHOD.MERGE,
560570
key: ONYXKEYS.ACCOUNT,
@@ -566,7 +576,7 @@ function validateLogin(accountID: number, validateCode: string) {
566576

567577
const parameters: ValidateLoginParams = {accountID, validateCode};
568578

569-
API.write(WRITE_COMMANDS.VALIDATE_LOGIN, parameters, {optimisticData});
579+
API.write(WRITE_COMMANDS.VALIDATE_LOGIN, parameters, {optimisticData, finallyData});
570580
Navigation.navigate(ROUTES.HOME);
571581
}
572582

src/pages/settings/Wallet/VerifyAccountPage.tsx

+5-3
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,19 @@ function VerifyAccountPage({route}: VerifyAccountPageProps) {
2121
const loginData = loginList?.[contactMethod];
2222
const validateLoginError = ErrorUtils.getEarliestErrorField(loginData, 'validateLogin');
2323
const [isUserValidated] = useOnyx(ONYXKEYS.USER, {selector: (user) => !!user?.validated});
24+
const [accountID] = useOnyx(ONYXKEYS.SESSION, {selector: (session) => session?.accountID ?? 0});
25+
2426
const [isValidateCodeActionModalVisible, setIsValidateCodeActionModalVisible] = useState(true);
2527

2628
const navigateBackTo = route?.params?.backTo;
2729

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

3032
const handleSubmitForm = useCallback(
31-
(submitCode: string) => {
32-
User.validateSecondaryLogin(loginList, contactMethod ?? '', submitCode);
33+
(validateCode: string) => {
34+
User.validateLogin(accountID ?? 0, validateCode);
3335
},
34-
[loginList, contactMethod],
36+
[accountID],
3537
);
3638

3739
const clearError = useCallback(() => {

0 commit comments

Comments
 (0)