Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
262961a
use snapshot data
luacmartins Oct 10, 2025
b1e80ec
Merge branch 'main' into cmartins-useSaerchReportActions
luacmartins Oct 13, 2025
98df80f
fix ts
luacmartins Oct 13, 2025
b5a5d4c
resolve conflicts
luacmartins Oct 14, 2025
c92a707
update function signature
luacmartins Oct 14, 2025
67b07b5
gst
luacmartins Oct 14, 2025
f662310
refactor usages of canEditFieldOfMoneyRequest
luacmartins Oct 14, 2025
ceb3c92
refactor more code
luacmartins Oct 14, 2025
b8239d6
fix conditional
luacmartins Oct 14, 2025
ccb6872
update map
luacmartins Oct 14, 2025
9eb946d
revert chants
luacmartins Oct 14, 2025
5eab236
refacor data
luacmartins Oct 14, 2025
14347c7
rm searchreportaction
luacmartins Oct 14, 2025
52aa166
rm unused code
luacmartins Oct 14, 2025
8d5314a
fix prettier, eslint
luacmartins Oct 14, 2025
a51d9ed
fix tests
luacmartins Oct 14, 2025
521716e
fix eslint
luacmartins Oct 14, 2025
983d131
fix more tests
luacmartins Oct 14, 2025
897e082
fix more tests
luacmartins Oct 15, 2025
e196d55
rm const
luacmartins Oct 15, 2025
1fe0630
resolve conflicts
luacmartins Oct 15, 2025
2926684
resolve conflicts
luacmartins Oct 17, 2025
b68301f
fix test
luacmartins Oct 17, 2025
7b550d6
fix eslint
luacmartins Oct 17, 2025
e477f73
resolve conflicts
luacmartins Oct 20, 2025
1d9983c
resolve conflicts
luacmartins Oct 21, 2025
f354f08
resolve conflicts
luacmartins Oct 22, 2025
3b9ee76
resolve conflicts
luacmartins Oct 22, 2025
44d9762
resolve conflicts
luacmartins Oct 23, 2025
fe98197
resolve conflcits
luacmartins Oct 24, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ function MoneyRequestReceiptView({
}, [parentReportAction]);

const [transaction] = useOnyx(`${ONYXKEYS.COLLECTION.TRANSACTION}${getNonEmptyStringOnyxID(linkedTransactionID)}`, {canBeMissing: true});
const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${moneyRequestReport?.policyID}`, {canBeMissing: true});
const transactionViolations = useTransactionViolations(transaction?.transactionID);

const isDistanceRequest = isDistanceRequestTransactionUtils(transaction);
Expand All @@ -135,7 +136,7 @@ function MoneyRequestReceiptView({
// Used for non-restricted fields such as: description, category, tag, billable, etc...
const isReportArchived = useReportIsArchived(report?.reportID);
const isEditable = !!canUserPerformWriteActionReportUtils(report, isReportArchived) && !readonly;
const canEdit = isMoneyRequestAction(parentReportAction) && canEditMoneyRequest(parentReportAction, transaction, isChatReportArchived) && isEditable;
const canEdit = isMoneyRequestAction(parentReportAction) && canEditMoneyRequest(parentReportAction, isChatReportArchived, moneyRequestReport, policy, transaction) && isEditable;
Comment thread
luacmartins marked this conversation as resolved.
Resolved

const canEditReceipt = isEditable && canEditFieldOfMoneyRequest(parentReportAction, CONST.EDIT_REQUEST_FIELD.RECEIPT, undefined, isChatReportArchived);

Expand Down
2 changes: 1 addition & 1 deletion src/components/ReportActionItem/MoneyRequestView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ function MoneyRequestView({
// Used for non-restricted fields such as: description, category, tag, billable, etc...
const isReportArchived = useReportIsArchived(report?.reportID);
const isEditable = !!canUserPerformWriteActionReportUtils(report, isReportArchived) && !readonly;
const canEdit = isMoneyRequestAction(parentReportAction) && canEditMoneyRequest(parentReportAction, transaction, isChatReportArchived) && isEditable;
const canEdit = isMoneyRequestAction(parentReportAction) && canEditMoneyRequest(parentReportAction, isChatReportArchived, moneyRequestReport, policy, transaction) && isEditable;
Comment thread
luacmartins marked this conversation as resolved.
Resolved
const {isExpenseSplit} = getOriginalTransactionWithSplitInfo(transaction);
const isSplitAvailable = moneyRequestReport && transaction && isSplitAction(moneyRequestReport, [transaction], policy);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import Text from '@components/Text';
import TransactionPreviewSkeletonView from '@components/TransactionPreviewSkeletonView';
import useLocalize from '@hooks/useLocalize';
import useOnyx from '@hooks/useOnyx';
import useReportIsArchived from '@hooks/useReportIsArchived';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
import {convertToDisplayString} from '@libs/CurrencyUtils';
Expand Down Expand Up @@ -72,6 +73,7 @@ function TransactionPreviewContent({
const ownerAccountID = report?.ownerAccountID ?? reportPreviewAction?.childOwnerAccountID ?? CONST.DEFAULT_NUMBER_ID;
const isReportAPolicyExpenseChat = isPolicyExpenseChat(chatReport);
const [reportActions] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${getNonEmptyStringOnyxID(report?.reportID)}`, {canBeMissing: true});
const isChatReportArchived = useReportIsArchived(chatReport?.reportID);

const transactionPreviewCommonArguments = useMemo(
() => ({
Expand Down Expand Up @@ -99,7 +101,7 @@ function TransactionPreviewContent({

const firstViolation = violations.at(0);
const isIOUActionType = isMoneyRequestAction(action);
const canEdit = isIOUActionType && canEditMoneyRequest(action, transaction);
const canEdit = isIOUActionType && canEditMoneyRequest(action, isChatReportArchived, report, policy, transaction);
Comment thread
luacmartins marked this conversation as resolved.
Resolved

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.

Coming from #73516, the report passed to canEditMoneyRequest should be iouReport

const violationMessage = firstViolation ? ViolationsUtils.getViolationTranslation(firstViolation, translate, canEdit) : undefined;

const previewText = useMemo(
Expand Down
148 changes: 63 additions & 85 deletions src/components/Search/index.tsx

Large diffs are not rendered by default.

9 changes: 8 additions & 1 deletion src/components/SelectionListWithSections/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ import type UnreportedExpenseListItem from '@pages/UnreportedExpenseListItem';
import type CursorStyles from '@styles/utils/cursor/types';
import type {TransactionPreviewData} from '@userActions/Search';
import type CONST from '@src/CONST';
import type {PersonalDetailsList, Policy, Report, SearchResults, TransactionViolation, TransactionViolations} from '@src/types/onyx';
import type {PersonalDetailsList, Policy, Report, ReportAction, SearchResults, TransactionViolation, TransactionViolations} from '@src/types/onyx';
import type {Attendee, SplitExpense} from '@src/types/onyx/IOU';
import type {Errors, Icon, PendingAction} from '@src/types/onyx/OnyxCommon';
import type {
SearchCardGroup,
SearchMemberGroup,
SearchPersonalDetails,
SearchPolicy,
SearchReport,
SearchReportAction,
SearchTask,
Expand Down Expand Up @@ -239,6 +240,12 @@ type TransactionListItemType = ListItem &
/** Report to which the transaction belongs */
report: Report | undefined;

/** Policy to which the transaction belongs */
policy: SearchPolicy | undefined;

/** Report IOU action to which the transaction belongs */
reportAction: ReportAction | undefined;

/** The personal details of the user requesting money */
from: SearchPersonalDetails;

Expand Down
3 changes: 2 additions & 1 deletion src/hooks/useShowNotFoundPageInIOUStep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const useShowNotFoundPageInIOUStep = (action: IOUAction, iouType: IOUType, repor
const isSplitExpense = iouType === CONST.IOU.TYPE.SPLIT_EXPENSE;

const [session] = useOnyx(ONYXKEYS.SESSION, {canBeMissing: true});
const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${report?.policyID}`, {canBeMissing: true});

const reportActionsReportID = useMemo(() => {
let actionsReportID;
Expand Down Expand Up @@ -60,7 +61,7 @@ const useShowNotFoundPageInIOUStep = (action: IOUAction, iouType: IOUType, repor
} else if (isSplitExpense) {
shouldShowNotFoundPage = !canEditSplitExpense;
} else {
shouldShowNotFoundPage = !isMoneyRequestAction(reportAction) || !canEditMoneyRequest(reportAction);
shouldShowNotFoundPage = !isMoneyRequestAction(reportAction) || !canEditMoneyRequest(reportAction, false, report, policy, transaction);
Comment thread
luacmartins marked this conversation as resolved.
Resolved
}
}

Expand Down
30 changes: 17 additions & 13 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4323,8 +4323,10 @@ function getTransactionCommentObject(transaction: OnyxEntry<Transaction>): Comme
*/
function canEditMoneyRequest(
reportAction: OnyxInputOrEntry<ReportAction<typeof CONST.REPORT.ACTIONS.TYPE.IOU>>,
linkedTransaction?: OnyxEntry<Transaction>,
isChatReportArchived = false,
report?: OnyxInputOrEntry<Report> | SearchReport,
policy?: OnyxEntry<Policy> | SearchPolicy,
linkedTransaction?: OnyxEntry<Transaction> | SearchTransaction,
): boolean {
const isDeleted = isDeletedAction(reportAction);

Expand Down Expand Up @@ -4354,16 +4356,16 @@ function canEditMoneyRequest(
return actionType === CONST.IOU.REPORT_ACTION_TYPE.TRACK && isRequestor;
}

const moneyRequestReport = getReportOrDraftReport(String(moneyRequestReportID));
const moneyRequestReport = report ?? getReportOrDraftReport(String(moneyRequestReportID));

const isSubmitted = isProcessingReport(moneyRequestReport);
if (isIOUReport(moneyRequestReport)) {
return isSubmitted && isRequestor;
}
// This will be fixed as part of https://github.com/Expensify/Expensify/issues/507850
// eslint-disable-next-line @typescript-eslint/no-deprecated
const policy = getPolicy(moneyRequestReport?.policyID);
const isAdmin = policy?.role === CONST.POLICY.ROLE.ADMIN;
const reportPolicy = policy ?? getPolicy(moneyRequestReport?.policyID);
const isAdmin = reportPolicy?.role === CONST.POLICY.ROLE.ADMIN;
const isManager = currentUserAccountID === moneyRequestReport?.managerID;

if (isInvoiceReport(moneyRequestReport) && (isManager || isChatReportArchived)) {
Expand All @@ -4375,8 +4377,8 @@ function canEditMoneyRequest(
return true;
}

if (policy?.type === CONST.POLICY.TYPE.CORPORATE && moneyRequestReport && isSubmitted && isCurrentUserSubmitter(moneyRequestReport)) {
const isForwarded = getSubmitToAccountID(policy, moneyRequestReport) !== moneyRequestReport.managerID;
if (reportPolicy?.type === CONST.POLICY.TYPE.CORPORATE && moneyRequestReport && isSubmitted && isCurrentUserSubmitter(moneyRequestReport)) {
const isForwarded = getSubmitToAccountID(reportPolicy, moneyRequestReport) !== moneyRequestReport.managerID;
return !isForwarded;
}

Expand Down Expand Up @@ -4456,7 +4458,9 @@ function canEditFieldOfMoneyRequest(
isDeleteAction?: boolean,
isChatReportArchived = false,
outstandingReportsByPolicyID?: OutstandingReportsByPolicyIDDerivedValue,
canMoveExpense = true,
linkedTransaction?: OnyxEntry<Transaction> | SearchTransaction,
report?: OnyxInputOrEntry<Report> | SearchReport,
policy?: OnyxEntry<Policy> | SearchPolicy,
): boolean {
// A list of fields that cannot be edited by anyone, once an expense has been settled
const restrictedFields: string[] = [
Expand All @@ -4471,7 +4475,7 @@ function canEditFieldOfMoneyRequest(
CONST.EDIT_REQUEST_FIELD.REPORT,
];

if (!isMoneyRequestAction(reportAction) || !canEditMoneyRequest(reportAction, undefined, isChatReportArchived)) {
if (!isMoneyRequestAction(reportAction) || !canEditMoneyRequest(reportAction, isChatReportArchived, report, policy, linkedTransaction)) {
return false;
}

Expand All @@ -4481,8 +4485,8 @@ function canEditFieldOfMoneyRequest(
}

const iouMessage = getOriginalMessage(reportAction);
const moneyRequestReport = iouMessage?.IOUReportID ? (getReport(iouMessage?.IOUReportID, allReports) ?? ({} as Report)) : ({} as Report);
const transaction = allTransactions?.[`${ONYXKEYS.COLLECTION.TRANSACTION}${iouMessage?.IOUTransactionID}`] ?? ({} as Transaction);
const moneyRequestReport = report ?? (iouMessage?.IOUReportID ? (getReport(iouMessage?.IOUReportID, allReports) ?? ({} as Report)) : ({} as Report));
const transaction = linkedTransaction ?? allTransactions?.[`${ONYXKEYS.COLLECTION.TRANSACTION}${iouMessage?.IOUTransactionID}`] ?? ({} as Transaction);

if (isSettled(String(moneyRequestReport.reportID)) || isReportIDApproved(String(moneyRequestReport.reportID))) {
return false;
Expand All @@ -4501,8 +4505,8 @@ function canEditFieldOfMoneyRequest(

// This will be fixed as part of https://github.com/Expensify/Expensify/issues/507850
// eslint-disable-next-line @typescript-eslint/no-deprecated
const policy = getPolicy(moneyRequestReport?.policyID);
const isAdmin = isExpenseReport(moneyRequestReport) && policy?.role === CONST.POLICY.ROLE.ADMIN;
const reportPolicy = policy ?? getPolicy(moneyRequestReport?.policyID);
const isAdmin = isExpenseReport(moneyRequestReport) && reportPolicy?.role === CONST.POLICY.ROLE.ADMIN;
const isManager = isExpenseReport(moneyRequestReport) && currentUserAccountID === moneyRequestReport?.managerID;
const isRequestor = currentUserAccountID === reportAction?.actorAccountID;

Expand Down Expand Up @@ -4541,7 +4545,7 @@ function canEditFieldOfMoneyRequest(
// Unreported transaction from OldDot can have the reportID as an empty string
const isUnreportedExpense = !transaction?.reportID || transaction?.reportID === CONST.REPORT.UNREPORTED_REPORT_ID;

if (isUnreportedExpense && canMoveExpense) {
if (isUnreportedExpense) {
return true;
}

Expand Down
35 changes: 33 additions & 2 deletions src/libs/SearchUIUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -950,6 +950,31 @@ function getTransactionViolations(
return transactionViolations.filter((violation) => !isViolationDismissed(transaction, violation, currentUserEmail));
}

/**
* @private
* Creates an optimized lookup map for report actions indexed by transaction ID.
*/
function createReportActionsByTransactionIDMap(data: OnyxTypes.SearchResults['data']): Map<string, OnyxTypes.ReportAction> {
const reportActionsByTransactionID = new Map<string, OnyxTypes.ReportAction>();

for (const key of Object.keys(data)) {
if (isReportActionEntry(key)) {
const actions = Object.values(data[key]);
for (const action of actions) {
if (isMoneyRequestAction(action)) {
const originalMessage = getOriginalMessage<typeof CONST.REPORT.ACTIONS.TYPE.IOU>(action);
const transactionID = originalMessage?.IOUTransactionID;
if (transactionID) {
reportActionsByTransactionID.set(transactionID, action);
}
}
}
}
}

return reportActionsByTransactionID;
}

/**
* @private
* Organizes data into List Sections for display, for the TransactionListItemType of Search Results.
Expand All @@ -972,8 +997,9 @@ function getTransactionsSections(
// Get violations - optimize by using a Map for faster lookups
const allViolations = getViolations(data);

// Use Map for faster lookups of personal details
// Use Map for faster lookups of personal details and reportActions
const personalDetailsMap = new Map(Object.entries(data.personalDetailsList || {}));
const reportActionsByTransactionIDMap = createReportActionsByTransactionIDMap(data);

const transactionsSections: TransactionListItemType[] = [];

Expand Down Expand Up @@ -1010,14 +1036,16 @@ function getTransactionsSections(
const to = transactionItem.managerID && !shouldShowBlankTo ? (personalDetailsMap.get(transactionItem.managerID.toString()) ?? emptyPersonalDetails) : emptyPersonalDetails;

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

const reportAction = reportActionsByTransactionIDMap.get(transactionItem.transactionID);
const allActions = getActions(data, allViolations, key, currentSearch, currentAccountID, currentUserEmail);
const transactionSection: TransactionListItemType = {
...transactionItem,
keyForList: transactionItem.transactionID,
action: allActions.at(0) ?? CONST.SEARCH.ACTION_TYPES.VIEW,
allActions,
report,
policy,
reportAction,
from,
to,
formattedFrom,
Expand Down Expand Up @@ -1404,6 +1432,7 @@ function getReportSections(

const doesDataContainAPastYearTransaction = shouldShowYear(data);
const {shouldShowAmountInWideColumn, shouldShowTaxAmountInWideColumn} = getWideAmountIndicators(data);
const reportActionsByTransactionIDMap = createReportActionsByTransactionIDMap(data);

// Get violations - optimize by using a Map for faster lookups
const allViolations = getViolations(data);
Expand Down Expand Up @@ -1488,6 +1517,8 @@ function getReportSections(
action: allActions.at(0) ?? CONST.SEARCH.ACTION_TYPES.VIEW,
allActions,
report,
reportAction: reportActionsByTransactionIDMap.get(transactionItem.transactionID),
policy,
from,
to,
formattedFrom,
Expand Down
Loading