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
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ function useConfirmApproval(reportID: string | undefined, startApprovedAnimation
approveMoneyRequest({
expenseReport: moneyRequestReport,
expenseReportPolicy: policy,
policy,
currentUserAccountIDParam: accountID,
currentUserEmailParam: email ?? '',
hasViolations,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails'
import useLocalize from '@hooks/useLocalize';
import useOnyx from '@hooks/useOnyx';
import usePermissions from '@hooks/usePermissions';
import usePolicy from '@hooks/usePolicy';
import useTransactionsAndViolationsForReport from '@hooks/useTransactionsAndViolationsForReport';
import {hasHeldExpensesFromTransactions as hasHeldExpensesReportUtils, hasViolations as hasViolationsReportUtils} from '@libs/ReportUtils';
import {approveMoneyRequest} from '@userActions/IOU/ReportWorkflow';
Expand All @@ -30,8 +29,6 @@ function ApproveActionButton({iouReportID, startApprovedAnimation, onHoldMenuOpe
const {isDelegateAccessRestricted} = useDelegateNoAccessState();
const {showDelegateNoAccessModal} = useDelegateNoAccessActions();

const [activePolicyID] = useOnyx(ONYXKEYS.NVP_ACTIVE_POLICY_ID);
const activePolicy = usePolicy(activePolicyID);
const [iouReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${iouReportID}`);
const [expenseReportPolicy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${iouReport?.policyID}`);
const [userBillingGracePeriodEnds] = useOnyx(ONYXKEYS.COLLECTION.SHARED_NVP_PRIVATE_USER_BILLING_GRACE_PERIOD_END);
Expand Down Expand Up @@ -59,7 +56,6 @@ function ApproveActionButton({iouReportID, startApprovedAnimation, onHoldMenuOpe
approveMoneyRequest({
expenseReport: iouReport,
expenseReportPolicy,
policy: activePolicy,
currentUserAccountIDParam: currentUserAccountID,
currentUserEmailParam: currentUserEmail,
hasViolations,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ function PayActionButton({
approveMoneyRequest({
expenseReport: iouReport,
expenseReportPolicy: policy,
policy: activePolicy,
currentUserAccountIDParam: currentUserAccountID,
currentUserEmailParam: currentUserEmail,
hasViolations,
Expand Down
1 change: 0 additions & 1 deletion src/components/SettlementButton/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,6 @@ function SettlementButton({
approveMoneyRequest({
expenseReport: iouReport,
expenseReportPolicy,
policy,
currentUserAccountIDParam: accountID,
currentUserEmailParam: email ?? '',
hasViolations,
Expand Down
1 change: 0 additions & 1 deletion src/hooks/useHoldMenuSubmit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ function useHoldMenuSubmit({moneyRequestReport, chatReport, requestType, payment
if (isApprove) {
approveMoneyRequest({
expenseReport: currentMoneyRequestReport,
policy: activePolicy,
currentUserAccountIDParam: currentUserDetails.accountID,
currentUserEmailParam: currentUserDetails.email ?? '',
hasViolations,
Expand Down
1 change: 0 additions & 1 deletion src/hooks/useLifecycleActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ function useLifecycleActions({reportID, startApprovedAnimation, startAnimation,
approveMoneyRequest({
expenseReport: moneyRequestReport,
expenseReportPolicy: policy,
policy,
currentUserAccountIDParam: accountID,
currentUserEmailParam: email ?? '',
hasViolations,
Expand Down
1 change: 0 additions & 1 deletion src/hooks/useSelectionModeReportActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,6 @@ function useSelectionModeReportActions({
} else {
approveMoneyRequest({
expenseReport: report,
policy,
currentUserAccountIDParam: currentUserAccountID,
currentUserEmailParam: currentUserEmail ?? '',
hasViolations,
Expand Down
1 change: 0 additions & 1 deletion src/libs/PaymentUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,6 @@ const selectPaymentType = (params: SelectPaymentTypeParams) => {
approveMoneyRequest({
expenseReport: iouReport,
expenseReportPolicy,
policy,
currentUserAccountIDParam: currentAccountID,
currentUserEmailParam: currentEmail,
hasViolations,
Expand Down
21 changes: 8 additions & 13 deletions src/libs/actions/IOU/ReportWorkflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ import {getReportFromHoldRequestsOnyxData} from './Hold';
type ApproveMoneyRequestFunctionParams = {
expenseReport: OnyxEntry<OnyxTypes.Report>;
expenseReportPolicy: OnyxEntry<OnyxTypes.Policy>;
policy: OnyxEntry<OnyxTypes.Policy>;
currentUserAccountIDParam: number;
currentUserEmailParam: string;
hasViolations: boolean;
Expand Down Expand Up @@ -384,7 +383,6 @@ function getReportOriginalCreationTimestamp(expenseReport?: OnyxEntry<OnyxTypes.
function approveMoneyRequest(params: ApproveMoneyRequestFunctionParams) {
const {
expenseReport,
policy,
currentUserAccountIDParam,
currentUserEmailParam,
hasViolations,
Expand All @@ -404,14 +402,11 @@ function approveMoneyRequest(params: ApproveMoneyRequestFunctionParams) {
}

// If this is a Submit workspace, approving requires upgrading first.
// IMPORTANT: gate by the workspace that owns the expense report (`expenseReportPolicy`) instead of `policy`,
// since some call sites pass `policy` as the active/personal policy rather than the report's workspace policy.
const policyToUpgrade = expenseReportPolicy ?? policy;
if (isSubmitPolicy(policyToUpgrade) && policyToUpgrade?.id) {
if (isSubmitPolicy(expenseReportPolicy) && expenseReportPolicy?.id) {
const upgradeFeatureAlias = CONST.UPGRADE_FEATURE_INTRO_MAPPING.approvalSubmitReport.alias;
const backTo = Navigation.getActiveRoute() || ROUTES.REPORT_WITH_ID.getRoute(expenseReport.reportID);

Navigation.navigate(ROUTES.WORKSPACE_UPGRADE.getRoute(policyToUpgrade.id, upgradeFeatureAlias, backTo, expenseReport.reportID));
Navigation.navigate(ROUTES.WORKSPACE_UPGRADE.getRoute(expenseReportPolicy.id, upgradeFeatureAlias, backTo, expenseReport.reportID));
return;
}

Expand All @@ -425,13 +420,13 @@ function approveMoneyRequest(params: ApproveMoneyRequestFunctionParams) {
const hasHeldExpenses = hasHeldExpensesReportUtils(reportTransactions);
// TODO: https://github.com/Expensify/App/issues/66512
// eslint-disable-next-line @typescript-eslint/no-deprecated
const hasDuplicates = hasDuplicateTransactions(currentUserEmailParam, currentUserAccountIDParam, expenseReport, policy, getAllTransactionViolations());
const hasDuplicates = hasDuplicateTransactions(currentUserEmailParam, currentUserAccountIDParam, expenseReport, expenseReportPolicy, getAllTransactionViolations());
if (hasHeldExpenses && !full && !!expenseReport.unheldTotal) {
total = expenseReport.unheldTotal;
}
const optimisticApprovedReportAction = buildOptimisticApprovedReportAction(total, expenseReport.currency ?? '', expenseReport.reportID, currentUserAccountIDParam, delegateEmail);

const isDEWPolicy = hasDynamicExternalWorkflow(policy);
const isDEWPolicy = hasDynamicExternalWorkflow(expenseReportPolicy);
const shouldAddOptimisticApproveAction = !isDEWPolicy || getIsOffline();

const nextApproverAccountID = getNextApproverAccountID(expenseReport);
Expand All @@ -444,7 +439,7 @@ function approveMoneyRequest(params: ApproveMoneyRequestFunctionParams) {
? null
: buildNextStepNew({
report: expenseReport,
policy,
policy: expenseReportPolicy,
currentUserAccountIDParam,
currentUserEmailParam,
hasViolations,
Expand All @@ -455,7 +450,7 @@ function approveMoneyRequest(params: ApproveMoneyRequestFunctionParams) {
? null
: buildOptimisticNextStep({
report: expenseReport,
policy,
policy: expenseReportPolicy,
currentUserAccountIDParam,
currentUserEmailParam,
hasViolations,
Expand Down Expand Up @@ -699,7 +694,7 @@ function approveMoneyRequest(params: ApproveMoneyRequestFunctionParams) {
chatReport,
iouReport: expenseReport,
recipient: {accountID: expenseReport.ownerAccountID},
policy,
policy: expenseReportPolicy,
createdTimestamp: originalCreated,
isApprovalFlow: true,
betas,
Expand All @@ -723,7 +718,7 @@ function approveMoneyRequest(params: ApproveMoneyRequestFunctionParams) {
currentUserEmailParam,
currentUserAccountIDParam,
expenseReport,
policy,
expenseReportPolicy,
// TODO: https://github.com/Expensify/App/issues/66512
// eslint-disable-next-line @typescript-eslint/no-deprecated
getAllTransactionViolations()?.[ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS + transaction.transactionID],
Expand Down
113 changes: 75 additions & 38 deletions tests/actions/IOUTest/ReportWorkflowTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import initOnyxDerivedValues from '@libs/actions/OnyxDerived';
import {createWorkspace, deleteWorkspace, generatePolicyID, setWorkspaceApprovalMode} from '@libs/actions/Policy/Policy';
import {submitMoneyRequestOnSearch} from '@libs/actions/Search';
import Navigation from '@libs/Navigation/Navigation';
import getReportPreviewAction from '@libs/ReportPreviewActionUtils';
import CONST from '@src/CONST';
import IntlStore from '@src/languages/IntlStore';
import OnyxUpdateManager from '@src/libs/actions/OnyxUpdateManager';
Expand Down Expand Up @@ -2019,11 +2020,14 @@ describe('actions/IOU/ReportWorkflow', () => {
currency: CONST.CURRENCY.USD,
statusNum: CONST.REPORT.STATUS_NUM.SUBMITTED,
};
const expenseReportPolicy = {
...createRandomPolicy(Number(expenseReport.policyID), CONST.POLICY.TYPE.TEAM),
approvalMode: CONST.POLICY.APPROVAL_MODE.BASIC,
};

approveMoneyRequest({
expenseReport,
expenseReportPolicy: createRandomPolicy(Number(expenseReport.policyID), CONST.POLICY.TYPE.TEAM),
policy: {} as Policy,
expenseReportPolicy,
currentUserAccountIDParam: CARLOS_ACCOUNT_ID,
currentUserEmailParam: CARLOS_EMAIL,
hasViolations: false,
Expand Down Expand Up @@ -2056,11 +2060,14 @@ describe('actions/IOU/ReportWorkflow', () => {
currency: CONST.CURRENCY.USD,
statusNum: CONST.REPORT.STATUS_NUM.SUBMITTED,
};
const expenseReportPolicy = {
...createRandomPolicy(Number(expenseReport.policyID), CONST.POLICY.TYPE.TEAM),
approvalMode: CONST.POLICY.APPROVAL_MODE.BASIC,
};

approveMoneyRequest({
expenseReport,
expenseReportPolicy: createRandomPolicy(Number(expenseReport.policyID), CONST.POLICY.TYPE.TEAM),
policy: {} as Policy,
expenseReportPolicy,
currentUserAccountIDParam: CARLOS_ACCOUNT_ID,
currentUserEmailParam: CARLOS_EMAIL,
hasViolations: false,
Expand Down Expand Up @@ -2564,10 +2571,9 @@ describe('actions/IOU/ReportWorkflow', () => {
policyID,
});

const createApproveMoneyRequestParams = (expenseReport: Report, expenseReportPolicy: Policy, policy: Policy = teamPolicy) => ({
const createApproveMoneyRequestParams = (expenseReport: Report, expenseReportPolicy: OnyxEntry<Policy>) => ({
expenseReport,
expenseReportPolicy,
policy,
currentUserAccountIDParam: CARLOS_ACCOUNT_ID,
currentUserEmailParam: CARLOS_EMAIL,
hasViolations: false,
Expand Down Expand Up @@ -2595,30 +2601,6 @@ describe('actions/IOU/ReportWorkflow', () => {
expect(Navigation.navigate).toHaveBeenCalledWith(expectedRoute);
});

it('gates upgrade by expenseReportPolicy instead of the policy param', () => {
const expenseReport = createSubmittedExpenseReport();
const expectedRoute = ROUTES.WORKSPACE_UPGRADE.getRoute(submitPolicyID, upgradeFeatureAlias, ROUTES.REPORT_WITH_ID.getRoute(expenseReport.reportID), expenseReport.reportID);

approveMoneyRequest(createApproveMoneyRequestParams(expenseReport, submitPolicy, teamPolicy));

expect(Navigation.navigate).toHaveBeenCalledTimes(1);
expect(Navigation.navigate).toHaveBeenCalledWith(expectedRoute);
});

it('falls back to policy when expenseReportPolicy is undefined', () => {
const expenseReport = createSubmittedExpenseReport(submitPolicyID);
const expectedRoute = ROUTES.WORKSPACE_UPGRADE.getRoute(submitPolicyID, upgradeFeatureAlias, ROUTES.REPORT_WITH_ID.getRoute(expenseReport.reportID), expenseReport.reportID);

approveMoneyRequest({
...createApproveMoneyRequestParams(expenseReport, submitPolicy),
expenseReportPolicy: undefined,
policy: submitPolicy,
});

expect(Navigation.navigate).toHaveBeenCalledTimes(1);
expect(Navigation.navigate).toHaveBeenCalledWith(expectedRoute);
});

it('uses Navigation.getActiveRoute as backTo when available', () => {
const expenseReport = createSubmittedExpenseReport();
const activeRoute = `search?q=reportID%3A${expenseReport.reportID}`;
Expand Down Expand Up @@ -2778,7 +2760,6 @@ describe('actions/IOU/ReportWorkflow', () => {
approveMoneyRequest({
expenseReport,
expenseReportPolicy: policy,
policy,
currentUserAccountIDParam: adminAccountID,
currentUserEmailParam: adminEmail,
hasViolations: false,
Expand Down Expand Up @@ -2830,7 +2811,6 @@ describe('actions/IOU/ReportWorkflow', () => {
approveMoneyRequest({
expenseReport,
expenseReportPolicy: policy,
policy,
currentUserAccountIDParam: managerAccountID,
currentUserEmailParam: managerEmail,
hasViolations: false,
Expand Down Expand Up @@ -2878,7 +2858,6 @@ describe('actions/IOU/ReportWorkflow', () => {
approveMoneyRequest({
expenseReport,
expenseReportPolicy: policy,
policy,
currentUserAccountIDParam: adminAccountID,
currentUserEmailParam: adminEmail,
hasViolations: false,
Expand Down Expand Up @@ -3002,7 +2981,6 @@ describe('actions/IOU/ReportWorkflow', () => {
approveMoneyRequest({
expenseReport,
expenseReportPolicy: policy,
policy,
currentUserAccountIDParam: managerAccountID,
currentUserEmailParam: managerEmail,
hasViolations: false,
Expand Down Expand Up @@ -3033,7 +3011,6 @@ describe('actions/IOU/ReportWorkflow', () => {
approveMoneyRequest({
expenseReport,
expenseReportPolicy: policy,
policy,
currentUserAccountIDParam: managerAccountID,
currentUserEmailParam: managerEmail,
hasViolations: false,
Expand Down Expand Up @@ -3062,7 +3039,6 @@ describe('actions/IOU/ReportWorkflow', () => {
approveMoneyRequest({
expenseReport: updatedReport,
expenseReportPolicy: policy,
policy,
currentUserAccountIDParam: adminAccountID,
currentUserEmailParam: adminEmail,
hasViolations: false,
Expand Down Expand Up @@ -3121,7 +3097,6 @@ describe('actions/IOU/ReportWorkflow', () => {
approveMoneyRequest({
expenseReport: singleApproverReport,
expenseReportPolicy: singleApproverPolicy,
policy: singleApproverPolicy,
currentUserAccountIDParam: managerAccountID,
currentUserEmailParam: managerEmail,
hasViolations: false,
Expand All @@ -3140,6 +3115,69 @@ describe('actions/IOU/ReportWorkflow', () => {
expect(updatedReport?.stateNum).toBe(CONST.REPORT.STATE_NUM.APPROVED);
expect(updatedReport?.statusNum).toBe(CONST.REPORT.STATUS_NUM.APPROVED);
});

it('uses the expense report policy (not the activePolicy) to determine if it is a DEW approval', () => {
const activeDEWPolicy: Policy = {
...policy,
id: '2',
approvalMode: CONST.POLICY.APPROVAL_MODE.DYNAMICEXTERNAL,
};
const normalReport: Report = {
...expenseReport,
reportID: '789',
managerID: managerAccountID,
};
const transaction = {
...createRandomTransaction(1),
reportID: normalReport.reportID,
};

return Onyx.multiSet({
[ONYXKEYS.SESSION]: {
email: managerEmail,
accountID: managerAccountID,
},
[ONYXKEYS.NVP_ACTIVE_POLICY_ID]: activeDEWPolicy.id,
[`${ONYXKEYS.COLLECTION.POLICY}${activeDEWPolicy.id}`]: activeDEWPolicy,
[`${ONYXKEYS.COLLECTION.REPORT}${normalReport.reportID}`]: normalReport,
[`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`]: transaction,
} as unknown as OnyxMultiSetInput)
.then(() => {
approveMoneyRequest({
expenseReport: normalReport,
expenseReportPolicy: policy,
currentUserAccountIDParam: managerAccountID,
currentUserEmailParam: managerEmail,
hasViolations: false,
isASAPSubmitBetaEnabled: false,
expenseReportCurrentNextStepDeprecated: undefined,
betas: [CONST.BETAS.ALL],
userBillingGracePeriodEnds: undefined,
amountOwed: 0,
ownerBillingGracePeriodEnd: undefined,
delegateEmail: undefined,
});
return waitForBatchedUpdates();
})
.then(() => getOnyxValue(`${ONYXKEYS.COLLECTION.REPORT}${normalReport.reportID}`))
.then((updatedReport) => {
expect(updatedReport?.stateNum).toBe(CONST.REPORT.STATE_NUM.SUBMITTED);
expect(updatedReport?.statusNum).toBe(CONST.REPORT.STATUS_NUM.SUBMITTED);
expect(updatedReport?.managerID).toBe(adminAccountID);

const previewAction = getReportPreviewAction({
isReportArchived: false,
currentUserAccountID: managerAccountID,
currentUserLogin: managerEmail,
report: updatedReport,
policy,
transactions: [transaction],
bankAccountList: {},
reportMetadata: undefined,
});
expect(previewAction).not.toBe(CONST.REPORT.REPORT_PREVIEW_ACTIONS.APPROVE);
});
});
});

describe('approveMoneyRequest partially', () => {
Expand Down Expand Up @@ -3236,7 +3274,6 @@ describe('actions/IOU/ReportWorkflow', () => {
const newExpenseReportID = approveMoneyRequest({
expenseReport,
expenseReportPolicy: policy,
policy,
currentUserAccountIDParam: adminAccountID,
currentUserEmailParam: adminEmail,
hasViolations: false,
Expand Down
Loading
Loading