diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index c0c90143a67a..26f19ceb8f95 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -439,6 +439,10 @@ type AncestorIDs = { type MissingPaymentMethod = 'bankAccount' | 'wallet'; +type OutstandingChildRequest = { + hasOutstandingChildRequest?: boolean; +}; + let currentUserEmail: string | undefined; let currentUserPrivateDomain: string | undefined; let currentUserAccountID: number | undefined; @@ -5706,6 +5710,32 @@ function hasActionsWithErrors(reportID: string): boolean { return Object.values(reportActions ?? {}).some((action) => !isEmptyObject(action.errors)); } +/** + * @returns the object to update `report.hasOutstandingChildRequest` + */ +function getOutstandingChildRequest(iouReport: OnyxEntry | EmptyObject): OutstandingChildRequest { + if (!iouReport || isEmptyObject(iouReport)) { + return {}; + } + + if (!isExpenseReport(iouReport)) { + return { + hasOutstandingChildRequest: 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, + }; + } + + // We don't need to update hasOutstandingChildRequest in this case + return {}; +} + export { getReportParticipantsTitle, isReportMessageAttachment, @@ -5931,6 +5961,7 @@ export { isTrackExpenseReport, hasActionsWithErrors, getGroupChatName, + getOutstandingChildRequest, }; export type { diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 5c92cd87a2bc..f145819f50c4 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -127,10 +127,6 @@ type SendMoneyParamsData = { failureData: OnyxUpdate[]; }; -type OutstandingChildRequest = { - hasOutstandingChildRequest?: boolean; -}; - let betas: OnyxTypes.Beta[] = []; Onyx.connect({ key: ONYXKEYS.BETAS, @@ -450,41 +446,6 @@ function getReceiptError(receipt?: Receipt, filename?: string, isScanRequest = t : ErrorUtils.getMicroSecondOnyxErrorObject({error: CONST.IOU.RECEIPT_ERROR, source: receipt.source?.toString() ?? '', filename: filename ?? ''}, errorKey); } -function needsToBeManuallySubmitted(iouReport: OnyxTypes.Report) { - const isPolicyExpenseChat = ReportUtils.isExpenseReport(iouReport); - - if (isPolicyExpenseChat) { - const policy = getPolicy(iouReport.policyID); - const isFromPaidPolicy = PolicyUtils.isPaidGroupPolicy(policy); - - // If the scheduled submit is turned off on the policy, user needs to manually submit the report which is indicated by GBR in LHN - return isFromPaidPolicy && !policy.harvesting?.enabled; - } - - return true; -} - -/** - * Return the object to update hasOutstandingChildRequest - */ -function getOutstandingChildRequest(policy: OnyxEntry | EmptyObject, iouReport: OnyxTypes.Report): OutstandingChildRequest { - if (!needsToBeManuallySubmitted(iouReport)) { - return { - hasOutstandingChildRequest: false, - }; - } - - if (PolicyUtils.isPolicyAdmin(policy)) { - return { - hasOutstandingChildRequest: true, - }; - } - - return { - hasOutstandingChildRequest: iouReport.managerID === userAccountID && iouReport.total !== 0, - }; -} - /** Builds the Onyx data for a money request */ function buildOnyxDataForMoneyRequest( chatReport: OnyxEntry, @@ -508,7 +469,7 @@ function buildOnyxDataForMoneyRequest( isOneOnOneSplit = false, ): [OnyxUpdate[], OnyxUpdate[], OnyxUpdate[]] { const isScanRequest = TransactionUtils.isScanRequest(transaction); - const outstandingChildRequest = getOutstandingChildRequest(policy ?? {}, iouReport); + const outstandingChildRequest = ReportUtils.getOutstandingChildRequest(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; @@ -1581,17 +1542,21 @@ function getUpdateMoneyRequestParams( } // Step 4: Compute the IOU total and update the report preview message (and report header) so LHN amount owed is correct. - let updatedMoneyRequestReport = {...iouReport}; const diff = calculateDiffAmount(iouReport, updatedTransaction, transaction); - if (ReportUtils.isExpenseReport(iouReport) && typeof updatedMoneyRequestReport.total === 'number') { - // For expense report, the amount is negative so we should subtract total from diff - updatedMoneyRequestReport.total -= diff; + let updatedMoneyRequestReport: OnyxTypes.Report | EmptyObject; + if (!iouReport) { + updatedMoneyRequestReport = {}; + } else if (ReportUtils.isExpenseReport(iouReport) && typeof iouReport.total === 'number') { + // For expense report, the amount is negative, so we should subtract total from diff + updatedMoneyRequestReport = { + ...iouReport, + total: iouReport.total - diff, + }; } else { - updatedMoneyRequestReport = iouReport - ? IOUUtils.updateIOUOwnerAndTotal(iouReport, updatedReportAction.actorAccountID ?? -1, diff, TransactionUtils.getCurrency(transaction), false, true) - : {}; + updatedMoneyRequestReport = IOUUtils.updateIOUOwnerAndTotal(iouReport, updatedReportAction.actorAccountID ?? -1, diff, TransactionUtils.getCurrency(transaction), false, true); } + updatedMoneyRequestReport.cachedTotal = CurrencyUtils.convertToDisplayString(updatedMoneyRequestReport.total, transactionDetails?.currency); optimisticData.push( @@ -1603,10 +1568,7 @@ function getUpdateMoneyRequestParams( { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport?.parentReportID}`, - value: { - hasOutstandingChildRequest: - iouReport && needsToBeManuallySubmitted(iouReport) && updatedMoneyRequestReport.managerID === userAccountID && updatedMoneyRequestReport.total !== 0, - }, + value: ReportUtils.getOutstandingChildRequest(updatedMoneyRequestReport), }, ); successData.push({ @@ -3790,9 +3752,7 @@ function deleteMoneyRequest(transactionID: string, reportAction: OnyxTypes.Repor { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT}${chatReport?.reportID}`, - value: { - hasOutstandingChildRequest: iouReport && needsToBeManuallySubmitted(iouReport) && updatedIOUReport?.managerID === userAccountID && updatedIOUReport.total !== 0, - }, + value: ReportUtils.getOutstandingChildRequest(updatedIOUReport), }, );