Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Use formula for the optimistic expense report #37160

Merged
merged 3 commits into from
Feb 28, 2024
Merged
Changes from 1 commit
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
35 changes: 33 additions & 2 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {format} from 'date-fns';
import ExpensiMark from 'expensify-common/lib/ExpensiMark';
import Str from 'expensify-common/lib/str';
import {isEmpty} from 'lodash';
Expand Down Expand Up @@ -1996,6 +1997,13 @@ function getFormulaTypeReportField(reportFields: PolicyReportFields) {
return Object.values(reportFields).find((field) => field.type === 'formula');
}

/**
* Given a set of report fields, return the field that refers to title
*/
function getTitleReportField(reportFields: PolicyReportFields) {
return Object.values(reportFields).find((field) => isReportFieldOfTypeTitle(field));
}

/**
* Get the report fields attached to the policy given policyID
*/
Expand Down Expand Up @@ -2904,6 +2912,26 @@ function buildOptimisticIOUReport(payeeAccountID: number, payerAccountID: number
};
}

/**
* Populates the report field formula with the values from the report and policy.
* Currently, is supports only optimistic expense reports.
paultsimura marked this conversation as resolved.
Show resolved Hide resolved
* Each formula field is either replaced with a value, or removed.
* If after all replacements the formula is empty, the original formula is returned.
paultsimura marked this conversation as resolved.
Show resolved Hide resolved
*/
function populateOptimisticReportFormula(formula: string, report: OptimisticExpenseReport, policy: Policy | EmptyObject): string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a couple more we can handle here optimistically

{report:created}
{report:created:yyyy-MM-dd}
{report:status}
{user:email}
{user:email|frontPart} would output bob assuming that is the currently logged in Expensify user’s email.

const result = formula
.replaceAll('{report:id}', report.reportID)
// We don't translate because the server response is always in English
.replaceAll('{report:type}', 'Expense Report')
.replaceAll('{report:startdate}', report.lastVisibleActionCreated ? format(new Date(report.lastVisibleActionCreated), CONST.DATE.FNS_FORMAT_STRING) : '')
.replaceAll('{report:total}', report.total?.toString() ?? '')
.replaceAll('{report:currency}', report.currency ?? '')
.replaceAll('{report:policyname}', policy.name ?? '')
.replaceAll(/\{report:(.+)}/g, '');

return result.trim().length ? result : formula;
}

/**
* Builds an optimistic Expense report with a randomly generated reportID
*
Expand All @@ -2913,7 +2941,6 @@ function buildOptimisticIOUReport(payeeAccountID: number, payerAccountID: number
* @param total - Amount in cents
* @param currency
*/

function buildOptimisticExpenseReport(chatReportID: string, policyID: string, payeeAccountID: number, total: number, currency: string): OptimisticExpenseReport {
// The amount for Expense reports are stored as negative value in the database
const storedTotal = total * -1;
Expand All @@ -2934,7 +2961,6 @@ function buildOptimisticExpenseReport(chatReportID: string, policyID: string, pa
type: CONST.REPORT.TYPE.EXPENSE,
ownerAccountID: payeeAccountID,
currency,

// We don't translate reportName because the server response is always in English
reportName: `${policyName} owes ${formattedTotal}`,
stateNum,
Expand All @@ -2950,6 +2976,11 @@ function buildOptimisticExpenseReport(chatReportID: string, policyID: string, pa
expenseReport.managerID = policy.submitsTo;
}

const titleReportField = getTitleReportField(getReportFieldsByPolicyID(policyID) ?? {});
if (!!titleReportField && reportFieldsEnabled(expenseReport)) {
expenseReport.reportName = populateOptimisticReportFormula(titleReportField.defaultValue, expenseReport, policy);
}

return expenseReport;
}

Expand Down
Loading