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
29 changes: 24 additions & 5 deletions src/libs/OptimisticReportNames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
33 changes: 32 additions & 1 deletion tests/unit/OptimisticReportNamesTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ const mockReportUtils = ReportUtils as jest.Mocked<typeof ReportUtils>;
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 = {
Expand Down Expand Up @@ -80,6 +86,7 @@ describe('OptimisticReportNames', () => {
private_isArchived: '',
},
},
allPolicies: {},
};
const result = shouldComputeReportName(mockReport, context);
expect(result).toBe(false);
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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: {
Expand Down