Skip to content

Commit

Permalink
Merge pull request #28225 from Expensify/alberto-fixDispl
Browse files Browse the repository at this point in the history
Handle latest receipts as object
  • Loading branch information
arosiclair authored Oct 11, 2023
2 parents 0ce685c + 0a6e1f3 commit 8c73c2b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
29 changes: 19 additions & 10 deletions src/libs/ReportUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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] : [],
};
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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 : [],
Expand Down Expand Up @@ -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;
},
Expand Down
10 changes: 10 additions & 0 deletions src/libs/TransactionUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,15 @@ function getValidWaypoints(waypoints: WaypointCollection, reArrangeIndexes = fal
}, {});
}

/**
* Returns the most recent transactions in an object
*/
function getRecentTransactions(transactions: Record<string, string>, 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,
Expand All @@ -399,4 +408,5 @@ export {
hasMissingSmartscanFields,
getWaypointIndex,
waypointHasValidAddress,
getRecentTransactions,
};

0 comments on commit 8c73c2b

Please sign in to comment.