From 846da9c1131fc70e4ba99534f104140c8ab79bf3 Mon Sep 17 00:00:00 2001 From: mkzie2 Date: Thu, 12 Dec 2024 15:17:15 +0700 Subject: [PATCH 1/2] refactor createDistanceRequest function --- src/libs/actions/IOU.ts | 64 ++++++++++++------- .../step/IOURequestStepConfirmation.tsx | 48 +++++++------- .../request/step/IOURequestStepDistance.tsx | 60 +++++++++++------ 3 files changed, 105 insertions(+), 67 deletions(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 6b45283421ea..084b13ac4ca2 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -289,6 +289,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, @@ -5352,30 +5378,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 39634eb061b9..46f1d427f1d9 100644 --- a/src/pages/iou/request/step/IOURequestStepConfirmation.tsx +++ b/src/pages/iou/request/step/IOURequestStepConfirmation.tsx @@ -333,30 +333,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 9e18a74246e9..f5b11ed69943 100644 --- a/src/pages/iou/request/step/IOURequestStepDistance.tsx +++ b/src/pages/iou/request/step/IOURequestStepDistance.tsx @@ -341,30 +341,48 @@ function IOURequestStepDistance({ } playSound(SOUNDS.DONE); - 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, + // iouType, + // transaction, + // ); + 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); From 61c58137e9a471d76f3e04a1a7f26fe6c0925020 Mon Sep 17 00:00:00 2001 From: mkzie2 Date: Mon, 16 Dec 2024 12:26:25 +0700 Subject: [PATCH 2/2] remove comment --- .../request/step/IOURequestStepDistance.tsx | 23 ------------------- 1 file changed, 23 deletions(-) diff --git a/src/pages/iou/request/step/IOURequestStepDistance.tsx b/src/pages/iou/request/step/IOURequestStepDistance.tsx index e4634c9b9979..305ba9a9a064 100644 --- a/src/pages/iou/request/step/IOURequestStepDistance.tsx +++ b/src/pages/iou/request/step/IOURequestStepDistance.tsx @@ -341,29 +341,6 @@ function IOURequestStepDistance({ } playSound(SOUNDS.DONE); - // 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, - // iouType, - // transaction, - // ); IOU.createDistanceRequest({ report, participants,