Skip to content

Commit 754ed7a

Browse files
Refactor: removed ONYXKEYS.COLLECTION.POLICY from PolicyUtils
1 parent 1dd01da commit 754ed7a

6 files changed

Lines changed: 26 additions & 143 deletions

File tree

src/libs/DistanceRequestUtils.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import type {OnyxEntry} from 'react-native-onyx';
1+
import type {OnyxCollection, OnyxEntry} from 'react-native-onyx';
2+
import Onyx from 'react-native-onyx';
23
import type {CurrencyListActionsContextType} from '@components/CurrencyListContextProvider';
34
import type {LocaleContextProps} from '@components/LocaleContextProvider';
45
import CONST from '@src/CONST';
6+
import ONYXKEYS from '@src/ONYXKEYS';
57
import type {LastSelectedDistanceRates, OnyxInputOrEntry, Transaction} from '@src/types/onyx';
68
import type {Unit} from '@src/types/onyx/Policy';
79
import type Policy from '@src/types/onyx/Policy';
810
import {isEmptyObject} from '@src/types/utils/EmptyObject';
911
import {replaceAllDigits} from './MoneyRequestUtils';
10-
// This will be fixed as part of https://github.com/Expensify/App/issues/66397
11-
12-
import {getDistanceRateCustomUnit, getDistanceRateCustomUnitRate, getPersonalPolicy, getUnitRateValue} from './PolicyUtils';
12+
import {getDistanceRateCustomUnit, getDistanceRateCustomUnitRate, getUnitRateValue} from './PolicyUtils';
1313
import {getCurrency, getRateID, isCustomUnitRateIDForP2P, isExpenseUnreported} from './TransactionUtils';
1414

1515
type MileageRate = {
@@ -22,6 +22,15 @@ type MileageRate = {
2222
index?: number;
2323
};
2424

25+
/** @private Only for getRate function */
26+
let allPolicies: OnyxCollection<Policy>;
27+
28+
Onyx.connectWithoutView({
29+
key: ONYXKEYS.COLLECTION.POLICY,
30+
waitForCollectionCallback: true,
31+
callback: (value) => (allPolicies = value),
32+
});
33+
2534
const METERS_TO_KM = 0.001; // 1 kilometer is 1000 meters
2635
const METERS_TO_MILES = 0.000621371; // There are approximately 0.000621371 miles in a meter
2736

@@ -396,6 +405,11 @@ function getDistanceUnit(transaction: OnyxEntry<Transaction>, mileageRate: OnyxE
396405
return transaction?.comment?.customUnit?.distanceUnit ?? mileageRate?.unit ?? CONST.CUSTOM_UNITS.DISTANCE_UNIT_MILES;
397406
}
398407

408+
/** @private This is only for internal use for getRate function */
409+
function getPersonalPolicy() {
410+
return Object.values(allPolicies ?? {}).find((policy) => policy?.type === CONST.POLICY.TYPE.PERSONAL);
411+
}
412+
399413
/**
400414
* Get the selected rate for a transaction, from the policy or P2P default rate.
401415
* Use the distanceUnit stored on the transaction by default to prevent policy changes modifying existing transactions. Otherwise, get the unit from the rate.
@@ -424,8 +438,6 @@ function getRate({
424438
mileageRates = getMileageRates(policyDraft, true, transaction?.comment?.customUnit?.customUnitRateID);
425439
}
426440
const mileageRatesForMovingExpenses = getMileageRates(policyForMovingExpenses, true, transaction?.comment?.customUnit?.customUnitRateID);
427-
// This will be fixed as part of https://github.com/Expensify/App/issues/66397
428-
// eslint-disable-next-line @typescript-eslint/no-deprecated
429441
const policyCurrency = policy?.outputCurrency ?? getPersonalPolicy()?.outputCurrency ?? CONST.CURRENCY.USD;
430442
const isUnreportedExpense = isExpenseUnreported(transaction);
431443
const defaultMileageRate = getDefaultMileageRate(policy);

src/libs/OptionsListUtils/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import {
3232
getCountOfRequiredTagLists,
3333
getSubmitToAccountID,
3434
hasDynamicExternalWorkflow,
35-
isCurrentUserMemberOfAnyPolicy,
3635
isTimeTrackingEnabled,
3736
} from '@libs/PolicyUtils';
3837
import {
@@ -2406,7 +2405,7 @@ function prepareReportOptionsForDisplay(
24062405
*/
24072406
function getRestrictedLogins(config: GetOptionsConfig, options: OptionList, canShowManagerMcTest: boolean): Record<string, boolean> {
24082407
return {
2409-
[CONST.EMAIL.MANAGER_MCTEST]: !canShowManagerMcTest || !Permissions.isBetaEnabled(CONST.BETAS.NEWDOT_MANAGER_MCTEST, config.betas) || isCurrentUserMemberOfAnyPolicy(),
2408+
[CONST.EMAIL.MANAGER_MCTEST]: !canShowManagerMcTest || !Permissions.isBetaEnabled(CONST.BETAS.NEWDOT_MANAGER_MCTEST, config.betas),
24102409
};
24112410
}
24122411

src/libs/PolicyUtils.ts

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import {Str} from 'expensify-common';
22
import type {OnyxCollection, OnyxEntry} from 'react-native-onyx';
3-
import Onyx from 'react-native-onyx';
43
import type {TupleToUnion, ValueOf} from 'type-fest';
54
import type {LocaleContextProps, LocalizedTranslate} from '@components/LocaleContextProvider';
65
import type {SelectorType} from '@components/SelectionScreen';
@@ -72,14 +71,6 @@ type ConnectionWithLastSyncData = {
7271
lastSync?: ConnectionLastSync;
7372
};
7473

75-
let allPolicies: OnyxCollection<Policy>;
76-
77-
Onyx.connect({
78-
key: ONYXKEYS.COLLECTION.POLICY,
79-
waitForCollectionCallback: true,
80-
callback: (value) => (allPolicies = value),
81-
});
82-
8374
/**
8475
* Returns true if the policy has no fieldList or its fieldList is empty.
8576
*/
@@ -405,10 +396,6 @@ function getPolicyRole(policy: OnyxInputOrEntry<Policy>, currentUserLogin?: stri
405396
return policy?.employeeList?.[currentUserLogin]?.role;
406397
}
407398

408-
function getPolicyNameByID(policyID: string): string {
409-
return allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`]?.name ?? '';
410-
}
411-
412399
/**
413400
* Check if the policy can be displayed
414401
* If shouldShowPendingDeletePolicy is true, show the policy pending deletion.
@@ -1276,11 +1263,6 @@ function getReimburserAccountID(policy: OnyxEntry<Policy>): number {
12761263
return reimburserEmail ? (getAccountIDsByLogins([reimburserEmail]).at(0) ?? -1) : -1;
12771264
}
12781265

1279-
/** @deprecated Please use ONYXKEYS.PERSONAL_POLICY_ID to find the personal policyID */
1280-
function getPersonalPolicy() {
1281-
return Object.values(allPolicies ?? {}).find((policy) => policy?.type === CONST.POLICY.TYPE.PERSONAL);
1282-
}
1283-
12841266
function getAdminEmployees(policy: OnyxEntry<Policy>): PolicyEmployee[] {
12851267
if (!policy?.employeeList) {
12861268
return [];
@@ -1306,7 +1288,7 @@ function getActiveEmployeeWorkspaces(policies: OnyxCollection<Policy> | null, cu
13061288
* Checks whether the current user has a policy with admin access
13071289
*/
13081290
function hasActiveAdminWorkspaces(currentUserLogin: string | undefined, policies?: OnyxCollection<Policy>) {
1309-
return getActiveAdminWorkspaces(policies ?? allPolicies, currentUserLogin).length > 0;
1291+
return getActiveAdminWorkspaces(policies, currentUserLogin).length > 0;
13101292
}
13111293

13121294
/**
@@ -2053,13 +2035,6 @@ function isPreferredExporter(policy: Policy, currentUserLogin: string) {
20532035
return exporters.some((exporter) => exporter && exporter === currentUserLogin);
20542036
}
20552037

2056-
/**
2057-
* Checks if the current user is a member of any policyExpenseChatEnabled policy
2058-
*/
2059-
function isCurrentUserMemberOfAnyPolicy(): boolean {
2060-
return Object.values(allPolicies ?? {}).some((policy) => policy?.isPolicyExpenseChatEnabled && policy?.id && policy.id !== CONST.POLICY.ID_FAKE);
2061-
}
2062-
20632038
/**
20642039
* Determines which travel step should be shown based on policy state
20652040
*/
@@ -2145,9 +2120,6 @@ export {
21452120
getSoftExclusionsForGuideAndAccountManager,
21462121
filterGuideAndAccountManager,
21472122
isMultiLevelTags,
2148-
// This will be fixed as part of https://github.com/Expensify/App/issues/66397
2149-
// eslint-disable-next-line @typescript-eslint/no-deprecated
2150-
getPersonalPolicy,
21512123
getPolicyBrickRoadIndicatorStatus,
21522124
getSortedTagKeys,
21532125
getTagList,
@@ -2273,7 +2245,6 @@ export {
22732245
getRuleApprovers,
22742246
canModifyPlan,
22752247
getAdminsPrivateEmailDomains,
2276-
getPolicyNameByID,
22772248
getMostFrequentEmailDomain,
22782249
getDescriptionForPolicyDomainCard,
22792250
getManagerAccountID,
@@ -2282,7 +2253,6 @@ export {
22822253
areAllGroupPoliciesExpenseChatDisabled,
22832254
getCountOfRequiredTagLists,
22842255
getActiveEmployeeWorkspaces,
2285-
isCurrentUserMemberOfAnyPolicy,
22862256
getPolicyRole,
22872257
hasIndependentTags,
22882258
getLengthOfTag,

src/libs/ReportUtils.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,6 @@ import {
156156
getManagerAccountID,
157157
getPerDiemCustomUnit,
158158
getPolicyByCustomUnitID,
159-
getPolicyNameByID,
160159
getPolicyRole,
161160
getRuleApprovers,
162161
getSubmitToAccountID,
@@ -6999,14 +6998,16 @@ function getMovedActionMessage(translate: LocalizedTranslate, action: ReportActi
69996998
return '';
70006999
}
70017000
const {toPolicyID, newParentReportID, movedReportID} = movedActionOriginalMessage;
7002-
const toPolicyName = getPolicyNameByID(toPolicyID);
7001+
const toPolicyName = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${toPolicyID}`]?.name ?? '';
70037002
return translate('iou.movedAction', !isDM(report), getReportURLForCurrentContext(movedReportID), getReportURLForCurrentContext(newParentReportID), toPolicyName);
70047003
}
70057004

70067005
function getPolicyChangeMessage(translate: LocalizedTranslate, action: ReportAction) {
70077006
const PolicyChangeOriginalMessage = getOriginalMessage(action as ReportAction<typeof CONST.REPORT.ACTIONS.TYPE.CHANGE_POLICY>) ?? {};
70087007
const {fromPolicy: fromPolicyID, toPolicy: toPolicyID} = PolicyChangeOriginalMessage as OriginalMessageChangePolicy;
7009-
const message = translate('report.actions.type.changeReportPolicy', getPolicyNameByID(toPolicyID), fromPolicyID ? getPolicyNameByID(fromPolicyID) : undefined);
7008+
const toPolicyName = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${toPolicyID}`]?.name ?? '';
7009+
const fromPolicyName = fromPolicyID ? (allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${fromPolicyID}`]?.name ?? '') : undefined;
7010+
const message = translate('report.actions.type.changeReportPolicy', toPolicyName, fromPolicyName);
70107011
return message;
70117012
}
70127013

src/pages/iou/request/ParticipantSearchResults.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,8 @@ function ParticipantSearchResults({
156156

157157
// This is necessary to prevent showing the Manager McTest when there are multiple transactions being created
158158
const hasMultipleTransactions = optimisticTransactions.length > 1;
159-
const canShowManagerMcTest = !hasBeenAddedToNudgeMigration && action !== CONST.IOU.ACTION.SUBMIT && !hasMultipleTransactions;
159+
const isCurrentUserMemberOfAnyPolicy = Object.values(allPolicies ?? {}).some((pol) => pol?.isPolicyExpenseChatEnabled && pol?.id && pol.id !== CONST.POLICY.ID_FAKE);
160+
const canShowManagerMcTest = !hasBeenAddedToNudgeMigration && action !== CONST.IOU.ACTION.SUBMIT && !hasMultipleTransactions && !isCurrentUserMemberOfAnyPolicy;
160161

161162
const getValidOptionsConfig = {
162163
selectedOptions: participants as Participant[],

tests/unit/PolicyUtilsTest.ts

Lines changed: 0 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import {
1818
getEligibleBankAccountShareRecipients,
1919
getManagerAccountID,
2020
getPolicyEmployeeAccountIDs,
21-
getPolicyNameByID,
2221
getRateDisplayValue,
2322
getSubmitToAccountID,
2423
getTagApproverRule,
@@ -33,7 +32,6 @@ import {
3332
hasOnlyPersonalPolicies,
3433
hasOtherControlWorkspaces,
3534
hasPolicyWithXeroConnection,
36-
isCurrentUserMemberOfAnyPolicy,
3735
isPolicyMemberWithoutPendingDelete,
3836
shouldShowPolicy,
3937
sortPoliciesByName,
@@ -779,31 +777,6 @@ describe('PolicyUtils', () => {
779777
});
780778
});
781779

782-
describe('getPolicyNameByID', () => {
783-
it('should return the policy name for a given policyID', async () => {
784-
const policy: Policy = {
785-
...createRandomPolicy(1, CONST.POLICY.TYPE.TEAM),
786-
name: 'testName',
787-
};
788-
789-
await Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}1`, policy);
790-
791-
expect(getPolicyNameByID('1')).toBe('testName');
792-
});
793-
794-
it('should return the empty if the name is not set', async () => {
795-
const policy: Policy = {
796-
...createRandomPolicy(1, CONST.POLICY.TYPE.TEAM),
797-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
798-
name: null!,
799-
};
800-
801-
await Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}1`, policy);
802-
803-
expect(getPolicyNameByID('1')).toBe('');
804-
});
805-
});
806-
807780
describe('getManagerAccountID', () => {
808781
beforeEach(() => {
809782
wrapOnyxWithWaitForBatchedUpdates(Onyx);
@@ -978,79 +951,6 @@ describe('PolicyUtils', () => {
978951
});
979952
});
980953

981-
describe('isCurrentUserMemberOfAnyPolicy', () => {
982-
beforeEach(() => {
983-
wrapOnyxWithWaitForBatchedUpdates(Onyx);
984-
});
985-
afterEach(async () => {
986-
await Onyx.clear();
987-
await waitForBatchedUpdatesWithAct();
988-
});
989-
990-
it('should return false if user has no policies', async () => {
991-
const currentUserLogin = approverEmail;
992-
const currentUserAccountID = approverAccountID;
993-
994-
await Onyx.set(ONYXKEYS.SESSION, {email: currentUserLogin, accountID: currentUserAccountID});
995-
await Onyx.set(ONYXKEYS.COLLECTION.POLICY, {});
996-
997-
const result = isCurrentUserMemberOfAnyPolicy();
998-
999-
expect(result).toBeFalsy();
1000-
});
1001-
1002-
it('should return true if user owns a workspace', async () => {
1003-
const currentUserLogin = approverEmail;
1004-
const currentUserAccountID = approverAccountID;
1005-
const policies = {...createRandomPolicy(0, CONST.POLICY.TYPE.TEAM, `John's Workspace`), ownerAccountID: approverAccountID, isPolicyExpenseChatEnabled: true};
1006-
1007-
await Onyx.set(ONYXKEYS.SESSION, {email: currentUserLogin, accountID: currentUserAccountID});
1008-
await Onyx.set(ONYXKEYS.COLLECTION.POLICY, policies);
1009-
1010-
const result = isCurrentUserMemberOfAnyPolicy();
1011-
1012-
expect(result).toBeTruthy();
1013-
});
1014-
1015-
it('should return false if expense chat is not enabled', async () => {
1016-
const currentUserLogin = approverEmail;
1017-
const currentUserAccountID = approverAccountID;
1018-
const policies = {...createRandomPolicy(0, CONST.POLICY.TYPE.TEAM, `John's Workspace`), isPolicyExpenseChatEnabled: false};
1019-
1020-
await Onyx.set(ONYXKEYS.SESSION, {email: currentUserLogin, accountID: currentUserAccountID});
1021-
await Onyx.set(ONYXKEYS.COLLECTION.POLICY, policies);
1022-
1023-
const result = isCurrentUserMemberOfAnyPolicy();
1024-
1025-
expect(result).toBeFalsy();
1026-
});
1027-
1028-
it('should return false if its a fake policy id', async () => {
1029-
const currentUserLogin = approverEmail;
1030-
const currentUserAccountID = approverAccountID;
1031-
const policies = {...createRandomPolicy(0, CONST.POLICY.TYPE.TEAM, `John's Workspace`), id: CONST.POLICY.ID_FAKE};
1032-
1033-
await Onyx.set(ONYXKEYS.SESSION, {email: currentUserLogin, accountID: currentUserAccountID});
1034-
await Onyx.set(ONYXKEYS.COLLECTION.POLICY, policies);
1035-
1036-
const result = isCurrentUserMemberOfAnyPolicy();
1037-
1038-
expect(result).toBeFalsy();
1039-
});
1040-
1041-
it('should return true if user is invited to a workspace', async () => {
1042-
const currentUserLogin = approverEmail;
1043-
const currentUserAccountID = approverAccountID;
1044-
const policies = {...createRandomPolicy(0, CONST.POLICY.TYPE.TEAM, `John's Workspace`), ownerAccountID, isPolicyExpenseChatEnabled: true};
1045-
1046-
await Onyx.set(ONYXKEYS.SESSION, {email: currentUserLogin, accountID: currentUserAccountID});
1047-
await Onyx.set(ONYXKEYS.COLLECTION.POLICY, policies);
1048-
1049-
const result = isCurrentUserMemberOfAnyPolicy();
1050-
1051-
expect(result).toBeTruthy();
1052-
});
1053-
});
1054954
describe('getTagList', () => {
1055955
it.each([
1056956
['when index is 0', 0, policyTags.TagListTest0.name],

0 commit comments

Comments
 (0)