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
8 changes: 5 additions & 3 deletions src/libs/NextStepUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type {EmptyObject} from '@src/types/utils/EmptyObject';
import DateUtils from './DateUtils';
import EmailUtils from './EmailUtils';
import * as PersonalDetailsUtils from './PersonalDetailsUtils';
import * as PolicyUtils from './PolicyUtils';
import * as ReportUtils from './ReportUtils';

let currentUserAccountID = -1;
Expand Down Expand Up @@ -81,12 +82,13 @@ function buildNextStep(

const {policyID = '', ownerAccountID = -1, managerID = -1} = report;
const policy = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`] ?? ({} as Policy);
const {submitsTo, harvesting, preventSelfApproval, autoReportingFrequency, autoReportingOffset} = policy;
const {harvesting, preventSelfApproval, autoReportingFrequency, autoReportingOffset} = policy;
const submitToAccountID = PolicyUtils.getSubmitToAccountID(policy, ownerAccountID);
const isOwner = currentUserAccountID === ownerAccountID;
const isManager = currentUserAccountID === managerID;
const isSelfApproval = currentUserAccountID === submitsTo;
const isSelfApproval = currentUserAccountID === submitToAccountID;
const ownerLogin = PersonalDetailsUtils.getLoginsByAccountIDs([ownerAccountID])[0] ?? '';
const managerDisplayName = isSelfApproval ? 'you' : ReportUtils.getDisplayNameForParticipant(submitsTo) ?? '';
const managerDisplayName = isSelfApproval ? 'you' : ReportUtils.getDisplayNameForParticipant(submitToAccountID) ?? '';
const type: ReportNextStep['type'] = 'neutral';
let optimisticNextStep: ReportNextStep | null;

Expand Down
35 changes: 34 additions & 1 deletion src/libs/PolicyUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {isEmptyObject} from '@src/types/utils/EmptyObject';
import getPolicyIDFromState from './Navigation/getPolicyIDFromState';
import Navigation, {navigationRef} from './Navigation/Navigation';
import type {RootStackParamList, State} from './Navigation/types';
import {getPersonalDetailByEmail} from './PersonalDetailsUtils';
import {getAccountIDsByLogins, getLoginsByAccountIDs, getPersonalDetailByEmail} from './PersonalDetailsUtils';

type MemberEmailsToAccountIDs = Record<string, number>;

Expand Down Expand Up @@ -321,6 +321,38 @@ function isPolicyFeatureEnabled(policy: OnyxEntry<Policy> | EmptyObject, feature
return Boolean(policy?.[featureName]);
}

function getApprovalWorkflow(policy: OnyxEntry<Policy> | EmptyObject): ValueOf<typeof CONST.POLICY.APPROVAL_MODE> {
if (policy?.type === CONST.POLICY.TYPE.PERSONAL) {
return CONST.POLICY.APPROVAL_MODE.OPTIONAL;
}

return policy?.approvalMode ?? CONST.POLICY.APPROVAL_MODE.ADVANCED;
}

function getDefaultApprover(policy: OnyxEntry<Policy> | EmptyObject): string {
return policy?.approver ?? policy?.owner ?? '';
}

/**
* Returns the accountID to whom the given employeeAccountID submits reports to in the given Policy.
*/
function getSubmitToAccountID(policy: OnyxEntry<Policy> | EmptyObject, employeeAccountID: number): number {
const employeeLogin = getLoginsByAccountIDs([employeeAccountID])[0];
const defaultApprover = getDefaultApprover(policy);

// For policy using the optional or basic workflow, the manager is the policy default approver.
if (([CONST.POLICY.APPROVAL_MODE.OPTIONAL, CONST.POLICY.APPROVAL_MODE.BASIC] as Array<ValueOf<typeof CONST.POLICY.APPROVAL_MODE>>).includes(getApprovalWorkflow(policy))) {
return getAccountIDsByLogins([defaultApprover])[0];
}

const employee = policy?.employeeList?.[employeeLogin];
if (!employee) {
return -1;
}

return getAccountIDsByLogins([employee.submitsTo ?? defaultApprover])[0];
}

function getPersonalPolicy() {
return Object.values(allPolicies ?? {}).find((policy) => policy?.type === CONST.POLICY.TYPE.PERSONAL);
}
Expand Down Expand Up @@ -386,6 +418,7 @@ export {
getTaxByID,
hasPolicyCategoriesError,
getPolicyIDFromNavigationState,
getSubmitToAccountID,
getAdminEmployees,
getPolicy,
};
Expand Down
11 changes: 6 additions & 5 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3468,9 +3468,10 @@ function buildOptimisticExpenseReport(chatReportID: string, policyID: string, pa
lastVisibleActionCreated: DateUtils.getDBTime(),
};

// The account defined in the policy submitsTo field is the approver/ manager for this report
if (policy?.submitsTo) {
expenseReport.managerID = policy.submitsTo;
// Get the approver/manager for this report to properly display the optimistic data
const submitToAccountID = PolicyUtils.getSubmitToAccountID(policy, payeeAccountID);
if (submitToAccountID) {
expenseReport.managerID = submitToAccountID;
}

const titleReportField = getTitleReportField(getReportFieldsByPolicyID(policyID) ?? {});
Expand Down Expand Up @@ -6010,9 +6011,9 @@ function isAllowedToApproveExpenseReport(report: OnyxEntry<Report>, approverAcco

function isAllowedToSubmitDraftExpenseReport(report: OnyxEntry<Report>): boolean {
const policy = getPolicy(report?.policyID);
const {submitsTo} = policy;
const submitToAccountID = PolicyUtils.getSubmitToAccountID(policy, report?.ownerAccountID ?? -1);

return isAllowedToApproveExpenseReport(report, submitsTo);
return isAllowedToApproveExpenseReport(report, submitToAccountID);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/libs/actions/IOU.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5584,7 +5584,7 @@ function submitReport(expenseReport: OnyxTypes.Report) {

const parameters: SubmitReportParams = {
reportID: expenseReport.reportID,
managerAccountID: policy.submitsTo ?? expenseReport.managerID,
managerAccountID: PolicyUtils.getSubmitToAccountID(policy, expenseReport.ownerAccountID ?? -1) ?? expenseReport.managerID,
reportActionID: optimisticSubmittedReportAction.reportActionID,
};

Expand Down
30 changes: 25 additions & 5 deletions tests/unit/NextStepUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ describe('libs/NextStepUtils', () => {
// Important props
id: policyID,
owner: currentUserEmail,
submitsTo: currentUserAccountID,
harvesting: {
enabled: false,
},
Expand Down Expand Up @@ -51,6 +50,11 @@ describe('libs/NextStepUtils', () => {
login: strangeEmail,
avatar: '',
},
[currentUserAccountID]: {
accountID: currentUserAccountID,
login: currentUserEmail,
avatar: '',
},
},
...policyCollectionDataSet,
}).then(waitForBatchedUpdates);
Expand Down Expand Up @@ -341,8 +345,12 @@ describe('libs/NextStepUtils', () => {
];

return Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, {
submitsTo: currentUserAccountID,
preventSelfApproval: true,
employeeList: {
[currentUserEmail]: {
submitsTo: currentUserEmail,
},
},
}).then(() => {
const result = NextStepUtils.buildNextStep(report, CONST.REPORT.STATUS_NUM.OPEN);

Expand Down Expand Up @@ -403,7 +411,11 @@ describe('libs/NextStepUtils', () => {
];

return Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, {
submitsTo: strangeAccountID,
employeeList: {
[currentUserEmail]: {
submitsTo: strangeEmail,
},
},
}).then(() => {
const result = NextStepUtils.buildNextStep(report, CONST.REPORT.STATUS_NUM.SUBMITTED);

Expand Down Expand Up @@ -438,9 +450,17 @@ describe('libs/NextStepUtils', () => {
},
];

const result = NextStepUtils.buildNextStep(report, CONST.REPORT.STATUS_NUM.SUBMITTED);
return Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, {
employeeList: {
[strangeEmail]: {
submitsTo: currentUserEmail,
},
},
}).then(() => {
const result = NextStepUtils.buildNextStep(report, CONST.REPORT.STATUS_NUM.SUBMITTED);

expect(result).toMatchObject(optimisticNextStep);
expect(result).toMatchObject(optimisticNextStep);
});
});

test('submit and close approval mode', () => {
Expand Down