Skip to content
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
16 changes: 14 additions & 2 deletions src/components/Search/SearchContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,23 @@ function SearchContextProvider({children}: ChildrenProps) {
if (data.length && data.every(isTransactionReportGroupListItemType)) {
selectedReports = data
.filter((item) => isMoneyRequestReport(item) && item.transactions.length > 0 && item.transactions.every(({keyForList}) => selectedTransactions[keyForList]?.isSelected))
.map(({reportID, action = CONST.SEARCH.ACTION_TYPES.VIEW, total = CONST.DEFAULT_NUMBER_ID, policyID}) => ({reportID, action, total, policyID}));
.map(({reportID, action = CONST.SEARCH.ACTION_TYPES.VIEW, total = CONST.DEFAULT_NUMBER_ID, policyID, allActions = [action]}) => ({
reportID,
action,
total,
policyID,
allActions,
}));
} else if (data.length && data.every(isTransactionListItemType)) {
selectedReports = data
.filter(({keyForList}) => !!keyForList && selectedTransactions[keyForList]?.isSelected)
.map(({reportID, action = CONST.SEARCH.ACTION_TYPES.VIEW, amount: total = CONST.DEFAULT_NUMBER_ID, policyID}) => ({reportID, action, total, policyID}));
.map(({reportID, action = CONST.SEARCH.ACTION_TYPES.VIEW, amount: total = CONST.DEFAULT_NUMBER_ID, policyID, allActions = [action]}) => ({
reportID,
action,
total,
policyID,
allActions,
}));
}

setSearchContextData((prevState) => ({
Expand Down
1 change: 1 addition & 0 deletions src/components/Search/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type SelectedReports = {
reportID: string;
policyID: string | undefined;
action: ValueOf<typeof CONST.SEARCH.ACTION_TYPES>;
allActions: Array<ValueOf<typeof CONST.SEARCH.ACTION_TYPES>>;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

BTW I don't think this was the correct solution. We should not have added this key to Onyx

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

SelectedReports is not an Onyx data. It's just a derived value.

total: number;
};

Expand Down
54 changes: 31 additions & 23 deletions src/libs/SearchUIUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -884,8 +884,10 @@ function getTransactionsSections(

const {formattedFrom, formattedTo, formattedTotal, formattedMerchant, date} = getTransactionItemCommonFormattedProperties(transactionItem, from, to, policy, formatPhoneNumber);

const allActions = getActions(data, allViolations, key, currentSearch, currentAccountID);
const transactionSection: TransactionListItemType = {
action: getAction(data, allViolations, key, currentSearch, currentAccountID),
action: allActions.at(0) ?? CONST.SEARCH.ACTION_TYPES.VIEW,
allActions,
report,
from,
to,
Expand Down Expand Up @@ -1024,52 +1026,53 @@ function getReviewerPermissionFlags(
*
* Do not use directly, use only via `getSections()` facade.
*/
function getAction(
function getActions(
data: OnyxTypes.SearchResults['data'],
allViolations: OnyxCollection<OnyxTypes.TransactionViolation[]>,
key: string,
currentSearch: SearchKey,
currentAccountID: number | undefined,
reportActions: OnyxTypes.ReportAction[] = [],
): SearchTransactionAction {
): SearchTransactionAction[] {
const isTransaction = isTransactionEntry(key);
const report = getReportFromKey(data, key);

if (currentSearch === CONST.SEARCH.SEARCH_KEYS.EXPORT) {
return CONST.SEARCH.ACTION_TYPES.EXPORT_TO_ACCOUNTING;
return [CONST.SEARCH.ACTION_TYPES.EXPORT_TO_ACCOUNTING];
}

if (!isTransaction && !isReportEntry(key)) {
return CONST.SEARCH.ACTION_TYPES.VIEW;
return [CONST.SEARCH.ACTION_TYPES.VIEW];
}

const transaction = isTransaction ? data[key] : undefined;
if (isUnreportedAndHasInvalidDistanceRateTransaction(transaction)) {
return CONST.SEARCH.ACTION_TYPES.REVIEW;
return [CONST.SEARCH.ACTION_TYPES.REVIEW];
}
// Tracked and unreported expenses don't have a report, so we return early.
if (!report) {
return CONST.SEARCH.ACTION_TYPES.VIEW;
return [CONST.SEARCH.ACTION_TYPES.VIEW];
}

const policy = getPolicyFromKey(data, report) as OnyxTypes.Policy;
const isExportAvailable = isExportAction(report, policy, reportActions) && !isTransaction;

if (isSettled(report) && !isExportAvailable) {
return CONST.SEARCH.ACTION_TYPES.PAID;
return [CONST.SEARCH.ACTION_TYPES.PAID];
}

// We need to check both options for a falsy value since the transaction might not have an error but the report associated with it might. We return early if there are any errors for performance reasons, so we don't need to compute any other possible actions.
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
if (transaction?.errors || report?.errors) {
return CONST.SEARCH.ACTION_TYPES.REVIEW;
return [CONST.SEARCH.ACTION_TYPES.REVIEW];
}

// We don't need to run the logic if this is not a transaction or iou/expense report, so let's shortcut the logic for performance reasons
if (!isMoneyRequestReport(report) && !isInvoiceReport(report)) {
return CONST.SEARCH.ACTION_TYPES.VIEW;
return [CONST.SEARCH.ACTION_TYPES.VIEW];
}

const allActions: SearchTransactionAction[] = [];
let allReportTransactions: SearchTransaction[];
if (isReportEntry(key)) {
allReportTransactions = getTransactionsForReport(data, report.reportID);
Expand All @@ -1088,14 +1091,15 @@ function getAction(
// Only check for violations if we need to (when user has permission to review)
if ((isSubmitter || isApprover || isAdmin) && hasAnyViolations(report.reportID, allViolations, allReportTransactions)) {
if (isSubmitter && !isApprover && !isAdmin && !canReview(report, allViolations, isIOUReportArchived || isChatReportArchived, policy, allReportTransactions)) {
return CONST.SEARCH.ACTION_TYPES.VIEW;
allActions.push(CONST.SEARCH.ACTION_TYPES.VIEW);
} else {
allActions.push(CONST.SEARCH.ACTION_TYPES.REVIEW);
}
return CONST.SEARCH.ACTION_TYPES.REVIEW;
}
// Submit/Approve/Pay can only be taken on transactions if the transaction is the only one on the report, otherwise `View` is the only option.
// If this condition is not met, return early for performance reasons
if (isTransaction && !transaction?.isFromOneTransactionReport) {
return CONST.SEARCH.ACTION_TYPES.VIEW;
return allActions.length > 0 ? allActions : [CONST.SEARCH.ACTION_TYPES.VIEW];
}

const invoiceReceiverPolicy: SearchPolicy | undefined =
Expand All @@ -1107,15 +1111,15 @@ function getAction(
const canBePaid = canIOUBePaid(report, chatReport, policy, allReportTransactions, false, chatReportRNVP, invoiceReceiverPolicy);

if (canBePaid && !hasOnlyHeldExpenses(report.reportID, allReportTransactions)) {
return CONST.SEARCH.ACTION_TYPES.PAY;
allActions.push(CONST.SEARCH.ACTION_TYPES.PAY);
}

if (isExportAvailable) {
return CONST.SEARCH.ACTION_TYPES.EXPORT_TO_ACCOUNTING;
allActions.push(CONST.SEARCH.ACTION_TYPES.EXPORT_TO_ACCOUNTING);
}

if (isClosedReport(report)) {
return CONST.SEARCH.ACTION_TYPES.DONE;
return allActions.length > 0 ? allActions : [CONST.SEARCH.ACTION_TYPES.DONE];
}

const hasOnlyPendingCardOrScanningTransactions = allReportTransactions.length > 0 && allReportTransactions.every(isPendingCardOrScanningTransaction);
Expand All @@ -1127,19 +1131,19 @@ function getAction(
!hasOnlyPendingCardOrScanningTransactions &&
!hasOnlyHeldExpenses(report.reportID, allReportTransactions)
) {
return CONST.SEARCH.ACTION_TYPES.APPROVE;
allActions.push(CONST.SEARCH.ACTION_TYPES.APPROVE);
}

// We check for isAllowedToApproveExpenseReport because if the policy has preventSelfApprovals enabled, we disable the Submit action and in that case we want to show the View action instead
if (canSubmitReport(report, policy, allReportTransactions, allViolations, isIOUReportArchived || isChatReportArchived) && isAllowedToApproveExpenseReport) {
return CONST.SEARCH.ACTION_TYPES.SUBMIT;
allActions.push(CONST.SEARCH.ACTION_TYPES.SUBMIT);
}

if (reportNVP?.exportFailedTime) {
return CONST.SEARCH.ACTION_TYPES.REVIEW;
return allActions.length > 0 ? allActions : [CONST.SEARCH.ACTION_TYPES.REVIEW];
}

return CONST.SEARCH.ACTION_TYPES.VIEW;
return allActions.length > 0 ? allActions : [CONST.SEARCH.ACTION_TYPES.VIEW];
}

/**
Expand Down Expand Up @@ -1320,9 +1324,11 @@ function getReportSections(
if (shouldShow) {
const reportPendingAction = reportItem?.pendingAction ?? reportItem?.pendingFields?.preview;
const shouldShowBlankTo = !reportItem || isOpenExpenseReport(reportItem);
const allActions = getActions(data, allViolations, key, currentSearch, currentAccountID, actions);
reportIDToTransactions[reportKey] = {
...reportItem,
action: getAction(data, allViolations, key, currentSearch, currentAccountID, actions),
action: allActions.at(0) ?? CONST.SEARCH.ACTION_TYPES.VIEW,
allActions,
groupedBy: CONST.SEARCH.GROUP_BY.REPORTS,
keyForList: reportItem.reportID,
from: transactions.length > 0 ? data.personalDetailsList[data?.[reportKey as ReportKey]?.accountID ?? CONST.DEFAULT_NUMBER_ID] : emptyPersonalDetails,
Expand All @@ -1349,9 +1355,11 @@ function getReportSections(

const {formattedFrom, formattedTo, formattedTotal, formattedMerchant, date} = getTransactionItemCommonFormattedProperties(transactionItem, from, to, policy, formatPhoneNumber);

const allActions = getActions(data, allViolations, key, currentSearch, currentAccountID, actions);
const transaction = {
...transactionItem,
action: getAction(data, allViolations, key, currentSearch, currentAccountID, actions),
action: allActions.at(0) ?? CONST.SEARCH.ACTION_TYPES.VIEW,
allActions,
report,
from,
to,
Expand Down Expand Up @@ -2040,7 +2048,7 @@ export {
isCorrectSearchUserName,
isReportActionEntry,
isTaskListItemType,
getAction,
getActions,
createTypeMenuSections,
createBaseSavedSearchMenuItem,
shouldShowEmptyState,
Expand Down
6 changes: 4 additions & 2 deletions src/pages/Search/SearchPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ function SearchPage({route}: SearchPageProps) {
!isOffline &&
!isAnyTransactionOnHold &&
(selectedReports.length
? selectedReports.every((report) => report.action === CONST.SEARCH.ACTION_TYPES.APPROVE)
? selectedReports.every((report) => report.allActions.includes(CONST.SEARCH.ACTION_TYPES.APPROVE))
: selectedTransactionsKeys.every((id) => selectedTransactions[id].action === CONST.SEARCH.ACTION_TYPES.APPROVE));

if (shouldShowApproveOption) {
Expand Down Expand Up @@ -333,7 +333,9 @@ function SearchPage({route}: SearchPageProps) {
!isOffline &&
!isAnyTransactionOnHold &&
(selectedReports.length
? selectedReports.every((report) => report.action === CONST.SEARCH.ACTION_TYPES.PAY && report.policyID && getLastPolicyPaymentMethod(report.policyID, lastPaymentMethods))
? selectedReports.every(
(report) => report.allActions.includes(CONST.SEARCH.ACTION_TYPES.PAY) && report.policyID && getLastPolicyPaymentMethod(report.policyID, lastPaymentMethods),
)
: selectedTransactionsKeys.every(
(id) =>
selectedTransactions[id].action === CONST.SEARCH.ACTION_TYPES.PAY &&
Expand Down
10 changes: 8 additions & 2 deletions src/types/onyx/SearchResults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,12 @@ type SearchReport = {
/** The date the report was created */
created?: string;

/** The action that can be performed for the report */
/** The main action that can be performed for the report */
action?: SearchTransactionAction;

/** The available actions that can be performed for the report */
allActions?: SearchTransactionAction[];

/** The type of chat if this is a chat report */
chatType?: ValueOf<typeof CONST.REPORT.CHAT_TYPE>;

Expand Down Expand Up @@ -403,9 +406,12 @@ type SearchTransaction = {
/** The report ID of the transaction thread associated with the transaction */
transactionThreadReportID: string;

/** The action that can be performed for the transaction */
/** The main action that can be performed for the transaction */
action: SearchTransactionAction;

/** The available actions that can be performed for the transaction */
allActions: SearchTransactionAction[];

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

Expand Down
Loading