Skip to content

Only get outstanding reports for current report owner #60857

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

Merged
merged 10 commits into from
Apr 28, 2025
Merged
Show file tree
Hide file tree
Changes from 6 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
5 changes: 3 additions & 2 deletions src/components/MoneyRequestConfirmationListFooter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {getDestinationForDisplay, getSubratesFields, getSubratesForDisplay, getT
import {canSendInvoice, getPerDiemCustomUnit, isMultiLevelTags as isMultiLevelTagsPolicyUtils, isPaidGroupPolicy} from '@libs/PolicyUtils';
import type {ThumbnailAndImageURI} from '@libs/ReceiptUtils';
import {getThumbnailAndImageURIs} from '@libs/ReceiptUtils';
import {buildOptimisticExpenseReport, getDefaultWorkspaceAvatar, getOutstandingReports, isReportOutstanding, populateOptimisticReportFormula} from '@libs/ReportUtils';
import {buildOptimisticExpenseReport, getDefaultWorkspaceAvatar, getOutstandingReportsForUser, isReportOutstanding, populateOptimisticReportFormula} from '@libs/ReportUtils';
import {hasEnabledTags} from '@libs/TagsOptionsListUtils';
import {
getTagForDisplay,
Expand Down Expand Up @@ -286,8 +286,9 @@ function MoneyRequestConfirmationListFooter({
*/
const transactionReport = !!transaction?.reportID && Object.values(allReports ?? {}).find((report) => report?.reportID === transaction.reportID);
const policyID = selectedParticipants?.at(0)?.policyID;
const reportOwnerAccountID = selectedParticipants?.at(0)?.ownerAccountID;
const shouldUseTransactionReport = !!transactionReport && isReportOutstanding(transactionReport, policyID);
const firstOutstandingReport = getOutstandingReports(policyID, allReports ?? {}).at(0);
const firstOutstandingReport = getOutstandingReportsForUser(policyID, reportOwnerAccountID, allReports ?? {}).at(0);
let reportName: string | undefined;
if (shouldUseTransactionReport) {
reportName = transactionReport.reportName;
Expand Down
9 changes: 5 additions & 4 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3866,7 +3866,7 @@ function canEditFieldOfMoneyRequest(reportAction: OnyxInputOrEntry<ReportAction>
}

if (fieldToEdit === CONST.EDIT_REQUEST_FIELD.REPORT) {
return getOutstandingReports(moneyRequestReport?.policyID, allReports ?? {}).length > 0;
return getOutstandingReportsForUser(moneyRequestReport?.policyID, moneyRequestReport?.ownerAccountID, allReports ?? {}).length > 0;
}

return true;
Expand Down Expand Up @@ -9552,15 +9552,16 @@ function isReportOutstanding(iouReport: OnyxInputOrEntry<Report>, policyID: stri
/**
* Get outstanding expense reports for a given policy ID
* @param policyID - The policy ID to filter reports by
* @param reportOwnerAccountID - The accountID of the report owner
* @param reports - Collection of reports to filter
* @returns Array of outstanding expense reports sorted by name
*/
function getOutstandingReports(policyID: string | undefined, reports: OnyxCollection<Report> = allReports): Array<OnyxEntry<Report>> {
function getOutstandingReportsForUser(policyID: string | undefined, reportOwnerAccountID: number | undefined, reports: OnyxCollection<Report> = allReports): Array<OnyxEntry<Report>> {
if (!reports) {
return [];
}
return Object.values(reports)
.filter((report) => isReportOutstanding(report, policyID))
.filter((report) => isReportOutstanding(report, policyID) && report?.ownerAccountID === reportOwnerAccountID)
.sort((a, b) => a?.reportName?.localeCompare(b?.reportName?.toLowerCase() ?? '') ?? 0);
}

Expand Down Expand Up @@ -10838,7 +10839,7 @@ export {
getChatListItemReportName,
buildOptimisticMovedTransactionAction,
populateOptimisticReportFormula,
getOutstandingReports,
getOutstandingReportsForUser,
isReportOutstanding,
isAllowedToSubmitDraftExpenseReport,
};
Expand Down
12 changes: 6 additions & 6 deletions src/pages/iou/request/step/IOURequestStepReport.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import {useOnyx} from 'react-native-onyx';
import SelectionList from '@components/SelectionList';
import type {ListItem} from '@components/SelectionList/types';
import UserListItem from '@components/SelectionList/UserListItem';
import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails';
import useDebouncedState from '@hooks/useDebouncedState';
import useLocalize from '@hooks/useLocalize';
import {changeTransactionsReport, setTransactionReport} from '@libs/actions/Transaction';
import Navigation from '@libs/Navigation/Navigation';
import {getOutstandingReports} from '@libs/ReportUtils';
import {getOutstandingReportsForUser} from '@libs/ReportUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type SCREENS from '@src/SCREENS';
Expand All @@ -33,6 +34,7 @@ type IOURequestStepReportProps = WithWritableReportOrNotFoundProps<typeof SCREEN
*/
const reportSelector = (report: OnyxEntry<Report>): OnyxEntry<Report> =>
report && {
ownerAccountID: report.ownerAccountID,
reportID: report.reportID,
policyID: report.policyID,
reportName: report.reportName,
Expand All @@ -45,14 +47,12 @@ function IOURequestStepReport({route, transaction}: IOURequestStepReportProps) {
const {translate} = useLocalize();
const {backTo, action} = route.params;
const [allReports] = useOnyx(ONYXKEYS.COLLECTION.REPORT, {selector: (c) => mapOnyxCollectionItems(c, reportSelector), canBeMissing: true});
const currentUserPersonalDetails = useCurrentUserPersonalDetails();
const [searchValue, debouncedSearchValue, setSearchValue] = useDebouncedState('');
const isEditing = action === CONST.IOU.ACTION.EDIT;
// We need to get the policyID because it's not defined in the transaction object before we select a report manually.
const policyID = Object.values(allReports ?? {}).find(
(report) => report?.reportID === transaction?.reportID || report?.reportID === transaction?.participants?.at(0)?.reportID,
)?.policyID;
const expenseReports = getOutstandingReports(policyID, allReports ?? {});

const transactionReport = Object.values(allReports ?? {}).find((report) => report?.reportID === transaction?.reportID || report?.reportID === transaction?.participants?.at(0)?.reportID);
const expenseReports = getOutstandingReportsForUser(transactionReport?.policyID, transactionReport?.ownerAccountID ?? currentUserPersonalDetails.accountID, allReports ?? {});
const reportOptions: ReportListItem[] = useMemo(() => {
if (!allReports) {
return [];
Expand Down