diff --git a/src/libs/OptimisticReportNames.ts b/src/libs/OptimisticReportNames.ts index ef38240d27e0..f630b6a68565 100644 --- a/src/libs/OptimisticReportNames.ts +++ b/src/libs/OptimisticReportNames.ts @@ -11,16 +11,35 @@ import {compute, FORMULA_PART_TYPES, parse} from './Formula'; import Log from './Log'; import type {UpdateContext} from './OptimisticReportNamesConnectionManager'; import Permissions from './Permissions'; -import {isArchivedReport} from './ReportUtils'; +import {getTitleReportField, isArchivedReport} from './ReportUtils'; /** * Get the title field from report name value pairs */ +// eslint-disable-next-line @typescript-eslint/no-unused-vars -- this will be used in near future function getTitleFieldFromRNVP(reportID: string, context: UpdateContext) { const reportNameValuePairs = context.allReportNameValuePairs[`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${reportID}`]; return reportNameValuePairs?.expensify_text_title; } +/** + * Temporary function to get the title field from a policy. Eventually we want to move this to report name value pairs. + * @param policyId + * @param context + */ +function getTitleFieldFromPolicy(policyId: string | undefined, context: UpdateContext) { + if (!policyId) { + return; + } + + const policy = context.allPolicies[`${ONYXKEYS.COLLECTION.POLICY}${policyId}`]; + if (!policy || !policy.fieldList) { + return; + } + + return getTitleReportField(policy.fieldList); +} + /** * Get the object type from an Onyx key */ @@ -154,9 +173,9 @@ function shouldComputeReportName(report: Report, context: UpdateContext): boolea } // Only compute names for expense reports with policies that have title fields - // Check if the report has a title field with a formula in rNVP - const reportTitleField = getTitleFieldFromRNVP(report.reportID, context); - if (!reportTitleField?.defaultValue) { + // Check if the report has a title field with a formula in policy + const policyTitleField = getTitleFieldFromPolicy(report.policyID, context); + if (!policyTitleField?.defaultValue) { return false; } return true; @@ -194,7 +213,7 @@ function computeReportNameIfNeeded(report: Report | undefined, incomingUpdate: O return null; } - const titleField = getTitleFieldFromRNVP(targetReport.reportID, context); + const titleField = getTitleFieldFromPolicy(targetReport.policyID, context); // Quick check: see if the update might affect the report name const updateType = determineObjectTypeByKey(incomingUpdate.key); diff --git a/tests/unit/OptimisticReportNamesTest.ts b/tests/unit/OptimisticReportNamesTest.ts index 5a3d0ffe3144..8d6200f67a96 100644 --- a/tests/unit/OptimisticReportNamesTest.ts +++ b/tests/unit/OptimisticReportNamesTest.ts @@ -23,6 +23,12 @@ const mockReportUtils = ReportUtils as jest.Mocked; describe('OptimisticReportNames', () => { const mockPolicy = { id: 'policy1', + fieldList: { + [CONST.REPORT_FIELD_TITLE_FIELD_ID]: { + fieldID: CONST.REPORT_FIELD_TITLE_FIELD_ID, + defaultValue: '{report:type} - {report:total}', + }, + }, } as unknown as Policy; const mockReport = { @@ -80,6 +86,7 @@ describe('OptimisticReportNames', () => { private_isArchived: '', }, }, + allPolicies: {}, }; const result = shouldComputeReportName(mockReport, context); expect(result).toBe(false); @@ -131,7 +138,7 @@ describe('OptimisticReportNames', () => { }); describe('updateOptimisticReportNamesFromUpdates()', () => { - test.skip('should detect new report creation and add name update', () => { + test('should detect new report creation and add name update', () => { const updates = [ { key: 'report_456' as OnyxKey, @@ -180,6 +187,18 @@ describe('OptimisticReportNames', () => { // eslint-disable-next-line @typescript-eslint/naming-convention report_789: {...mockReport, reportID: '789'}, }, + allPolicies: { + // eslint-disable-next-line @typescript-eslint/naming-convention + policy_policy1: { + id: 'policy1', + fieldList: { + [CONST.REPORT_FIELD_TITLE_FIELD_ID]: { + fieldID: CONST.REPORT_FIELD_TITLE_FIELD_ID, + defaultValue: 'Policy: {report:policyname}', + }, + }, + } as unknown as Policy, + }, allReportNameValuePairs: { // eslint-disable-next-line @typescript-eslint/naming-convention reportNameValuePairs_123: {private_isArchived: '', expensify_text_title: {defaultValue: 'Policy: {report:policyname}'}} as unknown as ReportNameValuePairs, @@ -381,6 +400,18 @@ describe('OptimisticReportNames', () => { }, } as unknown as ReportNameValuePairs, }, + allPolicies: { + // eslint-disable-next-line @typescript-eslint/naming-convention + policy_policy1: { + id: 'policy1', + fieldList: { + [CONST.REPORT_FIELD_TITLE_FIELD_ID]: { + fieldID: CONST.REPORT_FIELD_TITLE_FIELD_ID, + defaultValue: 'Report from {report:startdate}', + }, + }, + } as unknown as Policy, + }, allTransactions: { // eslint-disable-next-line @typescript-eslint/naming-convention transactions_formula123: {