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
14 changes: 1 addition & 13 deletions src/libs/Middleware/HandleUnusedOptimisticID.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,6 @@ import type {OnyxEntry} from 'react-native-onyx';
import clone from 'lodash/clone';
import Onyx from 'react-native-onyx';

/**
* Use this only in non-React contexts (e.g. request middleware) where `useOnyx` is not available;
* React code should read the list via `useOnyx(ONYXKEYS.PERSONAL_DETAILS_LIST)` and pass it down.
*/
let allPersonalDetails: OnyxEntry<PersonalDetailsList>;
Onyx.connectWithoutView({
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
callback: (value) => {
allPersonalDetails = value;
},
});

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

Expand All @@ -49,7 +37,7 @@ const handleUnusedOptimisticID: Middleware = (requestResponse, request, isFromSe
// 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, allPersonalDetails));
reportOptimisticData.set(currentRequestReportID, prepareOnyxDataForCleanUpOptimisticParticipants(currentRequestReportID));
}

const responseOnyxData = response?.onyxData ?? [];
Expand Down
19 changes: 15 additions & 4 deletions src/libs/actions/Report/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2098,7 +2098,6 @@ function createGroupChat(

function prepareOnyxDataForCleanUpOptimisticParticipants(
reportID: string,
personalDetails: OnyxEntry<PersonalDetailsList>,
): {settledPersonalDetails: OnyxEntry<PersonalDetailsList>; redundantParticipants: Record<number, null>} | undefined {
const existingReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`];
if (!existingReport?.participants) {
Expand All @@ -2107,7 +2106,7 @@ function prepareOnyxDataForCleanUpOptimisticParticipants(
const settledPersonalDetails: OnyxEntry<PersonalDetailsList> = {};
const redundantParticipants: Record<number, null> = {};
for (const accountID in existingReport.participants) {
if (!personalDetails?.[accountID]?.isOptimisticPersonalDetail) {
if (!allPersonalDetails?.[accountID]?.isOptimisticPersonalDetail) {
continue;
}
settledPersonalDetails[accountID] = null;
Expand Down Expand Up @@ -2228,7 +2227,8 @@ function createTransactionThreadReport(params: CreateTransactionThreadReportPara
reportID: optimisticTransactionThreadReportID,
introSelected,
participants,
personalDetails,
// TODO: allPersonalDetails fallback should be removed in follow-up PRs https://github.com/Expensify/App/issues/73656
personalDetails: personalDetails ?? allPersonalDetails,
newReportObject: optimisticTransactionThread,
parentReportActionID: iouReportAction?.reportActionID,
transaction,
Expand Down Expand Up @@ -3961,7 +3961,18 @@ function navigateToConciergeChat(
if (!checkIfCurrentPageActive()) {
return;
}
navigateToAndOpenReport([CONST.EMAIL.CONCIERGE], personalDetails, currentUserAccountID, introSelected, isSelfTourViewed, betas, shouldDismissModal, false, linkToOptions);
// TODO: allPersonalDetails fallback should be removed in follow-up PRs https://github.com/Expensify/App/issues/73656
navigateToAndOpenReport(
[CONST.EMAIL.CONCIERGE],
personalDetails ?? allPersonalDetails,
currentUserAccountID,
introSelected,
isSelfTourViewed,
betas,
shouldDismissModal,
false,
linkToOptions,
);
});
} else if (shouldDismissModal) {
const reportParams = {reportID: conciergeReportID, reportActionID};
Expand Down
61 changes: 0 additions & 61 deletions tests/actions/ReportTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9009,65 +9009,4 @@ describe('actions/Report', () => {
expect(result.delegateAccountID).toBeUndefined();
});
});

describe('prepareOnyxDataForCleanUpOptimisticParticipants', () => {
const REPORT_ID = '1';
const OPTIMISTIC_ACCOUNT_ID = 100;
const SETTLED_ACCOUNT_ID = 200;

it('marks only participants with optimistic personal details for clean up', async () => {
// Given a report with one optimistic and one settled participant
await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, {
reportID: REPORT_ID,
participants: {
[OPTIMISTIC_ACCOUNT_ID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS},
[SETTLED_ACCOUNT_ID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS},
},
});
await waitForBatchedUpdates();

// When preparing the clean up data using the provided personal details
const result = Report.prepareOnyxDataForCleanUpOptimisticParticipants(REPORT_ID, {
[OPTIMISTIC_ACCOUNT_ID]: {accountID: OPTIMISTIC_ACCOUNT_ID, isOptimisticPersonalDetail: true},
[SETTLED_ACCOUNT_ID]: {accountID: SETTLED_ACCOUNT_ID},
});

// Then only the optimistic participant is marked for clean up
expect(result).toEqual({
settledPersonalDetails: {[OPTIMISTIC_ACCOUNT_ID]: null},
redundantParticipants: {[OPTIMISTIC_ACCOUNT_ID]: null},
});
});

it('returns empty clean up data when no participant has optimistic personal details', async () => {
await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, {
reportID: REPORT_ID,
participants: {[SETTLED_ACCOUNT_ID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS}},
});
await waitForBatchedUpdates();

const result = Report.prepareOnyxDataForCleanUpOptimisticParticipants(REPORT_ID, {[SETTLED_ACCOUNT_ID]: {accountID: SETTLED_ACCOUNT_ID}});

expect(result).toEqual({settledPersonalDetails: {}, redundantParticipants: {}});
});

it('returns undefined when the report has no participants', async () => {
await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, {reportID: REPORT_ID});
await waitForBatchedUpdates();

const result = Report.prepareOnyxDataForCleanUpOptimisticParticipants(REPORT_ID, {
[OPTIMISTIC_ACCOUNT_ID]: {accountID: OPTIMISTIC_ACCOUNT_ID, isOptimisticPersonalDetail: true},
});

expect(result).toBeUndefined();
});

it('returns undefined when the report does not exist', () => {
const result = Report.prepareOnyxDataForCleanUpOptimisticParticipants('non-existent-report', {
[OPTIMISTIC_ACCOUNT_ID]: {accountID: OPTIMISTIC_ACCOUNT_ID, isOptimisticPersonalDetail: true},
});

expect(result).toBeUndefined();
});
});
});
Loading