From d4b8cfdc5e70641f2a6f928829eacf23e7331930 Mon Sep 17 00:00:00 2001 From: war-in Date: Fri, 17 May 2024 12:52:15 +0200 Subject: [PATCH 01/23] wip --- src/libs/actions/IOU.ts | 49 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 2464dbff7dbd..bd9e02879539 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -40,7 +40,7 @@ import Permissions from '@libs/Permissions'; import * as PhoneNumber from '@libs/PhoneNumber'; import * as PolicyUtils from '@libs/PolicyUtils'; import * as ReportActionsUtils from '@libs/ReportActionsUtils'; -import type {OptimisticChatReport, OptimisticCreatedReportAction, OptimisticIOUReportAction, TransactionDetails} from '@libs/ReportUtils'; +import {generateReportID, OptimisticChatReport, OptimisticCreatedReportAction, OptimisticIOUReportAction, TransactionDetails} from '@libs/ReportUtils'; import * as ReportUtils from '@libs/ReportUtils'; import * as TransactionUtils from '@libs/TransactionUtils'; import * as UserUtils from '@libs/UserUtils'; @@ -50,6 +50,7 @@ import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import ROUTES from '@src/ROUTES'; import type * as OnyxTypes from '@src/types/onyx'; +import {ReportActions} from '@src/types/onyx'; import type {Participant, Split} from '@src/types/onyx/IOU'; import type {ErrorFields, Errors} from '@src/types/onyx/OnyxCommon'; import type {IOUMessage, PaymentMethodType} from '@src/types/onyx/OriginalMessage'; @@ -5810,6 +5811,36 @@ function getPayMoneyRequestParams( const currentNextStep = allNextSteps[`${ONYXKEYS.COLLECTION.NEXT_STEP}${iouReport.reportID}`] ?? null; const optimisticNextStep = NextStepUtils.buildNextStep(iouReport, CONST.REPORT.STATUS_NUM.REIMBURSED, {isPaidWithExpensify: paymentMethodType === CONST.IOU.PAYMENT_TYPE.VBBA}); + let holdTransactions = Object.values(allTransactions ?? {}).filter((transaction) => { + if (`${transaction?.reportID}` === `${iouReport.reportID}` && transaction?.comment?.hold) return transaction; + }); + console.log(holdTransactions); + + let reportActionsToBeMoved = Object.values(allReportActions?.[`reportActions_${iouReport.reportID}`] ?? {}).filter((reportAction) => { + let hasHoldTransaction = holdTransactions.find((transaction) => transaction?.transactionID === reportAction.originalMessage?.IOUTransactionID); + + if (hasHoldTransaction) { + return reportAction; + } + }); + console.log(reportActionsToBeMoved); + + let removeReportActions: Record = {}; + reportActionsToBeMoved.forEach((reportAction) => { + removeReportActions[reportAction.reportActionID] = null; + }); + console.log(removeReportActions); + + let setReportActions: ReportActions = {}; + reportActionsToBeMoved.forEach((reportAction) => { + setReportActions[reportAction.reportActionID] = reportAction; + }); + console.log(setReportActions); + + let newIOUReportID = generateReportID(); + let newIOUReport = iouReport; + newIOUReport.reportID = newIOUReportID; + const optimisticData: OnyxUpdate[] = [ { onyxMethod: Onyx.METHOD.MERGE, @@ -5819,11 +5850,12 @@ function getPayMoneyRequestParams( lastReadTime: DateUtils.getDBTime(), lastVisibleActionCreated: optimisticIOUReportAction.created, hasOutstandingChildRequest: false, - iouReportID: null, + iouReportID: newIOUReportID, lastMessageText: optimisticIOUReportAction.message?.[0]?.text, lastMessageHtml: optimisticIOUReportAction.message?.[0]?.html, }, }, + // removed hold report actions from old iou report { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport.reportID}`, @@ -5832,8 +5864,15 @@ function getPayMoneyRequestParams( ...(optimisticIOUReportAction as OnyxTypes.ReportAction), pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, }, + ...removeReportActions, }, }, + // added hold report actions to new iou report + { + onyxMethod: Onyx.METHOD.SET, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${newIOUReport.reportID}`, + value: setReportActions, + }, { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport.reportID}`, @@ -5850,6 +5889,12 @@ function getPayMoneyRequestParams( }, }, }, + // added new iou report + { + onyxMethod: Onyx.METHOD.SET, + key: `${ONYXKEYS.COLLECTION.REPORT}${newIOUReport.reportID}`, + value: newIOUReport, + }, { onyxMethod: Onyx.METHOD.MERGE, key: ONYXKEYS.NVP_LAST_PAYMENT_METHOD, From d2a24ba6c6360c6da18649aaeeb3cc0af8eed9b9 Mon Sep 17 00:00:00 2001 From: war-in Date: Fri, 17 May 2024 17:03:49 +0200 Subject: [PATCH 02/23] wip, but preview is showing and actions are moved --- src/libs/actions/IOU.ts | 74 ++++++++++++++++++++++++++++------------- 1 file changed, 50 insertions(+), 24 deletions(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index bd9e02879539..432e9fdb0e75 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -40,7 +40,7 @@ import Permissions from '@libs/Permissions'; import * as PhoneNumber from '@libs/PhoneNumber'; import * as PolicyUtils from '@libs/PolicyUtils'; import * as ReportActionsUtils from '@libs/ReportActionsUtils'; -import {generateReportID, OptimisticChatReport, OptimisticCreatedReportAction, OptimisticIOUReportAction, TransactionDetails} from '@libs/ReportUtils'; +import type {OptimisticChatReport, OptimisticCreatedReportAction, OptimisticIOUReportAction, TransactionDetails} from '@libs/ReportUtils'; import * as ReportUtils from '@libs/ReportUtils'; import * as TransactionUtils from '@libs/TransactionUtils'; import * as UserUtils from '@libs/UserUtils'; @@ -50,7 +50,6 @@ import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import ROUTES from '@src/ROUTES'; import type * as OnyxTypes from '@src/types/onyx'; -import {ReportActions} from '@src/types/onyx'; import type {Participant, Split} from '@src/types/onyx/IOU'; import type {ErrorFields, Errors} from '@src/types/onyx/OnyxCommon'; import type {IOUMessage, PaymentMethodType} from '@src/types/onyx/OriginalMessage'; @@ -5811,35 +5810,48 @@ function getPayMoneyRequestParams( const currentNextStep = allNextSteps[`${ONYXKEYS.COLLECTION.NEXT_STEP}${iouReport.reportID}`] ?? null; const optimisticNextStep = NextStepUtils.buildNextStep(iouReport, CONST.REPORT.STATUS_NUM.REIMBURSED, {isPaidWithExpensify: paymentMethodType === CONST.IOU.PAYMENT_TYPE.VBBA}); - let holdTransactions = Object.values(allTransactions ?? {}).filter((transaction) => { - if (`${transaction?.reportID}` === `${iouReport.reportID}` && transaction?.comment?.hold) return transaction; + const holdTransactions = Object.values(allTransactions ?? {}).filter((transaction) => { + if (`${transaction?.reportID}` === `${iouReport.reportID}` && transaction?.comment?.hold) { + return transaction; + } }); - console.log(holdTransactions); - let reportActionsToBeMoved = Object.values(allReportActions?.[`reportActions_${iouReport.reportID}`] ?? {}).filter((reportAction) => { + const reportActionsToBeMoved = Object.values(allReportActions?.[`reportActions_${iouReport.reportID}`] ?? {}).filter((reportAction) => { let hasHoldTransaction = holdTransactions.find((transaction) => transaction?.transactionID === reportAction.originalMessage?.IOUTransactionID); if (hasHoldTransaction) { return reportAction; } }); - console.log(reportActionsToBeMoved); - let removeReportActions: Record = {}; + const removeReportActions: Record = {}; reportActionsToBeMoved.forEach((reportAction) => { removeReportActions[reportAction.reportActionID] = null; }); - console.log(removeReportActions); - let setReportActions: ReportActions = {}; + const setReportActions: OnyxTypes.ReportActions = {}; reportActionsToBeMoved.forEach((reportAction) => { setReportActions[reportAction.reportActionID] = reportAction; }); - console.log(setReportActions); - let newIOUReportID = generateReportID(); - let newIOUReport = iouReport; - newIOUReport.reportID = newIOUReportID; + const optimisticExpenseReport = ReportUtils.buildOptimisticExpenseReport( + chatReport.reportID, + chatReport.policyID ?? iouReport.policyID ?? '', + recipient.accountID ?? 1, + holdTransactions[0]?.amount * -1 ?? 0, + iouReport.currency ?? 'PLN', + true, + ); + const optimisticExpenseReportPreview = ReportUtils.buildOptimisticReportPreview(chatReport, optimisticExpenseReport, '', holdTransactions[0]); + + const movedTransactions: Record = {}; + holdTransactions.forEach((transaction) => { + if (transaction !== null) { + transaction.reportID = optimisticExpenseReport.reportID; + + movedTransactions[`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`] = transaction; + } + }); const optimisticData: OnyxUpdate[] = [ { @@ -5850,12 +5862,32 @@ function getPayMoneyRequestParams( lastReadTime: DateUtils.getDBTime(), lastVisibleActionCreated: optimisticIOUReportAction.created, hasOutstandingChildRequest: false, - iouReportID: newIOUReportID, + iouReportID: optimisticExpenseReport.reportID, lastMessageText: optimisticIOUReportAction.message?.[0]?.text, lastMessageHtml: optimisticIOUReportAction.message?.[0]?.html, }, }, - // removed hold report actions from old iou report + // add new optimistic expense report + { + onyxMethod: Onyx.METHOD.SET, + key: `${ONYXKEYS.COLLECTION.REPORT}${optimisticExpenseReport.reportID}`, + value: optimisticExpenseReport, + }, + // add transaction to the new expense report + { + onyxMethod: Onyx.METHOD.MERGE_COLLECTION, + key: `${ONYXKEYS.COLLECTION.TRANSACTION}`, + value: movedTransactions, + }, + // add preview report action to main chat + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport.reportID}`, + value: { + [optimisticExpenseReportPreview.reportActionID]: optimisticExpenseReportPreview, + }, + }, + // remove hold report actions from old iou report { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport.reportID}`, @@ -5867,10 +5899,10 @@ function getPayMoneyRequestParams( ...removeReportActions, }, }, - // added hold report actions to new iou report + // add hold report actions to new iou report { onyxMethod: Onyx.METHOD.SET, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${newIOUReport.reportID}`, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${optimisticExpenseReport.reportID}`, value: setReportActions, }, { @@ -5889,12 +5921,6 @@ function getPayMoneyRequestParams( }, }, }, - // added new iou report - { - onyxMethod: Onyx.METHOD.SET, - key: `${ONYXKEYS.COLLECTION.REPORT}${newIOUReport.reportID}`, - value: newIOUReport, - }, { onyxMethod: Onyx.METHOD.MERGE, key: ONYXKEYS.NVP_LAST_PAYMENT_METHOD, From e48f86c2db7300e28ecb026d19650cb48a45cea1 Mon Sep 17 00:00:00 2001 From: war-in Date: Fri, 17 May 2024 17:38:33 +0200 Subject: [PATCH 03/23] fix linter --- src/libs/actions/IOU.ts | 26 ++++++++------------------ 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 432e9fdb0e75..0bc2631421ea 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -5810,19 +5810,13 @@ function getPayMoneyRequestParams( const currentNextStep = allNextSteps[`${ONYXKEYS.COLLECTION.NEXT_STEP}${iouReport.reportID}`] ?? null; const optimisticNextStep = NextStepUtils.buildNextStep(iouReport, CONST.REPORT.STATUS_NUM.REIMBURSED, {isPaidWithExpensify: paymentMethodType === CONST.IOU.PAYMENT_TYPE.VBBA}); - const holdTransactions = Object.values(allTransactions ?? {}).filter((transaction) => { - if (`${transaction?.reportID}` === `${iouReport.reportID}` && transaction?.comment?.hold) { - return transaction; - } - }); - - const reportActionsToBeMoved = Object.values(allReportActions?.[`reportActions_${iouReport.reportID}`] ?? {}).filter((reportAction) => { - let hasHoldTransaction = holdTransactions.find((transaction) => transaction?.transactionID === reportAction.originalMessage?.IOUTransactionID); + const holdTransactions: OnyxTypes.Transaction[] = Object.values(allTransactions).filter( + (transaction): transaction is OnyxTypes.Transaction => transaction !== null && transaction.reportID === iouReport.reportID && !!transaction.comment?.hold, + ); - if (hasHoldTransaction) { - return reportAction; - } - }); + const reportActionsToBeMoved = Object.values(allReportActions?.[`reportActions_${iouReport.reportID}`] ?? {}).filter((reportAction) => + holdTransactions.find((transaction) => transaction?.transactionID === (reportAction.originalMessage as IOUMessage)?.IOUTransactionID), + ); const removeReportActions: Record = {}; reportActionsToBeMoved.forEach((reportAction) => { @@ -5845,12 +5839,8 @@ function getPayMoneyRequestParams( const optimisticExpenseReportPreview = ReportUtils.buildOptimisticReportPreview(chatReport, optimisticExpenseReport, '', holdTransactions[0]); const movedTransactions: Record = {}; - holdTransactions.forEach((transaction) => { - if (transaction !== null) { - transaction.reportID = optimisticExpenseReport.reportID; - - movedTransactions[`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`] = transaction; - } + holdTransactions.forEach((transaction: OnyxTypes.Transaction) => { + movedTransactions[`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`] = {...transaction, reportID: optimisticExpenseReport.reportID}; }); const optimisticData: OnyxUpdate[] = [ From 5ae1de0c94e44ccc1b2b1258e2cd68b8b785df0e Mon Sep 17 00:00:00 2001 From: war-in Date: Mon, 20 May 2024 13:08:33 +0200 Subject: [PATCH 04/23] create optimistic data only when not `full` report --- src/libs/actions/IOU.ts | 166 +++++++++++++++++++++++----------------- 1 file changed, 94 insertions(+), 72 deletions(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 0bc2631421ea..f1290a5b433c 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -5810,39 +5810,6 @@ function getPayMoneyRequestParams( const currentNextStep = allNextSteps[`${ONYXKEYS.COLLECTION.NEXT_STEP}${iouReport.reportID}`] ?? null; const optimisticNextStep = NextStepUtils.buildNextStep(iouReport, CONST.REPORT.STATUS_NUM.REIMBURSED, {isPaidWithExpensify: paymentMethodType === CONST.IOU.PAYMENT_TYPE.VBBA}); - const holdTransactions: OnyxTypes.Transaction[] = Object.values(allTransactions).filter( - (transaction): transaction is OnyxTypes.Transaction => transaction !== null && transaction.reportID === iouReport.reportID && !!transaction.comment?.hold, - ); - - const reportActionsToBeMoved = Object.values(allReportActions?.[`reportActions_${iouReport.reportID}`] ?? {}).filter((reportAction) => - holdTransactions.find((transaction) => transaction?.transactionID === (reportAction.originalMessage as IOUMessage)?.IOUTransactionID), - ); - - const removeReportActions: Record = {}; - reportActionsToBeMoved.forEach((reportAction) => { - removeReportActions[reportAction.reportActionID] = null; - }); - - const setReportActions: OnyxTypes.ReportActions = {}; - reportActionsToBeMoved.forEach((reportAction) => { - setReportActions[reportAction.reportActionID] = reportAction; - }); - - const optimisticExpenseReport = ReportUtils.buildOptimisticExpenseReport( - chatReport.reportID, - chatReport.policyID ?? iouReport.policyID ?? '', - recipient.accountID ?? 1, - holdTransactions[0]?.amount * -1 ?? 0, - iouReport.currency ?? 'PLN', - true, - ); - const optimisticExpenseReportPreview = ReportUtils.buildOptimisticReportPreview(chatReport, optimisticExpenseReport, '', holdTransactions[0]); - - const movedTransactions: Record = {}; - holdTransactions.forEach((transaction: OnyxTypes.Transaction) => { - movedTransactions[`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`] = {...transaction, reportID: optimisticExpenseReport.reportID}; - }); - const optimisticData: OnyxUpdate[] = [ { onyxMethod: Onyx.METHOD.MERGE, @@ -5852,49 +5819,11 @@ function getPayMoneyRequestParams( lastReadTime: DateUtils.getDBTime(), lastVisibleActionCreated: optimisticIOUReportAction.created, hasOutstandingChildRequest: false, - iouReportID: optimisticExpenseReport.reportID, + iouReportID: null, lastMessageText: optimisticIOUReportAction.message?.[0]?.text, lastMessageHtml: optimisticIOUReportAction.message?.[0]?.html, }, }, - // add new optimistic expense report - { - onyxMethod: Onyx.METHOD.SET, - key: `${ONYXKEYS.COLLECTION.REPORT}${optimisticExpenseReport.reportID}`, - value: optimisticExpenseReport, - }, - // add transaction to the new expense report - { - onyxMethod: Onyx.METHOD.MERGE_COLLECTION, - key: `${ONYXKEYS.COLLECTION.TRANSACTION}`, - value: movedTransactions, - }, - // add preview report action to main chat - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport.reportID}`, - value: { - [optimisticExpenseReportPreview.reportActionID]: optimisticExpenseReportPreview, - }, - }, - // remove hold report actions from old iou report - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport.reportID}`, - value: { - [optimisticIOUReportAction.reportActionID]: { - ...(optimisticIOUReportAction as OnyxTypes.ReportAction), - pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, - }, - ...removeReportActions, - }, - }, - // add hold report actions to new iou report - { - onyxMethod: Onyx.METHOD.SET, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${optimisticExpenseReport.reportID}`, - value: setReportActions, - }, { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport.reportID}`, @@ -5923,6 +5852,99 @@ function getPayMoneyRequestParams( }, ]; + if (!full) { + const holdTransactions: OnyxTypes.Transaction[] = Object.values(allTransactions).filter( + (transaction): transaction is OnyxTypes.Transaction => transaction !== null && transaction.reportID === iouReport.reportID && !!transaction.comment?.hold, + ); + + const reportActionsToBeMoved = Object.values(allReportActions?.[`reportActions_${iouReport.reportID}`] ?? {}).filter((reportAction) => + holdTransactions.find((transaction) => transaction?.transactionID === (reportAction.originalMessage as IOUMessage)?.IOUTransactionID), + ); + + const removeReportActions: Record = {}; + reportActionsToBeMoved.forEach((reportAction) => { + removeReportActions[reportAction.reportActionID] = null; + }); + + const setReportActions: OnyxTypes.ReportActions = {}; + reportActionsToBeMoved.forEach((reportAction) => { + setReportActions[reportAction.reportActionID] = reportAction; + }); + + const optimisticExpenseReport = ReportUtils.buildOptimisticExpenseReport( + chatReport.reportID, + chatReport.policyID ?? iouReport.policyID ?? '', + recipient.accountID ?? 1, + holdTransactions[0]?.amount * -1 ?? 0, + iouReport.currency ?? 'PLN', + true, + ); + const optimisticExpenseReportPreview = ReportUtils.buildOptimisticReportPreview(chatReport, optimisticExpenseReport, '', holdTransactions[0]); + + const movedTransactions: Record = {}; + holdTransactions.forEach((transaction: OnyxTypes.Transaction) => { + movedTransactions[`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`] = { + ...transaction, + reportID: optimisticExpenseReport.reportID, + }; + }); + + optimisticData.push( + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${chatReport.reportID}`, + value: { + hasOutstandingChildRequest: true, + iouReportID: optimisticExpenseReport.reportID, + }, + }, + // add new optimistic expense report + { + onyxMethod: Onyx.METHOD.SET, + key: `${ONYXKEYS.COLLECTION.REPORT}${optimisticExpenseReport.reportID}`, + value: { + ...optimisticExpenseReport, + pendingFields: { + createChat: 'add', + preview: 'update', + }, + }, + }, + // add transaction to the new expense report + { + onyxMethod: Onyx.METHOD.MERGE_COLLECTION, + key: `${ONYXKEYS.COLLECTION.TRANSACTION}`, + value: movedTransactions, + }, + // add preview report action to main chat + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport.reportID}`, + value: { + [optimisticExpenseReportPreview.reportActionID]: optimisticExpenseReportPreview, + }, + }, + // remove hold report actions from old iou report + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport.reportID}`, + value: { + [optimisticIOUReportAction.reportActionID]: { + ...(optimisticIOUReportAction as OnyxTypes.ReportAction), + pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, + }, + ...removeReportActions, + }, + }, + // add hold report actions to new iou report + { + onyxMethod: Onyx.METHOD.SET, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${optimisticExpenseReport.reportID}`, + value: setReportActions, + }, + ); + } + const successData: OnyxUpdate[] = [ { onyxMethod: Onyx.METHOD.MERGE, From c04ce34a10d650045d9cd638c1598f4ad769f648 Mon Sep 17 00:00:00 2001 From: war-in Date: Fri, 24 May 2024 10:32:19 +0200 Subject: [PATCH 05/23] add optimisticHoldReportID api parameter --- src/libs/API/parameters/ApproveMoneyRequestParams.ts | 1 + src/libs/API/parameters/PayMoneyRequestParams.ts | 1 + src/libs/actions/IOU.ts | 3 +++ 3 files changed, 5 insertions(+) diff --git a/src/libs/API/parameters/ApproveMoneyRequestParams.ts b/src/libs/API/parameters/ApproveMoneyRequestParams.ts index fc6528047f22..49e367b85ba9 100644 --- a/src/libs/API/parameters/ApproveMoneyRequestParams.ts +++ b/src/libs/API/parameters/ApproveMoneyRequestParams.ts @@ -2,6 +2,7 @@ type ApproveMoneyRequestParams = { reportID: string; approvedReportActionID: string; full?: boolean; + optimisticHoldReportID?: string; }; export default ApproveMoneyRequestParams; diff --git a/src/libs/API/parameters/PayMoneyRequestParams.ts b/src/libs/API/parameters/PayMoneyRequestParams.ts index 229cb0b8c98e..b196f187e676 100644 --- a/src/libs/API/parameters/PayMoneyRequestParams.ts +++ b/src/libs/API/parameters/PayMoneyRequestParams.ts @@ -7,6 +7,7 @@ type PayMoneyRequestParams = { paymentMethodType: PaymentMethodType; full: boolean; amount?: number; + optimisticHoldReportID?: string; }; export default PayMoneyRequestParams; diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 7f313c9f54ad..81592947761f 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -5852,6 +5852,7 @@ function getPayMoneyRequestParams( }, ]; + let optimisticHoldReportID; if (!full) { const holdTransactions: OnyxTypes.Transaction[] = Object.values(allTransactions).filter( (transaction): transaction is OnyxTypes.Transaction => transaction !== null && transaction.reportID === iouReport.reportID && !!transaction.comment?.hold, @@ -5889,6 +5890,7 @@ function getPayMoneyRequestParams( }; }); + optimisticHoldReportID = optimisticExpenseReport.reportID; optimisticData.push( { onyxMethod: Onyx.METHOD.MERGE, @@ -6016,6 +6018,7 @@ function getPayMoneyRequestParams( paymentMethodType, full, amount: Math.abs(total), + optimisticHoldReportID, }, optimisticData, successData, From a321cc3c8f0bf69ed3e34635fa0be7128707ebb1 Mon Sep 17 00:00:00 2001 From: war-in Date: Fri, 24 May 2024 14:53:24 +0200 Subject: [PATCH 06/23] add failureData --- src/libs/actions/IOU.ts | 218 ++++++++++++++++++++++++---------------- 1 file changed, 130 insertions(+), 88 deletions(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 81592947761f..3dc39909a2f4 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -43,6 +43,7 @@ import * as ReportActionsUtils from '@libs/ReportActionsUtils'; import type {OptimisticChatReport, OptimisticCreatedReportAction, OptimisticIOUReportAction, TransactionDetails} from '@libs/ReportUtils'; import * as ReportUtils from '@libs/ReportUtils'; import * as TransactionUtils from '@libs/TransactionUtils'; +import {getCurrency} from '@libs/TransactionUtils'; import * as UserUtils from '@libs/UserUtils'; import ViolationsUtils from '@libs/Violations/ViolationsUtils'; import type {IOUAction, IOUType} from '@src/CONST'; @@ -5852,39 +5853,102 @@ function getPayMoneyRequestParams( }, ]; - let optimisticHoldReportID; - if (!full) { - const holdTransactions: OnyxTypes.Transaction[] = Object.values(allTransactions).filter( - (transaction): transaction is OnyxTypes.Transaction => transaction !== null && transaction.reportID === iouReport.reportID && !!transaction.comment?.hold, - ); + const successData: OnyxUpdate[] = [ + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport.reportID}`, + value: { + pendingFields: { + preview: null, + reimbursed: null, + partial: null, + }, + }, + }, + ]; - const reportActionsToBeMoved = Object.values(allReportActions?.[`reportActions_${iouReport.reportID}`] ?? {}).filter((reportAction) => - holdTransactions.find((transaction) => transaction?.transactionID === (reportAction.originalMessage as IOUMessage)?.IOUTransactionID), - ); + const failureData: OnyxUpdate[] = [ + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport.reportID}`, + value: { + [optimisticIOUReportAction.reportActionID]: { + errors: ErrorUtils.getMicroSecondOnyxError('iou.error.other'), + }, + }, + }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport.reportID}`, + value: { + ...iouReport, + }, + }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${chatReport.reportID}`, + value: chatReport, + }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.NEXT_STEP}${iouReport.reportID}`, + value: currentNextStep, + }, + ]; - const removeReportActions: Record = {}; - reportActionsToBeMoved.forEach((reportAction) => { - removeReportActions[reportAction.reportActionID] = null; + // In case the report preview action is loaded locally, let's update it. + if (optimisticReportPreviewAction) { + optimisticData.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport.reportID}`, + value: { + [optimisticReportPreviewAction.reportActionID]: optimisticReportPreviewAction, + }, }); - - const setReportActions: OnyxTypes.ReportActions = {}; - reportActionsToBeMoved.forEach((reportAction) => { - setReportActions[reportAction.reportActionID] = reportAction; + failureData.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport.reportID}`, + value: { + [optimisticReportPreviewAction.reportActionID]: { + created: optimisticReportPreviewAction.created, + }, + }, }); + } + + let optimisticHoldReportID; + if (!full) { + const holdTransactions: OnyxTypes.Transaction[] = Object.values(allTransactions).filter( + (transaction): transaction is OnyxTypes.Transaction => !!transaction && transaction.reportID === iouReport.reportID && !!transaction.comment?.hold, + ); const optimisticExpenseReport = ReportUtils.buildOptimisticExpenseReport( chatReport.reportID, chatReport.policyID ?? iouReport.policyID ?? '', recipient.accountID ?? 1, - holdTransactions[0]?.amount * -1 ?? 0, - iouReport.currency ?? 'PLN', + (holdTransactions[0]?.amount ?? 0) * -1, + getCurrency(holdTransactions[0]), true, ); const optimisticExpenseReportPreview = ReportUtils.buildOptimisticReportPreview(chatReport, optimisticExpenseReport, '', holdTransactions[0]); - const movedTransactions: Record = {}; + const holdReportActions = Object.values(allReportActions?.[`reportActions_${iouReport.reportID}`] ?? {}).filter((reportAction) => + holdTransactions.find((transaction) => transaction?.transactionID === (reportAction.originalMessage as IOUMessage)?.IOUTransactionID), + ); + + const removeHoldReportActions: Record = {}; + holdReportActions.forEach((reportAction) => { + removeHoldReportActions[reportAction.reportActionID] = null; + }); + + const addHoldReportActions: OnyxTypes.ReportActions = {}; + holdReportActions.forEach((reportAction) => { + addHoldReportActions[reportAction.reportActionID] = reportAction; + }); + + const moveTransactions: Record = {}; holdTransactions.forEach((transaction: OnyxTypes.Transaction) => { - movedTransactions[`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`] = { + moveTransactions[`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`] = { ...transaction, reportID: optimisticExpenseReport.reportID, }; @@ -5906,17 +5970,13 @@ function getPayMoneyRequestParams( key: `${ONYXKEYS.COLLECTION.REPORT}${optimisticExpenseReport.reportID}`, value: { ...optimisticExpenseReport, - pendingFields: { - createChat: 'add', - preview: 'update', - }, }, }, // add transaction to the new expense report { onyxMethod: Onyx.METHOD.MERGE_COLLECTION, key: `${ONYXKEYS.COLLECTION.TRANSACTION}`, - value: movedTransactions, + value: moveTransactions, }, // add preview report action to main chat { @@ -5930,84 +5990,66 @@ function getPayMoneyRequestParams( { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport.reportID}`, - value: { - [optimisticIOUReportAction.reportActionID]: { - ...(optimisticIOUReportAction as OnyxTypes.ReportAction), - pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, - }, - ...removeReportActions, - }, + value: removeHoldReportActions, }, // add hold report actions to new iou report { onyxMethod: Onyx.METHOD.SET, key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${optimisticExpenseReport.reportID}`, - value: setReportActions, + value: addHoldReportActions, }, ); - } - const successData: OnyxUpdate[] = [ - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport.reportID}`, - value: { - pendingFields: { - preview: null, - reimbursed: null, - partial: null, - }, - }, - }, - ]; + const moveTransactionsBack: Record = {}; + holdTransactions.forEach((transaction: OnyxTypes.Transaction) => { + moveTransactionsBack[`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`] = transaction; + }); - const failureData: OnyxUpdate[] = [ - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport.reportID}`, - value: { - [optimisticIOUReportAction.reportActionID]: { - errors: ErrorUtils.getMicroSecondOnyxError('iou.error.other'), - }, + const bringReportActionsBack: Record = {}; + holdReportActions.forEach((reportAction) => { + bringReportActionsBack[reportAction.reportActionID] = reportAction; + }); + + failureData.push( + // remove added optimistic expense report + { + onyxMethod: Onyx.METHOD.SET, + key: `${ONYXKEYS.COLLECTION.REPORT}${optimisticExpenseReport.reportID}`, + value: null, }, - }, - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport.reportID}`, - value: { - ...iouReport, + // move transactions back to the old expense report + { + onyxMethod: Onyx.METHOD.MERGE_COLLECTION, + key: `${ONYXKEYS.COLLECTION.TRANSACTION}`, + value: moveTransactionsBack, }, - }, - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${chatReport.reportID}`, - value: chatReport, - }, - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.NEXT_STEP}${iouReport.reportID}`, - value: currentNextStep, - }, - ]; - - // In case the report preview action is loaded locally, let's update it. - if (optimisticReportPreviewAction) { - optimisticData.push({ - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport.reportID}`, - value: { - [optimisticReportPreviewAction.reportActionID]: optimisticReportPreviewAction, + // remove preview report action from the main chat + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport.reportID}`, + value: { + [optimisticExpenseReportPreview.reportActionID]: null, + }, }, - }); - failureData.push({ - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport.reportID}`, - value: { - [optimisticReportPreviewAction.reportActionID]: { - created: optimisticReportPreviewAction.created, + // add hold report actions back to old iou report + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport.reportID}`, + value: { + [optimisticIOUReportAction.reportActionID]: { + ...(optimisticIOUReportAction as OnyxTypes.ReportAction), + pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, + }, + ...bringReportActionsBack, }, }, - }); + // remove hold report actions from the new iou report + { + onyxMethod: Onyx.METHOD.SET, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${optimisticExpenseReport.reportID}`, + value: null, + }, + ); } return { From 827b020f8334653bb14d7908a1d08734e1ed2adc Mon Sep 17 00:00:00 2001 From: war-in Date: Fri, 24 May 2024 14:59:42 +0200 Subject: [PATCH 07/23] fix mistakes --- src/libs/actions/IOU.ts | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 3dc39909a2f4..a5169d6fe74d 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -5825,6 +5825,16 @@ function getPayMoneyRequestParams( lastMessageHtml: optimisticIOUReportAction.message?.[0]?.html, }, }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport.reportID}`, + value: { + [optimisticIOUReportAction.reportActionID]: { + ...(optimisticIOUReportAction as OnyxTypes.ReportAction), + pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, + }, + }, + }, { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport.reportID}`, @@ -6035,13 +6045,7 @@ function getPayMoneyRequestParams( { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport.reportID}`, - value: { - [optimisticIOUReportAction.reportActionID]: { - ...(optimisticIOUReportAction as OnyxTypes.ReportAction), - pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, - }, - ...bringReportActionsBack, - }, + value: bringReportActionsBack, }, // remove hold report actions from the new iou report { From af47d1b1b2fb9196b5dd07decad4bfb5317ab925 Mon Sep 17 00:00:00 2001 From: war-in Date: Fri, 24 May 2024 15:42:09 +0200 Subject: [PATCH 08/23] move code to separate function --- src/libs/actions/IOU.ts | 263 +++++++++++++++++++++------------------- 1 file changed, 138 insertions(+), 125 deletions(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index a5169d6fe74d..805f3a442403 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -5776,6 +5776,140 @@ function getSendMoneyParams( }; } +function getReportFromHoldRequestsOnyxData( + chatReport: OnyxTypes.Report, + iouReport: OnyxTypes.Report, + recipient: Participant, +): {optimisticHoldReportID: string; optimisticData: OnyxUpdate[]; failureData: OnyxUpdate[]} { + const holdTransactions: OnyxTypes.Transaction[] = Object.values(allTransactions).filter( + (transaction): transaction is OnyxTypes.Transaction => !!transaction && transaction.reportID === iouReport.reportID && !!transaction.comment?.hold, + ); + + const optimisticExpenseReport = ReportUtils.buildOptimisticExpenseReport( + chatReport.reportID, + chatReport.policyID ?? iouReport.policyID ?? '', + recipient.accountID ?? 1, + (holdTransactions[0]?.amount ?? 0) * -1, + getCurrency(holdTransactions[0]), + true, + ); + const optimisticExpenseReportPreview = ReportUtils.buildOptimisticReportPreview(chatReport, optimisticExpenseReport, '', holdTransactions[0]); + + const holdReportActions = Object.values(allReportActions?.[`reportActions_${iouReport.reportID}`] ?? {}).filter((reportAction) => + holdTransactions.find((transaction) => transaction?.transactionID === (reportAction.originalMessage as IOUMessage)?.IOUTransactionID), + ); + + const removeHoldReportActions: Record = {}; + holdReportActions.forEach((reportAction) => { + removeHoldReportActions[reportAction.reportActionID] = null; + }); + + const addHoldReportActions: OnyxTypes.ReportActions = {}; + holdReportActions.forEach((reportAction) => { + addHoldReportActions[reportAction.reportActionID] = reportAction; + }); + + const moveTransactions: Record = {}; + holdTransactions.forEach((transaction: OnyxTypes.Transaction) => { + moveTransactions[`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`] = { + ...transaction, + reportID: optimisticExpenseReport.reportID, + }; + }); + + const optimisticData: OnyxUpdate[] = [ + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${chatReport.reportID}`, + value: { + hasOutstandingChildRequest: true, + iouReportID: optimisticExpenseReport.reportID, + }, + }, + // add new optimistic expense report + { + onyxMethod: Onyx.METHOD.SET, + key: `${ONYXKEYS.COLLECTION.REPORT}${optimisticExpenseReport.reportID}`, + value: { + ...optimisticExpenseReport, + }, + }, + // add transaction to the new expense report + { + onyxMethod: Onyx.METHOD.MERGE_COLLECTION, + key: `${ONYXKEYS.COLLECTION.TRANSACTION}`, + value: moveTransactions, + }, + // add preview report action to main chat + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport.reportID}`, + value: { + [optimisticExpenseReportPreview.reportActionID]: optimisticExpenseReportPreview, + }, + }, + // remove hold report actions from old iou report + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport.reportID}`, + value: removeHoldReportActions, + }, + // add hold report actions to new iou report + { + onyxMethod: Onyx.METHOD.SET, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${optimisticExpenseReport.reportID}`, + value: addHoldReportActions, + }, + ]; + + const moveTransactionsBack: Record = {}; + holdTransactions.forEach((transaction: OnyxTypes.Transaction) => { + moveTransactionsBack[`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`] = transaction; + }); + + const bringReportActionsBack: Record = {}; + holdReportActions.forEach((reportAction) => { + bringReportActionsBack[reportAction.reportActionID] = reportAction; + }); + + const failureData: OnyxUpdate[] = [ + // remove added optimistic expense report + { + onyxMethod: Onyx.METHOD.SET, + key: `${ONYXKEYS.COLLECTION.REPORT}${optimisticExpenseReport.reportID}`, + value: null, + }, + // move transactions back to the old expense report + { + onyxMethod: Onyx.METHOD.MERGE_COLLECTION, + key: `${ONYXKEYS.COLLECTION.TRANSACTION}`, + value: moveTransactionsBack, + }, + // remove preview report action from the main chat + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport.reportID}`, + value: { + [optimisticExpenseReportPreview.reportActionID]: null, + }, + }, + // add hold report actions back to old iou report + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport.reportID}`, + value: bringReportActionsBack, + }, + // remove hold report actions from the new iou report + { + onyxMethod: Onyx.METHOD.SET, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${optimisticExpenseReport.reportID}`, + value: null, + }, + ]; + + return {optimisticData, failureData, optimisticHoldReportID: optimisticExpenseReport.reportID}; +} + function getPayMoneyRequestParams( chatReport: OnyxTypes.Report, iouReport: OnyxTypes.Report, @@ -5928,132 +6062,11 @@ function getPayMoneyRequestParams( let optimisticHoldReportID; if (!full) { - const holdTransactions: OnyxTypes.Transaction[] = Object.values(allTransactions).filter( - (transaction): transaction is OnyxTypes.Transaction => !!transaction && transaction.reportID === iouReport.reportID && !!transaction.comment?.hold, - ); - - const optimisticExpenseReport = ReportUtils.buildOptimisticExpenseReport( - chatReport.reportID, - chatReport.policyID ?? iouReport.policyID ?? '', - recipient.accountID ?? 1, - (holdTransactions[0]?.amount ?? 0) * -1, - getCurrency(holdTransactions[0]), - true, - ); - const optimisticExpenseReportPreview = ReportUtils.buildOptimisticReportPreview(chatReport, optimisticExpenseReport, '', holdTransactions[0]); - - const holdReportActions = Object.values(allReportActions?.[`reportActions_${iouReport.reportID}`] ?? {}).filter((reportAction) => - holdTransactions.find((transaction) => transaction?.transactionID === (reportAction.originalMessage as IOUMessage)?.IOUTransactionID), - ); - - const removeHoldReportActions: Record = {}; - holdReportActions.forEach((reportAction) => { - removeHoldReportActions[reportAction.reportActionID] = null; - }); - - const addHoldReportActions: OnyxTypes.ReportActions = {}; - holdReportActions.forEach((reportAction) => { - addHoldReportActions[reportAction.reportActionID] = reportAction; - }); - - const moveTransactions: Record = {}; - holdTransactions.forEach((transaction: OnyxTypes.Transaction) => { - moveTransactions[`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`] = { - ...transaction, - reportID: optimisticExpenseReport.reportID, - }; - }); + const holdReportOnyxData = getReportFromHoldRequestsOnyxData(chatReport, iouReport, recipient); - optimisticHoldReportID = optimisticExpenseReport.reportID; - optimisticData.push( - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${chatReport.reportID}`, - value: { - hasOutstandingChildRequest: true, - iouReportID: optimisticExpenseReport.reportID, - }, - }, - // add new optimistic expense report - { - onyxMethod: Onyx.METHOD.SET, - key: `${ONYXKEYS.COLLECTION.REPORT}${optimisticExpenseReport.reportID}`, - value: { - ...optimisticExpenseReport, - }, - }, - // add transaction to the new expense report - { - onyxMethod: Onyx.METHOD.MERGE_COLLECTION, - key: `${ONYXKEYS.COLLECTION.TRANSACTION}`, - value: moveTransactions, - }, - // add preview report action to main chat - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport.reportID}`, - value: { - [optimisticExpenseReportPreview.reportActionID]: optimisticExpenseReportPreview, - }, - }, - // remove hold report actions from old iou report - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport.reportID}`, - value: removeHoldReportActions, - }, - // add hold report actions to new iou report - { - onyxMethod: Onyx.METHOD.SET, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${optimisticExpenseReport.reportID}`, - value: addHoldReportActions, - }, - ); - - const moveTransactionsBack: Record = {}; - holdTransactions.forEach((transaction: OnyxTypes.Transaction) => { - moveTransactionsBack[`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`] = transaction; - }); - - const bringReportActionsBack: Record = {}; - holdReportActions.forEach((reportAction) => { - bringReportActionsBack[reportAction.reportActionID] = reportAction; - }); - - failureData.push( - // remove added optimistic expense report - { - onyxMethod: Onyx.METHOD.SET, - key: `${ONYXKEYS.COLLECTION.REPORT}${optimisticExpenseReport.reportID}`, - value: null, - }, - // move transactions back to the old expense report - { - onyxMethod: Onyx.METHOD.MERGE_COLLECTION, - key: `${ONYXKEYS.COLLECTION.TRANSACTION}`, - value: moveTransactionsBack, - }, - // remove preview report action from the main chat - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport.reportID}`, - value: { - [optimisticExpenseReportPreview.reportActionID]: null, - }, - }, - // add hold report actions back to old iou report - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport.reportID}`, - value: bringReportActionsBack, - }, - // remove hold report actions from the new iou report - { - onyxMethod: Onyx.METHOD.SET, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${optimisticExpenseReport.reportID}`, - value: null, - }, - ); + optimisticData.push(...holdReportOnyxData.optimisticData); + failureData.push(...holdReportOnyxData.failureData); + optimisticHoldReportID = holdReportOnyxData.optimisticHoldReportID; } return { From 26ace8c8abe711427080a3d8d595c294d5564895 Mon Sep 17 00:00:00 2001 From: war-in Date: Fri, 24 May 2024 16:57:10 +0200 Subject: [PATCH 09/23] add logic to approve flow --- src/libs/actions/IOU.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 805f3a442403..59cd6e45717c 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -6311,10 +6311,20 @@ function approveMoneyRequest(expenseReport: OnyxTypes.Report | EmptyObject, full }); } + let optimisticHoldReportID; + if (!full) { + const holdReportOnyxData = getReportFromHoldRequestsOnyxData(chatReport, expenseReport, expenseReport.ownerAccountID); + + optimisticData.push(...holdReportOnyxData.optimisticData); + failureData.push(...holdReportOnyxData.failureData); + optimisticHoldReportID = holdReportOnyxData.optimisticHoldReportID; + } + const parameters: ApproveMoneyRequestParams = { reportID: expenseReport.reportID, approvedReportActionID: optimisticApprovedReportAction.reportActionID, full, + optimisticHoldReportID, }; API.write(WRITE_COMMANDS.APPROVE_MONEY_REQUEST, parameters, {optimisticData, successData, failureData}); From 039a1231ce91a719e856cb33ee2ebb7dcd4bac3f Mon Sep 17 00:00:00 2001 From: war-in Date: Fri, 7 Jun 2024 14:21:39 +0200 Subject: [PATCH 10/23] add parentReportActionID to optimistic expense report --- src/libs/actions/IOU.ts | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index b95ffaeca5ff..47bcd77264da 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -5882,6 +5882,7 @@ function getReportFromHoldRequestsOnyxData( true, ); const optimisticExpenseReportPreview = ReportUtils.buildOptimisticReportPreview(chatReport, optimisticExpenseReport, '', holdTransactions[0]); + optimisticExpenseReport.parentReportActionID = optimisticExpenseReportPreview.reportActionID; const holdReportActions = Object.values(allReportActions?.[`reportActions_${iouReport.reportID}`] ?? {}).filter((reportAction) => holdTransactions.find((transaction) => transaction?.transactionID === (reportAction.originalMessage as IOUMessage)?.IOUTransactionID), @@ -5906,14 +5907,14 @@ function getReportFromHoldRequestsOnyxData( }); const optimisticData: OnyxUpdate[] = [ - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${chatReport.reportID}`, - value: { - hasOutstandingChildRequest: true, - iouReportID: optimisticExpenseReport.reportID, - }, - }, + // { + // onyxMethod: Onyx.METHOD.MERGE, + // key: `${ONYXKEYS.COLLECTION.REPORT}${chatReport.reportID}`, + // value: { + // hasOutstandingChildRequest: true, + // iouReportID: optimisticExpenseReport.reportID, + // }, + // }, // add new optimistic expense report { onyxMethod: Onyx.METHOD.SET, From 245b715fb2cab49536a19cdf6516f633e2b1d203 Mon Sep 17 00:00:00 2001 From: war-in Date: Mon, 10 Jun 2024 17:07:13 +0200 Subject: [PATCH 11/23] mark as deleted and create new reportActions --- src/libs/actions/IOU.ts | 68 ++++++++++++++++++++++++++++++++--------- 1 file changed, 53 insertions(+), 15 deletions(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 47bcd77264da..2f5e0879d8c4 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -37,11 +37,12 @@ import * as LocalePhoneNumber from '@libs/LocalePhoneNumber'; import * as Localize from '@libs/Localize'; import Navigation from '@libs/Navigation/Navigation'; import * as NextStepUtils from '@libs/NextStepUtils'; +import {rand64} from '@libs/NumberUtils'; import Permissions from '@libs/Permissions'; import * as PhoneNumber from '@libs/PhoneNumber'; import * as PolicyUtils from '@libs/PolicyUtils'; import * as ReportActionsUtils from '@libs/ReportActionsUtils'; -import type {OptimisticChatReport, OptimisticCreatedReportAction, OptimisticIOUReportAction, TransactionDetails} from '@libs/ReportUtils'; +import {getReport, OptimisticChatReport, OptimisticCreatedReportAction, OptimisticIOUReportAction, TransactionDetails} from '@libs/ReportUtils'; import * as ReportUtils from '@libs/ReportUtils'; import * as TransactionUtils from '@libs/TransactionUtils'; import {getCurrency} from '@libs/TransactionUtils'; @@ -5879,23 +5880,52 @@ function getReportFromHoldRequestsOnyxData( recipient.accountID ?? 1, (holdTransactions[0]?.amount ?? 0) * -1, getCurrency(holdTransactions[0]), - true, + false, ); const optimisticExpenseReportPreview = ReportUtils.buildOptimisticReportPreview(chatReport, optimisticExpenseReport, '', holdTransactions[0]); - optimisticExpenseReport.parentReportActionID = optimisticExpenseReportPreview.reportActionID; + optimisticExpenseReportPreview.isOptimisticAction = true; const holdReportActions = Object.values(allReportActions?.[`reportActions_${iouReport.reportID}`] ?? {}).filter((reportAction) => holdTransactions.find((transaction) => transaction?.transactionID === (reportAction.originalMessage as IOUMessage)?.IOUTransactionID), ); - const removeHoldReportActions: Record = {}; + const deleteHoldReportActions: Record = {}; holdReportActions.forEach((reportAction) => { - removeHoldReportActions[reportAction.reportActionID] = null; + deleteHoldReportActions[reportAction.reportActionID] = { + ...reportAction, + message: [ + { + deleted: DateUtils.getDBTime(), + type: 'TEXT', + }, + ], + originalMessage: { + deleted: DateUtils.getDBTime(), + }, + }; }); + let previousReportActionID = '0'; + const updateHeldReports: Record = {}; const addHoldReportActions: OnyxTypes.ReportActions = {}; + const removeReportActionsFromOptimisticReport: OnyxTypes.ReportActions = {}; holdReportActions.forEach((reportAction) => { - addHoldReportActions[reportAction.reportActionID] = reportAction; + reportAction.originalMessage.IOUReportID = Number(optimisticExpenseReport.reportID); + reportAction.originalMessage.type = 'create'; + reportAction.previousReportActionID = previousReportActionID; + const reportActionID = rand64(); + reportAction.reportActionID = reportActionID; + + addHoldReportActions[reportActionID] = reportAction; + removeReportActionsFromOptimisticReport[reportActionID] = {}; + + const heldReport = getReport(reportAction.childReportID); + updateHeldReports[heldReport?.reportID ?? '0'] = { + ...heldReport, + parentReportActionID: reportActionID, + }; + + previousReportActionID = reportActionID; }); const moveTransactions: Record = {}; @@ -5906,15 +5936,17 @@ function getReportFromHoldRequestsOnyxData( }; }); + // okazuje się, że backend nie przenosi report actions pomiędzy starym i nowym raportem + // wywala je z pierwszego i tak jakby tworzy nowe w nowym + // trzeba też w takim razie pamiętać o zmianie pola parentReportActionID w przenoszonych raportach (tych podłączonych pod przenoszone akcje) const optimisticData: OnyxUpdate[] = [ - // { - // onyxMethod: Onyx.METHOD.MERGE, - // key: `${ONYXKEYS.COLLECTION.REPORT}${chatReport.reportID}`, - // value: { - // hasOutstandingChildRequest: true, - // iouReportID: optimisticExpenseReport.reportID, - // }, - // }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${chatReport.reportID}`, + value: { + iouReportID: optimisticExpenseReport.reportID, + }, + }, // add new optimistic expense report { onyxMethod: Onyx.METHOD.SET, @@ -5941,7 +5973,7 @@ function getReportFromHoldRequestsOnyxData( { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport.reportID}`, - value: removeHoldReportActions, + value: deleteHoldReportActions, }, // add hold report actions to new iou report { @@ -5949,6 +5981,12 @@ function getReportFromHoldRequestsOnyxData( key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${optimisticExpenseReport.reportID}`, value: addHoldReportActions, }, + // update held reports with new parentReportActionID + { + onyxMethod: Onyx.METHOD.MERGE_COLLECTION, + key: `${ONYXKEYS.COLLECTION.REPORT}`, + value: updateHeldReports, + }, ]; const moveTransactionsBack: Record = {}; From a49af7be047ddf0b2788c2ca7c6eea5687aac922 Mon Sep 17 00:00:00 2001 From: war-in Date: Mon, 10 Jun 2024 18:09:56 +0200 Subject: [PATCH 12/23] non-clickable optimistic report preview --- .../ReportActionItem/ReportPreview.tsx | 3 + src/libs/actions/IOU.ts | 87 ++++--------------- 2 files changed, 18 insertions(+), 72 deletions(-) diff --git a/src/components/ReportActionItem/ReportPreview.tsx b/src/components/ReportActionItem/ReportPreview.tsx index 0bf5d18ddfa4..b3ec45156afe 100644 --- a/src/components/ReportActionItem/ReportPreview.tsx +++ b/src/components/ReportActionItem/ReportPreview.tsx @@ -302,6 +302,9 @@ function ReportPreview({ { + if (action.isOptimisticAction) { + return; + } Navigation.navigate(ROUTES.REPORT_WITH_ID.getRoute(iouReportID)); }} onPressIn={() => DeviceCapabilities.canUseTouchScreen() && ControlSelection.block()} diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 2f5e0879d8c4..e60f625643ee 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -37,12 +37,11 @@ import * as LocalePhoneNumber from '@libs/LocalePhoneNumber'; import * as Localize from '@libs/Localize'; import Navigation from '@libs/Navigation/Navigation'; import * as NextStepUtils from '@libs/NextStepUtils'; -import {rand64} from '@libs/NumberUtils'; import Permissions from '@libs/Permissions'; import * as PhoneNumber from '@libs/PhoneNumber'; import * as PolicyUtils from '@libs/PolicyUtils'; import * as ReportActionsUtils from '@libs/ReportActionsUtils'; -import {getReport, OptimisticChatReport, OptimisticCreatedReportAction, OptimisticIOUReportAction, TransactionDetails} from '@libs/ReportUtils'; +import type {OptimisticChatReport, OptimisticCreatedReportAction, OptimisticIOUReportAction, TransactionDetails} from '@libs/ReportUtils'; import * as ReportUtils from '@libs/ReportUtils'; import * as TransactionUtils from '@libs/TransactionUtils'; import {getCurrency} from '@libs/TransactionUtils'; @@ -5869,7 +5868,7 @@ function getReportFromHoldRequestsOnyxData( chatReport: OnyxTypes.Report, iouReport: OnyxTypes.Report, recipient: Participant, -): {optimisticHoldReportID: string; optimisticData: OnyxUpdate[]; failureData: OnyxUpdate[]} { +): {optimisticHoldReportID: string; optimisticData: OnyxUpdate[]; failureData: OnyxUpdate[]; successData: OnyxUpdate[]} { const holdTransactions: OnyxTypes.Transaction[] = Object.values(allTransactions).filter( (transaction): transaction is OnyxTypes.Transaction => !!transaction && transaction.reportID === iouReport.reportID && !!transaction.comment?.hold, ); @@ -5905,40 +5904,6 @@ function getReportFromHoldRequestsOnyxData( }; }); - let previousReportActionID = '0'; - const updateHeldReports: Record = {}; - const addHoldReportActions: OnyxTypes.ReportActions = {}; - const removeReportActionsFromOptimisticReport: OnyxTypes.ReportActions = {}; - holdReportActions.forEach((reportAction) => { - reportAction.originalMessage.IOUReportID = Number(optimisticExpenseReport.reportID); - reportAction.originalMessage.type = 'create'; - reportAction.previousReportActionID = previousReportActionID; - const reportActionID = rand64(); - reportAction.reportActionID = reportActionID; - - addHoldReportActions[reportActionID] = reportAction; - removeReportActionsFromOptimisticReport[reportActionID] = {}; - - const heldReport = getReport(reportAction.childReportID); - updateHeldReports[heldReport?.reportID ?? '0'] = { - ...heldReport, - parentReportActionID: reportActionID, - }; - - previousReportActionID = reportActionID; - }); - - const moveTransactions: Record = {}; - holdTransactions.forEach((transaction: OnyxTypes.Transaction) => { - moveTransactions[`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`] = { - ...transaction, - reportID: optimisticExpenseReport.reportID, - }; - }); - - // okazuje się, że backend nie przenosi report actions pomiędzy starym i nowym raportem - // wywala je z pierwszego i tak jakby tworzy nowe w nowym - // trzeba też w takim razie pamiętać o zmianie pola parentReportActionID w przenoszonych raportach (tych podłączonych pod przenoszone akcje) const optimisticData: OnyxUpdate[] = [ { onyxMethod: Onyx.METHOD.MERGE, @@ -5955,12 +5920,6 @@ function getReportFromHoldRequestsOnyxData( ...optimisticExpenseReport, }, }, - // add transaction to the new expense report - { - onyxMethod: Onyx.METHOD.MERGE_COLLECTION, - key: `${ONYXKEYS.COLLECTION.TRANSACTION}`, - value: moveTransactions, - }, // add preview report action to main chat { onyxMethod: Onyx.METHOD.MERGE, @@ -5975,25 +5934,8 @@ function getReportFromHoldRequestsOnyxData( key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport.reportID}`, value: deleteHoldReportActions, }, - // add hold report actions to new iou report - { - onyxMethod: Onyx.METHOD.SET, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${optimisticExpenseReport.reportID}`, - value: addHoldReportActions, - }, - // update held reports with new parentReportActionID - { - onyxMethod: Onyx.METHOD.MERGE_COLLECTION, - key: `${ONYXKEYS.COLLECTION.REPORT}`, - value: updateHeldReports, - }, ]; - const moveTransactionsBack: Record = {}; - holdTransactions.forEach((transaction: OnyxTypes.Transaction) => { - moveTransactionsBack[`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`] = transaction; - }); - const bringReportActionsBack: Record = {}; holdReportActions.forEach((reportAction) => { bringReportActionsBack[reportAction.reportActionID] = reportAction; @@ -6006,12 +5948,6 @@ function getReportFromHoldRequestsOnyxData( key: `${ONYXKEYS.COLLECTION.REPORT}${optimisticExpenseReport.reportID}`, value: null, }, - // move transactions back to the old expense report - { - onyxMethod: Onyx.METHOD.MERGE_COLLECTION, - key: `${ONYXKEYS.COLLECTION.TRANSACTION}`, - value: moveTransactionsBack, - }, // remove preview report action from the main chat { onyxMethod: Onyx.METHOD.MERGE, @@ -6022,19 +5958,25 @@ function getReportFromHoldRequestsOnyxData( }, // add hold report actions back to old iou report { - onyxMethod: Onyx.METHOD.MERGE, + onyxMethod: Onyx.METHOD.SET, key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport.reportID}`, value: bringReportActionsBack, }, - // remove hold report actions from the new iou report + ]; + + const successData: OnyxUpdate[] = [ { - onyxMethod: Onyx.METHOD.SET, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${optimisticExpenseReport.reportID}`, - value: null, + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport.reportID}`, + value: { + [optimisticExpenseReportPreview.reportActionID]: { + isOptimisticAction: false, + }, + }, }, ]; - return {optimisticData, failureData, optimisticHoldReportID: optimisticExpenseReport.reportID}; + return {optimisticData, failureData, successData, optimisticHoldReportID: optimisticExpenseReport.reportID}; } function getPayMoneyRequestParams( @@ -6453,6 +6395,7 @@ function approveMoneyRequest(expenseReport: OnyxTypes.Report | EmptyObject, full optimisticData.push(...holdReportOnyxData.optimisticData); failureData.push(...holdReportOnyxData.failureData); + successData.push(...holdReportOnyxData.successData); optimisticHoldReportID = holdReportOnyxData.optimisticHoldReportID; } From ec55722516be50fd320f5738524fbcb1c8b907e5 Mon Sep 17 00:00:00 2001 From: war-in Date: Tue, 11 Jun 2024 13:02:21 +0200 Subject: [PATCH 13/23] Revert "non-clickable optimistic report preview" This reverts commit a49af7be047ddf0b2788c2ca7c6eea5687aac922. --- .../ReportActionItem/ReportPreview.tsx | 3 - src/libs/actions/IOU.ts | 87 +++++++++++++++---- 2 files changed, 72 insertions(+), 18 deletions(-) diff --git a/src/components/ReportActionItem/ReportPreview.tsx b/src/components/ReportActionItem/ReportPreview.tsx index b3ec45156afe..0bf5d18ddfa4 100644 --- a/src/components/ReportActionItem/ReportPreview.tsx +++ b/src/components/ReportActionItem/ReportPreview.tsx @@ -302,9 +302,6 @@ function ReportPreview({ { - if (action.isOptimisticAction) { - return; - } Navigation.navigate(ROUTES.REPORT_WITH_ID.getRoute(iouReportID)); }} onPressIn={() => DeviceCapabilities.canUseTouchScreen() && ControlSelection.block()} diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index e60f625643ee..2f5e0879d8c4 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -37,11 +37,12 @@ import * as LocalePhoneNumber from '@libs/LocalePhoneNumber'; import * as Localize from '@libs/Localize'; import Navigation from '@libs/Navigation/Navigation'; import * as NextStepUtils from '@libs/NextStepUtils'; +import {rand64} from '@libs/NumberUtils'; import Permissions from '@libs/Permissions'; import * as PhoneNumber from '@libs/PhoneNumber'; import * as PolicyUtils from '@libs/PolicyUtils'; import * as ReportActionsUtils from '@libs/ReportActionsUtils'; -import type {OptimisticChatReport, OptimisticCreatedReportAction, OptimisticIOUReportAction, TransactionDetails} from '@libs/ReportUtils'; +import {getReport, OptimisticChatReport, OptimisticCreatedReportAction, OptimisticIOUReportAction, TransactionDetails} from '@libs/ReportUtils'; import * as ReportUtils from '@libs/ReportUtils'; import * as TransactionUtils from '@libs/TransactionUtils'; import {getCurrency} from '@libs/TransactionUtils'; @@ -5868,7 +5869,7 @@ function getReportFromHoldRequestsOnyxData( chatReport: OnyxTypes.Report, iouReport: OnyxTypes.Report, recipient: Participant, -): {optimisticHoldReportID: string; optimisticData: OnyxUpdate[]; failureData: OnyxUpdate[]; successData: OnyxUpdate[]} { +): {optimisticHoldReportID: string; optimisticData: OnyxUpdate[]; failureData: OnyxUpdate[]} { const holdTransactions: OnyxTypes.Transaction[] = Object.values(allTransactions).filter( (transaction): transaction is OnyxTypes.Transaction => !!transaction && transaction.reportID === iouReport.reportID && !!transaction.comment?.hold, ); @@ -5904,6 +5905,40 @@ function getReportFromHoldRequestsOnyxData( }; }); + let previousReportActionID = '0'; + const updateHeldReports: Record = {}; + const addHoldReportActions: OnyxTypes.ReportActions = {}; + const removeReportActionsFromOptimisticReport: OnyxTypes.ReportActions = {}; + holdReportActions.forEach((reportAction) => { + reportAction.originalMessage.IOUReportID = Number(optimisticExpenseReport.reportID); + reportAction.originalMessage.type = 'create'; + reportAction.previousReportActionID = previousReportActionID; + const reportActionID = rand64(); + reportAction.reportActionID = reportActionID; + + addHoldReportActions[reportActionID] = reportAction; + removeReportActionsFromOptimisticReport[reportActionID] = {}; + + const heldReport = getReport(reportAction.childReportID); + updateHeldReports[heldReport?.reportID ?? '0'] = { + ...heldReport, + parentReportActionID: reportActionID, + }; + + previousReportActionID = reportActionID; + }); + + const moveTransactions: Record = {}; + holdTransactions.forEach((transaction: OnyxTypes.Transaction) => { + moveTransactions[`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`] = { + ...transaction, + reportID: optimisticExpenseReport.reportID, + }; + }); + + // okazuje się, że backend nie przenosi report actions pomiędzy starym i nowym raportem + // wywala je z pierwszego i tak jakby tworzy nowe w nowym + // trzeba też w takim razie pamiętać o zmianie pola parentReportActionID w przenoszonych raportach (tych podłączonych pod przenoszone akcje) const optimisticData: OnyxUpdate[] = [ { onyxMethod: Onyx.METHOD.MERGE, @@ -5920,6 +5955,12 @@ function getReportFromHoldRequestsOnyxData( ...optimisticExpenseReport, }, }, + // add transaction to the new expense report + { + onyxMethod: Onyx.METHOD.MERGE_COLLECTION, + key: `${ONYXKEYS.COLLECTION.TRANSACTION}`, + value: moveTransactions, + }, // add preview report action to main chat { onyxMethod: Onyx.METHOD.MERGE, @@ -5934,8 +5975,25 @@ function getReportFromHoldRequestsOnyxData( key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport.reportID}`, value: deleteHoldReportActions, }, + // add hold report actions to new iou report + { + onyxMethod: Onyx.METHOD.SET, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${optimisticExpenseReport.reportID}`, + value: addHoldReportActions, + }, + // update held reports with new parentReportActionID + { + onyxMethod: Onyx.METHOD.MERGE_COLLECTION, + key: `${ONYXKEYS.COLLECTION.REPORT}`, + value: updateHeldReports, + }, ]; + const moveTransactionsBack: Record = {}; + holdTransactions.forEach((transaction: OnyxTypes.Transaction) => { + moveTransactionsBack[`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`] = transaction; + }); + const bringReportActionsBack: Record = {}; holdReportActions.forEach((reportAction) => { bringReportActionsBack[reportAction.reportActionID] = reportAction; @@ -5948,6 +6006,12 @@ function getReportFromHoldRequestsOnyxData( key: `${ONYXKEYS.COLLECTION.REPORT}${optimisticExpenseReport.reportID}`, value: null, }, + // move transactions back to the old expense report + { + onyxMethod: Onyx.METHOD.MERGE_COLLECTION, + key: `${ONYXKEYS.COLLECTION.TRANSACTION}`, + value: moveTransactionsBack, + }, // remove preview report action from the main chat { onyxMethod: Onyx.METHOD.MERGE, @@ -5958,25 +6022,19 @@ function getReportFromHoldRequestsOnyxData( }, // add hold report actions back to old iou report { - onyxMethod: Onyx.METHOD.SET, + onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport.reportID}`, value: bringReportActionsBack, }, - ]; - - const successData: OnyxUpdate[] = [ + // remove hold report actions from the new iou report { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport.reportID}`, - value: { - [optimisticExpenseReportPreview.reportActionID]: { - isOptimisticAction: false, - }, - }, + onyxMethod: Onyx.METHOD.SET, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${optimisticExpenseReport.reportID}`, + value: null, }, ]; - return {optimisticData, failureData, successData, optimisticHoldReportID: optimisticExpenseReport.reportID}; + return {optimisticData, failureData, optimisticHoldReportID: optimisticExpenseReport.reportID}; } function getPayMoneyRequestParams( @@ -6395,7 +6453,6 @@ function approveMoneyRequest(expenseReport: OnyxTypes.Report | EmptyObject, full optimisticData.push(...holdReportOnyxData.optimisticData); failureData.push(...holdReportOnyxData.failureData); - successData.push(...holdReportOnyxData.successData); optimisticHoldReportID = holdReportOnyxData.optimisticHoldReportID; } From 63fed389a3ea346af4e66230df5664551d57939a Mon Sep 17 00:00:00 2001 From: war-in Date: Tue, 11 Jun 2024 15:05:25 +0200 Subject: [PATCH 14/23] clean the code, prepare optimisticHoldReportExpenseActionIDs --- src/libs/actions/IOU.ts | 91 +++++++++++++++++------------------------ 1 file changed, 37 insertions(+), 54 deletions(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 2f5e0879d8c4..2aa57885ab33 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -42,7 +42,7 @@ import Permissions from '@libs/Permissions'; import * as PhoneNumber from '@libs/PhoneNumber'; import * as PolicyUtils from '@libs/PolicyUtils'; import * as ReportActionsUtils from '@libs/ReportActionsUtils'; -import {getReport, OptimisticChatReport, OptimisticCreatedReportAction, OptimisticIOUReportAction, TransactionDetails} from '@libs/ReportUtils'; +import type {OptimisticChatReport, OptimisticCreatedReportAction, OptimisticIOUReportAction, TransactionDetails} from '@libs/ReportUtils'; import * as ReportUtils from '@libs/ReportUtils'; import * as TransactionUtils from '@libs/TransactionUtils'; import {getCurrency} from '@libs/TransactionUtils'; @@ -5865,11 +5865,16 @@ function getSendMoneyParams( }; } +type OptimisticHoldReportExpenseActionID = { + optimisticReportActionID: string; + oldReportActionID?: string; +}; + function getReportFromHoldRequestsOnyxData( chatReport: OnyxTypes.Report, iouReport: OnyxTypes.Report, recipient: Participant, -): {optimisticHoldReportID: string; optimisticData: OnyxUpdate[]; failureData: OnyxUpdate[]} { +): {optimisticHoldReportID: string; optimisticHoldReportExpenseActionIDs: OptimisticHoldReportExpenseActionID[]; optimisticData: OnyxUpdate[]; failureData: OnyxUpdate[]} { const holdTransactions: OnyxTypes.Transaction[] = Object.values(allTransactions).filter( (transaction): transaction is OnyxTypes.Transaction => !!transaction && transaction.reportID === iouReport.reportID && !!transaction.comment?.hold, ); @@ -5889,56 +5894,51 @@ function getReportFromHoldRequestsOnyxData( holdTransactions.find((transaction) => transaction?.transactionID === (reportAction.originalMessage as IOUMessage)?.IOUTransactionID), ); - const deleteHoldReportActions: Record = {}; - holdReportActions.forEach((reportAction) => { + let previousReportActionID = '0'; + const updateHeldReports: Record = {}; + const addHoldReportActions: OnyxTypes.ReportActions = {}; + const deleteHoldReportActions: OnyxTypes.ReportActions = {}; + const optimisticHoldReportExpenseActionIDs: OptimisticHoldReportExpenseActionID[] = []; + + holdReportActions.forEach((holdReportAction) => { + const reportAction = {...holdReportAction}; + const originalMessage = reportAction.originalMessage as IOUMessage; + deleteHoldReportActions[reportAction.reportActionID] = { ...reportAction, message: [ { deleted: DateUtils.getDBTime(), - type: 'TEXT', + type: CONST.REPORT.MESSAGE.TYPE.TEXT, + text: '', }, ], - originalMessage: { - deleted: DateUtils.getDBTime(), - }, }; - }); - let previousReportActionID = '0'; - const updateHeldReports: Record = {}; - const addHoldReportActions: OnyxTypes.ReportActions = {}; - const removeReportActionsFromOptimisticReport: OnyxTypes.ReportActions = {}; - holdReportActions.forEach((reportAction) => { - reportAction.originalMessage.IOUReportID = Number(optimisticExpenseReport.reportID); - reportAction.originalMessage.type = 'create'; - reportAction.previousReportActionID = previousReportActionID; const reportActionID = rand64(); - reportAction.reportActionID = reportActionID; + addHoldReportActions[reportActionID] = { + ...reportAction, + reportActionID, + previousReportActionID, + originalMessage: { + ...originalMessage, + IOUReportID: optimisticExpenseReport.reportID, + }, + }; - addHoldReportActions[reportActionID] = reportAction; - removeReportActionsFromOptimisticReport[reportActionID] = {}; + const heldReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportAction.childReportID}`]; + if (heldReport) { + optimisticHoldReportExpenseActionIDs.push({optimisticReportActionID: reportActionID, oldReportActionID: heldReport?.parentReportActionID}); - const heldReport = getReport(reportAction.childReportID); - updateHeldReports[heldReport?.reportID ?? '0'] = { - ...heldReport, - parentReportActionID: reportActionID, - }; + updateHeldReports[heldReport.reportID] = { + ...heldReport, + parentReportActionID: reportActionID, + }; + } previousReportActionID = reportActionID; }); - const moveTransactions: Record = {}; - holdTransactions.forEach((transaction: OnyxTypes.Transaction) => { - moveTransactions[`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`] = { - ...transaction, - reportID: optimisticExpenseReport.reportID, - }; - }); - - // okazuje się, że backend nie przenosi report actions pomiędzy starym i nowym raportem - // wywala je z pierwszego i tak jakby tworzy nowe w nowym - // trzeba też w takim razie pamiętać o zmianie pola parentReportActionID w przenoszonych raportach (tych podłączonych pod przenoszone akcje) const optimisticData: OnyxUpdate[] = [ { onyxMethod: Onyx.METHOD.MERGE, @@ -5955,12 +5955,6 @@ function getReportFromHoldRequestsOnyxData( ...optimisticExpenseReport, }, }, - // add transaction to the new expense report - { - onyxMethod: Onyx.METHOD.MERGE_COLLECTION, - key: `${ONYXKEYS.COLLECTION.TRANSACTION}`, - value: moveTransactions, - }, // add preview report action to main chat { onyxMethod: Onyx.METHOD.MERGE, @@ -5989,11 +5983,6 @@ function getReportFromHoldRequestsOnyxData( }, ]; - const moveTransactionsBack: Record = {}; - holdTransactions.forEach((transaction: OnyxTypes.Transaction) => { - moveTransactionsBack[`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`] = transaction; - }); - const bringReportActionsBack: Record = {}; holdReportActions.forEach((reportAction) => { bringReportActionsBack[reportAction.reportActionID] = reportAction; @@ -6006,12 +5995,6 @@ function getReportFromHoldRequestsOnyxData( key: `${ONYXKEYS.COLLECTION.REPORT}${optimisticExpenseReport.reportID}`, value: null, }, - // move transactions back to the old expense report - { - onyxMethod: Onyx.METHOD.MERGE_COLLECTION, - key: `${ONYXKEYS.COLLECTION.TRANSACTION}`, - value: moveTransactionsBack, - }, // remove preview report action from the main chat { onyxMethod: Onyx.METHOD.MERGE, @@ -6034,7 +6017,7 @@ function getReportFromHoldRequestsOnyxData( }, ]; - return {optimisticData, failureData, optimisticHoldReportID: optimisticExpenseReport.reportID}; + return {optimisticData, failureData, optimisticHoldReportID: optimisticExpenseReport.reportID, optimisticHoldReportExpenseActionIDs}; } function getPayMoneyRequestParams( From 3b049567e20b32726a17cf03fbb0108da576738d Mon Sep 17 00:00:00 2001 From: war-in Date: Fri, 14 Jun 2024 10:59:06 +0200 Subject: [PATCH 15/23] pass optimisticHoldActionID to API --- .../parameters/ApproveMoneyRequestParams.ts | 1 + .../API/parameters/PayMoneyRequestParams.ts | 1 + src/libs/actions/IOU.ts | 22 +++++++++++++++++-- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/libs/API/parameters/ApproveMoneyRequestParams.ts b/src/libs/API/parameters/ApproveMoneyRequestParams.ts index 49e367b85ba9..a3925f39a943 100644 --- a/src/libs/API/parameters/ApproveMoneyRequestParams.ts +++ b/src/libs/API/parameters/ApproveMoneyRequestParams.ts @@ -3,6 +3,7 @@ type ApproveMoneyRequestParams = { approvedReportActionID: string; full?: boolean; optimisticHoldReportID?: string; + optimisticHoldActionID?: string; }; export default ApproveMoneyRequestParams; diff --git a/src/libs/API/parameters/PayMoneyRequestParams.ts b/src/libs/API/parameters/PayMoneyRequestParams.ts index b196f187e676..3ccf28ab867d 100644 --- a/src/libs/API/parameters/PayMoneyRequestParams.ts +++ b/src/libs/API/parameters/PayMoneyRequestParams.ts @@ -8,6 +8,7 @@ type PayMoneyRequestParams = { full: boolean; amount?: number; optimisticHoldReportID?: string; + optimisticHoldActionID?: string; }; export default PayMoneyRequestParams; diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index be363efaf313..356582e88a21 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -5903,7 +5903,13 @@ function getReportFromHoldRequestsOnyxData( chatReport: OnyxTypes.Report, iouReport: OnyxTypes.Report, recipient: Participant, -): {optimisticHoldReportID: string; optimisticHoldReportExpenseActionIDs: OptimisticHoldReportExpenseActionID[]; optimisticData: OnyxUpdate[]; failureData: OnyxUpdate[]} { +): { + optimisticHoldReportID: string; + optimisticHoldActionID: string; + optimisticHoldReportExpenseActionIDs: OptimisticHoldReportExpenseActionID[]; + optimisticData: OnyxUpdate[]; + failureData: OnyxUpdate[]; +} { const holdTransactions: OnyxTypes.Transaction[] = Object.values(allTransactions).filter( (transaction): transaction is OnyxTypes.Transaction => !!transaction && transaction.reportID === iouReport.reportID && !!transaction.comment?.hold, ); @@ -6046,7 +6052,13 @@ function getReportFromHoldRequestsOnyxData( }, ]; - return {optimisticData, failureData, optimisticHoldReportID: optimisticExpenseReport.reportID, optimisticHoldReportExpenseActionIDs}; + return { + optimisticData, + optimisticHoldActionID: optimisticExpenseReportPreview.reportActionID, + failureData, + optimisticHoldReportID: optimisticExpenseReport.reportID, + optimisticHoldReportExpenseActionIDs, + }; } function getPayMoneyRequestParams( @@ -6205,12 +6217,14 @@ function getPayMoneyRequestParams( } let optimisticHoldReportID; + let optimisticHoldActionID; if (!full) { const holdReportOnyxData = getReportFromHoldRequestsOnyxData(chatReport, iouReport, recipient); optimisticData.push(...holdReportOnyxData.optimisticData); failureData.push(...holdReportOnyxData.failureData); optimisticHoldReportID = holdReportOnyxData.optimisticHoldReportID; + optimisticHoldActionID = holdReportOnyxData.optimisticHoldActionID; } return { @@ -6222,6 +6236,7 @@ function getPayMoneyRequestParams( full, amount: Math.abs(total), optimisticHoldReportID, + optimisticHoldActionID, }, optimisticData, successData, @@ -6460,12 +6475,14 @@ function approveMoneyRequest(expenseReport: OnyxTypes.Report | EmptyObject, full } let optimisticHoldReportID; + let optimisticHoldActionID; if (!full) { const holdReportOnyxData = getReportFromHoldRequestsOnyxData(chatReport, expenseReport, expenseReport.ownerAccountID); optimisticData.push(...holdReportOnyxData.optimisticData); failureData.push(...holdReportOnyxData.failureData); optimisticHoldReportID = holdReportOnyxData.optimisticHoldReportID; + optimisticHoldActionID = holdReportOnyxData.optimisticHoldActionID; } const parameters: ApproveMoneyRequestParams = { @@ -6473,6 +6490,7 @@ function approveMoneyRequest(expenseReport: OnyxTypes.Report | EmptyObject, full approvedReportActionID: optimisticApprovedReportAction.reportActionID, full, optimisticHoldReportID, + optimisticHoldActionID, }; API.write(WRITE_COMMANDS.APPROVE_MONEY_REQUEST, parameters, {optimisticData, successData, failureData}); From acdc7ada329c358ebb31073b3e5d3fa36ae4b664 Mon Sep 17 00:00:00 2001 From: war-in Date: Fri, 5 Jul 2024 15:16:05 +0200 Subject: [PATCH 16/23] clean up code, remove lint and type errors --- src/libs/actions/IOU.ts | 46 ++++++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 19 deletions(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 443934bb9fb9..f733c1d44790 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -47,7 +47,7 @@ import type {OptimisticChatReport, OptimisticCreatedReportAction, OptimisticIOUR import * as ReportUtils from '@libs/ReportUtils'; import * as SubscriptionUtils from '@libs/SubscriptionUtils'; import * as TransactionUtils from '@libs/TransactionUtils'; -import {getCurrency} from '@libs/TransactionUtils'; +import {getCurrency, getTransaction} from '@libs/TransactionUtils'; import ViolationsUtils from '@libs/Violations/ViolationsUtils'; import type {IOUAction, IOUType} from '@src/CONST'; import CONST from '@src/CONST'; @@ -5950,6 +5950,24 @@ type OptimisticHoldReportExpenseActionID = { oldReportActionID?: string; }; +function getHoldReportActions(reportID: string) { + const iouReportActions = ReportActionsUtils.getAllReportActions(reportID); + + return Object.values(iouReportActions).filter((action) => { + const transactionID = ReportActionsUtils.isMoneyRequestAction(action) ? ReportActionsUtils.getOriginalMessage(action)?.IOUTransactionID ?? null : null; + return transactionID ? !getTransaction(transactionID)?.comment?.hold : false; + }) as Array>; +} + +function getHoldTransaction(reportAction: OnyxTypes.ReportAction): OnyxEntry { + const transactionID = ReportActionsUtils.getOriginalMessage(reportAction)?.IOUTransactionID; + if (transactionID) { + const transaction = getTransaction(transactionID); + return !transaction?.comment?.hold ? transaction : undefined; + } + return undefined; +} + function getReportFromHoldRequestsOnyxData( chatReport: OnyxTypes.Report, iouReport: OnyxTypes.Report, @@ -5961,26 +5979,19 @@ function getReportFromHoldRequestsOnyxData( optimisticData: OnyxUpdate[]; failureData: OnyxUpdate[]; } { - const holdTransactions: OnyxTypes.Transaction[] = Object.values(allTransactions).filter( - (transaction): transaction is OnyxTypes.Transaction => !!transaction && transaction.reportID === iouReport.reportID && !!transaction.comment?.hold, - ); + const holdReportActions = getHoldReportActions(iouReport.reportID); + const firstHoldTransaction = getHoldTransaction(holdReportActions[0]); const optimisticExpenseReport = ReportUtils.buildOptimisticExpenseReport( chatReport.reportID, chatReport.policyID ?? iouReport.policyID ?? '', recipient.accountID ?? 1, - (holdTransactions[0]?.amount ?? 0) * -1, - getCurrency(holdTransactions[0]), + (firstHoldTransaction?.amount ?? 0) * -1, + getCurrency(firstHoldTransaction), false, ); - const optimisticExpenseReportPreview = ReportUtils.buildOptimisticReportPreview(chatReport, optimisticExpenseReport, '', holdTransactions[0]); - optimisticExpenseReportPreview.isOptimisticAction = true; - - const holdReportActions = Object.values(allReportActions?.[`reportActions_${iouReport.reportID}`] ?? {}).filter((reportAction) => - holdTransactions.find((transaction) => transaction?.transactionID === (reportAction.originalMessage as IOUMessage)?.IOUTransactionID), - ); + const optimisticExpenseReportPreview = ReportUtils.buildOptimisticReportPreview(chatReport, optimisticExpenseReport, '', firstHoldTransaction); - let previousReportActionID = '0'; const updateHeldReports: Record = {}; const addHoldReportActions: OnyxTypes.ReportActions = {}; const deleteHoldReportActions: OnyxTypes.ReportActions = {}; @@ -5988,7 +5999,7 @@ function getReportFromHoldRequestsOnyxData( holdReportActions.forEach((holdReportAction) => { const reportAction = {...holdReportAction}; - const originalMessage = reportAction.originalMessage as IOUMessage; + const originalMessage = ReportActionsUtils.getOriginalMessage(reportAction) as OnyxTypes.OriginalMessageIOU; deleteHoldReportActions[reportAction.reportActionID] = { ...reportAction, @@ -6005,7 +6016,6 @@ function getReportFromHoldRequestsOnyxData( addHoldReportActions[reportActionID] = { ...reportAction, reportActionID, - previousReportActionID, originalMessage: { ...originalMessage, IOUReportID: optimisticExpenseReport.reportID, @@ -6021,8 +6031,6 @@ function getReportFromHoldRequestsOnyxData( parentReportActionID: reportActionID, }; } - - previousReportActionID = reportActionID; }); const optimisticData: OnyxUpdate[] = [ @@ -6557,8 +6565,8 @@ function approveMoneyRequest(expenseReport: OnyxEntry, full?: let optimisticHoldReportID; let optimisticHoldActionID; - if (!full) { - const holdReportOnyxData = getReportFromHoldRequestsOnyxData(chatReport, expenseReport, expenseReport.ownerAccountID); + if (!full && !!chatReport && !!expenseReport) { + const holdReportOnyxData = getReportFromHoldRequestsOnyxData(chatReport, expenseReport, {accountID: expenseReport.ownerAccountID}); optimisticData.push(...holdReportOnyxData.optimisticData); failureData.push(...holdReportOnyxData.failureData); From ba4b50e5070c4f182ca6c5157d26ca293034a866 Mon Sep 17 00:00:00 2001 From: war-in Date: Mon, 8 Jul 2024 10:56:00 +0200 Subject: [PATCH 17/23] add `optimisticHoldReportExpenseActionIDs` to API commands --- .../parameters/ApproveMoneyRequestParams.ts | 8 +++++ .../API/parameters/PayMoneyRequestParams.ts | 8 +++++ src/libs/actions/IOU.ts | 29 ++++++++++--------- 3 files changed, 31 insertions(+), 14 deletions(-) diff --git a/src/libs/API/parameters/ApproveMoneyRequestParams.ts b/src/libs/API/parameters/ApproveMoneyRequestParams.ts index a3925f39a943..521226aeeff2 100644 --- a/src/libs/API/parameters/ApproveMoneyRequestParams.ts +++ b/src/libs/API/parameters/ApproveMoneyRequestParams.ts @@ -4,6 +4,14 @@ type ApproveMoneyRequestParams = { full?: boolean; optimisticHoldReportID?: string; optimisticHoldActionID?: string; + /** + * Stringified JSON object with type of following structure: + * Array<{ + * optimisticReportActionID: string; + * oldReportActionID: string; + * }> + */ + optimisticHoldReportExpenseActionIDs?: string; }; export default ApproveMoneyRequestParams; diff --git a/src/libs/API/parameters/PayMoneyRequestParams.ts b/src/libs/API/parameters/PayMoneyRequestParams.ts index 3ccf28ab867d..3ad98429b75c 100644 --- a/src/libs/API/parameters/PayMoneyRequestParams.ts +++ b/src/libs/API/parameters/PayMoneyRequestParams.ts @@ -9,6 +9,14 @@ type PayMoneyRequestParams = { amount?: number; optimisticHoldReportID?: string; optimisticHoldActionID?: string; + /** + * Stringified JSON object with type of following structure: + * Array<{ + * optimisticReportActionID: string; + * oldReportActionID: string; + * }> + */ + optimisticHoldReportExpenseActionIDs?: string; }; export default PayMoneyRequestParams; diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index f733c1d44790..c32bb08fb439 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -5947,7 +5947,7 @@ function getSendMoneyParams( type OptimisticHoldReportExpenseActionID = { optimisticReportActionID: string; - oldReportActionID?: string; + oldReportActionID: string; }; function getHoldReportActions(reportID: string) { @@ -5992,17 +5992,15 @@ function getReportFromHoldRequestsOnyxData( ); const optimisticExpenseReportPreview = ReportUtils.buildOptimisticReportPreview(chatReport, optimisticExpenseReport, '', firstHoldTransaction); - const updateHeldReports: Record = {}; + const updateHeldReports: Record> = {}; const addHoldReportActions: OnyxTypes.ReportActions = {}; - const deleteHoldReportActions: OnyxTypes.ReportActions = {}; + const deleteHoldReportActions: Record> = {}; const optimisticHoldReportExpenseActionIDs: OptimisticHoldReportExpenseActionID[] = []; holdReportActions.forEach((holdReportAction) => { - const reportAction = {...holdReportAction}; - const originalMessage = ReportActionsUtils.getOriginalMessage(reportAction) as OnyxTypes.OriginalMessageIOU; + const originalMessage = ReportActionsUtils.getOriginalMessage(holdReportAction); - deleteHoldReportActions[reportAction.reportActionID] = { - ...reportAction, + deleteHoldReportActions[holdReportAction.reportActionID] = { message: [ { deleted: DateUtils.getDBTime(), @@ -6014,7 +6012,7 @@ function getReportFromHoldRequestsOnyxData( const reportActionID = rand64(); addHoldReportActions[reportActionID] = { - ...reportAction, + ...holdReportAction, reportActionID, originalMessage: { ...originalMessage, @@ -6022,12 +6020,11 @@ function getReportFromHoldRequestsOnyxData( }, }; - const heldReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportAction.childReportID}`]; + const heldReport = getReportOrDraftReport(holdReportAction.childReportID); if (heldReport) { - optimisticHoldReportExpenseActionIDs.push({optimisticReportActionID: reportActionID, oldReportActionID: heldReport?.parentReportActionID}); + optimisticHoldReportExpenseActionIDs.push({optimisticReportActionID: reportActionID, oldReportActionID: holdReportAction.reportActionID}); updateHeldReports[heldReport.reportID] = { - ...heldReport, parentReportActionID: reportActionID, }; } @@ -6045,9 +6042,7 @@ function getReportFromHoldRequestsOnyxData( { onyxMethod: Onyx.METHOD.SET, key: `${ONYXKEYS.COLLECTION.REPORT}${optimisticExpenseReport.reportID}`, - value: { - ...optimisticExpenseReport, - }, + value: optimisticExpenseReport, }, // add preview report action to main chat { @@ -6294,6 +6289,7 @@ function getPayMoneyRequestParams( let optimisticHoldReportID; let optimisticHoldActionID; + let optimisticHoldReportExpenseActionIDs; if (!full) { const holdReportOnyxData = getReportFromHoldRequestsOnyxData(chatReport, iouReport, recipient); @@ -6301,6 +6297,7 @@ function getPayMoneyRequestParams( failureData.push(...holdReportOnyxData.failureData); optimisticHoldReportID = holdReportOnyxData.optimisticHoldReportID; optimisticHoldActionID = holdReportOnyxData.optimisticHoldActionID; + optimisticHoldReportExpenseActionIDs = JSON.stringify(holdReportOnyxData.optimisticHoldReportExpenseActionIDs); } return { @@ -6313,6 +6310,7 @@ function getPayMoneyRequestParams( amount: Math.abs(total), optimisticHoldReportID, optimisticHoldActionID, + optimisticHoldReportExpenseActionIDs, }, optimisticData, successData, @@ -6565,6 +6563,7 @@ function approveMoneyRequest(expenseReport: OnyxEntry, full?: let optimisticHoldReportID; let optimisticHoldActionID; + let optimisticHoldReportExpenseActionIDs; if (!full && !!chatReport && !!expenseReport) { const holdReportOnyxData = getReportFromHoldRequestsOnyxData(chatReport, expenseReport, {accountID: expenseReport.ownerAccountID}); @@ -6572,6 +6571,7 @@ function approveMoneyRequest(expenseReport: OnyxEntry, full?: failureData.push(...holdReportOnyxData.failureData); optimisticHoldReportID = holdReportOnyxData.optimisticHoldReportID; optimisticHoldActionID = holdReportOnyxData.optimisticHoldActionID; + optimisticHoldReportExpenseActionIDs = JSON.stringify(holdReportOnyxData.optimisticHoldReportExpenseActionIDs); } const parameters: ApproveMoneyRequestParams = { @@ -6580,6 +6580,7 @@ function approveMoneyRequest(expenseReport: OnyxEntry, full?: full, optimisticHoldReportID, optimisticHoldActionID, + optimisticHoldReportExpenseActionIDs, }; API.write(WRITE_COMMANDS.APPROVE_MONEY_REQUEST, parameters, {optimisticData, successData, failureData}); From 12c8a8f9711ae6078290bda6c7034cea8d80093e Mon Sep 17 00:00:00 2001 From: war-in Date: Mon, 8 Jul 2024 11:44:26 +0200 Subject: [PATCH 18/23] fix wrong hold transaction condition and update reports --- src/libs/actions/IOU.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index c32bb08fb439..cac5c6d64e9c 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -5955,7 +5955,7 @@ function getHoldReportActions(reportID: string) { return Object.values(iouReportActions).filter((action) => { const transactionID = ReportActionsUtils.isMoneyRequestAction(action) ? ReportActionsUtils.getOriginalMessage(action)?.IOUTransactionID ?? null : null; - return transactionID ? !getTransaction(transactionID)?.comment?.hold : false; + return transactionID ? !!getTransaction(transactionID)?.comment?.hold : false; }) as Array>; } @@ -5992,7 +5992,7 @@ function getReportFromHoldRequestsOnyxData( ); const optimisticExpenseReportPreview = ReportUtils.buildOptimisticReportPreview(chatReport, optimisticExpenseReport, '', firstHoldTransaction); - const updateHeldReports: Record> = {}; + const updateHeldReports: Record> = {}; const addHoldReportActions: OnyxTypes.ReportActions = {}; const deleteHoldReportActions: Record> = {}; const optimisticHoldReportExpenseActionIDs: OptimisticHoldReportExpenseActionID[] = []; @@ -6024,8 +6024,10 @@ function getReportFromHoldRequestsOnyxData( if (heldReport) { optimisticHoldReportExpenseActionIDs.push({optimisticReportActionID: reportActionID, oldReportActionID: holdReportAction.reportActionID}); - updateHeldReports[heldReport.reportID] = { + updateHeldReports[`${ONYXKEYS.COLLECTION.REPORT}${heldReport.reportID}`] = { parentReportActionID: reportActionID, + parentReportID: optimisticExpenseReport.reportID, + chatReportID: optimisticExpenseReport.reportID, }; } }); From 50b780805f76691eddb135f451968b4eb78a81e1 Mon Sep 17 00:00:00 2001 From: war-in Date: Mon, 8 Jul 2024 13:33:14 +0200 Subject: [PATCH 19/23] fix first transaction extraction --- src/libs/actions/IOU.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index bbb12acfcfda..b59d1401c519 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -5966,8 +5966,7 @@ function getHoldReportActions(reportID: string) { function getHoldTransaction(reportAction: OnyxTypes.ReportAction): OnyxEntry { const transactionID = ReportActionsUtils.getOriginalMessage(reportAction)?.IOUTransactionID; if (transactionID) { - const transaction = getTransaction(transactionID); - return !transaction?.comment?.hold ? transaction : undefined; + return getTransaction(transactionID); } return undefined; } @@ -6286,7 +6285,7 @@ function getPayMoneyRequestParams( failureData.push(...holdReportOnyxData.failureData); optimisticHoldReportID = holdReportOnyxData.optimisticHoldReportID; optimisticHoldActionID = holdReportOnyxData.optimisticHoldActionID; - optimisticHoldReportExpenseActionIDs = JSON.stringify(holdReportOnyxData.optimisticHoldReportExpenseActionIDs); + // optimisticHoldReportExpenseActionIDs = JSON.stringify(holdReportOnyxData.optimisticHoldReportExpenseActionIDs); } return { From 5b11c4b0446fdb350c378c3d2001cc4c78e1baa8 Mon Sep 17 00:00:00 2001 From: war-in Date: Wed, 24 Jul 2024 13:46:18 +0200 Subject: [PATCH 20/23] use newest API --- src/libs/actions/IOU.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 0acd66402491..364bd215a1e0 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -6293,7 +6293,7 @@ function getReportFromHoldRequestsOnyxData( }, // add new optimistic expense report { - onyxMethod: Onyx.METHOD.SET, + onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT}${optimisticExpenseReport.reportID}`, value: optimisticExpenseReport, }, @@ -6313,7 +6313,7 @@ function getReportFromHoldRequestsOnyxData( }, // add hold report actions to new iou report { - onyxMethod: Onyx.METHOD.SET, + onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${optimisticExpenseReport.reportID}`, value: addHoldReportActions, }, @@ -6333,7 +6333,7 @@ function getReportFromHoldRequestsOnyxData( const failureData: OnyxUpdate[] = [ // remove added optimistic expense report { - onyxMethod: Onyx.METHOD.SET, + onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT}${optimisticExpenseReport.reportID}`, value: null, }, @@ -6353,7 +6353,7 @@ function getReportFromHoldRequestsOnyxData( }, // remove hold report actions from the new iou report { - onyxMethod: Onyx.METHOD.SET, + onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${optimisticExpenseReport.reportID}`, value: null, }, @@ -6533,7 +6533,7 @@ function getPayMoneyRequestParams( failureData.push(...holdReportOnyxData.failureData); optimisticHoldReportID = holdReportOnyxData.optimisticHoldReportID; optimisticHoldActionID = holdReportOnyxData.optimisticHoldActionID; - // optimisticHoldReportExpenseActionIDs = JSON.stringify(holdReportOnyxData.optimisticHoldReportExpenseActionIDs); + optimisticHoldReportExpenseActionIDs = JSON.stringify(holdReportOnyxData.optimisticHoldReportExpenseActionIDs); } return { From 238270f1b4eda5bd4bd9a5fa574aa5fc8b2092bb Mon Sep 17 00:00:00 2001 From: war-in Date: Wed, 24 Jul 2024 15:55:16 +0200 Subject: [PATCH 21/23] update transactions to see correct expenses number --- src/libs/actions/IOU.ts | 46 ++++++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 364bd215a1e0..fa37206ea0c5 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -6202,21 +6202,22 @@ type OptimisticHoldReportExpenseActionID = { oldReportActionID: string; }; -function getHoldReportActions(reportID: string) { +function getHoldReportActionsAndTransactions(reportID: string) { const iouReportActions = ReportActionsUtils.getAllReportActions(reportID); + const holdReportActions: Array> = []; + const holdTransactions: Array> = []; - return Object.values(iouReportActions).filter((action) => { + Object.values(iouReportActions).forEach((action) => { const transactionID = ReportActionsUtils.isMoneyRequestAction(action) ? ReportActionsUtils.getOriginalMessage(action)?.IOUTransactionID ?? null : null; - return transactionID ? !!getTransaction(transactionID)?.comment?.hold : false; - }) as Array>; -} + const transaction = getTransaction(transactionID ?? '-1'); -function getHoldTransaction(reportAction: OnyxTypes.ReportAction): OnyxEntry { - const transactionID = ReportActionsUtils.getOriginalMessage(reportAction)?.IOUTransactionID; - if (transactionID) { - return getTransaction(transactionID); - } - return undefined; + if (transaction?.comment?.hold) { + holdReportActions.push(action as OnyxTypes.ReportAction); + holdTransactions.push(transaction); + } + }); + + return {holdReportActions, holdTransactions}; } function getReportFromHoldRequestsOnyxData( @@ -6230,8 +6231,8 @@ function getReportFromHoldRequestsOnyxData( optimisticData: OnyxUpdate[]; failureData: OnyxUpdate[]; } { - const holdReportActions = getHoldReportActions(iouReport.reportID); - const firstHoldTransaction = getHoldTransaction(holdReportActions[0]); + const {holdReportActions, holdTransactions} = getHoldReportActionsAndTransactions(iouReport.reportID); + const firstHoldTransaction = holdTransactions[0]; const optimisticExpenseReport = ReportUtils.buildOptimisticExpenseReport( chatReport.reportID, @@ -6241,7 +6242,7 @@ function getReportFromHoldRequestsOnyxData( getCurrency(firstHoldTransaction), false, ); - const optimisticExpenseReportPreview = ReportUtils.buildOptimisticReportPreview(chatReport, optimisticExpenseReport, '', firstHoldTransaction); + const optimisticExpenseReportPreview = ReportUtils.buildOptimisticReportPreview(chatReport, optimisticExpenseReport, '', firstHoldTransaction, optimisticExpenseReport.reportID); const updateHeldReports: Record> = {}; const addHoldReportActions: OnyxTypes.ReportActions = {}; @@ -6283,6 +6284,17 @@ function getReportFromHoldRequestsOnyxData( } }); + const updateHeldTransactions: Record> = {}; + holdTransactions.forEach((transaction) => { + if (!transaction) { + return; + } + + updateHeldTransactions[`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`] = { + reportID: optimisticExpenseReport.reportID, + }; + }); + const optimisticData: OnyxUpdate[] = [ { onyxMethod: Onyx.METHOD.MERGE, @@ -6323,6 +6335,12 @@ function getReportFromHoldRequestsOnyxData( key: `${ONYXKEYS.COLLECTION.REPORT}`, value: updateHeldReports, }, + // update transactions with new iouReportID + { + onyxMethod: Onyx.METHOD.MERGE_COLLECTION, + key: `${ONYXKEYS.COLLECTION.TRANSACTION}`, + value: updateHeldTransactions, + }, ]; const bringReportActionsBack: Record = {}; From 6002cfbfd350737aea01800e49ef86c4599531ec Mon Sep 17 00:00:00 2001 From: war-in Date: Wed, 24 Jul 2024 16:05:36 +0200 Subject: [PATCH 22/23] bring moved transactions back on failure --- src/libs/actions/IOU.ts | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index fa37206ea0c5..45560fa580eb 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -6205,7 +6205,7 @@ type OptimisticHoldReportExpenseActionID = { function getHoldReportActionsAndTransactions(reportID: string) { const iouReportActions = ReportActionsUtils.getAllReportActions(reportID); const holdReportActions: Array> = []; - const holdTransactions: Array> = []; + const holdTransactions: OnyxTypes.Transaction[] = []; Object.values(iouReportActions).forEach((action) => { const transactionID = ReportActionsUtils.isMoneyRequestAction(action) ? ReportActionsUtils.getOriginalMessage(action)?.IOUTransactionID ?? null : null; @@ -6286,10 +6286,6 @@ function getReportFromHoldRequestsOnyxData( const updateHeldTransactions: Record> = {}; holdTransactions.forEach((transaction) => { - if (!transaction) { - return; - } - updateHeldTransactions[`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`] = { reportID: optimisticExpenseReport.reportID, }; @@ -6348,6 +6344,11 @@ function getReportFromHoldRequestsOnyxData( bringReportActionsBack[reportAction.reportActionID] = reportAction; }); + const bringHeldTransactionsBack: Record = {}; + holdTransactions.forEach((transaction) => { + bringHeldTransactionsBack[`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`] = transaction; + }); + const failureData: OnyxUpdate[] = [ // remove added optimistic expense report { @@ -6375,6 +6376,12 @@ function getReportFromHoldRequestsOnyxData( key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${optimisticExpenseReport.reportID}`, value: null, }, + // add hold transactions back to old iou report + { + onyxMethod: Onyx.METHOD.MERGE_COLLECTION, + key: `${ONYXKEYS.COLLECTION.TRANSACTION}`, + value: bringHeldTransactionsBack, + }, ]; return { From 110921d5b090ea40957f409debb27d170cd75a20 Mon Sep 17 00:00:00 2001 From: war-in Date: Wed, 24 Jul 2024 16:14:36 +0200 Subject: [PATCH 23/23] add pending action to all created previews --- src/libs/actions/IOU.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 45560fa580eb..4690e2ca284d 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -6270,6 +6270,7 @@ function getReportFromHoldRequestsOnyxData( ...originalMessage, IOUReportID: optimisticExpenseReport.reportID, }, + pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, }; const heldReport = getReportOrDraftReport(holdReportAction.childReportID);