diff --git a/src/libs/OptionsListUtils.ts b/src/libs/OptionsListUtils.ts index f2cdc03bb472..7e2123a91b3d 100644 --- a/src/libs/OptionsListUtils.ts +++ b/src/libs/OptionsListUtils.ts @@ -77,6 +77,7 @@ import { canUserPerformWriteAction, formatReportLastMessageText, getAllReportErrors, + getChatByParticipants, getChatRoomSubtitle, getDeletedParentActionMessageForChatReport, getDisplayNameForParticipant, @@ -97,7 +98,6 @@ import { getReportAutomaticallySubmittedMessage, getReportLastMessage, getReportName, - getReportNameValuePairs, getReportNotificationPreference, getReportOrDraftReport, getReportParticipantsTitle, @@ -355,6 +355,15 @@ Onyx.connect({ }, }); +let allReportNameValuePairs: OnyxCollection; +Onyx.connect({ + key: ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS, + waitForCollectionCallback: true, + callback: (value) => { + allReportNameValuePairs = value; + }, +}); + const lastReportActions: ReportActions = {}; const allSortedReportActions: Record = {}; let allReportActions: OnyxCollection; @@ -859,7 +868,7 @@ function createOption( result.participantsList = personalDetailList; result.isOptimisticPersonalDetail = personalDetail?.isOptimisticPersonalDetail; if (report) { - const reportNameValuePairs = getReportNameValuePairs(report.reportID); + const reportNameValuePairs = allReportNameValuePairs?.[`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${report.reportID}`]; result.isChatRoom = reportUtilsIsChatRoom(report); result.isDefaultRoom = isDefaultRoom(report); result.private_isArchived = reportNameValuePairs?.private_isArchived; @@ -2300,7 +2309,9 @@ function shouldUseBoldText(report: OptionData): boolean { function getManagerMcTestParticipant(): Participant | undefined { const managerMcTestPersonalDetails = Object.values(allPersonalDetails ?? {}).find((personalDetails) => personalDetails?.login === CONST.EMAIL.MANAGER_MCTEST); - return managerMcTestPersonalDetails ? getParticipantsOption(managerMcTestPersonalDetails, allPersonalDetails) : undefined; + const managerMcTestReport = + managerMcTestPersonalDetails?.accountID && currentUserAccountID ? getChatByParticipants([managerMcTestPersonalDetails?.accountID, currentUserAccountID]) : undefined; + return managerMcTestPersonalDetails ? {...getParticipantsOption(managerMcTestPersonalDetails, allPersonalDetails), reportID: managerMcTestReport?.reportID} : undefined; } export { diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index e3d9433340d2..34fb41d8d15a 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -123,7 +123,6 @@ import { getOutstandingChildRequest, getParsedComment, getPersonalDetailsForAccountID, - getReportNameValuePairs, getReportNotificationPreference, getReportOrDraftReport, getReportTransactions, @@ -667,6 +666,15 @@ Onyx.connect({ }, }); +let allReportNameValuePairs: OnyxCollection; +Onyx.connect({ + key: ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS, + waitForCollectionCallback: true, + callback: (value) => { + allReportNameValuePairs = value; + }, +}); + let userAccountID = -1; let currentUserEmail = ''; Onyx.connect({ @@ -937,8 +945,8 @@ function setMoneyRequestBillable(transactionID: string, billable: boolean) { Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${transactionID}`, {billable}); } -function setMoneyRequestParticipants(transactionID: string, participants: Participant[] = []) { - Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${transactionID}`, {participants}); +function setMoneyRequestParticipants(transactionID: string, participants: Participant[] = [], shouldUpdateReportID = false) { + Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${transactionID}`, {participants, reportID: shouldUpdateReportID ? participants.at(0)?.reportID : undefined}); } function setSplitPayer(transactionID: string, payerAccountID: number) { @@ -8674,7 +8682,7 @@ function canApproveIOU(iouReport: OnyxTypes.OnyxInputOrEntry | const isOpenExpenseReport = isOpenExpenseReportReportUtils(iouReport); const isApproved = isReportApproved({report: iouReport}); const iouSettled = isSettled(iouReport); - const reportNameValuePairs = getReportNameValuePairs(iouReport?.reportID); + const reportNameValuePairs = allReportNameValuePairs?.[`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${iouReport?.reportID}`]; const isArchivedExpenseReport = isArchivedReport(reportNameValuePairs); const reportTransactions = getReportTransactions(iouReport?.reportID); const hasOnlyPendingCardOrScanningTransactions = reportTransactions.length > 0 && reportTransactions.every(isPendingCardOrScanningTransaction); @@ -8709,7 +8717,7 @@ function canIOUBePaid( shouldCheckApprovedState = true, ) { const isPolicyExpenseChat = isPolicyExpenseChatReportUtil(chatReport); - const reportNameValuePairs = chatReportRNVP ?? getReportNameValuePairs(chatReport?.reportID); + const reportNameValuePairs = chatReportRNVP ?? allReportNameValuePairs?.[`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${chatReport?.reportID}`]; const isChatReportArchived = isArchivedReport(reportNameValuePairs); const iouSettled = isSettled(iouReport); diff --git a/src/pages/iou/request/step/IOURequestStepScan/index.native.tsx b/src/pages/iou/request/step/IOURequestStepScan/index.native.tsx index 15253a6c5fb7..fca429a74da6 100644 --- a/src/pages/iou/request/step/IOURequestStepScan/index.native.tsx +++ b/src/pages/iou/request/step/IOURequestStepScan/index.native.tsx @@ -43,7 +43,7 @@ import Navigation from '@libs/Navigation/Navigation'; import {getIsUserSubmittedExpenseOrScannedReceipt, getManagerMcTestParticipant, getParticipantsOption, getReportOption} from '@libs/OptionsListUtils'; import Permissions from '@libs/Permissions'; import {isPaidGroupPolicy} from '@libs/PolicyUtils'; -import {getPolicyExpenseChat, isArchivedReport, isPolicyExpenseChat} from '@libs/ReportUtils'; +import {generateReportID, getPolicyExpenseChat, isArchivedReport, isPolicyExpenseChat} from '@libs/ReportUtils'; import playSound, {SOUNDS} from '@libs/Sound'; import {shouldRestrictUserBillableActions} from '@libs/SubscriptionUtils'; import {getDefaultTaxCode} from '@libs/TransactionUtils'; @@ -94,15 +94,15 @@ function IOURequestStepScan({ const [startLocationPermissionFlow, setStartLocationPermissionFlow] = useState(false); const [fileResize, setFileResize] = useState(null); const [fileSource, setFileSource] = useState(''); - const [reportNameValuePairs] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${report?.reportID}`); + const [reportNameValuePairs] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${report?.reportID}`, {canBeMissing: true}); const policy = usePolicy(report?.policyID); - const [personalDetails] = useOnyx(ONYXKEYS.PERSONAL_DETAILS_LIST); - const [skipConfirmation] = useOnyx(`${ONYXKEYS.COLLECTION.SKIP_CONFIRMATION}${transactionID}`); - const [activePolicyID] = useOnyx(ONYXKEYS.NVP_ACTIVE_POLICY_ID); - const [activePolicy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${activePolicyID}`); - const [betas] = useOnyx(ONYXKEYS.BETAS); + const [personalDetails] = useOnyx(ONYXKEYS.PERSONAL_DETAILS_LIST, {canBeMissing: false}); + const [skipConfirmation] = useOnyx(`${ONYXKEYS.COLLECTION.SKIP_CONFIRMATION}${transactionID}`, {canBeMissing: true}); + const [activePolicyID] = useOnyx(ONYXKEYS.NVP_ACTIVE_POLICY_ID, {canBeMissing: false}); + const [activePolicy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${activePolicyID}`, {canBeMissing: true}); + const [betas] = useOnyx(ONYXKEYS.BETAS, {canBeMissing: false}); const platform = getPlatform(true); - const [mutedPlatforms = {}] = useOnyx(ONYXKEYS.NVP_MUTED_PLATFORMS); + const [mutedPlatforms = {}] = useOnyx(ONYXKEYS.NVP_MUTED_PLATFORMS, {canBeMissing: true}); const isPlatformMuted = mutedPlatforms[platform]; const [cameraPermissionStatus, setCameraPermissionStatus] = useState(null); const [didCapturePhoto, setDidCapturePhoto] = useState(false); @@ -255,7 +255,7 @@ function IOURequestStepScan({ }, [iouType, reportID, transactionID]); const navigateToConfirmationPage = useCallback( - (isTestTransaction = false) => { + (isTestTransaction = false, reportIDParam: string | undefined = undefined) => { switch (iouType) { case CONST.IOU.TYPE.REQUEST: Navigation.navigate(ROUTES.MONEY_REQUEST_STEP_CONFIRMATION.getRoute(CONST.IOU.ACTION.CREATE, CONST.IOU.TYPE.SUBMIT, transactionID, reportID)); @@ -265,7 +265,13 @@ function IOURequestStepScan({ break; default: Navigation.navigate( - ROUTES.MONEY_REQUEST_STEP_CONFIRMATION.getRoute(CONST.IOU.ACTION.CREATE, isTestTransaction ? CONST.IOU.TYPE.SUBMIT : iouType, transactionID, reportID), + ROUTES.MONEY_REQUEST_STEP_CONFIRMATION.getRoute( + CONST.IOU.ACTION.CREATE, + isTestTransaction ? CONST.IOU.TYPE.SUBMIT : iouType, + transactionID, + // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing + reportIDParam || reportID, + ), ); } }, @@ -319,6 +325,17 @@ function IOURequestStepScan({ return; } + if (isTestTransaction) { + const managerMcTestParticipant = getManagerMcTestParticipant() ?? {}; + let reportIDParam = managerMcTestParticipant.reportID; + if (!managerMcTestParticipant.reportID && report?.reportID) { + reportIDParam = generateReportID(); + } + setMoneyRequestParticipants(transactionID, [{...managerMcTestParticipant, reportID: reportIDParam, selected: true}], true); + navigateToConfirmationPage(true, reportIDParam); + return; + } + // If a reportID exists in the report object, it's because either: // - The user started this flow from using the + button in the composer inside a report. // - The user started this flow from using the global create menu by selecting the Track expense option. @@ -451,12 +468,6 @@ function IOURequestStepScan({ ); }); } else { - if (isTestTransaction) { - const managerMcTestParticipant = getManagerMcTestParticipant() ?? {}; - setMoneyRequestParticipants(transactionID, [{...managerMcTestParticipant, selected: true}]); - navigateToConfirmationPage(true); - return; - } navigateToParticipantPage(); } }, diff --git a/src/pages/iou/request/step/IOURequestStepScan/index.tsx b/src/pages/iou/request/step/IOURequestStepScan/index.tsx index 14a098b8b2ba..545cc8b0d5a8 100644 --- a/src/pages/iou/request/step/IOURequestStepScan/index.tsx +++ b/src/pages/iou/request/step/IOURequestStepScan/index.tsx @@ -43,7 +43,7 @@ import Navigation from '@libs/Navigation/Navigation'; import {getIsUserSubmittedExpenseOrScannedReceipt, getManagerMcTestParticipant, getParticipantsOption, getReportOption} from '@libs/OptionsListUtils'; import Permissions from '@libs/Permissions'; import {isPaidGroupPolicy} from '@libs/PolicyUtils'; -import {getPolicyExpenseChat, isArchivedReport, isPolicyExpenseChat} from '@libs/ReportUtils'; +import {generateReportID, getPolicyExpenseChat, isArchivedReport, isPolicyExpenseChat} from '@libs/ReportUtils'; import playSound, {SOUNDS} from '@libs/Sound'; import {shouldRestrictUserBillableActions} from '@libs/SubscriptionUtils'; import {getDefaultTaxCode} from '@libs/TransactionUtils'; @@ -111,13 +111,13 @@ function IOURequestStepScan({ const [elementTop, setElementTop] = useState(0); const getScreenshotTimeoutRef = useRef(null); - const [reportNameValuePairs] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${report?.reportID}`); + const [reportNameValuePairs] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${report?.reportID}`, {canBeMissing: true}); const policy = usePolicy(report?.policyID); - const [personalDetails] = useOnyx(ONYXKEYS.PERSONAL_DETAILS_LIST); - const [skipConfirmation] = useOnyx(`${ONYXKEYS.COLLECTION.SKIP_CONFIRMATION}${transactionID}`); - const [activePolicyID] = useOnyx(ONYXKEYS.NVP_ACTIVE_POLICY_ID); - const [activePolicy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${activePolicyID}`); - const [betas] = useOnyx(ONYXKEYS.BETAS); + const [personalDetails] = useOnyx(ONYXKEYS.PERSONAL_DETAILS_LIST, {canBeMissing: false}); + const [skipConfirmation] = useOnyx(`${ONYXKEYS.COLLECTION.SKIP_CONFIRMATION}${transactionID}`, {canBeMissing: true}); + const [activePolicyID] = useOnyx(ONYXKEYS.NVP_ACTIVE_POLICY_ID, {canBeMissing: false}); + const [activePolicy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${activePolicyID}`, {canBeMissing: true}); + const [betas] = useOnyx(ONYXKEYS.BETAS, {canBeMissing: false}); const [isLoadingReceipt, setIsLoadingReceipt] = useState(false); const [videoConstraints, setVideoConstraints] = useState(); @@ -279,7 +279,7 @@ function IOURequestStepScan({ }, [iouType, reportID, transactionID]); const navigateToConfirmationPage = useCallback( - (isTestTransaction = false) => { + (isTestTransaction = false, reportIDParam: string | undefined = undefined) => { switch (iouType) { case CONST.IOU.TYPE.REQUEST: Navigation.navigate(ROUTES.MONEY_REQUEST_STEP_CONFIRMATION.getRoute(CONST.IOU.ACTION.CREATE, CONST.IOU.TYPE.SUBMIT, transactionID, reportID)); @@ -289,7 +289,13 @@ function IOURequestStepScan({ break; default: Navigation.navigate( - ROUTES.MONEY_REQUEST_STEP_CONFIRMATION.getRoute(CONST.IOU.ACTION.CREATE, isTestTransaction ? CONST.IOU.TYPE.SUBMIT : iouType, transactionID, reportID), + ROUTES.MONEY_REQUEST_STEP_CONFIRMATION.getRoute( + CONST.IOU.ACTION.CREATE, + isTestTransaction ? CONST.IOU.TYPE.SUBMIT : iouType, + transactionID, + // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing + reportIDParam || reportID, + ), ); } }, @@ -343,6 +349,17 @@ function IOURequestStepScan({ return; } + if (isTestTransaction) { + const managerMcTestParticipant = getManagerMcTestParticipant() ?? {}; + let reportIDParam = managerMcTestParticipant.reportID; + if (!managerMcTestParticipant.reportID && report?.reportID) { + reportIDParam = generateReportID(); + } + setMoneyRequestParticipants(transactionID, [{...managerMcTestParticipant, reportID: reportIDParam, selected: true}], true); + navigateToConfirmationPage(true, reportIDParam); + return; + } + // If a reportID exists in the report object, it's because either: // - The user started this flow from using the + button in the composer inside a report. // - The user started this flow from using the global create menu by selecting the Track expense option. @@ -475,12 +492,6 @@ function IOURequestStepScan({ ); }); } else { - if (isTestTransaction) { - const managerMcTestParticipant = getManagerMcTestParticipant() ?? {}; - setMoneyRequestParticipants(transactionID, [{...managerMcTestParticipant, selected: true}]); - navigateToConfirmationPage(true); - return; - } navigateToParticipantPage(); } },