diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index c094873379b9..696853f49fd7 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -284,6 +284,32 @@ type BuildOnyxDataForMoneyRequestParams = { optimisticParams: MoneyRequestOptimisticParams; }; +type DistanceRequestTransactionParams = { + comment: string; + created: string; + category?: string; + tag?: string; + taxCode?: string; + taxAmount?: number; + amount: number; + currency: string; + merchant: string; + billable?: boolean; + validWaypoints: WaypointCollection; + customUnitRateID?: string; + splitShares?: SplitShares; +}; +type CreateDistanceRequestInformation = { + report: OnyxEntry; + participants: Participant[]; + currentUserLogin?: string; + currentUserAccountID?: number; + iouType?: ValueOf; + existingTransaction?: OnyxEntry; + transactionParams: DistanceRequestTransactionParams; + policyParams?: RequestMoneyPolicyParams; +}; + let allPersonalDetails: OnyxTypes.PersonalDetailsList = {}; Onyx.connect({ key: ONYXKEYS.PERSONAL_DETAILS_LIST, @@ -5347,30 +5373,20 @@ function setDraftSplitTransaction(transactionID: string, transactionChanges: Tra } /** 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 = '', - currentUserLogin = '', - currentUserAccountID = -1, - splitShares: SplitShares = {}, - iouType: ValueOf = CONST.IOU.TYPE.SUBMIT, - existingTransaction: OnyxEntry | undefined = undefined, -) { +function createDistanceRequest(distanceRequestInformation: CreateDistanceRequestInformation) { + const { + report, + participants, + currentUserLogin = '', + currentUserAccountID = -1, + iouType = CONST.IOU.TYPE.SUBMIT, + existingTransaction, + transactionParams, + policyParams = {}, + } = distanceRequestInformation; + const {policy, policyCategories, policyTagList} = policyParams; + const {amount, comment, currency, created, category, tag, taxAmount, taxCode, merchant, billable, validWaypoints, customUnitRateID = '', splitShares = {}} = transactionParams; + // 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.getReportOrDraftReport(report?.chatReportID) : report; diff --git a/src/pages/iou/request/step/IOURequestStepConfirmation.tsx b/src/pages/iou/request/step/IOURequestStepConfirmation.tsx index 989277bb5fc1..15aef60bf3d3 100644 --- a/src/pages/iou/request/step/IOURequestStepConfirmation.tsx +++ b/src/pages/iou/request/step/IOURequestStepConfirmation.tsx @@ -320,30 +320,34 @@ function IOURequestStepConfirmation({ if (!transaction) { return; } - IOU.createDistanceRequest( + IOU.createDistanceRequest({ report, - selectedParticipants, - trimmedComment, - transaction.created, - transaction.category, - transaction.tag, - transactionTaxCode, - transactionTaxAmount, - transaction.amount, - transaction.currency, - transaction.merchant, - transaction.billable, - TransactionUtils.getValidWaypoints(transaction.comment?.waypoints, true), - policy, - policyTags, - policyCategories, - customUnitRateID, - currentUserPersonalDetails.login, - currentUserPersonalDetails.accountID, - transaction.splitShares, + participants: selectedParticipants, + currentUserLogin: currentUserPersonalDetails.login, + currentUserAccountID: currentUserPersonalDetails.accountID, iouType, - transaction, - ); + existingTransaction: transaction, + policyParams: { + policy, + policyCategories, + policyTagList: policyTags, + }, + transactionParams: { + amount: transaction.amount, + comment: trimmedComment, + created: transaction.created, + currency: transaction.currency, + merchant: transaction.merchant, + category: transaction.category, + tag: transaction.tag, + taxCode: transactionTaxCode, + taxAmount: transactionTaxAmount, + customUnitRateID, + splitShares: transaction.splitShares, + validWaypoints: TransactionUtils.getValidWaypoints(transaction.comment?.waypoints, true), + billable: transaction.billable, + }, + }); }, [policy, policyCategories, policyTags, report, transaction, transactionTaxCode, transactionTaxAmount, customUnitRateID, currentUserPersonalDetails, iouType], ); diff --git a/src/pages/iou/request/step/IOURequestStepDistance.tsx b/src/pages/iou/request/step/IOURequestStepDistance.tsx index 461ed6298c84..305ba9a9a064 100644 --- a/src/pages/iou/request/step/IOURequestStepDistance.tsx +++ b/src/pages/iou/request/step/IOURequestStepDistance.tsx @@ -341,30 +341,25 @@ function IOURequestStepDistance({ } playSound(SOUNDS.DONE); - IOU.createDistanceRequest( + IOU.createDistanceRequest({ report, participants, - '', - transaction?.created ?? '', - '', - '', - '', - 0, - 0, - transaction?.currency ?? 'USD', - translate('iou.fieldPending'), - !!policy?.defaultBillable, - TransactionUtils.getValidWaypoints(waypoints, true), - undefined, - undefined, - undefined, - DistanceRequestUtils.getCustomUnitRateID(report.reportID), - currentUserPersonalDetails.login ?? '', - currentUserPersonalDetails.accountID, - transaction?.splitShares, + currentUserLogin: currentUserPersonalDetails.login, + currentUserAccountID: currentUserPersonalDetails.accountID, iouType, - transaction, - ); + existingTransaction: transaction, + transactionParams: { + amount: 0, + comment: '', + created: transaction?.created ?? '', + currency: transaction?.currency ?? 'USD', + merchant: translate('iou.fieldPending'), + billable: !!policy?.defaultBillable, + validWaypoints: TransactionUtils.getValidWaypoints(waypoints, true), + customUnitRateID: DistanceRequestUtils.getCustomUnitRateID(report.reportID), + splitShares: transaction?.splitShares, + }, + }); return; } IOU.setMoneyRequestParticipantsFromReport(transactionID, report);