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
4 changes: 3 additions & 1 deletion src/hooks/useReportSubmitToPopover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import ReportSubmitToContent from '@pages/ReportSubmitToContent';

import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import {personalDetailsLoginSelector} from '@src/selectors/PersonalDetails';
import type AnchorAlignment from '@src/types/utils/AnchorAlignment';

import type {RefObject} from 'react';
Expand Down Expand Up @@ -90,6 +91,7 @@ function useReportSubmitToPopover({reportID, onSubmitSuccess, anchorAlignment =
const [report] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`);
const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${getNonEmptyStringOnyxID(report?.policyID)}`);
const [isLoadingReportData] = useOnyx(ONYXKEYS.IS_LOADING_REPORT_DATA);
const [ownerLogin] = useOnyx(ONYXKEYS.PERSONAL_DETAILS_LIST, {selector: personalDetailsLoginSelector(report?.ownerAccountID)});
const [willAlertModalBecomeVisible] = useOnyx(ONYXKEYS.MODAL, {
selector: willAlertModalBecomeVisibleSelector,
});
Expand All @@ -102,7 +104,7 @@ function useReportSubmitToPopover({reportID, onSubmitSuccess, anchorAlignment =
willAlertModalBecomeVisibleRef.current = willAlertModalBecomeVisible;
}, [willAlertModalBecomeVisible]);

const submitToContentKey = useMemo(() => `${reportID}:${getSubmitToEmail(policy, report)}`, [reportID, policy, report]);
const submitToContentKey = `${reportID}:${getSubmitToEmail(policy, report, ownerLogin)}`;

const clearDismissGuard = useCallback(() => {
setIsDismissGuardActive(false);
Expand Down
38 changes: 14 additions & 24 deletions src/libs/PolicyUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ import {isAnyHRConnected, isMergeHRCompleteSetupNeeded, shouldShowHRConnectionEr
import Navigation from './Navigation/Navigation';
import {getIsOffline} from './NetworkState';
import {formatMemberForList} from './OptionsListUtils';
import {getAccountIDsByLogins, getKnownAccountIDByLogin, getLoginsByAccountIDs, getPersonalDetailByEmail} from './PersonalDetailsUtils';
import {getAccountIDsByLogins, getKnownAccountIDByLogin, getPersonalDetailByEmail} from './PersonalDetailsUtils';
import {getAllSortedTransactions, getCategory, getTag, getTagArrayFromName} from './TransactionUtils';
import {generateAccountID} from './UserUtils';
import {isPublicDomain, isValidAccountRoute} from './ValidationUtils';
Expand Down Expand Up @@ -1751,19 +1751,28 @@ function getManagerAccountID(policy: OnyxEntry<Policy>, ownerLogin: string | und
}

/**
* Returns the accountID to whom the given expenseReport submits reports to in the given Policy.
* Returns the email the expense report should submit to per workspace approval config
* (approval rules, employee submitsTo, or default approver for basic/optional workflows).
*/
function getSubmitToAccountID(policy: OnyxEntry<Policy>, expenseReport: OnyxEntry<Report>, ownerLogin: string | undefined): number {
function getSubmitToEmail(policy: OnyxEntry<Policy>, expenseReport: OnyxEntry<Report>, ownerLogin: string | undefined): string {
const approvalRules = policy?.rules?.approvalRules;

if (!isSubmitAndClose(policy) && approvalRules?.length) {
const ruleApprover = getFirstRuleApprover(approvalRules, expenseReport, ownerLogin);
if (ruleApprover) {
return getAccountIDsByLogins([ruleApprover]).at(0) ?? -1;
return ruleApprover;
}
}

return getManagerAccountID(policy, ownerLogin);
return getManagerAccountEmail(policy, ownerLogin);
}

/**
* Returns the accountID to whom the given expenseReport submits reports to in the given Policy.
*/
function getSubmitToAccountID(policy: OnyxEntry<Policy>, expenseReport: OnyxEntry<Report>, ownerLogin: string | undefined): number {
const submitToEmail = getSubmitToEmail(policy, expenseReport, ownerLogin);
return submitToEmail ? (getAccountIDsByLogins([submitToEmail]).at(0) ?? CONST.DEFAULT_NUMBER_ID) : CONST.DEFAULT_NUMBER_ID;
}

function getSubmitReportManagerAccountID(policy: OnyxEntry<Policy>, expenseReport: OnyxEntry<Report>, submitterLogin: string | undefined): number | undefined {
Expand All @@ -1790,25 +1799,6 @@ function getSubmitReportManagerAccountID(policy: OnyxEntry<Policy>, expenseRepor
return isValidSubmitToAccountID ? submitToAccountID : existingManagerID;
}

/**
* Returns the email the expense report should submit to per workspace approval config
* (approval rules, employee submitsTo, or default approver for basic/optional workflows).
*/
function getSubmitToEmail(policy: OnyxEntry<Policy>, expenseReport: OnyxEntry<Report>): string {
const defaultApprover = getDefaultApprover(policy).trim();
if (!expenseReport) {
return defaultApprover;
}

const ownerLogin = getLoginsByAccountIDs([expenseReport.ownerAccountID ?? CONST.DEFAULT_NUMBER_ID]).at(0);
const submitToAccountID = getSubmitToAccountID(policy, expenseReport, ownerLogin);
if (!isValidAccountRoute(submitToAccountID)) {
return defaultApprover;
}

return getLoginsByAccountIDs([submitToAccountID]).at(0)?.trim() ?? defaultApprover;
}

/**
* Returns the email of the account to forward the report to depending on the approver's approval limit.
* Used for advanced approval mode only.
Expand Down
4 changes: 2 additions & 2 deletions src/pages/ReportSubmitToContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ function ReportSubmitToContent({
const isASAPSubmitBetaEnabled = isBetaEnabled(CONST.BETAS.ASAP_SUBMIT);
const hasViolations = hasViolationsReportUtils(report?.reportID, transactionViolations, currentUserDetails.accountID, currentUserDetails.login ?? '');

const prepopulatedEmail = useMemo(() => getSubmitToEmail(policy, report), [policy, report]);
const prepopulatedEmail = getSubmitToEmail(policy, report, submitterLogin);

const [userSelectedManagerEmail, setUserSelectedManagerEmail] = useState<string | undefined>();
const [extraSubmitToRecipients, setExtraSubmitToRecipients] = useState<WorkspaceMemberItem[]>([]);
Expand Down Expand Up @@ -160,7 +160,7 @@ function ReportSubmitToContent({
}

const accountID = getKnownAccountIDByLogin(email);
const details = accountID ? getPersonalDetailsByID(accountID, personalDetails) : undefined;
const details = getPersonalDetailsByID(accountID, personalDetails);

return {
accountID,
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/PolicyUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1101,7 +1101,7 @@ describe('PolicyUtils', () => {
ownerAccountID: employeeAccountID,
};

expect(getSubmitToEmail(policy, report)).toBe(adminEmail);
expect(getSubmitToEmail(policy, report, employeeEmail)).toBe(adminEmail);
});

it('should return the default approver', () => {
Expand Down
Loading