diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 9e00646886fc..fd14317169a1 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -21,12 +21,12 @@ import type { PersonalDetailsList, Policy, PolicyReportField, + PolicyReportFields, Report, ReportAction, ReportMetadata, Session, Transaction, - TransactionViolation, } from '@src/types/onyx'; import type {Errors, Icon, PendingAction} from '@src/types/onyx/OnyxCommon'; import type {IOUMessage, OriginalMessageActionName, OriginalMessageCreated} from '@src/types/onyx/OriginalMessage'; @@ -457,6 +457,19 @@ Onyx.connect({ callback: (value) => (allPolicies = value), }); +let allPolicyReportFields: OnyxCollection; +Onyx.connect({ + key: ONYXKEYS.COLLECTION.POLICY_REPORT_FIELDS, + waitForCollectionCallback: true, + callback: (value) => (allPolicyReportFields = value), +}); + +let allBetas: OnyxEntry; +Onyx.connect({ + key: ONYXKEYS.BETAS, + callback: (value) => (allBetas = value), +}); + let loginList: OnyxEntry; Onyx.connect({ key: ONYXKEYS.LOGIN_LIST, @@ -1897,10 +1910,34 @@ function getPolicyExpenseChatName(report: OnyxEntry, policy: OnyxEntry

, reportField: PolicyReportField): string { + const value = report?.reportFields?.[reportField.fieldID] ?? reportField.defaultValue; + + if (reportField.type !== 'formula') { + return value; + } + + return value.replaceAll(CONST.REGEX.REPORT_FIELD_TITLE, (match, property) => { + if (report && property in report) { + return report[property as keyof Report]?.toString() ?? match; + } + return match; + }); +} + /** * Get the title for an IOU or expense chat which will be showing the payer and the amount */ function getMoneyRequestReportName(report: OnyxEntry, policy: OnyxEntry | undefined = undefined): string { + const reportFields = Object.entries(allPolicyReportFields ?? {}).find(([key]) => key.replace(ONYXKEYS.COLLECTION.POLICY_REPORT_FIELDS, '') === report?.policyID)?.[1]; + const titleReportField = Object.values(reportFields ?? {})?.find((field) => field.type === 'formula'); + if (titleReportField && Permissions.canUseReportFields(allBetas ?? [])) { + return getReportFieldTitle(report, titleReportField); + } const moneyRequestTotal = getMoneyRequestReimbursableTotal(report); const formattedAmount = CurrencyUtils.convertToDisplayString(moneyRequestTotal, report?.currency, hasOnlyDistanceRequestTransactions(report?.reportID)); const payerOrApproverName = isExpenseReport(report) ? getPolicyName(report, false, policy) : getDisplayNameForParticipant(report?.managerID) ?? ''; @@ -4504,25 +4541,6 @@ function navigateToPrivateNotes(report: Report, session: Session) { Navigation.navigate(ROUTES.PRIVATE_NOTES_LIST.getRoute(report.reportID)); } -/** - * Given a report field and a report, get the title of the field. - * This is specially useful when we have a report field of type formula. - */ -function getReportFieldTitle(report: OnyxEntry, reportField: PolicyReportField): string { - const value = report?.reportFields?.[reportField.fieldID] ?? reportField.defaultValue; - - if (reportField.type !== 'formula') { - return value; - } - - return value.replaceAll(CONST.REGEX.REPORT_FIELD_TITLE, (match, property) => { - if (report && property in report) { - return report[property as keyof Report]?.toString() ?? match; - } - return match; - }); -} - /** * Checks if thread replies should be displayed */