Skip to content

Commit

Permalink
Merge pull request #40046 from EzraEllette/iss-39091-deprecate-getAll…
Browse files Browse the repository at this point in the history
…ReportActions

deprecate `ReportActionUtils.getAllReportActions()` (Issue 39091)
  • Loading branch information
tgolen authored Apr 12, 2024
2 parents 0665fa4 + ad4697c commit 617f9e9
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 12 deletions.
1 change: 0 additions & 1 deletion src/libs/ReportActionsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1093,7 +1093,6 @@ function getReportActionMessageText(reportAction: OnyxEntry<ReportAction> | Empt
export {
extractLinksFromMessageHtml,
getOneTransactionThreadReportID,
getAllReportActions,
getIOUReportIDFromReportActionPreview,
getLastClosedReportAction,
getLastVisibleAction,
Expand Down
8 changes: 4 additions & 4 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4395,7 +4395,7 @@ function canAccessReport(report: OnyxEntry<Report>, policies: OnyxCollection<Pol
function shouldHideReport(report: OnyxEntry<Report>, currentReportId: string): boolean {
const currentReport = getReport(currentReportId);
const parentReport = getParentReport(!isEmptyObject(currentReport) ? currentReport : null);
const reportActions = ReportActionsUtils.getAllReportActions(report?.reportID ?? '');
const reportActions = reportActionsByReport?.[report?.reportID ?? ''] ?? {};
const isChildReportHasComment = Object.values(reportActions ?? {})?.some((reportAction) => (reportAction?.childVisibleActionCount ?? 0) > 0);
return parentReport?.reportID !== report?.reportID && !isChildReportHasComment;
}
Expand Down Expand Up @@ -5742,7 +5742,7 @@ function getIndicatedMissingPaymentMethod(userWallet: OnyxEntry<UserWallet>, rep
* Checks if report chat contains missing payment method
*/
function hasMissingPaymentMethod(userWallet: OnyxEntry<UserWallet>, iouReportID: string): boolean {
const reportActions = ReportActionsUtils.getAllReportActions(iouReportID);
const reportActions = reportActionsByReport?.[iouReportID] ?? {};
return Object.values(reportActions).some((action) => getIndicatedMissingPaymentMethod(userWallet, iouReportID, action) !== undefined);
}

Expand All @@ -5761,8 +5761,8 @@ function shouldCreateNewMoneyRequestReport(existingIOUReport: OnyxEntry<Report>
* Checks if report contains actions with errors
*/
function hasActionsWithErrors(reportID: string): boolean {
const reportActions = ReportActionsUtils.getAllReportActions(reportID ?? '');
return Object.values(reportActions ?? {}).some((action) => !isEmptyObject(action.errors));
const reportActions = reportActionsByReport?.[reportID ?? ''] ?? {};
return Object.values(reportActions).some((action) => !isEmptyObject(action.errors));
}

function getReportActionActorAccountID(reportAction: OnyxEntry<ReportAction>, iouReport: OnyxEntry<Report> | undefined): number | undefined {
Expand Down
5 changes: 4 additions & 1 deletion src/libs/SidebarUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import * as ReportUtils from './ReportUtils';
import * as TaskUtils from './TaskUtils';
import * as UserUtils from './UserUtils';

const reportActionsByReport: OnyxCollection<ReportActions> = {};
const visibleReportActionItems: ReportActions = {};

Onyx.connect({
Expand All @@ -31,7 +32,9 @@ Onyx.connect({
if (!key || !actions) {
return;
}

const reportID = CollectionUtils.extractCollectionItemID(key);
reportActionsByReport[reportID] = actions;

const actionsArray: ReportAction[] = ReportActionsUtils.getSortedReportActions(Object.values(actions));

Expand Down Expand Up @@ -84,7 +87,7 @@ function getOrderedReportIDs(

const parentReportActionsKey = `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report.parentReportID}`;
const parentReportActions = allReportActions?.[parentReportActionsKey];
const reportActions = ReportActionsUtils.getAllReportActions(report.reportID);
const reportActions = reportActionsByReport?.[report.reportID] ?? {};
const parentReportAction = parentReportActions?.find((action) => action && action?.reportActionID === report.parentReportActionID);
const doesReportHaveViolations = !!(
betas?.includes(CONST.BETAS.VIOLATIONS) &&
Expand Down
21 changes: 17 additions & 4 deletions src/libs/WorkspacesSettingsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import type {ValueOf} from 'type-fest';
import CONST from '@src/CONST';
import type {TranslationPaths} from '@src/languages/types';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Policy, PolicyMembers, ReimbursementAccount, Report} from '@src/types/onyx';
import type {Policy, PolicyMembers, ReimbursementAccount, Report, ReportActions} from '@src/types/onyx';
import type {Unit} from '@src/types/onyx/Policy';
import * as CollectionUtils from './CollectionUtils';
import * as CurrencyUtils from './CurrencyUtils';
import type {Phrase, PhraseParameters} from './Localize';
import * as OptionsListUtils from './OptionsListUtils';
import {hasCustomUnitsError, hasPolicyError, hasPolicyMemberError, hasTaxRateError} from './PolicyUtils';
import * as ReportActionsUtils from './ReportActionsUtils';
import * as ReportUtils from './ReportUtils';

type CheckingMethod = () => boolean;
Expand Down Expand Up @@ -52,12 +52,25 @@ Onyx.connect({
},
});

const reportActionsByReport: OnyxCollection<ReportActions> = {};
Onyx.connect({
key: ONYXKEYS.COLLECTION.REPORT_ACTIONS,
callback: (actions, key) => {
if (!key || !actions) {
return;
}

const reportID = CollectionUtils.extractCollectionItemID(key);
reportActionsByReport[reportID] = actions;
},
});

/**
* @param report
* @returns BrickRoad for the policy passed as a param
*/
const getBrickRoadForPolicy = (report: Report): BrickRoad => {
const reportActions = ReportActionsUtils.getAllReportActions(report.reportID);
const reportActions = reportActionsByReport?.[report.reportID] ?? {};
const reportErrors = OptionsListUtils.getAllReportErrors(report, reportActions);
const doesReportContainErrors = Object.keys(reportErrors ?? {}).length !== 0 ? CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR : undefined;
if (doesReportContainErrors) {
Expand All @@ -67,7 +80,7 @@ const getBrickRoadForPolicy = (report: Report): BrickRoad => {
// To determine if the report requires attention from the current user, we need to load the parent report action
let itemParentReportAction = {};
if (report.parentReportID) {
const itemParentReportActions = ReportActionsUtils.getAllReportActions(report.parentReportID);
const itemParentReportActions = reportActionsByReport[report.parentReportID] ?? {};
itemParentReportAction = report.parentReportActionID ? itemParentReportActions[report.parentReportActionID] : {};
}
const reportOption = {...report, isUnread: ReportUtils.isUnread(report), isUnreadWithMention: ReportUtils.isUnreadWithMention(report)};
Expand Down
16 changes: 15 additions & 1 deletion src/libs/actions/IOU.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import type {
UpdateMoneyRequestParams,
} from '@libs/API/parameters';
import {WRITE_COMMANDS} from '@libs/API/types';
import * as CollectionUtils from '@libs/CollectionUtils';
import * as CurrencyUtils from '@libs/CurrencyUtils';
import DateUtils from '@libs/DateUtils';
import DistanceRequestUtils from '@libs/DistanceRequestUtils';
Expand Down Expand Up @@ -258,6 +259,19 @@ Onyx.connect({
callback: (value) => (allPolicies = value),
});

const reportActionsByReport: OnyxCollection<OnyxTypes.ReportActions> = {};
Onyx.connect({
key: ONYXKEYS.COLLECTION.REPORT_ACTIONS,
callback: (actions, key) => {
if (!key || !actions) {
return;
}

const reportID = CollectionUtils.extractCollectionItemID(key);
reportActionsByReport[reportID] = actions;
},
});

/**
* Returns the policy of the report
*/
Expand Down Expand Up @@ -4874,7 +4888,7 @@ function canIOUBePaid(iouReport: OnyxEntry<OnyxTypes.Report> | EmptyObject, chat
}

function hasIOUToApproveOrPay(chatReport: OnyxEntry<OnyxTypes.Report> | EmptyObject, excludedIOUReportID: string): boolean {
const chatReportActions = ReportActionsUtils.getAllReportActions(chatReport?.reportID ?? '');
const chatReportActions = reportActionsByReport?.[chatReport?.reportID ?? ''] ?? {};

return Object.values(chatReportActions).some((action) => {
const iouReport = ReportUtils.getReport(action.childReportID ?? '');
Expand Down
18 changes: 17 additions & 1 deletion src/libs/actions/ReportActions.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import type {OnyxCollection} from 'react-native-onyx';
import Onyx from 'react-native-onyx';
import * as CollectionUtils from '@libs/CollectionUtils';
import * as ReportActionUtils from '@libs/ReportActionsUtils';
import * as ReportUtils from '@libs/ReportUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {ReportActions} from '@src/types/onyx/ReportAction';
import type ReportAction from '@src/types/onyx/ReportAction';
import * as Report from './Report';

Expand Down Expand Up @@ -56,6 +59,19 @@ function clearReportActionErrors(reportID: string, reportAction: ReportAction, k
});
}

const reportActionsByReport: OnyxCollection<ReportActions> = {};
Onyx.connect({
key: ONYXKEYS.COLLECTION.REPORT_ACTIONS,
callback: (actions, key) => {
if (!key || !actions) {
return;
}

const reportID = CollectionUtils.extractCollectionItemID(key);
reportActionsByReport[reportID] = actions;
},
});

/**
*
ignore: `undefined` means we want to check both parent and children report actions
Expand All @@ -78,7 +94,7 @@ function clearAllRelatedReportActionErrors(reportID: string, reportAction: Repor
}

if (reportAction.childReportID && ignore !== 'child') {
const childActions = ReportActionUtils.getAllReportActions(reportAction.childReportID);
const childActions = reportActionsByReport?.[reportAction.childReportID] ?? {};
Object.values(childActions).forEach((action) => {
const childErrorKeys = Object.keys(action.errors ?? {}).filter((err) => errorKeys.includes(err));
clearAllRelatedReportActionErrors(reportAction.childReportID ?? '', action, 'parent', childErrorKeys);
Expand Down
5 changes: 5 additions & 0 deletions tests/actions/EnforceActionExportRestrictions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ describe('ReportUtils', () => {
// @ts-expect-error the test is asserting that it's undefined, so the TS error is normal
expect(ReportUtils.getPolicy).toBeUndefined();
});

it('does not export getAllReportActions', () => {
// @ts-expect-error the test is asserting that it's undefined, so the TS error is normal
expect(ReportUtils.getAllReportActions).toBeUndefined();
});
});

describe('Policy', () => {
Expand Down

0 comments on commit 617f9e9

Please sign in to comment.