From 87e7f014e0514413939c8607bda19575af63261b Mon Sep 17 00:00:00 2001 From: Chirag Chandrakant Salian Date: Thu, 16 Jan 2025 10:53:08 -0800 Subject: [PATCH] Revert "[HybridApp] Necessary copilot changes" --- src/libs/actions/Delegate.ts | 56 ++++++++--------------------- src/libs/actions/Session/index.ts | 28 +++------------ src/types/modules/react-native.d.ts | 2 +- src/types/onyx/Response.ts | 6 ---- 4 files changed, 20 insertions(+), 72 deletions(-) diff --git a/src/libs/actions/Delegate.ts b/src/libs/actions/Delegate.ts index 320f91f8a677..f99400d87d3e 100644 --- a/src/libs/actions/Delegate.ts +++ b/src/libs/actions/Delegate.ts @@ -1,12 +1,13 @@ import {NativeModules} from 'react-native'; import Onyx from 'react-native-onyx'; -import type {OnyxEntry, OnyxUpdate} from 'react-native-onyx'; +import type {OnyxUpdate} from 'react-native-onyx'; import * as API from '@libs/API'; import type {AddDelegateParams, RemoveDelegateParams, UpdateDelegateRoleParams} from '@libs/API/parameters'; import {SIDE_EFFECT_REQUEST_COMMANDS, WRITE_COMMANDS} from '@libs/API/types'; import * as ErrorUtils from '@libs/ErrorUtils'; import Log from '@libs/Log'; import * as NetworkStore from '@libs/Network/NetworkStore'; +import {getCurrentUserEmail} from '@libs/Network/NetworkStore'; import * as SequentialQueue from '@libs/Network/SequentialQueue'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; @@ -15,7 +16,6 @@ import type Credentials from '@src/types/onyx/Credentials'; import type Response from '@src/types/onyx/Response'; import type Session from '@src/types/onyx/Session'; import {confirmReadyToOpenApp, openApp} from './App'; -import {getCurrentUserAccountID} from './Report'; import updateSessionAuthTokens from './Session/updateSessionAuthTokens'; import updateSessionUser from './Session/updateSessionUser'; @@ -51,14 +51,6 @@ Onyx.connect({ callback: (value) => (stashedSession = value ?? {}), }); -let activePolicyID: OnyxEntry; -Onyx.connect({ - key: ONYXKEYS.NVP_ACTIVE_POLICY_ID, - callback: (newActivePolicyID) => { - activePolicyID = newActivePolicyID; - }, -}); - const KEYS_TO_PRESERVE_DELEGATE_ACCESS = [ ONYXKEYS.NVP_TRY_FOCUS_MODE, ONYXKEYS.PREFERRED_THEME, @@ -81,8 +73,6 @@ function connect(email: string) { Onyx.set(ONYXKEYS.STASHED_CREDENTIALS, credentials); Onyx.set(ONYXKEYS.STASHED_SESSION, session); - const previousAccountID = getCurrentUserAccountID(); - const optimisticData: OnyxUpdate[] = [ { onyxMethod: Onyx.METHOD.MERGE, @@ -140,14 +130,6 @@ function connect(email: string) { Onyx.update(failureData); return; } - if (!activePolicyID) { - Log.alert('[Delegate] Unable to access activePolicyID'); - Onyx.update(failureData); - return; - } - const restrictedToken = response.restrictedToken; - const policyID = activePolicyID; - return SequentialQueue.waitForIdle() .then(() => Onyx.clear(KEYS_TO_PRESERVE_DELEGATE_ACCESS)) .then(() => { @@ -156,7 +138,9 @@ function connect(email: string) { NetworkStore.setAuthToken(response?.restrictedToken ?? null); confirmReadyToOpenApp(); - openApp().then(() => NativeModules.HybridAppModule.switchAccount(email, restrictedToken, policyID, String(previousAccountID))); + openApp(); + + NativeModules.HybridAppModule.switchAccount(email); }); }) .catch((error) => { @@ -211,34 +195,22 @@ function disconnect() { return; } - if (!response?.requesterID || !response?.requesterEmail) { - Log.alert('[Delegate] No requester data returned while disconnecting as a delegate'); - return; - } - - const requesterEmail = response.requesterEmail; - const authToken = response.authToken; return SequentialQueue.waitForIdle() .then(() => Onyx.clear(KEYS_TO_PRESERVE_DELEGATE_ACCESS)) .then(() => { - Onyx.set(ONYXKEYS.CREDENTIALS, { - ...stashedCredentials, - accountID: response.requesterID, - }); - Onyx.set(ONYXKEYS.SESSION, { - ...stashedSession, - accountID: response.requesterID, - email: requesterEmail, - authToken, - encryptedAuthToken: response.encryptedAuthToken, - }); - Onyx.set(ONYXKEYS.STASHED_CREDENTIALS, {}); - Onyx.set(ONYXKEYS.STASHED_SESSION, {}); + // Update authToken in Onyx and in our local variables so that API requests will use the new authToken + updateSessionAuthTokens(response?.authToken, response?.encryptedAuthToken); NetworkStore.setAuthToken(response?.authToken ?? null); + Onyx.set(ONYXKEYS.CREDENTIALS, stashedCredentials); + Onyx.set(ONYXKEYS.SESSION, stashedSession); + Onyx.set(ONYXKEYS.STASHED_CREDENTIALS, {}); + Onyx.set(ONYXKEYS.STASHED_SESSION, {}); confirmReadyToOpenApp(); - openApp().then(() => NativeModules.HybridAppModule.switchAccount(requesterEmail, authToken, '', '')); + openApp(); + + NativeModules.HybridAppModule.switchAccount(getCurrentUserEmail() ?? ''); }); }) .catch((error) => { diff --git a/src/libs/actions/Session/index.ts b/src/libs/actions/Session/index.ts index 416fbd52b7cd..11eab64a36df 100644 --- a/src/libs/actions/Session/index.ts +++ b/src/libs/actions/Session/index.ts @@ -33,7 +33,6 @@ import Navigation from '@libs/Navigation/Navigation'; import navigationRef from '@libs/Navigation/navigationRef'; import * as MainQueue from '@libs/Network/MainQueue'; import * as NetworkStore from '@libs/Network/NetworkStore'; -import {getCurrentUserEmail} from '@libs/Network/NetworkStore'; import NetworkConnection from '@libs/NetworkConnection'; import * as Pusher from '@libs/Pusher/pusher'; import {getReportIDFromLink, parseReportRouteParams as parseReportRouteParamsReportUtils} from '@libs/ReportUtils'; @@ -525,7 +524,7 @@ function signInAfterTransitionFromOldDot(transitionURL: string) { nudgeMigrationTimestamp, isSingleNewDotEntry, primaryLogin, - oldDotOriginalAccountEmail, + shouldRemoveDelegatedAccess, } = Object.fromEntries( queryParams.split('&').map((param) => { const [key, value] = param.split('='); @@ -544,39 +543,22 @@ function signInAfterTransitionFromOldDot(transitionURL: string) { const setSessionDataAndOpenApp = new Promise((resolve) => { clearOnyxForNewAccount() .then(() => { - // This section controls copilot changes - const currentUserEmail = getCurrentUserEmail(); - - // If ND and OD account are the same - do nothing - if (email === currentUserEmail) { - return; - } - - // If account was changed to original one on OD side - clear onyx - if (!oldDotOriginalAccountEmail) { - return Onyx.clear(KEYS_TO_PRESERVE_DELEGATE_ACCESS); - } - - // If we're already logged in - do nothing, data will be set in next step - if (currentUserEmail) { + if (!shouldRemoveDelegatedAccess) { return; } - - // If we're not logged in - set stashed data - return Onyx.multiSet({ - [ONYXKEYS.STASHED_CREDENTIALS]: {autoGeneratedLogin, autoGeneratedPassword}, - }); + return Onyx.clear(KEYS_TO_PRESERVE_DELEGATE_ACCESS); }) .then(() => Onyx.multiSet({ [ONYXKEYS.SESSION]: {email, authToken, encryptedAuthToken: decodeURIComponent(encryptedAuthToken), accountID: Number(accountID)}, + [ONYXKEYS.ACCOUNT]: {primaryLogin}, [ONYXKEYS.CREDENTIALS]: {autoGeneratedLogin, autoGeneratedPassword}, [ONYXKEYS.IS_SINGLE_NEW_DOT_ENTRY]: isSingleNewDotEntry === 'true', [ONYXKEYS.NVP_TRYNEWDOT]: { classicRedirect: {completedHybridAppOnboarding: completedHybridAppOnboarding === 'true'}, nudgeMigration: nudgeMigrationTimestamp ? {timestamp: new Date(nudgeMigrationTimestamp)} : undefined, }, - }).then(() => Onyx.merge(ONYXKEYS.ACCOUNT, {primaryLogin})), + }), ) .then(() => { if (clearOnyxOnStart === 'true') { diff --git a/src/types/modules/react-native.d.ts b/src/types/modules/react-native.d.ts index 340f396882af..c72d4bf2a653 100644 --- a/src/types/modules/react-native.d.ts +++ b/src/types/modules/react-native.d.ts @@ -8,7 +8,7 @@ import type StartupTimer from '@libs/StartupTimer/types'; type HybridAppModule = { closeReactNativeApp: (shouldSignOut: boolean, shouldSetNVP: boolean) => void; completeOnboarding: (status: boolean) => void; - switchAccount: (newDotCurrentAccount: string, authToken: string, policyID: string, accountID: string) => void; + switchAccount: (newDotCurrentAccount: string) => void; exitApp: () => void; }; diff --git a/src/types/onyx/Response.ts b/src/types/onyx/Response.ts index 146cf327dd31..ce9acbaaccc6 100644 --- a/src/types/onyx/Response.ts +++ b/src/types/onyx/Response.ts @@ -83,12 +83,6 @@ type Response = { /** If there is newer data to load for pagination commands */ hasNewerActions?: boolean; - - /** The email of the original user (returned when in delegate mode) */ - requesterEmail?: string; - - /** The ID of the original user (returned when in delegate mode) */ - requesterID?: number; }; export default Response;