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
7 changes: 4 additions & 3 deletions src/libs/actions/IOU/MoneyRequestBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1301,9 +1301,9 @@ function maybeUpdateReportNameForFormulaTitle(
policy: OnyxEntry<OnyxTypes.Policy>,
getCurrencyDecimals: CurrencyListActionsContextType['getCurrencyDecimals'],
optimisticTransactions: Record<string, OnyxTypes.Transaction> = {},
iouReportNameValuePairs?: OnyxEntry<OnyxTypes.ReportNameValuePairs>,
): OnyxTypes.Report {
const allReportNameValuePairs = getAllReportNameValuePairs();
const reportNameValuePairs = allReportNameValuePairs?.[`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${iouReport.reportID}`];
const reportNameValuePairs = iouReportNameValuePairs ?? getAllReportNameValuePairs()?.[`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${iouReport.reportID}`];
const titleField = reportNameValuePairs?.expensify_text_title;

// Manual renames null out expensify_text_title in RNVP; if RNVP isn't loaded, skip rather than overwrite.
Expand Down Expand Up @@ -1927,6 +1927,7 @@ function getUpdatedMoneyRequestReportData(
transactionChanges?: TransactionChanges,
// Overlaid on Onyx in the formula context — search snapshots, prior bulk-edit iterations, etc.
additionalTransactionsForFormula: Record<string, OnyxTypes.Transaction> = {},
iouReportNameValuePairs?: OnyxEntry<OnyxTypes.ReportNameValuePairs>,
) {
const calculatedDiffAmount = calculateDiffAmount(iouReport, updatedTransaction, transaction);
// Once a prior iteration made the total pending, later ones must not recompute against a stale base.
Expand Down Expand Up @@ -1987,7 +1988,7 @@ function getUpdatedMoneyRequestReportData(
if (updatedTransaction?.transactionID) {
optimisticTransactions[updatedTransaction.transactionID] = updatedTransaction;
}
updatedMoneyRequestReport = maybeUpdateReportNameForFormulaTitle(updatedMoneyRequestReport, policy, getCurrencyDecimals, optimisticTransactions);
updatedMoneyRequestReport = maybeUpdateReportNameForFormulaTitle(updatedMoneyRequestReport, policy, getCurrencyDecimals, optimisticTransactions, iouReportNameValuePairs);
}
} else {
updatedMoneyRequestReport = updateIOUOwnerAndTotal(iouReport, actorAccountID ?? CONST.DEFAULT_NUMBER_ID, diff, getCurrency(transaction), false, true, isTransactionOnHold);
Expand Down
17 changes: 17 additions & 0 deletions src/libs/actions/IOU/UpdateMoneyRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ type UpdateMoneyRequestDateParams = {
getCurrencySymbol: CurrencyListActionsContextType['getCurrencySymbol'];
rules: OnyxCollection<OnyxTypes.Rule>;
isVendorMatchingBetaEnabled: boolean | undefined;
/** RNVP of `parentReport`. Falls back to the module-level Onyx cache when omitted */
parentReportNameValuePairs?: OnyxEntry<OnyxTypes.ReportNameValuePairs>;
};

type SearchSnapshotOnyxData = {
Expand Down Expand Up @@ -266,6 +268,7 @@ function updateMoneyRequestDate({
getCurrencySymbol,
rules,
isVendorMatchingBetaEnabled,
parentReportNameValuePairs,
}: UpdateMoneyRequestDateParams) {
const isTrackExpense = isTrackExpenseReport(transactionThreadReport) && isSelfDM(parentReport);
const effectivePolicy = isTrackExpense ? policyForTrackExpense : policy;
Expand Down Expand Up @@ -368,6 +371,7 @@ function updateMoneyRequestDate({
getCurrencyDecimals,
getCurrencySymbol,
rules,
iouReportNameValuePairs: parentReportNameValuePairs,
Comment thread
parasharrajat marked this conversation as resolved.
Resolved
});
removeTransactionFromDuplicateTransactionViolation(data.onyxData, transactionID, transactions, transactionViolations);
}
Expand Down Expand Up @@ -576,6 +580,7 @@ function updateMoneyRequestMerchant({
getCurrencySymbol,
rules,
isVendorMatchingBetaEnabled,
parentReportNameValuePairs,
}: {
translate: LocaleContextProps['translate'];
isVendorMatchingBetaEnabled: boolean | undefined;
Expand All @@ -600,6 +605,8 @@ function updateMoneyRequestMerchant({
getCurrencyDecimals: CurrencyListActionsContextType['getCurrencyDecimals'];
getCurrencySymbol: CurrencyListActionsContextType['getCurrencySymbol'];
rules: OnyxCollection<OnyxTypes.Rule>;
/** RNVP of `parentReport`. Falls back to the module-level Onyx cache when omitted */
parentReportNameValuePairs?: OnyxEntry<OnyxTypes.ReportNameValuePairs>;
}) {
const transactionChanges: TransactionChanges = {
merchant: value,
Expand Down Expand Up @@ -645,6 +652,7 @@ function updateMoneyRequestMerchant({
getCurrencyDecimals,
getCurrencySymbol,
rules,
iouReportNameValuePairs: parentReportNameValuePairs,
});
}
const {params, onyxData} = data;
Expand Down Expand Up @@ -1575,6 +1583,7 @@ function updateMoneyRequestDescription({
rules,
isVendorMatchingBetaEnabled,
policyVendors,
parentReportNameValuePairs,
}: {
translate: LocaleContextProps['translate'];
isVendorMatchingBetaEnabled: boolean | undefined;
Expand All @@ -1599,6 +1608,8 @@ function updateMoneyRequestDescription({
getCurrencyDecimals: CurrencyListActionsContextType['getCurrencyDecimals'];
getCurrencySymbol: CurrencyListActionsContextType['getCurrencySymbol'];
rules: OnyxCollection<OnyxTypes.Rule>;
/** RNVP of `parentReport`. Falls back to the module-level Onyx cache when omitted */
parentReportNameValuePairs?: OnyxEntry<OnyxTypes.ReportNameValuePairs>;
}) {
const parsedComment = getParsedComment(comment);
const transactionChanges: TransactionChanges = {
Expand Down Expand Up @@ -1645,6 +1656,7 @@ function updateMoneyRequestDescription({
getCurrencyDecimals,
getCurrencySymbol,
rules,
iouReportNameValuePairs: parentReportNameValuePairs,
});
}
const {params, onyxData} = data;
Expand Down Expand Up @@ -1968,6 +1980,8 @@ type GetUpdateMoneyRequestParamsType = {
rules: OnyxCollection<OnyxTypes.Rule>;
isVendorMatchingBetaEnabled: boolean | undefined;
policyVendors?: OnyxEntry<OnyxTypes.PolicyVendors>;
/** RNVP of `iouReport`. Falls back to the module-level Onyx cache when omitted */
iouReportNameValuePairs?: OnyxEntry<OnyxTypes.ReportNameValuePairs>;
};

type UpdateMoneyRequestDataKeys =
Expand Down Expand Up @@ -2023,6 +2037,7 @@ function getUpdateMoneyRequestParams(params: GetUpdateMoneyRequestParamsType): U
rules,
isVendorMatchingBetaEnabled,
policyVendors: policyVendorsParam,
iouReportNameValuePairs,
} = params;
const optimisticData: Array<
OnyxUpdate<
Expand Down Expand Up @@ -2282,6 +2297,8 @@ function getUpdateMoneyRequestParams(params: GetUpdateMoneyRequestParamsType): U
getCurrencyDecimals,
updatedReportAction?.actorAccountID,
transactionChanges,
undefined, // additionalTransactionsForFormula
iouReportNameValuePairs,
);

optimisticData.push(
Expand Down
5 changes: 4 additions & 1 deletion src/pages/iou/request/step/DynamicIOURequestStepDate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,15 @@ function DynamicIOURequestStepDate({
return;
}

const [rules, lastSelectedDistanceRates, policyCategories, policyTags, reportPolicyTags, introSelected] = await Onyx.multiGet([
// eslint-disable-next-line rulesdir/no-onyx-get-snapshot-key -- replaces maybeUpdateReportNameForFormulaTitle's module-level Onyx.connect fallback, which never read a Search snapshot
const [rules, lastSelectedDistanceRates, policyCategories, policyTags, reportPolicyTags, introSelected, parentReportNameValuePairs] = await Onyx.multiGet([
ONYXKEYS.COLLECTION.RULE,
ONYXKEYS.NVP_LAST_SELECTED_DISTANCE_RATES,
`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${report?.policyID}`,
`${ONYXKEYS.COLLECTION.POLICY_TAGS}${report?.policyID}`,
`${ONYXKEYS.COLLECTION.POLICY_TAGS}${getNonEmptyStringOnyxID(parentReport?.policyID)}`,
ONYXKEYS.NVP_INTRO_SELECTED,
`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${getNonEmptyStringOnyxID(report?.parentReportID)}`,
]);
const isTransactionDraft = shouldUseTransactionDraft(action);

Expand Down Expand Up @@ -166,6 +168,7 @@ function DynamicIOURequestStepDate({
getCurrencyDecimals,
getCurrencySymbol,
rules,
parentReportNameValuePairs,
});
} else {
setMoneyRequestCreated(transactionID, newCreated, isTransactionDraft, hasReceipt(transaction));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,14 @@ function DynamicIOURequestStepDescription({
}

if (action === CONST.IOU.ACTION.EDIT) {
const [rules, policyTags, policyVendors, reportPolicyTags, introSelected] = await Onyx.multiGet([
// eslint-disable-next-line rulesdir/no-onyx-get-snapshot-key -- replaces maybeUpdateReportNameForFormulaTitle's module-level Onyx.connect fallback, which never read a Search snapshot
const [rules, policyTags, policyVendors, reportPolicyTags, introSelected, parentReportNameValuePairs] = await Onyx.multiGet([
ONYXKEYS.COLLECTION.RULE,
`${ONYXKEYS.COLLECTION.POLICY_TAGS}${report?.policyID}`,
`${ONYXKEYS.COLLECTION.POLICY_VENDORS}${report?.policyID}`,
`${ONYXKEYS.COLLECTION.POLICY_TAGS}${getNonEmptyStringOnyxID(parentReport?.policyID)}`,
ONYXKEYS.NVP_INTRO_SELECTED,
`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${getNonEmptyStringOnyxID(report?.parentReportID)}`,
]);
updateMoneyRequestDescription({
translate,
Expand All @@ -179,6 +181,7 @@ function DynamicIOURequestStepDescription({
getCurrencyDecimals,
getCurrencySymbol,
rules,
parentReportNameValuePairs,
});
} else {
setMoneyRequestDescription(transaction?.transactionID, newComment, isTransactionDraft, hasReceipt(transaction));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,14 @@ function DynamicIOURequestStepMerchant({
// also calling setMoneyRequestMerchant would trigger a redundant Onyx commit and
// re-render every subscriber of that key for nothing.
if (isEditing) {
const [rules, policyCategories, policyTags, reportPolicyTags, introSelected] = await Onyx.multiGet([
// eslint-disable-next-line rulesdir/no-onyx-get-snapshot-key -- replaces maybeUpdateReportNameForFormulaTitle's module-level Onyx.connect fallback, which never read a Search snapshot
const [rules, policyCategories, policyTags, reportPolicyTags, introSelected, parentReportNameValuePairs] = await Onyx.multiGet([
ONYXKEYS.COLLECTION.RULE,
`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${report?.policyID}`,
`${ONYXKEYS.COLLECTION.POLICY_TAGS}${report?.policyID}`,
`${ONYXKEYS.COLLECTION.POLICY_TAGS}${getNonEmptyStringOnyxID(parentReport?.policyID)}`,
ONYXKEYS.NVP_INTRO_SELECTED,
`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${getNonEmptyStringOnyxID(report?.parentReportID)}`,
]);
updateMoneyRequestMerchant({
translate,
Expand All @@ -167,6 +169,7 @@ function DynamicIOURequestStepMerchant({
getCurrencyDecimals,
getCurrencySymbol,
rules,
parentReportNameValuePairs,
});
} else if (!newMerchant) {
clearMoneyRequestMerchant(transactionID);
Expand Down
111 changes: 110 additions & 1 deletion tests/actions/IOUTest/GetUpdateMoneyRequestParamsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import {isRecord} from '@libs/ObjectUtils';
import CONST from '@src/CONST';
import IntlStore from '@src/languages/IntlStore';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Policy, PolicyTagLists, RecentlyUsedTags, Report, Transaction} from '@src/types/onyx';
import type {Policy, PolicyTagLists, RecentlyUsedTags, Report, ReportNameValuePairs, Transaction} from '@src/types/onyx';

import type {OnyxEntry} from 'react-native-onyx';

import Onyx from 'react-native-onyx';

Expand Down Expand Up @@ -660,3 +662,110 @@ describe('getUpdateMoneyRequestParams — next step of a submitted report', () =
expect(hasOptimisticNextStep(approvedReport, policy, {reimbursable: false})).toBe(false);
});
});

describe('getUpdateMoneyRequestParams — formula report title', () => {
const TRIP_TITLE_FORMULA = 'Trip from {report:autoreporting:start:MMM dd} to {report:autoreporting:end:MMM dd, yyyy}';
const ORIGINAL_REPORT_NAME = 'Original report name';

const titleField = {
fieldID: CONST.REPORT_FIELD_TITLE_FIELD_ID,
name: 'Title',
type: CONST.REPORT_FIELD_TYPES.FORMULA,
defaultValue: TRIP_TITLE_FORMULA,
deletable: false,
target: CONST.POLICY.DEFAULT_FIELD_LIST_TARGET,
values: [],
keys: [],
externalIDs: [],
disabledOptions: [],
orderWeight: 1,
isTax: false,
};

const policy: Policy = {
...createRandomPolicy(0, CONST.POLICY.TYPE.TEAM),
id: POLICY_ID,
autoReportingFrequency: CONST.POLICY.AUTO_REPORTING_FREQUENCIES.TRIP,
fieldList: {[CONST.POLICY.FIELDS.FIELD_LIST_TITLE]: titleField},
};

const expenseReport: Report = {
reportID: IOU_REPORT_ID,
reportName: ORIGINAL_REPORT_NAME,
type: CONST.REPORT.TYPE.EXPENSE,
policyID: POLICY_ID,
total: -1000,
currency: CONST.CURRENCY.USD,
ownerAccountID: RORY_ACCOUNT_ID,
};

const transaction: Transaction = {
transactionID: TRANSACTION_ID,
reportID: IOU_REPORT_ID,
amount: -1000,
currency: CONST.CURRENCY.USD,
created: '2025-01-10',
merchant: 'Merchant',
};

function getOptimisticReportName(iouReportNameValuePairs: OnyxEntry<ReportNameValuePairs>): string | undefined {
const {onyxData} = getUpdateMoneyRequestParams({
isVendorMatchingBetaEnabled: false,
rules: undefined,
iouReportOwnerLogin: undefined,
transactionID: TRANSACTION_ID,
transaction,
transactionThreadReport,
iouReport: expenseReport,
delegateAccountID: undefined,
transactionChanges: {created: '2025-01-15'},
policy,
policyTagList: undefined,
reportPolicyTags: undefined,
policyCategories: undefined,
currentUserAccountIDParam: RORY_ACCOUNT_ID,
currentUserEmailParam: RORY_EMAIL,
isASAPSubmitBetaEnabled: false,
isTrackIntentUser: false,
getCurrencyDecimals: getCurrencyDecimalsLocal,
getCurrencySymbol: getCurrencySymbolLocal,
iouReportNameValuePairs,
});

const reportUpdate = onyxData.optimisticData?.find((entry) => entry.key === `${ONYXKEYS.COLLECTION.REPORT}${IOU_REPORT_ID}` && isRecord(entry.value) && 'reportName' in entry.value);
const reportUpdateValue: unknown = reportUpdate?.value;
const reportName = isRecord(reportUpdateValue) ? reportUpdateValue.reportName : undefined;
return typeof reportName === 'string' ? reportName : undefined;
}

beforeEach(async () => {
await Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${POLICY_ID}`, policy);
await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${IOU_REPORT_ID}`, expenseReport);
await Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION}${TRANSACTION_ID}`, transaction);
await waitForBatchedUpdates();
});

it('should recalculate the title from the passed RNVP entry when Onyx has no RNVP for the report', () => {
// Given the report's RNVP, which marks its title as a formula, is passed in by the caller and isn't in Onyx,
// so the title can only be recalculated if the passed entry is used
// eslint-disable-next-line @typescript-eslint/naming-convention
const iouReportNameValuePairs = {expensify_text_title: titleField};

// When the expense date changes
// Then the title is recalculated with the new date
expect(getOptimisticReportName(iouReportNameValuePairs)).toBe('Trip from Jan 15 to Jan 15, 2025');
});

it('should keep the title when the passed RNVP entry has no formula title, even if Onyx has one', async () => {
// Given Onyx still holds a formula title for the report, but the caller passes an RNVP entry without one,
// as happens after the user renames the report manually
// eslint-disable-next-line @typescript-eslint/naming-convention
await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${IOU_REPORT_ID}`, {expensify_text_title: titleField});
await waitForBatchedUpdates();
const iouReportNameValuePairs = {};

// When the expense date changes
// Then the passed entry wins over the module-level cache and the title isn't recalculated
expect(getOptimisticReportName(iouReportNameValuePairs)).toBe(ORIGINAL_REPORT_NAME);
});
});
Loading
Loading