diff --git a/src/components/AttachmentModal.js b/src/components/AttachmentModal.js index d24d1e18907f..e5b605dd3ff2 100755 --- a/src/components/AttachmentModal.js +++ b/src/components/AttachmentModal.js @@ -19,7 +19,6 @@ import fileDownload from '@libs/fileDownload'; import * as FileUtils from '@libs/fileDownload/FileUtils'; import Navigation from '@libs/Navigation/Navigation'; import * as ReportActionsUtils from '@libs/ReportActionsUtils'; -import * as ReportUtils from '@libs/ReportUtils'; import * as TransactionUtils from '@libs/TransactionUtils'; import useNativeDriver from '@libs/useNativeDriver'; import reportPropTypes from '@pages/reportPropTypes'; @@ -95,6 +94,9 @@ const propTypes = { /** Whether it is a receipt attachment or not */ isReceiptAttachment: PropTypes.bool, + + /** Whether the receipt can be replaced */ + canEditReceipt: PropTypes.bool, }; const defaultProps = { @@ -113,6 +115,7 @@ const defaultProps = { onCarouselAttachmentChange: () => {}, isWorkspaceAvatar: false, isReceiptAttachment: false, + canEditReceipt: false, }; function AttachmentModal(props) { @@ -372,13 +375,9 @@ function AttachmentModal(props) { if (!props.isReceiptAttachment || !props.parentReport || !props.parentReportActions) { return []; } - const menuItems = []; - const parentReportAction = props.parentReportActions[props.report.parentReportActionID]; - const canEdit = - ReportUtils.canEditFieldOfMoneyRequest(parentReportAction, props.parentReport.reportID, CONST.EDIT_REQUEST_FIELD.RECEIPT, props.transaction) && - !TransactionUtils.isDistanceRequest(props.transaction); - if (canEdit) { + const menuItems = []; + if (props.canEditReceipt) { menuItems.push({ icon: Expensicons.Camera, text: props.translate('common.replace'), @@ -393,7 +392,7 @@ function AttachmentModal(props) { text: props.translate('common.download'), onSelected: () => downloadAttachment(source), }); - if (TransactionUtils.hasReceipt(props.transaction) && !TransactionUtils.isReceiptBeingScanned(props.transaction) && canEdit) { + if (TransactionUtils.hasReceipt(props.transaction) && !TransactionUtils.isReceiptBeingScanned(props.transaction) && props.canEditReceipt) { menuItems.push({ icon: Expensicons.Trashcan, text: props.translate('receipt.deleteReceipt'), diff --git a/src/components/MoneyReportHeader.js b/src/components/MoneyReportHeader.js index 8ed6d0746438..5b59fca6cdae 100644 --- a/src/components/MoneyReportHeader.js +++ b/src/components/MoneyReportHeader.js @@ -87,9 +87,9 @@ function MoneyReportHeader({session, personalDetails, policy, chatReport, nextSt const isSettled = ReportUtils.isSettled(moneyRequestReport.reportID); const policyType = lodashGet(policy, 'type'); const isPolicyAdmin = policyType !== CONST.POLICY.TYPE.PERSONAL && lodashGet(policy, 'role') === CONST.POLICY.ROLE.ADMIN; - const isGroupPolicy = _.contains([CONST.POLICY.TYPE.CORPORATE, CONST.POLICY.TYPE.TEAM], policyType); + const isPaidGroupPolicy = ReportUtils.isPaidGroupPolicy(moneyRequestReport); const isManager = ReportUtils.isMoneyRequestReport(moneyRequestReport) && lodashGet(session, 'accountID', null) === moneyRequestReport.managerID; - const isPayer = isGroupPolicy + const isPayer = isPaidGroupPolicy ? // In a group policy, the admin approver can pay the report directly by skipping the approval step isPolicyAdmin && (isApproved || isManager) : isPolicyAdmin || (ReportUtils.isMoneyRequestReport(moneyRequestReport) && isManager); @@ -99,11 +99,11 @@ function MoneyReportHeader({session, personalDetails, policy, chatReport, nextSt [isPayer, isDraft, isSettled, moneyRequestReport, reimbursableTotal, chatReport], ); const shouldShowApproveButton = useMemo(() => { - if (!isGroupPolicy) { + if (!isPaidGroupPolicy) { return false; } return isManager && !isDraft && !isApproved && !isSettled; - }, [isGroupPolicy, isManager, isDraft, isApproved, isSettled]); + }, [isPaidGroupPolicy, isManager, isDraft, isApproved, isSettled]); const shouldShowSettlementButton = shouldShowPayButton || shouldShowApproveButton; const shouldShowSubmitButton = isDraft && reimbursableTotal !== 0; const isFromPaidPolicy = policyType === CONST.POLICY.TYPE.TEAM || policyType === CONST.POLICY.TYPE.CORPORATE; diff --git a/src/components/ReportActionItem/MoneyRequestPreview.js b/src/components/ReportActionItem/MoneyRequestPreview.js index d425e236431b..c052a885245f 100644 --- a/src/components/ReportActionItem/MoneyRequestPreview.js +++ b/src/components/ReportActionItem/MoneyRequestPreview.js @@ -210,7 +210,7 @@ function MoneyRequestPreview(props) { } let message = translate('iou.cash'); - if (ReportUtils.isGroupPolicyExpenseReport(props.iouReport) && ReportUtils.isReportApproved(props.iouReport) && !ReportUtils.isSettled(props.iouReport)) { + if (ReportUtils.isPaidGroupPolicyExpenseReport(props.iouReport) && ReportUtils.isReportApproved(props.iouReport) && !ReportUtils.isSettled(props.iouReport)) { message += ` • ${translate('iou.approved')}`; } else if (props.iouReport.isWaitingOnBankAccount) { message += ` • ${translate('iou.pending')}`; diff --git a/src/components/ReportActionItem/MoneyRequestView.js b/src/components/ReportActionItem/MoneyRequestView.js index 3437058efa45..37ff163f23c8 100644 --- a/src/components/ReportActionItem/MoneyRequestView.js +++ b/src/components/ReportActionItem/MoneyRequestView.js @@ -1,7 +1,7 @@ import lodashGet from 'lodash/get'; import lodashValues from 'lodash/values'; import PropTypes from 'prop-types'; -import React, {useCallback, useMemo} from 'react'; +import React, {useCallback} from 'react'; import {View} from 'react-native'; import {withOnyx} from 'react-native-onyx'; import categoryPropTypes from '@components/categoryPropTypes'; @@ -37,6 +37,7 @@ import AnimatedEmptyStateBackground from '@pages/home/report/AnimatedEmptyStateB import reportActionPropTypes from '@pages/home/report/reportActionPropTypes'; import iouReportPropTypes from '@pages/iouReportPropTypes'; import reportPropTypes from '@pages/reportPropTypes'; +import {policyDefaultProps, policyPropTypes} from '@pages/workspace/withPolicy'; import * as IOU from '@userActions/IOU'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; @@ -83,6 +84,9 @@ const propTypes = { /** The actions from the parent report */ parentReportActions: PropTypes.objectOf(PropTypes.shape(reportActionPropTypes)), + /** The policy the report is tied to */ + ...policyPropTypes, + /** Collection of categories attached to a policy */ policyCategories: PropTypes.objectOf(categoryPropTypes), @@ -101,14 +105,15 @@ const propTypes = { const defaultProps = { parentReport: {}, parentReportActions: {}, - policyCategories: {}, transaction: { amount: 0, currency: CONST.CURRENCY.USD, comment: {comment: ''}, }, transactionViolations: [], + policyCategories: {}, policyTags: {}, + ...policyDefaultProps, }; function MoneyRequestView({report, parentReport, parentReportActions, policyCategories, shouldShowHorizontalRule, transaction, policyTags, policy, transactionViolations}) { @@ -147,12 +152,18 @@ function MoneyRequestView({report, parentReport, parentReportActions, policyCate // Flags for allowing or disallowing editing a money request const isSettled = ReportUtils.isSettled(moneyRequestReport.reportID); const isCancelled = moneyRequestReport && moneyRequestReport.isCancelledIOU; + + // Used for non-restricted fields such as: description, category, tag, billable, etc. const canEdit = ReportUtils.canEditMoneyRequest(parentReportAction); - const canEditAmount = ReportUtils.canEditMoneyRequest(parentReportAction, CONST.EDIT_REQUEST_FIELD.AMOUNT, transaction) && !isSettled && !isCardTransaction; - const canEditReceipt = ReportUtils.canEditFieldOfMoneyRequest(parentReportAction, moneyRequestReport.reportID, CONST.EDIT_REQUEST_FIELD.RECEIPT); + const canEditAmount = ReportUtils.canEditFieldOfMoneyRequest(parentReportAction, CONST.EDIT_REQUEST_FIELD.AMOUNT); + const canEditMerchant = ReportUtils.canEditFieldOfMoneyRequest(parentReportAction, CONST.EDIT_REQUEST_FIELD.MERCHANT); + const canEditDate = ReportUtils.canEditFieldOfMoneyRequest(parentReportAction, CONST.EDIT_REQUEST_FIELD.DATE); + const canEditReceipt = ReportUtils.canEditFieldOfMoneyRequest(parentReportAction, CONST.EDIT_REQUEST_FIELD.RECEIPT); + const canEditDistance = ReportUtils.canEditFieldOfMoneyRequest(parentReportAction, CONST.EDIT_REQUEST_FIELD.DISTANCE); // A flag for verifying that the current report is a sub-report of a workspace chat - const isPolicyExpenseChat = useMemo(() => ReportUtils.isPolicyExpenseChat(ReportUtils.getRootParentReport(report)), [report]); + // if the policy of the report is either Collect or Control, then this report must be tied to workspace chat + const isPolicyExpenseChat = ReportUtils.isGroupPolicy(report); // Fetches only the first tag, for now const policyTag = PolicyUtils.getTag(policyTags); @@ -193,18 +204,12 @@ function MoneyRequestView({report, parentReport, parentReportActions, policyCate } } - // A temporary solution to hide the transaction detail - // This will be removed after we properly add the transaction as a prop - if (ReportActionsUtils.isDeletedAction(parentReportAction)) { - return null; - } - const hasReceipt = TransactionUtils.hasReceipt(transaction); let receiptURIs; let hasErrors = false; if (hasReceipt) { receiptURIs = ReceiptUtils.getThumbnailAndImageURIs(transaction); - hasErrors = canEdit && TransactionUtils.hasMissingSmartscanFields(transaction); + hasErrors = canEditReceipt && TransactionUtils.hasMissingSmartscanFields(transaction); } const pendingAction = lodashGet(transaction, 'pendingAction'); @@ -223,11 +228,12 @@ function MoneyRequestView({report, parentReport, parentReportActions, policyCate isLocalFile={receiptURIs.isLocalFile} transaction={transaction} enablePreviewModal + canEditReceipt={canEditReceipt} /> )} - {!hasReceipt && canEditReceipt && !isSettled && canUseViolations && ( + {!hasReceipt && canEditReceipt && canUseViolations && ( Navigation.navigate(ROUTES.EDIT_REQUEST.getRoute(report.reportID, CONST.EDIT_REQUEST_FIELD.RECEIPT))} @@ -269,8 +275,8 @@ function MoneyRequestView({report, parentReport, parentReportActions, policyCate Navigation.navigate(ROUTES.EDIT_REQUEST.getRoute(report.reportID, CONST.EDIT_REQUEST_FIELD.DISTANCE))} /> @@ -280,8 +286,8 @@ function MoneyRequestView({report, parentReport, parentReportActions, policyCate Navigation.navigate(ROUTES.EDIT_REQUEST.getRoute(report.reportID, CONST.EDIT_REQUEST_FIELD.MERCHANT))} brickRoadIndicator={hasViolations('merchant') || (hasErrors && isEmptyMerchant) ? CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR : ''} @@ -294,8 +300,8 @@ function MoneyRequestView({report, parentReport, parentReportActions, policyCate Navigation.navigate(ROUTES.EDIT_REQUEST.getRoute(report.reportID, CONST.EDIT_REQUEST_FIELD.DATE))} brickRoadIndicator={hasViolations('date') || (hasErrors && transactionDate === '') ? CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR : ''} diff --git a/src/components/ReportActionItem/ReportActionItemImage.js b/src/components/ReportActionItem/ReportActionItemImage.js index 4336a5eddd8a..1495dcbd9111 100644 --- a/src/components/ReportActionItem/ReportActionItemImage.js +++ b/src/components/ReportActionItem/ReportActionItemImage.js @@ -31,6 +31,9 @@ const propTypes = { /** whether thumbnail is refer the local file or not */ isLocalFile: PropTypes.bool, + + /** whether the receipt can be replaced */ + canEditReceipt: PropTypes.bool, }; const defaultProps = { @@ -38,6 +41,7 @@ const defaultProps = { transaction: {}, enablePreviewModal: false, isLocalFile: false, + canEditReceipt: false, }; /** @@ -46,7 +50,7 @@ const defaultProps = { * and optional preview modal as well. */ -function ReportActionItemImage({thumbnail, image, enablePreviewModal, transaction, isLocalFile}) { +function ReportActionItemImage({thumbnail, image, enablePreviewModal, transaction, canEditReceipt, isLocalFile}) { const styles = useThemeStyles(); const {translate} = useLocalize(); const imageSource = tryResolveUrlFromApiRoot(image || ''); @@ -88,6 +92,7 @@ function ReportActionItemImage({thumbnail, image, enablePreviewModal, transactio isAuthTokenRequired={!isLocalFile} report={report} isReceiptAttachment + canEditReceipt={canEditReceipt} allowToDownload originalFileName={transaction.filename} > diff --git a/src/components/ReportActionItem/ReportPreview.js b/src/components/ReportActionItem/ReportPreview.js index e88c057a615d..27447a10a32b 100644 --- a/src/components/ReportActionItem/ReportPreview.js +++ b/src/components/ReportActionItem/ReportPreview.js @@ -243,10 +243,10 @@ function ReportPreview(props) { // eslint-disable-next-line react-hooks/exhaustive-deps }, []); - const isGroupPolicy = ReportUtils.isGroupPolicyExpenseChat(props.chatReport); + const isPaidGroupPolicy = ReportUtils.isPaidGroupPolicyExpenseChat(props.chatReport); const isPolicyAdmin = policyType !== CONST.POLICY.TYPE.PERSONAL && lodashGet(props.policy, 'role') === CONST.POLICY.ROLE.ADMIN; - const isPayer = isGroupPolicy - ? // In a group policy, the admin approver can pay the report directly by skipping the approval step + const isPayer = isPaidGroupPolicy + ? // In a paid group policy, the admin approver can pay the report directly by skipping the approval step isPolicyAdmin && (isApproved || isCurrentUserManager) : isPolicyAdmin || (isMoneyRequestReport && isCurrentUserManager); const shouldShowPayButton = useMemo( @@ -254,11 +254,11 @@ function ReportPreview(props) { [isPayer, isDraftExpenseReport, iouSettled, reimbursableSpend, iouCanceled, props.iouReport], ); const shouldShowApproveButton = useMemo(() => { - if (!isGroupPolicy) { + if (!isPaidGroupPolicy) { return false; } return isCurrentUserManager && !isDraftExpenseReport && !isApproved && !iouSettled; - }, [isGroupPolicy, isCurrentUserManager, isDraftExpenseReport, isApproved, iouSettled]); + }, [isPaidGroupPolicy, isCurrentUserManager, isDraftExpenseReport, isApproved, iouSettled]); const shouldShowSettlementButton = shouldShowPayButton || shouldShowApproveButton; return ( diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 848dfecfe4bf..baf8c460fd2a 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -543,7 +543,8 @@ function isExpenseReport(report: OnyxEntry | EmptyObject): boolean { /** * Checks if a report is an IOU report. */ -function isIOUReport(report: OnyxEntry): boolean { +function isIOUReport(reportOrID: OnyxEntry | string | EmptyObject): boolean { + const report = typeof reportOrID === 'string' ? allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportOrID}`] ?? null : reportOrID; return report?.type === CONST.REPORT.TYPE.IOU; } @@ -599,14 +600,15 @@ function isReportManager(report: OnyxEntry): boolean { /** * Checks if the supplied report has been approved */ -function isReportApproved(report: OnyxEntry | EmptyObject): boolean { +function isReportApproved(reportOrID: OnyxEntry | string | EmptyObject): boolean { + const report = typeof reportOrID === 'string' ? allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportOrID}`] ?? null : reportOrID; return report?.stateNum === CONST.REPORT.STATE_NUM.SUBMITTED && report?.statusNum === CONST.REPORT.STATUS.APPROVED; } /** * Checks if the supplied report is an expense report in Open state and status. */ -function isDraftExpenseReport(report: OnyxEntry): boolean { +function isDraftExpenseReport(report: OnyxEntry | EmptyObject): boolean { return isExpenseReport(report) && report?.stateNum === CONST.REPORT.STATE_NUM.OPEN && report?.statusNum === CONST.REPORT.STATUS.OPEN; } @@ -713,9 +715,17 @@ function isControlPolicyExpenseChat(report: OnyxEntry): boolean { } /** - * Whether the provided report belongs to a Control or Collect policy + * Whether the provided report belongs to a Free, Collect or Control policy */ function isGroupPolicy(report: OnyxEntry): boolean { + const policyType = getPolicyType(report, allPolicies); + return policyType === CONST.POLICY.TYPE.CORPORATE || policyType === CONST.POLICY.TYPE.TEAM || policyType === CONST.POLICY.TYPE.FREE; +} + +/** + * Whether the provided report belongs to a Control or Collect policy + */ +function isPaidGroupPolicy(report: OnyxEntry): boolean { const policyType = getPolicyType(report, allPolicies); return policyType === CONST.POLICY.TYPE.CORPORATE || policyType === CONST.POLICY.TYPE.TEAM; } @@ -723,8 +733,8 @@ function isGroupPolicy(report: OnyxEntry): boolean { /** * Whether the provided report belongs to a Control or Collect policy and is an expense chat */ -function isGroupPolicyExpenseChat(report: OnyxEntry): boolean { - return isPolicyExpenseChat(report) && isGroupPolicy(report); +function isPaidGroupPolicyExpenseChat(report: OnyxEntry): boolean { + return isPolicyExpenseChat(report) && isPaidGroupPolicy(report); } /** @@ -737,8 +747,8 @@ function isControlPolicyExpenseReport(report: OnyxEntry): boolean { /** * Whether the provided report belongs to a Control or Collect policy and is an expense report */ -function isGroupPolicyExpenseReport(report: OnyxEntry): boolean { - return isExpenseReport(report) && isGroupPolicy(report); +function isPaidGroupPolicyExpenseReport(report: OnyxEntry): boolean { + return isExpenseReport(report) && isPaidGroupPolicy(report); } /** @@ -816,7 +826,7 @@ function isConciergeChatReport(report: OnyxEntry): boolean { /** * Returns true if report is still being processed */ -function isProcessingReport(report: OnyxEntry): boolean { +function isProcessingReport(report: OnyxEntry | EmptyObject): boolean { return report?.stateNum === CONST.REPORT.STATE_NUM.PROCESSING && report?.statusNum === CONST.REPORT.STATUS.SUBMITTED; } @@ -1847,9 +1857,13 @@ function getTransactionDetails(transaction: OnyxEntry, createdDateF * - the current user is the requestor and is not settled yet * - in case of expense report * - the current user is the requestor and is not settled yet - * - or the user is an admin on the policy the expense report is tied to + * - the current user is the manager of the report + * - or the current user is an admin on the policy the expense report is tied to + * + * This is used in conjunction with canEditRestrictedField to control editing of specific fields like amount, currency, created, receipt, and distance. + * On its own, it only controls allowing/disallowing navigating to the editing pages or showing/hiding the 'Edit' icon on report actions */ -function canEditMoneyRequest(reportAction: OnyxEntry, fieldToEdit = '', transaction?: OnyxEntry): boolean { +function canEditMoneyRequest(reportAction: OnyxEntry): boolean { const isDeleted = ReportActionsUtils.isDeletedAction(reportAction); if (isDeleted) { @@ -1872,55 +1886,76 @@ function canEditMoneyRequest(reportAction: OnyxEntry, fieldToEdit } const moneyRequestReport = getReport(String(moneyRequestReportID)); - const isReportSettled = isSettled(moneyRequestReport?.reportID); - const isApproved = isReportApproved(moneyRequestReport); - const isAdmin = isExpenseReport(moneyRequestReport) && (getPolicy(moneyRequestReport?.policyID ?? '')?.role ?? '') === CONST.POLICY.ROLE.ADMIN; const isRequestor = currentUserAccountID === reportAction?.actorAccountID; - const isDistanceRequest = !isEmpty(transaction) && TransactionUtils.isDistanceRequest(transaction); - if (isAdmin && !isRequestor && fieldToEdit === CONST.EDIT_REQUEST_FIELD.RECEIPT) { - return false; - } - if (isDistanceRequest && fieldToEdit === CONST.EDIT_REQUEST_FIELD.AMOUNT) { - return isAdmin; + if (isIOUReport(moneyRequestReport)) { + return isProcessingReport(moneyRequestReport) && isRequestor; } - if (isAdmin) { + + const policy = getPolicy(moneyRequestReport?.policyID ?? ''); + const isAdmin = policy.role === CONST.POLICY.ROLE.ADMIN; + const isManager = currentUserAccountID === moneyRequestReport?.managerID; + + // Admin & managers can always edit coding fields such as tag, category, billable, etc. As long as the report has a state higher than OPEN. + if ((isAdmin || isManager) && !isDraftExpenseReport(moneyRequestReport)) { return true; } - return !isApproved && !isReportSettled && isRequestor; + return !isReportApproved(moneyRequestReport) && !isSettled(moneyRequestReport?.reportID) && isRequestor; } /** * Checks if the current user can edit the provided property of a money request * */ -function canEditFieldOfMoneyRequest( - reportAction: OnyxEntry, - reportID: string, - fieldToEdit: ValueOf, - transaction: OnyxEntry, -): boolean { +function canEditFieldOfMoneyRequest(reportAction: OnyxEntry, fieldToEdit: ValueOf): boolean { // A list of fields that cannot be edited by anyone, once a money request has been settled - const nonEditableFieldsWhenSettled: string[] = [ + const restrictedFields: string[] = [ CONST.EDIT_REQUEST_FIELD.AMOUNT, CONST.EDIT_REQUEST_FIELD.CURRENCY, + CONST.EDIT_REQUEST_FIELD.MERCHANT, CONST.EDIT_REQUEST_FIELD.DATE, CONST.EDIT_REQUEST_FIELD.RECEIPT, CONST.EDIT_REQUEST_FIELD.DISTANCE, ]; - // Checks if this user has permissions to edit this money request - if (!canEditMoneyRequest(reportAction, fieldToEdit, transaction)) { - return false; // User doesn't have permission to edit + if (!canEditMoneyRequest(reportAction)) { + return false; + } + + // If we're editing fields such as category, tag, description, etc. the check above should be enough for handling the permission + if (!restrictedFields.includes(fieldToEdit)) { + return true; } - if (!isEmpty(transaction) && fieldToEdit === CONST.EDIT_REQUEST_FIELD.RECEIPT && TransactionUtils.hasReceipt(transaction) && TransactionUtils.isReceiptBeingScanned(transaction)) { + + const iouMessage = reportAction?.originalMessage as IOUMessage; + const moneyRequestReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${iouMessage?.IOUReportID}`] ?? ({} as Report); + const transaction = allTransactions?.[`${ONYXKEYS.COLLECTION.TRANSACTION}${iouMessage?.IOUTransactionID}`] ?? ({} as Transaction); + + if (isSettled(String(moneyRequestReport.reportID)) || isReportApproved(String(moneyRequestReport.reportID))) { return false; } - // Checks if the report is settled - // Checks if the provided property is a restricted one - return !isSettled(reportID) || !nonEditableFieldsWhenSettled.includes(fieldToEdit); + if (fieldToEdit === CONST.EDIT_REQUEST_FIELD.AMOUNT || fieldToEdit === CONST.EDIT_REQUEST_FIELD.CURRENCY) { + if (TransactionUtils.isCardTransaction(transaction)) { + return false; + } + + if (TransactionUtils.isDistanceRequest(transaction)) { + const policy = getPolicy(moneyRequestReport?.reportID ?? ''); + const isAdmin = isExpenseReport(moneyRequestReport) && policy.role === CONST.POLICY.ROLE.ADMIN; + const isManager = isExpenseReport(moneyRequestReport) && currentUserAccountID === moneyRequestReport?.managerID; + + return isAdmin || isManager; + } + } + + if (fieldToEdit === CONST.EDIT_REQUEST_FIELD.RECEIPT) { + const isRequestor = currentUserAccountID === reportAction?.actorAccountID; + return !TransactionUtils.isReceiptBeingScanned(transaction) && !TransactionUtils.isDistanceRequest(transaction) && isRequestor; + } + + return true; } /** @@ -2060,7 +2095,7 @@ function getReportPreviewMessage( const formattedAmount = CurrencyUtils.convertToDisplayString(totalAmount, report.currency); - if (isReportApproved(report) && isGroupPolicy(report)) { + if (isReportApproved(report) && isPaidGroupPolicy(report)) { return Localize.translateLocal('iou.managerApprovedAmount', { manager: payerName ?? '', amount: formattedAmount, @@ -4312,10 +4347,12 @@ export { formatReportLastMessageText, chatIncludesConcierge, isPolicyExpenseChat, + isGroupPolicy, + isPaidGroupPolicy, isControlPolicyExpenseChat, isControlPolicyExpenseReport, - isGroupPolicyExpenseChat, - isGroupPolicyExpenseReport, + isPaidGroupPolicyExpenseChat, + isPaidGroupPolicyExpenseReport, getIconsForParticipants, getIcons, getRoomWelcomeMessage, diff --git a/src/pages/EditRequestPage.js b/src/pages/EditRequestPage.js index 54c5202fb205..bc05c110ab2f 100644 --- a/src/pages/EditRequestPage.js +++ b/src/pages/EditRequestPage.js @@ -1,7 +1,7 @@ import lodashGet from 'lodash/get'; import lodashValues from 'lodash/values'; import PropTypes from 'prop-types'; -import React, {useCallback, useEffect, useMemo} from 'react'; +import React, {useCallback, useEffect} from 'react'; import {withOnyx} from 'react-native-onyx'; import FullPageNotFoundView from '@components/BlockingViews/FullPageNotFoundView'; import categoryPropTypes from '@components/categoryPropTypes'; @@ -47,9 +47,6 @@ const propTypes = { /** The report object for the thread report */ report: reportPropTypes, - /** The parent report object for the thread report */ - parentReport: reportPropTypes, - /** Collection of categories attached to a policy */ policyCategories: PropTypes.objectOf(categoryPropTypes), @@ -65,14 +62,13 @@ const propTypes = { const defaultProps = { report: {}, - parentReport: {}, policyCategories: {}, policyTags: {}, parentReportActions: {}, transaction: {}, }; -function EditRequestPage({report, route, parentReport, policyCategories, policyTags, parentReportActions, transaction}) { +function EditRequestPage({report, route, policyCategories, policyTags, parentReportActions, transaction}) { const parentReportActionID = lodashGet(report, 'parentReportActionID', '0'); const parentReportAction = lodashGet(parentReportActions, parentReportActionID, {}); const { @@ -93,7 +89,7 @@ function EditRequestPage({report, route, parentReport, policyCategories, policyT const tagListName = PolicyUtils.getTagListName(policyTags); // A flag for verifying that the current report is a sub-report of a workspace chat - const isPolicyExpenseChat = useMemo(() => ReportUtils.isPolicyExpenseChat(ReportUtils.getRootParentReport(report)), [report]); + const isPolicyExpenseChat = ReportUtils.isGroupPolicy(report); // A flag for showing the categories page const shouldShowCategories = isPolicyExpenseChat && (transactionCategory || OptionsListUtils.hasEnabledOptions(lodashValues(policyCategories))); @@ -104,7 +100,7 @@ function EditRequestPage({report, route, parentReport, policyCategories, policyT // Decides whether to allow or disallow editing a money request useEffect(() => { // Do not dismiss the modal, when a current user can edit this property of the money request. - if (ReportUtils.canEditFieldOfMoneyRequest(parentReportAction, parentReport.reportID, fieldToEdit, transaction)) { + if (ReportUtils.canEditFieldOfMoneyRequest(parentReportAction, fieldToEdit)) { return; } @@ -112,7 +108,7 @@ function EditRequestPage({report, route, parentReport, policyCategories, policyT Navigation.isNavigationReady().then(() => { Navigation.dismissModal(); }); - }, [parentReportAction, parentReport.reportID, fieldToEdit, transaction]); + }, [parentReportAction, fieldToEdit]); // Update the transaction object and close the modal function editMoneyRequest(transactionChanges) { @@ -303,9 +299,6 @@ export default compose( policyTags: { key: ({report}) => `${ONYXKEYS.COLLECTION.POLICY_TAGS}${report ? report.policyID : '0'}`, }, - parentReport: { - key: ({report}) => `${ONYXKEYS.COLLECTION.REPORT}${report ? report.parentReportID : '0'}`, - }, parentReportActions: { key: ({report}) => `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report ? report.parentReportID : '0'}`, canEvict: false, diff --git a/src/pages/iou/IOUCurrencySelection.js b/src/pages/iou/IOUCurrencySelection.js index eab9ab5a7510..24833fc96fdc 100644 --- a/src/pages/iou/IOUCurrencySelection.js +++ b/src/pages/iou/IOUCurrencySelection.js @@ -86,7 +86,7 @@ function IOUCurrencySelection(props) { const parentReportAction = ReportActionsUtils.getReportAction(report.parentReportID, report.parentReportActionID); // Do not dismiss the modal, when a current user can edit this currency of this money request. - if (ReportUtils.canEditFieldOfMoneyRequest(parentReportAction, report.parentReportID, CONST.EDIT_REQUEST_FIELD.CURRENCY)) { + if (ReportUtils.canEditFieldOfMoneyRequest(parentReportAction, CONST.EDIT_REQUEST_FIELD.CURRENCY)) { return; }