diff --git a/src/libs/ReportUtils.js b/src/libs/ReportUtils.js index b5fc0bff6ec7..d688bc544fec 100644 --- a/src/libs/ReportUtils.js +++ b/src/libs/ReportUtils.js @@ -2410,6 +2410,7 @@ function buildOptimisticReportPreview(chatReport, iouReport, comment = '', trans const hasReceipt = TransactionUtils.hasReceipt(transaction); const isReceiptBeingScanned = hasReceipt && TransactionUtils.isReceiptBeingScanned(transaction); const message = getReportPreviewMessage(iouReport); + const created = DateUtils.getDBTime(); return { reportActionID: NumberUtils.rand64(), reportID: chatReport.reportID, @@ -2426,13 +2427,13 @@ function buildOptimisticReportPreview(chatReport, iouReport, comment = '', trans type: CONST.REPORT.MESSAGE.TYPE.COMMENT, }, ], - created: DateUtils.getDBTime(), + created, accountID: iouReport.managerID || 0, // The preview is initially whispered if created with a receipt, so the actor is the current user as well actorAccountID: hasReceipt ? currentUserAccountID : iouReport.managerID || 0, childMoneyRequestCount: 1, childLastMoneyRequestComment: comment, - childLastReceiptTransactionIDs: hasReceipt ? transaction.transactionID : '', + childRecentReceiptTransactionIDs: hasReceipt ? {[transaction.transactionID]: created} : [], whisperedToAccountIDs: isReceiptBeingScanned ? [currentUserAccountID] : [], }; } @@ -2491,8 +2492,9 @@ function buildOptimisticModifiedExpenseReportAction(transactionThread, oldTransa */ function updateReportPreview(iouReport, reportPreviewAction, isPayRequest = false, comment = '', transaction = undefined) { const hasReceipt = TransactionUtils.hasReceipt(transaction); - const lastReceiptTransactionIDs = lodashGet(reportPreviewAction, 'childLastReceiptTransactionIDs', ''); - const previousTransactionIDs = lastReceiptTransactionIDs.split(',').slice(0, 2); + const recentReceiptTransactions = lodashGet(reportPreviewAction, 'childRecentReceiptTransactionIDs', {}); + const transactionsToKeep = TransactionUtils.getRecentTransactions(recentReceiptTransactions); + const previousTransactions = _.mapObject(recentReceiptTransactions, (value, key) => (_.contains(transactionsToKeep, key) ? value : null)); const message = getReportPreviewMessage(iouReport, reportPreviewAction); return { @@ -2508,7 +2510,12 @@ function updateReportPreview(iouReport, reportPreviewAction, isPayRequest = fals ], childLastMoneyRequestComment: comment || reportPreviewAction.childLastMoneyRequestComment, childMoneyRequestCount: reportPreviewAction.childMoneyRequestCount + (isPayRequest ? 0 : 1), - childLastReceiptTransactionIDs: hasReceipt ? [transaction.transactionID, ...previousTransactionIDs].join(',') : lastReceiptTransactionIDs, + childRecentReceiptTransactionIDs: hasReceipt + ? { + [transaction.transactionID]: transaction.created, + ...previousTransactions, + } + : recentReceiptTransactions, // As soon as we add a transaction without a receipt to the report, it will have ready money requests, // so we remove the whisper whisperedToAccountIDs: hasReceipt ? reportPreviewAction.whisperedToAccountIDs : [], @@ -3752,13 +3759,15 @@ function getParticipantsIDs(report) { * @returns {Object} */ function getReportPreviewDisplayTransactions(reportPreviewAction) { - const transactionIDs = lodashGet(reportPreviewAction, ['childLastReceiptTransactionIDs'], '').split(','); + const transactionIDs = lodashGet(reportPreviewAction, ['childRecentReceiptTransactionIDs']); return _.reduce( - transactionIDs, + _.keys(transactionIDs), (transactions, transactionID) => { - const transaction = TransactionUtils.getTransaction(transactionID); - if (TransactionUtils.hasReceipt(transaction)) { - transactions.push(transaction); + if (transactionIDs[transactionID] !== null) { + const transaction = TransactionUtils.getTransaction(transactionID); + if (TransactionUtils.hasReceipt(transaction)) { + transactions.push(transaction); + } } return transactions; }, diff --git a/src/libs/TransactionUtils.ts b/src/libs/TransactionUtils.ts index beb1f9c323d6..b5b8271c35a3 100644 --- a/src/libs/TransactionUtils.ts +++ b/src/libs/TransactionUtils.ts @@ -374,6 +374,15 @@ function getValidWaypoints(waypoints: WaypointCollection, reArrangeIndexes = fal }, {}); } +/** + * Returns the most recent transactions in an object + */ +function getRecentTransactions(transactions: Record, size = 2): string[] { + return Object.keys(transactions) + .sort((transactionID1, transactionID2) => (new Date(transactions[transactionID1]) < new Date(transactions[transactionID2]) ? 1 : -1)) + .slice(0, size); +} + export { buildOptimisticTransaction, getUpdatedTransaction, @@ -399,4 +408,5 @@ export { hasMissingSmartscanFields, getWaypointIndex, waypointHasValidAddress, + getRecentTransactions, };