Skip to content

Commit ed75020

Browse files
authored
Merge pull request #57426 from callstack-internal/JKobrynski/fix/56609-display-name-missing
[Fix] Create Expense - Submits To Display Name Missing
2 parents d235a03 + 4ce0268 commit ed75020

File tree

2 files changed

+97
-6
lines changed

2 files changed

+97
-6
lines changed

src/libs/PolicyUtils.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -613,11 +613,11 @@ function getManagerAccountID(policy: OnyxEntry<Policy> | SearchPolicy, expenseRe
613613
}
614614

615615
const employee = policy?.employeeList?.[employeeLogin];
616-
if (!employee) {
616+
if (!employee && !defaultApprover) {
617617
return -1;
618618
}
619619

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

623623
/**
@@ -1519,6 +1519,7 @@ export {
15191519
getPolicyNameByID,
15201520
getMostFrequentEmailDomain,
15211521
getDescriptionForPolicyDomainCard,
1522+
getManagerAccountID,
15221523
isPrefferedExporter,
15231524
isAutoSyncEnabled,
15241525
};

tests/unit/PolicyUtilsTest.ts

Lines changed: 94 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import Onyx from 'react-native-onyx';
33
import type {OnyxEntry} from 'react-native-onyx';
44
import DateUtils from '@libs/DateUtils';
5-
import {getActivePolicies, getPolicyNameByID, getRateDisplayValue, getSubmitToAccountID, getUnitRateValue, shouldShowPolicy} from '@libs/PolicyUtils';
5+
import {getActivePolicies, getManagerAccountID, getPolicyNameByID, getRateDisplayValue, getSubmitToAccountID, getUnitRateValue, shouldShowPolicy} from '@libs/PolicyUtils';
66
import CONST from '@src/CONST';
77
import ONYXKEYS from '@src/ONYXKEYS';
88
import type {PersonalDetailsList, Policy, PolicyEmployeeList, Report, Transaction} from '@src/types/onyx';
@@ -20,6 +20,12 @@ const CARLOS_ACCOUNT_ID = 1;
2020
function toLocaleDigitMock(dot: string): string {
2121
return dot;
2222
}
23+
const GENERATED_ACCOUNT_ID = '555555';
24+
25+
jest.mock('@libs/UserUtils', () => ({
26+
// generateAccountID: () => GENERATED_ACCOUNT_ID,
27+
generateAccountID: jest.fn().mockReturnValue(GENERATED_ACCOUNT_ID),
28+
}));
2329

2430
const testDate = DateUtils.getDBTime();
2531
const employeeList: PolicyEmployeeList = {
@@ -67,19 +73,22 @@ const categoryapprover2AccountID = 4;
6773
const tagapprover1AccountID = 5;
6874
const tagapprover2AccountID = 6;
6975
const ownerAccountID = 7;
76+
const employeeEmail = '[email protected]';
77+
const adminEmail = '[email protected]';
78+
const categoryApprover1Email = '[email protected]';
7079

7180
const personalDetails: PersonalDetailsList = {
7281
'1': {
7382
accountID: adminAccountID,
74-
83+
login: adminEmail,
7584
},
7685
'2': {
7786
accountID: employeeAccountID,
78-
87+
login: employeeEmail,
7988
},
8089
'3': {
8190
accountID: categoryapprover1AccountID,
82-
91+
login: categoryApprover1Email,
8392
},
8493
'4': {
8594
accountID: categoryapprover2AccountID,
@@ -499,4 +508,85 @@ describe('PolicyUtils', () => {
499508
expect(getPolicyNameByID('1')).toBe('1');
500509
});
501510
});
511+
512+
describe('getManagerAccountID', () => {
513+
beforeEach(() => {
514+
wrapOnyxWithWaitForBatchedUpdates(Onyx);
515+
Onyx.set(ONYXKEYS.PERSONAL_DETAILS_LIST, personalDetails);
516+
});
517+
afterEach(async () => {
518+
await Onyx.clear();
519+
await waitForBatchedUpdatesWithAct();
520+
});
521+
522+
it('should return default approver for personal workspaces', () => {
523+
const policy: Policy = {
524+
...createRandomPolicy(0),
525+
type: CONST.POLICY.TYPE.PERSONAL,
526+
approver: categoryApprover1Email,
527+
};
528+
const report: Report = {
529+
...createRandomReport(0),
530+
};
531+
const result = getManagerAccountID(policy, report);
532+
533+
expect(result).toBe(categoryapprover1AccountID);
534+
});
535+
536+
it('should return -1 if there is no employee or default approver', () => {
537+
const policy: Policy = {
538+
...createRandomPolicy(0),
539+
type: CONST.POLICY.TYPE.TEAM,
540+
approvalMode: undefined,
541+
approver: undefined,
542+
owner: '',
543+
};
544+
const report: Report = {
545+
...createRandomReport(0),
546+
};
547+
548+
const result = getManagerAccountID(policy, report);
549+
550+
expect(result).toBe(-1);
551+
});
552+
553+
it('should return submitsTo account ID', () => {
554+
const policy: Policy = {
555+
...createRandomPolicy(0),
556+
type: CONST.POLICY.TYPE.TEAM,
557+
approvalMode: undefined,
558+
employeeList: {
559+
[employeeEmail]: {
560+
email: employeeEmail,
561+
submitsTo: adminEmail,
562+
},
563+
},
564+
};
565+
const report: Report = {
566+
...createRandomReport(0),
567+
ownerAccountID: employeeAccountID,
568+
};
569+
570+
const result = getManagerAccountID(policy, report);
571+
572+
expect(result).toBe(adminAccountID);
573+
});
574+
575+
it('should return the default approver', () => {
576+
const policy: Policy = {
577+
...createRandomPolicy(0),
578+
type: CONST.POLICY.TYPE.TEAM,
579+
approvalMode: undefined,
580+
approver: categoryApprover1Email,
581+
};
582+
const report: Report = {
583+
...createRandomReport(0),
584+
ownerAccountID: employeeAccountID,
585+
};
586+
587+
const result = getManagerAccountID(policy, report);
588+
589+
expect(result).toBe(categoryapprover1AccountID);
590+
});
591+
});
502592
});

0 commit comments

Comments
 (0)