Skip to content

Commit c002eae

Browse files
committed
change structure of reports by policy ID to simplify the solution
1 parent 9c76f19 commit c002eae

3 files changed

Lines changed: 17 additions & 9 deletions

File tree

src/components/MoneyRequestConfirmationListFooter.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ function MoneyRequestConfirmationListFooter({
288288
const policyID = selectedParticipants?.at(0)?.policyID;
289289
const reportOwnerAccountID = selectedParticipants?.at(0)?.ownerAccountID;
290290
const shouldUseTransactionReport = !!transactionReport && isReportOutstanding(transactionReport, policyID);
291-
const firstOutstandingReport = getOutstandingReportsForUser(policyID, reportOwnerAccountID, Object.values(allReports ?? {})).at(0);
291+
const firstOutstandingReport = getOutstandingReportsForUser(policyID, reportOwnerAccountID, allReports ?? {}).at(0);
292292
let reportName: string | undefined;
293293
if (shouldUseTransactionReport) {
294294
reportName = transactionReport.reportName;

src/libs/ReportUtils.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -863,7 +863,7 @@ type GetReportNameParams = {
863863
policies?: SearchPolicy[];
864864
};
865865

866-
type ReportByPolicyMap = Record<string, Report[]>;
866+
type ReportByPolicyMap = Record<string, OnyxCollection<Report>>;
867867

868868
let currentUserEmail: string | undefined;
869869
let currentUserPrivateDomain: string | undefined;
@@ -949,7 +949,7 @@ Onyx.connect({
949949
return;
950950
}
951951

952-
reportsByPolicyID = Object.values(value).reduce<ReportByPolicyMap>((acc, report) => {
952+
reportsByPolicyID = Object.entries(value).reduce<ReportByPolicyMap>((acc, [reportID, report]) => {
953953
if (!report) {
954954
return acc;
955955
}
@@ -962,9 +962,13 @@ Onyx.connect({
962962
// - Belong to the same workspace
963963
if (report.policyID && report.ownerAccountID === currentUserAccountID && (report.stateNum ?? 0) <= 1) {
964964
if (!acc[report.policyID]) {
965-
acc[report.policyID] = [];
965+
acc[report.policyID] = {};
966966
}
967-
acc[report.policyID].push(report);
967+
968+
acc[report.policyID] = {
969+
...acc[report.policyID],
970+
[reportID]: report,
971+
};
968972
}
969973

970974
return acc;
@@ -7935,8 +7939,12 @@ function shouldDisplayViolationsRBRInLHN(report: OnyxEntry<Report>, transactionV
79357939
}
79367940

79377941
// If any report has a violation, then it should have a RBR
7938-
const potentialReports = reportsByPolicyID[report.policyID] ?? [];
7942+
const potentialReports = Object.values(reportsByPolicyID[report.policyID] ?? {}) ?? [];
79397943
return potentialReports.some((potentialReport) => {
7944+
if (!potentialReport) {
7945+
return false;
7946+
}
7947+
79407948
return (
79417949
!isInvoiceReport(potentialReport) &&
79427950
(hasViolations(potentialReport.reportID, transactionViolations, true) ||
@@ -9985,13 +9993,13 @@ function isReportOutstanding(
99859993
function getOutstandingReportsForUser(
99869994
policyID: string | undefined,
99879995
reportOwnerAccountID: number | undefined,
9988-
reports: Array<OnyxEntry<Report>> = Object.values(allReports ?? {}),
9996+
reports: OnyxCollection<Report> = allReports,
99899997
reportNameValuePairs: OnyxCollection<ReportNameValuePairs> = allReportNameValuePair,
99909998
): Array<OnyxEntry<Report>> {
99919999
if (!reports) {
999210000
return [];
999310001
}
9994-
return reports
10002+
return Object.values(reports)
999510003
.filter((report) => isReportOutstanding(report, policyID, reportNameValuePairs) && report?.ownerAccountID === reportOwnerAccountID)
999610004
.sort((a, b) => a?.reportName?.localeCompare(b?.reportName?.toLowerCase() ?? '') ?? 0);
999710005
}

src/pages/iou/request/step/IOURequestEditReportCommon.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ function IOURequestEditReportCommon({backTo, transactionsReports, selectReport,
7272
const reports = getOutstandingReportsForUser(
7373
policyID,
7474
transactionsReports.at(0)?.ownerAccountID ?? currentUserPersonalDetails.accountID,
75-
Object.values(allReports ?? {}),
75+
allReports ?? {},
7676
reportNameValuePairs,
7777
);
7878
return reports;

0 commit comments

Comments
 (0)