From ba930e4be7da76fa339d2ca4a60e5806ad71d849 Mon Sep 17 00:00:00 2001 From: Youssef Lourayad Date: Mon, 25 Dec 2023 02:05:43 +0100 Subject: [PATCH 01/32] Add support for editing approved reports --- src/components/AttachmentModal.js | 13 ++-- .../ReportActionItem/MoneyRequestView.js | 33 ++++----- .../ReportActionItem/ReportActionItemImage.js | 7 +- src/libs/ReportUtils.ts | 74 +++++++++++++------ src/pages/EditRequestPage.js | 13 +--- 5 files changed, 81 insertions(+), 59 deletions(-) diff --git a/src/components/AttachmentModal.js b/src/components/AttachmentModal.js index 863e59aa4474..7e9e828e4780 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) { @@ -365,12 +368,8 @@ function AttachmentModal(props) { return []; } const menuItems = []; - const parentReportAction = props.parentReportActions[props.report.parentReportActionID]; - const canEdit = - ReportUtils.canEditFieldOfMoneyRequest(parentReportAction, props.parentReport.reportID, CONST.EDIT_REQUEST_FIELD.RECEIPT) && - !TransactionUtils.isDistanceRequest(props.transaction); - if (canEdit) { + if (props.canEditReceipt) { menuItems.push({ icon: Expensicons.Camera, text: props.translate('common.replace'), @@ -385,7 +384,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/ReportActionItem/MoneyRequestView.js b/src/components/ReportActionItem/MoneyRequestView.js index 47031bfc164c..1e9873e1cf5b 100644 --- a/src/components/ReportActionItem/MoneyRequestView.js +++ b/src/components/ReportActionItem/MoneyRequestView.js @@ -28,7 +28,6 @@ import Navigation from '@libs/Navigation/Navigation'; import * as OptionsListUtils from '@libs/OptionsListUtils'; import * as PolicyUtils from '@libs/PolicyUtils'; import * as ReceiptUtils from '@libs/ReceiptUtils'; -import * as ReportActionsUtils from '@libs/ReportActionsUtils'; import * as ReportUtils from '@libs/ReportUtils'; import * as TransactionUtils from '@libs/TransactionUtils'; import AnimatedEmptyStateBackground from '@pages/home/report/AnimatedEmptyStateBackground'; @@ -115,9 +114,14 @@ 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; + const canEdit = ReportUtils.canEditMoneyRequest(parentReportAction); - const canEditAmount = canEdit && !isSettled && !isCardTransaction; - const canEditReceipt = ReportUtils.canEditFieldOfMoneyRequest(parentReportAction, moneyRequestReport.reportID, CONST.EDIT_REQUEST_FIELD.RECEIPT); + const canEditAmount = useMemo(() => ReportUtils.canEditFieldOfMoneyRequest(parentReportAction, transaction, CONST.EDIT_REQUEST_FIELD.AMOUNT), [transaction, parentReportAction]); + console.log('can edit amount', canEditAmount); + const canEditMerchant = useMemo(() => ReportUtils.canEditFieldOfMoneyRequest(parentReportAction, transaction, CONST.EDIT_REQUEST_FIELD.MERCHANT), [transaction, parentReportAction]); + const canEditDate = useMemo(() => ReportUtils.canEditFieldOfMoneyRequest(parentReportAction, transaction, CONST.EDIT_REQUEST_FIELD.DATE), [transaction, parentReportAction]); + const canEditReceipt = useMemo(() => ReportUtils.canEditFieldOfMoneyRequest(parentReportAction, transaction, CONST.EDIT_REQUEST_FIELD.RECEIPT), [transaction, parentReportAction]); + const canEditDistance = useMemo(() => ReportUtils.canEditFieldOfMoneyRequest(parentReportAction, transaction, CONST.EDIT_REQUEST_FIELD.DISTANCE), [transaction, parentReportAction]); // 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]); @@ -158,18 +162,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'); @@ -188,11 +186,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))} @@ -230,8 +229,8 @@ function MoneyRequestView({report, parentReport, parentReportActions, policyCate Navigation.navigate(ROUTES.EDIT_REQUEST.getRoute(report.reportID, CONST.EDIT_REQUEST_FIELD.DISTANCE))} /> @@ -241,8 +240,8 @@ function MoneyRequestView({report, parentReport, parentReportActions, policyCate Navigation.navigate(ROUTES.EDIT_REQUEST.getRoute(report.reportID, CONST.EDIT_REQUEST_FIELD.MERCHANT))} brickRoadIndicator={hasErrors && isEmptyMerchant ? CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR : ''} @@ -254,8 +253,8 @@ function MoneyRequestView({report, parentReport, parentReportActions, policyCate Navigation.navigate(ROUTES.EDIT_REQUEST.getRoute(report.reportID, CONST.EDIT_REQUEST_FIELD.DATE))} brickRoadIndicator={hasErrors && transactionDate === '' ? CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR : ''} diff --git a/src/components/ReportActionItem/ReportActionItemImage.js b/src/components/ReportActionItem/ReportActionItemImage.js index 2c5ef22b1b8e..4ea4de6cb0ef 100644 --- a/src/components/ReportActionItem/ReportActionItemImage.js +++ b/src/components/ReportActionItem/ReportActionItemImage.js @@ -30,6 +30,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 = { @@ -37,6 +40,7 @@ const defaultProps = { transaction: {}, enablePreviewModal: false, isLocalFile: false, + canEditReceipt: false, }; /** @@ -45,7 +49,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 || ''); @@ -87,6 +91,7 @@ function ReportActionItemImage({thumbnail, image, enablePreviewModal, transactio isAuthTokenRequired={!isLocalFile} report={report} isReceiptAttachment + canEditReceipt={canEditReceipt} allowToDownload originalFileName={transaction.filename} > diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 8e0ff19bf3dc..325bc749ecb9 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -486,7 +486,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; } @@ -542,7 +543,8 @@ 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; } @@ -759,10 +761,17 @@ 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; } +/** + * Returns true if report is still open + */ +function isOpenReport(report: OnyxEntry | EmptyObject): boolean { + return report?.stateNum === CONST.REPORT.STATE_NUM.OPEN && report?.statusNum === CONST.REPORT.STATUS.OPEN; +} + /** * Check if the report is a single chat report that isn't a thread * and personal detail of participant is optimistic data @@ -1806,8 +1815,11 @@ function getTransactionDetails(transaction: OnyxEntry, createdDateF * - 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 + * + * This is used in conjuction with canEditRestrictedField to control editing of specific fields like amount, currency, created, and receipt. + * 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 = ''): boolean { +function canEditMoneyRequest(reportAction: OnyxEntry): boolean { const isDeleted = ReportActionsUtils.isDeletedAction(reportAction); if (isDeleted) { @@ -1830,44 +1842,58 @@ 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 policy = getPolicy(moneyRequestReport?.policyID ?? ''); + const isAdmin = isExpenseReport(moneyRequestReport) && policy.role === CONST.POLICY.ROLE.ADMIN; + const isManager = isExpenseReport(moneyRequestReport) && currentUserAccountID === moneyRequestReport?.managerID; const isRequestor = currentUserAccountID === reportAction?.actorAccountID; - if (isAdmin && !isRequestor && fieldToEdit === CONST.EDIT_REQUEST_FIELD.RECEIPT) { - return false; + // Admin & managers can always allow coding fields such as tag, category, billable, etc. As long as the report has a state higher than OPEN. + if ((isAdmin || isManager) && !isOpenReport(moneyRequestReport)) { + return true; } - if (isAdmin) { - return true; + // We allow editing of PROCESSING reports in Free policies, but not in paid policie (yet) + if (policy.type === CONST.POLICY.TYPE.FREE) { + return isRequestor && isProcessingReport(moneyRequestReport); } - 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): boolean { +function canEditFieldOfMoneyRequest(reportAction: OnyxEntry, transaction: Transaction, 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)) { - return false; // User doesn't have permission to edit + if (!canEditMoneyRequest(reportAction)) { + return false; + } + + if (!restrictedFields.includes(fieldToEdit)) { + return true; + } + + const reportID = reportAction?.originalMessage?.IOUReportID ?? 0; + if (isSettled(reportID) || isReportApproved(reportID)) { + return false; + } + + if (fieldToEdit === CONST.EDIT_REQUEST_FIELD.AMOUNT || fieldToEdit === CONST.EDIT_REQUEST_FIELD.CURRENCY) { + return !TransactionUtils.isCardTransaction(transaction); } - // 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.RECEIPT) { + const isRequestor = currentUserAccountID === reportAction?.actorAccountID; + return !TransactionUtils.isDistanceRequest(transaction) && isRequestor; + } + + return true; } /** diff --git a/src/pages/EditRequestPage.js b/src/pages/EditRequestPage.js index 2bdf3d19f16a..c257b21adb9e 100644 --- a/src/pages/EditRequestPage.js +++ b/src/pages/EditRequestPage.js @@ -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 { @@ -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)) { + if (ReportUtils.canEditFieldOfMoneyRequest(parentReportAction, transaction, fieldToEdit)) { return; } @@ -112,7 +108,7 @@ function EditRequestPage({report, route, parentReport, policyCategories, policyT Navigation.isNavigationReady().then(() => { Navigation.dismissModal(); }); - }, [parentReportAction, parentReport.reportID, fieldToEdit]); + }, [parentReportAction, transaction, fieldToEdit]); // Update the transaction object and close the modal function editMoneyRequest(transactionChanges) { @@ -284,9 +280,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, From bcb8dc405362a16fbdd2841033ce5ef777dd04bd Mon Sep 17 00:00:00 2001 From: Youssef Lourayad Date: Mon, 25 Dec 2023 02:57:39 +0100 Subject: [PATCH 02/32] Cleanup --- src/components/ReportActionItem/MoneyRequestView.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/components/ReportActionItem/MoneyRequestView.js b/src/components/ReportActionItem/MoneyRequestView.js index 1e9873e1cf5b..dffe6746b63d 100644 --- a/src/components/ReportActionItem/MoneyRequestView.js +++ b/src/components/ReportActionItem/MoneyRequestView.js @@ -117,7 +117,6 @@ function MoneyRequestView({report, parentReport, parentReportActions, policyCate const canEdit = ReportUtils.canEditMoneyRequest(parentReportAction); const canEditAmount = useMemo(() => ReportUtils.canEditFieldOfMoneyRequest(parentReportAction, transaction, CONST.EDIT_REQUEST_FIELD.AMOUNT), [transaction, parentReportAction]); - console.log('can edit amount', canEditAmount); const canEditMerchant = useMemo(() => ReportUtils.canEditFieldOfMoneyRequest(parentReportAction, transaction, CONST.EDIT_REQUEST_FIELD.MERCHANT), [transaction, parentReportAction]); const canEditDate = useMemo(() => ReportUtils.canEditFieldOfMoneyRequest(parentReportAction, transaction, CONST.EDIT_REQUEST_FIELD.DATE), [transaction, parentReportAction]); const canEditReceipt = useMemo(() => ReportUtils.canEditFieldOfMoneyRequest(parentReportAction, transaction, CONST.EDIT_REQUEST_FIELD.RECEIPT), [transaction, parentReportAction]); From c41b554fb17d25e5695b69db9c9553b0127a2a5a Mon Sep 17 00:00:00 2001 From: Youssef Lourayad Date: Mon, 25 Dec 2023 03:29:49 +0100 Subject: [PATCH 03/32] Fix bugs --- src/components/ReportActionItem/MoneyRequestView.js | 2 +- src/libs/ReportUtils.ts | 2 +- src/pages/EditRequestPage.js | 3 ++- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/components/ReportActionItem/MoneyRequestView.js b/src/components/ReportActionItem/MoneyRequestView.js index dffe6746b63d..dbd95c268678 100644 --- a/src/components/ReportActionItem/MoneyRequestView.js +++ b/src/components/ReportActionItem/MoneyRequestView.js @@ -123,7 +123,7 @@ function MoneyRequestView({report, parentReport, parentReportActions, policyCate const canEditDistance = useMemo(() => ReportUtils.canEditFieldOfMoneyRequest(parentReportAction, transaction, CONST.EDIT_REQUEST_FIELD.DISTANCE), [transaction, parentReportAction]); // 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 = policy && policy.type !== CONST.POLICY.TYPE.PERSONAL; // Fetches only the first tag, for now const policyTag = PolicyUtils.getTag(policyTags); diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 325bc749ecb9..a86adfb4f500 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -1880,7 +1880,7 @@ function canEditFieldOfMoneyRequest(reportAction: OnyxEntry, trans } const reportID = reportAction?.originalMessage?.IOUReportID ?? 0; - if (isSettled(reportID) || isReportApproved(reportID)) { + if (isSettled(String(reportID)) || isReportApproved(String(reportID))) { return false; } diff --git a/src/pages/EditRequestPage.js b/src/pages/EditRequestPage.js index c257b21adb9e..978ec93546de 100644 --- a/src/pages/EditRequestPage.js +++ b/src/pages/EditRequestPage.js @@ -89,7 +89,8 @@ function EditRequestPage({report, route, policyCategories, policyTags, parentRep 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 policy = ReportUtils.getPolicy(report.policyID || ''); + const isPolicyExpenseChat = policy && policy.type !== CONST.POLICY.TYPE.PERSONAL; // A flag for showing the categories page const shouldShowCategories = isPolicyExpenseChat && (transactionCategory || OptionsListUtils.hasEnabledOptions(lodashValues(policyCategories))); From 0459190cd7582cc8fc1b049877bb955b39bdcf4c Mon Sep 17 00:00:00 2001 From: Youssef Lourayad Date: Tue, 26 Dec 2023 23:36:42 +0100 Subject: [PATCH 04/32] Lint --- src/components/ReportActionItem/MoneyRequestView.js | 2 +- src/pages/EditRequestPage.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/components/ReportActionItem/MoneyRequestView.js b/src/components/ReportActionItem/MoneyRequestView.js index dbd95c268678..db1e302958d9 100644 --- a/src/components/ReportActionItem/MoneyRequestView.js +++ b/src/components/ReportActionItem/MoneyRequestView.js @@ -85,7 +85,7 @@ function MoneyRequestView({report, parentReport, parentReportActions, policyCate const {isSmallScreenWidth} = useWindowDimensions(); const {translate} = useLocalize(); const {canUseViolations} = usePermissions(); - const parentReportAction = parentReportActions[report.parentReportActionID] || {}; + const parentReportAction = useMemo(() => parentReportActions[report.parentReportActionID] || {}, [parentReportActions, report.parentReportActionID]); const moneyRequestReport = parentReport; const { created: transactionDate, diff --git a/src/pages/EditRequestPage.js b/src/pages/EditRequestPage.js index 978ec93546de..19edc74f290f 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'; From d273fc6c2d5e8e3fbc62542935416060e769dfc4 Mon Sep 17 00:00:00 2001 From: Youssef Lourayad Date: Fri, 29 Dec 2023 18:21:13 +0100 Subject: [PATCH 05/32] Fix typescript commplaining --- src/libs/ReportUtils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 69db817d44b3..79721d50df35 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -1872,7 +1872,7 @@ function canEditFieldOfMoneyRequest(reportAction: OnyxEntry, trans return true; } - const reportID = reportAction?.originalMessage?.IOUReportID ?? 0; + const reportID = (reportAction?.originalMessage as IOUMessage).IOUReportID ?? 0; if (isSettled(String(reportID)) || isReportApproved(String(reportID))) { return false; } From 7ba51461bc06946499545d6ac885e5ceaf82a8fb Mon Sep 17 00:00:00 2001 From: Youssef Lourayad Date: Fri, 29 Dec 2023 18:22:17 +0100 Subject: [PATCH 06/32] Add safety check --- src/libs/ReportUtils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 79721d50df35..5db9da334715 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -1872,7 +1872,7 @@ function canEditFieldOfMoneyRequest(reportAction: OnyxEntry, trans return true; } - const reportID = (reportAction?.originalMessage as IOUMessage).IOUReportID ?? 0; + const reportID = (reportAction?.originalMessage as IOUMessage)?.IOUReportID ?? 0; if (isSettled(String(reportID)) || isReportApproved(String(reportID))) { return false; } From d85421dafb19c116d4da1495328ab2511f022a8c Mon Sep 17 00:00:00 2001 From: Youssef Lourayad Date: Fri, 29 Dec 2023 19:02:16 +0100 Subject: [PATCH 07/32] Remove useMemo --- .../ReportActionItem/MoneyRequestView.js | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/components/ReportActionItem/MoneyRequestView.js b/src/components/ReportActionItem/MoneyRequestView.js index 5d4a127900cf..ce4b5d44a801 100644 --- a/src/components/ReportActionItem/MoneyRequestView.js +++ b/src/components/ReportActionItem/MoneyRequestView.js @@ -116,9 +116,10 @@ function MoneyRequestView({report, parentReport, parentReportActions, policyCate const isCancelled = moneyRequestReport && moneyRequestReport.isCancelledIOU; const canEdit = ReportUtils.canEditMoneyRequest(parentReportAction); - const canEditAmount = useMemo(() => ReportUtils.canEditFieldOfMoneyRequest(parentReportAction, transaction, CONST.EDIT_REQUEST_FIELD.AMOUNT), [transaction, parentReportAction]); - const canEditMerchant = useMemo(() => ReportUtils.canEditFieldOfMoneyRequest(parentReportAction, transaction, CONST.EDIT_REQUEST_FIELD.MERCHANT), [transaction, parentReportAction]); - const canEditDate = useMemo(() => ReportUtils.canEditFieldOfMoneyRequest(parentReportAction, transaction, CONST.EDIT_REQUEST_FIELD.DATE), [transaction, parentReportAction]); + + // The fields amount, currency, merchant, and date share the same logic, so we'll use Amount here for getting the permission + // and it can be used for the rest of the restricted fields + const canEditRestrictedField = ReportUtils.canEditFieldOfMoneyRequest(parentReportAction, transaction, CONST.EDIT_REQUEST_FIELD.AMOUNT); const canEditReceipt = useMemo(() => ReportUtils.canEditFieldOfMoneyRequest(parentReportAction, transaction, CONST.EDIT_REQUEST_FIELD.RECEIPT), [transaction, parentReportAction]); const canEditDistance = useMemo(() => ReportUtils.canEditFieldOfMoneyRequest(parentReportAction, transaction, CONST.EDIT_REQUEST_FIELD.DISTANCE), [transaction, parentReportAction]); @@ -203,8 +204,8 @@ function MoneyRequestView({report, parentReport, parentReportActions, policyCate titleIcon={Expensicons.Checkmark} description={amountDescription} titleStyle={styles.newKansasLarge} - interactive={canEditAmount} - shouldShowRightIcon={canEditAmount} + interactive={canEditRestrictedField} + shouldShowRightIcon={canEditRestrictedField} onPress={() => Navigation.navigate(ROUTES.EDIT_REQUEST.getRoute(report.reportID, CONST.EDIT_REQUEST_FIELD.AMOUNT))} brickRoadIndicator={hasErrors && transactionAmount === 0 ? CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR : ''} error={hasErrors && transactionAmount === 0 ? translate('common.error.enterAmount') : ''} @@ -239,8 +240,8 @@ function MoneyRequestView({report, parentReport, parentReportActions, policyCate Navigation.navigate(ROUTES.EDIT_REQUEST.getRoute(report.reportID, CONST.EDIT_REQUEST_FIELD.MERCHANT))} brickRoadIndicator={hasErrors && isEmptyMerchant ? CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR : ''} @@ -252,8 +253,8 @@ function MoneyRequestView({report, parentReport, parentReportActions, policyCate Navigation.navigate(ROUTES.EDIT_REQUEST.getRoute(report.reportID, CONST.EDIT_REQUEST_FIELD.DATE))} brickRoadIndicator={hasErrors && transactionDate === '' ? CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR : ''} From 07e2148ad8555a58fe9c87b2d00843fc019eeb7d Mon Sep 17 00:00:00 2001 From: Youssef Lourayad Date: Fri, 29 Dec 2023 19:03:30 +0100 Subject: [PATCH 08/32] Remove useMemo --- src/components/ReportActionItem/MoneyRequestView.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/components/ReportActionItem/MoneyRequestView.js b/src/components/ReportActionItem/MoneyRequestView.js index ce4b5d44a801..3410cceafa0f 100644 --- a/src/components/ReportActionItem/MoneyRequestView.js +++ b/src/components/ReportActionItem/MoneyRequestView.js @@ -117,11 +117,10 @@ function MoneyRequestView({report, parentReport, parentReportActions, policyCate const canEdit = ReportUtils.canEditMoneyRequest(parentReportAction); - // The fields amount, currency, merchant, and date share the same logic, so we'll use Amount here for getting the permission - // and it can be used for the rest of the restricted fields + // The fields amount, currency, merchant, and date share the same logic so we'll use Amount here for getting the permission and it can be used for the rest of the restricted fields const canEditRestrictedField = ReportUtils.canEditFieldOfMoneyRequest(parentReportAction, transaction, CONST.EDIT_REQUEST_FIELD.AMOUNT); - const canEditReceipt = useMemo(() => ReportUtils.canEditFieldOfMoneyRequest(parentReportAction, transaction, CONST.EDIT_REQUEST_FIELD.RECEIPT), [transaction, parentReportAction]); - const canEditDistance = useMemo(() => ReportUtils.canEditFieldOfMoneyRequest(parentReportAction, transaction, CONST.EDIT_REQUEST_FIELD.DISTANCE), [transaction, parentReportAction]); + const canEditReceipt = ReportUtils.canEditFieldOfMoneyRequest(parentReportAction, transaction, CONST.EDIT_REQUEST_FIELD.RECEIPT); + const canEditDistance = ReportUtils.canEditFieldOfMoneyRequest(parentReportAction, transaction, CONST.EDIT_REQUEST_FIELD.DISTANCE); // A flag for verifying that the current report is a sub-report of a workspace chat const isPolicyExpenseChat = policy && policy.type !== CONST.POLICY.TYPE.PERSONAL; From 9a2ef5c8dee0245143b45b217d8aa93634baed3c Mon Sep 17 00:00:00 2001 From: Youssef Lourayad Date: Sun, 31 Dec 2023 14:04:46 +0100 Subject: [PATCH 09/32] Remove unnecessary call to canEditFieldOfMoneyRequest --- src/components/ReportActionItem/MoneyRequestView.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/components/ReportActionItem/MoneyRequestView.js b/src/components/ReportActionItem/MoneyRequestView.js index 3410cceafa0f..2c5221f3268d 100644 --- a/src/components/ReportActionItem/MoneyRequestView.js +++ b/src/components/ReportActionItem/MoneyRequestView.js @@ -120,7 +120,6 @@ function MoneyRequestView({report, parentReport, parentReportActions, policyCate // The fields amount, currency, merchant, and date share the same logic so we'll use Amount here for getting the permission and it can be used for the rest of the restricted fields const canEditRestrictedField = ReportUtils.canEditFieldOfMoneyRequest(parentReportAction, transaction, CONST.EDIT_REQUEST_FIELD.AMOUNT); const canEditReceipt = ReportUtils.canEditFieldOfMoneyRequest(parentReportAction, transaction, CONST.EDIT_REQUEST_FIELD.RECEIPT); - const canEditDistance = ReportUtils.canEditFieldOfMoneyRequest(parentReportAction, transaction, CONST.EDIT_REQUEST_FIELD.DISTANCE); // A flag for verifying that the current report is a sub-report of a workspace chat const isPolicyExpenseChat = policy && policy.type !== CONST.POLICY.TYPE.PERSONAL; @@ -228,8 +227,8 @@ function MoneyRequestView({report, parentReport, parentReportActions, policyCate Navigation.navigate(ROUTES.EDIT_REQUEST.getRoute(report.reportID, CONST.EDIT_REQUEST_FIELD.DISTANCE))} /> From bdfa31931328954c2edaaca2c47ba64847573262 Mon Sep 17 00:00:00 2001 From: Youssef Lourayad Date: Sun, 31 Dec 2023 14:05:00 +0100 Subject: [PATCH 10/32] Address comments --- src/pages/EditRequestPage.js | 2 +- src/pages/iou/IOUCurrencySelection.js | 22 ++++++++++++++++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/pages/EditRequestPage.js b/src/pages/EditRequestPage.js index b7f8c7af0d1e..ce9f0e0f5830 100644 --- a/src/pages/EditRequestPage.js +++ b/src/pages/EditRequestPage.js @@ -90,7 +90,7 @@ function EditRequestPage({report, route, policyCategories, policyTags, parentRep // A flag for verifying that the current report is a sub-report of a workspace chat const policy = ReportUtils.getPolicy(report.policyID || ''); - const isPolicyExpenseChat = policy && policy.type !== CONST.POLICY.TYPE.PERSONAL; + const isPolicyExpenseChat = policy.type && policy.type !== CONST.POLICY.TYPE.PERSONAL; // A flag for showing the categories page const shouldShowCategories = isPolicyExpenseChat && (transactionCategory || OptionsListUtils.hasEnabledOptions(lodashValues(policyCategories))); diff --git a/src/pages/iou/IOUCurrencySelection.js b/src/pages/iou/IOUCurrencySelection.js index eab9ab5a7510..b3180bbd5964 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, props.transaction, CONST.EDIT_REQUEST_FIELD.CURRENCY)) { return; } @@ -94,7 +94,7 @@ function IOUCurrencySelection(props) { Navigation.isNavigationReady().then(() => { Navigation.dismissModal(); }); - }, [threadReportID]); + }, [threadReportID, props.transaction]); const confirmCurrencySelection = useCallback( (option) => { @@ -193,6 +193,24 @@ export default compose( withOnyx({ currencyList: {key: ONYXKEYS.CURRENCY_LIST}, iou: {key: ONYXKEYS.IOU}, + report: { + key: ({route}) => `${ONYXKEYS.COLLECTION.REPORT}${route.params.threadReportID}`, + }, + }), + withOnyx({ + parentReportActions: { + key: ({report}) => `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report ? report.parentReportID : '0'}`, + canEvict: false, + }, + }), + withOnyx({ + transaction: { + key: ({report, parentReportActions}) => { + const parentReportActionID = lodashGet(report, 'parentReportActionID', '0'); + const parentReportAction = lodashGet(parentReportActions, parentReportActionID); + return `${ONYXKEYS.COLLECTION.TRANSACTION}${lodashGet(parentReportAction, 'originalMessage.IOUTransactionID', 0)}`; + }, + }, }), withNetwork(), )(IOUCurrencySelection); From de8ea549fd682dbbe290a6b4e942df938a3d3588 Mon Sep 17 00:00:00 2001 From: Youssef Lourayad Date: Sun, 31 Dec 2023 14:35:59 +0100 Subject: [PATCH 11/32] Remove unneeded check --- src/libs/ReportUtils.ts | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 5db9da334715..1a39504b3b0a 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -1845,11 +1845,6 @@ function canEditMoneyRequest(reportAction: OnyxEntry): boolean { return true; } - // We allow editing of PROCESSING reports in Free policies, but not in paid policie (yet) - if (policy.type === CONST.POLICY.TYPE.FREE) { - return isRequestor && isProcessingReport(moneyRequestReport); - } - return !isReportApproved(moneyRequestReport) && !isSettled(moneyRequestReport?.reportID) && isRequestor; } From 982ba480178f3d07a1d63ed2eeee6c08fe3923c9 Mon Sep 17 00:00:00 2001 From: Youssef Lourayad Date: Sun, 31 Dec 2023 15:07:26 +0100 Subject: [PATCH 12/32] Remove code causing regression --- .../ReportActionItem/MoneyRequestView.js | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/components/ReportActionItem/MoneyRequestView.js b/src/components/ReportActionItem/MoneyRequestView.js index 2c5221f3268d..013cc7e7bf55 100644 --- a/src/components/ReportActionItem/MoneyRequestView.js +++ b/src/components/ReportActionItem/MoneyRequestView.js @@ -115,11 +115,13 @@ function MoneyRequestView({report, parentReport, parentReportActions, policyCate const isSettled = ReportUtils.isSettled(moneyRequestReport.reportID); const isCancelled = moneyRequestReport && moneyRequestReport.isCancelledIOU; + // Use for non-restricted fields: description, category, tag, billable, etc. const canEdit = ReportUtils.canEditMoneyRequest(parentReportAction); - - // The fields amount, currency, merchant, and date share the same logic so we'll use Amount here for getting the permission and it can be used for the rest of the restricted fields - const canEditRestrictedField = ReportUtils.canEditFieldOfMoneyRequest(parentReportAction, transaction, CONST.EDIT_REQUEST_FIELD.AMOUNT); + const canEditAmount = ReportUtils.canEditFieldOfMoneyRequest(parentReportAction, transaction, CONST.EDIT_REQUEST_FIELD.AMOUNT); + const canEditMerchant = ReportUtils.canEditFieldOfMoneyRequest(parentReportAction, transaction, CONST.EDIT_REQUEST_FIELD.MERCHANT); + const canEditDate = ReportUtils.canEditFieldOfMoneyRequest(parentReportAction, transaction, CONST.EDIT_REQUEST_FIELD.DATE); const canEditReceipt = ReportUtils.canEditFieldOfMoneyRequest(parentReportAction, transaction, CONST.EDIT_REQUEST_FIELD.RECEIPT); + const canEditDistance = ReportUtils.canEditFieldOfMoneyRequest(parentReportAction, transaction, CONST.EDIT_REQUEST_FIELD.DISTANCE); // A flag for verifying that the current report is a sub-report of a workspace chat const isPolicyExpenseChat = policy && policy.type !== CONST.POLICY.TYPE.PERSONAL; @@ -202,8 +204,8 @@ function MoneyRequestView({report, parentReport, parentReportActions, policyCate titleIcon={Expensicons.Checkmark} description={amountDescription} titleStyle={styles.newKansasLarge} - interactive={canEditRestrictedField} - shouldShowRightIcon={canEditRestrictedField} + interactive={canEditAmount} + shouldShowRightIcon={canEditAmount} onPress={() => Navigation.navigate(ROUTES.EDIT_REQUEST.getRoute(report.reportID, CONST.EDIT_REQUEST_FIELD.AMOUNT))} brickRoadIndicator={hasErrors && transactionAmount === 0 ? CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR : ''} error={hasErrors && transactionAmount === 0 ? translate('common.error.enterAmount') : ''} @@ -227,8 +229,8 @@ function MoneyRequestView({report, parentReport, parentReportActions, policyCate Navigation.navigate(ROUTES.EDIT_REQUEST.getRoute(report.reportID, CONST.EDIT_REQUEST_FIELD.DISTANCE))} /> @@ -238,8 +240,8 @@ function MoneyRequestView({report, parentReport, parentReportActions, policyCate Navigation.navigate(ROUTES.EDIT_REQUEST.getRoute(report.reportID, CONST.EDIT_REQUEST_FIELD.MERCHANT))} brickRoadIndicator={hasErrors && isEmptyMerchant ? CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR : ''} @@ -251,8 +253,8 @@ function MoneyRequestView({report, parentReport, parentReportActions, policyCate Navigation.navigate(ROUTES.EDIT_REQUEST.getRoute(report.reportID, CONST.EDIT_REQUEST_FIELD.DATE))} brickRoadIndicator={hasErrors && transactionDate === '' ? CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR : ''} From cfaac6f93da3a625efd8c7fc3c920da28ce76f5a Mon Sep 17 00:00:00 2001 From: Youssef Lourayad Date: Sun, 31 Dec 2023 15:09:29 +0100 Subject: [PATCH 13/32] Add comment --- src/libs/ReportUtils.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 1a39504b3b0a..2bfb310cc206 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -1863,6 +1863,7 @@ function canEditFieldOfMoneyRequest(reportAction: OnyxEntry, trans 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; } From f672b91a0ab81bc34dd41fdfdda64018ebd0be7c Mon Sep 17 00:00:00 2001 From: Youssef Lourayad Date: Mon, 1 Jan 2024 15:03:25 +0100 Subject: [PATCH 14/32] Update src/components/ReportActionItem/MoneyRequestView.js Co-authored-by: c3024 <102477862+c3024@users.noreply.github.com> --- src/components/ReportActionItem/MoneyRequestView.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/ReportActionItem/MoneyRequestView.js b/src/components/ReportActionItem/MoneyRequestView.js index 013cc7e7bf55..bae1c90a1682 100644 --- a/src/components/ReportActionItem/MoneyRequestView.js +++ b/src/components/ReportActionItem/MoneyRequestView.js @@ -124,7 +124,7 @@ function MoneyRequestView({report, parentReport, parentReportActions, policyCate const canEditDistance = ReportUtils.canEditFieldOfMoneyRequest(parentReportAction, transaction, CONST.EDIT_REQUEST_FIELD.DISTANCE); // A flag for verifying that the current report is a sub-report of a workspace chat - const isPolicyExpenseChat = policy && policy.type !== CONST.POLICY.TYPE.PERSONAL; + const isPolicyExpenseChat = policy.type && policy.type !== CONST.POLICY.TYPE.PERSONAL; // Fetches only the first tag, for now const policyTag = PolicyUtils.getTag(policyTags); From 31e6a15f1fedc071dee237cdf1683bb8d55c061c Mon Sep 17 00:00:00 2001 From: Youssef Lourayad Date: Mon, 1 Jan 2024 15:05:52 +0100 Subject: [PATCH 15/32] Update comment --- src/libs/ReportUtils.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 2bfb310cc206..890cdf2ad4d1 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -1807,7 +1807,8 @@ 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 conjuction with canEditRestrictedField to control editing of specific fields like amount, currency, created, and receipt. * On its own, it only controls allowing/disallowing navigating to the editing pages or showing/hiding the 'Edit' icon on report actions From ba8506d00b78fc88e85ba811f931ebf62b726dad Mon Sep 17 00:00:00 2001 From: Youssef Lourayad Date: Mon, 1 Jan 2024 15:22:25 +0100 Subject: [PATCH 16/32] Remove transaction dependency from canEditFieldOfMoneyRequest --- .../ReportActionItem/MoneyRequestView.js | 10 +++++----- src/libs/ReportUtils.ts | 7 ++++--- src/pages/EditRequestPage.js | 2 +- src/pages/iou/IOUCurrencySelection.js | 20 +------------------ 4 files changed, 11 insertions(+), 28 deletions(-) diff --git a/src/components/ReportActionItem/MoneyRequestView.js b/src/components/ReportActionItem/MoneyRequestView.js index bae1c90a1682..8c0dd159d3f4 100644 --- a/src/components/ReportActionItem/MoneyRequestView.js +++ b/src/components/ReportActionItem/MoneyRequestView.js @@ -117,11 +117,11 @@ function MoneyRequestView({report, parentReport, parentReportActions, policyCate // Use for non-restricted fields: description, category, tag, billable, etc. const canEdit = ReportUtils.canEditMoneyRequest(parentReportAction); - const canEditAmount = ReportUtils.canEditFieldOfMoneyRequest(parentReportAction, transaction, CONST.EDIT_REQUEST_FIELD.AMOUNT); - const canEditMerchant = ReportUtils.canEditFieldOfMoneyRequest(parentReportAction, transaction, CONST.EDIT_REQUEST_FIELD.MERCHANT); - const canEditDate = ReportUtils.canEditFieldOfMoneyRequest(parentReportAction, transaction, CONST.EDIT_REQUEST_FIELD.DATE); - const canEditReceipt = ReportUtils.canEditFieldOfMoneyRequest(parentReportAction, transaction, CONST.EDIT_REQUEST_FIELD.RECEIPT); - const canEditDistance = ReportUtils.canEditFieldOfMoneyRequest(parentReportAction, transaction, CONST.EDIT_REQUEST_FIELD.DISTANCE); + 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 = policy.type && policy.type !== CONST.POLICY.TYPE.PERSONAL; diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 890cdf2ad4d1..33a5f2f5bb1e 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -1849,7 +1849,7 @@ function canEditMoneyRequest(reportAction: OnyxEntry): boolean { return !isReportApproved(moneyRequestReport) && !isSettled(moneyRequestReport?.reportID) && isRequestor; } -function canEditFieldOfMoneyRequest(reportAction: OnyxEntry, transaction: Transaction, fieldToEdit: ValueOf): 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 restrictedFields: string[] = [ CONST.EDIT_REQUEST_FIELD.AMOUNT, @@ -1869,11 +1869,12 @@ function canEditFieldOfMoneyRequest(reportAction: OnyxEntry, trans return true; } - const reportID = (reportAction?.originalMessage as IOUMessage)?.IOUReportID ?? 0; - if (isSettled(String(reportID)) || isReportApproved(String(reportID))) { + const iouMessage: IOUMessage = reportAction?.originalMessage; + if (isSettled(String(iouMessage.IOUReportID)) || isReportApproved(String(iouMessage.IOUReportID))) { return false; } + const transaction = allTransactions?.[`${ONYXKEYS.COLLECTION.TRANSACTION}${iouMessage?.IOUTransactionID}`] ?? {}; if (fieldToEdit === CONST.EDIT_REQUEST_FIELD.AMOUNT || fieldToEdit === CONST.EDIT_REQUEST_FIELD.CURRENCY) { return !TransactionUtils.isCardTransaction(transaction); } diff --git a/src/pages/EditRequestPage.js b/src/pages/EditRequestPage.js index ce9f0e0f5830..7cc1bde4e91f 100644 --- a/src/pages/EditRequestPage.js +++ b/src/pages/EditRequestPage.js @@ -101,7 +101,7 @@ function EditRequestPage({report, route, policyCategories, policyTags, parentRep // 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, transaction, fieldToEdit)) { + if (ReportUtils.canEditFieldOfMoneyRequest(parentReportAction, fieldToEdit)) { return; } diff --git a/src/pages/iou/IOUCurrencySelection.js b/src/pages/iou/IOUCurrencySelection.js index b3180bbd5964..29580dec4bef 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, props.transaction, CONST.EDIT_REQUEST_FIELD.CURRENCY)) { + if (ReportUtils.canEditFieldOfMoneyRequest(parentReportAction, CONST.EDIT_REQUEST_FIELD.CURRENCY)) { return; } @@ -193,24 +193,6 @@ export default compose( withOnyx({ currencyList: {key: ONYXKEYS.CURRENCY_LIST}, iou: {key: ONYXKEYS.IOU}, - report: { - key: ({route}) => `${ONYXKEYS.COLLECTION.REPORT}${route.params.threadReportID}`, - }, - }), - withOnyx({ - parentReportActions: { - key: ({report}) => `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report ? report.parentReportID : '0'}`, - canEvict: false, - }, - }), - withOnyx({ - transaction: { - key: ({report, parentReportActions}) => { - const parentReportActionID = lodashGet(report, 'parentReportActionID', '0'); - const parentReportAction = lodashGet(parentReportActions, parentReportActionID); - return `${ONYXKEYS.COLLECTION.TRANSACTION}${lodashGet(parentReportAction, 'originalMessage.IOUTransactionID', 0)}`; - }, - }, }), withNetwork(), )(IOUCurrencySelection); From c51f2507081a87e60c2bfddca9336d96e42a3b2f Mon Sep 17 00:00:00 2001 From: Youssef Lourayad Date: Mon, 1 Jan 2024 16:47:20 +0100 Subject: [PATCH 17/32] Update comment --- src/components/ReportActionItem/MoneyRequestView.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/ReportActionItem/MoneyRequestView.js b/src/components/ReportActionItem/MoneyRequestView.js index 8c0dd159d3f4..5b4c98ec0682 100644 --- a/src/components/ReportActionItem/MoneyRequestView.js +++ b/src/components/ReportActionItem/MoneyRequestView.js @@ -115,7 +115,7 @@ function MoneyRequestView({report, parentReport, parentReportActions, policyCate const isSettled = ReportUtils.isSettled(moneyRequestReport.reportID); const isCancelled = moneyRequestReport && moneyRequestReport.isCancelledIOU; - // Use for non-restricted fields: description, category, tag, billable, etc. + // Used for non-restricted fields such as: description, category, tag, billable, etc. const canEdit = ReportUtils.canEditMoneyRequest(parentReportAction); const canEditAmount = ReportUtils.canEditFieldOfMoneyRequest(parentReportAction, CONST.EDIT_REQUEST_FIELD.AMOUNT); const canEditMerchant = ReportUtils.canEditFieldOfMoneyRequest(parentReportAction, CONST.EDIT_REQUEST_FIELD.MERCHANT); From 6ebe31564e9f7f055d27a2f3ea48b21fc3b5bfab Mon Sep 17 00:00:00 2001 From: Youssef Lourayad Date: Tue, 2 Jan 2024 11:31:42 +0100 Subject: [PATCH 18/32] Fix typescript errors --- src/libs/ReportUtils.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 33a5f2f5bb1e..c3acd4f94fb0 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -1869,12 +1869,12 @@ function canEditFieldOfMoneyRequest(reportAction: OnyxEntry, field return true; } - const iouMessage: IOUMessage = reportAction?.originalMessage; + const iouMessage = reportAction?.originalMessage as IOUMessage; if (isSettled(String(iouMessage.IOUReportID)) || isReportApproved(String(iouMessage.IOUReportID))) { return false; } - const transaction = allTransactions?.[`${ONYXKEYS.COLLECTION.TRANSACTION}${iouMessage?.IOUTransactionID}`] ?? {}; + const transaction = allTransactions?.[`${ONYXKEYS.COLLECTION.TRANSACTION}${iouMessage?.IOUTransactionID}`] ?? ({} as Transaction); if (fieldToEdit === CONST.EDIT_REQUEST_FIELD.AMOUNT || fieldToEdit === CONST.EDIT_REQUEST_FIELD.CURRENCY) { return !TransactionUtils.isCardTransaction(transaction); } From c5e9c58db6a4bc69edaf1de6bdff555c997a60cd Mon Sep 17 00:00:00 2001 From: Youssef Lourayad Date: Tue, 2 Jan 2024 23:32:18 +0100 Subject: [PATCH 19/32] Fix condition --- src/libs/ReportUtils.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 1c86b1715d3d..3ef13015aac3 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -1895,9 +1895,9 @@ function canEditFieldOfMoneyRequest(reportAction: OnyxEntry, field return !TransactionUtils.isCardTransaction(transaction); } - if (fieldToEdit === CONST.EDIT_REQUEST_FIELD.RECEIPT) { + if (fieldToEdit === CONST.EDIT_REQUEST_FIELD.RECEIPT && TransactionUtils.hasReceipt(transaction)) { const isRequestor = currentUserAccountID === reportAction?.actorAccountID; - return TransactionUtils.hasReceipt(transaction) && TransactionUtils.isReceiptBeingScanned(transaction) && !TransactionUtils.isDistanceRequest(transaction) && isRequestor; + return !TransactionUtils.isReceiptBeingScanned(transaction) && !TransactionUtils.isDistanceRequest(transaction) && isRequestor; } return true; From 1bea72cd98d5ca78c6a80e98b2c9c93c4651dd98 Mon Sep 17 00:00:00 2001 From: Youssef Lourayad Date: Wed, 3 Jan 2024 14:39:32 +0100 Subject: [PATCH 20/32] Update comment --- src/components/ReportActionItem/MoneyRequestView.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/components/ReportActionItem/MoneyRequestView.js b/src/components/ReportActionItem/MoneyRequestView.js index 5b4c98ec0682..b1143d561d29 100644 --- a/src/components/ReportActionItem/MoneyRequestView.js +++ b/src/components/ReportActionItem/MoneyRequestView.js @@ -124,6 +124,7 @@ function MoneyRequestView({report, parentReport, parentReportActions, policyCate 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 + // if the policy of the report is not of type Personal, then this report must be tied to workspace chat const isPolicyExpenseChat = policy.type && policy.type !== CONST.POLICY.TYPE.PERSONAL; // Fetches only the first tag, for now From 22ec1dd3e16c1f6599a6ecb1cf7488812bbba72a Mon Sep 17 00:00:00 2001 From: Youssef Lourayad Date: Wed, 3 Jan 2024 17:04:59 +0100 Subject: [PATCH 21/32] Fi comment --- src/libs/ReportUtils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index bb77466d92d2..bac6d3019ed4 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -1888,7 +1888,7 @@ function canEditMoneyRequest(reportAction: OnyxEntry): boolean { const isManager = isExpenseReport(moneyRequestReport) && currentUserAccountID === moneyRequestReport?.managerID; const isRequestor = currentUserAccountID === reportAction?.actorAccountID; - // Admin & managers can always allow coding fields such as tag, category, billable, etc. As long as the report has a state higher than OPEN. + // 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) && !isOpenReport(moneyRequestReport)) { return true; } From 7021d27460d72a7e466ed41adc2852d9e6ae2f63 Mon Sep 17 00:00:00 2001 From: Youssef Lourayad Date: Wed, 3 Jan 2024 21:47:57 +0100 Subject: [PATCH 22/32] Add policy to proptypes with default --- src/components/ReportActionItem/MoneyRequestView.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/components/ReportActionItem/MoneyRequestView.js b/src/components/ReportActionItem/MoneyRequestView.js index b1143d561d29..6d302ce14eef 100644 --- a/src/components/ReportActionItem/MoneyRequestView.js +++ b/src/components/ReportActionItem/MoneyRequestView.js @@ -34,6 +34,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'; @@ -54,6 +55,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), @@ -69,12 +73,13 @@ const propTypes = { const defaultProps = { parentReport: {}, parentReportActions: {}, - policyCategories: {}, transaction: { amount: 0, currency: CONST.CURRENCY.USD, comment: {comment: ''}, }, + ...policyDefaultProps, + policyCategories: {}, policyTags: {}, }; From a5cdf152aa52062fad53e9b1828c29da83dbb762 Mon Sep 17 00:00:00 2001 From: Youssef Lourayad Date: Wed, 3 Jan 2024 21:56:25 +0100 Subject: [PATCH 23/32] Use isGroupPolicy --- src/components/ReportActionItem/MoneyRequestView.js | 4 ++-- src/libs/ReportUtils.ts | 1 + src/pages/EditRequestPage.js | 3 +-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/components/ReportActionItem/MoneyRequestView.js b/src/components/ReportActionItem/MoneyRequestView.js index 6d302ce14eef..595fab237efb 100644 --- a/src/components/ReportActionItem/MoneyRequestView.js +++ b/src/components/ReportActionItem/MoneyRequestView.js @@ -129,8 +129,8 @@ function MoneyRequestView({report, parentReport, parentReportActions, policyCate 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 - // if the policy of the report is not of type Personal, then this report must be tied to workspace chat - const isPolicyExpenseChat = policy.type && policy.type !== CONST.POLICY.TYPE.PERSONAL; + // 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); diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index bac6d3019ed4..1b369e721c0a 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -4327,6 +4327,7 @@ export { formatReportLastMessageText, chatIncludesConcierge, isPolicyExpenseChat, + isGroupPolicy, isControlPolicyExpenseChat, isControlPolicyExpenseReport, isGroupPolicyExpenseChat, diff --git a/src/pages/EditRequestPage.js b/src/pages/EditRequestPage.js index c110fd346ff6..d84477aa8b9d 100644 --- a/src/pages/EditRequestPage.js +++ b/src/pages/EditRequestPage.js @@ -89,8 +89,7 @@ function EditRequestPage({report, route, policyCategories, policyTags, parentRep const tagListName = PolicyUtils.getTagListName(policyTags); // A flag for verifying that the current report is a sub-report of a workspace chat - const policy = ReportUtils.getPolicy(report.policyID || ''); - const isPolicyExpenseChat = policy.type && policy.type !== CONST.POLICY.TYPE.PERSONAL; + const isPolicyExpenseChat = ReportUtils.isGroupPolicy(report); // A flag for showing the categories page const shouldShowCategories = isPolicyExpenseChat && (transactionCategory || OptionsListUtils.hasEnabledOptions(lodashValues(policyCategories))); From 53e263ac535785ca76eac4651896caa8370143f0 Mon Sep 17 00:00:00 2001 From: Youssef Lourayad Date: Thu, 4 Jan 2024 23:41:14 +0100 Subject: [PATCH 24/32] Cleanup --- src/components/ReportActionItem/MoneyRequestView.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/ReportActionItem/MoneyRequestView.js b/src/components/ReportActionItem/MoneyRequestView.js index 5324fe5c6cdf..a63a9c33a046 100644 --- a/src/components/ReportActionItem/MoneyRequestView.js +++ b/src/components/ReportActionItem/MoneyRequestView.js @@ -123,7 +123,7 @@ function MoneyRequestView({report, parentReport, parentReportActions, policyCate const {isSmallScreenWidth} = useWindowDimensions(); const {translate} = useLocalize(); const {canUseViolations} = usePermissions(); - const parentReportAction = useMemo(() => parentReportActions[report.parentReportActionID] || {}, [parentReportActions, report.parentReportActionID]); + const parentReportAction = parentReportActions[report.parentReportActionID] || {}; const moneyRequestReport = parentReport; const { created: transactionDate, From 7de81d4cab0cd3292812a911be6e868fc676f3d1 Mon Sep 17 00:00:00 2001 From: Youssef Lourayad Date: Thu, 4 Jan 2024 23:51:41 +0100 Subject: [PATCH 25/32] Cleanup --- src/components/ReportActionItem/MoneyRequestView.js | 2 +- src/libs/ReportUtils.ts | 2 +- src/pages/iou/IOUCurrencySelection.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/components/ReportActionItem/MoneyRequestView.js b/src/components/ReportActionItem/MoneyRequestView.js index a63a9c33a046..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'; diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index b11ba5c7f9c3..8fe4f729e607 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -1923,7 +1923,7 @@ function canEditFieldOfMoneyRequest(reportAction: OnyxEntry, field } const iouMessage = reportAction?.originalMessage as IOUMessage; - const moneyRequestReport = (allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${iouMessage?.IOUReportID}`] ?? {}) as Report; + 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))) { diff --git a/src/pages/iou/IOUCurrencySelection.js b/src/pages/iou/IOUCurrencySelection.js index 29580dec4bef..24833fc96fdc 100644 --- a/src/pages/iou/IOUCurrencySelection.js +++ b/src/pages/iou/IOUCurrencySelection.js @@ -94,7 +94,7 @@ function IOUCurrencySelection(props) { Navigation.isNavigationReady().then(() => { Navigation.dismissModal(); }); - }, [threadReportID, props.transaction]); + }, [threadReportID]); const confirmCurrencySelection = useCallback( (option) => { From 4123a3106f41fc0576f75bb8050f6577fb879481 Mon Sep 17 00:00:00 2001 From: Youssef Lourayad Date: Fri, 5 Jan 2024 00:06:07 +0100 Subject: [PATCH 26/32] Remove unneeded check --- src/libs/ReportUtils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 8fe4f729e607..7df2e9c0c2ee 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -1944,7 +1944,7 @@ function canEditFieldOfMoneyRequest(reportAction: OnyxEntry, field } } - if (fieldToEdit === CONST.EDIT_REQUEST_FIELD.RECEIPT && TransactionUtils.hasReceipt(transaction)) { + if (fieldToEdit === CONST.EDIT_REQUEST_FIELD.RECEIPT) { const isRequestor = currentUserAccountID === reportAction?.actorAccountID; return !TransactionUtils.isReceiptBeingScanned(transaction) && !TransactionUtils.isDistanceRequest(transaction) && isRequestor; } From a130cb381a61b785556841f58c41a52961cf82b0 Mon Sep 17 00:00:00 2001 From: Youssef Lourayad Date: Fri, 5 Jan 2024 16:15:29 +0100 Subject: [PATCH 27/32] Update src/libs/ReportUtils.ts Co-authored-by: Vit Horacek <36083550+mountiny@users.noreply.github.com> --- src/libs/ReportUtils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 7df2e9c0c2ee..0117694addb3 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -1859,7 +1859,7 @@ function getTransactionDetails(transaction: OnyxEntry, createdDateF * - 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 conjuction with canEditRestrictedField to control editing of specific fields like amount, currency, created, receipt, and distance. + * 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): boolean { From b6ba955c90b210f78120fe94f326ae3d686de265 Mon Sep 17 00:00:00 2001 From: Youssef Lourayad Date: Fri, 5 Jan 2024 16:17:54 +0100 Subject: [PATCH 28/32] use isDraftExpenseReport --- src/libs/ReportUtils.ts | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 0117694addb3..2956f16da697 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -608,7 +608,7 @@ function isReportApproved(reportOrID: OnyxEntry | string | EmptyObject): /** * 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; } @@ -822,13 +822,6 @@ function isProcessingReport(report: OnyxEntry | EmptyObject): boolean { return report?.stateNum === CONST.REPORT.STATE_NUM.PROCESSING && report?.statusNum === CONST.REPORT.STATUS.SUBMITTED; } -/** - * Returns true if report is still open - */ -function isOpenReport(report: OnyxEntry | EmptyObject): boolean { - return report?.stateNum === CONST.REPORT.STATE_NUM.OPEN && report?.statusNum === CONST.REPORT.STATUS.OPEN; -} - /** * Check if the report is a single chat report that isn't a thread * and personal detail of participant is optimistic data @@ -1891,7 +1884,7 @@ function canEditMoneyRequest(reportAction: OnyxEntry): boolean { const isRequestor = currentUserAccountID === reportAction?.actorAccountID; // 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) && !isOpenReport(moneyRequestReport)) { + if ((isAdmin || isManager) && !isDraftExpenseReport(moneyRequestReport)) { return true; } From 1707e031ec7f54ea8fac491b8fc96c2288e891ba Mon Sep 17 00:00:00 2001 From: Youssef Lourayad Date: Fri, 5 Jan 2024 16:19:37 +0100 Subject: [PATCH 29/32] Move variables into the scope where they're needed --- src/libs/ReportUtils.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 2956f16da697..a3aedf0e57a9 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -1928,11 +1928,11 @@ function canEditFieldOfMoneyRequest(reportAction: OnyxEntry, field return false; } - const policy = getPolicy(moneyRequestReport?.reportID ?? ''); - const isAdmin = isExpenseReport(moneyRequestReport) && policy.role === CONST.POLICY.ROLE.ADMIN; - const isManager = isExpenseReport(moneyRequestReport) && currentUserAccountID === moneyRequestReport?.managerID; - 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; } } From 8491e868729b4cb1a3c9c4bd7eee74213d066c0f Mon Sep 17 00:00:00 2001 From: Youssef Lourayad Date: Fri, 5 Jan 2024 17:23:47 +0100 Subject: [PATCH 30/32] Rename methods --- src/components/MoneyReportHeader.js | 8 +++--- .../ReportActionItem/MoneyRequestPreview.js | 2 +- .../ReportActionItem/ReportPreview.js | 10 +++---- src/libs/ReportUtils.ts | 27 ++++++++++++------- 4 files changed, 28 insertions(+), 19 deletions(-) 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/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 a3aedf0e57a9..165fd611da44 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -609,7 +609,7 @@ function isReportApproved(reportOrID: OnyxEntry | string | EmptyObject): * Checks if the supplied report is an expense report in Open state and status. */ function isDraftExpenseReport(report: OnyxEntry | EmptyObject): boolean { - return isExpenseReport(report) && report?.stateNum === CONST.REPORT.STATE_NUM.OPEN && report?.statusNum === CONST.REPORT.STATUS.OPEN; + return report?.stateNum === CONST.REPORT.STATE_NUM.OPEN && report?.statusNum === CONST.REPORT.STATUS.OPEN; } /** @@ -715,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; } @@ -725,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); } /** @@ -739,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); } /** @@ -2082,7 +2090,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, @@ -4335,10 +4343,11 @@ export { chatIncludesConcierge, isPolicyExpenseChat, isGroupPolicy, + isPaidGroupPolicy, isControlPolicyExpenseChat, isControlPolicyExpenseReport, - isGroupPolicyExpenseChat, - isGroupPolicyExpenseReport, + isPaidGroupPolicyExpenseChat, + isPaidGroupPolicyExpenseReport, getIconsForParticipants, getIcons, getRoomWelcomeMessage, From 3525c5e6f0e7a6e8ae335c44ea3e5569d7ed7313 Mon Sep 17 00:00:00 2001 From: Youssef Lourayad Date: Fri, 5 Jan 2024 18:18:35 +0100 Subject: [PATCH 31/32] Add code removed by mistake --- src/libs/ReportUtils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 165fd611da44..cee7a56d520b 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -609,7 +609,7 @@ function isReportApproved(reportOrID: OnyxEntry | string | EmptyObject): * Checks if the supplied report is an expense report in Open state and status. */ function isDraftExpenseReport(report: OnyxEntry | EmptyObject): boolean { - return report?.stateNum === CONST.REPORT.STATE_NUM.OPEN && report?.statusNum === CONST.REPORT.STATUS.OPEN; + return isExpenseReport(report) && report?.stateNum === CONST.REPORT.STATE_NUM.OPEN && report?.statusNum === CONST.REPORT.STATUS.OPEN; } /** From 316b71832ac00b310034775e96f067ee95fd891f Mon Sep 17 00:00:00 2001 From: Youssef Lourayad Date: Fri, 5 Jan 2024 18:50:10 +0100 Subject: [PATCH 32/32] Make code clearer --- src/libs/ReportUtils.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index cee7a56d520b..baf8c460fd2a 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -1886,11 +1886,16 @@ function canEditMoneyRequest(reportAction: OnyxEntry): boolean { } const moneyRequestReport = getReport(String(moneyRequestReportID)); - const policy = getPolicy(moneyRequestReport?.policyID ?? ''); - const isAdmin = isExpenseReport(moneyRequestReport) && policy.role === CONST.POLICY.ROLE.ADMIN; - const isManager = isExpenseReport(moneyRequestReport) && currentUserAccountID === moneyRequestReport?.managerID; const isRequestor = currentUserAccountID === reportAction?.actorAccountID; + if (isIOUReport(moneyRequestReport)) { + return isProcessingReport(moneyRequestReport) && isRequestor; + } + + 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;