Skip to content
Merged
5 changes: 3 additions & 2 deletions src/components/ReportActionItem/ReportPreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ function ReportPreview({
const policy = usePolicy(policyID);
const [chatReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${chatReportID}`, {canBeMissing: false});
const [iouReport, transactions, violations] = useReportWithTransactionsAndViolations(iouReportID);
const [reportNameValuePairs] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${iouReport?.reportID}`, {canBeMissing: true});
const lastTransaction = transactions?.at(0);
const transactionIDList = transactions?.map((reportTransaction) => reportTransaction.transactionID) ?? [];
const [userWallet] = useOnyx(ONYXKEYS.USER_WALLET, {canBeMissing: false});
Expand Down Expand Up @@ -512,8 +513,8 @@ function ReportPreview({
if (isPaidAnimationRunning) {
return CONST.REPORT.REPORT_PREVIEW_ACTIONS.PAY;
}
return getReportPreviewAction(violations, iouReport, policy, transactions);
}, [isPaidAnimationRunning, violations, iouReport, policy, transactions]);
return getReportPreviewAction(violations, iouReport, policy, transactions, reportNameValuePairs);
}, [isPaidAnimationRunning, violations, iouReport, policy, transactions, reportNameValuePairs]);

const reportPreviewActions = {
[CONST.REPORT.REPORT_PREVIEW_ACTIONS.SUBMIT]: (
Expand Down
23 changes: 17 additions & 6 deletions src/libs/ReportPreviewActionUtils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type {OnyxCollection} from 'react-native-onyx';
import type {ValueOf} from 'type-fest';
import CONST from '@src/CONST';
import type {Policy, Report, Transaction, TransactionViolation} from '@src/types/onyx';
import type {Policy, Report, ReportNameValuePairs, Transaction, TransactionViolation} from '@src/types/onyx';
import {isApprover as isApproverMember} from './actions/Policy/Member';
import {getCurrentUserAccountID} from './actions/Report';
import {
Expand All @@ -11,12 +11,12 @@ import {
getSubmitToAccountID,
hasAccountingConnections,
hasIntegrationAutoSync,
isPolicyAdmin,
isPrefferedExporter,
} from './PolicyUtils';
import {
getMoneyRequestSpendBreakdown,
getParentReport,
getReportNameValuePairs,
getReportTransactions,
hasMissingSmartscanFields,
hasNoticeTypeViolations,
Expand All @@ -32,10 +32,11 @@ import {
isPayer,
isProcessingReport,
isReportApproved,
isReportManuallyReimbursed,
isSettled,
} from './ReportUtils';
import {getSession} from './SessionUtils';
import {isReceiptBeingScanned} from './TransactionUtils';
import {allHavePendingRTERViolation, isReceiptBeingScanned, shouldShowBrokenConnectionViolationForMultipleTransactions} from './TransactionUtils';

function canSubmit(report: Report, violations: OnyxCollection<TransactionViolation[]>, policy?: Policy, transactions?: Transaction[]) {
const isExpense = isExpenseReport(report);
Expand Down Expand Up @@ -80,8 +81,7 @@ function canApprove(report: Report, violations: OnyxCollection<TransactionViolat
return isExpense && isApprover && isProcessing && isApprovalEnabled && !hasAnyViolations && reportTransactions.length > 0;
}

function canPay(report: Report, violations: OnyxCollection<TransactionViolation[]>, policy?: Policy) {
const reportNameValuePairs = getReportNameValuePairs(report.chatReportID);
function canPay(report: Report, violations: OnyxCollection<TransactionViolation[]>, policy?: Policy, reportNameValuePairs?: ReportNameValuePairs) {
const isChatReportArchived = isArchivedReport(reportNameValuePairs);

if (isChatReportArchived) {
Expand Down Expand Up @@ -168,6 +168,16 @@ function canReview(report: Report, violations: OnyxCollection<TransactionViolati
return false;
}

// We handle RTER violations independently because those are not configured via policy workflows
const isAdmin = isPolicyAdmin(policy);
const transactionIDs = transactions?.map((transaction) => transaction.transactionID) ?? [];
const hasAllPendingRTERViolations = allHavePendingRTERViolation(transactionIDs, violations);
const shouldShowBrokenConnectionViolation = shouldShowBrokenConnectionViolationForMultipleTransactions(transactionIDs, report, policy, violations);

if (hasAllPendingRTERViolations || (shouldShowBrokenConnectionViolation && (!isAdmin || isSubmitter) && !isReportApproved({report}) && !isReportManuallyReimbursed(report))) {
return true;
}

if (policy) {
return !!policy.areWorkflowsEnabled;
}
Expand All @@ -180,6 +190,7 @@ function getReportPreviewAction(
report?: Report,
policy?: Policy,
transactions?: Transaction[],
reportNameValuePairs?: ReportNameValuePairs,
): ValueOf<typeof CONST.REPORT.REPORT_PREVIEW_ACTIONS> {
if (!report) {
return CONST.REPORT.REPORT_PREVIEW_ACTIONS.VIEW;
Expand All @@ -190,7 +201,7 @@ function getReportPreviewAction(
if (canApprove(report, violations, policy, transactions)) {
return CONST.REPORT.REPORT_PREVIEW_ACTIONS.APPROVE;
}
if (canPay(report, violations, policy)) {
if (canPay(report, violations, policy, reportNameValuePairs)) {
return CONST.REPORT.REPORT_PREVIEW_ACTIONS.PAY;
}
if (canExport(report, violations, policy)) {
Expand Down
9 changes: 4 additions & 5 deletions src/libs/ReportPrimaryActionUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,6 @@ function isMarkAsCashAction(report: Report, reportTransactions: Transaction[], v
const isAdmin = policy?.role === CONST.POLICY.ROLE.ADMIN;

const shouldShowBrokenConnectionViolation = shouldShowBrokenConnectionViolationForMultipleTransactions(transactionIDs, report, policy, violations);

const userControlsReport = isReportSubmitter || isReportApprover || isAdmin;
return userControlsReport && shouldShowBrokenConnectionViolation;
}
Expand All @@ -261,6 +260,10 @@ function getReportPrimaryAction(
return CONST.REPORT.PRIMARY_ACTIONS.ADD_EXPENSE;
}

if (isMarkAsCashAction(report, reportTransactions, violations, policy)) {
return CONST.REPORT.PRIMARY_ACTIONS.MARK_AS_CASH;
}

if (isReviewDuplicatesAction(report, reportTransactions, policy)) {
return CONST.REPORT.PRIMARY_ACTIONS.REVIEW_DUPLICATES;
}
Expand All @@ -285,10 +288,6 @@ function getReportPrimaryAction(
return CONST.REPORT.PRIMARY_ACTIONS.EXPORT_TO_ACCOUNTING;
}

if (isMarkAsCashAction(report, reportTransactions, violations, policy)) {
return CONST.REPORT.PRIMARY_ACTIONS.MARK_AS_CASH;
}

return '';
}

Expand Down
30 changes: 30 additions & 0 deletions tests/actions/ReportPreviewActionUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,34 @@ describe('getReportPreviewAction', () => {

expect(getReportPreviewAction(VIOLATIONS, report, policy)).toBe(CONST.REPORT.REPORT_PREVIEW_ACTIONS.REVIEW);
});

it('canReview should return true for reports with RTER violations regardless of workspace workflow configuration', async () => {
(ReportUtils.hasViolations as jest.Mock).mockReturnValue(true);
const report = {
...createRandomReport(REPORT_ID),
ownerAccountID: CURRENT_USER_ACCOUNT_ID,
};

const policy = createRandomPolicy(0);
policy.areWorkflowsEnabled = true;
policy.type = CONST.POLICY.TYPE.CORPORATE;

await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, report);
const REPORT_VIOLATION = {
FIELD_REQUIRED: 'fieldRequired',
} as unknown as ReportViolations;
await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT_VIOLATIONS}${REPORT_ID}`, REPORT_VIOLATION);

await Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${TRANSACTION_ID}`, [
{
name: CONST.VIOLATIONS.RTER,
data: {
pendingPattern: true,
rterType: CONST.RTER_VIOLATION_TYPES.SEVEN_DAY_HOLD,
},
} as TransactionViolation,
]);

expect(getReportPreviewAction(VIOLATIONS, report, policy)).toBe(CONST.REPORT.REPORT_PREVIEW_ACTIONS.REVIEW);
});
});
8 changes: 5 additions & 3 deletions tests/unit/ReportPrimaryActionUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,13 @@ describe('getPrimaryAction', () => {
reportID: REPORT_ID,
type: CONST.REPORT.TYPE.EXPENSE,
ownerAccountID: CURRENT_USER_ACCOUNT_ID,
stateNum: CONST.REPORT.STATE_NUM.OPEN,
statusNum: CONST.REPORT.STATUS_NUM.OPEN,
statusNum: CONST.REPORT.STATUS_NUM.CLOSED,
total: -300,
} as unknown as Report;
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, report);
const policy = {};
const policy = {
role: CONST.POLICY.ROLE.ADMIN,
};
const TRANSACTION_ID = 'TRANSACTION_ID';

const transaction = {
Expand Down