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
19 changes: 12 additions & 7 deletions src/libs/TransactionUtils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@
};

let deprecatedAllReports: OnyxCollection<Report> = {};
Onyx.connect({

Check warning on line 133 in src/libs/TransactionUtils/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.REPORT,
waitForCollectionCallback: true,
callback: (value) => {
Expand All @@ -139,7 +139,7 @@
});

let deprecatedAllTransactionViolations: OnyxCollection<TransactionViolations> = {};
Onyx.connect({

Check warning on line 142 in src/libs/TransactionUtils/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS,
waitForCollectionCallback: true,
callback: (value) => (deprecatedAllTransactionViolations = value),
Expand Down Expand Up @@ -2467,16 +2467,21 @@
}

/**
* Retrieves all “child” transactions associated with a given original transaction
* Retrieves all "child" transactions associated with a given original transaction.
* By default excludes orphaned transactions (reportID '0'). Use includeOrphaned=true for counting.
*/
function getChildTransactions(transactions: OnyxCollection<Transaction>, reports: OnyxCollection<Report>, originalTransactionID: string | undefined) {
function getChildTransactions(transactions: OnyxCollection<Transaction>, reports: OnyxCollection<Report>, originalTransactionID: string | undefined, includeOrphaned = false) {
return Object.values(transactions ?? {}).filter((currentTransaction) => {
const isSplitChild = currentTransaction?.comment?.originalTransactionID === originalTransactionID;
if (!isSplitChild || currentTransaction?.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE) {
return false;
}
const isOrphaned = currentTransaction?.reportID === CONST.REPORT.UNREPORTED_REPORT_ID;
if (isOrphaned) {
return includeOrphaned;
}
const currentReport = reports?.[`${ONYXKEYS.COLLECTION.REPORT}${currentTransaction?.reportID}`];
return (
currentTransaction?.comment?.originalTransactionID === originalTransactionID &&
!!currentReport &&
currentTransaction?.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE
);
return !!currentReport || currentTransaction?.comment?.source === CONST.IOU.TYPE.SPLIT;
});
}

Expand Down
9 changes: 6 additions & 3 deletions src/libs/actions/IOU/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,7 @@
};

let allPersonalDetails: OnyxTypes.PersonalDetailsList = {};
Onyx.connect({

Check warning on line 744 in src/libs/actions/IOU/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
callback: (value) => {
allPersonalDetails = value ?? {};
Expand Down Expand Up @@ -840,7 +840,7 @@
};

let allTransactions: NonNullable<OnyxCollection<OnyxTypes.Transaction>> = {};
Onyx.connect({

Check warning on line 843 in src/libs/actions/IOU/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.TRANSACTION,
waitForCollectionCallback: true,
callback: (value) => {
Expand All @@ -854,7 +854,7 @@
});

let allTransactionDrafts: NonNullable<OnyxCollection<OnyxTypes.Transaction>> = {};
Onyx.connect({

Check warning on line 857 in src/libs/actions/IOU/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.TRANSACTION_DRAFT,
waitForCollectionCallback: true,
callback: (value) => {
Expand All @@ -863,7 +863,7 @@
});

let allTransactionViolations: NonNullable<OnyxCollection<OnyxTypes.TransactionViolations>> = {};
Onyx.connect({

Check warning on line 866 in src/libs/actions/IOU/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS,
waitForCollectionCallback: true,
callback: (value) => {
Expand All @@ -877,7 +877,7 @@
});

let allNextSteps: NonNullable<OnyxCollection<OnyxTypes.ReportNextStepDeprecated>> = {};
Onyx.connect({

Check warning on line 880 in src/libs/actions/IOU/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.NEXT_STEP,
waitForCollectionCallback: true,
callback: (value) => {
Expand All @@ -886,7 +886,7 @@
});

let allReports: OnyxCollection<OnyxTypes.Report>;
Onyx.connect({

Check warning on line 889 in src/libs/actions/IOU/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.REPORT,
waitForCollectionCallback: true,
callback: (value) => {
Expand All @@ -895,7 +895,7 @@
});

let allReportNameValuePairs: OnyxCollection<OnyxTypes.ReportNameValuePairs>;
Onyx.connect({

Check warning on line 898 in src/libs/actions/IOU/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS,
waitForCollectionCallback: true,
callback: (value) => {
Expand All @@ -905,7 +905,7 @@

let userAccountID = -1;
let currentUserEmail = '';
Onyx.connect({

Check warning on line 908 in src/libs/actions/IOU/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.SESSION,
callback: (value) => {
currentUserEmail = value?.email ?? '';
Expand Down Expand Up @@ -13853,15 +13853,18 @@
const participants = getMoneyRequestParticipantsFromReport(expenseReport, currentUserPersonalDetails.accountID);
const splitExpenses = transactionData?.splitExpenses ?? [];

// List of all child transactions that have been created after split
const originalChildTransactions = getChildTransactions(allTransactionsList, allReportsList, originalTransactionID);
// Get all children once (including orphaned), then filter for non-orphaned
const allChildTransactions = getChildTransactions(allTransactionsList, allReportsList, originalTransactionID, true);
const originalChildTransactions = allChildTransactions.filter((tx) => tx?.reportID !== CONST.REPORT.UNREPORTED_REPORT_ID);
const processedChildTransactionIDs: string[] = [];

const splitExpensesTotal = transactionData?.splitExpensesTotal ?? 0;

const isCreationOfSplits = originalChildTransactions.length === 0;
const hasEditableSplitExpensesLeft = splitExpenses.some((expense) => (expense.statusNum ?? 0) < CONST.REPORT.STATUS_NUM.SUBMITTED);
const isReverseSplitOperation = splitExpenses.length === 1 && originalChildTransactions.length > 0 && hasEditableSplitExpensesLeft;
// Don't revert split if there are orphaned children (reportID '0') - they're still part of the split
const isReverseSplitOperation =
splitExpenses.length === 1 && originalChildTransactions.length > 0 && hasEditableSplitExpensesLeft && allChildTransactions.length === originalChildTransactions.length;

let changesInReportTotal = 0;
// Validate custom unit rate before proceeding with split
Expand Down
141 changes: 138 additions & 3 deletions tests/unit/TransactionUtilsTest.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import Onyx from 'react-native-onyx';
import type {OnyxCollection} from 'react-native-onyx';
import DateUtils from '@libs/DateUtils';
import {shouldShowBrokenConnectionViolation, shouldShowBrokenConnectionViolationForMultipleTransactions} from '@libs/TransactionUtils';
import CONST from '@src/CONST';
import IntlStore from '@src/languages/IntlStore';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Attendee} from '@src/types/onyx/IOU';
import type {CustomUnit, Rate} from '@src/types/onyx/Policy';
import type {ReportCollectionDataSet} from '@src/types/onyx/Report';
import type {TransactionCustomUnit} from '@src/types/onyx/Transaction';
import * as TransactionUtils from '../../src/libs/TransactionUtils';
import type {Policy, Report, Transaction} from '../../src/types/onyx';
Expand Down Expand Up @@ -70,12 +70,12 @@ const secondUserOpenReport = {
stateNum: CONST.REPORT.STATE_NUM.OPEN,
statusNum: CONST.REPORT.STATUS_NUM.OPEN,
};
const reportCollectionDataSet: ReportCollectionDataSet = {
const reportCollectionDataSet = {
[`${ONYXKEYS.COLLECTION.REPORT}${FAKE_OPEN_REPORT_ID}`]: openReport,
[`${ONYXKEYS.COLLECTION.REPORT}${FAKE_PROCESSING_REPORT_ID}`]: processingReport,
[`${ONYXKEYS.COLLECTION.REPORT}${FAKE_APPROVED_REPORT_ID}`]: approvedReport,
[`${ONYXKEYS.COLLECTION.REPORT}${FAKE_OPEN_REPORT_SECOND_USER_ID}`]: secondUserOpenReport,
};
} as OnyxCollection<Report>;
const defaultDistanceRatePolicyID1: Record<string, Rate> = {
customUnitRateID1: {
currency: 'USD',
Expand Down Expand Up @@ -1553,6 +1553,141 @@ describe('TransactionUtils', () => {
});
});

describe('getChildTransactions', () => {
const originalTransactionID = 'original-123';

it('should return child transactions that have a valid report', () => {
const childTransaction = generateTransaction({
transactionID: 'child-1',
reportID: FAKE_OPEN_REPORT_ID,
comment: {
originalTransactionID,
source: CONST.IOU.TYPE.SPLIT,
},
});

const transactions = {
[`${ONYXKEYS.COLLECTION.TRANSACTION}child-1`]: childTransaction,
};

const result = TransactionUtils.getChildTransactions(transactions, reportCollectionDataSet, originalTransactionID);

expect(result).toHaveLength(1);
expect(result.at(0)?.transactionID).toBe('child-1');
});

it('should return split child transactions even if their report was deleted', () => {
const childTransaction = generateTransaction({
transactionID: 'child-2',
reportID: 'deleted-report-id',
comment: {
originalTransactionID,
source: CONST.IOU.TYPE.SPLIT,
},
});

const transactions = {
[`${ONYXKEYS.COLLECTION.TRANSACTION}child-2`]: childTransaction,
};

// Report doesn't exist in reportCollectionDataSet
const result = TransactionUtils.getChildTransactions(transactions, reportCollectionDataSet, originalTransactionID);

expect(result).toHaveLength(1);
expect(result.at(0)?.transactionID).toBe('child-2');
});

it('should exclude orphaned transactions with reportID "0" from processing', () => {
const orphanedTransaction = generateTransaction({
transactionID: 'orphaned-1',
reportID: '0',
comment: {
originalTransactionID,
source: CONST.IOU.TYPE.SPLIT,
},
});

const transactions = {
[`${ONYXKEYS.COLLECTION.TRANSACTION}orphaned-1`]: orphanedTransaction,
};

// Orphaned split children should be excluded from getChildTransactions for processing
const result = TransactionUtils.getChildTransactions(transactions, reportCollectionDataSet, originalTransactionID);

expect(result).toHaveLength(0);
});

it('should exclude transactions with pendingAction DELETE', () => {
const deletingTransaction = generateTransaction({
transactionID: 'deleting-1',
reportID: FAKE_OPEN_REPORT_ID,
pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE,
comment: {
originalTransactionID,
source: CONST.IOU.TYPE.SPLIT,
},
});

const transactions = {
[`${ONYXKEYS.COLLECTION.TRANSACTION}deleting-1`]: deletingTransaction,
};

const result = TransactionUtils.getChildTransactions(transactions, reportCollectionDataSet, originalTransactionID);

expect(result).toHaveLength(0);
});

it('should only return transactions matching the originalTransactionID', () => {
const matchingChild = generateTransaction({
transactionID: 'matching-1',
reportID: FAKE_OPEN_REPORT_ID,
comment: {
originalTransactionID,
source: CONST.IOU.TYPE.SPLIT,
},
});

const nonMatchingChild = generateTransaction({
transactionID: 'non-matching-1',
reportID: FAKE_OPEN_REPORT_ID,
comment: {
originalTransactionID: 'different-original-id',
source: CONST.IOU.TYPE.SPLIT,
},
});

const transactions = {
[`${ONYXKEYS.COLLECTION.TRANSACTION}matching-1`]: matchingChild,
[`${ONYXKEYS.COLLECTION.TRANSACTION}non-matching-1`]: nonMatchingChild,
};

const result = TransactionUtils.getChildTransactions(transactions, reportCollectionDataSet, originalTransactionID);

expect(result).toHaveLength(1);
expect(result.at(0)?.transactionID).toBe('matching-1');
});

it('should include orphaned transactions when includeOrphaned=true', () => {
const orphanedTransaction = generateTransaction({
transactionID: 'orphaned-1',
reportID: '0',
comment: {
originalTransactionID,
source: CONST.IOU.TYPE.SPLIT,
},
});

const transactions = {
[`${ONYXKEYS.COLLECTION.TRANSACTION}orphaned-1`]: orphanedTransaction,
};

const result = TransactionUtils.getChildTransactions(transactions, reportCollectionDataSet, originalTransactionID, true);

expect(result).toHaveLength(1);
expect(result.at(0)?.transactionID).toBe('orphaned-1');
});
});

describe('getConvertedAmount', () => {
it('should return the absolute amount if transaction is not from expense report, tracked expense and allowNegative is false', () => {
const transaction = generateTransaction({
Expand Down
Loading