Skip to content

Stop creating the transaction thread reports optimistically for RequestMoney #58825

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
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
1 change: 1 addition & 0 deletions src/CONST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1000,6 +1000,7 @@ const CONST = {
EMPTY_ARRAY,
EMPTY_OBJECT,
DEFAULT_NUMBER_ID,
FAKE_REPORT_ID: 'FAKE_REPORT_ID',
USE_EXPENSIFY_URL,
EXPENSIFY_URL,
GOOGLE_MEET_URL_ANDROID: 'https://meet.google.com',
Expand Down
6 changes: 5 additions & 1 deletion src/ROUTES.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ const ROUTES = {
REPORT: 'r',
REPORT_WITH_ID: {
route: 'r/:reportID?/:reportActionID?',
getRoute: (reportID: string | undefined, reportActionID?: string, referrer?: string, moneyRequestReportActionID?: string, transactionID?: string) => {
getRoute: (reportID: string | undefined, reportActionID?: string, referrer?: string, moneyRequestReportActionID?: string, transactionID?: string, iouReportID?: string) => {
if (!reportID) {
Log.warn('Invalid reportID is used to build the REPORT_WITH_ID route');
}
Expand All @@ -360,6 +360,10 @@ const ROUTES = {
queryParams.push(`transactionID=${transactionID}`);
}

if (iouReportID) {
queryParams.push(`iouReportID=${iouReportID}`);
}

const queryString = queryParams.length > 0 ? `?${queryParams.join('&')}` : '';

return `${baseRoute}${queryString}` as const;
Expand Down
2 changes: 1 addition & 1 deletion src/components/ReportActionItem/MoneyRequestAction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ function MoneyRequestAction({
const transactionID = isMoneyRequestAction(action) ? getOriginalMessage(action)?.IOUTransactionID : CONST.DEFAULT_NUMBER_ID;
if (!action?.childReportID && transactionID && action.reportActionID) {
const optimisticReportID = generateReportID();
Navigation.navigate(ROUTES.REPORT_WITH_ID.getRoute(optimisticReportID, undefined, undefined, action.reportActionID, transactionID));
Navigation.navigate(ROUTES.REPORT_WITH_ID.getRoute(optimisticReportID, undefined, undefined, action.reportActionID, transactionID, requestReportID));
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/libs/API/parameters/CreatePerDiemRequestParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type CreatePerDiemRequestParams = {
createdChatReportActionID?: string;
createdIOUReportActionID?: string;
reportPreviewReportActionID: string;
transactionThreadReportID: string;
transactionThreadReportID?: string;
createdReportActionIDForThread: string | undefined;
billable?: boolean;
};
Expand Down
4 changes: 2 additions & 2 deletions src/libs/API/parameters/RequestMoneyParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ type RequestMoneyParams = {
taxAmount: number;
billable?: boolean;
receiptGpsPoints?: string;
transactionThreadReportID: string;
createdReportActionIDForThread: string | undefined;
transactionThreadReportID?: string;
createdReportActionIDForThread?: string;
reimbursible?: boolean;
description?: string;
};
Expand Down
1 change: 1 addition & 0 deletions src/libs/Navigation/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1619,6 +1619,7 @@ type ReportsSplitNavigatorParamList = {
referrer?: string;
moneyRequestReportActionID?: string;
transactionID?: string;
iouReportID?: string;
};
};

Expand Down
5 changes: 2 additions & 3 deletions src/libs/ReportActionsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1175,7 +1175,6 @@ function getOneTransactionThreadReportID(
if (
actionType &&
iouRequestTypes.has(actionType) &&
action.childReportID &&
// Include deleted IOU reportActions if:
// - they have an assocaited IOU transaction ID or
// - they have visibile childActions (like comments) that we'd want to display
Expand Down Expand Up @@ -1203,8 +1202,8 @@ function getOneTransactionThreadReportID(
return;
}

// Ensure we have a childReportID associated with the IOU report action
return singleAction?.childReportID;
// Since we don't always create transaction thread optimistically, we return CONST.FAKE_REPORT_ID
return singleAction?.childReportID ?? CONST.FAKE_REPORT_ID;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need the constant? Can't we use null or "" or leave undefined which IIRC is what we said when we did not have a string ID?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this specific case, we need to return a truly value, so null, '', or undefined won't work.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does it need to be truthy?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To explain that, let's take a look at a couple of lines above of the getOneTransactionThreadReportID function and what it does.

We won't always have the childReportID inside singleAction now, cause we won't generate the transaction thread report right from the start.
But the app should still act with the report as with the one transaction thread report. Handle the navigation and UI in an appropriate way.
That's why in this case we return truly value.

@iwiznia what do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

/**
Expand Down
29 changes: 24 additions & 5 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ type OptimisticModifiedExpenseReportAction = Pick<
| 'delegateAccountID'
> & {reportID?: string};

type OptimisticMoneyRequestEntities = {
type BaseOptimisticMoneyRequestEntities = {
iouReport: Report;
type: ValueOf<typeof CONST.IOU.REPORT_ACTION_TYPE>;
amount: number;
Expand All @@ -578,6 +578,10 @@ type OptimisticMoneyRequestEntities = {
linkedTrackedExpenseReportAction?: ReportAction;
};

type OptimisticMoneyRequestEntities = BaseOptimisticMoneyRequestEntities & {shouldGenerateOptimisticTransactionThread?: boolean};
type OptimisticMoneyRequestEntitiesWithTransactionThreadFlag = BaseOptimisticMoneyRequestEntities & {shouldGenerateOptimisticTransactionThread: boolean};
type OptimisticMoneyRequestEntitiesWithoutTransactionThreadFlag = BaseOptimisticMoneyRequestEntities;

type OptimisticTaskReport = SetRequired<
Pick<
Report,
Expand Down Expand Up @@ -6982,6 +6986,7 @@ function buildTransactionThread(
reportAction: OnyxEntry<ReportAction | OptimisticIOUReportAction>,
moneyRequestReport: OnyxEntry<Report>,
existingTransactionThreadReportID?: string,
optimisticTransactionThreadReportID?: string,
): OptimisticChatReport {
const participantAccountIDs = [...new Set([currentUserAccountID, Number(reportAction?.actorAccountID)])].filter(Boolean) as number[];
const existingTransactionThreadReport = getReportOrDraftReport(existingTransactionThreadReportID);
Expand All @@ -7004,6 +7009,7 @@ function buildTransactionThread(
notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN,
parentReportActionID: reportAction?.reportActionID,
parentReportID: moneyRequestReport?.reportID,
optimisticReportID: optimisticTransactionThreadReportID,
});
}

Expand All @@ -7016,6 +7022,12 @@ function buildTransactionThread(
* 4. Transaction Thread linked to the IOU action via `parentReportActionID`
* 5. CREATED action for the Transaction Thread
*/
function buildOptimisticMoneyRequestEntities(
optimisticMoneyRequestEntities: OptimisticMoneyRequestEntitiesWithoutTransactionThreadFlag,
): [OptimisticCreatedReportAction, OptimisticCreatedReportAction, OptimisticIOUReportAction, OptimisticChatReport, OptimisticCreatedReportAction | null];
function buildOptimisticMoneyRequestEntities(
optimisticMoneyRequestEntities: OptimisticMoneyRequestEntitiesWithTransactionThreadFlag,
): [OptimisticCreatedReportAction, OptimisticCreatedReportAction, OptimisticIOUReportAction, OptimisticChatReport | undefined, OptimisticCreatedReportAction | null];
function buildOptimisticMoneyRequestEntities({
iouReport,
type,
Expand All @@ -7029,10 +7041,17 @@ function buildOptimisticMoneyRequestEntities({
isSettlingUp = false,
isSendMoneyFlow = false,
isOwnPolicyExpenseChat = false,
shouldGenerateOptimisticTransactionThread = true,
isPersonalTrackingExpense,
existingTransactionThreadReportID,
linkedTrackedExpenseReportAction,
}: OptimisticMoneyRequestEntities): [OptimisticCreatedReportAction, OptimisticCreatedReportAction, OptimisticIOUReportAction, OptimisticChatReport, OptimisticCreatedReportAction | null] {
}: OptimisticMoneyRequestEntities): [
OptimisticCreatedReportAction,
OptimisticCreatedReportAction,
OptimisticIOUReportAction,
OptimisticChatReport | undefined,
OptimisticCreatedReportAction | null,
] {
const createdActionForChat = buildOptimisticCreatedReportAction(payeeEmail);

// The `CREATED` action must be optimistically generated before the IOU action so that it won't appear after the IOU action in the chat.
Expand All @@ -7056,11 +7075,11 @@ function buildOptimisticMoneyRequestEntities({
});

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

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

return [createdActionForChat, createdActionForIOUReport, iouAction, transactionThread, createdActionForTransactionThread];
}
Expand Down
98 changes: 44 additions & 54 deletions src/libs/actions/IOU.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ type MoneyRequestInformation = {
createdChatReportActionID?: string;
createdIOUReportActionID?: string;
reportPreviewAction: OnyxTypes.ReportAction;
transactionThreadReportID: string;
transactionThreadReportID?: string;
createdReportActionIDForThread: string | undefined;
onyxData: OnyxData;
billable?: boolean;
Expand Down Expand Up @@ -397,6 +397,7 @@ type MoneyRequestInformationParams = {
existingTransactionID?: string;
existingTransaction?: OnyxEntry<OnyxTypes.Transaction>;
retryParams?: StartSplitBilActionParams | CreateTrackExpenseParams | RequestMoneyInformation | ReplaceReceipt;
shouldGenerateOptimisticTransactionThread?: boolean;
};

type MoneyRequestOptimisticParams = {
Expand All @@ -412,8 +413,8 @@ type MoneyRequestOptimisticParams = {
};
transactionParams: {
transaction: OnyxTypes.Transaction;
transactionThreadReport: OptimisticChatReport | null;
transactionThreadCreatedReportAction: OptimisticCreatedReportAction | null;
transactionThreadReport?: OptimisticChatReport | null;
transactionThreadCreatedReportAction?: OptimisticCreatedReportAction | null;
};
policyRecentlyUsed: {
categories?: string[];
Expand Down Expand Up @@ -2880,6 +2881,7 @@ function getMoneyRequestInformation(moneyRequestInformation: MoneyRequestInforma
existingTransactionID,
moneyRequestReportID = '',
retryParams,
shouldGenerateOptimisticTransactionThread = true,
} = moneyRequestInformation;
const {payeeAccountID = userAccountID, payeeEmail = currentUserEmail, participant} = participantParams;
const {policy, policyCategories, policyTagList} = policyParams;
Expand Down Expand Up @@ -2997,9 +2999,9 @@ function getMoneyRequestInformation(moneyRequestInformation: MoneyRequestInforma
participants: [participant],
transactionID: optimisticTransaction.transactionID,
paymentType: isSelectedManagerMcTest(participant.login) ? CONST.IOU.PAYMENT_TYPE.ELSEWHERE : undefined,

existingTransactionThreadReportID: linkedTrackedExpenseReportAction?.childReportID,
linkedTrackedExpenseReportAction,
shouldGenerateOptimisticTransactionThread,
});

let reportPreviewAction = shouldCreateNewMoneyRequestReport ? null : getReportPreviewAction(chatReport.reportID, iouReport.reportID);
Expand Down Expand Up @@ -4454,30 +4456,32 @@ const getConvertTrackedExpenseInformation = (
// Build modified expense report action with the transaction changes
const modifiedExpenseReportAction = buildOptimisticMovedTrackedExpenseModifiedReportAction(transactionThreadReportID, moneyRequestReportID);

optimisticData?.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${transactionThreadReportID}`,
value: {
[modifiedExpenseReportAction.reportActionID]: modifiedExpenseReportAction as OnyxTypes.ReportAction,
},
});
successData?.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${transactionThreadReportID}`,
value: {
[modifiedExpenseReportAction.reportActionID]: {pendingAction: null},
},
});
failureData?.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${transactionThreadReportID}`,
value: {
[modifiedExpenseReportAction.reportActionID]: {
...(modifiedExpenseReportAction as OnyxTypes.ReportAction),
errors: getMicroSecondOnyxErrorWithTranslationKey('iou.error.genericEditFailureMessage'),
if (transactionThreadReportID) {
optimisticData?.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${transactionThreadReportID}`,
value: {
[modifiedExpenseReportAction.reportActionID]: modifiedExpenseReportAction as OnyxTypes.ReportAction,
},
},
});
});
successData?.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${transactionThreadReportID}`,
value: {
[modifiedExpenseReportAction.reportActionID]: {pendingAction: null},
},
});
failureData?.push({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${transactionThreadReportID}`,
value: {
[modifiedExpenseReportAction.reportActionID]: {
...(modifiedExpenseReportAction as OnyxTypes.ReportAction),
errors: getMicroSecondOnyxErrorWithTranslationKey('iou.error.genericEditFailureMessage'),
},
},
});
}

return {optimisticData, successData, failureData, modifiedExpenseReportActionID: modifiedExpenseReportAction.reportActionID};
};
Expand Down Expand Up @@ -4526,7 +4530,7 @@ type ConvertTrackedExpenseToRequestParams = {
merchant: string;
created: string;
attendees?: Attendee[];
transactionThreadReportID: string;
transactionThreadReportID?: string;
};
chatParams: {
reportID: string;
Expand Down Expand Up @@ -4791,29 +4795,18 @@ function requestMoney(requestMoneyInformation: RequestMoneyInformation) {
},
};

const {
payerAccountID,
payerEmail,
iouReport,
chatReport,
transaction,
iouAction,
createdChatReportActionID,
createdIOUReportActionID,
reportPreviewAction,
transactionThreadReportID,
createdReportActionIDForThread,
onyxData,
} = getMoneyRequestInformation({
parentChatReport: isMovingTransactionFromTrackExpense ? undefined : currentChatReport,
participantParams,
policyParams,
transactionParams,
moneyRequestReportID,
existingTransactionID,
existingTransaction: isDistanceRequestTransactionUtils(existingTransaction) ? existingTransaction : undefined,
retryParams,
});
const {payerAccountID, payerEmail, iouReport, chatReport, transaction, iouAction, createdChatReportActionID, createdIOUReportActionID, reportPreviewAction, onyxData} =
getMoneyRequestInformation({
parentChatReport: isMovingTransactionFromTrackExpense ? undefined : currentChatReport,
participantParams,
policyParams,
transactionParams,
moneyRequestReportID,
existingTransactionID,
existingTransaction: isDistanceRequestTransactionUtils(existingTransaction) ? existingTransaction : undefined,
retryParams,
shouldGenerateOptimisticTransactionThread: false,
});
const activeReportID = isMoneyRequestReport ? report?.reportID : chatReport.reportID;

switch (action) {
Expand Down Expand Up @@ -4851,7 +4844,6 @@ function requestMoney(requestMoneyInformation: RequestMoneyInformation) {
actionableWhisperReportActionID,
linkedTrackedExpenseReportAction,
linkedTrackedExpenseReportID,
transactionThreadReportID,
},
chatParams: {
reportID: chatReport.reportID,
Expand Down Expand Up @@ -4893,8 +4885,6 @@ function requestMoney(requestMoneyInformation: RequestMoneyInformation) {
billable,
// This needs to be a string of JSON because of limitations with the fetch() API and nested objects
receiptGpsPoints: gpsPoints ? JSON.stringify(gpsPoints) : undefined,
transactionThreadReportID,
createdReportActionIDForThread,
reimbursible,
description: parsedComment,
};
Expand Down
Loading