diff --git a/src/libs/API/parameters/EditMoneyRequestParams.ts b/src/libs/API/parameters/EditMoneyRequestParams.ts deleted file mode 100644 index 6d320510e267..000000000000 --- a/src/libs/API/parameters/EditMoneyRequestParams.ts +++ /dev/null @@ -1,14 +0,0 @@ -type EditMoneyRequestParams = { - transactionID: string; - reportActionID: string; - created?: string; - amount?: number; - currency?: string; - comment?: string; - merchant?: string; - category?: string; - billable?: boolean; - tag?: string; -}; - -export default EditMoneyRequestParams; diff --git a/src/libs/API/parameters/index.ts b/src/libs/API/parameters/index.ts index fb5558fb0350..a6fca0ea19e8 100644 --- a/src/libs/API/parameters/index.ts +++ b/src/libs/API/parameters/index.ts @@ -157,7 +157,6 @@ export type {default as StartSplitBillParams} from './StartSplitBillParams'; export type {default as SendMoneyParams} from './SendMoneyParams'; export type {default as ApproveMoneyRequestParams} from './ApproveMoneyRequestParams'; export type {default as UnapproveExpenseReportParams} from './UnapproveExpenseReportParams'; -export type {default as EditMoneyRequestParams} from './EditMoneyRequestParams'; export type {default as ReplaceReceiptParams} from './ReplaceReceiptParams'; export type {default as SubmitReportParams} from './SubmitReportParams'; export type {default as DetachReceiptParams} from './DetachReceiptParams'; diff --git a/src/libs/API/types.ts b/src/libs/API/types.ts index b8b4bb749701..855e0c26950f 100644 --- a/src/libs/API/types.ts +++ b/src/libs/API/types.ts @@ -192,7 +192,6 @@ const WRITE_COMMANDS = { HOLD_MONEY_REQUEST: 'HoldRequest', UPDATE_BILLING_CARD_CURRENCY: 'UpdateBillingCardCurrency', UNHOLD_MONEY_REQUEST: 'UnHoldRequest', - UPDATE_DISTANCE_REQUEST: 'UpdateDistanceRequest', REQUEST_MONEY: 'RequestMoney', SPLIT_BILL: 'SplitBill', SPLIT_BILL_AND_OPEN_REPORT: 'SplitBillAndOpenReport', @@ -203,7 +202,6 @@ const WRITE_COMMANDS = { SEND_MONEY_WITH_WALLET: 'SendMoneyWithWallet', APPROVE_MONEY_REQUEST: 'ApproveMoneyRequest', UNAPPROVE_EXPENSE_REPORT: 'UnapproveExpenseReport', - EDIT_MONEY_REQUEST: 'EditMoneyRequest', REPLACE_RECEIPT: 'ReplaceReceipt', SUBMIT_REPORT: 'SubmitReport', DETACH_RECEIPT: 'DetachReceipt', @@ -616,7 +614,6 @@ type WriteCommandParameters = { [WRITE_COMMANDS.UPDATE_MONEY_REQUEST_DESCRIPTION]: Parameters.UpdateMoneyRequestParams; [WRITE_COMMANDS.HOLD_MONEY_REQUEST]: Parameters.HoldMoneyRequestParams; [WRITE_COMMANDS.UNHOLD_MONEY_REQUEST]: Parameters.UnHoldMoneyRequestParams; - [WRITE_COMMANDS.UPDATE_DISTANCE_REQUEST]: Parameters.UpdateMoneyRequestParams; [WRITE_COMMANDS.UPDATE_MONEY_REQUEST_AMOUNT_AND_CURRENCY]: Parameters.UpdateMoneyRequestParams; [WRITE_COMMANDS.REQUEST_MONEY]: Parameters.RequestMoneyParams; [WRITE_COMMANDS.SPLIT_BILL]: Parameters.SplitBillParams; @@ -628,7 +625,6 @@ type WriteCommandParameters = { [WRITE_COMMANDS.SEND_MONEY_WITH_WALLET]: Parameters.SendMoneyParams; [WRITE_COMMANDS.APPROVE_MONEY_REQUEST]: Parameters.ApproveMoneyRequestParams; [WRITE_COMMANDS.UNAPPROVE_EXPENSE_REPORT]: Parameters.UnapproveExpenseReportParams; - [WRITE_COMMANDS.EDIT_MONEY_REQUEST]: Parameters.EditMoneyRequestParams; [WRITE_COMMANDS.REPLACE_RECEIPT]: Parameters.ReplaceReceiptParams; [WRITE_COMMANDS.SUBMIT_REPORT]: Parameters.SubmitReportParams; [WRITE_COMMANDS.DETACH_RECEIPT]: Parameters.DetachReceiptParams; diff --git a/src/libs/TransactionUtils/index.ts b/src/libs/TransactionUtils/index.ts index 99a97ea08672..802a998fed37 100644 --- a/src/libs/TransactionUtils/index.ts +++ b/src/libs/TransactionUtils/index.ts @@ -248,7 +248,19 @@ function areRequiredFieldsEmpty(transaction: OnyxEntry): boolean { /** * Given the edit made to the expense, return an updated transaction object. */ -function getUpdatedTransaction(transaction: Transaction, transactionChanges: TransactionChanges, isFromExpenseReport: boolean, shouldUpdateReceiptState = true): Transaction { +function getUpdatedTransaction({ + transaction, + transactionChanges, + isFromExpenseReport, + shouldUpdateReceiptState = true, + policy = undefined, +}: { + transaction: Transaction; + transactionChanges: TransactionChanges; + isFromExpenseReport: boolean; + shouldUpdateReceiptState?: boolean; + policy?: OnyxEntry; +}): Transaction { // Only changing the first level fields so no need for deep clone now const updatedTransaction = lodashDeepClone(transaction); let shouldStopSmartscan = false; @@ -289,10 +301,6 @@ function getUpdatedTransaction(transaction: Transaction, transactionChanges: Tra shouldStopSmartscan = true; const existingDistanceUnit = transaction?.comment?.customUnit?.distanceUnit; - const allReports = ReportConnection.getAllReports(); - const report = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${transaction.reportID}`] ?? null; - const policyID = report?.policyID ?? ''; - const policy = PolicyUtils.getPolicy(policyID); // Get the new distance unit from the rate's unit const newDistanceUnit = DistanceRequestUtils.getUpdatedDistanceUnit({transaction: updatedTransaction, policy}); diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 3d0324d81549..feb3ed784e83 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -13,7 +13,6 @@ import type { CreateWorkspaceParams, DeleteMoneyRequestParams, DetachReceiptParams, - EditMoneyRequestParams, PayInvoiceParams, PayMoneyRequestParams, ReplaceReceiptParams, @@ -2486,10 +2485,6 @@ function calculateDiffAmount( * @param policy May be undefined, an empty object, or an object matching the Policy type (src/types/onyx/Policy.ts) * @param policyTagList * @param policyCategories - * @param onlyIncludeChangedFields - * When 'true', then the returned params will only include the transaction details for the fields that were changed. - * When `false`, then the returned params will include all the transaction details, regardless of which fields were changed. - * This setting is necessary while the UpdateDistanceRequest API is refactored to be fully 1:1:1 in https://github.com/Expensify/App/issues/28358 */ function getUpdateMoneyRequestParams( transactionID: string, @@ -2498,7 +2493,6 @@ function getUpdateMoneyRequestParams( policy: OnyxEntry, policyTagList: OnyxTypes.OnyxInputOrEntry, policyCategories: OnyxTypes.OnyxInputOrEntry, - onlyIncludeChangedFields: boolean, violations?: OnyxEntry, ): UpdateMoneyRequestData { const optimisticData: OnyxUpdate[] = []; @@ -2516,7 +2510,14 @@ function getUpdateMoneyRequestParams( const iouReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${transactionThread?.parentReportID}`] ?? null; const isFromExpenseReport = ReportUtils.isExpenseReport(iouReport); const isScanning = TransactionUtils.hasReceipt(transaction) && TransactionUtils.isReceiptBeingScanned(transaction); - let updatedTransaction: OnyxEntry = transaction ? TransactionUtils.getUpdatedTransaction(transaction, transactionChanges, isFromExpenseReport) : undefined; + let updatedTransaction: OnyxEntry = transaction + ? TransactionUtils.getUpdatedTransaction({ + transaction, + transactionChanges, + isFromExpenseReport, + policy, + }) + : undefined; const transactionDetails = ReportUtils.getTransactionDetails(updatedTransaction); if (updatedTransaction?.pendingFields) { @@ -2531,9 +2532,7 @@ function getUpdateMoneyRequestParams( transactionDetails.waypoints = JSON.stringify(transactionDetails.waypoints); } - const dataToIncludeInParams: Partial | undefined = onlyIncludeChangedFields - ? Object.fromEntries(Object.entries(transactionDetails ?? {}).filter(([key]) => Object.keys(transactionChanges).includes(key))) - : transactionDetails; + const dataToIncludeInParams: Partial = Object.fromEntries(Object.entries(transactionDetails ?? {}).filter(([key]) => Object.keys(transactionChanges).includes(key))); const params: UpdateMoneyRequestParams = { ...dataToIncludeInParams, @@ -2834,18 +2833,13 @@ function getUpdateMoneyRequestParams( * @param transactionThreadReportID * @param transactionChanges * @param [transactionChanges.created] Present when updated the date field - * @param onlyIncludeChangedFields - * When 'true', then the returned params will only include the transaction details for the fields that were changed. - * When `false`, then the returned params will include all the transaction details, regardless of which fields were changed. - * This setting is necessary while the UpdateDistanceRequest API is refactored to be fully 1:1:1 in https://github.com/Expensify/App/issues/28358 * @param policy May be undefined, an empty object, or an object matching the Policy type (src/types/onyx/Policy.ts) */ function getUpdateTrackExpenseParams( transactionID: string, transactionThreadReportID: string, transactionChanges: TransactionChanges, - onlyIncludeChangedFields: boolean, - policy: OnyxTypes.OnyxInputOrEntry, + policy: OnyxEntry, ): UpdateMoneyRequestData { const optimisticData: OnyxUpdate[] = []; const successData: OnyxUpdate[] = []; @@ -2861,7 +2855,14 @@ function getUpdateTrackExpenseParams( const transaction = allTransactions?.[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`]; const chatReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${transactionThread?.parentReportID}`] ?? null; const isScanning = TransactionUtils.hasReceipt(transaction) && TransactionUtils.isReceiptBeingScanned(transaction); - let updatedTransaction = transaction ? TransactionUtils.getUpdatedTransaction(transaction, transactionChanges, false) : null; + let updatedTransaction = transaction + ? TransactionUtils.getUpdatedTransaction({ + transaction, + transactionChanges, + isFromExpenseReport: false, + policy, + }) + : null; const transactionDetails = ReportUtils.getTransactionDetails(updatedTransaction); if (transactionDetails?.waypoints) { @@ -2869,9 +2870,7 @@ function getUpdateTrackExpenseParams( transactionDetails.waypoints = JSON.stringify(transactionDetails.waypoints); } - const dataToIncludeInParams: Partial | undefined = onlyIncludeChangedFields - ? Object.fromEntries(Object.entries(transactionDetails ?? {}).filter(([key]) => Object.keys(transactionChanges).includes(key))) - : transactionDetails; + const dataToIncludeInParams: Partial = Object.fromEntries(Object.entries(transactionDetails ?? {}).filter(([key]) => Object.keys(transactionChanges).includes(key))); const params: UpdateMoneyRequestParams = { ...dataToIncludeInParams, @@ -2913,7 +2912,7 @@ function getUpdateTrackExpenseParams( // - we're updating the distance rate while the waypoints are still pending // In these cases, there isn't a valid optimistic mileage data we can use, // and the report action is created on the server with the distance-related response from the MapBox API - const updatedReportAction = ReportUtils.buildOptimisticModifiedExpenseReportAction(transactionThread, transaction, transactionChanges, false, policy); + const updatedReportAction = ReportUtils.buildOptimisticModifiedExpenseReportAction(transactionThread, transaction, transactionChanges, false, policy, updatedTransaction); if (!hasPendingWaypoints && !(hasModifiedDistanceRate && TransactionUtils.isFetchingWaypointsFromServer(transaction))) { params.reportActionID = updatedReportAction.reportActionID; @@ -3033,9 +3032,9 @@ function updateMoneyRequestDate( const parentReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${transactionThreadReport?.parentReportID}`] ?? null; let data: UpdateMoneyRequestData; if (ReportUtils.isTrackExpenseReport(transactionThreadReport) && ReportUtils.isSelfDM(parentReport)) { - data = getUpdateTrackExpenseParams(transactionID, transactionThreadReportID, transactionChanges, true, policy); + data = getUpdateTrackExpenseParams(transactionID, transactionThreadReportID, transactionChanges, policy); } else { - data = getUpdateMoneyRequestParams(transactionID, transactionThreadReportID, transactionChanges, policy, policyTags, policyCategories, true); + data = getUpdateMoneyRequestParams(transactionID, transactionThreadReportID, transactionChanges, policy, policyTags, policyCategories); } const {params, onyxData} = data; API.write(WRITE_COMMANDS.UPDATE_MONEY_REQUEST_DATE, params, onyxData); @@ -3053,7 +3052,7 @@ function updateMoneyRequestBillable( const transactionChanges: TransactionChanges = { billable: value, }; - const {params, onyxData} = getUpdateMoneyRequestParams(transactionID, transactionThreadReportID, transactionChanges, policy, policyTagList, policyCategories, true); + const {params, onyxData} = getUpdateMoneyRequestParams(transactionID, transactionThreadReportID, transactionChanges, policy, policyTagList, policyCategories); API.write(WRITE_COMMANDS.UPDATE_MONEY_REQUEST_BILLABLE, params, onyxData); } @@ -3074,9 +3073,9 @@ function updateMoneyRequestMerchant( const parentReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${transactionThreadReport?.parentReportID}`] ?? null; let data: UpdateMoneyRequestData; if (ReportUtils.isTrackExpenseReport(transactionThreadReport) && ReportUtils.isSelfDM(parentReport)) { - data = getUpdateTrackExpenseParams(transactionID, transactionThreadReportID, transactionChanges, true, policy); + data = getUpdateTrackExpenseParams(transactionID, transactionThreadReportID, transactionChanges, policy); } else { - data = getUpdateMoneyRequestParams(transactionID, transactionThreadReportID, transactionChanges, policy, policyTagList, policyCategories, true); + data = getUpdateMoneyRequestParams(transactionID, transactionThreadReportID, transactionChanges, policy, policyTagList, policyCategories); } const {params, onyxData} = data; API.write(WRITE_COMMANDS.UPDATE_MONEY_REQUEST_MERCHANT, params, onyxData); @@ -3095,7 +3094,7 @@ function updateMoneyRequestAttendees( const transactionChanges: TransactionChanges = { attendees, }; - const data = getUpdateMoneyRequestParams(transactionID, transactionThreadReportID, transactionChanges, policy, policyTagList, policyCategories, true, violations); + const data = getUpdateMoneyRequestParams(transactionID, transactionThreadReportID, transactionChanges, policy, policyTagList, policyCategories, violations); const {params, onyxData} = data; API.write(WRITE_COMMANDS.UPDATE_MONEY_REQUEST_ATTENDEES, params, onyxData); } @@ -3112,7 +3111,7 @@ function updateMoneyRequestTag( const transactionChanges: TransactionChanges = { tag, }; - const {params, onyxData} = getUpdateMoneyRequestParams(transactionID, transactionThreadReportID, transactionChanges, policy, policyTagList, policyCategories, true); + const {params, onyxData} = getUpdateMoneyRequestParams(transactionID, transactionThreadReportID, transactionChanges, policy, policyTagList, policyCategories); API.write(WRITE_COMMANDS.UPDATE_MONEY_REQUEST_TAG, params, onyxData); } @@ -3128,7 +3127,7 @@ function updateMoneyRequestTaxAmount( const transactionChanges = { taxAmount, }; - const {params, onyxData} = getUpdateMoneyRequestParams(transactionID, optimisticReportActionID, transactionChanges, policy, policyTagList, policyCategories, true); + const {params, onyxData} = getUpdateMoneyRequestParams(transactionID, optimisticReportActionID, transactionChanges, policy, policyTagList, policyCategories); API.write('UpdateMoneyRequestTaxAmount', params, onyxData); } @@ -3148,7 +3147,7 @@ function updateMoneyRequestTaxRate({transactionID, optimisticReportActionID, tax taxCode, taxAmount, }; - const {params, onyxData} = getUpdateMoneyRequestParams(transactionID, optimisticReportActionID, transactionChanges, policy, policyTagList, policyCategories, true); + const {params, onyxData} = getUpdateMoneyRequestParams(transactionID, optimisticReportActionID, transactionChanges, policy, policyTagList, policyCategories); API.write('UpdateMoneyRequestTaxRate', params, onyxData); } @@ -3181,9 +3180,9 @@ function updateMoneyRequestDistance({ const parentReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${transactionThreadReport?.parentReportID}`] ?? null; let data: UpdateMoneyRequestData; if (ReportUtils.isTrackExpenseReport(transactionThreadReport) && ReportUtils.isSelfDM(parentReport)) { - data = getUpdateTrackExpenseParams(transactionID, transactionThreadReportID, transactionChanges, true, policy); + data = getUpdateTrackExpenseParams(transactionID, transactionThreadReportID, transactionChanges, policy); } else { - data = getUpdateMoneyRequestParams(transactionID, transactionThreadReportID, transactionChanges, policy, policyTagList, policyCategories, true); + data = getUpdateMoneyRequestParams(transactionID, transactionThreadReportID, transactionChanges, policy, policyTagList, policyCategories); } const {params, onyxData} = data; @@ -3209,7 +3208,7 @@ function updateMoneyRequestCategory( const transactionChanges: TransactionChanges = { category, }; - const {params, onyxData} = getUpdateMoneyRequestParams(transactionID, transactionThreadReportID, transactionChanges, policy, policyTagList, policyCategories, true); + const {params, onyxData} = getUpdateMoneyRequestParams(transactionID, transactionThreadReportID, transactionChanges, policy, policyTagList, policyCategories); API.write(WRITE_COMMANDS.UPDATE_MONEY_REQUEST_CATEGORY, params, onyxData); } @@ -3230,9 +3229,9 @@ function updateMoneyRequestDescription( const parentReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${transactionThreadReport?.parentReportID}`] ?? null; let data: UpdateMoneyRequestData; if (ReportUtils.isTrackExpenseReport(transactionThreadReport) && ReportUtils.isSelfDM(parentReport)) { - data = getUpdateTrackExpenseParams(transactionID, transactionThreadReportID, transactionChanges, true, policy); + data = getUpdateTrackExpenseParams(transactionID, transactionThreadReportID, transactionChanges, policy); } else { - data = getUpdateMoneyRequestParams(transactionID, transactionThreadReportID, transactionChanges, policy, policyTagList, policyCategories, true); + data = getUpdateMoneyRequestParams(transactionID, transactionThreadReportID, transactionChanges, policy, policyTagList, policyCategories); } const {params, onyxData} = data; API.write(WRITE_COMMANDS.UPDATE_MONEY_REQUEST_DESCRIPTION, params, onyxData); @@ -3259,9 +3258,9 @@ function updateMoneyRequestDistanceRate( const parentReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${transactionThreadReport?.parentReportID}`] ?? null; let data: UpdateMoneyRequestData; if (ReportUtils.isTrackExpenseReport(transactionThreadReport) && ReportUtils.isSelfDM(parentReport)) { - data = getUpdateTrackExpenseParams(transactionID, transactionThreadReportID, transactionChanges, true, policy); + data = getUpdateTrackExpenseParams(transactionID, transactionThreadReportID, transactionChanges, policy); } else { - data = getUpdateMoneyRequestParams(transactionID, transactionThreadReportID, transactionChanges, policy, policyTagList, policyCategories, true); + data = getUpdateMoneyRequestParams(transactionID, transactionThreadReportID, transactionChanges, policy, policyTagList, policyCategories); } const {params, onyxData} = data; // `taxAmount` & `taxCode` only needs to be updated in the optimistic data, so we need to remove them from the params @@ -3269,19 +3268,6 @@ function updateMoneyRequestDistanceRate( API.write(WRITE_COMMANDS.UPDATE_MONEY_REQUEST_DISTANCE_RATE, paramsWithoutTaxUpdated, onyxData); } -/** Edits an existing distance expense */ -function updateDistanceRequest( - transactionID: string, - transactionThreadReportID: string, - transactionChanges: TransactionChanges, - policy: OnyxTypes.Policy, - policyTagList: OnyxTypes.PolicyTagLists, - policyCategories: OnyxTypes.PolicyCategories, -) { - const {params, onyxData} = getUpdateMoneyRequestParams(transactionID, transactionThreadReportID, transactionChanges, policy, policyTagList, policyCategories, false); - API.write(WRITE_COMMANDS.UPDATE_DISTANCE_REQUEST, params, onyxData); -} - const getConvertTrackedExpenseInformation = ( transactionID: string, actionableWhisperReportActionID: string, @@ -5165,7 +5151,14 @@ function setDraftSplitTransaction(transactionID: string, transactionChanges: Tra draftSplitTransaction = allTransactions[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`]; } - const updatedTransaction = draftSplitTransaction ? TransactionUtils.getUpdatedTransaction(draftSplitTransaction, transactionChanges, false, false) : null; + const updatedTransaction = draftSplitTransaction + ? TransactionUtils.getUpdatedTransaction({ + transaction: draftSplitTransaction, + transactionChanges, + isFromExpenseReport: false, + shouldUpdateReceiptState: false, + }) + : null; Onyx.merge(`${ONYXKEYS.COLLECTION.SPLIT_TRANSACTION_DRAFT}${transactionID}`, updatedTransaction); } @@ -5329,296 +5322,6 @@ function createDistanceRequest( Report.notifyNewAction(activeReportID, userAccountID); } -function editRegularMoneyRequest( - transactionID: string, - transactionThreadReportID: string, - transactionChanges: TransactionChanges, - policy: OnyxTypes.Policy, - policyTags: OnyxTypes.PolicyTagLists, - policyCategories: OnyxTypes.PolicyCategories, -) { - const allReports = ReportConnection.getAllReports(); - // STEP 1: Get all collections we're updating - const transactionThread = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${transactionThreadReportID}`] ?? null; - const transaction = allTransactions[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`]; - const iouReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${transactionThread?.parentReportID}`] ?? null; - const chatReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${iouReport?.chatReportID}`] ?? null; - const isFromExpenseReport = ReportUtils.isExpenseReport(iouReport); - - // STEP 2: Build new modified expense report action. - const updatedReportAction = ReportUtils.buildOptimisticModifiedExpenseReportAction(transactionThread, transaction, transactionChanges, isFromExpenseReport, policy); - const updatedTransaction = transaction ? TransactionUtils.getUpdatedTransaction(transaction, transactionChanges, isFromExpenseReport) : null; - - // STEP 3: Compute the IOU total and update the report preview message so LHN amount owed is correct - // Should only update if the transaction matches the currency of the report, else we wait for the update - // from the server with the currency conversion - let updatedMoneyRequestReport = {...iouReport}; - const updatedChatReport = {...chatReport}; - const diff = TransactionUtils.getAmount(transaction, true) - TransactionUtils.getAmount(updatedTransaction, true); - if (updatedTransaction?.currency === iouReport?.currency && updatedTransaction?.modifiedAmount && diff !== 0) { - if (ReportUtils.isExpenseReport(iouReport) && typeof updatedMoneyRequestReport.total === 'number') { - updatedMoneyRequestReport.total += diff; - } else { - updatedMoneyRequestReport = iouReport - ? IOUUtils.updateIOUOwnerAndTotal(iouReport, updatedReportAction.actorAccountID ?? -1, diff, TransactionUtils.getCurrency(transaction), false) - : {}; - } - - updatedMoneyRequestReport.cachedTotal = CurrencyUtils.convertToDisplayString(updatedMoneyRequestReport.total, updatedTransaction.currency); - - // Update the last message of the IOU report - const lastMessage = ReportUtils.getIOUReportActionMessage( - iouReport?.reportID ?? '-1', - CONST.IOU.REPORT_ACTION_TYPE.CREATE, - updatedMoneyRequestReport.total ?? 0, - '', - updatedTransaction.currency, - '', - false, - ); - updatedMoneyRequestReport.lastMessageText = ReportActionsUtils.getTextFromHtml(lastMessage.at(0)?.html); - updatedMoneyRequestReport.lastMessageHtml = lastMessage.at(0)?.html; - - // Update the last message of the chat report - const hasNonReimbursableTransactions = ReportUtils.hasNonReimbursableTransactions(iouReport?.reportID); - const messageText = Localize.translateLocal(hasNonReimbursableTransactions ? 'iou.payerSpentAmount' : 'iou.payerOwesAmount', { - payer: ReportUtils.getPersonalDetailsForAccountID(updatedMoneyRequestReport.managerID ?? -1).login ?? '', - amount: CurrencyUtils.convertToDisplayString(updatedMoneyRequestReport.total, updatedMoneyRequestReport.currency), - }); - updatedChatReport.lastMessageText = messageText; - updatedChatReport.lastMessageHtml = messageText; - } - - const isScanning = TransactionUtils.hasReceipt(updatedTransaction) && TransactionUtils.isReceiptBeingScanned(updatedTransaction); - - // STEP 4: Compose the optimistic data - const currentTime = DateUtils.getDBTime(); - const optimisticData: OnyxUpdate[] = [ - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${transactionThread?.reportID}`, - value: { - [updatedReportAction.reportActionID]: updatedReportAction as OnyxTypes.ReportAction, - }, - }, - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`, - value: updatedTransaction, - }, - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport?.reportID}`, - value: updatedMoneyRequestReport, - }, - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport?.chatReportID}`, - value: updatedChatReport, - }, - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${transactionThreadReportID}`, - value: { - lastReadTime: currentTime, - lastVisibleActionCreated: currentTime, - }, - }, - ]; - - if (!isScanning) { - optimisticData.push( - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport?.reportID}`, - value: { - [transactionThread?.parentReportActionID ?? '-1']: { - originalMessage: { - whisperedTo: [], - }, - }, - }, - }, - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport?.parentReportID}`, - value: { - [iouReport?.parentReportActionID ?? '-1']: { - originalMessage: { - whisperedTo: [], - }, - }, - }, - }, - ); - } - - // Update recently used categories if the category is changed - if ('category' in transactionChanges) { - const optimisticPolicyRecentlyUsedCategories = Category.buildOptimisticPolicyRecentlyUsedCategories(iouReport?.policyID, transactionChanges.category); - if (optimisticPolicyRecentlyUsedCategories.length) { - optimisticData.push({ - onyxMethod: Onyx.METHOD.SET, - key: `${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CATEGORIES}${iouReport?.policyID}`, - value: optimisticPolicyRecentlyUsedCategories, - }); - } - } - - // Update recently used currencies if the currency is changed - if ('currency' in transactionChanges) { - const optimisticRecentlyUsedCurrencies = Policy.buildOptimisticRecentlyUsedCurrencies(transactionChanges.currency); - if (optimisticRecentlyUsedCurrencies.length) { - optimisticData.push({ - onyxMethod: Onyx.METHOD.SET, - key: ONYXKEYS.RECENTLY_USED_CURRENCIES, - value: optimisticRecentlyUsedCurrencies, - }); - } - } - - // Update recently used categories if the tag is changed - if ('tag' in transactionChanges) { - const optimisticPolicyRecentlyUsedTags = Tag.buildOptimisticPolicyRecentlyUsedTags(iouReport?.policyID, transactionChanges.tag); - if (!isEmptyObject(optimisticPolicyRecentlyUsedTags)) { - optimisticData.push({ - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_TAGS}${iouReport?.policyID}`, - value: optimisticPolicyRecentlyUsedTags, - }); - } - } - - const successData: OnyxUpdate[] = [ - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${transactionThread?.reportID}`, - value: { - [updatedReportAction.reportActionID]: {pendingAction: null}, - }, - }, - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`, - value: { - pendingFields: { - comment: null, - amount: null, - created: null, - currency: null, - merchant: null, - billable: null, - category: null, - tag: null, - }, - }, - }, - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport?.reportID}`, - value: {pendingAction: null}, - }, - ]; - - const failureData: OnyxUpdate[] = [ - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${transactionThread?.reportID}`, - value: { - [updatedReportAction.reportActionID]: { - errors: ErrorUtils.getMicroSecondOnyxErrorWithTranslationKey('iou.error.genericEditFailureMessage'), - }, - }, - }, - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`, - value: { - ...transaction, - modifiedCreated: transaction?.modifiedCreated ? transaction.modifiedCreated : null, - modifiedAmount: transaction?.modifiedAmount ? transaction.modifiedAmount : null, - modifiedCurrency: transaction?.modifiedCurrency ? transaction.modifiedCurrency : null, - modifiedMerchant: transaction?.modifiedMerchant ? transaction.modifiedMerchant : null, - modifiedWaypoints: transaction?.modifiedWaypoints ? transaction.modifiedWaypoints : null, - pendingFields: null, - }, - }, - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport?.reportID}`, - value: { - ...iouReport, - cachedTotal: iouReport?.cachedTotal ? iouReport?.cachedTotal : null, - }, - }, - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport?.chatReportID}`, - value: chatReport, - }, - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${transactionThreadReportID}`, - value: { - lastReadTime: transactionThread?.lastReadTime, - lastVisibleActionCreated: transactionThread?.lastVisibleActionCreated, - }, - }, - ]; - - // Add transaction violations if we have a paid policy and an updated transaction - if (policy && PolicyUtils.isPaidGroupPolicy(policy) && updatedTransaction) { - const currentTransactionViolations = allTransactionViolations[`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transactionID}`] ?? []; - const updatedViolationsOnyxData = ViolationsUtils.getViolationsOnyxData( - updatedTransaction, - currentTransactionViolations, - policy, - policyTags, - policyCategories, - PolicyUtils.hasDependentTags(policy, policyTags), - ); - optimisticData.push(updatedViolationsOnyxData); - failureData.push({ - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transactionID}`, - value: currentTransactionViolations, - }); - } - - // STEP 6: Call the API endpoint - const {created, amount, currency, comment, merchant, category, billable, tag} = ReportUtils.getTransactionDetails(updatedTransaction) ?? {}; - - const parameters: EditMoneyRequestParams = { - transactionID, - reportActionID: updatedReportAction.reportActionID, - created, - amount, - currency, - comment, - merchant, - category, - billable, - tag, - }; - - API.write(WRITE_COMMANDS.EDIT_MONEY_REQUEST, parameters, {optimisticData, successData, failureData}); -} - -function editMoneyRequest( - transaction: OnyxTypes.Transaction, - transactionThreadReportID: string, - transactionChanges: TransactionChanges, - policy: OnyxTypes.Policy, - policyTags: OnyxTypes.PolicyTagLists, - policyCategories: OnyxTypes.PolicyCategories, -) { - if (TransactionUtils.isDistanceRequest(transaction)) { - updateDistanceRequest(transaction.transactionID, transactionThreadReportID, transactionChanges, policy, policyTags, policyCategories); - } else { - editRegularMoneyRequest(transaction.transactionID, transactionThreadReportID, transactionChanges, policy, policyTags, policyCategories); - } -} - type UpdateMoneyRequestAmountAndCurrencyParams = { transactionID: string; transactionThreadReportID: string; @@ -5654,9 +5357,9 @@ function updateMoneyRequestAmountAndCurrency({ const parentReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${transactionThreadReport?.parentReportID}`] ?? null; let data: UpdateMoneyRequestData; if (ReportUtils.isTrackExpenseReport(transactionThreadReport) && ReportUtils.isSelfDM(parentReport)) { - data = getUpdateTrackExpenseParams(transactionID, transactionThreadReportID, transactionChanges, true, policy ?? null); + data = getUpdateTrackExpenseParams(transactionID, transactionThreadReportID, transactionChanges, policy); } else { - data = getUpdateMoneyRequestParams(transactionID, transactionThreadReportID, transactionChanges, policy, policyTagList ?? null, policyCategories ?? null, true); + data = getUpdateMoneyRequestParams(transactionID, transactionThreadReportID, transactionChanges, policy, policyTagList ?? null, policyCategories ?? null); } const {params, onyxData} = data; API.write(WRITE_COMMANDS.UPDATE_MONEY_REQUEST_AMOUNT_AND_CURRENCY, params, onyxData); @@ -8585,7 +8288,6 @@ export { deleteTrackExpense, detachReceipt, dismissHoldUseExplanation, - editMoneyRequest, getIOURequestPolicyID, initMoneyRequest, navigateToStartStepIfScanFileCannotBeRead, diff --git a/tests/actions/IOUTest.ts b/tests/actions/IOUTest.ts index c2005d221273..991441b66b9c 100644 --- a/tests/actions/IOUTest.ts +++ b/tests/actions/IOUTest.ts @@ -1613,6 +1613,8 @@ describe('actions/IOU', () => { const comment = '💸💸💸💸'; const merchant = 'NASDAQ'; + const modifiedComment = 'Modified the comment!'; + afterEach(() => { mockFetch?.resume?.(); }); @@ -1684,10 +1686,10 @@ describe('actions/IOU', () => { }) .then(() => { if (transaction) { - IOU.editMoneyRequest( - transaction, + IOU.updateMoneyRequestDescription( + transaction.transactionID, thread.reportID, - {amount: 20000, comment: 'Double the amount!'}, + modifiedComment, { id: '123', role: 'user', @@ -1713,8 +1715,7 @@ describe('actions/IOU', () => { Onyx.disconnect(connection); const updatedTransaction = Object.values(allTransactions ?? {}).find((t) => !isEmptyObject(t)); - expect(updatedTransaction?.modifiedAmount).toBe(20000); - expect(updatedTransaction?.comment).toMatchObject({comment: 'Double the amount!'}); + expect(updatedTransaction?.comment).toMatchObject({comment: modifiedComment}); resolve(); }, }); @@ -1731,7 +1732,7 @@ describe('actions/IOU', () => { const updatedAction = Object.values(allActions ?? {}).find((reportAction) => !isEmptyObject(reportAction)); expect(updatedAction?.actionName).toEqual('MODIFIEDEXPENSE'); expect(updatedAction && ReportActionsUtils.getOriginalMessage(updatedAction)).toEqual( - expect.objectContaining({amount: 20000, newComment: 'Double the amount!', oldAmount: amount, oldComment: comment}), + expect.objectContaining({newComment: modifiedComment, oldComment: comment}), ); resolve(); }, @@ -1839,10 +1840,10 @@ describe('actions/IOU', () => { mockFetch?.fail?.(); if (transaction) { - IOU.editMoneyRequest( - transaction, + IOU.updateMoneyRequestDescription( + transaction.transactionID, thread.reportID, - {amount: 20000, comment: 'Double the amount!'}, + modifiedComment, { id: '123', role: 'user', @@ -2596,10 +2597,10 @@ describe('actions/IOU', () => { jest.advanceTimersByTime(10); if (transaction && createIOUAction) { - IOU.editMoneyRequest( - transaction, + IOU.updateMoneyRequestDescription( + transaction.transactionID, thread.reportID, - {amount: 20000, comment: 'Double the amount!'}, + 'Modified the comment!', { id: '123', role: 'user',