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
13 changes: 11 additions & 2 deletions src/hooks/useTransactionViolations.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {useMemo} from 'react';
import {isViolationDismissed} from '@libs/TransactionUtils';
import {isViolationDismissed, shouldShowViolation} from '@libs/TransactionUtils';
import ONYXKEYS from '@src/ONYXKEYS';
import type {TransactionViolation, TransactionViolations} from '@src/types/onyx';
import getEmptyArray from '@src/types/utils/getEmptyArray';
Expand All @@ -12,8 +12,17 @@ function useTransactionViolations(transactionID?: string): TransactionViolations
const [transactionViolations = getEmptyArray<TransactionViolation>()] = useOnyx(`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transactionID}`, {
canBeMissing: true,
});
const [iouReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${transaction?.reportID}`, {
canBeMissing: true,
});
const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${iouReport?.policyID}`, {
canBeMissing: true,
});

return useMemo(() => transactionViolations.filter((violation) => !isViolationDismissed(transaction, violation)), [transaction, transactionViolations]);
return useMemo(
() => transactionViolations.filter((violation: TransactionViolation) => !isViolationDismissed(transaction, violation) && shouldShowViolation(iouReport, policy, violation.name)),
[transaction, transactionViolations, iouReport, policy],
);
}

export default useTransactionViolations;
43 changes: 42 additions & 1 deletion src/libs/TransactionUtils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
isInstantSubmitEnabled,
isMultiLevelTags as isMultiLevelTagsPolicyUtils,
isPolicyAdmin,
isPolicyMember as isPolicyMemberPolicyUtils,
} from '@libs/PolicyUtils';
import {getOriginalMessage, getReportAction, isMoneyRequestAction} from '@libs/ReportActionsUtils';
import {
Expand All @@ -45,7 +46,19 @@
import type {IOUType} from '@src/CONST';
import IntlStore from '@src/languages/IntlStore';
import ONYXKEYS from '@src/ONYXKEYS';
import type {OnyxInputOrEntry, Policy, RecentWaypoint, Report, ReviewDuplicates, TaxRate, TaxRates, Transaction, TransactionViolation, TransactionViolations} from '@src/types/onyx';
import type {
OnyxInputOrEntry,
Policy,
RecentWaypoint,
Report,
ReviewDuplicates,
TaxRate,
TaxRates,
Transaction,
TransactionViolation,
TransactionViolations,
ViolationName,
} from '@src/types/onyx';
import type {Attendee, Participant, SplitExpense} from '@src/types/onyx/IOU';
import type {Errors, PendingAction} from '@src/types/onyx/OnyxCommon';
import type {SearchPolicy, SearchReport, SearchTransaction} from '@src/types/onyx/SearchResults';
Expand Down Expand Up @@ -86,7 +99,7 @@

let allTransactions: OnyxCollection<Transaction> = {};

Onyx.connect({

Check warning on line 102 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,
waitForCollectionCallback: true,
callback: (value) => {
Expand All @@ -98,7 +111,7 @@
});

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

Check warning on line 114 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_DRAFT,
waitForCollectionCallback: true,
callback: (value) => {
Expand All @@ -107,7 +120,7 @@
});

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

Check warning on line 123 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 @@ -116,7 +129,7 @@
});

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

Check warning on line 132 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) => (allTransactionViolations = value),
Expand All @@ -124,7 +137,7 @@

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

Check warning on line 140 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.SESSION,
callback: (val) => {
currentUserEmail = val?.email ?? '';
Expand Down Expand Up @@ -971,6 +984,33 @@
return shouldShowBrokenConnectionViolationInternal(brokenConnectionViolations, report, policy);
}

/**
* Check if the user should see the violation
*/
function shouldShowViolation(iouReport: OnyxEntry<Report>, policy: OnyxEntry<Policy>, violationName: ViolationName): boolean {
const isSubmitter = isCurrentUserSubmitter(iouReport?.reportID);
const isPolicyMember = isPolicyMemberPolicyUtils(currentUserEmail, policy?.id);
const isReportOpen = isOpenExpenseReport(iouReport);

if (violationName === CONST.VIOLATIONS.AUTO_REPORTED_REJECTED_EXPENSE) {
return isSubmitter;
}

if (violationName === CONST.VIOLATIONS.OVER_AUTO_APPROVAL_LIMIT) {
return isPolicyAdmin(policy) && !isSubmitter;
}

if (violationName === CONST.VIOLATIONS.RTER) {
return isSubmitter || isInstantSubmitEnabled(policy);
}

if (violationName === CONST.VIOLATIONS.RECEIPT_NOT_SMART_SCANNED) {
return isPolicyMember && !isSubmitter && !isReportOpen;
}

return true;
}

/**
* Check if there is pending rter violation in all transactionViolations with given transactionIDs.
*/
Expand Down Expand Up @@ -1761,6 +1801,7 @@
getTransactionPendingAction,
isTransactionPendingDelete,
createUnreportedExpenseSections,
shouldShowViolation,
isUnreportedAndHasInvalidDistanceRateTransaction,
};

Expand Down
Loading