Skip to content
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

Create transaction thread if needed #43070

Merged
merged 14 commits into from
Jun 7, 2024
22 changes: 14 additions & 8 deletions src/components/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import useThemeStyles from '@hooks/useThemeStyles';
import * as SearchActions from '@libs/actions/Search';
import * as DeviceCapabilities from '@libs/DeviceCapabilities';
import Log from '@libs/Log';
import * as ReportUtils from '@libs/ReportUtils';
import * as SearchUtils from '@libs/SearchUtils';
import type {SearchColumnType, SortOrder} from '@libs/SearchUtils';
import Navigation from '@navigation/Navigation';
Expand All @@ -34,9 +35,9 @@ type SearchProps = {

const sortableSearchTabs: SearchQuery[] = [CONST.TAB_SEARCH.ALL];

function isReportListItemType(item: TransactionListItemType | ReportListItemType): item is ReportListItemType {
const reportListItem = item as ReportListItemType;
return reportListItem.transactions !== undefined;
function isTransactionListItemType(item: TransactionListItemType | ReportListItemType): item is TransactionListItemType {
const transactionListItem = item as TransactionListItemType;
return transactionListItem.transactionID !== undefined;
}

function Search({query, policyIDs, sortBy, sortOrder}: SearchProps) {
Expand Down Expand Up @@ -76,11 +77,19 @@ function Search({query, policyIDs, sortBy, sortOrder}: SearchProps) {
return <EmptySearchView />;
}

const openReport = (reportID?: string) => {
const openReport = (item: TransactionListItemType | ReportListItemType) => {
let reportID = isTransactionListItemType(item) ? item.transactionThreadReportID : item.reportID;

if (!reportID) {
return;
}

// If we're trying to open a legacy transaction without a transaction thread, let's create the thread and navigate the user
if (isTransactionListItemType(item) && reportID === '0' && item.moneyRequestReportActionID) {
reportID = ReportUtils.generateReportID();
SearchActions.createTransactionThread(hash, item.transactionID, reportID, item.moneyRequestReportActionID);
}

Navigation.navigate(ROUTES.SEARCH_REPORT.getRoute(query, reportID));
};

Expand Down Expand Up @@ -137,10 +146,7 @@ function Search({query, policyIDs, sortBy, sortOrder}: SearchProps) {
updateCellsBatchingPeriod={200}
ListItem={ListItem}
sections={[{data: sortedData, isDisabled: false}]}
onSelectRow={(item) => {
const reportID = isReportListItemType(item) ? item.reportID : item.transactionThreadReportID;
openReport(reportID);
}}
onSelectRow={(item) => openReport(item)}
shouldDebounceRowSelect
shouldPreventDefaultFocusOnSelectRow={!DeviceCapabilities.canUseTouchScreen()}
listHeaderWrapperStyle={[styles.ph9, styles.pv3, styles.pb5]}
Expand Down
34 changes: 30 additions & 4 deletions src/libs/actions/Search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ import * as API from '@libs/API';
import type {SearchParams} from '@libs/API/parameters';
import {READ_COMMANDS} from '@libs/API/types';
import ONYXKEYS from '@src/ONYXKEYS';
import type {SearchTransaction} from '@src/types/onyx/SearchResults';
import * as Report from './Report';

let currentUserEmail: string;
Onyx.connect({
key: ONYXKEYS.SESSION,
callback: (val) => {
currentUserEmail = val?.email ?? '';
},
});

function search({hash, query, policyIDs, offset, sortBy, sortOrder}: SearchParams) {
const optimisticData: OnyxUpdate[] = [
Expand Down Expand Up @@ -32,7 +42,23 @@ function search({hash, query, policyIDs, offset, sortBy, sortOrder}: SearchParam

API.read(READ_COMMANDS.SEARCH, {hash, query, offset, policyIDs, sortBy, sortOrder}, {optimisticData, finallyData});
}
export {
// eslint-disable-next-line import/prefer-default-export
search,
};

/**
* It's possible that we return legacy transactions that don't have a transaction thread created yet.
* In that case, when users select the search result row, we need to create the transaction thread on the fly and update the search result with the new transactionThreadReport
*/
function createTransactionThread(hash: number, transactionID: string, reportID: string, moneyRequestReportActionID: string) {
Report.openReport(reportID, '', [currentUserEmail], {}, moneyRequestReportActionID);

const onyxUpdate: Record<string, Record<string, Partial<SearchTransaction>>> = {
data: {
[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`]: {
transactionThreadReportID: reportID,
},
},
};

Onyx.merge(`${ONYXKEYS.COLLECTION.SNAPSHOT}${hash}`, onyxUpdate);
}

export {search, createTransactionThread};
3 changes: 3 additions & 0 deletions src/types/onyx/SearchResults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ type SearchTransaction = {

/** The modified MCC Group associated with the transaction */
modifiedMCCGroup?: ValueOf<typeof CONST.MCC_GROUPS>;

/** The ID of the money request reportAction associated with the transaction */
moneyRequestReportActionID?: string;
};

type SearchAccountDetails = Partial<SearchPolicyDetails & SearchPersonalDetails>;
Expand Down
Loading