Skip to content

Commit 188979a

Browse files
authored
Merge pull request Expensify#62977 from Expensify/revert-62973-revert-62659-cmartins-fixReportSelection
Fix report selection
2 parents 988f305 + 5d5619c commit 188979a

7 files changed

Lines changed: 64 additions & 67 deletions

src/components/MoneyRequestConfirmationListFooter.tsx

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {getDestinationForDisplay, getSubratesFields, getSubratesForDisplay, getT
1717
import {canSendInvoice, getPerDiemCustomUnit, isMultiLevelTags as isMultiLevelTagsPolicyUtils, isPaidGroupPolicy} from '@libs/PolicyUtils';
1818
import type {ThumbnailAndImageURI} from '@libs/ReceiptUtils';
1919
import {getThumbnailAndImageURIs} from '@libs/ReceiptUtils';
20-
import {buildOptimisticExpenseReport, getDefaultWorkspaceAvatar, getOutstandingReportsForUser, isReportOutstanding, populateOptimisticReportFormula} from '@libs/ReportUtils';
20+
import {buildOptimisticExpenseReport, canAddTransaction, getDefaultWorkspaceAvatar, getOutstandingReportsForUser, getReportName, populateOptimisticReportFormula} from '@libs/ReportUtils';
2121
import {hasEnabledTags} from '@libs/TagsOptionsListUtils';
2222
import {
2323
getTagForDisplay,
@@ -247,6 +247,7 @@ function MoneyRequestConfirmationListFooter({
247247
const {isOffline} = useNetwork();
248248
const [allPolicies] = useOnyx(ONYXKEYS.COLLECTION.POLICY, {canBeMissing: true});
249249
const [allReports] = useOnyx(ONYXKEYS.COLLECTION.REPORT, {canBeMissing: true});
250+
const [reportNameValuePairs] = useOnyx(ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS, {canBeMissing: true});
250251

251252
const [currentUserLogin] = useOnyx(ONYXKEYS.SESSION, {selector: (session) => session?.email, canBeMissing: true});
252253

@@ -274,23 +275,21 @@ function MoneyRequestConfirmationListFooter({
274275
* We need to check if the transaction report exists first in order to prevent the outstanding reports from being used.
275276
* Also we need to check if transaction report exists in outstanding reports in order to show a correct report name.
276277
*/
277-
const transactionReport = !!transaction?.reportID && Object.values(allReports ?? {}).find((report) => report?.reportID === transaction.reportID);
278+
const transactionReport = transaction?.reportID ? Object.values(allReports ?? {}).find((report) => report?.reportID === transaction.reportID) : undefined;
279+
const [reportNameValuePair] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${transactionReport?.reportID}`, {canBeMissing: true});
278280
const policyID = selectedParticipants?.at(0)?.policyID;
279-
const reportOwnerAccountID = selectedParticipants?.at(0)?.ownerAccountID;
280-
const shouldUseTransactionReport = !!transactionReport && isReportOutstanding(transactionReport, policyID);
281-
const firstOutstandingReport = getOutstandingReportsForUser(policyID, reportOwnerAccountID, allReports ?? {}).at(0);
282-
let reportName: string | undefined;
283-
if (shouldUseTransactionReport) {
284-
reportName = transactionReport.reportName;
285-
} else {
286-
reportName = firstOutstandingReport?.reportName;
287-
}
281+
const selectedPolicy = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`];
282+
const shouldUseTransactionReport = !!transactionReport && canAddTransaction(transactionReport, !!reportNameValuePair?.private_isArchived);
283+
const outstandingReportID = isPolicyExpenseChat ? allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`]?.iouReportID : reportID;
288284

285+
let reportName = getReportName(shouldUseTransactionReport ? transactionReport : allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${outstandingReportID}`], selectedPolicy);
289286
if (!reportName) {
290-
const optimisticReport = buildOptimisticExpenseReport(reportID, policy?.id, policy?.ownerAccountID ?? CONST.DEFAULT_NUMBER_ID, Number(formattedAmount), currency);
291-
reportName = populateOptimisticReportFormula(policy?.fieldList?.text_title?.defaultValue ?? '', optimisticReport, policy);
287+
const optimisticReport = buildOptimisticExpenseReport(reportID, policyID, selectedPolicy?.ownerAccountID ?? CONST.DEFAULT_NUMBER_ID, Number(formattedAmount), currency);
288+
reportName = populateOptimisticReportFormula(selectedPolicy?.fieldList?.text_title?.defaultValue ?? '', optimisticReport, selectedPolicy);
292289
}
293-
const shouldReportBeEditable = !!firstOutstandingReport;
290+
291+
const availableOutstandingReports = getOutstandingReportsForUser(policyID, selectedParticipants?.at(0)?.ownerAccountID, allReports, reportNameValuePairs);
292+
const shouldReportBeEditable = availableOutstandingReports.length > 1;
294293

295294
const isTypeSend = iouType === CONST.IOU.TYPE.PAY;
296295
const taxRates = policy?.taxRates ?? null;

src/libs/ReportUtils.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9673,7 +9673,12 @@ function getOutstandingReportsForUser(
96739673
return [];
96749674
}
96759675
return Object.values(reports)
9676-
.filter((report) => isReportOutstanding(report, policyID, reportNameValuePairs) && report?.ownerAccountID === reportOwnerAccountID)
9676+
.filter(
9677+
(report) =>
9678+
report?.ownerAccountID === reportOwnerAccountID &&
9679+
report?.policyID === policyID &&
9680+
canAddTransaction(report, !!reportNameValuePairs?.[`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${report?.reportID}`]?.private_isArchived),
9681+
)
96779682
.sort((a, b) => a?.reportName?.localeCompare(b?.reportName?.toLowerCase() ?? '') ?? 0);
96789683
}
96799684

src/pages/Search/SearchTransactionsChangeReport.tsx

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
import React, {useMemo} from 'react';
2-
import {useOnyx} from 'react-native-onyx';
32
import {useSearchContext} from '@components/Search/SearchContext';
43
import type {ListItem} from '@components/SelectionList/types';
54
import {changeTransactionsReport} from '@libs/actions/Transaction';
65
import Navigation from '@libs/Navigation/Navigation';
76
import IOURequestEditReportCommon from '@pages/iou/request/step/IOURequestEditReportCommon';
8-
import ONYXKEYS from '@src/ONYXKEYS';
9-
import type {Report} from '@src/types/onyx';
107

118
type ReportListItem = ListItem & {
129
/** reportID of the report */
@@ -16,18 +13,11 @@ type ReportListItem = ListItem & {
1613
function SearchTransactionsChangeReport() {
1714
const {selectedTransactions, clearSelectedTransactions} = useSearchContext();
1815
const selectedTransactionsKeys = useMemo(() => Object.keys(selectedTransactions), [selectedTransactions]);
19-
20-
const [allReports] = useOnyx(ONYXKEYS.COLLECTION.REPORT, {canBeMissing: false});
21-
const transactionsReports = useMemo(() => {
22-
const reports = Object.values(selectedTransactions).reduce((acc, transaction) => {
23-
const report = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${transaction.reportID}`];
24-
if (report) {
25-
acc.add(report);
26-
}
27-
return acc;
28-
}, new Set<Report>());
29-
return [...reports];
30-
}, [allReports, selectedTransactions]);
16+
const firstTransactionKey = selectedTransactionsKeys.at(0);
17+
const firstTransactionReportID = firstTransactionKey ? selectedTransactions[firstTransactionKey]?.reportID : undefined;
18+
const firstTransactionPolicyID = firstTransactionKey ? selectedTransactions[firstTransactionKey]?.policyID : undefined;
19+
const selectedReportID = Object.values(selectedTransactions).every((transaction) => transaction.reportID === firstTransactionReportID) ? firstTransactionReportID : undefined;
20+
const selectedPolicyID = Object.values(selectedTransactions).every((transaction) => transaction.policyID === firstTransactionPolicyID) ? firstTransactionPolicyID : undefined;
3121

3222
const selectReport = (item: ReportListItem) => {
3323
if (selectedTransactionsKeys.length === 0) {
@@ -43,7 +33,8 @@ function SearchTransactionsChangeReport() {
4333
return (
4434
<IOURequestEditReportCommon
4535
backTo={undefined}
46-
transactionsReports={transactionsReports}
36+
selectedReportID={selectedReportID}
37+
selectedPolicyID={selectedPolicyID}
4738
selectReport={selectReport}
4839
/>
4940
);

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import React from 'react';
2-
import {useOnyx} from 'react-native-onyx';
32
import {useSearchContext} from '@components/Search/SearchContext';
43
import type {ListItem} from '@components/SelectionList/types';
54
import {changeTransactionsReport} from '@libs/actions/Transaction';
65
import Navigation from '@libs/Navigation/Navigation';
7-
import ONYXKEYS from '@src/ONYXKEYS';
86
import type SCREENS from '@src/SCREENS';
97
import IOURequestEditReportCommon from './IOURequestEditReportCommon';
108
import withWritableReportOrNotFound from './withWritableReportOrNotFound';
@@ -22,13 +20,11 @@ function IOURequestEditReport({route}: IOURequestEditReportProps) {
2220

2321
const {selectedTransactionIDs, clearSelectedTransactions} = useSearchContext();
2422

25-
const [transactionReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`, {canBeMissing: false});
26-
2723
const selectReport = (item: ReportListItem) => {
2824
if (selectedTransactionIDs.length === 0) {
2925
return;
3026
}
31-
if (item.value !== transactionReport?.reportID) {
27+
if (item.value !== reportID) {
3228
changeTransactionsReport(selectedTransactionIDs, item.value);
3329
clearSelectedTransactions(true);
3430
}
@@ -38,7 +34,7 @@ function IOURequestEditReport({route}: IOURequestEditReportProps) {
3834
return (
3935
<IOURequestEditReportCommon
4036
backTo={backTo}
41-
transactionsReports={transactionReport ? [transactionReport] : []}
37+
selectedReportID={reportID}
4238
selectReport={selectReport}
4339
/>
4440
);

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

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,12 @@ const reportSelector = (report: OnyxEntry<Report>): OnyxEntry<Report> =>
3838

3939
type Props = {
4040
backTo: Route | undefined;
41-
transactionsReports: Report[];
41+
selectedReportID: string | undefined;
42+
selectedPolicyID?: string | undefined;
4243
selectReport: (item: ReportListItem) => void;
4344
};
4445

45-
function IOURequestEditReportCommon({backTo, transactionsReports, selectReport}: Props) {
46+
function IOURequestEditReportCommon({backTo, selectedReportID, selectedPolicyID, selectReport}: Props) {
4647
const {translate} = useLocalize();
4748
const [allReports] = useOnyx(ONYXKEYS.COLLECTION.REPORT, {selector: (reports) => mapOnyxCollectionItems(reports, reportSelector), canBeMissing: true});
4849
const [reportNameValuePairs] = useOnyx(ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS, {canBeMissing: true});
@@ -51,30 +52,28 @@ function IOURequestEditReportCommon({backTo, transactionsReports, selectReport}:
5152
const currentUserPersonalDetails = useCurrentUserPersonalDetails();
5253
const [searchValue, debouncedSearchValue, setSearchValue] = useDebouncedState('');
5354

54-
const expenseReports = useMemo(
55-
() =>
56-
Object.values(allPoliciesID ?? {}).flatMap((policyID) => {
57-
if (!policyID) {
58-
return [];
59-
}
60-
const reports = getOutstandingReportsForUser(
61-
policyID,
62-
transactionsReports.at(0)?.ownerAccountID ?? currentUserPersonalDetails.accountID,
63-
allReports ?? {},
64-
reportNameValuePairs,
65-
);
66-
return reports;
67-
}),
68-
[allReports, currentUserPersonalDetails.accountID, transactionsReports, allPoliciesID, reportNameValuePairs],
69-
);
55+
const selectedReport = useMemo(() => {
56+
if (!selectedReportID) {
57+
return undefined;
58+
}
59+
return allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${selectedReportID}`];
60+
}, [allReports, selectedReportID]);
61+
62+
const expenseReports = useMemo(() => {
63+
if (!selectedReportID) {
64+
return selectedPolicyID
65+
? getOutstandingReportsForUser(selectedPolicyID, currentUserPersonalDetails.accountID, allReports ?? {}, reportNameValuePairs)
66+
: Object.values(allPoliciesID ?? {}).flatMap((policyID) =>
67+
getOutstandingReportsForUser(policyID, currentUserPersonalDetails.accountID, allReports ?? {}, reportNameValuePairs),
68+
);
69+
}
70+
return getOutstandingReportsForUser(selectedReport?.policyID, currentUserPersonalDetails.accountID, allReports ?? {}, reportNameValuePairs);
71+
}, [allReports, currentUserPersonalDetails.accountID, selectedReport, reportNameValuePairs, allPoliciesID, selectedReportID, selectedPolicyID]);
7072

7173
const reportOptions: ReportListItem[] = useMemo(() => {
7274
if (!allReports) {
7375
return [];
7476
}
75-
76-
const onlyReport = transactionsReports.length === 1 ? transactionsReports.at(0) : undefined;
77-
7877
return expenseReports
7978
.sort((a, b) => a?.reportName?.localeCompare(b?.reportName?.toLowerCase() ?? '') ?? 0)
8079
.filter((report) => !debouncedSearchValue || report?.reportName?.toLowerCase().includes(debouncedSearchValue.toLowerCase()))
@@ -83,9 +82,9 @@ function IOURequestEditReportCommon({backTo, transactionsReports, selectReport}:
8382
text: report.reportName,
8483
value: report.reportID,
8584
keyForList: report.reportID,
86-
isSelected: onlyReport && report.reportID === onlyReport?.reportID,
85+
isSelected: report.reportID === selectedReportID,
8786
}));
88-
}, [allReports, debouncedSearchValue, expenseReports, transactionsReports]);
87+
}, [allReports, debouncedSearchValue, expenseReports, selectedReportID]);
8988

9089
const navigateBack = () => {
9190
Navigation.goBack(backTo);
@@ -110,7 +109,7 @@ function IOURequestEditReportCommon({backTo, transactionsReports, selectReport}:
110109
textInputLabel={expenseReports.length >= CONST.STANDARD_LIST_ITEM_LIMIT ? translate('common.search') : undefined}
111110
shouldSingleExecuteRowSelect
112111
headerMessage={headerMessage}
113-
initiallyFocusedOptionKey={transactionsReports.length === 1 ? transactionsReports.at(0)?.reportID : undefined}
112+
initiallyFocusedOptionKey={selectedReportID}
114113
ListItem={UserListItem}
115114
/>
116115
</StepScreenWrapper>

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import navigateAfterInteraction from '@libs/Navigation/navigateAfterInteraction'
3333
import Navigation from '@libs/Navigation/Navigation';
3434
import {getParticipantsOption, getReportOption} from '@libs/OptionsListUtils';
3535
import Performance from '@libs/Performance';
36-
import {generateReportID, getBankAccountRoute, getReportOrDraftReport, isProcessingReport, isReportOutstanding, isSelectedManagerMcTest} from '@libs/ReportUtils';
36+
import {canAddTransaction, generateReportID, getBankAccountRoute, getReportOrDraftReport, isSelectedManagerMcTest} from '@libs/ReportUtils';
3737
import {getDefaultTaxCode, getRateID, getRequestType, getValidWaypoints, isScanRequest} from '@libs/TransactionUtils';
3838
import ReceiptDropUI from '@pages/iou/ReceiptDropUI';
3939
import type {FileObject} from '@pages/media/AttachmentModalScreen/types';
@@ -126,8 +126,8 @@ function IOURequestStepConfirmation({
126126
* Also if the report was submitted and delayed submission is on, then we should use an initial report
127127
*/
128128
const transactionReport = getReportOrDraftReport(transaction?.reportID);
129-
const shouldUseTransactionReport =
130-
transactionReport && !(isProcessingReport(transactionReport) && !policyReal?.harvesting?.enabled) && isReportOutstanding(transactionReport, policyReal?.id);
129+
const [reportNameValuePair] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${transactionReport?.reportID}`, {canBeMissing: true});
130+
const shouldUseTransactionReport = transactionReport && canAddTransaction(transactionReport, !!reportNameValuePair?.private_isArchived);
131131
const report = shouldUseTransactionReport ? transactionReport : (reportReal ?? reportDraft);
132132
const policy = policyReal ?? policyDraft;
133133
const isDraftPolicy = policy === policyDraft;

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {useOnyx} from 'react-native-onyx';
33
import type {ListItem} from '@components/SelectionList/types';
44
import {changeTransactionsReport, setTransactionReport} from '@libs/actions/Transaction';
55
import Navigation from '@libs/Navigation/Navigation';
6+
import {isPolicyExpenseChat, isReportOutstanding} from '@libs/ReportUtils';
67
import CONST from '@src/CONST';
78
import ONYXKEYS from '@src/ONYXKEYS';
89
import type SCREENS from '@src/SCREENS';
@@ -21,8 +22,14 @@ type IOURequestStepReportProps = WithWritableReportOrNotFoundProps<typeof SCREEN
2122

2223
function IOURequestStepReport({route, transaction}: IOURequestStepReportProps) {
2324
const {backTo, action} = route.params;
24-
const reportID = transaction?.reportID === '0' ? transaction?.participants?.at(0)?.reportID : transaction?.reportID;
25-
const [transactionReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`, {canBeMissing: true});
25+
26+
const [allReports] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}`, {canBeMissing: false});
27+
const transactionReport = Object.values(allReports ?? {}).find((report) => report?.reportID === transaction?.reportID);
28+
const participantReportID = transaction?.participants?.at(0)?.reportID;
29+
const participantReport = Object.values(allReports ?? {}).find((report) => report?.reportID === participantReportID);
30+
const shouldUseTransactionReport = !!transactionReport && isReportOutstanding(transactionReport, transactionReport?.policyID);
31+
const outstandingReportID = isPolicyExpenseChat(participantReport) ? participantReport?.iouReportID : participantReportID;
32+
const selectedReportID = shouldUseTransactionReport ? transactionReport?.reportID : outstandingReportID;
2633

2734
const isEditing = action === CONST.IOU.ACTION.EDIT;
2835

@@ -46,7 +53,7 @@ function IOURequestStepReport({route, transaction}: IOURequestStepReportProps) {
4653
return (
4754
<IOURequestEditReportCommon
4855
backTo={backTo}
49-
transactionsReports={transactionReport ? [transactionReport] : []}
56+
selectedReportID={selectedReportID}
5057
selectReport={selectReport}
5158
/>
5259
);

0 commit comments

Comments
 (0)