Skip to content

[Fix] Create Expense - Submits To Display Name Missing #57426

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

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
5 changes: 3 additions & 2 deletions src/libs/PolicyUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -613,11 +613,11 @@ function getManagerAccountID(policy: OnyxEntry<Policy> | SearchPolicy, expenseRe
}

const employee = policy?.employeeList?.[employeeLogin];
if (!employee) {
if (!employee && !defaultApprover) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JKobrynski I think this is a good candidate for a unit test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All right, I'm on it!

return -1;
}

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

/**
Expand Down Expand Up @@ -1519,6 +1519,7 @@ export {
getPolicyNameByID,
getMostFrequentEmailDomain,
getDescriptionForPolicyDomainCard,
getManagerAccountID,
isPrefferedExporter,
isAutoSyncEnabled,
};
Expand Down
98 changes: 94 additions & 4 deletions tests/unit/PolicyUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import Onyx from 'react-native-onyx';
import type {OnyxEntry} from 'react-native-onyx';
import DateUtils from '@libs/DateUtils';
import {getActivePolicies, getPolicyNameByID, getRateDisplayValue, getSubmitToAccountID, getUnitRateValue, shouldShowPolicy} from '@libs/PolicyUtils';
import {getActivePolicies, getManagerAccountID, getPolicyNameByID, getRateDisplayValue, getSubmitToAccountID, getUnitRateValue, shouldShowPolicy} from '@libs/PolicyUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {PersonalDetailsList, Policy, PolicyEmployeeList, Report, Transaction} from '@src/types/onyx';
Expand All @@ -20,6 +20,12 @@ const CARLOS_ACCOUNT_ID = 1;
function toLocaleDigitMock(dot: string): string {
return dot;
}
const GENERATED_ACCOUNT_ID = '555555';

jest.mock('@libs/UserUtils', () => ({
// generateAccountID: () => GENERATED_ACCOUNT_ID,
generateAccountID: jest.fn().mockReturnValue(GENERATED_ACCOUNT_ID),
}));

const testDate = DateUtils.getDBTime();
const employeeList: PolicyEmployeeList = {
Expand Down Expand Up @@ -67,19 +73,22 @@ const categoryapprover2AccountID = 4;
const tagapprover1AccountID = 5;
const tagapprover2AccountID = 6;
const ownerAccountID = 7;
const employeeEmail = '[email protected]';
const adminEmail = '[email protected]';
const categoryApprover1Email = '[email protected]';

const personalDetails: PersonalDetailsList = {
'1': {
accountID: adminAccountID,
login: '[email protected]',
login: adminEmail,
},
'2': {
accountID: employeeAccountID,
login: '[email protected]',
login: employeeEmail,
},
'3': {
accountID: categoryapprover1AccountID,
login: '[email protected]',
login: categoryApprover1Email,
},
'4': {
accountID: categoryapprover2AccountID,
Expand Down Expand Up @@ -499,4 +508,85 @@ describe('PolicyUtils', () => {
expect(getPolicyNameByID('1')).toBe('1');
});
});

describe('getManagerAccountID', () => {
beforeEach(() => {
wrapOnyxWithWaitForBatchedUpdates(Onyx);
Onyx.set(ONYXKEYS.PERSONAL_DETAILS_LIST, personalDetails);
});
afterEach(async () => {
await Onyx.clear();
await waitForBatchedUpdatesWithAct();
});

it('should return default approver for personal workspaces', () => {
const policy: Policy = {
...createRandomPolicy(0),
type: CONST.POLICY.TYPE.PERSONAL,
approver: categoryApprover1Email,
};
const report: Report = {
...createRandomReport(0),
};
const result = getManagerAccountID(policy, report);

expect(result).toBe(categoryapprover1AccountID);
});

it('should return -1 if there is no employee or default approver', () => {
const policy: Policy = {
...createRandomPolicy(0),
type: CONST.POLICY.TYPE.TEAM,
approvalMode: undefined,
approver: undefined,
owner: '',
};
const report: Report = {
...createRandomReport(0),
};

const result = getManagerAccountID(policy, report);

expect(result).toBe(-1);
});

it('should return submitsTo account ID', () => {
const policy: Policy = {
...createRandomPolicy(0),
type: CONST.POLICY.TYPE.TEAM,
approvalMode: undefined,
employeeList: {
[employeeEmail]: {
email: employeeEmail,
submitsTo: adminEmail,
},
},
};
const report: Report = {
...createRandomReport(0),
ownerAccountID: employeeAccountID,
};

const result = getManagerAccountID(policy, report);

expect(result).toBe(adminAccountID);
});

it('should return the default approver', () => {
const policy: Policy = {
...createRandomPolicy(0),
type: CONST.POLICY.TYPE.TEAM,
approvalMode: undefined,
approver: categoryApprover1Email,
};
const report: Report = {
...createRandomReport(0),
ownerAccountID: employeeAccountID,
};

const result = getManagerAccountID(policy, report);

expect(result).toBe(categoryapprover1AccountID);
});
});
});