Skip to content

Commit

Permalink
feat: use correct report title everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
allroundexperts committed Jan 22, 2024
1 parent ec91f2c commit 51c3baf
Showing 1 changed file with 38 additions and 20 deletions.
58 changes: 38 additions & 20 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ import type {
PersonalDetailsList,
Policy,
PolicyReportField,
PolicyReportFields,

Check failure on line 24 in src/libs/ReportUtils.ts

View workflow job for this annotation

GitHub Actions / typecheck

'"@src/types/onyx"' has no exported member named 'PolicyReportFields'. Did you mean 'PolicyReportField'?
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';
Expand Down Expand Up @@ -457,6 +457,19 @@ Onyx.connect({
callback: (value) => (allPolicies = value),
});

let allPolicyReportFields: OnyxCollection<PolicyReportFields>;
Onyx.connect({
key: ONYXKEYS.COLLECTION.POLICY_REPORT_FIELDS,
waitForCollectionCallback: true,
callback: (value) => (allPolicyReportFields = value),
});

let allBetas: OnyxEntry<Beta[]>;
Onyx.connect({
key: ONYXKEYS.BETAS,
callback: (value) => (allBetas = value),
});

let loginList: OnyxEntry<Login>;
Onyx.connect({
key: ONYXKEYS.LOGIN_LIST,
Expand Down Expand Up @@ -1897,10 +1910,34 @@ function getPolicyExpenseChatName(report: OnyxEntry<Report>, policy: OnyxEntry<P
return reportOwnerDisplayName;
}

/**
* 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<Report>, 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<Report>, policy: OnyxEntry<Policy> | 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);

Check failure on line 1939 in src/libs/ReportUtils.ts

View workflow job for this annotation

GitHub Actions / typecheck

'field' is of type 'unknown'.
}
const moneyRequestTotal = getMoneyRequestReimbursableTotal(report);

Check failure on line 1941 in src/libs/ReportUtils.ts

View workflow job for this annotation

GitHub Actions / typecheck

Argument of type '{}' is not assignable to parameter of type 'PolicyReportField'.
const formattedAmount = CurrencyUtils.convertToDisplayString(moneyRequestTotal, report?.currency, hasOnlyDistanceRequestTransactions(report?.reportID));
const payerOrApproverName = isExpenseReport(report) ? getPolicyName(report, false, policy) : getDisplayNameForParticipant(report?.managerID) ?? '';
Expand Down Expand Up @@ -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<Report>, 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
*/
Expand Down

0 comments on commit 51c3baf

Please sign in to comment.