diff --git a/src/CONST.js b/src/CONST.js index f0ccd47b3a8f..4f1ff5fc2f17 100755 --- a/src/CONST.js +++ b/src/CONST.js @@ -406,6 +406,7 @@ const CONST = { }, TYPE: { CHAT: 'chat', + EXPENSE: 'expense', IOU: 'iou', }, CHAT_TYPE: { diff --git a/src/libs/OptionsListUtils.js b/src/libs/OptionsListUtils.js index aa2fc49ce9b9..7db2e875660c 100644 --- a/src/libs/OptionsListUtils.js +++ b/src/libs/OptionsListUtils.js @@ -54,14 +54,23 @@ Onyx.connect({ }, }); +const expenseReports = {}; const iouReports = {}; Onyx.connect({ key: ONYXKEYS.COLLECTION.REPORT, - callback: (iouReport, key) => { - if (!iouReport || !key || !iouReport.ownerEmail || !ReportUtils.isIOUReport(iouReport)) { + callback: (report, key) => { + if (!report || !key || !report.ownerEmail) { return; } - iouReports[key] = iouReport; + + if (ReportUtils.isExpenseReport(report)) { + expenseReports[key] = report; + return; + } + + if (ReportUtils.isIOUReport(report)) { + iouReports[key] = report; + } }, }); diff --git a/src/libs/ReportUtils.js b/src/libs/ReportUtils.js index 31e5e98e8c31..4e7305a6d2f7 100644 --- a/src/libs/ReportUtils.js +++ b/src/libs/ReportUtils.js @@ -99,12 +99,23 @@ function getReportParticipantsTitle(logins) { } /** - * Attempts to find a report in onyx with the provided list of participants + * Checks if a report is an Expense report. + * + * @param {Object} report + * @returns {Boolean} + */ +function isExpenseReport(report) { + return lodashGet(report, 'type') === CONST.REPORT.TYPE.EXPENSE; +} + +/** + * Checks if a report is an IOU report. + * * @param {Object} report * @returns {Boolean} */ function isIOUReport(report) { - return report && _.has(report, 'total'); + return lodashGet(report, 'type') === CONST.REPORT.TYPE.IOU; } /** @@ -981,6 +992,7 @@ function buildOptimisticIOUReport(ownerEmail, userEmail, total, chatReportID, cu return { // If we're sending money, hasOutstandingIOU should be false hasOutstandingIOU: !isSendingMoney, + type: CONST.REPORT.TYPE.IOU, cachedTotal: formattedTotal, chatReportID, currency, @@ -1146,6 +1158,7 @@ function buildOptimisticChatReport( ) { const currentTime = DateUtils.getDBTime(); return { + type: CONST.REPORT.TYPE.CHAT, chatType, hasOutstandingIOU: false, isOwnPolicyExpenseChat, @@ -1741,6 +1754,7 @@ export { getAllPolicyReports, getIOUReportActionMessage, getDisplayNameForParticipant, + isExpenseReport, isIOUReport, chatIncludesChronos, getAvatar, diff --git a/tests/unit/SidebarOrderTest.js b/tests/unit/SidebarOrderTest.js index 0706ccf2ab97..ac246e01f8ca 100644 --- a/tests/unit/SidebarOrderTest.js +++ b/tests/unit/SidebarOrderTest.js @@ -330,6 +330,7 @@ describe('Sidebar', () => { }; const iouReport = { ...LHNTestUtils.getFakeReport(['email7@test.com', 'email8@test.com']), + type: CONST.REPORT.TYPE.IOU, ownerEmail: 'email2@test.com', hasOutstandingIOU: true, total: 10000, @@ -635,6 +636,7 @@ describe('Sidebar', () => { }; const iouReport1 = { ...LHNTestUtils.getFakeReport(['email7@test.com', 'email8@test.com']), + type: CONST.REPORT.TYPE.IOU, ownerEmail: 'email2@test.com', hasOutstandingIOU: true, total: 10000, @@ -643,6 +645,7 @@ describe('Sidebar', () => { }; const iouReport2 = { ...LHNTestUtils.getFakeReport(['email9@test.com', 'email10@test.com']), + type: CONST.REPORT.TYPE.IOU, ownerEmail: 'email2@test.com', hasOutstandingIOU: true, total: 10000, @@ -651,6 +654,7 @@ describe('Sidebar', () => { }; const iouReport3 = { ...LHNTestUtils.getFakeReport(['email11@test.com', 'email12@test.com']), + type: CONST.REPORT.TYPE.IOU, ownerEmail: 'email2@test.com', hasOutstandingIOU: true, total: 10000, diff --git a/tests/utils/LHNTestUtils.js b/tests/utils/LHNTestUtils.js index 480fa3fc9059..1a8f7db94427 100644 --- a/tests/utils/LHNTestUtils.js +++ b/tests/utils/LHNTestUtils.js @@ -75,6 +75,7 @@ let lastFakeReportID = 0; function getFakeReport(participants = ['email1@test.com', 'email2@test.com'], millisecondsInThePast = 0, isUnread = false) { const lastVisibleActionCreated = DateUtils.getDBTime(Date.now() - millisecondsInThePast); return { + type: CONST.REPORT.TYPE.CHAT, reportID: `${++lastFakeReportID}`, reportName: 'Report', lastVisibleActionCreated, @@ -98,6 +99,7 @@ function getFakeReport(participants = ['email1@test.com', 'email2@test.com'], mi function getAdvancedFakeReport(isArchived, isUserCreatedPolicyRoom, hasAddWorkspaceError, isUnread, isPinned, hasDraft) { return { ...getFakeReport(['email1@test.com', 'email2@test.com'], 0, isUnread), + type: CONST.REPORT.TYPE.CHAT, chatType: isUserCreatedPolicyRoom ? CONST.REPORT.CHAT_TYPE.POLICY_ROOM : CONST.REPORT.CHAT_TYPE.POLICY_ADMINS, statusNum: isArchived ? CONST.REPORT.STATUS.CLOSED : 0, stateNum: isArchived ? CONST.REPORT.STATE_NUM.SUBMITTED : 0,