diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index e3b87d1f517b5..ae1a519147b26 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -11315,7 +11315,6 @@ function createDraftWorkspaceAndNavigateToConfirmationScreen(introSelected: Onyx } type CreateDraftTransactionParams = { - transactionID: string | undefined; reportID: string | undefined; actionName: IOUAction; reportActionID: string | undefined; @@ -11326,10 +11325,10 @@ type CreateDraftTransactionParams = { amountOwed: OnyxEntry; isRestrictedToPreferredPolicy?: boolean; preferredPolicyID?: string; + transaction: OnyxEntry; }; function createDraftTransactionAndNavigateToParticipantSelector({ - transactionID, reportID, actionName, reportActionID, @@ -11340,15 +11339,16 @@ function createDraftTransactionAndNavigateToParticipantSelector({ amountOwed, isRestrictedToPreferredPolicy = false, preferredPolicyID, + transaction, }: CreateDraftTransactionParams): void { + const transactionID = transaction?.transactionID; if (!transactionID || !reportID) { return; } - const transaction = deprecatedAllTransactions?.[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`] ?? ({} as Transaction); const reportActions = allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`] ?? ([] as ReportAction[]); - if (!transaction || !reportActions) { + if (!reportActions) { return; } diff --git a/src/pages/ReportDetailsPage.tsx b/src/pages/ReportDetailsPage.tsx index 490c6a1bc312d..b5908b6b44d30 100644 --- a/src/pages/ReportDetailsPage.tsx +++ b/src/pages/ReportDetailsPage.tsx @@ -454,7 +454,6 @@ function ReportDetailsPage({policy, report, route, reportMetadata}: ReportDetail shouldShowRightIcon: true, action: () => { createDraftTransactionAndNavigateToParticipantSelector({ - transactionID: iouTransactionID, reportID: actionReportID, actionName: CONST.IOU.ACTION.SUBMIT, reportActionID: actionableWhisperReportActionID, @@ -465,6 +464,7 @@ function ReportDetailsPage({policy, report, route, reportMetadata}: ReportDetail amountOwed, isRestrictedToPreferredPolicy, preferredPolicyID, + transaction: iouTransaction, }); }, }); @@ -477,7 +477,6 @@ function ReportDetailsPage({policy, report, route, reportMetadata}: ReportDetail shouldShowRightIcon: true, action: () => { createDraftTransactionAndNavigateToParticipantSelector({ - transactionID: iouTransactionID, reportID: actionReportID, actionName: CONST.IOU.ACTION.CATEGORIZE, reportActionID: actionableWhisperReportActionID, @@ -486,6 +485,7 @@ function ReportDetailsPage({policy, report, route, reportMetadata}: ReportDetail activePolicy, userBillingGraceEndPeriods, amountOwed, + transaction: iouTransaction, }); }, }); @@ -497,7 +497,6 @@ function ReportDetailsPage({policy, report, route, reportMetadata}: ReportDetail shouldShowRightIcon: true, action: () => { createDraftTransactionAndNavigateToParticipantSelector({ - transactionID: iouTransactionID, reportID: actionReportID, actionName: CONST.IOU.ACTION.SHARE, reportActionID: actionableWhisperReportActionID, @@ -506,6 +505,7 @@ function ReportDetailsPage({policy, report, route, reportMetadata}: ReportDetail activePolicy, userBillingGraceEndPeriods, amountOwed, + transaction: iouTransaction, }); }, }); @@ -634,6 +634,7 @@ function ReportDetailsPage({policy, report, route, reportMetadata}: ReportDetail reportActionsForOriginalReportID, userBillingGraceEndPeriods, amountOwed, + iouTransaction, ]); const displayNamesWithTooltips = useMemo(() => { diff --git a/src/pages/inbox/report/PureReportActionItem.tsx b/src/pages/inbox/report/PureReportActionItem.tsx index 0f3d04ea2a1d2..9618bad1ed06e 100644 --- a/src/pages/inbox/report/PureReportActionItem.tsx +++ b/src/pages/inbox/report/PureReportActionItem.tsx @@ -579,6 +579,8 @@ function PureReportActionItem({ const [childReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${getNonEmptyStringOnyxID(action.childReportID)}`); const [chatReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${getNonEmptyStringOnyxID(report?.chatReportID)}`); + const trackExpenseTransactionID = isActionableTrackExpense(action) ? getOriginalMessage(action)?.transactionID : undefined; + const [trackExpenseTransaction] = useOnyx(`${ONYXKEYS.COLLECTION.TRANSACTION}${getNonEmptyStringOnyxID(trackExpenseTransactionID)}`); const highlightedBackgroundColorIfNeeded = useMemo( // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing @@ -933,14 +935,12 @@ function PureReportActionItem({ const reportActionReportID = originalReportID ?? reportID; if (isActionableTrackExpense(action)) { - const transactionID = getOriginalMessage(action)?.transactionID; const options = [ { text: 'actionableMentionTrackExpense.submit', key: `${action.reportActionID}-actionableMentionTrackExpense-submit`, onPress: () => { createDraftTransactionAndNavigateToParticipantSelector({ - transactionID, reportID: reportActionReportID, actionName: CONST.IOU.ACTION.SUBMIT, reportActionID: action.reportActionID, @@ -951,6 +951,7 @@ function PureReportActionItem({ amountOwed, isRestrictedToPreferredPolicy, preferredPolicyID, + transaction: trackExpenseTransaction, }); }, }, @@ -963,7 +964,6 @@ function PureReportActionItem({ key: `${action.reportActionID}-actionableMentionTrackExpense-categorize`, onPress: () => { createDraftTransactionAndNavigateToParticipantSelector({ - transactionID, reportID: reportActionReportID, actionName: CONST.IOU.ACTION.CATEGORIZE, reportActionID: action.reportActionID, @@ -972,6 +972,7 @@ function PureReportActionItem({ activePolicy, userBillingGraceEndPeriods, amountOwed, + transaction: trackExpenseTransaction, }); }, }, @@ -980,7 +981,6 @@ function PureReportActionItem({ key: `${action.reportActionID}-actionableMentionTrackExpense-share`, onPress: () => { createDraftTransactionAndNavigateToParticipantSelector({ - transactionID, reportID: reportActionReportID, actionName: CONST.IOU.ACTION.SHARE, reportActionID: action.reportActionID, @@ -989,6 +989,7 @@ function PureReportActionItem({ activePolicy, userBillingGraceEndPeriods, amountOwed, + transaction: trackExpenseTransaction, }); }, }, @@ -1135,6 +1136,7 @@ function PureReportActionItem({ personalPolicyID, userBillingGraceEndPeriods, amountOwed, + trackExpenseTransaction, ]); /** diff --git a/tests/actions/IOUTest.ts b/tests/actions/IOUTest.ts index 9bb6210111abe..5054f2febac2d 100644 --- a/tests/actions/IOUTest.ts +++ b/tests/actions/IOUTest.ts @@ -628,7 +628,6 @@ describe('actions/IOU', () => { // When the transaction is saved to draft by selecting a category in the selfDM report const reportActionableTrackExpense = Object.values(selfDMReportActions ?? {}).find((reportAction) => isActionableTrackExpense(reportAction)); createDraftTransactionAndNavigateToParticipantSelector({ - transactionID: transaction?.transactionID, reportID: selfDMReport.reportID, actionName: CONST.IOU.ACTION.CATEGORIZE, reportActionID: reportActionableTrackExpense?.reportActionID, @@ -637,6 +636,7 @@ describe('actions/IOU', () => { activePolicy: undefined, userBillingGraceEndPeriods: undefined, amountOwed: 0, + transaction, }); await waitForBatchedUpdates(); @@ -1201,7 +1201,6 @@ describe('actions/IOU', () => { // When a draft is created for categorization createDraftTransactionAndNavigateToParticipantSelector({ - transactionID: createdTransaction?.transactionID, reportID: selfDMReport.reportID, actionName: CONST.IOU.ACTION.CATEGORIZE, reportActionID: actionableWhisper?.reportActionID, @@ -1210,6 +1209,7 @@ describe('actions/IOU', () => { activePolicy: undefined, userBillingGraceEndPeriods: undefined, amountOwed: 0, + transaction: createdTransaction, }); await waitForBatchedUpdates(); @@ -1843,7 +1843,6 @@ describe('actions/IOU', () => { // When createDraftTransactionAndNavigateToParticipantSelector is called with draftTransactionIDs createDraftTransactionAndNavigateToParticipantSelector({ - transactionID: transactionToCategorize.transactionID, reportID: selfDMReport.reportID, actionName: CONST.IOU.ACTION.CATEGORIZE, reportActionID, @@ -1852,6 +1851,7 @@ describe('actions/IOU', () => { activePolicy: undefined, userBillingGraceEndPeriods: undefined, amountOwed: 0, + transaction: transactionToCategorize, }); await waitForBatchedUpdates(); @@ -1891,7 +1891,6 @@ describe('actions/IOU', () => { // When createDraftTransactionAndNavigateToParticipantSelector is called with empty allTransactionDrafts createDraftTransactionAndNavigateToParticipantSelector({ - transactionID: originalTransaction.transactionID, reportID: selfDMReport.reportID, actionName: CONST.IOU.ACTION.CATEGORIZE, reportActionID, @@ -1900,6 +1899,7 @@ describe('actions/IOU', () => { activePolicy: undefined, userBillingGraceEndPeriods: undefined, amountOwed: 0, + transaction: originalTransaction, }); await waitForBatchedUpdates(); @@ -1921,14 +1921,13 @@ describe('actions/IOU', () => { expect(draftTransaction?.linkedTrackedExpenseReportID).toBe(selfDMReport.reportID); }); - it('should not create draft transaction when transactionID is undefined', async () => { + it('should not create draft transaction when transaction is undefined', async () => { // Given a selfDM report const selfDMReport = createRandomReport(1, CONST.REPORT.CHAT_TYPE.SELF_DM); await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${selfDMReport.reportID}`, selfDMReport); - // When createDraftTransactionAndNavigateToParticipantSelector is called with undefined transactionID + // When createDraftTransactionAndNavigateToParticipantSelector is called with undefined transaction createDraftTransactionAndNavigateToParticipantSelector({ - transactionID: undefined, reportID: selfDMReport.reportID, actionName: CONST.IOU.ACTION.CATEGORIZE, reportActionID: 'some-report-action-id', @@ -1937,6 +1936,7 @@ describe('actions/IOU', () => { activePolicy: undefined, userBillingGraceEndPeriods: undefined, amountOwed: 0, + transaction: undefined, }); await waitForBatchedUpdates(); @@ -1960,13 +1960,13 @@ describe('actions/IOU', () => { // When createDraftTransactionAndNavigateToParticipantSelector is called with undefined reportID createDraftTransactionAndNavigateToParticipantSelector({ - transactionID: transaction.transactionID, reportID: undefined, actionName: CONST.IOU.ACTION.CATEGORIZE, reportActionID: 'some-report-action-id', introSelected: {choice: CONST.ONBOARDING_CHOICES.MANAGE_TEAM}, draftTransactionIDs: [], activePolicy: undefined, + transaction, userBillingGraceEndPeriods: undefined, amountOwed: 0, }); diff --git a/tests/unit/ReportUtilsTest.ts b/tests/unit/ReportUtilsTest.ts index ac34e40dec9e7..09268e664d855 100644 --- a/tests/unit/ReportUtilsTest.ts +++ b/tests/unit/ReportUtilsTest.ts @@ -13194,6 +13194,27 @@ describe('ReportUtils', () => { }); describe('createDraftTransactionAndNavigateToParticipantSelector', () => { + it('should return early and not navigate when transaction is undefined', async () => { + jest.clearAllMocks(); + await Onyx.clear(); + await Onyx.set(ONYXKEYS.SESSION, {email: currentUserEmail, accountID: currentUserAccountID}); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${1}`, {}); + + createDraftTransactionAndNavigateToParticipantSelector({ + reportID: '1', + actionName: CONST.IOU.ACTION.CATEGORIZE, + reportActionID: '1', + introSelected: undefined, + draftTransactionIDs: [], + activePolicy: undefined, + userBillingGraceEndPeriods: undefined, + amountOwed: 0, + transaction: undefined, + }); + + expect(Navigation.navigate).not.toHaveBeenCalled(); + }); + describe('when action is CATEGORIZE', () => { beforeEach(async () => { jest.clearAllMocks(); @@ -13217,7 +13238,6 @@ describe('ReportUtils', () => { // When we call createDraftTransactionAndNavigateToParticipantSelector with the restricted policy createDraftTransactionAndNavigateToParticipantSelector({ - transactionID: transaction.transactionID, reportID: '1', actionName: CONST.IOU.ACTION.CATEGORIZE, reportActionID: '1', @@ -13226,6 +13246,7 @@ describe('ReportUtils', () => { activePolicy, userBillingGraceEndPeriods: undefined, amountOwed: 1, + transaction, }); // Then it should navigate to the restricted action page @@ -13252,7 +13273,6 @@ describe('ReportUtils', () => { // When we call createDraftTransactionAndNavigateToParticipantSelector createDraftTransactionAndNavigateToParticipantSelector({ - transactionID: transaction.transactionID, reportID: '1', actionName: CONST.IOU.ACTION.CATEGORIZE, reportActionID: '1', @@ -13261,6 +13281,7 @@ describe('ReportUtils', () => { activePolicy, userBillingGraceEndPeriods, amountOwed: 0, + transaction, }); // Then it should navigate to the restricted action page @@ -13290,7 +13311,6 @@ describe('ReportUtils', () => { // When we call createDraftTransactionAndNavigateToParticipantSelector createDraftTransactionAndNavigateToParticipantSelector({ - transactionID: transaction.transactionID, reportID: '1', actionName: CONST.IOU.ACTION.CATEGORIZE, reportActionID: '1', @@ -13299,6 +13319,7 @@ describe('ReportUtils', () => { activePolicy, userBillingGraceEndPeriods: undefined, amountOwed: 0, + transaction, }); // Then it should navigate to the category step @@ -13330,7 +13351,6 @@ describe('ReportUtils', () => { // When we call createDraftTransactionAndNavigateToParticipantSelector with undefined activePolicy createDraftTransactionAndNavigateToParticipantSelector({ - transactionID: transaction.transactionID, reportID: '2', actionName: CONST.IOU.ACTION.CATEGORIZE, reportActionID: '2', @@ -13339,6 +13359,7 @@ describe('ReportUtils', () => { activePolicy: undefined, userBillingGraceEndPeriods: undefined, amountOwed: 0, + transaction, }); // Then it should automatically pick the available policy and navigate to the category step @@ -13357,7 +13378,6 @@ describe('ReportUtils', () => { // When we call createDraftTransactionAndNavigateToParticipantSelector with undefined activePolicy createDraftTransactionAndNavigateToParticipantSelector({ - transactionID: transaction.transactionID, reportID: '1', actionName: CONST.IOU.ACTION.CATEGORIZE, reportActionID: '1', @@ -13366,6 +13386,7 @@ describe('ReportUtils', () => { activePolicy: undefined, userBillingGraceEndPeriods: undefined, amountOwed: 0, + transaction, }); // Then it should navigate to the upgrade page because no policies were found to categorize with @@ -13405,7 +13426,6 @@ describe('ReportUtils', () => { // When we call createDraftTransactionAndNavigateToParticipantSelector with undefined activePolicy createDraftTransactionAndNavigateToParticipantSelector({ - transactionID: transaction.transactionID, reportID: '1', actionName: CONST.IOU.ACTION.CATEGORIZE, reportActionID: '1', @@ -13414,6 +13434,7 @@ describe('ReportUtils', () => { activePolicy: undefined, userBillingGraceEndPeriods: undefined, amountOwed: 0, + transaction, }); // Then it should navigate to the upgrade page because it's ambiguous which policy to use @@ -13449,7 +13470,6 @@ describe('ReportUtils', () => { // When we call createDraftTransactionAndNavigateToParticipantSelector createDraftTransactionAndNavigateToParticipantSelector({ - transactionID: transaction.transactionID, reportID: '1', actionName: CONST.IOU.ACTION.CATEGORIZE, reportActionID: '1', @@ -13458,6 +13478,7 @@ describe('ReportUtils', () => { activePolicy, userBillingGraceEndPeriods: undefined, amountOwed: 0, + transaction, }); // Then it should log a warning and not navigate @@ -13491,7 +13512,6 @@ describe('ReportUtils', () => { // When we call with amountOwed = 0 createDraftTransactionAndNavigateToParticipantSelector({ - transactionID: transaction.transactionID, reportID: '1', actionName: CONST.IOU.ACTION.CATEGORIZE, reportActionID: '1', @@ -13500,6 +13520,7 @@ describe('ReportUtils', () => { activePolicy, userBillingGraceEndPeriods: undefined, amountOwed: 0, + transaction, }); // Then it should NOT navigate to restricted action page, but to category step @@ -13525,7 +13546,6 @@ describe('ReportUtils', () => { // When we call with amountOwed = 50 createDraftTransactionAndNavigateToParticipantSelector({ - transactionID: transaction.transactionID, reportID: '1', actionName: CONST.IOU.ACTION.CATEGORIZE, reportActionID: '1', @@ -13534,6 +13554,7 @@ describe('ReportUtils', () => { activePolicy, userBillingGraceEndPeriods: undefined, amountOwed: 50, + transaction, }); // Then it should navigate to restricted action page