Skip to content

Commit

Permalink
refactor: use original time in optimistic iou report creation
Browse files Browse the repository at this point in the history
  • Loading branch information
cleverjam committed Apr 4, 2024
1 parent bd1c53d commit 3dc5330
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3193,9 +3193,18 @@ function buildOptimisticTaskCommentReportAction(taskReportID: string, taskTitle:
* @param chatReportID - Report ID of the chat where the IOU is.
* @param currency - IOU currency.
* @param isSendingMoney - If we send money the IOU should be created as settled
* @param created - The creation time for IOU action
*/

function buildOptimisticIOUReport(payeeAccountID: number, payerAccountID: number, total: number, chatReportID: string, currency: string, isSendingMoney = false): OptimisticIOUReport {
function buildOptimisticIOUReport(
payeeAccountID: number,
payerAccountID: number,
total: number,
chatReportID: string,
currency: string,
isSendingMoney = false,
created: string = DateUtils.getDBTime(),
): OptimisticIOUReport {
const formattedTotal = CurrencyUtils.convertToDisplayString(total, currency);
const personalDetails = getPersonalDetailsForAccountID(payerAccountID);
const payerEmail = 'login' in personalDetails ? personalDetails.login : '';
Expand All @@ -3221,7 +3230,7 @@ function buildOptimisticIOUReport(payeeAccountID: number, payerAccountID: number
reportName: `${payerEmail} owes ${formattedTotal}`,
notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN,
parentReportID: chatReportID,
lastVisibleActionCreated: DateUtils.getDBTime(),
lastVisibleActionCreated: created,
};
}

Expand Down Expand Up @@ -3586,7 +3595,7 @@ function buildOptimisticReportPreview(chatReport: OnyxEntry<Report>, iouReport:
const hasReceipt = TransactionUtils.hasReceipt(transaction);
const isReceiptBeingScanned = hasReceipt && TransactionUtils.isReceiptBeingScanned(transaction);
const message = getReportPreviewMessage(iouReport);
const created = DateUtils.getDBTime();
const created = iouReport.lastVisibleActionCreated ?? DateUtils.getDBTime();
return {
reportActionID: NumberUtils.rand64(),
reportID: chatReport?.reportID,
Expand Down Expand Up @@ -4234,10 +4243,10 @@ function buildOptimisticMoneyRequestEntities(
receipt: Receipt = {},
isOwnPolicyExpenseChat = false,
): [OptimisticCreatedReportAction, OptimisticCreatedReportAction, OptimisticIOUReportAction, OptimisticChatReport, OptimisticCreatedReportAction] {
const createdActionForChat = buildOptimisticCreatedReportAction(payeeEmail);
const createdActionForChat = buildOptimisticCreatedReportAction(payeeEmail, iouReport.lastVisibleActionCreated);

// The `CREATED` action must be optimistically generated before the IOU action so that it won't appear after the IOU action in the chat.
const iouActionCreationTime = DateUtils.getDBTime();
const iouActionCreationTime = iouReport.lastVisibleActionCreated ?? DateUtils.getDBTime();
const createdActionForIOUReport = buildOptimisticCreatedReportAction(payeeEmail, DateUtils.subtractMillisecondsFromDateTime(iouActionCreationTime, 1));
const iouAction = buildOptimisticIOUReportAction(
type,
Expand Down
1 change: 1 addition & 0 deletions src/libs/actions/IOU.ts
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,7 @@ function buildOnyxDataForMoneyRequest(
key: `${ONYXKEYS.COLLECTION.REPORT}${chatReport.reportID}`,
value: {
...chatReport,
lastVisibleActionCreated: iouReport.lastVisibleActionCreated,
lastReadTime: DateUtils.getDBTime(),
lastMessageTranslationKey: '',
iouReportID: iouReport.reportID,
Expand Down

0 comments on commit 3dc5330

Please sign in to comment.