Skip to content

Commit

Permalink
Merge pull request #39489 from ZhenjaHorbach/deprecate_policy_members
Browse files Browse the repository at this point in the history
Deprecate POLICY_MEMBERS
  • Loading branch information
luacmartins authored Apr 16, 2024
2 parents dcf14b2 + 905134e commit 7efdde8
Show file tree
Hide file tree
Showing 38 changed files with 369 additions and 538 deletions.
6 changes: 1 addition & 5 deletions src/ONYXKEYS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,9 +312,7 @@ const ONYXKEYS = {
COLLECTION: {
DOWNLOAD: 'download_',
POLICY: 'policy_',
POLICY_MEMBERS: 'policyMembers_',
POLICY_DRAFTS: 'policyDrafts_',
POLICY_MEMBERS_DRAFTS: 'policyMembersDrafts_',
POLICY_JOIN_MEMBER: 'policyJoinMember_',
POLICY_CATEGORIES: 'policyCategories_',
POLICY_RECENTLY_USED_CATEGORIES: 'policyRecentlyUsedCategories_',
Expand Down Expand Up @@ -526,10 +524,8 @@ type OnyxCollectionValuesMapping = {
[ONYXKEYS.COLLECTION.POLICY_DRAFTS]: OnyxTypes.Policy;
[ONYXKEYS.COLLECTION.POLICY_CATEGORIES]: OnyxTypes.PolicyCategories;
[ONYXKEYS.COLLECTION.POLICY_TAGS]: OnyxTypes.PolicyTagList;
[ONYXKEYS.COLLECTION.POLICY_MEMBERS]: OnyxTypes.PolicyMembers;
[ONYXKEYS.COLLECTION.POLICY_MEMBERS_DRAFTS]: OnyxTypes.PolicyMember;
[ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CATEGORIES]: OnyxTypes.RecentlyUsedCategories;
[ONYXKEYS.COLLECTION.DEPRECATED_POLICY_MEMBER_LIST]: OnyxTypes.PolicyMembers;
[ONYXKEYS.COLLECTION.DEPRECATED_POLICY_MEMBER_LIST]: OnyxTypes.PolicyEmployeeList;
[ONYXKEYS.COLLECTION.WORKSPACE_INVITE_MEMBERS_DRAFT]: OnyxTypes.InvitedEmailsToAccountIDs;
[ONYXKEYS.COLLECTION.WORKSPACE_INVITE_MESSAGE_DRAFT]: string;
[ONYXKEYS.COLLECTION.REPORT]: OnyxTypes.Report;
Expand Down
13 changes: 3 additions & 10 deletions src/components/Indicator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,11 @@ import * as PolicyUtils from '@libs/PolicyUtils';
import * as UserUtils from '@libs/UserUtils';
import * as PaymentMethods from '@userActions/PaymentMethods';
import ONYXKEYS from '@src/ONYXKEYS';
import type {BankAccountList, FundList, LoginList, Policy, PolicyMembers, ReimbursementAccount, UserWallet, WalletTerms} from '@src/types/onyx';
import type {BankAccountList, FundList, LoginList, Policy, ReimbursementAccount, UserWallet, WalletTerms} from '@src/types/onyx';

type CheckingMethod = () => boolean;

type IndicatorOnyxProps = {
/** The employee list of all policies (coming from Onyx) */
allPolicyMembers: OnyxCollection<PolicyMembers>;

/** All the user's policies (from Onyx via withFullPolicy) */
policies: OnyxCollection<Policy>;

Expand All @@ -40,14 +37,13 @@ type IndicatorOnyxProps = {

type IndicatorProps = IndicatorOnyxProps;

function Indicator({reimbursementAccount, allPolicyMembers, policies, bankAccountList, fundList, userWallet, walletTerms, loginList}: IndicatorOnyxProps) {
function Indicator({reimbursementAccount, policies, bankAccountList, fundList, userWallet, walletTerms, loginList}: IndicatorOnyxProps) {
const theme = useTheme();
const styles = useThemeStyles();

// If a policy was just deleted from Onyx, then Onyx will pass a null value to the props, and
// those should be cleaned out before doing any error checking
const cleanPolicies = Object.fromEntries(Object.entries(policies ?? {}).filter(([, policy]) => policy?.id));
const cleanAllPolicyMembers = Object.fromEntries(Object.entries(allPolicyMembers ?? {}).filter(([, policyMembers]) => !!policyMembers));

// All of the error & info-checking methods are put into an array. This is so that using _.some() will return
// early as soon as the first error / info condition is returned. This makes the checks very efficient since
Expand All @@ -57,7 +53,7 @@ function Indicator({reimbursementAccount, allPolicyMembers, policies, bankAccoun
() => PaymentMethods.hasPaymentMethodError(bankAccountList, fundList),
() => Object.values(cleanPolicies).some(PolicyUtils.hasPolicyError),
() => Object.values(cleanPolicies).some(PolicyUtils.hasCustomUnitsError),
() => Object.values(cleanAllPolicyMembers).some(PolicyUtils.hasPolicyMemberError),
() => Object.values(cleanPolicies).some(PolicyUtils.hasEmployeeListError),
() => Object.keys(reimbursementAccount?.errors ?? {}).length > 0,
() => !!loginList && UserUtils.hasLoginListError(loginList),

Expand All @@ -77,9 +73,6 @@ function Indicator({reimbursementAccount, allPolicyMembers, policies, bankAccoun
Indicator.displayName = 'Indicator';

export default withOnyx<IndicatorProps, IndicatorOnyxProps>({
allPolicyMembers: {
key: ONYXKEYS.COLLECTION.POLICY_MEMBERS,
},
policies: {
key: ONYXKEYS.COLLECTION.POLICY,
},
Expand Down
21 changes: 10 additions & 11 deletions src/libs/Navigation/AppNavigator/ReportScreenIDSetter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import type {OnyxCollection, OnyxEntry} from 'react-native-onyx';
import {withOnyx} from 'react-native-onyx';
import useActiveWorkspace from '@hooks/useActiveWorkspace';
import usePermissions from '@hooks/usePermissions';
import {getPolicyMembersByIdWithoutCurrentUser} from '@libs/PolicyUtils';
import {getPolicyEmployeeListByIdWithoutCurrentUser} from '@libs/PolicyUtils';
import * as ReportUtils from '@libs/ReportUtils';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Policy, PolicyMembers, Report, ReportMetadata} from '@src/types/onyx';
import type {PersonalDetailsList, Policy, Report, ReportMetadata} from '@src/types/onyx';
import type {ReportScreenWrapperProps} from './ReportScreenWrapper';

type ReportScreenIDSetterComponentProps = {
Expand All @@ -16,8 +16,8 @@ type ReportScreenIDSetterComponentProps = {
/** The policies which the user has access to */
policies: OnyxCollection<Policy>;

/** Members of all the workspaces the user is member of */
policyMembers: OnyxCollection<PolicyMembers>;
/** The personal details of the person who is logged in */
personalDetails: OnyxEntry<PersonalDetailsList>;

/** Whether user is a new user */
isFirstTimeNewExpensifyUser: OnyxEntry<boolean>;
Expand Down Expand Up @@ -58,7 +58,7 @@ const getLastAccessedReportID = (
};

// This wrapper is reponsible for opening the last accessed report if there is no reportID specified in the route params
function ReportScreenIDSetter({route, reports, policies, policyMembers = {}, navigation, isFirstTimeNewExpensifyUser = false, reportMetadata, accountID}: ReportScreenIDSetterProps) {
function ReportScreenIDSetter({route, reports, policies, navigation, isFirstTimeNewExpensifyUser = false, reportMetadata, accountID, personalDetails}: ReportScreenIDSetterProps) {
const {canUseDefaultRooms} = usePermissions();
const {activeWorkspaceID} = useActiveWorkspace();

Expand All @@ -73,7 +73,7 @@ function ReportScreenIDSetter({route, reports, policies, policyMembers = {}, nav
return;
}

const policyMemberAccountIDs = getPolicyMembersByIdWithoutCurrentUser(policyMembers, activeWorkspaceID, accountID);
const policyMemberAccountIDs = getPolicyEmployeeListByIdWithoutCurrentUser(policies, activeWorkspaceID, accountID);

// If there is no reportID in route, try to find last accessed and use it for setParams
const reportID = getLastAccessedReportID(
Expand All @@ -92,7 +92,7 @@ function ReportScreenIDSetter({route, reports, policies, policyMembers = {}, nav
if (reportID) {
navigation.setParams({reportID: String(reportID)});
}
}, [route, navigation, reports, canUseDefaultRooms, policies, isFirstTimeNewExpensifyUser, reportMetadata, activeWorkspaceID, policyMembers, accountID]);
}, [route, navigation, reports, canUseDefaultRooms, policies, isFirstTimeNewExpensifyUser, reportMetadata, activeWorkspaceID, personalDetails, accountID]);

// The ReportScreen without the reportID set will display a skeleton
// until the reportID is loaded and set in the route param
Expand All @@ -110,10 +110,6 @@ export default withOnyx<ReportScreenIDSetterProps, ReportScreenIDSetterComponent
key: ONYXKEYS.COLLECTION.POLICY,
allowStaleData: true,
},
policyMembers: {
key: ONYXKEYS.COLLECTION.POLICY_MEMBERS,
allowStaleData: true,
},
isFirstTimeNewExpensifyUser: {
key: ONYXKEYS.NVP_IS_FIRST_TIME_NEW_EXPENSIFY_USER,
initialValue: false,
Expand All @@ -126,4 +122,7 @@ export default withOnyx<ReportScreenIDSetterProps, ReportScreenIDSetterComponent
key: ONYXKEYS.SESSION,
selector: (session) => session?.accountID,
},
personalDetails: {
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
},
})(ReportScreenIDSetter);
4 changes: 2 additions & 2 deletions src/libs/Navigation/dismissModalWithReport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type {NavigationContainerRef} from '@react-navigation/native';
import {StackActions} from '@react-navigation/native';
import {findLastIndex} from 'lodash';
import Log from '@libs/Log';
import getPolicyMemberAccountIDs from '@libs/PolicyMembersUtils';
import getPolicyEmployeeAccountIDs from '@libs/PolicyEmployeeListUtils';
import {doesReportBelongToWorkspace} from '@libs/ReportUtils';
import NAVIGATORS from '@src/NAVIGATORS';
import ROUTES from '@src/ROUTES';
Expand Down Expand Up @@ -47,7 +47,7 @@ function dismissModalWithReport(targetReport: Report | EmptyObject, navigationRe
if (targetReport.reportID !== getTopmostReportId(state)) {
const reportState = getStateFromPath(ROUTES.REPORT_WITH_ID.getRoute(targetReport.reportID));
const policyID = getPolicyIDFromState(state as State<RootStackParamList>);
const policyMemberAccountIDs = getPolicyMemberAccountIDs(policyID);
const policyMemberAccountIDs = getPolicyEmployeeAccountIDs(policyID);
const shouldOpenAllWorkspace = isEmptyObject(targetReport) ? true : !doesReportBelongToWorkspace(targetReport, policyMemberAccountIDs, policyID);

if (shouldOpenAllWorkspace) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as OnyxUpdates from '@libs/actions/OnyxUpdates';
import * as ActiveClientManager from '@libs/ActiveClientManager';
import Log from '@libs/Log';
import Navigation from '@libs/Navigation/Navigation';
import getPolicyMemberAccountIDs from '@libs/PolicyMembersUtils';
import getPolicyEmployeeAccountIDs from '@libs/PolicyEmployeeListUtils';
import {extractPolicyIDFromPath} from '@libs/PolicyUtils';
import {doesReportBelongToWorkspace, getReport} from '@libs/ReportUtils';
import Visibility from '@libs/Visibility';
Expand Down Expand Up @@ -65,9 +65,9 @@ export default function subscribeToReportCommentPushNotifications() {

const policyID = lastVisitedPath && extractPolicyIDFromPath(lastVisitedPath);
const report = getReport(reportID.toString());
const policyMembersAccountIDs = policyID ? getPolicyMemberAccountIDs(policyID) : [];
const policyEmployeeAccountIDs = policyID ? getPolicyEmployeeAccountIDs(policyID) : [];

const reportBelongsToWorkspace = policyID && !isEmptyObject(report) && doesReportBelongToWorkspace(report, policyMembersAccountIDs, policyID);
const reportBelongsToWorkspace = policyID && !isEmptyObject(report) && doesReportBelongToWorkspace(report, policyEmployeeAccountIDs, policyID);

Log.info('[PushNotification] onSelected() - called', false, {reportID, reportActionID});
Navigation.isNavigationReady()
Expand Down
25 changes: 25 additions & 0 deletions src/libs/PolicyEmployeeListUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import type {OnyxCollection} from 'react-native-onyx';
import Onyx from 'react-native-onyx';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Policy} from '@src/types/onyx';
import {getCurrentUserAccountID} from './actions/Report';
import {getPolicyEmployeeListByIdWithoutCurrentUser} from './PolicyUtils';

let allPolicies: OnyxCollection<Policy> = {};
Onyx.connect({
key: ONYXKEYS.COLLECTION.POLICY,
waitForCollectionCallback: true,
callback: (value) => (allPolicies = value),
});

function getPolicyEmployeeAccountIDs(policyID?: string) {
if (!policyID) {
return [];
}

const currentUserAccountID = getCurrentUserAccountID();

return getPolicyEmployeeListByIdWithoutCurrentUser(allPolicies, policyID, currentUserAccountID);
}

export default getPolicyEmployeeAccountIDs;
25 changes: 0 additions & 25 deletions src/libs/PolicyMembersUtils.ts

This file was deleted.

Loading

0 comments on commit 7efdde8

Please sign in to comment.