From bd8716bd6d8c150baa71841fbafcc3bdeae104a7 Mon Sep 17 00:00:00 2001 From: Shubham Agrawal Date: Thu, 13 Mar 2025 22:55:08 +0530 Subject: [PATCH] Fixed scan expense flow when participants are missing --- src/libs/actions/IOU.ts | 15 ++++++++--- .../iou/request/step/IOURequestStepAmount.tsx | 27 ++++++++++--------- .../request/step/IOURequestStepDistance.tsx | 27 ++++++++++--------- .../step/IOURequestStepScan/index.native.tsx | 26 ++++++++++-------- .../request/step/IOURequestStepScan/index.tsx | 26 ++++++++++-------- 5 files changed, 72 insertions(+), 49 deletions(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index bedaebe90564..f067240e9991 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -9244,7 +9244,7 @@ function replaceReceipt(transactionID: string, file: File, source: string) { * @param transactionID of the transaction to set the participants of * @param report attached to the transaction */ -function setMoneyRequestParticipantsFromReport(transactionID: string, report: OnyxEntry): Participant[] { +function getMoneyRequestParticipantsFromReport(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 = isMoneyRequestReportReportUtils(report) ? getReportOrDraftReport(report?.chatReportID) : report; const currentUserAccountID = currentUserPersonalDetails?.accountID; @@ -9269,11 +9269,19 @@ function setMoneyRequestParticipantsFromReport(transactionID: string, report: On participants = chatReportOtherParticipants.map((accountID) => ({accountID, selected: true})); } - Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${transactionID}`, {participants, participantsAutoAssigned: true}); - return participants; } +/** + * Sets the participants for an IOU based on the attached report + * @param transactionID of the transaction to set the participants of + * @param report attached to the transaction + */ +function setMoneyRequestParticipantsFromReport(transactionID: string, report: OnyxEntry) { + const participants = getMoneyRequestParticipantsFromReport(report); + return Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${transactionID}`, {participants, participantsAutoAssigned: true}); +} + function setMoneyRequestTaxRate(transactionID: string, taxCode: string | null) { Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${transactionID}`, {taxCode}); } @@ -10114,6 +10122,7 @@ export { setMoneyRequestMerchant, setMoneyRequestParticipants, setMoneyRequestParticipantsFromReport, + getMoneyRequestParticipantsFromReport, setMoneyRequestPendingFields, setMoneyRequestReceipt, setMoneyRequestTag, diff --git a/src/pages/iou/request/step/IOURequestStepAmount.tsx b/src/pages/iou/request/step/IOURequestStepAmount.tsx index 97f872d3a0f9..e1dc5d94004c 100644 --- a/src/pages/iou/request/step/IOURequestStepAmount.tsx +++ b/src/pages/iou/request/step/IOURequestStepAmount.tsx @@ -17,6 +17,7 @@ import {shouldRestrictUserBillableActions} from '@libs/SubscriptionUtils'; import {calculateTaxAmount, getAmount, getCurrency, getDefaultTaxCode, getRequestType, getTaxValue} from '@libs/TransactionUtils'; import MoneyRequestAmountForm from '@pages/iou/MoneyRequestAmountForm'; import { + getMoneyRequestParticipantsFromReport, requestMoney, resetSplitShares, sendMoneyElsewhere, @@ -186,7 +187,7 @@ function IOURequestStepAmount({ // to the confirm step. // If the user is started this flow using the Create expense option (combined submit/track flow), they should be redirected to the participants page. if (report?.reportID && !isArchivedReport(reportNameValuePairs) && iouType !== CONST.IOU.TYPE.CREATE) { - const selectedParticipants = setMoneyRequestParticipantsFromReport(transactionID, report); + const selectedParticipants = getMoneyRequestParticipantsFromReport(report); const participants = selectedParticipants.map((participant) => { const participantAccountID = participant?.accountID ?? CONST.DEFAULT_NUMBER_ID; return participantAccountID ? getParticipantsOption(participant, personalDetails) : getReportOption(participant); @@ -242,12 +243,13 @@ function IOURequestStepAmount({ return; } } - setMoneyRequestParticipantsFromReport(transactionID, report); if (isSplitBill && !report.isOwnPolicyExpenseChat && report.participants) { const participantAccountIDs = Object.keys(report.participants).map((accountID) => Number(accountID)); setSplitShares(transaction, amountInSmallestCurrencyUnits, currency || CONST.CURRENCY.USD, participantAccountIDs); } - navigateToConfirmationPage(); + setMoneyRequestParticipantsFromReport(transactionID, report).then(() => { + navigateToConfirmationPage(); + }); return; } @@ -255,15 +257,16 @@ function IOURequestStepAmount({ // and an optimistic reportID was generated. In that case, the next step is to select the participants for this expense. if (iouType === CONST.IOU.TYPE.CREATE && isPaidGroupPolicy(activePolicy) && activePolicy?.isPolicyExpenseChatEnabled && !shouldRestrictUserBillableActions(activePolicy.id)) { const activePolicyExpenseChat = getPolicyExpenseChat(currentUserPersonalDetails.accountID, activePolicy?.id); - setMoneyRequestParticipantsFromReport(transactionID, activePolicyExpenseChat); - Navigation.navigate( - ROUTES.MONEY_REQUEST_STEP_CONFIRMATION.getRoute( - CONST.IOU.ACTION.CREATE, - iouType === CONST.IOU.TYPE.CREATE ? CONST.IOU.TYPE.SUBMIT : iouType, - transactionID, - activePolicyExpenseChat?.reportID, - ), - ); + setMoneyRequestParticipantsFromReport(transactionID, activePolicyExpenseChat).then(() => { + Navigation.navigate( + ROUTES.MONEY_REQUEST_STEP_CONFIRMATION.getRoute( + CONST.IOU.ACTION.CREATE, + iouType === CONST.IOU.TYPE.CREATE ? CONST.IOU.TYPE.SUBMIT : iouType, + transactionID, + activePolicyExpenseChat?.reportID, + ), + ); + }); } else { navigateToParticipantPage(); } diff --git a/src/pages/iou/request/step/IOURequestStepDistance.tsx b/src/pages/iou/request/step/IOURequestStepDistance.tsx index a439cb353683..d07d3be69d91 100644 --- a/src/pages/iou/request/step/IOURequestStepDistance.tsx +++ b/src/pages/iou/request/step/IOURequestStepDistance.tsx @@ -23,6 +23,7 @@ import useThemeStyles from '@hooks/useThemeStyles'; import { createDistanceRequest, getIOURequestPolicyID, + getMoneyRequestParticipantsFromReport, resetSplitShares, setCustomUnitRateID, setMoneyRequestAmount, @@ -312,7 +313,7 @@ function IOURequestStepDistance({ // to the confirm step. // If the user started this flow using the Create expense option (combined submit/track flow), they should be redirected to the participants page. if (report?.reportID && !isArchivedReport(reportNameValuePairs) && iouType !== CONST.IOU.TYPE.CREATE) { - const selectedParticipants = setMoneyRequestParticipantsFromReport(transactionID, report); + const selectedParticipants = getMoneyRequestParticipantsFromReport(report); const participants = selectedParticipants.map((participant) => { const participantAccountID = participant?.accountID ?? CONST.DEFAULT_NUMBER_ID; return participantAccountID ? getParticipantsOption(participant, personalDetails) : getReportOption(participant); @@ -371,8 +372,9 @@ function IOURequestStepDistance({ }); return; } - setMoneyRequestParticipantsFromReport(transactionID, report); - navigateToConfirmationPage(); + setMoneyRequestParticipantsFromReport(transactionID, report).then(() => { + navigateToConfirmationPage(); + }); return; } @@ -380,17 +382,18 @@ function IOURequestStepDistance({ // and an optimistic reportID was generated. In that case, the next step is to select the participants for this expense. if (iouType === CONST.IOU.TYPE.CREATE && isPaidGroupPolicy(activePolicy) && activePolicy?.isPolicyExpenseChatEnabled && !shouldRestrictUserBillableActions(activePolicy.id)) { const activePolicyExpenseChat = getPolicyExpenseChat(currentUserPersonalDetails.accountID, activePolicy?.id); - setMoneyRequestParticipantsFromReport(transactionID, activePolicyExpenseChat); const rateID = DistanceRequestUtils.getCustomUnitRateID(activePolicyExpenseChat?.reportID); setCustomUnitRateID(transactionID, rateID); - Navigation.navigate( - ROUTES.MONEY_REQUEST_STEP_CONFIRMATION.getRoute( - CONST.IOU.ACTION.CREATE, - iouType === CONST.IOU.TYPE.CREATE ? CONST.IOU.TYPE.SUBMIT : iouType, - transactionID, - activePolicyExpenseChat?.reportID, - ), - ); + setMoneyRequestParticipantsFromReport(transactionID, activePolicyExpenseChat).then(() => { + Navigation.navigate( + ROUTES.MONEY_REQUEST_STEP_CONFIRMATION.getRoute( + CONST.IOU.ACTION.CREATE, + iouType === CONST.IOU.TYPE.CREATE ? CONST.IOU.TYPE.SUBMIT : iouType, + transactionID, + activePolicyExpenseChat?.reportID, + ), + ); + }); } else { navigateToParticipantPage(); } diff --git a/src/pages/iou/request/step/IOURequestStepScan/index.native.tsx b/src/pages/iou/request/step/IOURequestStepScan/index.native.tsx index 42326f5ccf90..b013ce697bed 100644 --- a/src/pages/iou/request/step/IOURequestStepScan/index.native.tsx +++ b/src/pages/iou/request/step/IOURequestStepScan/index.native.tsx @@ -46,6 +46,7 @@ import StepScreenWrapper from '@pages/iou/request/step/StepScreenWrapper'; import withFullTransactionOrNotFound from '@pages/iou/request/step/withFullTransactionOrNotFound'; import withWritableReportOrNotFound from '@pages/iou/request/step/withWritableReportOrNotFound'; import { + getMoneyRequestParticipantsFromReport, replaceReceipt, requestMoney, setMoneyRequestParticipantsFromReport, @@ -304,15 +305,16 @@ function IOURequestStepScan({ if ((transaction?.isFromGlobalCreate && iouType !== CONST.IOU.TYPE.TRACK && !report?.reportID) || iouType === CONST.IOU.TYPE.CREATE) { if (activePolicy && isPaidGroupPolicy(activePolicy) && !shouldRestrictUserBillableActions(activePolicy.id)) { const activePolicyExpenseChat = getPolicyExpenseChat(currentUserPersonalDetails.accountID, activePolicy?.id); - setMoneyRequestParticipantsFromReport(transactionID, activePolicyExpenseChat); - Navigation.navigate( - ROUTES.MONEY_REQUEST_STEP_CONFIRMATION.getRoute( - CONST.IOU.ACTION.CREATE, - iouType === CONST.IOU.TYPE.CREATE ? CONST.IOU.TYPE.SUBMIT : iouType, - transactionID, - activePolicyExpenseChat?.reportID, - ), - ); + setMoneyRequestParticipantsFromReport(transactionID, activePolicyExpenseChat).then(() => { + Navigation.navigate( + ROUTES.MONEY_REQUEST_STEP_CONFIRMATION.getRoute( + CONST.IOU.ACTION.CREATE, + iouType === CONST.IOU.TYPE.CREATE ? CONST.IOU.TYPE.SUBMIT : iouType, + transactionID, + activePolicyExpenseChat?.reportID, + ), + ); + }); } else { navigateToParticipantPage(); } @@ -321,7 +323,7 @@ function IOURequestStepScan({ // If the transaction was created from the + menu from the composer inside of a chat, the participants can automatically // be added to the transaction (taken from the chat report participants) and then the person is taken to the confirmation step. - const selectedParticipants = setMoneyRequestParticipantsFromReport(transactionID, report); + const selectedParticipants = getMoneyRequestParticipantsFromReport(report); const participants = selectedParticipants.map((participant) => { const participantAccountID = participant?.accountID ?? CONST.DEFAULT_NUMBER_ID; return participantAccountID ? getParticipantsOption(participant, personalDetails) : getReportOption(participant); @@ -425,7 +427,9 @@ function IOURequestStepScan({ createTransaction(receipt, participant); return; } - navigateToConfirmationPage(); + setMoneyRequestParticipantsFromReport(transactionID, report).then(() => { + navigateToConfirmationPage(); + }); }, [ backTo, diff --git a/src/pages/iou/request/step/IOURequestStepScan/index.tsx b/src/pages/iou/request/step/IOURequestStepScan/index.tsx index 3cdb62e0d42a..3b22361f7b2d 100644 --- a/src/pages/iou/request/step/IOURequestStepScan/index.tsx +++ b/src/pages/iou/request/step/IOURequestStepScan/index.tsx @@ -47,6 +47,7 @@ import StepScreenDragAndDropWrapper from '@pages/iou/request/step/StepScreenDrag import withFullTransactionOrNotFound from '@pages/iou/request/step/withFullTransactionOrNotFound'; import withWritableReportOrNotFound from '@pages/iou/request/step/withWritableReportOrNotFound'; import { + getMoneyRequestParticipantsFromReport, replaceReceipt, requestMoney, setMoneyRequestParticipantsFromReport, @@ -359,15 +360,16 @@ function IOURequestStepScan({ if ((transaction?.isFromGlobalCreate && iouType !== CONST.IOU.TYPE.TRACK && !report?.reportID) || iouType === CONST.IOU.TYPE.CREATE) { if (activePolicy && isPaidGroupPolicy(activePolicy) && !shouldRestrictUserBillableActions(activePolicy.id)) { const activePolicyExpenseChat = getPolicyExpenseChat(currentUserPersonalDetails.accountID, activePolicy?.id); - setMoneyRequestParticipantsFromReport(transactionID, activePolicyExpenseChat); - Navigation.navigate( - ROUTES.MONEY_REQUEST_STEP_CONFIRMATION.getRoute( - CONST.IOU.ACTION.CREATE, - iouType === CONST.IOU.TYPE.CREATE ? CONST.IOU.TYPE.SUBMIT : iouType, - transactionID, - activePolicyExpenseChat?.reportID, - ), - ); + setMoneyRequestParticipantsFromReport(transactionID, activePolicyExpenseChat).then(() => { + Navigation.navigate( + ROUTES.MONEY_REQUEST_STEP_CONFIRMATION.getRoute( + CONST.IOU.ACTION.CREATE, + iouType === CONST.IOU.TYPE.CREATE ? CONST.IOU.TYPE.SUBMIT : iouType, + transactionID, + activePolicyExpenseChat?.reportID, + ), + ); + }); } else { navigateToParticipantPage(); } @@ -376,7 +378,7 @@ function IOURequestStepScan({ // If the transaction was created from the + menu from the composer inside of a chat, the participants can automatically // be added to the transaction (taken from the chat report participants) and then the person is taken to the confirmation step. - const selectedParticipants = setMoneyRequestParticipantsFromReport(transactionID, report); + const selectedParticipants = getMoneyRequestParticipantsFromReport(report); const participants = selectedParticipants.map((participant) => { const participantAccountID = participant?.accountID ?? CONST.DEFAULT_NUMBER_ID; return participantAccountID ? getParticipantsOption(participant, personalDetails) : getReportOption(participant); @@ -480,7 +482,9 @@ function IOURequestStepScan({ createTransaction(receipt, participant); return; } - navigateToConfirmationPage(); + setMoneyRequestParticipantsFromReport(transactionID, report).then(() => { + navigateToConfirmationPage(); + }); }, [ backTo,