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
28 changes: 20 additions & 8 deletions src/libs/ReportPreviewActionUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
isExported as isExportedUtil,
isInvoiceReport,
isIOUReport,
isOpenExpenseReport,
isOpenReport,
isPayer,
isProcessingReport,
Expand Down Expand Up @@ -62,7 +63,7 @@ function canSubmit(report: Report, violations: OnyxCollection<TransactionViolati
return isExpense && (isSubmitter || isManager || isAdmin) && isOpen && isManualSubmitEnabled && !hasAnyViolations && !isAnyReceiptBeingScanned;
}

function canApprove(report: Report, violations: OnyxCollection<TransactionViolation[]>, policy?: Policy, transactions?: Transaction[]) {
function canApprove(report: Report, violations: OnyxCollection<TransactionViolation[]>, policy?: Policy, transactions?: Transaction[], shouldConsiderViolations = true) {
const currentUserID = getCurrentUserAccountID();
const isExpense = isExpenseReport(report);
const isApprover = isApproverMember(policy, currentUserID);
Expand All @@ -84,10 +85,17 @@ function canApprove(report: Report, violations: OnyxCollection<TransactionViolat
return false;
}

return isExpense && isApprover && isProcessing && isApprovalEnabled && !hasAnyViolations && reportTransactions.length > 0 && isCurrentUserManager;
return isExpense && isApprover && isProcessing && !!isApprovalEnabled && (!hasAnyViolations || !shouldConsiderViolations) && reportTransactions.length > 0 && isCurrentUserManager;
}

function canPay(report: Report, violations: OnyxCollection<TransactionViolation[]>, policy?: Policy, isReportArchived = false, invoiceReceiverPolicy?: Policy) {
function canPay(
report: Report,
violations: OnyxCollection<TransactionViolation[]>,
policy?: Policy,
isReportArchived = false,
invoiceReceiverPolicy?: Policy,
shouldConsiderViolations = true,
) {
if (isReportArchived) {
return false;
}
Expand All @@ -107,7 +115,7 @@ function canPay(report: Report, violations: OnyxCollection<TransactionViolation[
const hasAnyViolations =
hasViolations(report.reportID, violations) || hasNoticeTypeViolations(report.reportID, violations, true) || hasWarningTypeViolations(report.reportID, violations, true);

if (isExpense && isReportPayer && isPaymentsEnabled && isReportFinished && !hasAnyViolations && reimbursableSpend > 0) {
if (isExpense && isReportPayer && isPaymentsEnabled && isReportFinished && (!hasAnyViolations || !shouldConsiderViolations) && reimbursableSpend > 0) {
return true;
}

Expand Down Expand Up @@ -167,17 +175,21 @@ function canExport(report: Report, violations: OnyxCollection<TransactionViolati
return (isApproved || isReimbursed || isClosed) && !hasAnyViolations;
}

function canReview(report: Report, violations: OnyxCollection<TransactionViolation[]>, policy?: Policy, transactions?: Transaction[]) {
function canReview(report: Report, violations: OnyxCollection<TransactionViolation[]>, policy?: Policy, transactions?: Transaction[], isReportArchived = false) {
const hasAnyViolations =
hasMissingSmartscanFields(report.reportID, transactions) ||
hasViolations(report.reportID, violations) ||
hasNoticeTypeViolations(report.reportID, violations, true) ||
hasWarningTypeViolations(report.reportID, violations, true);
const isSubmitter = isCurrentUserSubmitter(report.reportID);
const isOpen = isOpenExpenseReport(report);
const isReimbursed = isSettled(report);
const isApprover = isApproverMember(policy, getCurrentUserAccountID());

if (!hasAnyViolations || !(isSubmitter || isApprover) || isReimbursed) {
if (
!hasAnyViolations ||
isReimbursed ||
(!(isSubmitter && isOpen) && !canApprove(report, violations, policy, transactions, false) && !canPay(report, violations, policy, isReportArchived, policy, false))
) {
return false;
}

Expand Down Expand Up @@ -225,7 +237,7 @@ function getReportPreviewAction(
if (canExport(report, violations, policy, reportActions)) {
return CONST.REPORT.REPORT_PREVIEW_ACTIONS.EXPORT_TO_ACCOUNTING;
}
if (canReview(report, violations, policy, transactions)) {
if (canReview(report, violations, policy, transactions, isReportArchived)) {
return CONST.REPORT.REPORT_PREVIEW_ACTIONS.REVIEW;
}

Expand Down
10 changes: 8 additions & 2 deletions tests/actions/ReportPreviewActionUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,11 @@ describe('getReportPreviewAction', () => {

it('canReview should return true for reports where there are violations, user is submitter or approver and Workflows are enabled', async () => {
(ReportUtils.hasViolations as jest.Mock).mockReturnValue(true);
const report = {
const report: Report = {
...createRandomReport(REPORT_ID),
statusNum: 0,
stateNum: 0,
type: CONST.REPORT.TYPE.EXPENSE,
ownerAccountID: CURRENT_USER_ACCOUNT_ID,
isWaitingOnBankAccount: false,
};
Expand Down Expand Up @@ -261,8 +264,11 @@ describe('getReportPreviewAction', () => {

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 = {
const report: Report = {
...createRandomReport(REPORT_ID),
statusNum: 0,
stateNum: 0,
type: CONST.REPORT.TYPE.EXPENSE,
ownerAccountID: CURRENT_USER_ACCOUNT_ID,
isWaitingOnBankAccount: false,
};
Expand Down