Skip to content
Open
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
10 changes: 3 additions & 7 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11703,6 +11703,7 @@ type CreateDraftTransactionParams = {
reportID: string | undefined;
actionName: IOUAction;
reportActionID: string | undefined;
reportActions: OnyxEntry<ReportActions>;
introSelected: OnyxEntry<IntroSelected>;
draftTransactionIDs: string[] | undefined;
activePolicy: OnyxEntry<Policy>;
Expand Down Expand Up @@ -11731,6 +11732,7 @@ function createDraftTransactionAndNavigateToParticipantSelector({
reportID,
actionName,
reportActionID,
reportActions,
introSelected,
draftTransactionIDs,
activePolicy,
Expand All @@ -11753,13 +11755,7 @@ function createDraftTransactionAndNavigateToParticipantSelector({
return;
}

const reportActions = allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`] ?? ([] as ReportAction[]);

if (!reportActions) {
return;
}

const linkedTrackedExpenseReportAction = Object.values(reportActions)
const linkedTrackedExpenseReportAction = Object.values(reportActions ?? {})
.filter(Boolean)
.find((action) => isMoneyRequestAction(action) && getOriginalMessage(action)?.IOUTransactionID === transactionID);

Expand Down
12 changes: 9 additions & 3 deletions src/pages/DynamicReportDetailsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,10 @@ function DynamicReportDetailsPage({policy, report, route, reportMetadata, report

const {reportActions} = usePaginatedReportActions(report.reportID);
const [reportActionsForOriginalReportID] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report.reportID}`);
// The report from which a tracked expense would be submitted/categorized/shared, and its actions -
// createDraftTransactionAndNavigateToParticipantSelector uses them to find the linked track-expense action
const actionReportID = getOriginalReportID(report.reportID, parentReportAction, reportActionsForOriginalReportID);
const [actionReportActions] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${actionReportID}`);

const {removeTransaction} = useSearchSelectionActions();

Expand Down Expand Up @@ -516,7 +520,6 @@ function DynamicReportDetailsPage({policy, report, route, reportMetadata, report
}

if (isTrackExpenseReport && !isDeletedParentAction) {
const actionReportID = getOriginalReportID(report.reportID, parentReportAction, reportActionsForOriginalReportID);
const whisperAction = getTrackExpenseActionableWhisper(iouTransactionID, moneyRequestReport?.reportID, moneyRequestReportActions);
const actionableWhisperReportActionID = whisperAction?.reportActionID;
const currentUserLocalCurrency = currentUserPersonalDetails.localCurrencyCode ?? CONST.CURRENCY.USD;
Expand All @@ -526,6 +529,7 @@ function DynamicReportDetailsPage({policy, report, route, reportMetadata, report
if (!isSelfDMExpenseSplit || hasWorkspaceToSubmitTo) {
const baseSubmitParams = {
reportID: actionReportID,
reportActions: actionReportActions,
reportActionID: actionableWhisperReportActionID,
introSelected,
draftTransactionIDs,
Expand Down Expand Up @@ -607,6 +611,7 @@ function DynamicReportDetailsPage({policy, report, route, reportMetadata, report
action: () => {
createDraftTransactionAndNavigateToParticipantSelector({
reportID: actionReportID,
reportActions: actionReportActions,
actionName: CONST.IOU.ACTION.CATEGORIZE,
reportActionID: actionableWhisperReportActionID,
introSelected,
Expand All @@ -633,6 +638,7 @@ function DynamicReportDetailsPage({policy, report, route, reportMetadata, report
action: () => {
createDraftTransactionAndNavigateToParticipantSelector({
reportID: actionReportID,
reportActions: actionReportActions,
actionName: CONST.IOU.ACTION.SHARE,
reportActionID: actionableWhisperReportActionID,
introSelected,
Expand Down Expand Up @@ -769,8 +775,8 @@ function DynamicReportDetailsPage({policy, report, route, reportMetadata, report
styles.ph2,
shouldOpenRoomMembersPage,
navigateBackFromReportDetailsPath,
parentReportAction,
reportActionsForOriginalReportID,
actionReportID,
actionReportActions,
iouTransactionID,
moneyRequestReport?.reportID,
moneyRequestReportActions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ function ChatActionableButtons({action, originalReportID, reportID, hasPendingFo
const {translate} = useLocalize();
const lastWorkspaceNumber = useLastWorkspaceNumber();
const actionOwnerReportID = originalReportID ?? reportID;
const [actionOwnerReportActions] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${getNonEmptyStringOnyxID(actionOwnerReportID)}`);
const [originalReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${getNonEmptyStringOnyxID(originalReportID)}`);
const [report] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${getNonEmptyStringOnyxID(reportID)}`);
const actionOwnerReport = originalReport ?? report;
Expand Down Expand Up @@ -188,6 +189,7 @@ function ChatActionableButtons({action, originalReportID, reportID, hasPendingFo
if (isActionableTrackExpense(action)) {
const baseDraftTransactionParams = {
reportID: actionOwnerReportID,
reportActions: actionOwnerReportActions,
reportActionID: action.reportActionID,
introSelected,
draftTransactionIDs,
Expand Down
154 changes: 153 additions & 1 deletion tests/actions/IOU/CreateDraftTransactionTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import IntlStore from '@src/languages/IntlStore';
import OnyxUpdateManager from '@src/libs/actions/OnyxUpdateManager';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type {Policy, Report} from '@src/types/onyx';
import type {Policy, Report, ReportAction} from '@src/types/onyx';
import type Transaction from '@src/types/onyx/Transaction';

import type {OnyxCollection, OnyxEntry} from 'react-native-onyx';
Expand All @@ -19,6 +19,7 @@ import type {OnyxCollection, OnyxEntry} from 'react-native-onyx';
import Onyx from 'react-native-onyx';

import currencyList from '../../unit/currencyList.json';
import createRandomReportAction from '../../utils/collections/reportActions';
import {createPolicyExpenseChat, createRandomReport, createSelfDM} from '../../utils/collections/reports';
import createRandomTransaction from '../../utils/collections/transaction';
import {getGlobalFetchMock, getOnyxData} from '../../utils/TestHelper';
Expand Down Expand Up @@ -159,6 +160,7 @@ describe('actions/IOU', () => {
// When createDraftTransactionAndNavigateToParticipantSelector is called with draftTransactionIDs
createDraftTransactionAndNavigateToParticipantSelector({
reportID: selfDMReport.reportID,
reportActions: undefined,
actionName: CONST.IOU.ACTION.CATEGORIZE,
reportActionID,
introSelected: {choice: CONST.ONBOARDING_CHOICES.MANAGE_TEAM},
Expand Down Expand Up @@ -192,6 +194,149 @@ describe('actions/IOU', () => {
expect(updatedTransactionDrafts?.[`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${transactionToCategorize.transactionID}`]).toBeTruthy();
});

it('should link the track-expense action found in the passed reportActions', async () => {
// Given a selfDM report with a tracked transaction and its money request action
const selfDMReport = createRandomReport(1, CONST.REPORT.CHAT_TYPE.SELF_DM);
const transaction: Transaction = {...createRandomTransaction(1), transactionID: 'tracked-transaction'};
const trackedExpenseAction: ReportAction = {
...createRandomReportAction(101),
actionName: CONST.REPORT.ACTIONS.TYPE.IOU,
originalMessage: {
IOUReportID: selfDMReport.reportID,
IOUTransactionID: transaction.transactionID,
amount: transaction.amount,
currency: transaction.currency,
type: CONST.IOU.REPORT_ACTION_TYPE.CREATE,
},
};

await Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`, transaction);
await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${selfDMReport.reportID}`, selfDMReport);

// When the draft is created with the report actions passed as a parameter
createDraftTransactionAndNavigateToParticipantSelector({
reportID: selfDMReport.reportID,
reportActions: {[trackedExpenseAction.reportActionID]: trackedExpenseAction},
actionName: CONST.IOU.ACTION.CATEGORIZE,
reportActionID: '1',
introSelected: {choice: CONST.ONBOARDING_CHOICES.MANAGE_TEAM},
draftTransactionIDs: [],
activePolicy: undefined,
userBillingGracePeriodEnds: undefined,
amountOwed: 0,
transaction,
currentUserAccountID: RORY_ACCOUNT_ID,
currentUserEmail: RORY_EMAIL,
currentUserLocalCurrency: '',
filteredPoliciesCount: 0,
firstPolicyID: undefined,
});
await waitForBatchedUpdates();

// Then the draft should be linked to the money request action found in the passed reportActions
let drafts: OnyxCollection<Transaction>;
await getOnyxData({
key: ONYXKEYS.COLLECTION.TRANSACTION_DRAFT,
callback: (val) => {
drafts = val;
},
});
const draft = drafts?.[`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${transaction.transactionID}`];
expect(draft?.linkedTrackedExpenseReportAction?.reportActionID).toBe(trackedExpenseAction.reportActionID);
expect(draft?.linkedTrackedExpenseReportID).toBe(selfDMReport.reportID);
});

it('should read the passed reportActions rather than the report actions stored in Onyx', async () => {
// Given a selfDM report with a tracked transaction, one matching action passed as a parameter and a different matching action stored in Onyx
const selfDMReport = createRandomReport(1, CONST.REPORT.CHAT_TYPE.SELF_DM);
const transaction: Transaction = {...createRandomTransaction(1), transactionID: 'tracked-transaction'};
const originalMessage = {
IOUReportID: selfDMReport.reportID,
IOUTransactionID: transaction.transactionID,
amount: transaction.amount,
currency: transaction.currency,
type: CONST.IOU.REPORT_ACTION_TYPE.CREATE,
};
const passedAction: ReportAction = {...createRandomReportAction(102), actionName: CONST.REPORT.ACTIONS.TYPE.IOU, originalMessage};
const onyxOnlyAction: ReportAction = {...createRandomReportAction(103), actionName: CONST.REPORT.ACTIONS.TYPE.IOU, originalMessage};

await Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`, transaction);
await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${selfDMReport.reportID}`, selfDMReport);
await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${selfDMReport.reportID}`, {[onyxOnlyAction.reportActionID]: onyxOnlyAction});

// When the draft is created with only the passed action
createDraftTransactionAndNavigateToParticipantSelector({
reportID: selfDMReport.reportID,
reportActions: {[passedAction.reportActionID]: passedAction},
actionName: CONST.IOU.ACTION.CATEGORIZE,
reportActionID: '1',
introSelected: {choice: CONST.ONBOARDING_CHOICES.MANAGE_TEAM},
draftTransactionIDs: [],
activePolicy: undefined,
userBillingGracePeriodEnds: undefined,
amountOwed: 0,
transaction,
currentUserAccountID: RORY_ACCOUNT_ID,
currentUserEmail: RORY_EMAIL,
currentUserLocalCurrency: '',
filteredPoliciesCount: 0,
firstPolicyID: undefined,
});
await waitForBatchedUpdates();

// Then the draft should be linked to the passed action, proving the Onyx-stored actions are not read
let drafts: OnyxCollection<Transaction>;
await getOnyxData({
key: ONYXKEYS.COLLECTION.TRANSACTION_DRAFT,
callback: (val) => {
drafts = val;
},
});
const draft = drafts?.[`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${transaction.transactionID}`];
expect(draft?.linkedTrackedExpenseReportAction?.reportActionID).toBe(passedAction.reportActionID);
});

it('should not link any track-expense action when reportActions is undefined', async () => {
// Given a selfDM report with a tracked transaction and no reportActions passed
const selfDMReport = createRandomReport(1, CONST.REPORT.CHAT_TYPE.SELF_DM);
const transaction: Transaction = {...createRandomTransaction(1), transactionID: 'tracked-transaction'};

await Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`, transaction);
await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${selfDMReport.reportID}`, selfDMReport);

// When the draft is created without reportActions
createDraftTransactionAndNavigateToParticipantSelector({
reportID: selfDMReport.reportID,
reportActions: undefined,
actionName: CONST.IOU.ACTION.CATEGORIZE,
reportActionID: '1',
introSelected: {choice: CONST.ONBOARDING_CHOICES.MANAGE_TEAM},
draftTransactionIDs: [],
activePolicy: undefined,
userBillingGracePeriodEnds: undefined,
amountOwed: 0,
transaction,
currentUserAccountID: RORY_ACCOUNT_ID,
currentUserEmail: RORY_EMAIL,
currentUserLocalCurrency: '',
filteredPoliciesCount: 0,
firstPolicyID: undefined,
});
await waitForBatchedUpdates();

// Then the draft should still be created, with no linked track-expense action
let drafts: OnyxCollection<Transaction>;
await getOnyxData({
key: ONYXKEYS.COLLECTION.TRANSACTION_DRAFT,
callback: (val) => {
drafts = val;
},
});
const draft = drafts?.[`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${transaction.transactionID}`];
expect(draft).toBeTruthy();
expect(draft?.linkedTrackedExpenseReportAction).toBeFalsy();
});

it('should create a draft transaction with correct data when categorizing', async () => {
// Given a selfDM report and a transaction with specific data
const selfDMReport = createRandomReport(1, CONST.REPORT.CHAT_TYPE.SELF_DM);
Expand All @@ -211,6 +356,7 @@ describe('actions/IOU', () => {
// When createDraftTransactionAndNavigateToParticipantSelector is called with empty allTransactionDrafts
createDraftTransactionAndNavigateToParticipantSelector({
reportID: selfDMReport.reportID,
reportActions: undefined,
actionName: CONST.IOU.ACTION.CATEGORIZE,
reportActionID,
introSelected: {choice: CONST.ONBOARDING_CHOICES.MANAGE_TEAM},
Expand Down Expand Up @@ -252,6 +398,7 @@ describe('actions/IOU', () => {
// When createDraftTransactionAndNavigateToParticipantSelector is called with undefined transaction
createDraftTransactionAndNavigateToParticipantSelector({
reportID: selfDMReport.reportID,
reportActions: undefined,
actionName: CONST.IOU.ACTION.CATEGORIZE,
reportActionID: 'some-report-action-id',
introSelected: {choice: CONST.ONBOARDING_CHOICES.MANAGE_TEAM},
Expand Down Expand Up @@ -288,6 +435,7 @@ describe('actions/IOU', () => {
// When createDraftTransactionAndNavigateToParticipantSelector is called with undefined reportID
createDraftTransactionAndNavigateToParticipantSelector({
reportID: undefined,
reportActions: undefined,
actionName: CONST.IOU.ACTION.CATEGORIZE,
reportActionID: 'some-report-action-id',
introSelected: {choice: CONST.ONBOARDING_CHOICES.MANAGE_TEAM},
Expand Down Expand Up @@ -366,6 +514,7 @@ describe('actions/IOU', () => {
// When the expense is submitted to the employer, skipping the destination picker
createDraftTransactionAndNavigateToParticipantSelector({
reportID: selfDMReport.reportID,
reportActions: undefined,
actionName: CONST.IOU.ACTION.SUBMIT,
reportActionID: '1',
introSelected: {choice: CONST.ONBOARDING_CHOICES.MANAGE_TEAM},
Expand Down Expand Up @@ -402,6 +551,7 @@ describe('actions/IOU', () => {
// When the expense is submitted to the employer and there is no workspace to submit to
createDraftTransactionAndNavigateToParticipantSelector({
reportID: selfDMReport.reportID,
reportActions: undefined,
actionName: CONST.IOU.ACTION.SUBMIT,
reportActionID: '1',
introSelected: {choice: CONST.ONBOARDING_CHOICES.MANAGE_TEAM},
Expand Down Expand Up @@ -432,6 +582,7 @@ describe('actions/IOU', () => {
// When the expense is submitted to the employer and there is no workspace to submit to
createDraftTransactionAndNavigateToParticipantSelector({
reportID: selfDMReport.reportID,
reportActions: undefined,
actionName: CONST.IOU.ACTION.SUBMIT,
reportActionID: '1',
introSelected: {choice: CONST.ONBOARDING_CHOICES.MANAGE_TEAM},
Expand Down Expand Up @@ -461,6 +612,7 @@ describe('actions/IOU', () => {
// When the expense is submitted to the employer
createDraftTransactionAndNavigateToParticipantSelector({
reportID: selfDMReport.reportID,
reportActions: undefined,
actionName: CONST.IOU.ACTION.SUBMIT,
reportActionID: '1',
introSelected: {choice: CONST.ONBOARDING_CHOICES.MANAGE_TEAM},
Expand Down
2 changes: 2 additions & 0 deletions tests/actions/IOUTest/TrackExpenseTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@ describe('actions/IOU/TrackExpense', () => {
const reportActionableTrackExpense = Object.values(selfDMReportActions ?? {}).find((reportAction) => isActionableTrackExpense(reportAction));
createDraftTransactionAndNavigateToParticipantSelector({
reportID: selfDMReport.reportID,
reportActions: selfDMReportActions,
actionName: CONST.IOU.ACTION.CATEGORIZE,
reportActionID: reportActionableTrackExpense?.reportActionID,
introSelected: {choice: CONST.ONBOARDING_CHOICES.MANAGE_TEAM},
Expand Down Expand Up @@ -1451,6 +1452,7 @@ describe('actions/IOU/TrackExpense', () => {
// When a draft is created for categorization
createDraftTransactionAndNavigateToParticipantSelector({
reportID: selfDMReport.reportID,
reportActions: selfDMReportActions,
actionName: CONST.IOU.ACTION.CATEGORIZE,
reportActionID: actionableWhisper?.reportActionID,
introSelected: {choice: CONST.ONBOARDING_CHOICES.MANAGE_TEAM},
Expand Down
Loading
Loading