From 9bc5b3882beee69dbbd98b73cbfa110cf130cc6d Mon Sep 17 00:00:00 2001 From: Youssef Lourayad Date: Tue, 13 Feb 2024 00:39:23 +0100 Subject: [PATCH 01/22] Allow requesting money from processing reports in Collect with Instant Submit enabled --- 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 a2401ad926d3..d4772993dc7d 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -4100,7 +4100,7 @@ function canRequestMoney(report: OnyxEntry, policy: OnyxEntry, o if (isMoneyRequestReport(report)) { const isOwnExpenseReport = isExpenseReport(report) && isOwnPolicyExpenseChat; if (isOwnExpenseReport && PolicyUtils.isPaidGroupPolicy(policy)) { - return isDraftExpenseReport(report); + return isDraftExpenseReport(report) || (PolicyUtils.isInstantSubmitEnabled(policy) && isProcessingReport(report)); } return (isOwnExpenseReport || isIOUReport(report)) && !isReportApproved(report) && !isSettled(report?.reportID); From 34917e523e01b949b5c3a795388aa787478ce838 Mon Sep 17 00:00:00 2001 From: Youssef Lourayad Date: Tue, 13 Feb 2024 01:05:35 +0100 Subject: [PATCH 02/22] Add new expenses to processing reports --- src/libs/actions/IOU.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 7fca6614f1a1..2b65b04268d2 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -760,8 +760,11 @@ function getMoneyRequestInformation( // If the scheduled submit is turned off on the policy, user needs to manually submit the report which is indicated by GBR in LHN needsToBeManuallySubmitted = isFromPaidPolicy && !policy?.harvesting?.enabled; - // If the linked expense report on paid policy is not draft, we need to create a new draft expense report - if (iouReport && isFromPaidPolicy && !ReportUtils.isDraftExpenseReport(iouReport)) { + const isProcessingReportOnInstantSubmitPolicy = + isFromPaidPolicy && ReportUtils.isProcessingReport(iouReport) && PolicyUtils.isInstantSubmitEnabled(policy ?? ({} as OnyxEntry)); + + // If the linked expense report on paid policy is not draft, or is not processing in a policy with Instant Submit enabled, we need to create a new draft expense report + if (iouReport && isFromPaidPolicy && !ReportUtils.isDraftExpenseReport(iouReport) && !isProcessingReportOnInstantSubmitPolicy) { iouReport = null; } } From 99d1542527e61a1408e79a32a5c60298733796b3 Mon Sep 17 00:00:00 2001 From: Youssef Lourayad Date: Tue, 13 Feb 2024 22:53:46 +0100 Subject: [PATCH 03/22] Set optimisitc report as submitted if instant submit is turned on --- src/libs/PolicyUtils.ts | 2 +- src/libs/ReportUtils.ts | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libs/PolicyUtils.ts b/src/libs/PolicyUtils.ts index a3729ef81fe4..543ee57898fa 100644 --- a/src/libs/PolicyUtils.ts +++ b/src/libs/PolicyUtils.ts @@ -210,7 +210,7 @@ function isPaidGroupPolicy(policy: OnyxEntry | EmptyObject): boolean { * Checks if policy's scheduled submit / auto reporting frequency is "instant". * Note: Free policies have "instant" submit always enabled. */ -function isInstantSubmitEnabled(policy: OnyxEntry): boolean { +function isInstantSubmitEnabled(policy: OnyxEntry | EmptyObject): boolean { return policy?.autoReportingFrequency === CONST.POLICY.AUTO_REPORTING_FREQUENCIES.INSTANT || policy?.type === CONST.POLICY.TYPE.FREE; } diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index d4772993dc7d..1fe1445f6847 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -2833,11 +2833,11 @@ function buildOptimisticExpenseReport(chatReportID: string, policyID: string, pa const formattedTotal = CurrencyUtils.convertToDisplayString(storedTotal, currency); const policy = getPolicy(policyID); - const isFree = policy?.type === CONST.POLICY.TYPE.FREE; + const isInstantSubmitEnabled = policy?.type === CONST.POLICY.TYPE.FREE || (PolicyUtils.isPaidGroupPolicy(policy) && PolicyUtils.isInstantSubmitEnabled(policy)); // Define the state and status of the report based on whether the policy is free or paid - const stateNum = isFree ? CONST.REPORT.STATE_NUM.SUBMITTED : CONST.REPORT.STATE_NUM.OPEN; - const statusNum = isFree ? CONST.REPORT.STATUS_NUM.SUBMITTED : CONST.REPORT.STATUS_NUM.OPEN; + const stateNum = isInstantSubmitEnabled ? CONST.REPORT.STATE_NUM.SUBMITTED : CONST.REPORT.STATE_NUM.OPEN; + const statusNum = isInstantSubmitEnabled ? CONST.REPORT.STATUS_NUM.SUBMITTED : CONST.REPORT.STATUS_NUM.OPEN; const expenseReport: OptimisticExpenseReport = { reportID: generateReportID(), From 4fc82e74c8cb32c8f840d269a73e6b37b61f9cbc Mon Sep 17 00:00:00 2001 From: Youssef Lourayad Date: Tue, 13 Feb 2024 22:54:12 +0100 Subject: [PATCH 04/22] Do not create a new report preview if we have a processing report already --- src/libs/actions/IOU.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 2b65b04268d2..0f80d4d5af7d 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -744,7 +744,7 @@ function getMoneyRequestInformation( // STEP 2: Get the money request report. If the moneyRequestReportID has been provided, we want to add the transaction to this specific report. // If no such reportID has been provided, let's use the chatReport.iouReportID property. In case that is not present, build a new optimistic money request report. let iouReport: OnyxEntry = null; - const shouldCreateNewMoneyRequestReport = !moneyRequestReportID && (!chatReport.iouReportID || ReportUtils.hasIOUWaitingOnCurrentUserBankAccount(chatReport)); + let shouldCreateNewMoneyRequestReport = !moneyRequestReportID && (!chatReport.iouReportID || ReportUtils.hasIOUWaitingOnCurrentUserBankAccount(chatReport)); if (moneyRequestReportID) { iouReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${moneyRequestReportID}`] ?? null; } else if (!shouldCreateNewMoneyRequestReport) { @@ -767,6 +767,10 @@ function getMoneyRequestInformation( if (iouReport && isFromPaidPolicy && !ReportUtils.isDraftExpenseReport(iouReport) && !isProcessingReportOnInstantSubmitPolicy) { iouReport = null; } + + if (isProcessingReportOnInstantSubmitPolicy) { + shouldCreateNewMoneyRequestReport = false; + } } if (iouReport) { From 4eb5cbffbf0c8c37829b484a9c61bc58bf3f62c5 Mon Sep 17 00:00:00 2001 From: Youssef Lourayad Date: Fri, 23 Feb 2024 02:47:53 +0100 Subject: [PATCH 05/22] Add support for splitBill --- src/libs/actions/IOU.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index a7d3fba00504..56347998c5ac 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -1735,7 +1735,7 @@ function createSplitsAndOnyxData( // For Control policy expense chats, if the report is already approved, create a new expense report let oneOnOneIOUReport: OneOnOneIOUReport = oneOnOneChatReport.iouReportID ? allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${oneOnOneChatReport.iouReportID}`] : null; const shouldCreateNewOneOnOneIOUReport = - !oneOnOneIOUReport || (isOwnPolicyExpenseChat && ReportUtils.isControlPolicyExpenseReport(oneOnOneIOUReport) && ReportUtils.isReportApproved(oneOnOneIOUReport)); + !oneOnOneIOUReport || (isOwnPolicyExpenseChat && ReportUtils.isPaidGroupPolicy(oneOnOneIOUReport) && ReportUtils.isReportApproved(oneOnOneIOUReport)); if (!oneOnOneIOUReport || shouldCreateNewOneOnOneIOUReport) { oneOnOneIOUReport = isOwnPolicyExpenseChat @@ -2366,7 +2366,7 @@ function completeSplitBill(chatReportID: string, reportAction: OnyxTypes.ReportA let oneOnOneIOUReport: OneOnOneIOUReport = oneOnOneChatReport?.iouReportID ? allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${oneOnOneChatReport.iouReportID}`] : null; const shouldCreateNewOneOnOneIOUReport = - !oneOnOneIOUReport || (isPolicyExpenseChat && ReportUtils.isControlPolicyExpenseReport(oneOnOneIOUReport) && ReportUtils.isReportApproved(oneOnOneIOUReport)); + !oneOnOneIOUReport || (isPolicyExpenseChat && ReportUtils.isPaidGroupPolicy(oneOnOneIOUReport) && ReportUtils.isReportApproved(oneOnOneIOUReport)); if (!oneOnOneIOUReport || shouldCreateNewOneOnOneIOUReport) { oneOnOneIOUReport = isPolicyExpenseChat From 9587d196760d4aa71287c1bf74a3e1a6ffa46917 Mon Sep 17 00:00:00 2001 From: Youssef Lourayad Date: Wed, 28 Feb 2024 22:56:45 +0100 Subject: [PATCH 06/22] Add help method for deciding if we can add transactions to a report --- src/libs/ReportUtils.ts | 23 ++++++++++++++++++++++- src/libs/actions/IOU.ts | 6 ++---- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index bee0972baf97..20fb77f12daf 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -2921,7 +2921,7 @@ function buildOptimisticExpenseReport(chatReportID: string, policyID: string, pa const formattedTotal = CurrencyUtils.convertToDisplayString(storedTotal, currency); const policy = getPolicy(policyID); - const isInstantSubmitEnabled = policy?.type === CONST.POLICY.TYPE.FREE || (PolicyUtils.isPaidGroupPolicy(policy) && PolicyUtils.isInstantSubmitEnabled(policy)); + const isInstantSubmitEnabled = PolicyUtils.isInstantSubmitEnabled(policy); // Define the state and status of the report based on whether the policy is free or paid const stateNum = isInstantSubmitEnabled ? CONST.REPORT.STATE_NUM.SUBMITTED : CONST.REPORT.STATE_NUM.OPEN; @@ -4215,6 +4215,26 @@ function hasIOUWaitingOnCurrentUserBankAccount(chatReport: OnyxEntry): b return false; } +/** + * Checks whether the supplied report supports adding more transactions to it. + */ +function canAddTransactionsToExpenseReport(report: OnyxEntry) { + if (!isExpenseReport(report)) { + return false; + } + + if (isReportApproved(report) || isSettled(report)) { + return false; + } + + if (isProcessingReport(report) && !PolicyUtils.isInstantSubmitEnabled(getPolicy(report?.policyID))) { + return false; + } + + // We've narrowed it down to reports that are either drafts, or submitted under a policy with Instant Submit + return true; +} + /** * Users can request money: * - in policy expense chats only if they are in a role of a member in the chat (in other words, if it's their policy expense chat) @@ -5226,6 +5246,7 @@ export { canEditRoomVisibility, canEditPolicyDescription, getPolicyDescriptionText, + canAddTransactionsToExpenseReport, }; export type { diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 56347998c5ac..56409e8ade74 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -1734,8 +1734,7 @@ function createSplitsAndOnyxData( // STEP 2: Get existing IOU/Expense report and update its total OR build a new optimistic one // For Control policy expense chats, if the report is already approved, create a new expense report let oneOnOneIOUReport: OneOnOneIOUReport = oneOnOneChatReport.iouReportID ? allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${oneOnOneChatReport.iouReportID}`] : null; - const shouldCreateNewOneOnOneIOUReport = - !oneOnOneIOUReport || (isOwnPolicyExpenseChat && ReportUtils.isPaidGroupPolicy(oneOnOneIOUReport) && ReportUtils.isReportApproved(oneOnOneIOUReport)); + const shouldCreateNewOneOnOneIOUReport = !oneOnOneIOUReport || (isOwnPolicyExpenseChat && !ReportUtils.canAddTransactionsToExpenseReport(oneOnOneIOUReport)); if (!oneOnOneIOUReport || shouldCreateNewOneOnOneIOUReport) { oneOnOneIOUReport = isOwnPolicyExpenseChat @@ -2365,8 +2364,7 @@ function completeSplitBill(chatReportID: string, reportAction: OnyxTypes.ReportA } let oneOnOneIOUReport: OneOnOneIOUReport = oneOnOneChatReport?.iouReportID ? allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${oneOnOneChatReport.iouReportID}`] : null; - const shouldCreateNewOneOnOneIOUReport = - !oneOnOneIOUReport || (isPolicyExpenseChat && ReportUtils.isPaidGroupPolicy(oneOnOneIOUReport) && ReportUtils.isReportApproved(oneOnOneIOUReport)); + const shouldCreateNewOneOnOneIOUReport = !oneOnOneIOUReport || (isPolicyExpenseChat && !ReportUtils.canAddTransactionsToExpenseReport(oneOnOneIOUReport)); if (!oneOnOneIOUReport || shouldCreateNewOneOnOneIOUReport) { oneOnOneIOUReport = isPolicyExpenseChat From 72ddbc8d6228d6a1418a900851420d27993604f8 Mon Sep 17 00:00:00 2001 From: Youssef Lourayad Date: Thu, 29 Feb 2024 01:41:50 +0100 Subject: [PATCH 07/22] Add comment in function definition --- 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 caa0eb4fce7c..4c7f592aabb6 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -4168,6 +4168,10 @@ function hasIOUWaitingOnCurrentUserBankAccount(chatReport: OnyxEntry): b /** * Checks whether the supplied report supports adding more transactions to it. + * Return true if: + * - report is a non-settled IOU + * - report is a draft + * - report is processing and policy's on Instant Submit */ function canAddTransactionsToMoneyRequest(report: OnyxEntry) { if (!isIOUReport(report) && !isExpenseReport(report)) { @@ -4182,10 +4186,6 @@ function canAddTransactionsToMoneyRequest(report: OnyxEntry) { return false; } - // We've narrowed it down to reports that are either: - // - non-settled IOUs - // - draft expense reports - // - submitted expense reports under a policy with Instant Submit return true; } From f5a59595487b11a5ac2d44c047e671620b3f1bdd Mon Sep 17 00:00:00 2001 From: Youssef Lourayad Date: Thu, 29 Feb 2024 02:10:40 +0100 Subject: [PATCH 08/22] Clean up code in MoneyRequestHeader --- src/components/MoneyRequestHeader.tsx | 17 +++----- src/libs/ReportUtils.ts | 59 ++++++++++++--------------- 2 files changed, 31 insertions(+), 45 deletions(-) diff --git a/src/components/MoneyRequestHeader.tsx b/src/components/MoneyRequestHeader.tsx index fe8cc3506b3f..aedbdff84786 100644 --- a/src/components/MoneyRequestHeader.tsx +++ b/src/components/MoneyRequestHeader.tsx @@ -79,16 +79,11 @@ function MoneyRequestHeader({session, parentReport, report, parentReportAction, const isScanning = TransactionUtils.hasReceipt(transaction) && TransactionUtils.isReceiptBeingScanned(transaction); const isPending = TransactionUtils.isExpensifyCardTransaction(transaction) && TransactionUtils.isPending(transaction); - const isRequestModifiable = !isSettled && !isApproved && !ReportActionsUtils.isDeletedAction(parentReportAction); - const canModifyRequest = isActionOwner && !isSettled && !isApproved && !ReportActionsUtils.isDeletedAction(parentReportAction); - let canDeleteRequest = canModifyRequest; - - if (ReportUtils.isPaidGroupPolicyExpenseReport(moneyRequestReport)) { - // If it's a paid policy expense report, only allow deleting the request if it's in draft state or instantly submitted state or the user is the policy admin - canDeleteRequest = - canDeleteRequest && - (ReportUtils.isDraftExpenseReport(moneyRequestReport) || ReportUtils.isExpenseReportWithInstantSubmittedState(moneyRequestReport) || PolicyUtils.isPolicyAdmin(policy)); - } + const isDeletedParentAction = ReportActionsUtils.isDeletedAction(parentReportAction); + const canHoldOrUnholdRequest = !isSettled && !isApproved && !isDeletedParentAction; + + // If the report supports adding transactions to it, then it also supports deleting transactions from it. + const canDeleteRequest = isActionOwner && ReportUtils.canAddTransactionsToMoneyRequest(moneyRequestReport) && !isDeletedParentAction; const changeMoneyRequestStatus = () => { if (isOnHold) { @@ -108,7 +103,7 @@ function MoneyRequestHeader({session, parentReport, report, parentReportAction, }, [canDeleteRequest]); const threeDotsMenuItems = [HeaderUtils.getPinMenuItem(report)]; - if (isRequestModifiable) { + if (canHoldOrUnholdRequest) { const isRequestIOU = parentReport?.type === 'iou'; const isHoldCreator = ReportUtils.isHoldCreator(transaction, report?.reportID) && isRequestIOU; const canModifyStatus = isPolicyAdmin || isActionOwner || isApprover; diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 4c7f592aabb6..e85015199e9b 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -947,14 +947,6 @@ function isProcessingReport(report: OnyxEntry | EmptyObject): boolean { return report?.stateNum === CONST.REPORT.STATE_NUM.SUBMITTED && report?.statusNum === CONST.REPORT.STATUS_NUM.SUBMITTED; } -/** - * Returns true if the policy has `instant` reporting frequency and if the report is still being processed (i.e. submitted state) - */ -function isExpenseReportWithInstantSubmittedState(report: OnyxEntry | EmptyObject): boolean { - const policy = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${report?.policyID}`] ?? null; - return isExpenseReport(report) && isProcessingReport(report) && PolicyUtils.isInstantSubmitEnabled(policy); -} - /** * Check if the report is a single chat report that isn't a thread * and personal detail of participant is optimistic data @@ -1244,6 +1236,29 @@ function getChildReportNotificationPreference(reportAction: OnyxEntry): boolean { + if (!isIOUReport(report) && !isExpenseReport(report)) { + return false; + } + + if (isReportApproved(report) || isSettled(report?.reportID)) { + return false; + } + + if (isProcessingReport(report) && !PolicyUtils.isInstantSubmitEnabled(getPolicy(report?.policyID))) { + return false; + } + + return true; +} + /** * Can only delete if the author is this user and the action is an ADDCOMMENT action or an IOU action in an unsettled report, or if the user is a * policy admin @@ -1264,8 +1279,8 @@ function canDeleteReportAction(reportAction: OnyxEntry, reportID: if (isActionOwner) { if (!isEmptyObject(report) && isPaidGroupPolicyExpenseReport(report)) { - // If it's a paid policy expense report, only allow deleting the request if it's a draft or is instantly submitted or the user is the policy admin - return isDraftExpenseReport(report) || isExpenseReportWithInstantSubmittedState(report) || PolicyUtils.isPolicyAdmin(policy); + // If the report supports adding transactions to it, then it also supports deleting transactions from it. + return canAddTransactionsToMoneyRequest(report); } return true; } @@ -4166,29 +4181,6 @@ function hasIOUWaitingOnCurrentUserBankAccount(chatReport: OnyxEntry): b return false; } -/** - * Checks whether the supplied report supports adding more transactions to it. - * Return true if: - * - report is a non-settled IOU - * - report is a draft - * - report is processing and policy's on Instant Submit - */ -function canAddTransactionsToMoneyRequest(report: OnyxEntry) { - if (!isIOUReport(report) && !isExpenseReport(report)) { - return false; - } - - if (isReportApproved(report) || isSettled(report?.reportID)) { - return false; - } - - if (isProcessingReport(report) && !PolicyUtils.isInstantSubmitEnabled(getPolicy(report?.policyID))) { - return false; - } - - return true; -} - /** * Users can request money: * - in policy expense chats only if they are in a role of a member in the chat (in other words, if it's their policy expense chat) @@ -5025,7 +5017,6 @@ export { isPublicAnnounceRoom, isConciergeChatReport, isProcessingReport, - isExpenseReportWithInstantSubmittedState, isCurrentUserTheOnlyParticipant, hasAutomatedExpensifyAccountIDs, hasExpensifyGuidesEmails, From 5be0b3f258a243816323cde0cef0acb6e96e7c71 Mon Sep 17 00:00:00 2001 From: Youssef Lourayad Date: Thu, 29 Feb 2024 02:30:14 +0100 Subject: [PATCH 09/22] DRY up code in shouldBuildOptimisticMoneyRequestReport --- src/libs/ReportUtils.ts | 12 ++++++++++++ src/libs/actions/IOU.ts | 19 +++++++------------ 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index e85015199e9b..3ba62072a51d 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -4986,6 +4986,17 @@ function canBeAutoReimbursed(report: OnyxEntry, policy: OnyxEntry | undefined | null, chatReport: OnyxEntry | null): boolean { + return !existingIOUReport || hasIOUWaitingOnCurrentUserBankAccount(chatReport) || !canAddTransactionsToMoneyRequest(existingIOUReport); +} + export { getReportParticipantsTitle, isReportMessageAttachment, @@ -5188,6 +5199,7 @@ export { canEditPolicyDescription, getPolicyDescriptionText, canAddTransactionsToMoneyRequest, + shouldBuildOptimisticMoneyRequestReport, }; export type { diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index c9599aabec7e..a016e6001dc5 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -834,11 +834,7 @@ function getMoneyRequestInformation( iouReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${chatReport.iouReportID}`] ?? null; } - // Create a new report if: - // - we don't have an iouReport set in the chatReport - // - we have one, but it's waiting on the payee adding a bank account - // - we have one but we can't add more transactions to it due to: report is approved or settled, or, report is processing and policy isn't under Instant Submit - const shouldCreateNewMoneyRequestReport = !iouReport || ReportUtils.hasIOUWaitingOnCurrentUserBankAccount(chatReport) || !ReportUtils.canAddTransactionsToMoneyRequest(iouReport); + const shouldCreateNewMoneyRequestReport = ReportUtils.shouldBuildOptimisticMoneyRequestReport(iouReport, chatReport); if (!iouReport || shouldCreateNewMoneyRequestReport) { iouReport = isPolicyExpenseChat @@ -1836,11 +1832,10 @@ function createSplitsAndOnyxData( } // STEP 2: Get existing IOU/Expense report and update its total OR build a new optimistic one - // For Control policy expense chats, if the report is already approved, create a new expense report let oneOnOneIOUReport: OneOnOneIOUReport = oneOnOneChatReport.iouReportID ? allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${oneOnOneChatReport.iouReportID}`] : null; - const shouldCreateNewOneOnOneIOUReport = !oneOnOneIOUReport || (isOwnPolicyExpenseChat && !ReportUtils.canAddTransactionsToMoneyRequest(oneOnOneIOUReport)); + const shouldCreateNewMoneyRequestReport = ReportUtils.shouldBuildOptimisticMoneyRequestReport(oneOnOneIOUReport, oneOnOneChatReport); - if (!oneOnOneIOUReport || shouldCreateNewOneOnOneIOUReport) { + if (!oneOnOneIOUReport || shouldCreateNewMoneyRequestReport) { oneOnOneIOUReport = isOwnPolicyExpenseChat ? ReportUtils.buildOptimisticExpenseReport(oneOnOneChatReport.reportID, oneOnOneChatReport.policyID ?? '', currentUserAccountID, splitAmount, currency) : ReportUtils.buildOptimisticIOUReport(currentUserAccountID, accountID, splitAmount, oneOnOneChatReport.reportID, currency); @@ -1943,7 +1938,7 @@ function createSplitsAndOnyxData( isNewOneOnOneChatReport, optimisticTransactionThread, optimisticCreatedActionForTransactionThread, - shouldCreateNewOneOnOneIOUReport, + shouldCreateNewMoneyRequestReport, ); const individualSplit = { @@ -2476,9 +2471,9 @@ function completeSplitBill(chatReportID: string, reportAction: OnyxTypes.ReportA } let oneOnOneIOUReport: OneOnOneIOUReport = oneOnOneChatReport?.iouReportID ? allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${oneOnOneChatReport.iouReportID}`] : null; - const shouldCreateNewOneOnOneIOUReport = !oneOnOneIOUReport || (isPolicyExpenseChat && !ReportUtils.canAddTransactionsToMoneyRequest(oneOnOneIOUReport)); + const shouldCreateNewOneOnOneMoneyRequestReport = ReportUtils.shouldBuildOptimisticMoneyRequestReport(oneOnOneIOUReport, oneOnOneChatReport); - if (!oneOnOneIOUReport || shouldCreateNewOneOnOneIOUReport) { + if (!oneOnOneIOUReport || shouldCreateNewOneOnOneMoneyRequestReport) { oneOnOneIOUReport = isPolicyExpenseChat ? ReportUtils.buildOptimisticExpenseReport(oneOnOneChatReport?.reportID ?? '', participant.policyID ?? '', sessionAccountID, splitAmount, currency ?? '') : ReportUtils.buildOptimisticIOUReport(sessionAccountID, participant.accountID ?? -1, splitAmount, oneOnOneChatReport?.reportID ?? '', currency ?? ''); @@ -2545,7 +2540,7 @@ function completeSplitBill(chatReportID: string, reportAction: OnyxTypes.ReportA isNewOneOnOneChatReport, optimisticTransactionThread, optimisticCreatedActionForTransactionThread, - shouldCreateNewOneOnOneIOUReport, + shouldCreateNewOneOnOneMoneyRequestReport, ); splits.push({ From 206cd78e23b50e025a26aaf49d64513ed3fa8e3d Mon Sep 17 00:00:00 2001 From: Youssef Lourayad Date: Thu, 29 Feb 2024 02:41:39 +0100 Subject: [PATCH 10/22] Lint --- src/components/MoneyRequestHeader.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/src/components/MoneyRequestHeader.tsx b/src/components/MoneyRequestHeader.tsx index aedbdff84786..db14c7ee1898 100644 --- a/src/components/MoneyRequestHeader.tsx +++ b/src/components/MoneyRequestHeader.tsx @@ -7,7 +7,6 @@ import useThemeStyles from '@hooks/useThemeStyles'; import useWindowDimensions from '@hooks/useWindowDimensions'; import * as HeaderUtils from '@libs/HeaderUtils'; import Navigation from '@libs/Navigation/Navigation'; -import * as PolicyUtils from '@libs/PolicyUtils'; import * as ReportActionsUtils from '@libs/ReportActionsUtils'; import * as ReportUtils from '@libs/ReportUtils'; import * as TransactionUtils from '@libs/TransactionUtils'; From f0a99ebd4cbad487d15f876cf14c9848068462f1 Mon Sep 17 00:00:00 2001 From: Youssef Lourayad Date: Thu, 29 Feb 2024 02:45:50 +0100 Subject: [PATCH 11/22] Fix test --- 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 3ba62072a51d..2368dfb36c52 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -4218,7 +4218,8 @@ function canRequestMoney(report: OnyxEntry, policy: OnyxEntry, o // User can request money in any IOU report, unless paid, but user can only request money in an expense report // which is tied to their workspace chat. if (isMoneyRequestReport(report)) { - return isOwnPolicyExpenseChat && canAddTransactionsToMoneyRequest(report); + const canAddTransactions = canAddTransactionsToMoneyRequest(report); + return isPaidGroupPolicy(report) ? isOwnPolicyExpenseChat && canAddTransactions : canAddTransactions; } // In case of policy expense chat, users can only request money from their own policy expense chat From 933e0d80659e8839ad1fd807ce0449d515c42b3d Mon Sep 17 00:00:00 2001 From: Youssef Lourayad Date: Thu, 29 Feb 2024 02:54:22 +0100 Subject: [PATCH 12/22] Fix test for real now --- 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 2368dfb36c52..97f0c5686df0 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -1252,7 +1252,7 @@ function canAddTransactionsToMoneyRequest(report: OnyxEntry): boolean { return false; } - if (isProcessingReport(report) && !PolicyUtils.isInstantSubmitEnabled(getPolicy(report?.policyID))) { + if (isGroupPolicy(report) && isProcessingReport(report) && !PolicyUtils.isInstantSubmitEnabled(getPolicy(report?.policyID))) { return false; } @@ -4219,7 +4219,7 @@ function canRequestMoney(report: OnyxEntry, policy: OnyxEntry, o // which is tied to their workspace chat. if (isMoneyRequestReport(report)) { const canAddTransactions = canAddTransactionsToMoneyRequest(report); - return isPaidGroupPolicy(report) ? isOwnPolicyExpenseChat && canAddTransactions : canAddTransactions; + return isGroupPolicy(report) ? isOwnPolicyExpenseChat && canAddTransactions : canAddTransactions; } // In case of policy expense chat, users can only request money from their own policy expense chat From 902f45c5981b7ba164858bb83bbcdac9a7a47cef Mon Sep 17 00:00:00 2001 From: Youssef Lourayad Date: Thu, 29 Feb 2024 23:19:35 +0100 Subject: [PATCH 13/22] Apply suggestions from code review Co-authored-by: Alex Beaman --- 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 97f0c5686df0..0952cdaea513 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -1241,7 +1241,7 @@ function getChildReportNotificationPreference(reportAction: OnyxEntry): boolean { if (!isIOUReport(report) && !isExpenseReport(report)) { @@ -4992,7 +4992,7 @@ function canBeAutoReimbursed(report: OnyxEntry, policy: OnyxEntry | undefined | null, chatReport: OnyxEntry | null): boolean { return !existingIOUReport || hasIOUWaitingOnCurrentUserBankAccount(chatReport) || !canAddTransactionsToMoneyRequest(existingIOUReport); From 267de9d0c6c56d0657a9de6c36774b8d44165abb Mon Sep 17 00:00:00 2001 From: Youssef Lourayad Date: Fri, 1 Mar 2024 02:18:34 +0100 Subject: [PATCH 14/22] Update tests --- tests/unit/ReportUtilsTest.js | 39 +++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/tests/unit/ReportUtilsTest.js b/tests/unit/ReportUtilsTest.js index a5b0a5d3c151..f8c830c8f3c8 100644 --- a/tests/unit/ReportUtilsTest.js +++ b/tests/unit/ReportUtilsTest.js @@ -410,24 +410,27 @@ describe('ReportUtils', () => { }); }); - it("it is a non-open expense report tied to user's own paid policy expense chat", () => { + it("it is a submitted report tied to user's own policy expense chat and the policy has Instant Submit frequency", () => { Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}101`, { reportID: '101', chatType: CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT, isOwnPolicyExpenseChat: true, }).then(() => { + const paidPolicy = { + id: '3f54cca8', + type: CONST.POLICY.TYPE.TEAM, + autoReportingFrequency: CONST.POLICY.AUTO_REPORTING_FREQUENCIES.INSTANT, + }; const report = { ...LHNTestUtils.getFakeReport(), type: CONST.REPORT.TYPE.EXPENSE, stateNum: CONST.REPORT.STATE_NUM.SUBMITTED, statusNum: CONST.REPORT.STATUS_NUM.SUBMITTED, parentReportID: '101', - }; - const paidPolicy = { - type: CONST.POLICY.TYPE.TEAM, + policyID: paidPolicy.id, }; const moneyRequestOptions = ReportUtils.getMoneyRequestOptions(report, paidPolicy, [currentUserAccountID, participantsAccountIDs[0]]); - expect(moneyRequestOptions.length).toBe(0); + expect(moneyRequestOptions.length).toBe(1); }); }); }); @@ -498,7 +501,7 @@ describe('ReportUtils', () => { }); }); - it("it is an open expense report tied to user's own paid policy expense chat", () => { + it("it is an open expense report tied to user's own policy expense chat", () => { Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}103`, { reportID: '103', chatType: CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT, @@ -542,6 +545,30 @@ describe('ReportUtils', () => { expect(moneyRequestOptions.length).toBe(1); expect(moneyRequestOptions.includes(CONST.IOU.TYPE.REQUEST)).toBe(true); }); + + it("it is a submitted expense report in user's own policyExpenseChat and the policy has Instant Submit frequency", () => { + Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}101`, { + reportID: '101', + chatType: CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT, + isOwnPolicyExpenseChat: true, + }).then(() => { + const paidPolicy = { + id: 'ef72dfeb', + type: CONST.POLICY.TYPE.TEAM, + autoReportingFrequency: CONST.POLICY.AUTO_REPORTING_FREQUENCIES.INSTANT, + }; + const report = { + ...LHNTestUtils.getFakeReport(), + type: CONST.REPORT.TYPE.EXPENSE, + stateNum: CONST.REPORT.STATE_NUM.SUBMITTED, + statusNum: CONST.REPORT.STATUS_NUM.SUBMITTED, + parentReportID: '101', + policyID: paidPolicy.id, + }; + const moneyRequestOptions = ReportUtils.getMoneyRequestOptions(report, paidPolicy, [currentUserAccountID, participantsAccountIDs[0]]); + expect(moneyRequestOptions.length).toBe(1); + }); + }); }); describe('return multiple money request option if', () => { From 4e9ccc931831d93af8bed7f8fabc57186ab4fb2b Mon Sep 17 00:00:00 2001 From: Youssef Lourayad Date: Fri, 1 Mar 2024 02:47:37 +0100 Subject: [PATCH 15/22] Fix test --- tests/unit/ReportUtilsTest.js | 49 +++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 22 deletions(-) diff --git a/tests/unit/ReportUtilsTest.js b/tests/unit/ReportUtilsTest.js index f8c830c8f3c8..9fbea1df862e 100644 --- a/tests/unit/ReportUtilsTest.js +++ b/tests/unit/ReportUtilsTest.js @@ -410,17 +410,19 @@ describe('ReportUtils', () => { }); }); - it("it is a submitted report tied to user's own policy expense chat and the policy has Instant Submit frequency", () => { - Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}101`, { - reportID: '101', - chatType: CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT, - isOwnPolicyExpenseChat: true, - }).then(() => { - const paidPolicy = { - id: '3f54cca8', - type: CONST.POLICY.TYPE.TEAM, - autoReportingFrequency: CONST.POLICY.AUTO_REPORTING_FREQUENCIES.INSTANT, - }; + it("it is a submitted report tied to user's own policy expense chat and the policy does not have Instant Submit frequency", () => { + const paidPolicy = { + id: '3f54cca8', + type: CONST.POLICY.TYPE.TEAM, + }; + Promise.all([ + Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${paidPolicy.id}`, paidPolicy), + Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}101`, { + reportID: '101', + chatType: CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT, + isOwnPolicyExpenseChat: true, + }), + ]).then(() => { const report = { ...LHNTestUtils.getFakeReport(), type: CONST.REPORT.TYPE.EXPENSE, @@ -430,7 +432,7 @@ describe('ReportUtils', () => { policyID: paidPolicy.id, }; const moneyRequestOptions = ReportUtils.getMoneyRequestOptions(report, paidPolicy, [currentUserAccountID, participantsAccountIDs[0]]); - expect(moneyRequestOptions.length).toBe(1); + expect(moneyRequestOptions.length).toBe(0); }); }); }); @@ -547,16 +549,19 @@ describe('ReportUtils', () => { }); it("it is a submitted expense report in user's own policyExpenseChat and the policy has Instant Submit frequency", () => { - Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}101`, { - reportID: '101', - chatType: CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT, - isOwnPolicyExpenseChat: true, - }).then(() => { - const paidPolicy = { - id: 'ef72dfeb', - type: CONST.POLICY.TYPE.TEAM, - autoReportingFrequency: CONST.POLICY.AUTO_REPORTING_FREQUENCIES.INSTANT, - }; + const paidPolicy = { + id: 'ef72dfeb', + type: CONST.POLICY.TYPE.TEAM, + autoReportingFrequency: CONST.POLICY.AUTO_REPORTING_FREQUENCIES.INSTANT, + }; + Promise.all([ + Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${paidPolicy.id}`, paidPolicy), + Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}101`, { + reportID: '101', + chatType: CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT, + isOwnPolicyExpenseChat: true, + }), + ]).then(() => { const report = { ...LHNTestUtils.getFakeReport(), type: CONST.REPORT.TYPE.EXPENSE, From c0371b297f370c97b62db9d98ab6b1b71f7e92a0 Mon Sep 17 00:00:00 2001 From: Youssef Lourayad Date: Sun, 3 Mar 2024 13:29:05 +0100 Subject: [PATCH 16/22] Rename variables --- src/libs/ReportUtils.ts | 4 ++-- src/libs/actions/IOU.ts | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index defea52abbd7..c8c3452c720e 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -5037,7 +5037,7 @@ function canBeAutoReimbursed(report: OnyxEntry, policy: OnyxEntry | undefined | null, chatReport: OnyxEntry | null): boolean { +function shouldCreateNewMoneyRequestReport(existingIOUReport: OnyxEntry | undefined | null, chatReport: OnyxEntry | null): boolean { return !existingIOUReport || hasIOUWaitingOnCurrentUserBankAccount(chatReport) || !canAddTransactionsToMoneyRequest(existingIOUReport); } @@ -5243,7 +5243,7 @@ export { canEditPolicyDescription, getPolicyDescriptionText, canAddTransactionsToMoneyRequest, - shouldBuildOptimisticMoneyRequestReport, + shouldCreateNewMoneyRequestReport, }; export type { diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 36dbe666364e..96e97370b09e 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -1833,9 +1833,9 @@ function createSplitsAndOnyxData( // STEP 2: Get existing IOU/Expense report and update its total OR build a new optimistic one let oneOnOneIOUReport: OneOnOneIOUReport = oneOnOneChatReport.iouReportID ? allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${oneOnOneChatReport.iouReportID}`] : null; - const shouldCreateNewMoneyRequestReport = ReportUtils.shouldBuildOptimisticMoneyRequestReport(oneOnOneIOUReport, oneOnOneChatReport); + const shouldCreateNewOneOnOneIOUReport = ReportUtils.shouldBuildOptimisticMoneyRequestReport(oneOnOneIOUReport, oneOnOneChatReport); - if (!oneOnOneIOUReport || shouldCreateNewMoneyRequestReport) { + if (!oneOnOneIOUReport || shouldCreateNewOneOnOneIOUReport) { oneOnOneIOUReport = isOwnPolicyExpenseChat ? ReportUtils.buildOptimisticExpenseReport(oneOnOneChatReport.reportID, oneOnOneChatReport.policyID ?? '', currentUserAccountID, splitAmount, currency) : ReportUtils.buildOptimisticIOUReport(currentUserAccountID, accountID, splitAmount, oneOnOneChatReport.reportID, currency); @@ -1938,7 +1938,7 @@ function createSplitsAndOnyxData( isNewOneOnOneChatReport, optimisticTransactionThread, optimisticCreatedActionForTransactionThread, - shouldCreateNewMoneyRequestReport, + shouldCreateNewOneOnOneIOUReport, ); const individualSplit = { @@ -2471,9 +2471,9 @@ function completeSplitBill(chatReportID: string, reportAction: OnyxTypes.ReportA } let oneOnOneIOUReport: OneOnOneIOUReport = oneOnOneChatReport?.iouReportID ? allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${oneOnOneChatReport.iouReportID}`] : null; - const shouldCreateNewOneOnOneMoneyRequestReport = ReportUtils.shouldBuildOptimisticMoneyRequestReport(oneOnOneIOUReport, oneOnOneChatReport); + const shouldCreateNewOneOnOneIOUReport = ReportUtils.shouldBuildOptimisticMoneyRequestReport(oneOnOneIOUReport, oneOnOneChatReport); - if (!oneOnOneIOUReport || shouldCreateNewOneOnOneMoneyRequestReport) { + if (!oneOnOneIOUReport || shouldCreateNewOneOnOneIOUReport) { oneOnOneIOUReport = isPolicyExpenseChat ? ReportUtils.buildOptimisticExpenseReport(oneOnOneChatReport?.reportID ?? '', participant.policyID ?? '', sessionAccountID, splitAmount, currency ?? '') : ReportUtils.buildOptimisticIOUReport(sessionAccountID, participant.accountID ?? -1, splitAmount, oneOnOneChatReport?.reportID ?? '', currency ?? ''); @@ -2540,7 +2540,7 @@ function completeSplitBill(chatReportID: string, reportAction: OnyxTypes.ReportA isNewOneOnOneChatReport, optimisticTransactionThread, optimisticCreatedActionForTransactionThread, - shouldCreateNewOneOnOneMoneyRequestReport, + shouldCreateNewOneOnOneIOUReport, ); splits.push({ From 8496f5d661c576f590dd11757876f44e5a476942 Mon Sep 17 00:00:00 2001 From: Youssef Lourayad Date: Sun, 3 Mar 2024 13:34:10 +0100 Subject: [PATCH 17/22] Fix calls of functions I missed renaming --- src/libs/actions/IOU.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 662a15738f61..5d92d2086a57 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -834,7 +834,7 @@ function getMoneyRequestInformation( iouReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${chatReport.iouReportID}`] ?? null; } - const shouldCreateNewMoneyRequestReport = ReportUtils.shouldBuildOptimisticMoneyRequestReport(iouReport, chatReport); + const shouldCreateNewMoneyRequestReport = ReportUtils.shouldCreateNewMoneyRequestReport(iouReport, chatReport); if (!iouReport || shouldCreateNewMoneyRequestReport) { iouReport = isPolicyExpenseChat @@ -1833,7 +1833,7 @@ function createSplitsAndOnyxData( // STEP 2: Get existing IOU/Expense report and update its total OR build a new optimistic one let oneOnOneIOUReport: OneOnOneIOUReport = oneOnOneChatReport.iouReportID ? allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${oneOnOneChatReport.iouReportID}`] : null; - const shouldCreateNewOneOnOneIOUReport = ReportUtils.shouldBuildOptimisticMoneyRequestReport(oneOnOneIOUReport, oneOnOneChatReport); + const shouldCreateNewOneOnOneIOUReport = ReportUtils.shouldCreateNewMoneyRequestReport(oneOnOneIOUReport, oneOnOneChatReport); if (!oneOnOneIOUReport || shouldCreateNewOneOnOneIOUReport) { oneOnOneIOUReport = isOwnPolicyExpenseChat @@ -2471,7 +2471,7 @@ function completeSplitBill(chatReportID: string, reportAction: OnyxTypes.ReportA } let oneOnOneIOUReport: OneOnOneIOUReport = oneOnOneChatReport?.iouReportID ? allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${oneOnOneChatReport.iouReportID}`] : null; - const shouldCreateNewOneOnOneIOUReport = ReportUtils.shouldBuildOptimisticMoneyRequestReport(oneOnOneIOUReport, oneOnOneChatReport); + const shouldCreateNewOneOnOneIOUReport = ReportUtils.shouldCreateNewMoneyRequestReport(oneOnOneIOUReport, oneOnOneChatReport); if (!oneOnOneIOUReport || shouldCreateNewOneOnOneIOUReport) { oneOnOneIOUReport = isPolicyExpenseChat From ee17782a6d73008ee9ffcf228d397ad8998f1695 Mon Sep 17 00:00:00 2001 From: Youssef Lourayad Date: Thu, 7 Mar 2024 13:31:24 +0100 Subject: [PATCH 18/22] Apply suggestions from code review --- src/components/MoneyRequestHeader.tsx | 2 +- src/libs/ReportUtils.ts | 22 ++++++++++------------ 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/src/components/MoneyRequestHeader.tsx b/src/components/MoneyRequestHeader.tsx index db14c7ee1898..7f9ab3fe0dc9 100644 --- a/src/components/MoneyRequestHeader.tsx +++ b/src/components/MoneyRequestHeader.tsx @@ -82,7 +82,7 @@ function MoneyRequestHeader({session, parentReport, report, parentReportAction, const canHoldOrUnholdRequest = !isSettled && !isApproved && !isDeletedParentAction; // If the report supports adding transactions to it, then it also supports deleting transactions from it. - const canDeleteRequest = isActionOwner && ReportUtils.canAddTransactionsToMoneyRequest(moneyRequestReport) && !isDeletedParentAction; + const canDeleteRequest = isActionOwner && ReportUtils.canAddOrDeleteTransactions(moneyRequestReport) && !isDeletedParentAction; const changeMoneyRequestStatus = () => { if (isOnHold) { diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 3767c0d09def..e25a1638c2f9 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -1255,16 +1255,16 @@ function getChildReportNotificationPreference(reportAction: OnyxEntry): boolean { - if (!isIOUReport(report) && !isExpenseReport(report)) { +function canAddOrDeleteTransactions(moneyRequest: OnyxEntry): boolean { + if (!isMoneyRequest(moneyRequest)) { return false; } - if (isReportApproved(report) || isSettled(report?.reportID)) { + if (isReportApproved(moneyRequest) || isSettled(moneyRequest?.reportID)) { return false; } - if (isGroupPolicy(report) && isProcessingReport(report) && !PolicyUtils.isInstantSubmitEnabled(getPolicy(report?.policyID))) { + if (isGroupPolicy(moneyRequest) && isProcessingReport(moneyRequest) && !PolicyUtils.isInstantSubmitEnabled(getPolicy(moneyRequest?.policyID))) { return false; } @@ -1285,14 +1285,13 @@ function canDeleteReportAction(reportAction: OnyxEntry, reportID: // For now, users cannot delete split actions const isSplitAction = reportAction?.originalMessage?.type === CONST.IOU.REPORT_ACTION_TYPE.SPLIT; - if (isSplitAction || isSettled(String(reportAction?.originalMessage?.IOUReportID)) || (!isEmptyObject(report) && isReportApproved(report))) { + if (isSplitAction) { return false; } if (isActionOwner) { - if (!isEmptyObject(report) && isPaidGroupPolicyExpenseReport(report)) { - // If the report supports adding transactions to it, then it also supports deleting transactions from it. - return canAddTransactionsToMoneyRequest(report); + if (!isEmptyObject(report) && isMoneyRequest(report)) { + return canAddOrDeleteTransactions(report); } return true; } @@ -2974,7 +2973,6 @@ function buildOptimisticExpenseReport(chatReportID: string, policyID: string, pa const isInstantSubmitEnabled = PolicyUtils.isInstantSubmitEnabled(policy); - // Define the state and status of the report based on whether the policy is free or paid const stateNum = isInstantSubmitEnabled ? CONST.REPORT.STATE_NUM.SUBMITTED : CONST.REPORT.STATE_NUM.OPEN; const statusNum = isInstantSubmitEnabled ? CONST.REPORT.STATUS_NUM.SUBMITTED : CONST.REPORT.STATUS_NUM.OPEN; @@ -4315,7 +4313,7 @@ function canRequestMoney(report: OnyxEntry, policy: OnyxEntry, o // User can request money in any IOU report, unless paid, but user can only request money in an expense report // which is tied to their workspace chat. if (isMoneyRequestReport(report)) { - const canAddTransactions = canAddTransactionsToMoneyRequest(report); + const canAddTransactions = canAddOrDeleteTransactions(report); return isGroupPolicy(report) ? isOwnPolicyExpenseChat && canAddTransactions : canAddTransactions; } @@ -5093,7 +5091,7 @@ function canBeAutoReimbursed(report: OnyxEntry, policy: OnyxEntry | undefined | null, chatReport: OnyxEntry | null): boolean { - return !existingIOUReport || hasIOUWaitingOnCurrentUserBankAccount(chatReport) || !canAddTransactionsToMoneyRequest(existingIOUReport); + return !existingIOUReport || hasIOUWaitingOnCurrentUserBankAccount(chatReport) || !canAddOrDeleteTransactions(existingIOUReport); } export { @@ -5298,7 +5296,7 @@ export { canEditRoomVisibility, canEditPolicyDescription, getPolicyDescriptionText, - canAddTransactionsToMoneyRequest, + canAddOrDeleteTransactions, shouldCreateNewMoneyRequestReport, }; From 6a464b302a4d270fd36782e67f3685636cf18aba Mon Sep 17 00:00:00 2001 From: Youssef Lourayad Date: Thu, 7 Mar 2024 13:40:44 +0100 Subject: [PATCH 19/22] Fix bug, use isMoneyRequestReport --- 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 e25a1638c2f9..c38bc54de441 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -1276,6 +1276,7 @@ function canAddOrDeleteTransactions(moneyRequest: OnyxEntry): boolean { * policy admin */ function canDeleteReportAction(reportAction: OnyxEntry, reportID: string): boolean { + console.log('can delete reportAction'); const report = getReport(reportID); const isActionOwner = reportAction?.actorAccountID === currentUserAccountID; @@ -1290,7 +1291,7 @@ function canDeleteReportAction(reportAction: OnyxEntry, reportID: } if (isActionOwner) { - if (!isEmptyObject(report) && isMoneyRequest(report)) { + if (!isEmptyObject(report) && isMoneyRequestReport(report)) { return canAddOrDeleteTransactions(report); } return true; From 1777c6808fa77025729ea6c78d6ac12d43404f4b Mon Sep 17 00:00:00 2001 From: Youssef Lourayad Date: Thu, 7 Mar 2024 13:41:20 +0100 Subject: [PATCH 20/22] Cleanup --- src/libs/ReportUtils.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index c38bc54de441..904df1df95a9 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -1276,7 +1276,6 @@ function canAddOrDeleteTransactions(moneyRequest: OnyxEntry): boolean { * policy admin */ function canDeleteReportAction(reportAction: OnyxEntry, reportID: string): boolean { - console.log('can delete reportAction'); const report = getReport(reportID); const isActionOwner = reportAction?.actorAccountID === currentUserAccountID; From 9d9caf620b76837ea8f1faa37fe2efd64c9821c1 Mon Sep 17 00:00:00 2001 From: Youssef Lourayad Date: Thu, 7 Mar 2024 13:42:09 +0100 Subject: [PATCH 21/22] Rename variable --- 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 904df1df95a9..a32b1b872537 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -1255,16 +1255,16 @@ function getChildReportNotificationPreference(reportAction: OnyxEntry): boolean { - if (!isMoneyRequest(moneyRequest)) { +function canAddOrDeleteTransactions(moneyRequestReport: OnyxEntry): boolean { + if (!isMoneyRequest(moneyRequestReport)) { return false; } - if (isReportApproved(moneyRequest) || isSettled(moneyRequest?.reportID)) { + if (isReportApproved(moneyRequestReport) || isSettled(moneyRequestReport?.reportID)) { return false; } - if (isGroupPolicy(moneyRequest) && isProcessingReport(moneyRequest) && !PolicyUtils.isInstantSubmitEnabled(getPolicy(moneyRequest?.policyID))) { + if (isGroupPolicy(moneyRequestReport) && isProcessingReport(moneyRequestReport) && !PolicyUtils.isInstantSubmitEnabled(getPolicy(moneyRequestReport?.policyID))) { return false; } From 3fcd191fa4b02a0d12ed44ac4a981a2e7f0ee01f Mon Sep 17 00:00:00 2001 From: Youssef Lourayad Date: Thu, 7 Mar 2024 15:55:45 +0100 Subject: [PATCH 22/22] Fix wrong helper method used --- 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 a32b1b872537..31f4999063ab 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -1256,7 +1256,7 @@ function getChildReportNotificationPreference(reportAction: OnyxEntry): boolean { - if (!isMoneyRequest(moneyRequestReport)) { + if (!isMoneyRequestReport(moneyRequestReport)) { return false; }