Skip to content

Commit 9c144e6

Browse files
committed
Revert "Merge pull request Expensify#62024 from Expensify/revert-60292-VickyStash/feature/58828-stop-creating-optimistic-transaction-RequestMoney"
This reverts commit ce886f9, reversing changes made to a4f66c7.
1 parent 11385b8 commit 9c144e6

File tree

16 files changed

+320
-190
lines changed

16 files changed

+320
-190
lines changed

src/CONST.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1041,6 +1041,7 @@ const CONST = {
10411041
EMPTY_ARRAY,
10421042
EMPTY_OBJECT,
10431043
DEFAULT_NUMBER_ID,
1044+
FAKE_REPORT_ID: 'FAKE_REPORT_ID',
10441045
USE_EXPENSIFY_URL,
10451046
EXPENSIFY_URL,
10461047
GOOGLE_MEET_URL_ANDROID: 'https://meet.google.com',

src/ROUTES.ts

+29-5
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,14 @@ const ROUTES = {
8383
backTo,
8484
moneyRequestReportActionID,
8585
transactionID,
86+
iouReportID,
8687
}: {
8788
reportID: string | undefined;
8889
reportActionID?: string;
8990
backTo?: string;
9091
moneyRequestReportActionID?: string;
9192
transactionID?: string;
93+
iouReportID?: string;
9294
}) => {
9395
if (!reportID) {
9496
Log.warn('Invalid reportID is used to build the SEARCH_REPORT route');
@@ -105,6 +107,10 @@ const ROUTES = {
105107
queryParams.push(`moneyRequestReportActionID=${moneyRequestReportActionID}`);
106108
}
107109

110+
if (iouReportID) {
111+
queryParams.push(`iouReportID=${iouReportID}`);
112+
}
113+
108114
const queryString = queryParams.length > 0 ? (`${baseRoute}?${queryParams.join('&')}` as const) : baseRoute;
109115
return getUrlWithBackToParam(queryString, backTo);
110116
},
@@ -376,10 +382,19 @@ const ROUTES = {
376382
REPORT: 'r',
377383
REPORT_WITH_ID: {
378384
route: 'r/:reportID?/:reportActionID?',
379-
getRoute: (reportID: string | undefined, reportActionID?: string, referrer?: string, moneyRequestReportActionID?: string, transactionID?: string, backTo?: string) => {
385+
getRoute: (
386+
reportID: string | undefined,
387+
reportActionID?: string,
388+
referrer?: string,
389+
moneyRequestReportActionID?: string,
390+
transactionID?: string,
391+
backTo?: string,
392+
iouReportID?: string,
393+
) => {
380394
if (!reportID) {
381395
Log.warn('Invalid reportID is used to build the REPORT_WITH_ID route');
382396
}
397+
383398
const baseRoute = reportActionID ? (`r/${reportID}/${reportActionID}` as const) : (`r/${reportID}` as const);
384399

385400
const queryParams: string[] = [];
@@ -393,6 +408,10 @@ const ROUTES = {
393408
queryParams.push(`transactionID=${transactionID}`);
394409
}
395410

411+
if (iouReportID) {
412+
queryParams.push(`iouReportID=${iouReportID}`);
413+
}
414+
396415
const queryString = queryParams.length > 0 ? `?${queryParams.join('&')}` : '';
397416

398417
return getUrlWithBackToParam(`${baseRoute}${queryString}` as const, backTo);
@@ -574,10 +593,15 @@ const ROUTES = {
574593
},
575594
MONEY_REQUEST_HOLD_REASON: {
576595
route: ':type/edit/reason/:transactionID?/:searchHash?',
577-
getRoute: (type: ValueOf<typeof CONST.POLICY.TYPE>, transactionID: string, reportID: string, backTo: string, searchHash?: number) => {
578-
const route = searchHash
579-
? (`${type as string}/edit/reason/${transactionID}/${searchHash}/?backTo=${backTo}&reportID=${reportID}` as const)
580-
: (`${type as string}/edit/reason/${transactionID}/?backTo=${backTo}&reportID=${reportID}` as const);
596+
getRoute: (type: ValueOf<typeof CONST.POLICY.TYPE>, transactionID: string, reportID: string | undefined, backTo: string, searchHash?: number) => {
597+
let route = searchHash
598+
? (`${type as string}/edit/reason/${transactionID}/${searchHash}/?backTo=${backTo}` as const)
599+
: (`${type as string}/edit/reason/${transactionID}/?backTo=${backTo}` as const);
600+
601+
if (reportID) {
602+
route = `${route}&reportID=${reportID}` as const;
603+
}
604+
581605
return route;
582606
},
583607
},

src/components/MoneyRequestReportView/MoneyRequestReportTransactionList.tsx

+20-7
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,17 @@ import {convertToDisplayString} from '@libs/CurrencyUtils';
2525
import {getThreadReportIDsForTransactions} from '@libs/MoneyRequestReportUtils';
2626
import {navigationRef} from '@libs/Navigation/Navigation';
2727
import {getIOUActionForTransactionID} from '@libs/ReportActionsUtils';
28-
import {getMoneyRequestSpendBreakdown} from '@libs/ReportUtils';
28+
import {generateReportID, getMoneyRequestSpendBreakdown} from '@libs/ReportUtils';
2929
import {compareValues} from '@libs/SearchUIUtils';
3030
import shouldShowTransactionYear from '@libs/TransactionUtils/shouldShowTransactionYear';
3131
import Navigation from '@navigation/Navigation';
32+
import type {ReportsSplitNavigatorParamList} from '@navigation/types';
3233
import variables from '@styles/variables';
3334
import CONST from '@src/CONST';
3435
import NAVIGATORS from '@src/NAVIGATORS';
36+
import type {Route} from '@src/ROUTES';
3537
import ROUTES from '@src/ROUTES';
38+
import type SCREENS from '@src/SCREENS';
3639
import type * as OnyxTypes from '@src/types/onyx';
3740
import {useMoneyRequestReportContext} from './MoneyRequestReportContext';
3841
import MoneyRequestReportTableHeader from './MoneyRequestReportTableHeader';
@@ -72,6 +75,8 @@ type SortedTransactions = {
7275
sortOrder: SortOrder;
7376
};
7477

78+
type ReportScreenNavigationProps = ReportsSplitNavigatorParamList[typeof SCREENS.REPORT];
79+
7580
const isSortableColumnName = (key: unknown): key is SortableColumnName => !!sortableColumnNames.find((val) => val === key);
7681

7782
const getTransactionKey = (transaction: OnyxTypes.Transaction, key: SortableColumnName) => {
@@ -150,19 +155,27 @@ function MoneyRequestReportTransactionList({report, transactions, reportActions,
150155
const navigateToTransaction = useCallback(
151156
(activeTransaction: OnyxTypes.Transaction) => {
152157
const iouAction = getIOUActionForTransactionID(reportActions, activeTransaction.transactionID);
153-
const reportIDToNavigate = iouAction?.childReportID;
154-
if (!reportIDToNavigate) {
155-
return;
156-
}
158+
const reportIDToNavigate = iouAction?.childReportID ?? generateReportID();
157159

158-
const backTo = Navigation.getActiveRoute();
160+
const backTo = Navigation.getActiveRoute() as Route;
159161

160162
// Single transaction report will open in RHP, and we need to find every other report ID for the rest of transactions
161163
// to display prev/next arrows in RHP for navigating between transactions
162164
const sortedSiblingTransactionReportIDs = getThreadReportIDsForTransactions(reportActions, sortedTransactions);
163165
setActiveTransactionThreadIDs(sortedSiblingTransactionReportIDs);
164166

165-
Navigation.navigate(ROUTES.SEARCH_REPORT.getRoute({reportID: reportIDToNavigate, backTo}));
167+
const routeParams = {
168+
reportID: reportIDToNavigate,
169+
backTo,
170+
} as ReportScreenNavigationProps;
171+
172+
if (!iouAction?.childReportID) {
173+
routeParams.moneyRequestReportActionID = iouAction?.reportActionID;
174+
routeParams.transactionID = activeTransaction.transactionID;
175+
routeParams.iouReportID = activeTransaction.reportID;
176+
}
177+
178+
Navigation.navigate(ROUTES.SEARCH_REPORT.getRoute(routeParams));
166179
},
167180
[reportActions, sortedTransactions],
168181
);

src/components/ReportActionItem/MoneyRequestAction.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ function MoneyRequestAction({
9898
const transactionID = isMoneyRequestAction(action) ? getOriginalMessage(action)?.IOUTransactionID : CONST.DEFAULT_NUMBER_ID;
9999
if (!action?.childReportID && transactionID && action.reportActionID) {
100100
const optimisticReportID = generateReportID();
101-
Navigation.navigate(ROUTES.REPORT_WITH_ID.getRoute(optimisticReportID, undefined, undefined, action.reportActionID, transactionID, Navigation.getActiveRoute()));
101+
Navigation.navigate(ROUTES.REPORT_WITH_ID.getRoute(optimisticReportID, undefined, undefined, action.reportActionID, transactionID, Navigation.getActiveRoute(), requestReportID));
102102
return;
103103
}
104104

src/libs/API/parameters/CreatePerDiemRequestParams.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type CreatePerDiemRequestParams = {
1717
createdChatReportActionID?: string;
1818
createdIOUReportActionID?: string;
1919
reportPreviewReportActionID: string;
20-
transactionThreadReportID: string;
20+
transactionThreadReportID?: string;
2121
createdReportActionIDForThread: string | undefined;
2222
billable?: boolean;
2323
attendees?: string;

src/libs/API/parameters/HoldMoneyRequestParams.ts

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ type HoldMoneyRequestParams = {
33
comment: string;
44
reportActionID: string;
55
commentReportActionID: string;
6+
transactionThreadReportID?: string;
7+
createdReportActionIDForThread?: string;
68
};
79

810
export default HoldMoneyRequestParams;

src/libs/API/parameters/MergeDuplicatesParams.ts

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ type MergeDuplicatesParams = {
1313
receiptID: number;
1414
reportID: string | undefined;
1515
reportActionID?: string | undefined;
16+
transactionThreadReportID?: string;
17+
createdReportActionIDForThread?: string;
1618
};
1719

1820
export default MergeDuplicatesParams;

src/libs/API/parameters/RequestMoneyParams.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ type RequestMoneyParams = {
2525
taxAmount: number;
2626
billable?: boolean;
2727
receiptGpsPoints?: string;
28-
transactionThreadReportID: string;
29-
createdReportActionIDForThread: string | undefined;
28+
transactionThreadReportID?: string;
29+
createdReportActionIDForThread?: string | undefined;
3030
reimbursible?: boolean;
3131
description?: string;
3232
attendees?: string;

src/libs/Navigation/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1644,6 +1644,7 @@ type ReportsSplitNavigatorParamList = {
16441644
backTo?: Routes;
16451645
moneyRequestReportActionID?: string;
16461646
transactionID?: string;
1647+
iouReportID?: string;
16471648
};
16481649
};
16491650

src/libs/ReportActionsUtils.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1238,7 +1238,6 @@ function getOneTransactionThreadReportID(
12381238
if (
12391239
actionType &&
12401240
iouRequestTypesSet.has(actionType) &&
1241-
action.childReportID &&
12421241
// Include deleted IOU reportActions if:
12431242
// - they have an assocaited IOU transaction ID or
12441243
// - they have visibile childActions (like comments) that we'd want to display
@@ -1266,8 +1265,8 @@ function getOneTransactionThreadReportID(
12661265
return;
12671266
}
12681267

1269-
// Ensure we have a childReportID associated with the IOU report action
1270-
return singleAction?.childReportID;
1268+
// Since we don't always create transaction thread optimistically, we return CONST.FAKE_REPORT_ID
1269+
return singleAction?.childReportID ?? CONST.FAKE_REPORT_ID;
12711270
}
12721271

12731272
/**

src/libs/ReportUtils.ts

+30-11
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@ type OptimisticModifiedExpenseReportAction = Pick<
611611
| 'delegateAccountID'
612612
> & {reportID?: string};
613613

614-
type OptimisticMoneyRequestEntities = {
614+
type BaseOptimisticMoneyRequestEntities = {
615615
iouReport: Report;
616616
type: ValueOf<typeof CONST.IOU.REPORT_ACTION_TYPE>;
617617
amount: number;
@@ -629,6 +629,10 @@ type OptimisticMoneyRequestEntities = {
629629
linkedTrackedExpenseReportAction?: ReportAction;
630630
};
631631

632+
type OptimisticMoneyRequestEntities = BaseOptimisticMoneyRequestEntities & {shouldGenerateOptimisticTransactionThread?: boolean};
633+
type OptimisticMoneyRequestEntitiesWithTransactionThreadFlag = BaseOptimisticMoneyRequestEntities & {shouldGenerateOptimisticTransactionThread: boolean};
634+
type OptimisticMoneyRequestEntitiesWithoutTransactionThreadFlag = BaseOptimisticMoneyRequestEntities;
635+
632636
type OptimisticTaskReport = SetRequired<
633637
Pick<
634638
Report,
@@ -3996,16 +4000,16 @@ const changeMoneyRequestHoldStatus = (reportAction: OnyxEntry<ReportAction>, sea
39964000

39974001
const transactionID = getOriginalMessage(reportAction)?.IOUTransactionID;
39984002

3999-
if (!transactionID || !reportAction.childReportID) {
4000-
Log.warn('Missing transactionID and reportAction.childReportID during the change of the money request hold status');
4003+
if (!transactionID) {
4004+
Log.warn('Missing transactionID during the change of the money request hold status');
40014005
return;
40024006
}
40034007

40044008
const transaction = allTransactions?.[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`] ?? ({} as Transaction);
40054009
const isOnHold = isOnHoldTransactionUtils(transaction);
40064010
const policy = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${moneyRequestReport.policyID}`] ?? null;
40074011

4008-
if (isOnHold) {
4012+
if (isOnHold && reportAction.childReportID) {
40094013
unholdRequest(transactionID, reportAction.childReportID, searchHash);
40104014
} else {
40114015
const activeRoute = encodeURIComponent(Navigation.getActiveRoute());
@@ -7346,6 +7350,7 @@ function buildTransactionThread(
73467350
reportAction: OnyxEntry<ReportAction | OptimisticIOUReportAction>,
73477351
moneyRequestReport: OnyxEntry<Report>,
73487352
existingTransactionThreadReportID?: string,
7353+
optimisticTransactionThreadReportID?: string,
73497354
): OptimisticChatReport {
73507355
const participantAccountIDs = [...new Set([currentUserAccountID, Number(reportAction?.actorAccountID)])].filter(Boolean) as number[];
73517356
const existingTransactionThreadReport = getReportOrDraftReport(existingTransactionThreadReportID);
@@ -7368,6 +7373,7 @@ function buildTransactionThread(
73687373
notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN,
73697374
parentReportActionID: reportAction?.reportActionID,
73707375
parentReportID: moneyRequestReport?.reportID,
7376+
optimisticReportID: optimisticTransactionThreadReportID,
73717377
});
73727378
}
73737379

@@ -7380,6 +7386,12 @@ function buildTransactionThread(
73807386
* 4. Transaction Thread linked to the IOU action via `parentReportActionID`
73817387
* 5. CREATED action for the Transaction Thread
73827388
*/
7389+
function buildOptimisticMoneyRequestEntities(
7390+
optimisticMoneyRequestEntities: OptimisticMoneyRequestEntitiesWithoutTransactionThreadFlag,
7391+
): [OptimisticCreatedReportAction, OptimisticCreatedReportAction, OptimisticIOUReportAction, OptimisticChatReport, OptimisticCreatedReportAction | null];
7392+
function buildOptimisticMoneyRequestEntities(
7393+
optimisticMoneyRequestEntities: OptimisticMoneyRequestEntitiesWithTransactionThreadFlag,
7394+
): [OptimisticCreatedReportAction, OptimisticCreatedReportAction, OptimisticIOUReportAction, OptimisticChatReport | undefined, OptimisticCreatedReportAction | null];
73837395
function buildOptimisticMoneyRequestEntities({
73847396
iouReport,
73857397
type,
@@ -7393,10 +7405,17 @@ function buildOptimisticMoneyRequestEntities({
73937405
isSettlingUp = false,
73947406
isSendMoneyFlow = false,
73957407
isOwnPolicyExpenseChat = false,
7408+
shouldGenerateOptimisticTransactionThread = true,
73967409
isPersonalTrackingExpense,
73977410
existingTransactionThreadReportID,
73987411
linkedTrackedExpenseReportAction,
7399-
}: OptimisticMoneyRequestEntities): [OptimisticCreatedReportAction, OptimisticCreatedReportAction, OptimisticIOUReportAction, OptimisticChatReport, OptimisticCreatedReportAction | null] {
7412+
}: OptimisticMoneyRequestEntities): [
7413+
OptimisticCreatedReportAction,
7414+
OptimisticCreatedReportAction,
7415+
OptimisticIOUReportAction,
7416+
OptimisticChatReport | undefined,
7417+
OptimisticCreatedReportAction | null,
7418+
] {
74007419
const createdActionForChat = buildOptimisticCreatedReportAction(payeeEmail);
74017420

74027421
// The `CREATED` action must be optimistically generated before the IOU action so that it won't appear after the IOU action in the chat.
@@ -7421,11 +7440,11 @@ function buildOptimisticMoneyRequestEntities({
74217440
});
74227441

74237442
// Create optimistic transactionThread and the `CREATED` action for it, if existingTransactionThreadReportID is undefined
7424-
const transactionThread = buildTransactionThread(iouAction, iouReport, existingTransactionThreadReportID);
7425-
const createdActionForTransactionThread = existingTransactionThreadReportID ? null : buildOptimisticCreatedReportAction(payeeEmail);
7443+
const transactionThread = shouldGenerateOptimisticTransactionThread ? buildTransactionThread(iouAction, iouReport, existingTransactionThreadReportID) : undefined;
7444+
const createdActionForTransactionThread = !!existingTransactionThreadReportID || !shouldGenerateOptimisticTransactionThread ? null : buildOptimisticCreatedReportAction(payeeEmail);
74267445

74277446
// The IOU action and the transactionThread are co-dependent as parent-child, so we need to link them together
7428-
iouAction.childReportID = existingTransactionThreadReportID ?? transactionThread.reportID;
7447+
iouAction.childReportID = existingTransactionThreadReportID ?? transactionThread?.reportID;
74297448

74307449
return [createdActionForChat, createdActionForIOUReport, iouAction, transactionThread, createdActionForTransactionThread];
74317450
}
@@ -9214,12 +9233,12 @@ function getAllAncestorReportActionIDs(report: Report | null | undefined, includ
92149233

92159234
/**
92169235
* Get optimistic data of parent report action
9217-
* @param reportID The reportID of the report that is updated
9236+
* @param reportOrID The reportID of the report that is updated or the optimistic report on its own
92189237
* @param lastVisibleActionCreated Last visible action created of the child report
92199238
* @param type The type of action in the child report
92209239
*/
9221-
function getOptimisticDataForParentReportAction(reportID: string | undefined, lastVisibleActionCreated: string, type: string): Array<OnyxUpdate | null> {
9222-
const report = getReportOrDraftReport(reportID);
9240+
function getOptimisticDataForParentReportAction(reportOrID: Report | string | undefined, lastVisibleActionCreated: string, type: string): Array<OnyxUpdate | null> {
9241+
const report = typeof reportOrID === 'string' ? getReportOrDraftReport(reportOrID) : reportOrID;
92239242

92249243
if (!report || isEmptyObject(report)) {
92259244
return [];

0 commit comments

Comments
 (0)