Skip to content
Open
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
2 changes: 2 additions & 0 deletions src/hooks/useSelectedTransactionsActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,8 @@ function useSelectedTransactionsActions({
rules,
reportOwnerLogin,
reportManagerLogin,
// The paginated `reportActions` prop can miss the report's latest forward, so the hold check reads its combined history like the delete check
getCombinedReportActions(report?.reportID),
);

canHoldTransactions = canHoldTransactions && canHoldRequest;
Expand Down
2 changes: 1 addition & 1 deletion src/libs/ReportPrimaryActionUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ function getAllExpensesToHoldIfApplicable(
const transaction = transactionID ? reportTransactionsByID.get(transactionID) : undefined;
const holdReportAction = getReportAction(action?.childReportID, `${transaction?.comment?.hold ?? ''}`);
// Only canUnholdRequest is read here, and whether an expense can be unheld never depends on the report manager's login, so none is passed.
return canHoldUnholdReportAction(report, action, holdReportAction, transaction, policy, currentUserAccountID, rules, reportOwnerLogin, undefined).canUnholdRequest;
return canHoldUnholdReportAction(report, action, holdReportAction, transaction, policy, currentUserAccountID, rules, reportOwnerLogin, undefined, reportActions).canUnholdRequest;
});
}

Expand Down
22 changes: 19 additions & 3 deletions src/libs/ReportSecondaryActionUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ function isHoldAction(
}

const action = !!reportActions && getIOUActionForTransactionID(reportActions, transaction.transactionID);
return !!action && isHoldActionForTransaction(report, transaction, action, policy, currentUserAccountID, rules, reportOwnerLogin, reportManagerLogin);
return !!action && isHoldActionForTransaction(report, transaction, action, policy, currentUserAccountID, rules, reportOwnerLogin, reportManagerLogin, reportActions);
}

function isHoldActionForTransaction(
Expand All @@ -683,12 +683,25 @@ function isHoldActionForTransaction(
rules: OnyxCollection<Rule>,
reportOwnerLogin: string | undefined,
reportManagerLogin: string | undefined,
/** Actions of `report`, so a submitter can't hold once the report was forwarded past its first approver */
reportActions: OnyxEntry<ReportActions> | ReportAction[],
): boolean {
const isExpenseReport = isExpenseReportUtils(report);
const isIOUReport = isIOUReportUtils(report);
const iouOrExpenseReport = isExpenseReport || isIOUReport;
const holdReportAction = getReportAction(reportAction?.childReportID, `${reportTransaction?.comment?.hold ?? ''}`);
const {canHoldRequest} = canHoldUnholdReportAction(report, reportAction, holdReportAction, reportTransaction, policy, currentUserAccountID, rules, reportOwnerLogin, reportManagerLogin);
const {canHoldRequest} = canHoldUnholdReportAction(
report,
reportAction,
holdReportAction,
reportTransaction,
policy,
currentUserAccountID,
rules,
reportOwnerLogin,
reportManagerLogin,
reportActions,
);
const isActionOwner = isActionCreator(reportAction);

if (isOpenExpenseReport(report)) {
Expand Down Expand Up @@ -1383,7 +1396,10 @@ function getSecondaryTransactionThreadActions({
}): Array<ValueOf<typeof CONST.REPORT.TRANSACTION_SECONDARY_ACTIONS>> {
const options: Array<ValueOf<typeof CONST.REPORT.TRANSACTION_SECONDARY_ACTIONS>> = [];

if (!!reportAction && isHoldActionForTransaction(parentReport, reportTransaction, reportAction, policy, currentUserAccountID, rules, parentReportOwnerLogin, parentReportManagerLogin)) {
if (
!!reportAction &&
isHoldActionForTransaction(parentReport, reportTransaction, reportAction, policy, currentUserAccountID, rules, parentReportOwnerLogin, parentReportManagerLogin, parentReportActions)
) {
options.push(CONST.REPORT.TRANSACTION_SECONDARY_ACTIONS.HOLD);
}

Expand Down
11 changes: 9 additions & 2 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7008,6 +7008,8 @@ function canModifyHoldStatus(
rules: OnyxCollection<Rule>,
reportOwnerLogin: string | undefined,
reportManagerLogin: string | undefined,
/** Optional for the same reason as in canHoldUnholdReportAction, which is its only caller */
reportActions?: OnyxEntry<ReportActions> | ReportAction[],
): boolean {
if (!isMoneyRequestReport(report) || isTrackExpenseReport(report)) {
return false;
Expand All @@ -7025,7 +7027,7 @@ function canModifyHoldStatus(
}

if (isActionOwner && !isAdmin) {
return isAwaitingFirstLevelApproval(report, rules, reportOwnerLogin, reportManagerLogin);
return isAwaitingFirstLevelApproval(report, rules, reportOwnerLogin, reportManagerLogin, reportActions);
}

return (isAdmin || isManager) && isProcessingReport(report);
Expand All @@ -7041,6 +7043,11 @@ function canHoldUnholdReportAction(
rules: OnyxCollection<Rule>,
reportOwnerLogin: string | undefined,
reportManagerLogin: string | undefined,
/**
* Actions of `report`, read to tell whether it was forwarded since its last submit. Optional while Search rows
* (mapTransactionItemToSelectedEntry) leave it out and fall back to the module cache, see https://github.com/Expensify/App/issues/66419.
*/
reportActions?: OnyxEntry<ReportActions> | ReportAction[],
): {canHoldRequest: boolean; canUnholdRequest: boolean} {
// Some reported expenses have no IOU action. HoldRequest creates the missing action and transaction thread, so they can still be held.
// Unlike canEditFieldOfMoneyRequest, this check never looks up the expense's report and judges everything on the report passed in.
Expand All @@ -7063,7 +7070,7 @@ function canHoldUnholdReportAction(
const isOnHold = isOnHoldTransactionUtils(transaction);
const isClosed = isClosedReport(report);

const canModifyStatus = canModifyHoldStatus(report, reportAction, currentUserAccountID, isAdmin, rules, reportOwnerLogin, reportManagerLogin);
const canModifyStatus = canModifyHoldStatus(report, reportAction, currentUserAccountID, isAdmin, rules, reportOwnerLogin, reportManagerLogin, reportActions);
const canModifyUnholdStatus = !isTrackExpenseMoneyReport && (isAdmin || (isActionOwner && isHoldActionCreator) || isApprover);

const canHoldOrUnholdRequest = !isRequestSettled && !isApproved && !isClosed && !isDeletedParentAction(reportAction);
Expand Down
4 changes: 4 additions & 0 deletions src/pages/inbox/report/ContextMenu/ContextMenuActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,7 @@ const ContextMenuActions: ContextMenuAction[] = [
currentUserAccountID,
rules,
personalDetails,
moneyRequestReportActions,
}) => {
if (type !== CONST.CONTEXT_MENU_TYPES.REPORT_ACTION || !areHoldRequirementsMet) {
return false;
Expand All @@ -813,6 +814,7 @@ const ContextMenuActions: ContextMenuAction[] = [
getLoginByAccountID(moneyRequestReport?.ownerAccountID, personalDetails),
// Only canUnholdRequest is read here, and whether an expense can be unheld never depends on the report manager's login, so none is passed.
undefined,
moneyRequestReportActions,
).canUnholdRequest;
},
onPress: (
Expand Down Expand Up @@ -892,6 +894,7 @@ const ContextMenuActions: ContextMenuAction[] = [
currentUserAccountID,
rules,
personalDetails,
moneyRequestReportActions,
}) => {
if (type !== CONST.CONTEXT_MENU_TYPES.REPORT_ACTION || !areHoldRequirementsMet) {
return false;
Expand All @@ -906,6 +909,7 @@ const ContextMenuActions: ContextMenuAction[] = [
rules,
getLoginByAccountID(moneyRequestReport?.ownerAccountID, personalDetails),
getLoginByAccountID(moneyRequestReport?.managerID, personalDetails),
moneyRequestReportActions,
).canHoldRequest;
},
onPress: (
Expand Down
59 changes: 59 additions & 0 deletions tests/unit/ContextMenuActionsHoldTest.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// eslint-disable-next-line no-restricted-imports -- Namespace import is required to spy on ReportUtils without replacing the production module.
import * as ReportUtils from '@libs/ReportUtils';

import type * as ContextMenuActionsModule from '@pages/inbox/report/ContextMenu/ContextMenuActions';

import CONST from '@src/CONST';
Expand All @@ -23,6 +26,11 @@ if (!unholdAction) {
throw new Error('Unhold context menu action was not found');
}

const holdAction = ContextMenuActions.find((action) => 'textTranslateKey' in action && action.textTranslateKey === 'iou.hold');
if (!holdAction) {
throw new Error('Hold context menu action was not found');
}

const CURRENT_USER_ACCOUNT_ID = 1;
const OTHER_USER_ACCOUNT_ID = 2;

Expand Down Expand Up @@ -56,3 +64,54 @@ describe('ContextMenuActions unhold action', () => {
expect(shouldShowUnhold).toBe(expectedShouldShow);
});
});

describe('ContextMenuActions hold action', () => {
afterEach(() => {
jest.restoreAllMocks();
});

it('passes the money request report actions from the payload to the hold check', () => {
// Given an expense on a submitted report, whose report actions the context menu already loaded into the payload
const iouAction = createMock<ReportAction>({reportActionID: '14', actionName: CONST.REPORT.ACTIONS.TYPE.IOU, actorAccountID: CURRENT_USER_ACCOUNT_ID});
const forwardedAction = createMock<ReportAction>({reportActionID: '15', actionName: CONST.REPORT.ACTIONS.TYPE.FORWARDED});
const moneyRequestReportActions = {[iouAction.reportActionID]: iouAction, [forwardedAction.reportActionID]: forwardedAction};
const payload = createMock<Parameters<typeof holdAction.shouldShow>[0]>({
type: CONST.CONTEXT_MENU_TYPES.REPORT_ACTION,
areHoldRequirementsMet: true,
moneyRequestReport: {
reportID: '10',
type: CONST.REPORT.TYPE.EXPENSE,
ownerAccountID: CURRENT_USER_ACCOUNT_ID,
managerID: OTHER_USER_ACCOUNT_ID,
stateNum: CONST.REPORT.STATE_NUM.SUBMITTED,
statusNum: CONST.REPORT.STATUS_NUM.SUBMITTED,
},
moneyRequestAction: iouAction,
iouTransaction: {transactionID: '12', reportID: '10', comment: {}},
currentUserAccountID: CURRENT_USER_ACCOUNT_ID,
personalDetails: {
[CURRENT_USER_ACCOUNT_ID]: {accountID: CURRENT_USER_ACCOUNT_ID, login: 'submitter@test.com'},
[OTHER_USER_ACCOUNT_ID]: {accountID: OTHER_USER_ACCOUNT_ID, login: 'approver@test.com'},
},
moneyRequestReportActions,
});
const canHoldSpy = jest.spyOn(ReportUtils, 'canHoldUnholdReportAction');

// When the context menu decides whether to show Hold
holdAction.shouldShow(payload);

// Then the hold check reads whether the report was forwarded from those actions rather than from Onyx
expect(canHoldSpy).toHaveBeenCalledWith(
payload.moneyRequestReport,
payload.moneyRequestAction,
undefined,
payload.iouTransaction,
undefined,
CURRENT_USER_ACCOUNT_ID,
undefined,
'submitter@test.com',
'approver@test.com',
moneyRequestReportActions,
);
});
});
73 changes: 73 additions & 0 deletions tests/unit/ReportSecondaryActionUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4372,6 +4372,44 @@ describe('getSecondaryAction', () => {
expect(result.includes(CONST.REPORT.SECONDARY_ACTIONS.HOLD)).toBe(true);
});

it('passes the report actions to the hold check, so it reads the forward since the last submit from them', () => {
// Given a submitted expense report with one expense, and the report actions the caller already holds
const report = createMock<Report>({
reportID: REPORT_ID,
type: CONST.REPORT.TYPE.EXPENSE,
ownerAccountID: EMPLOYEE_ACCOUNT_ID,
stateNum: CONST.REPORT.STATE_NUM.SUBMITTED,
statusNum: CONST.REPORT.STATUS_NUM.SUBMITTED,
});
const transaction = createMock<Transaction>({transactionID: 'TRANSACTION_ID_R14932', comment: {}});
const policy = createMock<Policy>({});
const reportActions = [actionR14932];
const canHoldSpy = jest.spyOn(ReportUtils, 'canHoldUnholdReportAction').mockReturnValueOnce({canHoldRequest: true, canUnholdRequest: true});
jest.spyOn(ReportActionsUtils, 'getOneTransactionThreadReportID').mockReturnValueOnce(originalMessageR14932.IOUTransactionID);

// When the secondary actions are built
getSecondaryReportActions({
currentUserLogin: EMPLOYEE_EMAIL,
currentUserAccountID: EMPLOYEE_ACCOUNT_ID,
submitterLogin: EMPLOYEE_EMAIL,
managerLogin: MANAGER_EMAIL,
report,
chatReport,
reportTransactions: [transaction],
originalTransaction: createMock<Transaction>({}),
violations: {},
bankAccountList: {},
policy,
reportActions,
rules: undefined,
policyApprovalRules: undefined,
cardList: undefined,
});

// Then the hold check gets those same actions instead of reading the report's actions from Onyx
expect(canHoldSpy).toHaveBeenCalledWith(report, actionR14932, undefined, transaction, policy, EMPLOYEE_ACCOUNT_ID, undefined, EMPLOYEE_EMAIL, MANAGER_EMAIL, reportActions);
});

it('does not include HOLD option for submitter after first approval', () => {
const report = createMock<Report>({
reportID: REPORT_ID,
Expand Down Expand Up @@ -6445,6 +6483,41 @@ describe('getSecondaryTransactionThreadActions', () => {
expect(result.includes(CONST.REPORT.SECONDARY_ACTIONS.HOLD)).toBe(true);
});

it('passes the parent report actions to the hold check', () => {
// Given an expense on a submitted report, and the parent report's actions the transaction thread already holds
const report = createMock<Report>({
reportID: REPORT_ID,
type: CONST.REPORT.TYPE.EXPENSE,
ownerAccountID: EMPLOYEE_ACCOUNT_ID,
stateNum: CONST.REPORT.STATE_NUM.SUBMITTED,
statusNum: CONST.REPORT.STATUS_NUM.SUBMITTED,
});
const transaction = createMock<Transaction>({comment: {}});
const policy = createMock<Policy>({});
const parentReportActions = {[actionR14932.reportActionID]: actionR14932};
const canHoldSpy = jest.spyOn(ReportUtils, 'canHoldUnholdReportAction').mockReturnValueOnce({canHoldRequest: true, canUnholdRequest: true});

// When the transaction thread's secondary actions are built
getSecondaryTransactionThreadActions({
currentUserLogin: EMPLOYEE_EMAIL,
currentUserAccountID: EMPLOYEE_ACCOUNT_ID,
parentReport: report,
parentReportOwnerLogin: EMPLOYEE_EMAIL,
parentReportManagerLogin: MANAGER_EMAIL,
parentReportActions,
reportTransaction: transaction,
reportAction: actionR14932,
originalTransaction: createMock<Transaction>({}),
policy,
isChatReportArchived: false,
rules: undefined,
cardList: undefined,
});

// Then the hold check judges the parent report's forward from those actions
expect(canHoldSpy).toHaveBeenCalledWith(report, actionR14932, undefined, transaction, policy, EMPLOYEE_ACCOUNT_ID, undefined, EMPLOYEE_EMAIL, MANAGER_EMAIL, parentReportActions);
});

it('includes HOLD option for a non-admin expense creator while the parent report awaits its first approver', async () => {
// Given a submitted expense report whose first approver is its manager, a member whose personal details aren't stored
const TRANSACTION_ID = 'TRANSACTION_ID';
Expand Down
Loading
Loading