Skip to content

Commit

Permalink
Merge pull request #17077 from Expensify/Rory-CollectExpenseReports
Browse files Browse the repository at this point in the history
Aggregate expense reports in OptionsListUtils
  • Loading branch information
luacmartins authored Apr 11, 2023
2 parents abf0004 + dae7172 commit 35a578c
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/CONST.js
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ const CONST = {
},
TYPE: {
CHAT: 'chat',
EXPENSE: 'expense',
IOU: 'iou',
},
CHAT_TYPE: {
Expand Down
15 changes: 12 additions & 3 deletions src/libs/OptionsListUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
},
});

Expand Down
18 changes: 16 additions & 2 deletions src/libs/ReportUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -1146,6 +1158,7 @@ function buildOptimisticChatReport(
) {
const currentTime = DateUtils.getDBTime();
return {
type: CONST.REPORT.TYPE.CHAT,
chatType,
hasOutstandingIOU: false,
isOwnPolicyExpenseChat,
Expand Down Expand Up @@ -1741,6 +1754,7 @@ export {
getAllPolicyReports,
getIOUReportActionMessage,
getDisplayNameForParticipant,
isExpenseReport,
isIOUReport,
chatIncludesChronos,
getAvatar,
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/SidebarOrderTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions tests/utils/LHNTestUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit 35a578c

Please sign in to comment.