Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace policy.submitsTo with PolicyUtils.getSubmitToAccountID #40532

Merged
Merged
Show file tree
Hide file tree
Changes from 4 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
32 changes: 31 additions & 1 deletion src/libs/PolicyUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,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 @@ -320,6 +320,35 @@ 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 ?? '';
}

function getSubmitToAccountID(policy: OnyxEntry<Policy> | EmptyObject, employeeAccountID: number): number {
flodnv marked this conversation as resolved.
Show resolved Hide resolved
const employeeLogin = getLoginsByAccountIDs([employeeAccountID])[0];
const defaultApprover = getDefaultApprover(policy);

// For policy using the optional or basic workflow, the manager is the policy default approver.
bernhardoj marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -381,6 +410,7 @@ export {
getTaxByID,
hasPolicyCategoriesError,
getPolicyIDFromNavigationState,
getSubmitToAccountID,
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 @@ -3429,9 +3429,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 @@ -5869,9 +5870,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
Loading