Skip to content

Commit 964115d

Browse files
committed
Add getReportTransactions function
1 parent 93834a3 commit 964115d

File tree

2 files changed

+26
-36
lines changed

2 files changed

+26
-36
lines changed

src/libs/ReportUtils.ts

Lines changed: 21 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,14 @@ function getReportOrDraftReport(reportID: string | undefined): OnyxEntry<Report>
776776
return allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`] ?? allReportsDraft?.[`${ONYXKEYS.COLLECTION.REPORT_DRAFT}${reportID}`];
777777
}
778778

779+
function getReportTransactions(reportID: string | undefined): Transaction[] {
780+
if (!reportID) {
781+
return [];
782+
}
783+
784+
return reportsTransactions[reportID] ?? [];
785+
}
786+
779787
/**
780788
* Check if a report is a draft report
781789
*/
@@ -1647,11 +1655,7 @@ function isPolicyAdmin(policyID: string | undefined, policies: OnyxCollection<Po
16471655
* Checks whether all the transactions linked to the IOU report are of the Distance Request type with pending routes
16481656
*/
16491657
function hasOnlyTransactionsWithPendingRoutes(iouReportID: string | undefined): boolean {
1650-
if (!iouReportID) {
1651-
return false;
1652-
}
1653-
1654-
const transactions = reportsTransactions[iouReportID] ?? [];
1658+
const transactions = getReportTransactions(iouReportID);
16551659

16561660
// Early return false in case not having any transaction
16571661
if (!transactions || transactions.length === 0) {
@@ -1735,11 +1739,7 @@ function isMoneyRequestReport(reportOrID: OnyxInputOrEntry<Report> | SearchRepor
17351739
* Checks if a report contains only Non-Reimbursable transactions
17361740
*/
17371741
function hasOnlyNonReimbursableTransactions(iouReportID: string | undefined): boolean {
1738-
if (!iouReportID) {
1739-
return false;
1740-
}
1741-
1742-
const transactions = reportsTransactions[iouReportID] ?? [];
1742+
const transactions = getReportTransactions(iouReportID);
17431743
if (!transactions || transactions.length === 0) {
17441744
return false;
17451745
}
@@ -2906,11 +2906,7 @@ function requiresAttentionFromCurrentUser(optionOrReport: OnyxEntry<Report> | Op
29062906
* Checks if the report contains at least one Non-Reimbursable transaction
29072907
*/
29082908
function hasNonReimbursableTransactions(iouReportID: string | undefined): boolean {
2909-
if (!iouReportID) {
2910-
return false;
2911-
}
2912-
2913-
const transactions = reportsTransactions[iouReportID] ?? [];
2909+
const transactions = getReportTransactions(iouReportID);
29142910
return transactions.filter((transaction) => transaction.reimbursable === false).length > 0;
29152911
}
29162912

@@ -3441,10 +3437,7 @@ const changeMoneyRequestHoldStatus = (reportAction: OnyxEntry<ReportAction>, bac
34413437
* Gets all transactions on an IOU report with a receipt
34423438
*/
34433439
function getTransactionsWithReceipts(iouReportID: string | undefined): Transaction[] {
3444-
if (!iouReportID) {
3445-
return [];
3446-
}
3447-
const transactions = reportsTransactions[iouReportID] ?? [];
3440+
const transactions = getReportTransactions(iouReportID);
34483441
return transactions.filter((transaction) => TransactionUtils.hasReceipt(transaction));
34493442
}
34503443

@@ -3484,7 +3477,7 @@ function getLinkedTransaction(reportAction: OnyxEntry<ReportAction | OptimisticI
34843477
* Check if any of the transactions in the report has required missing fields
34853478
*/
34863479
function hasMissingSmartscanFields(iouReportID: string): boolean {
3487-
const reportTransactions = reportsTransactions[iouReportID] ?? [];
3480+
const reportTransactions = getReportTransactions(iouReportID);
34883481

34893482
return reportTransactions.some(TransactionUtils.hasMissingSmartscanFields);
34903483
}
@@ -6457,23 +6450,23 @@ function shouldDisplayViolationsRBRInLHN(report: OnyxEntry<Report>, transactionV
64576450
* Checks to see if a report contains a violation
64586451
*/
64596452
function hasViolations(reportID: string, transactionViolations: OnyxCollection<TransactionViolation[]>, shouldShowInReview?: boolean): boolean {
6460-
const transactions = reportsTransactions[reportID] ?? [];
6453+
const transactions = getReportTransactions(reportID);
64616454
return transactions.some((transaction) => TransactionUtils.hasViolation(transaction.transactionID, transactionViolations, shouldShowInReview));
64626455
}
64636456

64646457
/**
64656458
* Checks to see if a report contains a violation of type `warning`
64666459
*/
64676460
function hasWarningTypeViolations(reportID: string, transactionViolations: OnyxCollection<TransactionViolation[]>, shouldShowInReview?: boolean): boolean {
6468-
const transactions = reportsTransactions[reportID] ?? [];
6461+
const transactions = getReportTransactions(reportID);
64696462
return transactions.some((transaction) => TransactionUtils.hasWarningTypeViolation(transaction.transactionID, transactionViolations, shouldShowInReview));
64706463
}
64716464

64726465
/**
64736466
* Checks to see if a report contains a violation of type `notice`
64746467
*/
64756468
function hasNoticeTypeViolations(reportID: string, transactionViolations: OnyxCollection<TransactionViolation[]>, shouldShowInReview?: boolean): boolean {
6476-
const transactions = reportsTransactions[reportID] ?? [];
6469+
const transactions = getReportTransactions(reportID);
64776470
return transactions.some((transaction) => TransactionUtils.hasNoticeTypeViolation(transaction.transactionID, transactionViolations, shouldShowInReview));
64786471
}
64796472

@@ -7803,18 +7796,15 @@ function navigateToPrivateNotes(report: OnyxEntry<Report>, session: OnyxEntry<Se
78037796
* Get all held transactions of a iouReport
78047797
*/
78057798
function getAllHeldTransactions(iouReportID?: string): Transaction[] {
7806-
if (!iouReportID) {
7807-
return [];
7808-
}
7809-
const transactions = reportsTransactions[iouReportID] ?? [];
7799+
const transactions = getReportTransactions(iouReportID);
78107800
return transactions.filter((transaction) => TransactionUtils.isOnHold(transaction));
78117801
}
78127802

78137803
/**
78147804
* Check if Report has any held expenses
78157805
*/
78167806
function hasHeldExpenses(iouReportID?: string, allReportTransactions?: SearchTransaction[]): boolean {
7817-
const iouReportTransactions = iouReportID ? reportsTransactions[iouReportID] : undefined;
7807+
const iouReportTransactions = getReportTransactions(iouReportID);
78187808
const transactions = allReportTransactions ?? iouReportTransactions ?? [];
78197809
return transactions.some((transaction) => TransactionUtils.isOnHold(transaction));
78207810
}
@@ -7823,7 +7813,7 @@ function hasHeldExpenses(iouReportID?: string, allReportTransactions?: SearchTra
78237813
* Check if all expenses in the Report are on hold
78247814
*/
78257815
function hasOnlyHeldExpenses(iouReportID: string, allReportTransactions?: SearchTransaction[]): boolean {
7826-
const transactionsByIouReportID = iouReportID ? reportsTransactions[iouReportID] ?? [] : [];
7816+
const transactionsByIouReportID = getReportTransactions(iouReportID);
78277817
const reportTransactions = allReportTransactions ?? transactionsByIouReportID;
78287818
return reportTransactions.length > 0 && !reportTransactions.some((transaction) => !TransactionUtils.isOnHold(transaction));
78297819
}
@@ -7844,7 +7834,7 @@ function hasUpdatedTotal(report: OnyxInputOrEntry<Report>, policy: OnyxInputOrEn
78447834
return true;
78457835
}
78467836

7847-
const allReportTransactions = reportsTransactions[report.reportID] ?? [];
7837+
const allReportTransactions = getReportTransactions(report.reportID);
78487838

78497839
const hasPendingTransaction = allReportTransactions.some((transaction) => !!transaction.pendingAction);
78507840
const hasTransactionWithDifferentCurrency = allReportTransactions.some((transaction) => transaction.currency !== report.currency);
@@ -8121,12 +8111,7 @@ function getTripTransactions(tripRoomReportID: string | undefined, reportFieldTo
81218111
const tripTransactionReportIDs = Object.values(allReports ?? {})
81228112
.filter((report) => report && report?.[reportFieldToCompare] === tripRoomReportID)
81238113
.map((report) => report?.reportID);
8124-
return tripTransactionReportIDs.flatMap((reportID) => {
8125-
if (!reportID) {
8126-
return [];
8127-
}
8128-
return reportsTransactions[reportID] ?? [];
8129-
});
8114+
return tripTransactionReportIDs.flatMap((reportID) => getReportTransactions(reportID));
81308115
}
81318116

81328117
function getTripIDFromTransactionParentReportID(transactionParentReportID: string | undefined): string | undefined {

tests/actions/EnforceActionExportRestrictions.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ describe('ReportUtils', () => {
1919
expect(ReportUtils.getReport).toBeUndefined();
2020
});
2121

22+
it('does not export getReportTransactions', () => {
23+
// @ts-expect-error the test is asserting that it's undefined, so the TS error is normal
24+
expect(ReportUtils.getReportTransactions).toBeUndefined();
25+
});
26+
2227
it('does not export isOneTransactionReport', () => {
2328
// @ts-expect-error the test is asserting that it's undefined, so the TS error is normal
2429
expect(ReportUtils.isOneTransactionReport).toBeUndefined();

0 commit comments

Comments
 (0)