Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aggregate expense reports in OptionsListUtils #17077

Merged
merged 8 commits into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/CONST.js
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,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;
roryabraham marked this conversation as resolved.
Show resolved Hide resolved
}

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 @@ -1736,6 +1749,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