Skip to content

Commit

Permalink
Merge pull request #56403 from Krishna2323/krishna2323/issue/50113
Browse files Browse the repository at this point in the history
fix: Scan - Delete option is displayed for an admin which causes an error.
  • Loading branch information
tgolen authored Feb 6, 2025
2 parents 362c627 + ef9e74f commit f5be429
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 24 deletions.
5 changes: 4 additions & 1 deletion src/components/AttachmentModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ type AttachmentModalProps = {

canEditReceipt?: boolean;

canDeleteReceipt?: boolean;

shouldDisableSendButton?: boolean;

attachmentLink?: string;
Expand All @@ -157,6 +159,7 @@ function AttachmentModal({
children,
fallbackSource,
canEditReceipt = false,
canDeleteReceipt = false,
onModalClose = () => {},
isLoading = false,
shouldShowNotFoundPage = false,
Expand Down Expand Up @@ -447,7 +450,7 @@ function AttachmentModal({
}

const hasOnlyEReceipt = hasEReceipt(transaction) && !hasReceiptSource(transaction);
if (!hasOnlyEReceipt && hasReceipt(transaction) && !isReceiptBeingScanned(transaction) && canEditReceipt && !hasMissingSmartscanFields(transaction)) {
if (!hasOnlyEReceipt && hasReceipt(transaction) && !isReceiptBeingScanned(transaction) && canDeleteReceipt && !hasMissingSmartscanFields(transaction)) {
menuItems.push({
icon: Expensicons.Trashcan,
text: translate('receipt.deleteReceipt'),
Expand Down
11 changes: 6 additions & 5 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3546,7 +3546,7 @@ function canEditMoneyRequest(reportAction: OnyxInputOrEntry<ReportAction<typeof
* Checks if the current user can edit the provided property of an expense
*
*/
function canEditFieldOfMoneyRequest(reportAction: OnyxInputOrEntry<ReportAction>, fieldToEdit: ValueOf<typeof CONST.EDIT_REQUEST_FIELD>): boolean {
function canEditFieldOfMoneyRequest(reportAction: OnyxInputOrEntry<ReportAction>, fieldToEdit: ValueOf<typeof CONST.EDIT_REQUEST_FIELD>, isDeleteAction?: boolean): boolean {
// A list of fields that cannot be edited by anyone, once an expense has been settled
const restrictedFields: string[] = [
CONST.EDIT_REQUEST_FIELD.AMOUNT,
Expand Down Expand Up @@ -3599,13 +3599,14 @@ function canEditFieldOfMoneyRequest(reportAction: OnyxInputOrEntry<ReportAction>

if (fieldToEdit === CONST.EDIT_REQUEST_FIELD.RECEIPT) {
const isRequestor = currentUserAccountID === reportAction?.actorAccountID;
return (
!isInvoiceReport(moneyRequestReport) &&
return !isInvoiceReport(moneyRequestReport) &&
!isReceiptBeingScanned(transaction) &&
!isDistanceRequest(transaction) &&
!isPerDiemRequest(transaction) &&
(isAdmin || isManager || isRequestor)
);
(isAdmin || isManager || isRequestor) &&
isDeleteAction
? isRequestor
: true;
}

if (fieldToEdit === CONST.EDIT_REQUEST_FIELD.DISTANCE_RATE) {
Expand Down
43 changes: 25 additions & 18 deletions src/pages/TransactionReceiptPage.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import React, {useEffect} from 'react';
import {useOnyx} from 'react-native-onyx';
import AttachmentModal from '@components/AttachmentModal';
import {openReport} from '@libs/actions/Report';
import Navigation from '@libs/Navigation/Navigation';
import type {PlatformStackScreenProps} from '@libs/Navigation/PlatformStackNavigation/types';
import type {AuthScreensParamList, RootStackParamList, State} from '@libs/Navigation/types';
import * as ReceiptUtils from '@libs/ReceiptUtils';
import * as ReportActionUtils from '@libs/ReportActionsUtils';
import * as ReportUtils from '@libs/ReportUtils';
import * as TransactionUtils from '@libs/TransactionUtils';
import {getThumbnailAndImageURIs} from '@libs/ReceiptUtils';
import {getReportAction, isTrackExpenseAction as isTrackExpenseReportReportActionsUtils} from '@libs/ReportActionsUtils';
import {
canEditFieldOfMoneyRequest,
isMoneyRequestReport,
isOneTransactionThread as isOneTransactionThreadReportUtils,
isTrackExpenseReport as isTrackExpenseReportReportUtils,
} from '@libs/ReportUtils';
import {hasEReceipt, hasReceiptSource} from '@libs/TransactionUtils';
import tryResolveUrlFromApiRoot from '@libs/tryResolveUrlFromApiRoot';
import navigationRef from '@navigation/navigationRef';
import * as ReportActions from '@userActions/Report';
import CONST from '@src/CONST';
import NAVIGATORS from '@src/NAVIGATORS';
import ONYXKEYS from '@src/ONYXKEYS';
Expand All @@ -19,27 +24,28 @@ import type SCREENS from '@src/SCREENS';
type TransactionReceiptProps = PlatformStackScreenProps<AuthScreensParamList, typeof SCREENS.TRANSACTION_RECEIPT>;

function TransactionReceipt({route}: TransactionReceiptProps) {
const [report] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${route.params.reportID ?? '-1'}`);
const [transaction] = useOnyx(`${ONYXKEYS.COLLECTION.TRANSACTION}${route.params.transactionID ?? '-1'}`);
const [reportMetadata = {isLoadingInitialReportActions: true}] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_METADATA}${route.params.reportID ?? '-1'}`);
const receiptURIs = ReceiptUtils.getThumbnailAndImageURIs(transaction);
const [report] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${route.params.reportID ?? CONST.DEFAULT_NUMBER_ID}`);
const [transaction] = useOnyx(`${ONYXKEYS.COLLECTION.TRANSACTION}${route.params.transactionID ?? CONST.DEFAULT_NUMBER_ID}`);
const [reportMetadata = {isLoadingInitialReportActions: true}] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_METADATA}${route.params.reportID ?? CONST.DEFAULT_NUMBER_ID}`);
const receiptURIs = getThumbnailAndImageURIs(transaction);

const imageSource = tryResolveUrlFromApiRoot(receiptURIs.image ?? '');

const isLocalFile = receiptURIs.isLocalFile;
const readonly = route.params.readonly === 'true';
const isFromReviewDuplicates = route.params.isFromReviewDuplicates === 'true';

const parentReportAction = ReportActionUtils.getReportAction(report?.parentReportID ?? '-1', report?.parentReportActionID ?? '-1');
const canEditReceipt = ReportUtils.canEditFieldOfMoneyRequest(parentReportAction, CONST.EDIT_REQUEST_FIELD.RECEIPT);
const isEReceipt = transaction && !TransactionUtils.hasReceiptSource(transaction) && TransactionUtils.hasEReceipt(transaction);
const isTrackExpenseAction = ReportActionUtils.isTrackExpenseAction(parentReportAction);
const parentReportAction = getReportAction(report?.parentReportID, report?.parentReportActionID);
const canEditReceipt = canEditFieldOfMoneyRequest(parentReportAction, CONST.EDIT_REQUEST_FIELD.RECEIPT);
const canDeleteReceipt = canEditFieldOfMoneyRequest(parentReportAction, CONST.EDIT_REQUEST_FIELD.RECEIPT, true);
const isEReceipt = transaction && !hasReceiptSource(transaction) && hasEReceipt(transaction);
const isTrackExpenseAction = isTrackExpenseReportReportActionsUtils(parentReportAction);

useEffect(() => {
if (report && transaction) {
return;
}
ReportActions.openReport(route.params.reportID);
openReport(route.params.reportID);
// I'm disabling the warning, as it expects to use exhaustive deps, even though we want this useEffect to run only on the first render.
// eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps
}, []);
Expand All @@ -52,17 +58,17 @@ function TransactionReceipt({route}: TransactionReceiptProps) {
if (secondToLastRoute?.name === NAVIGATORS.RIGHT_MODAL_NAVIGATOR) {
Navigation.dismissModal();
} else {
const isOneTransactionThread = ReportUtils.isOneTransactionThread(report?.reportID ?? '-1', report?.parentReportID ?? '-1', parentReportAction);
const isOneTransactionThread = isOneTransactionThreadReportUtils(report?.reportID, report?.parentReportID, parentReportAction);
Navigation.dismissModal(isOneTransactionThread ? report?.parentReportID : report?.reportID);
}
};

const moneyRequestReportID = ReportUtils.isMoneyRequestReport(report) ? report?.reportID : report?.parentReportID;
const isTrackExpenseReport = ReportUtils.isTrackExpenseReport(report);
const moneyRequestReportID = isMoneyRequestReport(report) ? report?.reportID : report?.parentReportID;
const isTrackExpenseReport = isTrackExpenseReportReportUtils(report);

// eslint-disable-next-line rulesdir/no-negated-variables
const shouldShowNotFoundPage =
isTrackExpenseReport || transaction?.reportID === CONST.REPORT.SPLIT_REPORTID || isFromReviewDuplicates ? !transaction : (moneyRequestReportID ?? '-1') !== transaction?.reportID;
isTrackExpenseReport || transaction?.reportID === CONST.REPORT.SPLIT_REPORTID || isFromReviewDuplicates ? !transaction : moneyRequestReportID !== transaction?.reportID;

return (
<AttachmentModal
Expand All @@ -71,6 +77,7 @@ function TransactionReceipt({route}: TransactionReceiptProps) {
report={report}
isReceiptAttachment
canEditReceipt={canEditReceipt && !readonly}
canDeleteReceipt={canDeleteReceipt && !readonly}
allowDownload={!isEReceipt}
isTrackExpenseAction={isTrackExpenseAction}
originalFileName={receiptURIs?.filename}
Expand Down

0 comments on commit f5be429

Please sign in to comment.