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
34 changes: 34 additions & 0 deletions src/libs/ReportActionsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1184,6 +1184,33 @@ function isTagModificationAction(actionName: string): boolean {
);
}

/**
* Used for Send Money flow, which is a special case where we have no IOU create action and only one IOU pay action.
* In other reports, pay actions do not count as a transactions, but this is an exception to this rule.
*/
function getSendMoneyFlowOneTransactionThreadID(actions: OnyxEntry<ReportActions> | ReportAction[], chatReportID?: string) {
if (!chatReportID) {
return undefined;
}

const iouActions = Object.values(actions ?? {}).filter(isMoneyRequestAction);

// sendMoneyFlow has only one IOU action...
if (iouActions.length !== 1) {
return undefined;
}

// ...which is 'pay'...
const isFirstActionPay = getOriginalMessage(iouActions.at(0))?.type === CONST.IOU.REPORT_ACTION_TYPE.PAY;

const {type, chatType, parentReportID, parentReportActionID} = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${chatReportID}`] ?? {};
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please pass these props directly or pass the report object itself, this is not a pure function


// ...and can only be triggered on DM chats
const isDM = type === CONST.REPORT.TYPE.CHAT && !chatType && !(parentReportID && parentReportActionID);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

is there no helper method for this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

There is a simple helper isDM that does exactly this in ReportUtils but can't be imported due to circular import. Thought it would be better to just use the code directly instead of changing the structure of these files because it is rather simple check.


return isFirstActionPay && isDM ? iouActions.at(0)?.childReportID : undefined;
}

/** Whether action has no linked report by design */
const isIOUActionTypeExcludedFromFiltering = (type: OriginalMessageIOU['type'] | undefined) =>
[CONST.IOU.REPORT_ACTION_TYPE.SPLIT, CONST.IOU.REPORT_ACTION_TYPE.TRACK, CONST.IOU.REPORT_ACTION_TYPE.PAY].some((actionType) => actionType === type);
Expand Down Expand Up @@ -1236,6 +1263,12 @@ function getOneTransactionThreadReportID(
return;
}

const sendMoneyFlowID = getSendMoneyFlowOneTransactionThreadID(reportActions, report?.chatReportID);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

even here se should pass the full report


if (sendMoneyFlowID) {
return sendMoneyFlowID;
}

const iouRequestActions = [];
for (const action of reportActionsArray) {
// If the original message is a 'pay' IOU, it shouldn't be added to the transaction count.
Expand Down Expand Up @@ -2601,6 +2634,7 @@ export {
getWorkspaceDescriptionUpdatedMessage,
getWorkspaceReportFieldAddMessage,
getWorkspaceCustomUnitRateAddedMessage,
getSendMoneyFlowOneTransactionThreadID,
getWorkspaceTagUpdateMessage,
getWorkspaceReportFieldUpdateMessage,
getWorkspaceReportFieldDeleteMessage,
Expand Down
67 changes: 64 additions & 3 deletions tests/unit/ReportActionsUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import type {KeyValueMapping} from 'react-native-onyx';
import Onyx from 'react-native-onyx';
import {isExpenseReport} from '@libs/ReportUtils';
import {actionR14932 as mockIOUAction, originalMessageR14932 as mockOriginalMessage} from '../../__mocks__/reportData/actions';
import {iouReportR14932 as mockIOUReport} from '../../__mocks__/reportData/reports';
import {chatReportR14932 as mockChatReport, iouReportR14932 as mockIOUReport} from '../../__mocks__/reportData/reports';
import CONST from '../../src/CONST';
import * as ReportActionsUtils from '../../src/libs/ReportActionsUtils';
import {getOneTransactionThreadReportID, getOriginalMessage, isIOUActionMatchingTransactionList} from '../../src/libs/ReportActionsUtils';
import {getOneTransactionThreadReportID, getOriginalMessage, getSendMoneyFlowOneTransactionThreadID, isIOUActionMatchingTransactionList} from '../../src/libs/ReportActionsUtils';
import ONYXKEYS from '../../src/ONYXKEYS';
import type {Report, ReportAction} from '../../src/types/onyx';
import createRandomReport from '../utils/collections/reports';
Expand Down Expand Up @@ -856,7 +856,68 @@ describe('ReportActionsUtils', () => {
});
});

describe('getOneTransactionThreadReportID', () => {});
describe('getSendMoneyFlowOneTransactionThreadID', () => {
const mockChatReportID = 'REPORT';
const mockDMChatReportID = 'REPORT_DM';
const childReportID = 'childReport123';

const mockedReports: Record<`${typeof ONYXKEYS.COLLECTION.REPORT}${string}`, Report> = {
[`${ONYXKEYS.COLLECTION.REPORT}${mockChatReportID}`]: {...mockChatReport, reportID: mockChatReportID},
[`${ONYXKEYS.COLLECTION.REPORT}${mockDMChatReportID}`]: {
...mockChatReport,
reportID: mockDMChatReportID,
chatType: undefined,
parentReportID: undefined,
parentReportActionID: undefined,
},
};

beforeEach(async () => {
await Onyx.mergeCollection(ONYXKEYS.COLLECTION.REPORT, mockedReports);
});

const createAction = {
...mockIOUAction,
childReportID,
originalMessage: {...getOriginalMessage(mockIOUAction), type: CONST.IOU.TYPE.CREATE},
};

const nonIOUAction = {
...mockIOUAction,
childReportID,
type: CONST.REPORT.ACTIONS.TYPE.CREATED,
};

const payAction = {
...mockIOUAction,
childReportID,
originalMessage: {...getOriginalMessage(mockIOUAction), type: CONST.IOU.TYPE.PAY},
};

it('should return undefined for a single non-IOU action', () => {
expect(getSendMoneyFlowOneTransactionThreadID([nonIOUAction], mockDMChatReportID)).toBeUndefined();
});

it('should return undefined for multiple IOU actions regardless of type', () => {
expect(getSendMoneyFlowOneTransactionThreadID([payAction, payAction], mockDMChatReportID)).toBeUndefined();
});

it('should return undefined for a single IOU action that is not `Pay`', () => {
expect(getSendMoneyFlowOneTransactionThreadID([createAction], mockDMChatReportID)).toBeUndefined();
});

it('should return the appropriate childReportID for a valid single `Pay` IOU action in DM chat', () => {
expect(getSendMoneyFlowOneTransactionThreadID([payAction], mockDMChatReportID)).toEqual(childReportID);
});

it('should return undefined for a valid single `Pay` IOU action in a chat that is not DM', () => {
expect(getSendMoneyFlowOneTransactionThreadID([payAction], mockChatReportID)).toBeUndefined();
});

it('should return undefined for a valid `Pay` IOU action in DM chat that has also a create IOU action', () => {
expect(getSendMoneyFlowOneTransactionThreadID([payAction, createAction], mockDMChatReportID)).toBeUndefined();
});
});

describe('shouldShowAddMissingDetails', () => {
it('should return true if personal detail is not completed', async () => {
Expand Down