Skip to content
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
45 changes: 45 additions & 0 deletions src/libs/Middleware/HandleUnusedOptimisticID.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import clone from 'lodash/clone';
import type {OnyxEntry} from 'react-native-onyx';
import Onyx from 'react-native-onyx';
import {prepareOnyxDataForCleanUpOptimisticParticipants} from '@libs/actions/Report';
import {WRITE_COMMANDS} from '@libs/API/types';
import deepReplaceKeysAndValues from '@libs/deepReplaceKeysAndValues';
import type {Middleware} from '@libs/Request';
import * as PersistedRequests from '@userActions/PersistedRequests';
import ONYXKEYS from '@src/ONYXKEYS';
import type {PersonalDetailsList} from '@src/types/onyx';
import type Report from '@src/types/onyx/Report';
import {isEmptyObject} from '@src/types/utils/EmptyObject';

// Local cache of reportID to optimistic Onyx data
const reportOptimisticData = new Map<string, {settledPersonalDetails: OnyxEntry<PersonalDetailsList>; redundantParticipants: Record<number, null>} | undefined>();

/**
* This middleware checks for the presence of a field called preexistingReportID in the response.
Expand All @@ -17,6 +26,15 @@ import type Report from '@src/types/onyx/Report';
*/
const handleUnusedOptimisticID: Middleware = (requestResponse, request, isFromSequentialQueue) =>
requestResponse.then((response) => {
const currentRequestReportID = request?.data?.reportID as string;
const isCreatingNewReport = request?.command === WRITE_COMMANDS.OPEN_REPORT && !!request?.data?.createdReportActionID && !!currentRequestReportID;
if (isCreatingNewReport) {
// We're opening a new report, which can be a new or preexisting report
// For new report, clean up optimistic data after this request returned successfully
// For report redirect a preexisting report, clean up optimistic data after the request of preexisting report returned successfully
reportOptimisticData.set(currentRequestReportID, prepareOnyxDataForCleanUpOptimisticParticipants(currentRequestReportID));
}

const responseOnyxData = response?.onyxData ?? [];
for (const onyxData of responseOnyxData) {
const key = onyxData.key;
Expand All @@ -35,6 +53,12 @@ const handleUnusedOptimisticID: Middleware = (requestResponse, request, isFromSe
}
const oldReportID = key.split(ONYXKEYS.COLLECTION.REPORT).at(-1) ?? request.data?.reportID ?? request.data?.optimisticReportID;

if (isCreatingNewReport && currentRequestReportID === oldReportID) {
// move optimistic data to preexisting reportID, so it'll be cleaned up after preexisting report is returned successfully
reportOptimisticData.set(preexistingReportID, reportOptimisticData.get(currentRequestReportID));
reportOptimisticData.delete(currentRequestReportID);
}

if (isFromSequentialQueue) {
const ongoingRequest = PersistedRequests.getOngoingRequest();
const ongoingRequestReportIDParam = ongoingRequest?.data?.reportID ?? ongoingRequest?.data?.optimisticReportID;
Expand All @@ -51,6 +75,27 @@ const handleUnusedOptimisticID: Middleware = (requestResponse, request, isFromSe
PersistedRequests.update(index, persistedRequestClone);
}
}

if (!!currentRequestReportID && request?.command === WRITE_COMMANDS.OPEN_REPORT && !!response?.onyxData && reportOptimisticData.has(currentRequestReportID)) {
const {settledPersonalDetails, redundantParticipants} = reportOptimisticData.get(currentRequestReportID) ?? {};
reportOptimisticData.delete(currentRequestReportID);
if (!isEmptyObject(settledPersonalDetails) && !isEmptyObject(redundantParticipants)) {
response.onyxData.push(
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${currentRequestReportID}`,
value: {
participants: redundantParticipants,
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
value: redundantParticipants,
},
);
}
}
return response;
});

Expand Down
30 changes: 20 additions & 10 deletions src/libs/actions/Report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@
let currentUserEmail: string | undefined;

Onyx.connect({
key: ONYXKEYS.SESSION,

Check warning on line 280 in src/libs/actions/Report.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
callback: (value) => {
// When signed out, val is undefined
if (!value?.accountID) {
Expand All @@ -290,7 +290,7 @@
});

Onyx.connect({
key: ONYXKEYS.CONCIERGE_REPORT_ID,

Check warning on line 293 in src/libs/actions/Report.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
callback: (value) => (conciergeReportID = value),
});

Expand All @@ -298,7 +298,7 @@
const allReportActions: OnyxCollection<ReportActions> = {};

Onyx.connect({
key: ONYXKEYS.COLLECTION.REPORT_ACTIONS,

Check warning on line 301 in src/libs/actions/Report.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
callback: (actions, key) => {
if (!key || !actions) {
return;
Expand All @@ -310,14 +310,14 @@

let allTransactionViolations: OnyxCollection<TransactionViolations> = {};
Onyx.connect({
key: ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS,

Check warning on line 313 in src/libs/actions/Report.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
waitForCollectionCallback: true,
callback: (value) => (allTransactionViolations = value),
});

let allReports: OnyxCollection<Report>;
Onyx.connect({
key: ONYXKEYS.COLLECTION.REPORT,

Check warning on line 320 in src/libs/actions/Report.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
waitForCollectionCallback: true,
callback: (value) => {
allReports = value;
Expand All @@ -326,7 +326,7 @@

let isNetworkOffline = false;
Onyx.connect({
key: ONYXKEYS.NETWORK,

Check warning on line 329 in src/libs/actions/Report.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
callback: (value) => {
isNetworkOffline = value?.isOffline ?? false;
},
Expand All @@ -341,7 +341,7 @@
});

const typingWatchTimers: Record<string, NodeJS.Timeout> = {};

Check warning on line 344 in src/libs/actions/Report.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
let reportIDDeeplinkedFromOldDot: string | undefined;
Linking.getInitialURL().then((url) => {
reportIDDeeplinkedFromOldDot = processReportIDDeeplink(url ?? '');
Expand All @@ -352,20 +352,20 @@
key: ONYXKEYS.NVP_ONBOARDING,
callback: (val) => {
if (Array.isArray(val)) {
return;

Check warning on line 355 in src/libs/actions/Report.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
}
onboarding = val;
},
});

let introSelected: OnyxEntry<IntroSelected> = {};

Check warning on line 361 in src/libs/actions/Report.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
Onyx.connect({
key: ONYXKEYS.NVP_INTRO_SELECTED,
callback: (val) => (introSelected = val),
});

let allReportDraftComments: Record<string, string | undefined> = {};
Onyx.connect({

Check warning on line 368 in src/libs/actions/Report.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT,
waitForCollectionCallback: true,
callback: (value) => (allReportDraftComments = value),
Expand Down Expand Up @@ -1281,7 +1281,6 @@
// Add optimistic personal details for new participants
const optimisticPersonalDetails: OnyxEntry<PersonalDetailsList> = {};
const settledPersonalDetails: OnyxEntry<PersonalDetailsList> = {};
const redundantParticipants: Record<number, null> = {};
const participantAccountIDs = PersonalDetailsUtils.getAccountIDsByLogins(participantLoginList);
for (const [index, login] of participantLoginList.entries()) {
const accountID = participantAccountIDs.at(index) ?? -1;
Expand All @@ -1298,17 +1297,13 @@
isOptimisticPersonalDetail: true,
};
settledPersonalDetails[accountID] = null;

// BE will send different participants. We clear the optimistic ones to avoid duplicated entries
redundantParticipants[accountID] = null;
}

successData.push(
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`,
value: {
participants: redundantParticipants,
pendingFields: {
createChat: null,
reportName: null,
Expand All @@ -1332,11 +1327,6 @@
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
value: optimisticPersonalDetails,
});
successData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
value: settledPersonalDetails,
});
failureData.push({
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
Expand Down Expand Up @@ -1384,6 +1374,25 @@
}
}

function prepareOnyxDataForCleanUpOptimisticParticipants(
reportID: string,
): {settledPersonalDetails: OnyxEntry<PersonalDetailsList>; redundantParticipants: Record<number, null>} | undefined {
const existingReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`];
if (!existingReport?.participants) {
return undefined;
}
const settledPersonalDetails: OnyxEntry<PersonalDetailsList> = {};
const redundantParticipants: Record<number, null> = {};
for (const accountID in existingReport.participants) {
if (!allPersonalDetails?.[accountID]?.isOptimisticPersonalDetail) {
continue;
}
settledPersonalDetails[accountID] = null;
redundantParticipants[accountID] = null;
}
return {settledPersonalDetails, redundantParticipants};
}

/**
* This will return an optimistic report object for a given user we want to create a chat with without saving it, when the only thing we know about recipient is his accountID. *
* @param accountID accountID of the user that the optimistic chat report is created with.
Expand Down Expand Up @@ -6361,4 +6370,5 @@
openUnreportedExpense,
optimisticReportLastData,
setOptimisticTransactionThread,
prepareOnyxDataForCleanUpOptimisticParticipants,
};
214 changes: 213 additions & 1 deletion tests/unit/MiddlewareTest.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import Onyx from 'react-native-onyx';
import type {OnyxEntry} from 'react-native-onyx';
import SaveResponseInOnyx from '@libs/Middleware/SaveResponseInOnyx';
import HttpUtils from '@src/libs/HttpUtils';
import handleUnusedOptimisticID from '@src/libs/Middleware/HandleUnusedOptimisticID';
import * as MainQueue from '@src/libs/Network/MainQueue';
import * as NetworkStore from '@src/libs/Network/NetworkStore';
import * as SequentialQueue from '@src/libs/Network/SequentialQueue';
import * as Request from '@src/libs/Request';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Report as OnyxReport} from '@src/types/onyx';
import type {Report as OnyxReport, PersonalDetailsList} from '@src/types/onyx';
import * as TestHelper from '../utils/TestHelper';
import waitForBatchedUpdates from '../utils/waitForBatchedUpdates';
import waitForNetworkPromises from '../utils/waitForNetworkPromises';

type FormDataObject = {body: TestHelper.FormData};
Expand Down Expand Up @@ -217,5 +220,214 @@ describe('Middleware', () => {
expect(global.fetch).toHaveBeenNthCalledWith(1, 'https://www.expensify.com.dev/api/MoveIOUReportToExistingPolicy?', expect.anything());
TestHelper.assertFormDataMatchesObject({optimisticReportID: '1234'} as unknown as OnyxReport, ((global.fetch as jest.Mock).mock.calls.at(0) as FormDataObject[]).at(1)?.body);
});

test('OpenReport to a chat with preexistingReportID and clean up optimistic participant data', async () => {
const optimisticReportID = '1234';
const preexistingReportID = '5555';
const optimisticAccountID = 999;
const preexistingAccountID = 333;
await Onyx.multiSet({
[`${ONYXKEYS.COLLECTION.REPORT}${optimisticReportID}` as const]: {
reportID: optimisticReportID,
participants: {[optimisticAccountID]: {notificationPreference: 'always'}},
},
[ONYXKEYS.PERSONAL_DETAILS_LIST]: {
[optimisticAccountID]: {
accountID: optimisticAccountID,
isOptimisticPersonalDetail: true,
},
},
});

Request.addMiddleware(handleUnusedOptimisticID);
Request.addMiddleware(SaveResponseInOnyx);

const requests = [
{
command: 'OpenReport',
data: {authToken: 'testToken', reportID: optimisticReportID, createdReportActionID: '5678'},
},
{
command: 'OpenReport',
data: {authToken: 'testToken', reportID: preexistingReportID},
},
];
for (const request of requests) {
SequentialQueue.push(request);
}

// eslint-disable-next-line @typescript-eslint/require-await
(global.fetch as jest.Mock)
.mockImplementationOnce(async () => ({
ok: true,
// eslint-disable-next-line @typescript-eslint/require-await
json: async () => ({
jsonCode: 200,
onyxData: [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${optimisticReportID}`,
value: {
preexistingReportID,
},
},
],
}),
}))
.mockImplementationOnce(async () => ({
ok: true,
// eslint-disable-next-line @typescript-eslint/require-await
json: async () => ({
jsonCode: 200,
onyxData: [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${preexistingReportID}`,
value: {
reportID: preexistingReportID,
participants: {[preexistingAccountID]: {notificationPreference: 'always'}},
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
value: {
[preexistingAccountID]: {
accountID: preexistingAccountID,
},
},
},
],
}),
}));

SequentialQueue.unpause();
await waitForBatchedUpdates();

expect(global.fetch).toHaveBeenCalledTimes(2);

const optimisticReportUpdated = await new Promise<OnyxEntry<OnyxReport>>((resolve) => {
const connection = Onyx.connect({
key: `${ONYXKEYS.COLLECTION.REPORT}${optimisticReportID}`,
callback: (report) => {
Onyx.disconnect(connection);
resolve(report);
},
});
});
expect(optimisticReportUpdated?.participants?.[optimisticAccountID]).toBeUndefined();

const preexistingReportUpdated = await new Promise<OnyxEntry<OnyxReport>>((resolve) => {
const connection = Onyx.connect({
key: `${ONYXKEYS.COLLECTION.REPORT}${preexistingReportID}`,
callback: (report) => {
Onyx.disconnect(connection);
resolve(report);
},
});
});
expect(preexistingReportUpdated?.participants?.[optimisticAccountID]).toBeUndefined();
expect(preexistingReportUpdated?.participants?.[preexistingAccountID]).not.toBeUndefined();

const personalDetails = await new Promise<OnyxEntry<PersonalDetailsList>>((resolve) => {
const connection = Onyx.connect({
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
callback: (data) => {
Onyx.disconnect(connection);
resolve(data);
},
});
});
expect(personalDetails?.[optimisticAccountID]).toBeUndefined();
expect(personalDetails?.[preexistingAccountID]).not.toBeUndefined();
});

test('OpenReport to a new chat without preexistingReportID and clean up optimistic participant data', async () => {
const optimisticReportID = '1234';
const optimisticAccountID = 999;
const preexistingAccountID = 333;
await Onyx.multiSet({
[`${ONYXKEYS.COLLECTION.REPORT}${optimisticReportID}` as const]: {
reportID: optimisticReportID,
participants: {[optimisticAccountID]: {notificationPreference: 'always'}},
},
[ONYXKEYS.PERSONAL_DETAILS_LIST]: {
[optimisticAccountID]: {
accountID: optimisticAccountID,
isOptimisticPersonalDetail: true,
},
},
});

Request.addMiddleware(handleUnusedOptimisticID);
Request.addMiddleware(SaveResponseInOnyx);

const requests = [
{
command: 'OpenReport',
data: {authToken: 'testToken', reportID: optimisticReportID, createdReportActionID: '5678'},
},
];
for (const request of requests) {
SequentialQueue.push(request);
}

// eslint-disable-next-line @typescript-eslint/require-await
(global.fetch as jest.Mock).mockImplementationOnce(async () => ({
ok: true,
// eslint-disable-next-line @typescript-eslint/require-await
json: async () => ({
jsonCode: 200,
onyxData: [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${optimisticReportID}`,
value: {
reportID: optimisticReportID,
participants: {[preexistingAccountID]: {notificationPreference: 'always'}},
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
value: {
[preexistingAccountID]: {
accountID: preexistingAccountID,
},
},
},
],
}),
}));

SequentialQueue.unpause();
await waitForBatchedUpdates();

expect(global.fetch).toHaveBeenCalledTimes(1);

const optimisticReportUpdated = await new Promise<OnyxEntry<OnyxReport>>((resolve) => {
const connection = Onyx.connect({
key: `${ONYXKEYS.COLLECTION.REPORT}${optimisticReportID}`,
callback: (report) => {
Onyx.disconnect(connection);
resolve(report);
},
});
});
expect(optimisticReportUpdated?.participants?.[optimisticAccountID]).toBeUndefined();
expect(optimisticReportUpdated?.participants?.[preexistingAccountID]).not.toBeUndefined();

const personalDetails = await new Promise<OnyxEntry<PersonalDetailsList>>((resolve) => {
const connection = Onyx.connect({
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
callback: (data) => {
Onyx.disconnect(connection);
resolve(data);
},
});
});
expect(personalDetails?.[optimisticAccountID]).toBeUndefined();
expect(personalDetails?.[preexistingAccountID]).not.toBeUndefined();
});
});
});
Loading