diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index a6aeaf8d74cb..f686049a5154 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -186,6 +186,7 @@ import { getReportActionMessageText, getReportActionText, getRetractedMessage, + getSortedReportActions, getTravelUpdateMessage, getWorkspaceCurrencyUpdateMessage, getWorkspaceFrequencyUpdateMessage, @@ -4321,6 +4322,13 @@ function getNextApproverAccountID(report: OnyxEntry, isUnapproved = fals // eslint-disable-next-line @typescript-eslint/no-deprecated const policy = getPolicy(report?.policyID); + // If the current user took control, then they are the final approver and we don't have a next approver + // If someone else took control or rerouted, they are the next approver + const bypassApprover = getBypassApproverIfTakenControl(report); + if (bypassApprover) { + return bypassApprover === currentUserAccountID && !isUnapproved ? undefined : bypassApprover; + } + const approvalChain = getApprovalChain(policy, report); const submitToAccountID = getSubmitToAccountID(policy, report); @@ -4336,9 +4344,12 @@ function getNextApproverAccountID(report: OnyxEntry, isUnapproved = fals return submitToAccountID; } - const nextApproverEmail = approvalChain.length === 1 ? approvalChain.at(0) : approvalChain.at(approvalChain.indexOf(currentUserEmail ?? '') + 1); + const currentUserIndex = approvalChain.indexOf(currentUserEmail ?? ''); + const nextApproverEmail = currentUserIndex === -1 ? approvalChain.at(0) : approvalChain.at(currentUserIndex + 1); + if (!nextApproverEmail) { - return submitToAccountID; + // If there's no next approver in the chain, return undefined to indicate the current user is the final approver + return undefined; } return getAccountIDsByLogins([nextApproverEmail]).at(0); @@ -11468,6 +11479,42 @@ function isWorkspaceEligibleForReportChange(submitterEmail: string | undefined, return isPaidGroupPolicyPolicyUtils(newPolicy) && !!newPolicy.role; } +/** + * Checks if someone took control of the report and if that take control is still valid + * A take control is invalidated if there's a SUBMITTED action after it + */ +function getBypassApproverIfTakenControl(expenseReport: OnyxEntry): number | null { + if (!expenseReport?.reportID) { + return null; + } + + if (!isProcessingReport(expenseReport)) { + return null; + } + + const reportActions = getAllReportActions(expenseReport.reportID); + if (!reportActions) { + return null; + } + + // Sort actions by created timestamp to get chronological order + const sortedActions = getSortedReportActions(Object.values(reportActions ?? {}), true); + + // Look through actions in reverse chronological order (newest first) + // If we find a SUBMITTED action, there's no valid take control since any take control would be older + for (const action of sortedActions) { + if (isActionOfType(action, CONST.REPORT.ACTIONS.TYPE.SUBMITTED)) { + return null; + } + + if (isActionOfType(action, CONST.REPORT.ACTIONS.TYPE.TAKE_CONTROL)) { + return action.actorAccountID ?? null; + } + } + + return null; +} + function getApprovalChain(policy: OnyxEntry, expenseReport: OnyxEntry): string[] { const approvalChain: string[] = []; const fullApprovalChain: string[] = []; diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 0e755df46844..8eb053f5d99d 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -172,6 +172,7 @@ import { hasOutstandingChildRequest, hasReportBeenReopened, hasReportBeenRetracted, + hasViolations as hasViolationsReportUtils, isArchivedReport, isClosedReport as isClosedReportUtil, isDraftReport, @@ -10104,13 +10105,6 @@ function getIOUReportActionToApproveOrPay(chatReport: OnyxEntry, full?: boolean) { if (!expenseReport) { return; @@ -10132,15 +10126,23 @@ function approveMoneyRequest(expenseReport: OnyxEntry, full?: // This will be fixed as part of https://github.com/Expensify/Expensify/issues/507850 // eslint-disable-next-line @typescript-eslint/no-deprecated - const approvalChain = getApprovalChain(getPolicy(expenseReport.policyID), expenseReport); + const policy = getPolicy(expenseReport.policyID); + const nextApproverAccountID = getNextApproverAccountID(expenseReport); + const predictedNextStatus = !nextApproverAccountID ? CONST.REPORT.STATUS_NUM.APPROVED : CONST.REPORT.STATUS_NUM.SUBMITTED; + const predictedNextState = !nextApproverAccountID ? CONST.REPORT.STATE_NUM.APPROVED : CONST.REPORT.STATE_NUM.SUBMITTED; + const managerID = !nextApproverAccountID ? expenseReport.managerID : nextApproverAccountID; - const predictedNextStatus = isLastApprover(approvalChain) ? CONST.REPORT.STATUS_NUM.APPROVED : CONST.REPORT.STATUS_NUM.SUBMITTED; - const predictedNextState = isLastApprover(approvalChain) ? CONST.REPORT.STATE_NUM.APPROVED : CONST.REPORT.STATE_NUM.SUBMITTED; - const managerID = isLastApprover(approvalChain) ? expenseReport.managerID : getNextApproverAccountID(expenseReport); - - // TODO: Replace onyx.connect with useOnyx hook (https://github.com/Expensify/App/issues/66365) - // eslint-disable-next-line @typescript-eslint/no-deprecated - const optimisticNextStep = buildNextStep(expenseReport, predictedNextStatus); + const hasViolations = hasViolationsReportUtils(expenseReport.reportID, allTransactionViolations); + const isASAPSubmitBetaEnabled = Permissions.isBetaEnabled(CONST.BETAS.ASAP_SUBMIT, allBetas); + const optimisticNextStep = buildNextStepNew({ + report: expenseReport, + policy, + currentUserAccountIDParam: userAccountID, + currentUserEmailParam: currentUserEmail, + hasViolations, + isASAPSubmitBetaEnabled, + predictedNextStatus, + }); const chatReport = getReportOrDraftReport(expenseReport.chatReportID); const optimisticReportActionsData: OnyxUpdate = { diff --git a/tests/actions/IOUTest.ts b/tests/actions/IOUTest.ts index d00f3fbffcee..d7e811bdf9ef 100644 --- a/tests/actions/IOUTest.ts +++ b/tests/actions/IOUTest.ts @@ -12,6 +12,7 @@ import useReportWithTransactionsAndViolations from '@hooks/useReportWithTransact import type {PerDiemExpenseTransactionParams, RequestMoneyParticipantParams} from '@libs/actions/IOU'; import { addSplitExpenseField, + approveMoneyRequest, calculateDiffAmount, canApproveIOU, canCancelPayment, @@ -8908,4 +8909,389 @@ describe('actions/IOU', () => { ); }); }); + describe('approveMoneyRequest with take control', () => { + const adminAccountID = 1; + const managerAccountID = 2; + const employeeAccountID = 3; + const seniorManagerAccountID = 4; + const adminEmail = 'admin@test.com'; + const managerEmail = 'manager@test.com'; + const employeeEmail = 'employee@test.com'; + const seniorManagerEmail = 'seniormanager@test.com'; + + let expenseReport: Report; + let policy: Policy; + + beforeEach(async () => { + await Onyx.clear(); + + // Set up personal details + await Onyx.set(ONYXKEYS.PERSONAL_DETAILS_LIST, { + [adminAccountID]: { + accountID: adminAccountID, + login: adminEmail, + displayName: 'Admin User', + }, + [seniorManagerAccountID]: { + accountID: seniorManagerAccountID, + login: seniorManagerEmail, + displayName: 'Senior Manager User', + }, + [managerAccountID]: { + accountID: managerAccountID, + login: managerEmail, + displayName: 'Manager User', + }, + [employeeAccountID]: { + accountID: employeeAccountID, + login: employeeEmail, + displayName: 'Employee User', + }, + }); + + // Set up session as admin (who will approve) + await Onyx.set(ONYXKEYS.SESSION, { + email: adminEmail, + accountID: adminAccountID, + }); + + // Create policy with approval hierarchy + policy = { + id: '1', + name: 'Test Policy', + role: CONST.POLICY.ROLE.ADMIN, + owner: adminEmail, + ownerAccountID: adminAccountID, + outputCurrency: CONST.CURRENCY.USD, + isPolicyExpenseChatEnabled: true, + type: CONST.POLICY.TYPE.CORPORATE, + approvalMode: CONST.POLICY.APPROVAL_MODE.ADVANCED, + reimbursementChoice: CONST.POLICY.REIMBURSEMENT_CHOICES.REIMBURSEMENT_MANUAL, + employeeList: { + [employeeEmail]: { + email: employeeEmail, + role: CONST.POLICY.ROLE.USER, + submitsTo: managerEmail, + }, + [managerEmail]: { + email: managerEmail, + role: CONST.POLICY.ROLE.USER, + forwardsTo: seniorManagerEmail, + }, + [seniorManagerEmail]: { + email: seniorManagerEmail, + role: CONST.POLICY.ROLE.USER, + forwardsTo: adminEmail, + }, + [adminEmail]: { + email: adminEmail, + role: CONST.POLICY.ROLE.ADMIN, + forwardsTo: '', + }, + }, + }; + + // Create expense report + expenseReport = { + reportID: '123', + type: CONST.REPORT.TYPE.EXPENSE, + ownerAccountID: employeeAccountID, + managerID: managerAccountID, + policyID: policy.id, + stateNum: CONST.REPORT.STATE_NUM.SUBMITTED, + statusNum: CONST.REPORT.STATUS_NUM.SUBMITTED, + total: 1000, + currency: 'USD', + }; + + await Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${policy.id}`, policy); + await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${expenseReport.reportID}`, expenseReport); + }); + + afterEach(async () => { + await Onyx.clear(); + }); + + it('should set report to approved when admin takes control and approves', async () => { + // Admin takes control + const takeControlAction = { + reportActionID: 'takeControl1', + actionName: CONST.REPORT.ACTIONS.TYPE.TAKE_CONTROL, + actorAccountID: adminAccountID, + created: '2023-01-01T10:00:00.000Z', + }; + + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${expenseReport.reportID}`, { + [takeControlAction.reportActionID]: takeControlAction, + }); + + // Admin approves the report + approveMoneyRequest(expenseReport); + await waitForBatchedUpdates(); + + // Should be approved since admin took control and is the last approver + const updatedReport = await getOnyxValue(`${ONYXKEYS.COLLECTION.REPORT}${expenseReport.reportID}`); + expect(updatedReport?.stateNum).toBe(CONST.REPORT.STATE_NUM.APPROVED); + expect(updatedReport?.statusNum).toBe(CONST.REPORT.STATUS_NUM.APPROVED); + }); + + it('should invalidate take control when report is resubmitted after take control', async () => { + // Admin takes control first + const takeControlAction = { + reportActionID: 'takeControl3', + actionName: CONST.REPORT.ACTIONS.TYPE.TAKE_CONTROL, + actorAccountID: adminAccountID, + created: '2023-01-01T10:00:00.000Z', + }; + + // Employee resubmits after take control (invalidates it) + const submittedAction = { + reportActionID: 'submitted1', + actionName: CONST.REPORT.ACTIONS.TYPE.SUBMITTED, + actorAccountID: employeeAccountID, + created: '2023-01-01T11:00:00.000Z', // After take control + }; + + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${expenseReport.reportID}`, { + [takeControlAction.reportActionID]: takeControlAction, + [submittedAction.reportActionID]: submittedAction, + }); + + // Set session as manager (normal approver) + await Onyx.set(ONYXKEYS.SESSION, { + email: managerEmail, + accountID: managerAccountID, + }); + + // Manager approves the report + approveMoneyRequest(expenseReport); + await waitForBatchedUpdates(); + + // Should be submitted to senior manager (normal flow) since take control was invalidated + const updatedReport = await getOnyxValue(`${ONYXKEYS.COLLECTION.REPORT}${expenseReport.reportID}`); + expect(updatedReport?.stateNum).toBe(CONST.REPORT.STATE_NUM.SUBMITTED); + expect(updatedReport?.statusNum).toBe(CONST.REPORT.STATUS_NUM.SUBMITTED); + + // Get the optimistic next step + const nextStep = await getOnyxValue(`${ONYXKEYS.COLLECTION.NEXT_STEP}${expenseReport.reportID}`); + + // The next step message should be defined + expect(nextStep?.message).toBeDefined(); + + // Since take control was invalidated by resubmission, the normal approval chain applies + // The next step should indicate waiting for the senior manager to approve + const fullMessage = nextStep?.message?.map((part) => part.text).join(''); + expect(fullMessage).toBe('Waiting for Senior Manager User to approve %expenses.'); + }); + + it('should mention an admin to pay expenses in optimistic next step message when admin takes control and approves', async () => { + // Admin takes control + const takeControlAction = { + reportActionID: 'takeControl2', + actionName: CONST.REPORT.ACTIONS.TYPE.TAKE_CONTROL, + actorAccountID: adminAccountID, + created: '2023-01-01T10:00:00.000Z', + }; + + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${expenseReport.reportID}`, { + [takeControlAction.reportActionID]: takeControlAction, + }); + + // Admin approves the report + approveMoneyRequest(expenseReport); + await waitForBatchedUpdates(); + + // Get the optimistic next step + const nextStep = await getOnyxValue(`${ONYXKEYS.COLLECTION.NEXT_STEP}${expenseReport.reportID}`); + + // The next step message should be defined + expect(nextStep?.message).toBeDefined(); + + // Since the report is fully approved when admin takes control and approves, + // the next step should be about payment, which should mention the admin + // The message should equal "Waiting for Admin User to pay" + const fullMessage = nextStep?.message?.map((part) => part.text).join(''); + expect(fullMessage).toBe('Waiting for an admin to pay %expenses.'); + }); + }); + + describe('approveMoneyRequest with normal approval chain', () => { + const adminAccountID = 1; + const managerAccountID = 2; + const employeeAccountID = 3; + const adminEmail = 'admin@test.com'; + const managerEmail = 'manager@test.com'; + const employeeEmail = 'employee@test.com'; + + let expenseReport: Report; + let policy: Policy; + + beforeEach(async () => { + await Onyx.clear(); + + // Set up personal details + await Onyx.set(ONYXKEYS.PERSONAL_DETAILS_LIST, { + [adminAccountID]: { + accountID: adminAccountID, + login: adminEmail, + displayName: 'Admin User', + }, + [managerAccountID]: { + accountID: managerAccountID, + login: managerEmail, + displayName: 'Manager User', + }, + [employeeAccountID]: { + accountID: employeeAccountID, + login: employeeEmail, + displayName: 'Employee User', + }, + }); + + // Create policy with approval hierarchy + policy = { + id: '1', + name: 'Test Policy', + role: CONST.POLICY.ROLE.ADMIN, + owner: adminEmail, + outputCurrency: CONST.CURRENCY.USD, + isPolicyExpenseChatEnabled: true, + type: CONST.POLICY.TYPE.CORPORATE, + approvalMode: CONST.POLICY.APPROVAL_MODE.ADVANCED, + employeeList: { + [employeeEmail]: { + email: employeeEmail, + role: CONST.POLICY.ROLE.USER, + submitsTo: managerEmail, + }, + [managerEmail]: { + email: managerEmail, + role: CONST.POLICY.ROLE.USER, + submitsTo: adminEmail, + forwardsTo: adminEmail, + }, + [adminEmail]: { + email: adminEmail, + role: CONST.POLICY.ROLE.ADMIN, + submitsTo: '', + forwardsTo: '', + }, + }, + }; + + // Create expense report + expenseReport = { + reportID: '123', + type: CONST.REPORT.TYPE.EXPENSE, + ownerAccountID: employeeAccountID, + managerID: managerAccountID, + policyID: policy.id, + stateNum: CONST.REPORT.STATE_NUM.SUBMITTED, + statusNum: CONST.REPORT.STATUS_NUM.SUBMITTED, + total: 1000, + currency: 'USD', + }; + + await Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${policy.id}`, policy); + await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${expenseReport.reportID}`, expenseReport); + }); + + afterEach(async () => { + await Onyx.clear(); + }); + + it('should follow normal approval chain when manager approves without take control', async () => { + // Set session as manager (first approver in the chain) + await Onyx.set(ONYXKEYS.SESSION, { + email: managerEmail, + accountID: managerAccountID, + }); + + // Manager approves the report (no take control actions) + approveMoneyRequest(expenseReport); + await waitForBatchedUpdates(); + + // Should be submitted to admin (next in approval chain) since manager is not the final approver + const updatedReport = await getOnyxValue(`${ONYXKEYS.COLLECTION.REPORT}${expenseReport.reportID}`); + expect(updatedReport?.stateNum).toBe(CONST.REPORT.STATE_NUM.SUBMITTED); + expect(updatedReport?.statusNum).toBe(CONST.REPORT.STATUS_NUM.SUBMITTED); + expect(updatedReport?.managerID).toBe(adminAccountID); // Should be forwarded to admin + }); + + it('should handle multi-step approval chain correctly', async () => { + // First, manager approves + await Onyx.set(ONYXKEYS.SESSION, { + email: managerEmail, + accountID: managerAccountID, + }); + + approveMoneyRequest(expenseReport); + await waitForBatchedUpdates(); + + // Should be submitted to admin + let updatedReport = await getOnyxValue(`${ONYXKEYS.COLLECTION.REPORT}${expenseReport.reportID}`); + expect(updatedReport?.stateNum).toBe(CONST.REPORT.STATE_NUM.SUBMITTED); + expect(updatedReport?.statusNum).toBe(CONST.REPORT.STATUS_NUM.SUBMITTED); + expect(updatedReport?.managerID).toBe(adminAccountID); + + // Then, admin approves + await Onyx.set(ONYXKEYS.SESSION, { + email: adminEmail, + accountID: adminAccountID, + }); + + approveMoneyRequest(updatedReport); + await waitForBatchedUpdates(); + + // Should be fully approved + updatedReport = await getOnyxValue(`${ONYXKEYS.COLLECTION.REPORT}${expenseReport.reportID}`); + expect(updatedReport?.stateNum).toBe(CONST.REPORT.STATE_NUM.APPROVED); + expect(updatedReport?.statusNum).toBe(CONST.REPORT.STATUS_NUM.APPROVED); + }); + + it('should fully approve report when single approver approves', async () => { + // Create a policy with only one approver in the chain + const singleApproverPolicy: Policy = { + ...policy, + employeeList: { + [employeeEmail]: { + email: employeeEmail, + role: CONST.POLICY.ROLE.USER, + submitsTo: managerEmail, + }, + [managerEmail]: { + email: managerEmail, + role: CONST.POLICY.ROLE.ADMIN, + submitsTo: '', + forwardsTo: '', + }, + }, + }; + + // Create expense report with manager as the only approver + const singleApproverReport: Report = { + ...expenseReport, + reportID: '456', + managerID: managerAccountID, + }; + + await Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${singleApproverPolicy.id}`, singleApproverPolicy); + await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${singleApproverReport.reportID}`, singleApproverReport); + + // Set session as the single approver (manager) + await Onyx.set(ONYXKEYS.SESSION, { + email: managerEmail, + accountID: managerAccountID, + }); + + // Manager approves the report + approveMoneyRequest(singleApproverReport); + await waitForBatchedUpdates(); + + // Should be fully approved since manager is the final approver in the chain + const updatedReport = await getOnyxValue(`${ONYXKEYS.COLLECTION.REPORT}${singleApproverReport.reportID}`); + expect(updatedReport?.stateNum).toBe(CONST.REPORT.STATE_NUM.APPROVED); + expect(updatedReport?.statusNum).toBe(CONST.REPORT.STATUS_NUM.APPROVED); + }); + }); });