Skip to content

fix: pay button appears in invoice preview for sender #61659

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 21, 2025
Merged
Show file tree
Hide file tree
Changes from 5 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
Original file line number Diff line number Diff line change
Expand Up @@ -458,8 +458,8 @@ function MoneyRequestReportPreviewContent({
if (isPaidAnimationRunning) {
return CONST.REPORT.REPORT_PREVIEW_ACTIONS.PAY;
}
return getReportPreviewAction(violations, iouReport, policy, transactions, isIouReportArchived, reportActions);
}, [isPaidAnimationRunning, violations, iouReport, policy, transactions, isIouReportArchived, reportActions]);
return getReportPreviewAction(violations, iouReport, policy, transactions, isIouReportArchived, reportActions, invoiceReceiverPolicy);
}, [isPaidAnimationRunning, violations, iouReport, policy, transactions, isIouReportArchived, reportActions, invoiceReceiverPolicy]);

const reportPreviewActions = {
[CONST.REPORT.REPORT_PREVIEW_ACTIONS.SUBMIT]: (
Expand Down
4 changes: 2 additions & 2 deletions src/components/ReportActionItem/ReportPreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -514,8 +514,8 @@ function ReportPreview({
if (isPaidAnimationRunning) {
return CONST.REPORT.REPORT_PREVIEW_ACTIONS.PAY;
}
return getReportPreviewAction(violations, iouReport, policy, transactions, isIouReportArchived);
}, [isPaidAnimationRunning, violations, iouReport, policy, transactions, isIouReportArchived]);
return getReportPreviewAction(violations, iouReport, policy, transactions, isIouReportArchived, undefined, invoiceReceiverPolicy);
}, [isPaidAnimationRunning, violations, iouReport, policy, transactions, isIouReportArchived, invoiceReceiverPolicy]);

const reportPreviewActions = {
[CONST.REPORT.REPORT_PREVIEW_ACTIONS.SUBMIT]: (
Expand Down
7 changes: 4 additions & 3 deletions src/libs/ReportPreviewActionUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ function canApprove(report: Report, violations: OnyxCollection<TransactionViolat
return isExpense && isApprover && isProcessing && isApprovalEnabled && !hasAnyViolations && reportTransactions.length > 0 && isCurrentUserManager;
}

function canPay(report: Report, violations: OnyxCollection<TransactionViolation[]>, policy?: Policy, isReportArchived = false) {
function canPay(report: Report, violations: OnyxCollection<TransactionViolation[]>, policy?: Policy, isReportArchived = false, invoiceReceiverPolicy?: Policy) {
if (isReportArchived) {
return false;
}
Expand Down Expand Up @@ -130,7 +130,7 @@ function canPay(report: Report, violations: OnyxCollection<TransactionViolation[
return parentReport?.invoiceReceiver?.accountID === getCurrentUserAccountID();
}

return policy?.role === CONST.POLICY.ROLE.ADMIN;
return invoiceReceiverPolicy?.role === CONST.POLICY.ROLE.ADMIN;
}

function canExport(report: Report, violations: OnyxCollection<TransactionViolation[]>, policy?: Policy, reportActions?: OnyxEntry<ReportActions> | ReportAction[]) {
Expand Down Expand Up @@ -200,6 +200,7 @@ function getReportPreviewAction(
transactions?: Transaction[],
isReportArchived = false,
reportActions?: OnyxEntry<ReportActions> | ReportAction[],
invoiceReceiverPolicy?: Policy,
): ValueOf<typeof CONST.REPORT.REPORT_PREVIEW_ACTIONS> {
if (!report) {
return CONST.REPORT.REPORT_PREVIEW_ACTIONS.VIEW;
Expand All @@ -210,7 +211,7 @@ function getReportPreviewAction(
if (canApprove(report, violations, policy, transactions)) {
return CONST.REPORT.REPORT_PREVIEW_ACTIONS.APPROVE;
}
if (canPay(report, violations, policy, isReportArchived)) {
if (canPay(report, violations, policy, isReportArchived, invoiceReceiverPolicy)) {
return CONST.REPORT.REPORT_PREVIEW_ACTIONS.PAY;
}
if (canExport(report, violations, policy, reportActions)) {
Expand Down
11 changes: 8 additions & 3 deletions tests/actions/ReportPreviewActionUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,13 @@ describe('getReportPreviewAction', () => {
policy.type = CONST.POLICY.TYPE.CORPORATE;
policy.reimbursementChoice = CONST.POLICY.REIMBURSEMENT_CHOICES.REIMBURSEMENT_NO;

const invoiceReceiverPolicy = createRandomPolicy(0);
invoiceReceiverPolicy.role = CONST.POLICY.ROLE.ADMIN;

await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, report);

const {result: isReportArchived} = renderHook(() => useReportIsArchived(report?.parentReportID));
expect(getReportPreviewAction(VIOLATIONS, report, policy, undefined, isReportArchived.current)).toBe(CONST.REPORT.REPORT_PREVIEW_ACTIONS.PAY);
expect(getReportPreviewAction(VIOLATIONS, report, policy, undefined, isReportArchived.current, undefined, invoiceReceiverPolicy)).toBe(CONST.REPORT.REPORT_PREVIEW_ACTIONS.PAY);
});

it('canPay should return false for archived invoice', async () => {
Expand All @@ -156,15 +159,17 @@ describe('getReportPreviewAction', () => {
policy.type = CONST.POLICY.TYPE.CORPORATE;
policy.reimbursementChoice = CONST.POLICY.REIMBURSEMENT_CHOICES.REIMBURSEMENT_NO;

const invoiceReceiverPolicy = createRandomPolicy(0);
invoiceReceiverPolicy.role = CONST.POLICY.ROLE.ADMIN;

await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, report);

// This is what indicates that a report is archived (see ReportUtils.isArchivedReport())
await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${report.reportID}`, {
private_isArchived: new Date().toString(),
});

const {result: isReportArchived} = renderHook(() => useReportIsArchived(report?.parentReportID));
expect(getReportPreviewAction(VIOLATIONS, report, policy, undefined, isReportArchived.current)).toBe(CONST.REPORT.REPORT_PREVIEW_ACTIONS.PAY);
expect(getReportPreviewAction(VIOLATIONS, report, policy, undefined, isReportArchived.current, undefined, invoiceReceiverPolicy)).toBe(CONST.REPORT.REPORT_PREVIEW_ACTIONS.PAY);
});

it('canExport should return true for finished reports', async () => {
Expand Down