From 115c045f5ac3906b954b17e39177c187aaeb9c7c Mon Sep 17 00:00:00 2001 From: neil-marcellini Date: Wed, 15 May 2024 20:20:51 -0700 Subject: [PATCH 01/24] Allow distance splits on start page --- src/pages/iou/request/IOURequestStartPage.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pages/iou/request/IOURequestStartPage.tsx b/src/pages/iou/request/IOURequestStartPage.tsx index 1bbf0d02a941..573c44beaf96 100644 --- a/src/pages/iou/request/IOURequestStartPage.tsx +++ b/src/pages/iou/request/IOURequestStartPage.tsx @@ -103,7 +103,7 @@ function IOURequestStartPage({ const isExpenseChat = ReportUtils.isPolicyExpenseChat(report); const isExpenseReport = ReportUtils.isExpenseReport(report); - const shouldDisplayDistanceRequest = (!!canUseP2PDistanceRequests || isExpenseChat || isExpenseReport || isFromGlobalCreate) && iouType !== CONST.IOU.TYPE.SPLIT; + const shouldDisplayDistanceRequest = (!!canUseP2PDistanceRequests || isExpenseChat || isExpenseReport || isFromGlobalCreate); // Allow the user to submit the expense if we are submitting the expense in global menu or the report can create the exoense const isAllowedToCreateRequest = isEmptyObject(report?.reportID) || ReportUtils.canCreateRequest(report, policy, iouType) || PolicyUtils.canSendInvoice(allPolicies); From 3ee8a7aa5401ca541a7a0d3b501b1f931830679b Mon Sep 17 00:00:00 2001 From: neil-marcellini Date: Thu, 16 May 2024 07:37:19 -0700 Subject: [PATCH 02/24] Set split share when distanceAmount updates --- src/components/MoneyRequestConfirmationList.tsx | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/components/MoneyRequestConfirmationList.tsx b/src/components/MoneyRequestConfirmationList.tsx index 5ffd9beda6fe..5517b3b5f543 100755 --- a/src/components/MoneyRequestConfirmationList.tsx +++ b/src/components/MoneyRequestConfirmationList.tsx @@ -306,10 +306,11 @@ function MoneyRequestConfirmationList({ const isMovingTransactionFromTrackExpense = IOUUtils.isMovingTransactionFromTrackExpense(action); const hasRoute = TransactionUtils.hasRoute(transaction, isDistanceRequest); const isDistanceRequestWithPendingRoute = isDistanceRequest && (!hasRoute || !rate) && !isMovingTransactionFromTrackExpense; + const distanceRequestAmount = DistanceRequestUtils.getDistanceRequestAmount(distance, unit ?? CONST.CUSTOM_UNITS.DISTANCE_UNIT_MILES, rate ?? 0); const formattedAmount = isDistanceRequestWithPendingRoute ? '' : CurrencyUtils.convertToDisplayString( - shouldCalculateDistanceAmount ? DistanceRequestUtils.getDistanceRequestAmount(distance, unit ?? CONST.CUSTOM_UNITS.DISTANCE_UNIT_MILES, rate ?? 0) : iouAmount, + shouldCalculateDistanceAmount ? distanceRequestAmount : iouAmount, isDistanceRequest ? currency : iouCurrencyCode, ); const formattedTaxAmount = CurrencyUtils.convertToDisplayString(transaction?.taxAmount, iouCurrencyCode); @@ -365,9 +366,15 @@ function MoneyRequestConfirmationList({ return; } - const amount = DistanceRequestUtils.getDistanceRequestAmount(distance, unit ?? CONST.CUSTOM_UNITS.DISTANCE_UNIT_MILES, rate ?? 0); + const amount = distanceRequestAmount; IOU.setMoneyRequestAmount(transactionID, amount, currency ?? ''); - }, [shouldCalculateDistanceAmount, distance, rate, unit, transactionID, currency]); + + // If it's a split request among individuals, set the split shares + const participantAccountIDs: number[] = selectedParticipantsProp.map((participant) => participant.accountID ?? -1); + if (isTypeSplit && !isPolicyExpenseChat && amount && transaction?.currency) { + IOU.setSplitShares(transaction, amount, currency, participantAccountIDs); + } + }, [shouldCalculateDistanceAmount, distanceRequestAmount, transactionID, currency, isTypeSplit, isPolicyExpenseChat, selectedParticipantsProp, transaction]); // Calculate and set tax amount in transaction draft useEffect(() => { From 813f8ca9c0dd58bf15120631e816ca9129f216b8 Mon Sep 17 00:00:00 2001 From: neil-marcellini Date: Thu, 16 May 2024 08:30:45 -0700 Subject: [PATCH 03/24] WIP split distance global create set split data --- src/libs/actions/IOU.ts | 211 ++++++++++-------- .../step/IOURequestStepConfirmation.tsx | 51 +++-- 2 files changed, 148 insertions(+), 114 deletions(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 2464dbff7dbd..2d1b9115e8d1 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -2191,99 +2191,6 @@ function getTrackExpenseInformation( }; } -/** Requests money based on a distance (e.g. mileage from a map) */ -function createDistanceRequest( - report: OnyxEntry, - participant: Participant, - comment: string, - created: string, - category: string | undefined, - tag: string | undefined, - taxCode: string | undefined, - taxAmount: number | undefined, - amount: number, - currency: string, - merchant: string, - billable: boolean | undefined, - validWaypoints: WaypointCollection, - policy?: OnyxEntry, - policyTagList?: OnyxEntry, - policyCategories?: OnyxEntry, - customUnitRateID?: string, -) { - // 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 ? ReportUtils.getReport(report?.chatReportID) : report; - const moneyRequestReportID = isMoneyRequestReport ? report?.reportID : ''; - const currentCreated = DateUtils.enrichMoneyRequestTimestamp(created); - - const optimisticReceipt: Receipt = { - source: ReceiptGeneric as ReceiptSource, - state: CONST.IOU.RECEIPT_STATE.OPEN, - }; - const { - iouReport, - chatReport, - transaction, - iouAction, - createdChatReportActionID, - createdIOUReportActionID, - reportPreviewAction, - transactionThreadReportID, - createdReportActionIDForThread, - payerEmail, - onyxData, - } = getMoneyRequestInformation( - currentChatReport, - participant, - comment, - amount, - currency, - currentCreated, - merchant, - optimisticReceipt, - undefined, - category, - tag, - taxCode, - taxAmount, - billable, - policy, - policyTagList, - policyCategories, - userAccountID, - currentUserEmail, - moneyRequestReportID, - ); - - const activeReportID = isMoneyRequestReport ? report?.reportID ?? '' : chatReport.reportID; - const parameters: CreateDistanceRequestParams = { - comment, - iouReportID: iouReport.reportID, - chatReportID: chatReport.reportID, - transactionID: transaction.transactionID, - reportActionID: iouAction.reportActionID, - createdChatReportActionID, - createdIOUReportActionID, - reportPreviewReportActionID: reportPreviewAction.reportActionID, - waypoints: JSON.stringify(validWaypoints), - created: currentCreated, - category, - tag, - taxCode, - taxAmount, - billable, - transactionThreadReportID, - createdReportActionIDForThread, - payerEmail, - customUnitRateID, - }; - - API.write(WRITE_COMMANDS.CREATE_DISTANCE_REQUEST, parameters, onyxData); - Navigation.dismissModal(activeReportID); - Report.notifyNewAction(activeReportID, userAccountID); -} - /** * Compute the diff amount when we update the transaction */ @@ -4807,6 +4714,124 @@ function setDraftSplitTransaction(transactionID: string, transactionChanges: Tra Onyx.merge(`${ONYXKEYS.COLLECTION.SPLIT_TRANSACTION_DRAFT}${transactionID}`, updatedTransaction); } +/** Requests money based on a distance (e.g. mileage from a map) */ +function createDistanceRequest( + report: OnyxEntry, + participants: Participant[], + comment: string, + created: string, + category: string | undefined, + tag: string | undefined, + taxCode: string | undefined, + taxAmount: number | undefined, + amount: number, + currency: string, + merchant: string, + billable: boolean | undefined, + validWaypoints: WaypointCollection, + policy?: OnyxEntry, + policyTagList?: OnyxEntry, + policyCategories?: OnyxEntry, + customUnitRateID?: string, + currentUserLogin = '', + currentUserAccountID = -1, + splitShares: SplitShares = {}, +) { + // 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 ? ReportUtils.getReport(report?.chatReportID) : report; + const moneyRequestReportID = isMoneyRequestReport ? report?.reportID : ''; + const currentCreated = DateUtils.enrichMoneyRequestTimestamp(created); + + const optimisticReceipt: Receipt = { + source: ReceiptGeneric as ReceiptSource, + state: CONST.IOU.RECEIPT_STATE.OPEN, + }; + + if (participants.length > 1) { + const {splitData, splits, onyxData} = createSplitsAndOnyxData( + participants, + currentUserLogin ?? '', + currentUserAccountID, + amount, + comment, + currency, + merchant, + currentCreated, + category ?? '', + tag ?? '', + splitShares, + '', + billable, + CONST.IOU.REQUEST_TYPE.DISTANCE, + ); + + } + + const participant = participants[0] ?? {}; + const { + iouReport, + chatReport, + transaction, + iouAction, + createdChatReportActionID, + createdIOUReportActionID, + reportPreviewAction, + transactionThreadReportID, + createdReportActionIDForThread, + payerEmail, + onyxData, + } = getMoneyRequestInformation( + currentChatReport, + participant, + comment, + amount, + currency, + currentCreated, + merchant, + optimisticReceipt, + undefined, + category, + tag, + taxCode, + taxAmount, + billable, + policy, + policyTagList, + policyCategories, + userAccountID, + currentUserEmail, + moneyRequestReportID, + ); + + const activeReportID = isMoneyRequestReport ? report?.reportID ?? '' : chatReport.reportID; + const parameters: CreateDistanceRequestParams = { + comment, + iouReportID: iouReport.reportID, + chatReportID: chatReport.reportID, + transactionID: transaction.transactionID, + reportActionID: iouAction.reportActionID, + createdChatReportActionID, + createdIOUReportActionID, + reportPreviewReportActionID: reportPreviewAction.reportActionID, + waypoints: JSON.stringify(validWaypoints), + created: currentCreated, + category, + tag, + taxCode, + taxAmount, + billable, + transactionThreadReportID, + createdReportActionIDForThread, + payerEmail, + customUnitRateID, + }; + + API.write(WRITE_COMMANDS.CREATE_DISTANCE_REQUEST, parameters, onyxData); + Navigation.dismissModal(activeReportID); + Report.notifyNewAction(activeReportID, userAccountID); +} + function editRegularMoneyRequest( transactionID: string, transactionThreadReportID: string, diff --git a/src/pages/iou/request/step/IOURequestStepConfirmation.tsx b/src/pages/iou/request/step/IOURequestStepConfirmation.tsx index 458df99a5f6c..0384eb6f8179 100644 --- a/src/pages/iou/request/step/IOURequestStepConfirmation.tsx +++ b/src/pages/iou/request/step/IOURequestStepConfirmation.tsx @@ -289,13 +289,14 @@ function IOURequestStepConfirmation({ ); const createDistanceRequest = useCallback( - (selectedParticipants: Participant[], trimmedComment: string, customUnitRateID: string) => { + (selectedParticipants: Participant[], trimmedComment: string) => { if (!transaction) { return; } + const customUnitRateID = TransactionUtils.getRateID(transaction) ?? ''; IOU.createDistanceRequest( report, - selectedParticipants[0], + selectedParticipants, trimmedComment, transaction.created, transaction.category, @@ -311,9 +312,13 @@ function IOURequestStepConfirmation({ policyTags, policyCategories, customUnitRateID, + currentUserPersonalDetails.login, + currentUserPersonalDetails.accountID, + transaction.splitShares, + // splitPayerAccountIDs: transaction.splitPayerAccountIDs, ); }, - [policy, policyCategories, policyTags, report, transaction, transactionTaxCode, transactionTaxAmount], + [policy, policyCategories, policyTags, report, transaction, transactionTaxCode, transactionTaxAmount, currentUserPersonalDetails], ); const createTransaction = useCallback( @@ -383,22 +388,27 @@ function IOURequestStepConfirmation({ // If the split expense is created from the global create menu, we also navigate the user to the group report if (iouType === CONST.IOU.TYPE.SPLIT) { if (currentUserPersonalDetails.login && !!transaction) { - IOU.splitBillAndOpenReport({ - participants: splitParticipants, - currentUserLogin: currentUserPersonalDetails.login, - currentUserAccountID: currentUserPersonalDetails.accountID, - amount: transaction.amount, - comment: trimmedComment, - currency: transaction.currency, - merchant: transaction.merchant, - created: transaction.created, - category: transaction.category, - tag: transaction.tag, - billable: !!transaction.billable, - iouRequestType: transaction.iouRequestType, - splitShares: transaction.splitShares, - splitPayerAccountIDs: transaction.splitPayerAccountIDs, - }); + if (requestType === CONST.IOU.REQUEST_TYPE.DISTANCE) { + createDistanceRequest(splitParticipants, trimmedComment); + } else { + IOU.splitBillAndOpenReport({ + participants: splitParticipants, + currentUserLogin: currentUserPersonalDetails.login, + currentUserAccountID: currentUserPersonalDetails.accountID, + amount: transaction.amount, + comment: trimmedComment, + currency: transaction.currency, + merchant: transaction.merchant, + created: transaction.created, + category: transaction.category, + tag: transaction.tag, + billable: !!transaction.billable, + iouRequestType: transaction.iouRequestType, + splitShares: transaction.splitShares, + splitPayerAccountIDs: transaction.splitPayerAccountIDs, + }); + } + } return; } @@ -469,8 +479,7 @@ function IOURequestStepConfirmation({ } if (requestType === CONST.IOU.REQUEST_TYPE.DISTANCE && !IOUUtils.isMovingTransactionFromTrackExpense(action)) { - const customUnitRateID = TransactionUtils.getRateID(transaction) ?? ''; - createDistanceRequest(selectedParticipants, trimmedComment, customUnitRateID); + createDistanceRequest(selectedParticipants, trimmedComment); return; } From cc66e8083d25166c7a6865d5fb3566cddc56f771 Mon Sep 17 00:00:00 2001 From: neil-marcellini Date: Thu, 16 May 2024 08:49:08 -0700 Subject: [PATCH 04/24] WIP set up CreateDistanceRequestParams for split --- src/libs/actions/IOU.ts | 142 +++++++++++++++++++++++----------------- 1 file changed, 83 insertions(+), 59 deletions(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 2d1b9115e8d1..c93e2d0322bb 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -4748,6 +4748,8 @@ function createDistanceRequest( state: CONST.IOU.RECEIPT_STATE.OPEN, }; + let parameters: CreateDistanceRequestParams; + if (participants.length > 1) { const {splitData, splits, onyxData} = createSplitsAndOnyxData( participants, @@ -4766,66 +4768,88 @@ function createDistanceRequest( CONST.IOU.REQUEST_TYPE.DISTANCE, ); - } - - const participant = participants[0] ?? {}; - const { - iouReport, - chatReport, - transaction, - iouAction, - createdChatReportActionID, - createdIOUReportActionID, - reportPreviewAction, - transactionThreadReportID, - createdReportActionIDForThread, - payerEmail, - onyxData, - } = getMoneyRequestInformation( - currentChatReport, - participant, - comment, - amount, - currency, - currentCreated, - merchant, - optimisticReceipt, - undefined, - category, - tag, - taxCode, - taxAmount, - billable, - policy, - policyTagList, - policyCategories, - userAccountID, - currentUserEmail, - moneyRequestReportID, - ); + parameters = { + comment, + iouReportID: iouReport.reportID, + chatReportID: splitData.chatReportID, + transactionID: splitData.transactionID, + reportActionID: splitData.reportActionID, + createdChatReportActionID, + createdIOUReportActionID: splitData.createdReportActionID, + reportPreviewReportActionID: reportPreviewAction.reportActionID, + waypoints: JSON.stringify(validWaypoints), + created: currentCreated, + category, + tag, + taxCode, + taxAmount, + billable, + transactionThreadReportID, + createdReportActionIDForThread, + customUnitRateID, + splits: JSON.stringify(splits), + chatType: splitData.chatType, + }; + } else { + const participant = participants[0] ?? {}; + const { + iouReport, + chatReport, + transaction, + iouAction, + createdChatReportActionID, + createdIOUReportActionID, + reportPreviewAction, + transactionThreadReportID, + createdReportActionIDForThread, + payerEmail, + onyxData, + } = getMoneyRequestInformation( + currentChatReport, + participant, + comment, + amount, + currency, + currentCreated, + merchant, + optimisticReceipt, + undefined, + category, + tag, + taxCode, + taxAmount, + billable, + policy, + policyTagList, + policyCategories, + userAccountID, + currentUserEmail, + moneyRequestReportID, + ); - const activeReportID = isMoneyRequestReport ? report?.reportID ?? '' : chatReport.reportID; - const parameters: CreateDistanceRequestParams = { - comment, - iouReportID: iouReport.reportID, - chatReportID: chatReport.reportID, - transactionID: transaction.transactionID, - reportActionID: iouAction.reportActionID, - createdChatReportActionID, - createdIOUReportActionID, - reportPreviewReportActionID: reportPreviewAction.reportActionID, - waypoints: JSON.stringify(validWaypoints), - created: currentCreated, - category, - tag, - taxCode, - taxAmount, - billable, - transactionThreadReportID, - createdReportActionIDForThread, - payerEmail, - customUnitRateID, - }; + const activeReportID = isMoneyRequestReport ? report?.reportID ?? '' : chatReport.reportID; + parameters = { + comment, + iouReportID: iouReport.reportID, + chatReportID: chatReport.reportID, + transactionID: transaction.transactionID, + reportActionID: iouAction.reportActionID, + createdChatReportActionID, + createdIOUReportActionID, + reportPreviewReportActionID: reportPreviewAction.reportActionID, + waypoints: JSON.stringify(validWaypoints), + created: currentCreated, + category, + tag, + taxCode, + taxAmount, + billable, + transactionThreadReportID, + createdReportActionIDForThread, + payerEmail, + customUnitRateID, + }; + } API.write(WRITE_COMMANDS.CREATE_DISTANCE_REQUEST, parameters, onyxData); Navigation.dismissModal(activeReportID); From a9e267ca7f65b81a2e2ec2d091288f6cd2a55575 Mon Sep 17 00:00:00 2001 From: neil-marcellini Date: Thu, 16 May 2024 08:49:15 -0700 Subject: [PATCH 05/24] Prettier --- src/components/MoneyRequestConfirmationList.tsx | 5 +---- src/pages/iou/request/IOURequestStartPage.tsx | 2 +- src/pages/iou/request/step/IOURequestStepConfirmation.tsx | 5 ++--- 3 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/components/MoneyRequestConfirmationList.tsx b/src/components/MoneyRequestConfirmationList.tsx index 5517b3b5f543..a7f476cde9be 100755 --- a/src/components/MoneyRequestConfirmationList.tsx +++ b/src/components/MoneyRequestConfirmationList.tsx @@ -309,10 +309,7 @@ function MoneyRequestConfirmationList({ const distanceRequestAmount = DistanceRequestUtils.getDistanceRequestAmount(distance, unit ?? CONST.CUSTOM_UNITS.DISTANCE_UNIT_MILES, rate ?? 0); const formattedAmount = isDistanceRequestWithPendingRoute ? '' - : CurrencyUtils.convertToDisplayString( - shouldCalculateDistanceAmount ? distanceRequestAmount : iouAmount, - isDistanceRequest ? currency : iouCurrencyCode, - ); + : CurrencyUtils.convertToDisplayString(shouldCalculateDistanceAmount ? distanceRequestAmount : iouAmount, isDistanceRequest ? currency : iouCurrencyCode); const formattedTaxAmount = CurrencyUtils.convertToDisplayString(transaction?.taxAmount, iouCurrencyCode); const taxRateTitle = TransactionUtils.getTaxName(policy, transaction); diff --git a/src/pages/iou/request/IOURequestStartPage.tsx b/src/pages/iou/request/IOURequestStartPage.tsx index 573c44beaf96..aa089f7008f5 100644 --- a/src/pages/iou/request/IOURequestStartPage.tsx +++ b/src/pages/iou/request/IOURequestStartPage.tsx @@ -103,7 +103,7 @@ function IOURequestStartPage({ const isExpenseChat = ReportUtils.isPolicyExpenseChat(report); const isExpenseReport = ReportUtils.isExpenseReport(report); - const shouldDisplayDistanceRequest = (!!canUseP2PDistanceRequests || isExpenseChat || isExpenseReport || isFromGlobalCreate); + const shouldDisplayDistanceRequest = !!canUseP2PDistanceRequests || isExpenseChat || isExpenseReport || isFromGlobalCreate; // Allow the user to submit the expense if we are submitting the expense in global menu or the report can create the exoense const isAllowedToCreateRequest = isEmptyObject(report?.reportID) || ReportUtils.canCreateRequest(report, policy, iouType) || PolicyUtils.canSendInvoice(allPolicies); diff --git a/src/pages/iou/request/step/IOURequestStepConfirmation.tsx b/src/pages/iou/request/step/IOURequestStepConfirmation.tsx index 0384eb6f8179..e9874e0a0c77 100644 --- a/src/pages/iou/request/step/IOURequestStepConfirmation.tsx +++ b/src/pages/iou/request/step/IOURequestStepConfirmation.tsx @@ -312,10 +312,10 @@ function IOURequestStepConfirmation({ policyTags, policyCategories, customUnitRateID, - currentUserPersonalDetails.login, + currentUserPersonalDetails.login, currentUserPersonalDetails.accountID, transaction.splitShares, - // splitPayerAccountIDs: transaction.splitPayerAccountIDs, + // splitPayerAccountIDs: transaction.splitPayerAccountIDs, ); }, [policy, policyCategories, policyTags, report, transaction, transactionTaxCode, transactionTaxAmount, currentUserPersonalDetails], @@ -408,7 +408,6 @@ function IOURequestStepConfirmation({ splitPayerAccountIDs: transaction.splitPayerAccountIDs, }); } - } return; } From c9a7962248074e2a92e73b60abbf616bbd1b0030 Mon Sep 17 00:00:00 2001 From: neil-marcellini Date: Thu, 16 May 2024 12:51:23 -0700 Subject: [PATCH 06/24] Carefully fix distance split params --- .../API/parameters/CreateDistanceRequestParams.ts | 2 ++ src/libs/actions/IOU.ts | 15 +++++++++------ .../request/step/IOURequestStepConfirmation.tsx | 1 - 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/libs/API/parameters/CreateDistanceRequestParams.ts b/src/libs/API/parameters/CreateDistanceRequestParams.ts index 68469899af06..c8f9f4a80538 100644 --- a/src/libs/API/parameters/CreateDistanceRequestParams.ts +++ b/src/libs/API/parameters/CreateDistanceRequestParams.ts @@ -18,6 +18,8 @@ type CreateDistanceRequestParams = { createdReportActionIDForThread: string; payerEmail: string; customUnitRateID?: string; + splits?: string; + chatType?: string; }; export default CreateDistanceRequestParams; diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index c93e2d0322bb..a98141d01fff 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -4768,15 +4768,17 @@ function createDistanceRequest( CONST.IOU.REQUEST_TYPE.DISTANCE, ); + // Splits don't use the IOU report param. The split transaction isn't linked to a report shown in the UI, it's linked to a special default reportID of -2. + // Therefore, anything related to the IOU report is irrelevant. parameters = { comment, - iouReportID: iouReport.reportID, + iouReportID: '', chatReportID: splitData.chatReportID, transactionID: splitData.transactionID, reportActionID: splitData.reportActionID, - createdChatReportActionID, - createdIOUReportActionID: splitData.createdReportActionID, - reportPreviewReportActionID: reportPreviewAction.reportActionID, + createdChatReportActionID: splitData.createdReportActionID ?? '', + createdIOUReportActionID: '', + reportPreviewReportActionID: '', // No report preview needed, since the split transaction report is not shown in the UI waypoints: JSON.stringify(validWaypoints), created: currentCreated, category, @@ -4784,9 +4786,10 @@ function createDistanceRequest( taxCode, taxAmount, billable, - transactionThreadReportID, - createdReportActionIDForThread, + transactionThreadReportID: '', + createdReportActionIDForThread: '', customUnitRateID, + payerEmail: '', splits: JSON.stringify(splits), chatType: splitData.chatType, }; diff --git a/src/pages/iou/request/step/IOURequestStepConfirmation.tsx b/src/pages/iou/request/step/IOURequestStepConfirmation.tsx index e9874e0a0c77..45a331fa0402 100644 --- a/src/pages/iou/request/step/IOURequestStepConfirmation.tsx +++ b/src/pages/iou/request/step/IOURequestStepConfirmation.tsx @@ -315,7 +315,6 @@ function IOURequestStepConfirmation({ currentUserPersonalDetails.login, currentUserPersonalDetails.accountID, transaction.splitShares, - // splitPayerAccountIDs: transaction.splitPayerAccountIDs, ); }, [policy, policyCategories, policyTags, report, transaction, transactionTaxCode, transactionTaxAmount, currentUserPersonalDetails], From 87010b18e844c68656e4f4f51894d5c58068363b Mon Sep 17 00:00:00 2001 From: neil-marcellini Date: Thu, 16 May 2024 12:58:29 -0700 Subject: [PATCH 07/24] Set onyxData from split or money request data --- src/libs/actions/IOU.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index a98141d01fff..c2f05bb348ad 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -4739,6 +4739,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 activeReportID = isMoneyRequestReport ? report?.reportID ?? '' : chatReport.reportID; const currentChatReport = isMoneyRequestReport ? ReportUtils.getReport(report?.chatReportID) : report; const moneyRequestReportID = isMoneyRequestReport ? report?.reportID : ''; const currentCreated = DateUtils.enrichMoneyRequestTimestamp(created); @@ -4749,9 +4750,9 @@ function createDistanceRequest( }; let parameters: CreateDistanceRequestParams; - + let onyxData: OnyxData; if (participants.length > 1) { - const {splitData, splits, onyxData} = createSplitsAndOnyxData( + const {splitData, splits, onyxData: splitOnyxData} = createSplitsAndOnyxData( participants, currentUserLogin ?? '', currentUserAccountID, @@ -4767,6 +4768,7 @@ function createDistanceRequest( billable, CONST.IOU.REQUEST_TYPE.DISTANCE, ); + onyxData = splitOnyxData; // Splits don't use the IOU report param. The split transaction isn't linked to a report shown in the UI, it's linked to a special default reportID of -2. // Therefore, anything related to the IOU report is irrelevant. @@ -4806,7 +4808,7 @@ function createDistanceRequest( transactionThreadReportID, createdReportActionIDForThread, payerEmail, - onyxData, + onyxData: moneyRequestOnyxData, } = getMoneyRequestInformation( currentChatReport, participant, @@ -4829,8 +4831,8 @@ function createDistanceRequest( currentUserEmail, moneyRequestReportID, ); + onyxData = moneyRequestOnyxData; - const activeReportID = isMoneyRequestReport ? report?.reportID ?? '' : chatReport.reportID; parameters = { comment, iouReportID: iouReport.reportID, From d7c54247046fca71a8faa1c0e276a00d1c738f10 Mon Sep 17 00:00:00 2001 From: neil-marcellini Date: Thu, 16 May 2024 14:07:35 -0700 Subject: [PATCH 08/24] Make distance split navigate for report or global create --- src/libs/actions/IOU.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index c2f05bb348ad..4eec736647e2 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -4739,7 +4739,6 @@ 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 activeReportID = isMoneyRequestReport ? report?.reportID ?? '' : chatReport.reportID; const currentChatReport = isMoneyRequestReport ? ReportUtils.getReport(report?.chatReportID) : report; const moneyRequestReportID = isMoneyRequestReport ? report?.reportID : ''; const currentCreated = DateUtils.enrichMoneyRequestTimestamp(created); @@ -4857,6 +4856,7 @@ function createDistanceRequest( } API.write(WRITE_COMMANDS.CREATE_DISTANCE_REQUEST, parameters, onyxData); + const activeReportID = isMoneyRequestReport ? report?.reportID ?? '' : parameters.chatReportID; Navigation.dismissModal(activeReportID); Report.notifyNewAction(activeReportID, userAccountID); } From 8ba2bd1db5da235a9929593dfad1c9809398fe01 Mon Sep 17 00:00:00 2001 From: neil-marcellini Date: Thu, 16 May 2024 14:08:15 -0700 Subject: [PATCH 09/24] Simplify creating distance for split or regular request --- .../step/IOURequestStepConfirmation.tsx | 45 +++++++++---------- 1 file changed, 21 insertions(+), 24 deletions(-) diff --git a/src/pages/iou/request/step/IOURequestStepConfirmation.tsx b/src/pages/iou/request/step/IOURequestStepConfirmation.tsx index 45a331fa0402..60da290310ae 100644 --- a/src/pages/iou/request/step/IOURequestStepConfirmation.tsx +++ b/src/pages/iou/request/step/IOURequestStepConfirmation.tsx @@ -340,6 +340,11 @@ function IOURequestStepConfirmation({ formHasBeenSubmitted.current = true; + if (requestType === CONST.IOU.REQUEST_TYPE.DISTANCE && !IOUUtils.isMovingTransactionFromTrackExpense(action)) { + createDistanceRequest((iouType === CONST.IOU.TYPE.SPLIT) ? splitParticipants : selectedParticipants, trimmedComment); + return; + } + // If we have a receipt let's start the split expense by creating only the action, the transaction, and the group DM if needed if (iouType === CONST.IOU.TYPE.SPLIT && receiptFile) { if (currentUserPersonalDetails.login && !!transaction) { @@ -387,26 +392,22 @@ function IOURequestStepConfirmation({ // If the split expense is created from the global create menu, we also navigate the user to the group report if (iouType === CONST.IOU.TYPE.SPLIT) { if (currentUserPersonalDetails.login && !!transaction) { - if (requestType === CONST.IOU.REQUEST_TYPE.DISTANCE) { - createDistanceRequest(splitParticipants, trimmedComment); - } else { - IOU.splitBillAndOpenReport({ - participants: splitParticipants, - currentUserLogin: currentUserPersonalDetails.login, - currentUserAccountID: currentUserPersonalDetails.accountID, - amount: transaction.amount, - comment: trimmedComment, - currency: transaction.currency, - merchant: transaction.merchant, - created: transaction.created, - category: transaction.category, - tag: transaction.tag, - billable: !!transaction.billable, - iouRequestType: transaction.iouRequestType, - splitShares: transaction.splitShares, - splitPayerAccountIDs: transaction.splitPayerAccountIDs, - }); - } + IOU.splitBillAndOpenReport({ + participants: splitParticipants, + currentUserLogin: currentUserPersonalDetails.login, + currentUserAccountID: currentUserPersonalDetails.accountID, + amount: transaction.amount, + comment: trimmedComment, + currency: transaction.currency, + merchant: transaction.merchant, + created: transaction.created, + category: transaction.category, + tag: transaction.tag, + billable: !!transaction.billable, + iouRequestType: transaction.iouRequestType, + splitShares: transaction.splitShares, + splitPayerAccountIDs: transaction.splitPayerAccountIDs, + }); } return; } @@ -476,10 +477,6 @@ function IOURequestStepConfirmation({ return; } - if (requestType === CONST.IOU.REQUEST_TYPE.DISTANCE && !IOUUtils.isMovingTransactionFromTrackExpense(action)) { - createDistanceRequest(selectedParticipants, trimmedComment); - return; - } requestMoney(selectedParticipants, trimmedComment); }, From 4591003df992d7ad70f6852a1d9d02d61db5ce76 Mon Sep 17 00:00:00 2001 From: neil-marcellini Date: Thu, 16 May 2024 14:47:22 -0700 Subject: [PATCH 10/24] Make customUnitRateID basically required, fix tsc --- .../parameters/CreateDistanceRequestParams.ts | 20 +++++++++---------- src/libs/actions/IOU.ts | 18 ++++++----------- .../request/step/IOURequestStepDistance.tsx | 7 ++++++- 3 files changed, 22 insertions(+), 23 deletions(-) diff --git a/src/libs/API/parameters/CreateDistanceRequestParams.ts b/src/libs/API/parameters/CreateDistanceRequestParams.ts index c8f9f4a80538..07dd594d7356 100644 --- a/src/libs/API/parameters/CreateDistanceRequestParams.ts +++ b/src/libs/API/parameters/CreateDistanceRequestParams.ts @@ -1,23 +1,23 @@ type CreateDistanceRequestParams = { - comment: string; - iouReportID: string; - chatReportID: string; transactionID: string; - reportActionID: string; + chatReportID: string; createdChatReportActionID: string; - createdIOUReportActionID: string; - reportPreviewReportActionID: string; + reportActionID: string; waypoints: string; + customUnitRateID: string; + comment: string; created: string; + iouReportID?: string; + createdIOUReportActionID?: string; + reportPreviewReportActionID?: string; category?: string; tag?: string; taxCode?: string; taxAmount?: number; billable?: boolean; - transactionThreadReportID: string; - createdReportActionIDForThread: string; - payerEmail: string; - customUnitRateID?: string; + transactionThreadReportID?: string; + createdReportActionIDForThread?: string; + payerEmail?: string; splits?: string; chatType?: string; }; diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 4eec736647e2..0fe9347205ef 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -4732,7 +4732,7 @@ function createDistanceRequest( policy?: OnyxEntry, policyTagList?: OnyxEntry, policyCategories?: OnyxEntry, - customUnitRateID?: string, + customUnitRateID = '', currentUserLogin = '', currentUserAccountID = -1, splitShares: SplitShares = {}, @@ -4770,27 +4770,21 @@ function createDistanceRequest( onyxData = splitOnyxData; // Splits don't use the IOU report param. The split transaction isn't linked to a report shown in the UI, it's linked to a special default reportID of -2. - // Therefore, anything related to the IOU report is irrelevant. + // Therefore, any params related to the IOU report are irrelevant and omitted below. parameters = { - comment, - iouReportID: '', - chatReportID: splitData.chatReportID, transactionID: splitData.transactionID, - reportActionID: splitData.reportActionID, + chatReportID: splitData.chatReportID, createdChatReportActionID: splitData.createdReportActionID ?? '', - createdIOUReportActionID: '', - reportPreviewReportActionID: '', // No report preview needed, since the split transaction report is not shown in the UI + reportActionID: splitData.reportActionID, waypoints: JSON.stringify(validWaypoints), + customUnitRateID, + comment, created: currentCreated, category, tag, taxCode, taxAmount, billable, - transactionThreadReportID: '', - createdReportActionIDForThread: '', - customUnitRateID, - payerEmail: '', splits: JSON.stringify(splits), chatType: splitData.chatType, }; diff --git a/src/pages/iou/request/step/IOURequestStepDistance.tsx b/src/pages/iou/request/step/IOURequestStepDistance.tsx index 0d5972cffcb9..a15ca9809d5b 100644 --- a/src/pages/iou/request/step/IOURequestStepDistance.tsx +++ b/src/pages/iou/request/step/IOURequestStepDistance.tsx @@ -37,6 +37,7 @@ import StepScreenWrapper from './StepScreenWrapper'; import withFullTransactionOrNotFound from './withFullTransactionOrNotFound'; import type {WithWritableReportOrNotFoundProps} from './withWritableReportOrNotFound'; import withWritableReportOrNotFound from './withWritableReportOrNotFound'; +import DistanceRequestUtils from '@libs/DistanceRequestUtils'; type IOURequestStepDistanceOnyxProps = { /** backup version of the original transaction */ @@ -278,7 +279,7 @@ function IOURequestStepDistance({ IOU.createDistanceRequest( report, - participants[0], + participants, '', transaction?.created ?? '', '', @@ -290,6 +291,10 @@ function IOURequestStepDistance({ translate('iou.fieldPending'), false, TransactionUtils.getValidWaypoints(waypoints, true), + undefined, + undefined, + undefined, + DistanceRequestUtils.getCustomUnitRateID(report.reportID), ); return; } From 336172b25ce7a6098553a6cd388f8e776418a402 Mon Sep 17 00:00:00 2001 From: neil-marcellini Date: Thu, 20 Jun 2024 14:36:30 -0700 Subject: [PATCH 11/24] Remove accidentally committed file --- src/libs/actions/creatDistance.ts | 91 ------------------------------- 1 file changed, 91 deletions(-) delete mode 100644 src/libs/actions/creatDistance.ts diff --git a/src/libs/actions/creatDistance.ts b/src/libs/actions/creatDistance.ts deleted file mode 100644 index 42263b01b366..000000000000 --- a/src/libs/actions/creatDistance.ts +++ /dev/null @@ -1,91 +0,0 @@ -/** Requests money based on a distance (e.g. mileage from a map) */ -function createDistanceRequest( - report: OnyxEntry, - participant: Participant, - comment: string, - created: string, - category: string | undefined, - tag: string | undefined, - taxCode: string | undefined, - taxAmount: number | undefined, - amount: number, - currency: string, - merchant: string, - billable: boolean | undefined, - validWaypoints: WaypointCollection, - policy?: OnyxEntry, - policyTagList?: OnyxEntry, - policyCategories?: OnyxEntry, - customUnitRateID?: string, -) { - // 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 moneyRequestReportID = isMoneyRequestReport ? report?.reportID : ''; - - const optimisticReceipt: Receipt = { - source: ReceiptGeneric as ReceiptSource, - state: CONST.IOU.RECEIPT_STATE.OPEN, - }; - const { - iouReport, - chatReport, - transaction, - iouAction, - createdChatReportActionID, - createdIOUReportActionID, - reportPreviewAction, - transactionThreadReportID, - createdReportActionIDForThread, - payerEmail, - onyxData, - } = getMoneyRequestInformation( - currentChatReport, - participant, - comment, - amount, - currency, - created, - merchant, - optimisticReceipt, - undefined, - category, - tag, - taxCode, - taxAmount, - billable, - policy, - policyTagList, - policyCategories, - userAccountID, - currentUserEmail, - moneyRequestReportID, - ); - - const activeReportID = isMoneyRequestReport ? report?.reportID ?? '-1' : chatReport.reportID; - const parameters: CreateDistanceRequestParams = { - comment, - iouReportID: iouReport.reportID, - chatReportID: chatReport.reportID, - transactionID: transaction.transactionID, - reportActionID: iouAction.reportActionID, - createdChatReportActionID, - createdIOUReportActionID, - reportPreviewReportActionID: reportPreviewAction.reportActionID, - waypoints: JSON.stringify(validWaypoints), - created, - category, - tag, - taxCode, - taxAmount, - billable, - transactionThreadReportID, - createdReportActionIDForThread, - payerEmail, - customUnitRateID, - }; - - API.write(WRITE_COMMANDS.CREATE_DISTANCE_REQUEST, parameters, onyxData); - Navigation.dismissModal(activeReportID); - Report.notifyNewAction(activeReportID, userAccountID); -} \ No newline at end of file From 88915134fba1bcd0552b041eb33cfa3ebbdd4926 Mon Sep 17 00:00:00 2001 From: neil-marcellini Date: Fri, 28 Jun 2024 16:14:45 -0700 Subject: [PATCH 12/24] Fix creating workspace distance split and other cleanups --- src/libs/actions/IOU.ts | 3 ++- src/pages/iou/request/step/IOURequestStepConfirmation.tsx | 6 ++++-- src/pages/iou/request/step/IOURequestStepDistance.tsx | 4 ++++ 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 222b24771c2f..1b46c51da568 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -4880,6 +4880,7 @@ function createDistanceRequest( currentUserLogin = '', currentUserAccountID = -1, splitShares: SplitShares = {}, + iouType: ValueOf = CONST.IOU.TYPE.SUBMIT, ) { // 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); @@ -4893,7 +4894,7 @@ function createDistanceRequest( let parameters: CreateDistanceRequestParams; let onyxData: OnyxData; - if (participants.length > 1) { + if (iouType === CONST.IOU.TYPE.SPLIT) { const {splitData, splits, onyxData: splitOnyxData} = createSplitsAndOnyxData( participants, currentUserLogin ?? '', diff --git a/src/pages/iou/request/step/IOURequestStepConfirmation.tsx b/src/pages/iou/request/step/IOURequestStepConfirmation.tsx index 2a8998a17bf3..d09894bd8bb0 100644 --- a/src/pages/iou/request/step/IOURequestStepConfirmation.tsx +++ b/src/pages/iou/request/step/IOURequestStepConfirmation.tsx @@ -296,7 +296,6 @@ function IOURequestStepConfirmation({ if (!transaction) { return; } - const customUnitRateID = TransactionUtils.getRateID(transaction) ?? ''; IOU.createDistanceRequest( report, selectedParticipants, @@ -318,9 +317,10 @@ function IOURequestStepConfirmation({ currentUserPersonalDetails.login, currentUserPersonalDetails.accountID, transaction.splitShares, + iouType, ); }, - [policy, policyCategories, policyTags, report, transaction, transactionTaxCode, transactionTaxAmount, customUnitRateID, currentUserPersonalDetails], + [policy, policyCategories, policyTags, report, transaction, transactionTaxCode, transactionTaxAmount, customUnitRateID, currentUserPersonalDetails, iouType], ); const createTransaction = useCallback( @@ -514,6 +514,8 @@ function IOURequestStepConfirmation({ policyCategories, transactionTaxAmount, transactionTaxCode, + action, + requestType, ], ); diff --git a/src/pages/iou/request/step/IOURequestStepDistance.tsx b/src/pages/iou/request/step/IOURequestStepDistance.tsx index 259390b97c28..7b1f5f7ef66f 100644 --- a/src/pages/iou/request/step/IOURequestStepDistance.tsx +++ b/src/pages/iou/request/step/IOURequestStepDistance.tsx @@ -295,6 +295,10 @@ function IOURequestStepDistance({ undefined, undefined, DistanceRequestUtils.getCustomUnitRateID(report.reportID), + currentUserPersonalDetails.login ?? '', + currentUserPersonalDetails.accountID, + transaction?.splitShares, + iouType, ); return; } From 29a01f343e7a791751f061ae49701dc1928e1f26 Mon Sep 17 00:00:00 2001 From: neil-marcellini Date: Mon, 1 Jul 2024 19:16:05 -0700 Subject: [PATCH 13/24] Build optimistic receipt for distance split, and move it inside optimistic transaction function --- src/libs/TransactionUtils.ts | 4 ++-- src/libs/actions/IOU.ts | 19 ++++++++----------- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/src/libs/TransactionUtils.ts b/src/libs/TransactionUtils.ts index b28e5b782965..23ff5d78008b 100644 --- a/src/libs/TransactionUtils.ts +++ b/src/libs/TransactionUtils.ts @@ -147,8 +147,8 @@ function buildOptimisticTransaction( merchant: merchant || CONST.TRANSACTION.PARTIAL_TRANSACTION_MERCHANT, created: created || DateUtils.getDBTime(), pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, - receipt, - filename, + receipt: receipt?.source ? {source: receipt.source, state: receipt.state ?? CONST.IOU.RECEIPT_STATE.SCANREADY} : {}, + filename: receipt?.source ?? receipt?.name ?? filename, category, tag, taxCode, diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 1b46c51da568..dee07ba59733 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -1955,14 +1955,8 @@ function getMoneyRequestInformation( } else { iouReport = IOUUtils.updateIOUOwnerAndTotal(iouReport, payeeAccountID, amount, currency); } - // STEP 3: Build optimistic receipt and transaction - const receiptObject: Receipt = {}; - let filename; - if (receipt?.source) { - receiptObject.source = receipt.source; - receiptObject.state = receipt.state ?? CONST.IOU.RECEIPT_STATE.SCANREADY; - filename = receipt.name; - } + + // STEP 3: Build an optimistic transaction with the receipt const existingTransaction = allTransactionDrafts[`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${existingTransactionID ?? CONST.IOU.OPTIMISTIC_TRANSACTION_ID}`]; const isDistanceRequest = existingTransaction && existingTransaction.iouRequestType === CONST.IOU.REQUEST_TYPE.DISTANCE; let optimisticTransaction = TransactionUtils.buildOptimisticTransaction( @@ -1974,8 +1968,8 @@ function getMoneyRequestInformation( '', '', merchant, - receiptObject, - filename, + receipt, + '', existingTransactionID, category, tag, @@ -3758,6 +3752,9 @@ function createSplitsAndOnyxData( const {splitChatReport, existingSplitChatReport} = getOrCreateOptimisticSplitChatReport(existingSplitChatReportID, participants, participantAccountIDs, currentUserAccountID); const isOwnPolicyExpenseChat = !!splitChatReport.isOwnPolicyExpenseChat; + // Pass an open receipt so the distance expense will show a map with the route optimistically + const receipt: Receipt|undefined = (iouRequestType === CONST.IOU.REQUEST_TYPE.DISTANCE) ? {source: ReceiptGeneric as ReceiptSource, state: CONST.IOU.RECEIPT_STATE.OPEN} : undefined; + const splitTransaction = TransactionUtils.buildOptimisticTransaction( amount, currency, @@ -3767,7 +3764,7 @@ function createSplitsAndOnyxData( '', '', merchant || Localize.translateLocal('iou.expense'), - undefined, + receipt, undefined, undefined, category, From dc2d0dad89c161a0a7068790a5097b5c42298364 Mon Sep 17 00:00:00 2001 From: neil-marcellini Date: Tue, 2 Jul 2024 08:37:21 -0700 Subject: [PATCH 14/24] Use existing split chat reportID for distance if present --- src/libs/actions/IOU.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index dee07ba59733..93accc320928 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -4904,7 +4904,7 @@ function createDistanceRequest( category ?? '', tag ?? '', splitShares, - '', + report?.reportID ?? '', billable, CONST.IOU.REQUEST_TYPE.DISTANCE, ); From a981882edd5cdafbcad5b85a69723ec0de862719 Mon Sep 17 00:00:00 2001 From: neil-marcellini Date: Tue, 2 Jul 2024 16:01:14 -0700 Subject: [PATCH 15/24] Prevent using an existingSplitChatReport with undefined id --- src/libs/actions/IOU.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 93accc320928..ef6d4d314888 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -3676,7 +3676,7 @@ function trackExpense( function getOrCreateOptimisticSplitChatReport(existingSplitChatReportID: string, participants: Participant[], participantAccountIDs: number[], currentUserAccountID: number) { // The existing chat report could be passed as reportID or exist on the sole "participant" (in this case a report option) - const existingChatReportID = existingSplitChatReportID || participants[0].reportID; + const existingChatReportID = existingSplitChatReportID ?? participants[0].reportID ?? ''; // Check if the report is available locally if we do have one let existingSplitChatReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${existingChatReportID}`]; From 41dec6aa4c202ef8d8e91ebc04528168c1afaca2 Mon Sep 17 00:00:00 2001 From: neil-marcellini Date: Tue, 2 Jul 2024 18:37:25 -0700 Subject: [PATCH 16/24] Set optimistic data marking split as a distance transaction --- src/libs/actions/IOU.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index ef6d4d314888..35b94d067c3c 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -3755,7 +3755,7 @@ function createSplitsAndOnyxData( // Pass an open receipt so the distance expense will show a map with the route optimistically const receipt: Receipt|undefined = (iouRequestType === CONST.IOU.REQUEST_TYPE.DISTANCE) ? {source: ReceiptGeneric as ReceiptSource, state: CONST.IOU.RECEIPT_STATE.OPEN} : undefined; - const splitTransaction = TransactionUtils.buildOptimisticTransaction( + let splitTransaction = TransactionUtils.buildOptimisticTransaction( amount, currency, CONST.REPORT.SPLIT_REPORTID, @@ -3774,6 +3774,13 @@ function createSplitsAndOnyxData( billable, ); + // Important data is set on the draft distance transaction, such as the iouRequestType marking it as a distance request, so merge it into the optimistic split transaction + const existingTransaction = allTransactionDrafts[`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${CONST.IOU.OPTIMISTIC_TRANSACTION_ID}`]; + const isDistanceRequest = existingTransaction && existingTransaction.iouRequestType === CONST.IOU.REQUEST_TYPE.DISTANCE; + if (isDistanceRequest) { + splitTransaction = fastMerge(existingTransaction, splitTransaction, false); + } + // Note: The created action must be optimistically generated before the IOU action so there's no chance that the created action appears after the IOU action in the chat const splitCreatedReportAction = ReportUtils.buildOptimisticCreatedReportAction(currentUserEmailForIOUSplit); const splitIOUReportAction = ReportUtils.buildOptimisticIOUReportAction( From 1b10e4026f4b213737cedc0dc9aabce9d7055cfd Mon Sep 17 00:00:00 2001 From: neil-marcellini Date: Tue, 2 Jul 2024 18:46:22 -0700 Subject: [PATCH 17/24] Set waypoints pending so optimistic route shows --- src/libs/actions/IOU.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 35b94d067c3c..9ca7167e9544 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -3755,6 +3755,8 @@ function createSplitsAndOnyxData( // Pass an open receipt so the distance expense will show a map with the route optimistically const receipt: Receipt|undefined = (iouRequestType === CONST.IOU.REQUEST_TYPE.DISTANCE) ? {source: ReceiptGeneric as ReceiptSource, state: CONST.IOU.RECEIPT_STATE.OPEN} : undefined; + const existingTransaction = allTransactionDrafts[`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${CONST.IOU.OPTIMISTIC_TRANSACTION_ID}`]; + const isDistanceRequest = existingTransaction && existingTransaction.iouRequestType === CONST.IOU.REQUEST_TYPE.DISTANCE; let splitTransaction = TransactionUtils.buildOptimisticTransaction( amount, currency, @@ -3772,11 +3774,10 @@ function createSplitsAndOnyxData( taxCode, taxAmount, billable, + isDistanceRequest ? {waypoints: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD} : undefined, ); // Important data is set on the draft distance transaction, such as the iouRequestType marking it as a distance request, so merge it into the optimistic split transaction - const existingTransaction = allTransactionDrafts[`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${CONST.IOU.OPTIMISTIC_TRANSACTION_ID}`]; - const isDistanceRequest = existingTransaction && existingTransaction.iouRequestType === CONST.IOU.REQUEST_TYPE.DISTANCE; if (isDistanceRequest) { splitTransaction = fastMerge(existingTransaction, splitTransaction, false); } From c6909a46fcc0a15de3285a7239495933b07b0763 Mon Sep 17 00:00:00 2001 From: neil-marcellini Date: Wed, 3 Jul 2024 11:26:47 -0700 Subject: [PATCH 18/24] Clear pending fields after split --- src/libs/actions/IOU.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 9ca7167e9544..8c80a6f68500 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -3876,7 +3876,7 @@ function createSplitsAndOnyxData( { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.TRANSACTION}${splitTransaction.transactionID}`, - value: {pendingAction: null}, + value: {pendingAction: null, pendingFields: null}, }, ]; @@ -3895,6 +3895,8 @@ function createSplitsAndOnyxData( key: `${ONYXKEYS.COLLECTION.TRANSACTION}${splitTransaction.transactionID}`, value: { errors: ErrorUtils.getMicroSecondOnyxErrorWithTranslationKey('iou.error.genericCreateFailureMessage'), + pendingAction: null, + pendingFields: null, }, }, ]; @@ -3971,6 +3973,7 @@ function createSplitsAndOnyxData( // or, if the split is being made from the workspace chat, then the oneOnOneChatReport is the same as the splitChatReport // in this case existingSplitChatReport will belong to the policy expense chat and we won't be // entering code that creates optimistic personal details + console.log('Ndebug participant data', {participant, hasMultipleParticipants, existingSplitChatReportID, isOwnPolicyExpenseChat, accountID, currentUserAccountID}); if ((!hasMultipleParticipants && !existingSplitChatReportID) || isOwnPolicyExpenseChat) { oneOnOneChatReport = splitChatReport; shouldCreateOptimisticPersonalDetails = !existingSplitChatReport && !personalDetailExists; @@ -3978,6 +3981,7 @@ function createSplitsAndOnyxData( const existingChatReport = ReportUtils.getChatByParticipants([accountID, currentUserAccountID]); isNewOneOnOneChatReport = !existingChatReport; shouldCreateOptimisticPersonalDetails = isNewOneOnOneChatReport && !personalDetailExists; + console.log('Ndebug existingChatReport', existingChatReport); oneOnOneChatReport = existingChatReport ?? ReportUtils.buildOptimisticChatReport([accountID, currentUserAccountID]); } From 8c6175967d6b728d37df581963972427d276b979 Mon Sep 17 00:00:00 2001 From: neil-marcellini Date: Wed, 3 Jul 2024 11:28:00 -0700 Subject: [PATCH 19/24] Show optimistic distance route when the request failed --- src/components/ReportActionItem/ReportActionItemImage.tsx | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/components/ReportActionItem/ReportActionItemImage.tsx b/src/components/ReportActionItem/ReportActionItemImage.tsx index 1251be83994b..a20654947d4a 100644 --- a/src/components/ReportActionItem/ReportActionItemImage.tsx +++ b/src/components/ReportActionItem/ReportActionItemImage.tsx @@ -20,6 +20,7 @@ import variables from '@styles/variables'; import CONST from '@src/CONST'; import ROUTES from '@src/ROUTES'; import type {Transaction} from '@src/types/onyx'; +import { isEmptyObject } from '@src/types/utils/EmptyObject'; type ReportActionItemImageProps = { /** thumbnail URI for the image */ @@ -75,7 +76,8 @@ function ReportActionItemImage({ const {translate} = useLocalize(); const isDistanceRequest = !!transaction && TransactionUtils.isDistanceRequest(transaction); const hasPendingWaypoints = transaction && TransactionUtils.isFetchingWaypointsFromServer(transaction); - const showMapAsImage = isDistanceRequest && hasPendingWaypoints; + const hasErrors = !isEmptyObject(transaction?.errors) || !isEmptyObject(transaction?.errorFields); + const showMapAsImage = isDistanceRequest && (hasErrors || hasPendingWaypoints); if (showMapAsImage) { return ( From 5188d489fe7738e1ad7366ef9ee5216d8c7b1a31 Mon Sep 17 00:00:00 2001 From: neil-marcellini Date: Tue, 9 Jul 2024 10:59:42 -0700 Subject: [PATCH 20/24] Only allow global create distance for non-splits --- src/pages/iou/request/IOURequestStartPage.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pages/iou/request/IOURequestStartPage.tsx b/src/pages/iou/request/IOURequestStartPage.tsx index f320b013f2b3..b1428da962ec 100644 --- a/src/pages/iou/request/IOURequestStartPage.tsx +++ b/src/pages/iou/request/IOURequestStartPage.tsx @@ -101,7 +101,7 @@ function IOURequestStartPage({ const isExpenseChat = ReportUtils.isPolicyExpenseChat(report); const isExpenseReport = ReportUtils.isExpenseReport(report); - const shouldDisplayDistanceRequest = !!canUseP2PDistanceRequests || isExpenseChat || isExpenseReport || isFromGlobalCreate; + const shouldDisplayDistanceRequest = !!canUseP2PDistanceRequests || isExpenseChat || isExpenseReport || (isFromGlobalCreate && iouType !== CONST.IOU.TYPE.SPLIT); const navigateBack = () => { Navigation.closeRHPFlow(); From ff2f2583432b57898d71db63370fa7af25d52f89 Mon Sep 17 00:00:00 2001 From: neil-marcellini Date: Wed, 10 Jul 2024 09:20:33 -0700 Subject: [PATCH 21/24] Fix missed replacement of changed variable --- src/libs/actions/IOU.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 7239c702c884..188743492468 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -2010,7 +2010,7 @@ function getMoneyRequestInformation( undefined, false, false, - receiptObject, + optimisticTransaction.receipt, false, undefined, linkedTrackedExpenseReportAction?.childReportID, From e2809f44f7c3bb9bfaec6e68dc33ed99d268c506 Mon Sep 17 00:00:00 2001 From: neil-marcellini Date: Wed, 10 Jul 2024 09:26:16 -0700 Subject: [PATCH 22/24] Prettify --- src/components/MoneyRequestConfirmationList.tsx | 5 +---- src/components/ReportActionItem/ReportActionItemImage.tsx | 2 +- src/libs/actions/IOU.ts | 8 ++++++-- src/pages/iou/request/step/IOURequestStepConfirmation.tsx | 2 +- src/pages/iou/request/step/IOURequestStepDistance.tsx | 2 +- 5 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/components/MoneyRequestConfirmationList.tsx b/src/components/MoneyRequestConfirmationList.tsx index b8175dd2ed3a..af8959f9278e 100755 --- a/src/components/MoneyRequestConfirmationList.tsx +++ b/src/components/MoneyRequestConfirmationList.tsx @@ -262,10 +262,7 @@ function MoneyRequestConfirmationList({ const distanceRequestAmount = DistanceRequestUtils.getDistanceRequestAmount(distance, unit ?? CONST.CUSTOM_UNITS.DISTANCE_UNIT_MILES, rate ?? 0); const formattedAmount = isDistanceRequestWithPendingRoute ? '' - : CurrencyUtils.convertToDisplayString( - shouldCalculateDistanceAmount ? distanceRequestAmount : iouAmount, - isDistanceRequest ? currency : iouCurrencyCode, - ); + : CurrencyUtils.convertToDisplayString(shouldCalculateDistanceAmount ? distanceRequestAmount : iouAmount, isDistanceRequest ? currency : iouCurrencyCode); const previousTransactionAmount = usePrevious(transaction?.amount); const previousTransactionCurrency = usePrevious(transaction?.currency); diff --git a/src/components/ReportActionItem/ReportActionItemImage.tsx b/src/components/ReportActionItem/ReportActionItemImage.tsx index a20654947d4a..4f58b193eeb2 100644 --- a/src/components/ReportActionItem/ReportActionItemImage.tsx +++ b/src/components/ReportActionItem/ReportActionItemImage.tsx @@ -20,7 +20,7 @@ import variables from '@styles/variables'; import CONST from '@src/CONST'; import ROUTES from '@src/ROUTES'; import type {Transaction} from '@src/types/onyx'; -import { isEmptyObject } from '@src/types/utils/EmptyObject'; +import {isEmptyObject} from '@src/types/utils/EmptyObject'; type ReportActionItemImageProps = { /** thumbnail URI for the image */ diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 188743492468..e0d8fcafe34e 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -3777,7 +3777,7 @@ function createSplitsAndOnyxData( const isOwnPolicyExpenseChat = !!splitChatReport.isOwnPolicyExpenseChat; // Pass an open receipt so the distance expense will show a map with the route optimistically - const receipt: Receipt|undefined = (iouRequestType === CONST.IOU.REQUEST_TYPE.DISTANCE) ? {source: ReceiptGeneric as ReceiptSource, state: CONST.IOU.RECEIPT_STATE.OPEN} : undefined; + const receipt: Receipt | undefined = iouRequestType === CONST.IOU.REQUEST_TYPE.DISTANCE ? {source: ReceiptGeneric as ReceiptSource, state: CONST.IOU.RECEIPT_STATE.OPEN} : undefined; const existingTransaction = allTransactionDrafts[`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${CONST.IOU.OPTIMISTIC_TRANSACTION_ID}`]; const isDistanceRequest = existingTransaction && existingTransaction.iouRequestType === CONST.IOU.REQUEST_TYPE.DISTANCE; @@ -4931,7 +4931,11 @@ function createDistanceRequest( let parameters: CreateDistanceRequestParams; let onyxData: OnyxData; if (iouType === CONST.IOU.TYPE.SPLIT) { - const {splitData, splits, onyxData: splitOnyxData} = createSplitsAndOnyxData( + const { + splitData, + splits, + onyxData: splitOnyxData, + } = createSplitsAndOnyxData( participants, currentUserLogin ?? '', currentUserAccountID, diff --git a/src/pages/iou/request/step/IOURequestStepConfirmation.tsx b/src/pages/iou/request/step/IOURequestStepConfirmation.tsx index c02754c4eadb..9b77894cf561 100644 --- a/src/pages/iou/request/step/IOURequestStepConfirmation.tsx +++ b/src/pages/iou/request/step/IOURequestStepConfirmation.tsx @@ -344,7 +344,7 @@ function IOURequestStepConfirmation({ formHasBeenSubmitted.current = true; if (requestType === CONST.IOU.REQUEST_TYPE.DISTANCE && !IOUUtils.isMovingTransactionFromTrackExpense(action)) { - createDistanceRequest((iouType === CONST.IOU.TYPE.SPLIT) ? splitParticipants : selectedParticipants, trimmedComment); + createDistanceRequest(iouType === CONST.IOU.TYPE.SPLIT ? splitParticipants : selectedParticipants, trimmedComment); return; } diff --git a/src/pages/iou/request/step/IOURequestStepDistance.tsx b/src/pages/iou/request/step/IOURequestStepDistance.tsx index a48d6454c002..b3aead35f53c 100644 --- a/src/pages/iou/request/step/IOURequestStepDistance.tsx +++ b/src/pages/iou/request/step/IOURequestStepDistance.tsx @@ -17,6 +17,7 @@ import useLocalize from '@hooks/useLocalize'; import useNetwork from '@hooks/useNetwork'; import usePrevious from '@hooks/usePrevious'; import useThemeStyles from '@hooks/useThemeStyles'; +import DistanceRequestUtils from '@libs/DistanceRequestUtils'; import * as ErrorUtils from '@libs/ErrorUtils'; import Navigation from '@libs/Navigation/Navigation'; import * as OptionsListUtils from '@libs/OptionsListUtils'; @@ -37,7 +38,6 @@ import StepScreenWrapper from './StepScreenWrapper'; import withFullTransactionOrNotFound from './withFullTransactionOrNotFound'; import type {WithWritableReportOrNotFoundProps} from './withWritableReportOrNotFound'; import withWritableReportOrNotFound from './withWritableReportOrNotFound'; -import DistanceRequestUtils from '@libs/DistanceRequestUtils'; type IOURequestStepDistanceOnyxProps = { /** backup version of the original transaction */ From 0c865389313e33905d07171bd999023f79cc716d Mon Sep 17 00:00:00 2001 From: neil-marcellini Date: Wed, 10 Jul 2024 09:40:38 -0700 Subject: [PATCH 23/24] Remove leftover debug console logs --- src/libs/actions/IOU.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index e0d8fcafe34e..89495ef38523 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -3997,7 +3997,6 @@ function createSplitsAndOnyxData( // or, if the split is being made from the workspace chat, then the oneOnOneChatReport is the same as the splitChatReport // in this case existingSplitChatReport will belong to the policy expense chat and we won't be // entering code that creates optimistic personal details - console.log('Ndebug participant data', {participant, hasMultipleParticipants, existingSplitChatReportID, isOwnPolicyExpenseChat, accountID, currentUserAccountID}); if ((!hasMultipleParticipants && !existingSplitChatReportID) || isOwnPolicyExpenseChat) { oneOnOneChatReport = splitChatReport; shouldCreateOptimisticPersonalDetails = !existingSplitChatReport && !personalDetailExists; @@ -4005,7 +4004,6 @@ function createSplitsAndOnyxData( const existingChatReport = ReportUtils.getChatByParticipants([accountID, currentUserAccountID]); isNewOneOnOneChatReport = !existingChatReport; shouldCreateOptimisticPersonalDetails = isNewOneOnOneChatReport && !personalDetailExists; - console.log('Ndebug existingChatReport', existingChatReport); oneOnOneChatReport = existingChatReport ?? ReportUtils.buildOptimisticChatReport([accountID, currentUserAccountID]); } From 55ed6c7e37a9cfe05d8761fb96c95aff87aab6f0 Mon Sep 17 00:00:00 2001 From: neil-marcellini Date: Fri, 2 Aug 2024 16:53:12 -0700 Subject: [PATCH 24/24] Remove redundant call to createDistanceRequest --- .../iou/request/step/IOURequestStepConfirmation.tsx | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/pages/iou/request/step/IOURequestStepConfirmation.tsx b/src/pages/iou/request/step/IOURequestStepConfirmation.tsx index 509f934ae7c8..982cb4f3eab6 100644 --- a/src/pages/iou/request/step/IOURequestStepConfirmation.tsx +++ b/src/pages/iou/request/step/IOURequestStepConfirmation.tsx @@ -360,7 +360,7 @@ function IOURequestStepConfirmation({ formHasBeenSubmitted.current = true; playSound(SOUNDS.DONE); - if (requestType === CONST.IOU.REQUEST_TYPE.DISTANCE && !IOUUtils.isMovingTransactionFromTrackExpense(action)) { + if (isDistanceRequest && !isMovingTransactionFromTrackExpense) { createDistanceRequest(iouType === CONST.IOU.TYPE.SPLIT ? splitParticipants : selectedParticipants, trimmedComment); return; } @@ -503,11 +503,6 @@ function IOURequestStepConfirmation({ return; } - if (isDistanceRequest && !isMovingTransactionFromTrackExpense) { - createDistanceRequest(selectedParticipants, trimmedComment); - return; - } - requestMoney(selectedParticipants, trimmedComment); }, [ @@ -529,8 +524,6 @@ function IOURequestStepConfirmation({ policyCategories, transactionTaxAmount, transactionTaxCode, - action, - requestType, ], );