Skip to content

Commit 59c5aa4

Browse files
authored
Merge pull request Expensify#60147 from Expensify/arosiclair-merge-account-reconnect
2 parents 763bdf6 + c739215 commit 59c5aa4

2 files changed

Lines changed: 42 additions & 2 deletions

File tree

src/libs/actions/OnyxUpdates.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type {OnyxUpdate} from 'react-native-onyx';
22
import Onyx from 'react-native-onyx';
33
import type {Merge} from 'type-fest';
4-
import {WRITE_COMMANDS} from '@libs/API/types';
4+
import {SIDE_EFFECT_REQUEST_COMMANDS, WRITE_COMMANDS} from '@libs/API/types';
55
import Log from '@libs/Log';
66
import Performance from '@libs/Performance';
77
import PusherUtils from '@libs/PusherUtils';
@@ -114,7 +114,11 @@ function apply({lastUpdateID, type, request, response, updates}: OnyxUpdatesFrom
114114
function apply({lastUpdateID, type, request, response, updates}: OnyxUpdatesFromServer): Promise<void | Response> | undefined {
115115
Log.info(`[OnyxUpdateManager] Applying update type: ${type} with lastUpdateID: ${lastUpdateID}`, false, {command: request?.command});
116116

117-
if (lastUpdateID && lastUpdateIDAppliedToClient && Number(lastUpdateID) <= lastUpdateIDAppliedToClient && request?.command !== WRITE_COMMANDS.OPEN_APP) {
117+
const isUpdateOld = lastUpdateID && lastUpdateIDAppliedToClient && Number(lastUpdateID) <= lastUpdateIDAppliedToClient;
118+
const isOpenAppRequest = request?.command === WRITE_COMMANDS.OPEN_APP;
119+
const isFullReconnectRequest = request?.command === SIDE_EFFECT_REQUEST_COMMANDS.RECONNECT_APP && !request?.data?.updateIDFrom;
120+
121+
if (isUpdateOld && !isOpenAppRequest && !isFullReconnectRequest) {
118122
Log.info('[OnyxUpdateManager] Update received was older than or the same as current state, returning without applying the updates other than successData and failureData', false, {
119123
lastUpdateID,
120124
lastUpdateIDAppliedToClient,

tests/unit/OnyxUpdatesTest.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type {OnyxKey} from 'react-native-onyx';
22
import Onyx from 'react-native-onyx';
3+
import {SIDE_EFFECT_REQUEST_COMMANDS} from '@libs/API/types';
34
import CONST from '@src/CONST';
45
import * as OnyxUpdates from '@src/libs/actions/OnyxUpdates';
56
import DateUtils from '@src/libs/DateUtils';
@@ -64,6 +65,41 @@ describe('OnyxUpdatesTest', () => {
6465
expect(reportAction).toStrictEqual(reportActionValue);
6566
});
6667
});
68+
69+
it('applies full ReconnectApp Onyx updates even if they appear old', async () => {
70+
// Given the current lastUpdateIDAppliedToClient is merged
71+
const currentUpdateID = 100;
72+
await Onyx.merge(ONYXKEYS.ONYX_UPDATES_LAST_UPDATE_ID_APPLIED_TO_CLIENT, currentUpdateID);
73+
74+
// And we received onyx updates from a full ReconnectApp request with the same lastUpdateID
75+
const reportID = NumberUtils.rand64();
76+
const reportValue = {reportID};
77+
const fullReconnectUpdates: OnyxUpdatesFromServer = {
78+
type: CONST.ONYX_UPDATE_TYPES.HTTPS,
79+
request: {
80+
command: SIDE_EFFECT_REQUEST_COMMANDS.RECONNECT_APP,
81+
data: {
82+
updateIDFrom: null,
83+
},
84+
},
85+
response: {
86+
onyxData: [
87+
{
88+
onyxMethod: 'merge',
89+
key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`,
90+
value: reportValue,
91+
},
92+
],
93+
},
94+
previousUpdateID: currentUpdateID - 2,
95+
lastUpdateID: currentUpdateID - 1,
96+
};
97+
98+
// When we apply the updates, then they are still applied even if the lastUpdateID is old
99+
await OnyxUpdates.apply(fullReconnectUpdates);
100+
const report = await getOnyxValue(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`);
101+
expect(report).toStrictEqual(reportValue);
102+
});
67103
});
68104

69105
function getOnyxValues<TKey extends OnyxKey>(...keys: TKey[]) {

0 commit comments

Comments
 (0)