From 365429d6d7d834f0a533cf838a15b8d6e344de45 Mon Sep 17 00:00:00 2001 From: Pavlo Tsimura Date: Wed, 3 Apr 2024 19:14:14 +0200 Subject: [PATCH] Simplify getOptimisticOutstandingChildRequest --- src/libs/ReportUtils.ts | 22 +++++++--------------- src/libs/actions/IOU.ts | 10 ++++++---- 2 files changed, 13 insertions(+), 19 deletions(-) diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 0c79afe1de4c..76b7d532691e 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -438,10 +438,6 @@ type AncestorIDs = { type MissingPaymentMethod = 'bankAccount' | 'wallet'; -type OutstandingChildRequest = { - hasOutstandingChildRequest?: boolean; -}; - let currentUserEmail: string | undefined; let currentUserPrivateDomain: string | undefined; let currentUserAccountID: number | undefined; @@ -5689,29 +5685,25 @@ function hasActionsWithErrors(reportID: string): boolean { } /** - * @returns the object to update `report.hasOutstandingChildRequest` + * @returns the optimistically calculated `hasOutstandingChildRequest` for the report (`null` if the value cannot be calculated). */ -function getOutstandingChildRequest(iouReport: OnyxEntry | EmptyObject): OutstandingChildRequest { +function getOptimisticOutstandingChildRequest(iouReport: OnyxEntry | EmptyObject): boolean | null { if (!iouReport || isEmptyObject(iouReport)) { - return {}; + return null; } if (!isExpenseReport(iouReport)) { - return { - hasOutstandingChildRequest: iouReport.managerID === currentUserAccountID && iouReport.total !== 0, - }; + return iouReport.managerID === currentUserAccountID && iouReport.total !== 0; } const policy = getPolicy(iouReport.policyID); const shouldBeManuallySubmitted = PolicyUtils.isPaidGroupPolicy(policy) && !policy?.harvesting?.enabled; if (shouldBeManuallySubmitted || PolicyUtils.isPolicyAdmin(policy)) { - return { - hasOutstandingChildRequest: true, - }; + return true; } // We don't need to update hasOutstandingChildRequest in this case - return {}; + return null; } export { @@ -5939,7 +5931,7 @@ export { isTrackExpenseReport, hasActionsWithErrors, getGroupChatName, - getOutstandingChildRequest, + getOptimisticOutstandingChildRequest, }; export type { diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 9b53ad067271..54dce719012d 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -452,7 +452,7 @@ function buildOnyxDataForMoneyRequest( isOneOnOneSplit = false, ): [OnyxUpdate[], OnyxUpdate[], OnyxUpdate[]] { const isScanRequest = TransactionUtils.isScanRequest(transaction); - const outstandingChildRequest = ReportUtils.getOutstandingChildRequest(iouReport); + const outstandingChildRequest = ReportUtils.getOptimisticOutstandingChildRequest(iouReport); const clearedPendingFields = Object.fromEntries(Object.keys(transaction.pendingFields ?? {}).map((key) => [key, null])); const optimisticData: OnyxUpdate[] = []; let newQuickAction: ValueOf = isScanRequest ? CONST.QUICK_ACTIONS.REQUEST_SCAN : CONST.QUICK_ACTIONS.REQUEST_MANUAL; @@ -470,7 +470,7 @@ function buildOnyxDataForMoneyRequest( lastReadTime: DateUtils.getDBTime(), lastMessageTranslationKey: '', iouReportID: iouReport.reportID, - ...outstandingChildRequest, + ...(outstandingChildRequest !== null ? {hasOutstandingChildRequest: outstandingChildRequest} : {}), ...(isNewChatReport ? {pendingFields: {createChat: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD}} : {}), }, }); @@ -1542,6 +1542,7 @@ function getUpdateMoneyRequestParams( updatedMoneyRequestReport.cachedTotal = CurrencyUtils.convertToDisplayString(updatedMoneyRequestReport.total, transactionDetails?.currency); + const optimisticOutstandingChildRequest = ReportUtils.getOptimisticOutstandingChildRequest(updatedMoneyRequestReport); optimisticData.push( { onyxMethod: Onyx.METHOD.MERGE, @@ -1551,7 +1552,7 @@ function getUpdateMoneyRequestParams( { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport?.parentReportID}`, - value: ReportUtils.getOutstandingChildRequest(updatedMoneyRequestReport), + value: optimisticOutstandingChildRequest !== null ? {hasOutstandingChildRequest: optimisticOutstandingChildRequest} : {}, }, ); successData.push({ @@ -3714,6 +3715,7 @@ function deleteMoneyRequest(transactionID: string, reportAction: OnyxTypes.Repor ); } + const optimisticOutstandingChildRequest = ReportUtils.getOptimisticOutstandingChildRequest(updatedIOUReport); optimisticData.push( { onyxMethod: Onyx.METHOD.MERGE, @@ -3735,7 +3737,7 @@ function deleteMoneyRequest(transactionID: string, reportAction: OnyxTypes.Repor { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT}${chatReport?.reportID}`, - value: ReportUtils.getOutstandingChildRequest(updatedIOUReport), + value: optimisticOutstandingChildRequest !== null ? {hasOutstandingChildRequest: optimisticOutstandingChildRequest} : {}, }, );