diff --git a/src/libs/OptionsListUtils.ts b/src/libs/OptionsListUtils.ts index 98274e829001..2437b5cd1236 100644 --- a/src/libs/OptionsListUtils.ts +++ b/src/libs/OptionsListUtils.ts @@ -53,7 +53,6 @@ import * as PersonalDetailsUtils from './PersonalDetailsUtils'; import * as PhoneNumber from './PhoneNumber'; import * as PolicyUtils from './PolicyUtils'; import * as ReportActionUtils from './ReportActionsUtils'; -import * as ReportConnection from './ReportConnection'; import * as ReportUtils from './ReportUtils'; import * as TaskUtils from './TaskUtils'; import * as TransactionUtils from './TransactionUtils'; @@ -351,35 +350,12 @@ Onyx.connect({ }, {}); }, }); - -let allReportsDraft: OnyxCollection; -Onyx.connect({ - key: ONYXKEYS.COLLECTION.REPORT_DRAFT, - waitForCollectionCallback: true, - callback: (value) => (allReportsDraft = value), -}); - let activePolicyID: OnyxEntry; Onyx.connect({ key: ONYXKEYS.NVP_ACTIVE_POLICY_ID, callback: (value) => (activePolicyID = value), }); -/** - * Get the report or draft report given a reportID - */ -function getReportOrDraftReport(reportID: string | undefined): OnyxEntry { - const allReports = ReportConnection.getAllReports(); - if (!allReports && !allReportsDraft) { - return undefined; - } - - const report = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`]; - const draftReport = allReportsDraft?.[`${ONYXKEYS.COLLECTION.REPORT_DRAFT}${reportID}`]; - - return report ?? draftReport; -} - /** * @param defaultValues {login: accountID} In workspace invite page, when new user is added we pass available data to opt in * @returns Returns avatar data for a list of user accountIDs @@ -578,7 +554,7 @@ function getLastActorDisplayName(lastActorDetails: Partial | nu * Update alternate text for the option when applicable */ function getAlternateText(option: ReportUtils.OptionData, {showChatPreviewLine = false, forcePolicyNamePreview = false}: PreviewConfig) { - const report = getReportOrDraftReport(option.reportID); + const report = ReportUtils.getReportOrDraftReport(option.reportID); const isAdminRoom = ReportUtils.isAdminRoom(report); const isAnnounceRoom = ReportUtils.isAnnounceRoom(report); @@ -634,7 +610,7 @@ function getIOUReportIDOfLastAction(report: OnyxEntry): string | undefin if (!ReportActionUtils.isReportPreviewAction(lastAction)) { return; } - return getReportOrDraftReport(ReportActionUtils.getIOUReportIDFromReportActionPreview(lastAction))?.reportID; + return ReportUtils.getReportOrDraftReport(ReportActionUtils.getIOUReportIDFromReportActionPreview(lastAction))?.reportID; } /** @@ -675,7 +651,7 @@ function getLastMessageTextForReport(report: OnyxEntry, lastActorDetails const properSchemaForMoneyRequestMessage = ReportUtils.getReportPreviewMessage(report, lastReportAction, true, false, null, true); lastMessageTextFromReport = ReportUtils.formatReportLastMessageText(properSchemaForMoneyRequestMessage); } else if (ReportActionUtils.isReportPreviewAction(lastReportAction)) { - const iouReport = getReportOrDraftReport(ReportActionUtils.getIOUReportIDFromReportActionPreview(lastReportAction)); + const iouReport = ReportUtils.getReportOrDraftReport(ReportActionUtils.getIOUReportIDFromReportActionPreview(lastReportAction)); const lastIOUMoneyReportAction = allSortedReportActions[iouReport?.reportID ?? '-1']?.find( (reportAction, key): reportAction is ReportAction => ReportActionUtils.shouldReportActionBeVisible(reportAction, key) && @@ -862,7 +838,7 @@ function createOption( * Get the option for a given report. */ function getReportOption(participant: Participant): ReportUtils.OptionData { - const report = getReportOrDraftReport(participant.reportID); + const report = ReportUtils.getReportOrDraftReport(participant.reportID); const visibleParticipantAccountIDs = ReportUtils.getParticipantsAccountIDsForDisplay(report, true); const option = createOption( @@ -896,7 +872,7 @@ function getReportOption(participant: Participant): ReportUtils.OptionData { * Get the option for a policy expense report. */ function getPolicyExpenseReportOption(participant: Participant | ReportUtils.OptionData): ReportUtils.OptionData { - const expenseReport = ReportUtils.isPolicyExpenseChat(participant) ? getReportOrDraftReport(participant.reportID) : null; + const expenseReport = ReportUtils.isPolicyExpenseChat(participant) ? ReportUtils.getReportOrDraftReport(participant.reportID) : null; const visibleParticipantAccountIDs = Object.entries(expenseReport?.participants ?? {}) .filter(([, reportParticipant]) => reportParticipant && !reportParticipant.hidden) diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 822ac6bb699f..bd3e2147752d 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -658,14 +658,7 @@ function getChatType(report: OnyxInputOrEntry | Participant): ValueOf { const allReports = ReportConnection.getAllReports(); - if (!allReports && !allReportsDraft) { - return undefined; - } - - const report = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`]; - const draftReport = allReportsDraft?.[`${ONYXKEYS.COLLECTION.REPORT_DRAFT}${reportID}`]; - - return report ?? draftReport; + return allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`] ?? allReportsDraft?.[`${ONYXKEYS.COLLECTION.REPORT_DRAFT}${reportID}`]; } /** @@ -7826,6 +7819,7 @@ export { getReportParticipantsTitle, getReportPreviewMessage, getReportRecipientAccountIDs, + getReportOrDraftReport, getRoom, getRootParentReport, getRouteFromLink, diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 9cc844849c89..f3e58bc3474e 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -161,13 +161,6 @@ Onyx.connect({ }, }); -let allReportsDraft: OnyxCollection; -Onyx.connect({ - key: ONYXKEYS.COLLECTION.REPORT_DRAFT, - waitForCollectionCallback: true, - callback: (value) => (allReportsDraft = value), -}); - let allTransactions: NonNullable> = {}; Onyx.connect({ key: ONYXKEYS.COLLECTION.TRANSACTION, @@ -275,21 +268,6 @@ Onyx.connect({ callback: (value) => (activePolicyID = value), }); -/** - * Get the report or draft report given a reportID - */ -function getReportOrDraftReport(reportID: string | undefined): OnyxEntry { - const allReports = ReportConnection.getAllReports(); - if (!allReports && !allReportsDraft) { - return undefined; - } - - const report = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`]; - const draftReport = allReportsDraft?.[`${ONYXKEYS.COLLECTION.REPORT_DRAFT}${reportID}`]; - - return report ?? draftReport; -} - /** * Find the report preview action from given chat report and iou report */ @@ -3474,7 +3452,7 @@ function requestMoney( ) { // If the report is iou or expense report, we should get the linked chat report to be passed to the getMoneyRequestInformation function const isMoneyRequestReport = ReportUtils.isMoneyRequestReport(report); - const currentChatReport = isMoneyRequestReport ? getReportOrDraftReport(report?.chatReportID) : report; + const currentChatReport = isMoneyRequestReport ? ReportUtils.getReportOrDraftReport(report?.chatReportID) : report; const moneyRequestReportID = isMoneyRequestReport ? report?.reportID : ''; const isMovingTransactionFromTrackExpense = IOUUtils.isMovingTransactionFromTrackExpense(action); @@ -3659,7 +3637,7 @@ function trackExpense( linkedTrackedExpenseReportID?: string, ) { const isMoneyRequestReport = ReportUtils.isMoneyRequestReport(report); - const currentChatReport = isMoneyRequestReport ? getReportOrDraftReport(report.chatReportID) : report; + const currentChatReport = isMoneyRequestReport ? ReportUtils.getReportOrDraftReport(report.chatReportID) : report; const moneyRequestReportID = isMoneyRequestReport ? report.reportID : ''; const isMovingTransactionFromTrackExpense = IOUUtils.isMovingTransactionFromTrackExpense(action); @@ -5032,7 +5010,7 @@ function createDistanceRequest( ) { // If the report is an iou or expense report, we should get the linked chat report to be passed to the getMoneyRequestInformation function const isMoneyRequestReport = ReportUtils.isMoneyRequestReport(report); - const currentChatReport = isMoneyRequestReport ? getReportOrDraftReport(report?.chatReportID) : report; + const currentChatReport = isMoneyRequestReport ? ReportUtils.getReportOrDraftReport(report?.chatReportID) : report; const moneyRequestReportID = isMoneyRequestReport ? report?.reportID : ''; const optimisticReceipt: Receipt = { @@ -6417,7 +6395,7 @@ function getReportFromHoldRequestsOnyxData( pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, }; - const heldReport = getReportOrDraftReport(holdReportAction.childReportID); + const heldReport = ReportUtils.getReportOrDraftReport(holdReportAction.childReportID); if (heldReport) { optimisticHoldReportExpenseActionIDs.push({optimisticReportActionID: reportActionID, oldReportActionID: holdReportAction.reportActionID}); @@ -6860,7 +6838,7 @@ function hasIOUToApproveOrPay(chatReport: OnyxEntry, excludedI const chatReportActions = allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport?.reportID}`] ?? {}; return Object.values(chatReportActions).some((action) => { - const iouReport = getReportOrDraftReport(action.childReportID ?? '-1'); + const iouReport = ReportUtils.getReportOrDraftReport(action.childReportID ?? '-1'); const policy = PolicyUtils.getPolicy(iouReport?.policyID); const shouldShowSettlementButton = canIOUBePaid(iouReport, chatReport, policy) || canApproveIOU(iouReport, chatReport, policy); return action.childReportID?.toString() !== excludedIOUReportID && action.actionName === CONST.REPORT.ACTIONS.TYPE.REPORT_PREVIEW && shouldShowSettlementButton; @@ -6894,7 +6872,7 @@ function approveMoneyRequest(expenseReport: OnyxEntry, full?: const predictedNextState = isLastApprover(approvalChain) ? CONST.REPORT.STATE_NUM.APPROVED : CONST.REPORT.STATE_NUM.SUBMITTED; const optimisticNextStep = NextStepUtils.buildNextStep(expenseReport, predictedNextStatus); - const chatReport = getReportOrDraftReport(expenseReport?.chatReportID); + const chatReport = ReportUtils.getReportOrDraftReport(expenseReport?.chatReportID); const optimisticReportActionsData: OnyxUpdate = { onyxMethod: Onyx.METHOD.MERGE, @@ -7131,7 +7109,7 @@ function submitReport(expenseReport: OnyxTypes.Report) { } const currentNextStep = allNextSteps[`${ONYXKEYS.COLLECTION.NEXT_STEP}${expenseReport.reportID}`] ?? null; - const parentReport = getReportOrDraftReport(expenseReport.parentReportID); + const parentReport = ReportUtils.getReportOrDraftReport(expenseReport.parentReportID); const policy = PolicyUtils.getPolicy(expenseReport.policyID); const isCurrentUserManager = currentUserPersonalDetails?.accountID === expenseReport.managerID; const isSubmitAndClosePolicy = PolicyUtils.isSubmitAndClose(policy); @@ -7479,7 +7457,7 @@ function replaceReceipt(transactionID: string, file: File, source: string) { */ function setMoneyRequestParticipantsFromReport(transactionID: string, report: OnyxEntry): Participant[] { // If the report is iou or expense report, we should get the chat report to set participant for request money - const chatReport = ReportUtils.isMoneyRequestReport(report) ? getReportOrDraftReport(report?.chatReportID) : report; + const chatReport = ReportUtils.isMoneyRequestReport(report) ? ReportUtils.getReportOrDraftReport(report?.chatReportID) : report; const currentUserAccountID = currentUserPersonalDetails?.accountID; const shouldAddAsReport = !isEmptyObject(chatReport) && ReportUtils.isSelfDM(chatReport); let participants: Participant[] = []; diff --git a/tests/actions/EnforceActionExportRestrictions.ts b/tests/actions/EnforceActionExportRestrictions.ts index bd2a13304078..9590f878116e 100644 --- a/tests/actions/EnforceActionExportRestrictions.ts +++ b/tests/actions/EnforceActionExportRestrictions.ts @@ -28,11 +28,6 @@ describe('ReportUtils', () => { // @ts-expect-error the test is asserting that it's undefined, so the TS error is normal expect(ReportUtils.getAllReportActions).toBeUndefined(); }); - - it('does not export getReport', () => { - // @ts-expect-error the test is asserting that it's undefined, so the TS error is normal - expect(ReportUtils.getReportOrDraftReport).toBeUndefined(); - }); }); describe('Policy', () => {