Skip to content

Commit

Permalink
Merge pull request #39344 from nkdengineer/fix/38959
Browse files Browse the repository at this point in the history
Remove deprecated ReportUtils.getPolicy() method
  • Loading branch information
tgolen authored Apr 3, 2024
2 parents 255f841 + 5eb8ee2 commit 7cdb17b
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 37 deletions.
10 changes: 8 additions & 2 deletions src/components/SettlementButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import ONYXKEYS from '@src/ONYXKEYS';
import type {Route} from '@src/ROUTES';
import ROUTES from '@src/ROUTES';
import type {ButtonSizeValue} from '@src/styles/utils/types';
import type {LastPaymentMethod, Report} from '@src/types/onyx';
import type {LastPaymentMethod, Policy, Report} from '@src/types/onyx';
import type {PaymentMethodType} from '@src/types/onyx/OriginalMessage';
import type AnchorAlignment from '@src/types/utils/AnchorAlignment';
import type {EmptyObject} from '@src/types/utils/EmptyObject';
Expand All @@ -33,6 +33,9 @@ type EnablePaymentsRoute = typeof ROUTES.ENABLE_PAYMENTS | typeof ROUTES.IOU_SEN
type SettlementButtonOnyxProps = {
/** The last payment method used per policy */
nvpLastPaymentMethod?: OnyxEntry<LastPaymentMethod>;

/** The policy of the report */
policy: OnyxEntry<Policy>;
};

type SettlementButtonProps = SettlementButtonOnyxProps & {
Expand Down Expand Up @@ -135,6 +138,7 @@ function SettlementButton({
shouldShowPersonalBankAccountOption = false,
enterKeyEventListenerPriority = 0,
confirmApproval,
policy,
}: SettlementButtonProps) {
const {translate} = useLocalize();
const {isOffline} = useNetwork();
Expand All @@ -143,7 +147,6 @@ function SettlementButton({
PaymentMethods.openWalletPage();
}, []);

const policy = ReportUtils.getPolicy(policyID);
const session = useSession();
const chatReport = ReportUtils.getReport(chatReportID);
const isPaidGroupPolicy = ReportUtils.isPaidGroupPolicyExpenseChat(chatReport as OnyxEntry<Report>);
Expand Down Expand Up @@ -272,4 +275,7 @@ export default withOnyx<SettlementButtonProps, SettlementButtonOnyxProps>({
key: ONYXKEYS.NVP_LAST_PAYMENT_METHOD,
selector: (paymentMethod) => paymentMethod ?? {},
},
policy: {
key: ({policyID}) => `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
},
})(SettlementButton);
12 changes: 10 additions & 2 deletions src/libs/NextStepUtils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import {format, lastDayOfMonth, setDate} from 'date-fns';
import Str from 'expensify-common/lib/str';
import Onyx from 'react-native-onyx';
import type {OnyxCollection} from 'react-native-onyx';
import type {ValueOf} from 'type-fest';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Report, ReportNextStep} from '@src/types/onyx';
import type {Policy, Report, ReportNextStep} from '@src/types/onyx';
import type {Message} from '@src/types/onyx/ReportNextStep';
import type DeepValueOf from '@src/types/utils/DeepValueOf';
import type {EmptyObject} from '@src/types/utils/EmptyObject';
Expand All @@ -25,6 +26,13 @@ Onyx.connect({
},
});

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

function parseMessage(messages: Message[] | undefined) {
let nextStepHTML = '';

Expand Down Expand Up @@ -72,7 +80,7 @@ function buildNextStep(
}

const {policyID = '', ownerAccountID = -1, managerID = -1} = report;
const policy = ReportUtils.getPolicy(policyID);
const policy = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`] ?? ({} as Policy);
const {submitsTo, harvesting, isPreventSelfApprovalEnabled, preventSelfApproval, autoReportingFrequency, autoReportingOffset} = policy;
const isOwner = currentUserAccountID === ownerAccountID;
const isManager = currentUserAccountID === managerID;
Expand Down
3 changes: 1 addition & 2 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ function getRootParentReport(report: OnyxEntry<Report> | undefined | EmptyObject
}

/**
* @deprecated Use withOnyx or Onyx.connect() instead
* Returns the policy of the report
*/
function getPolicy(policyID: string | undefined): Policy | EmptyObject {
if (!allPolicies || !policyID) {
Expand Down Expand Up @@ -5845,7 +5845,6 @@ export {
getReportOfflinePendingActionAndErrors,
isDM,
isSelfDM,
getPolicy,
getWorkspaceChats,
shouldDisableRename,
hasSingleParticipant,
Expand Down
26 changes: 21 additions & 5 deletions src/libs/actions/IOU.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,23 @@ Onyx.connect({
},
});

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

/**
* Returns the policy of the report
*/
function getPolicy(policyID: string | undefined): OnyxTypes.Policy | EmptyObject {
if (!allPolicies || !policyID) {
return {};
}
return allPolicies[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`] ?? {};
}

/**
* Initialize money request info
* @param reportID to attach the transaction to
Expand Down Expand Up @@ -437,7 +454,7 @@ function needsToBeManuallySubmitted(iouReport: OnyxTypes.Report) {
const isPolicyExpenseChat = ReportUtils.isExpenseReport(iouReport);

if (isPolicyExpenseChat) {
const policy = ReportUtils.getPolicy(iouReport.policyID);
const policy = getPolicy(iouReport.policyID);
const isFromPaidPolicy = PolicyUtils.isPaidGroupPolicy(policy);

// If the scheduled submit is turned off on the policy, user needs to manually submit the report which is indicated by GBR in LHN
Expand Down Expand Up @@ -4647,8 +4664,7 @@ function hasIOUToApproveOrPay(chatReport: OnyxEntry<OnyxTypes.Report> | EmptyObj

return Object.values(chatReportActions).some((action) => {
const iouReport = ReportUtils.getReport(action.childReportID ?? '');
const policy = ReportUtils.getPolicy(iouReport?.policyID);

const policy = getPolicy(iouReport?.policyID);
const shouldShowSettlementButton = canIOUBePaid(iouReport, chatReport, policy) || canApproveIOU(iouReport, chatReport, policy);
return action.childReportID?.toString() !== excludedIOUReportID && action.actionName === CONST.REPORT.ACTIONS.TYPE.REPORTPREVIEW && shouldShowSettlementButton;
});
Expand Down Expand Up @@ -4764,7 +4780,7 @@ function submitReport(expenseReport: OnyxTypes.Report) {
const currentNextStep = allNextSteps[`${ONYXKEYS.COLLECTION.NEXT_STEP}${expenseReport.reportID}`] ?? null;
const optimisticSubmittedReportAction = ReportUtils.buildOptimisticSubmittedReportAction(expenseReport?.total ?? 0, expenseReport.currency ?? '', expenseReport.reportID);
const parentReport = ReportUtils.getReport(expenseReport.parentReportID);
const policy = ReportUtils.getPolicy(expenseReport.policyID);
const policy = getPolicy(expenseReport.policyID);
const isCurrentUserManager = currentUserPersonalDetails.accountID === expenseReport.managerID;
const optimisticNextStep = NextStepUtils.buildNextStep(expenseReport, CONST.REPORT.STATUS_NUM.SUBMITTED);
const isSubmitAndClosePolicy = PolicyUtils.isSubmitAndClose(policy);
Expand Down Expand Up @@ -4887,7 +4903,7 @@ function submitReport(expenseReport: OnyxTypes.Report) {

function cancelPayment(expenseReport: OnyxTypes.Report, chatReport: OnyxTypes.Report) {
const optimisticReportAction = ReportUtils.buildOptimisticCancelPaymentReportAction(expenseReport.reportID, -(expenseReport.total ?? 0), expenseReport.currency ?? '');
const policy = ReportUtils.getPolicy(chatReport.policyID);
const policy = getPolicy(chatReport.policyID);
const isFree = policy && policy.type === CONST.POLICY.TYPE.FREE;
const approvalMode = policy.approvalMode ?? CONST.POLICY.APPROVAL_MODE.BASIC;
let stateNum: ValueOf<typeof CONST.REPORT.STATE_NUM> = CONST.REPORT.STATE_NUM.SUBMITTED;
Expand Down
37 changes: 24 additions & 13 deletions src/libs/actions/Policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,16 @@ function isCurrencySupportedForDirectReimbursement(currency: string) {
return currency === CONST.CURRENCY.USD;
}

/**
* Returns the policy of the report
*/
function getPolicy(policyID: string | undefined): Policy | EmptyObject {
if (!allPolicies || !policyID) {
return {};
}
return allPolicies[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`] ?? {};
}

/**
* Check if the user has any active free policies (aka workspaces)
*/
Expand Down Expand Up @@ -484,7 +494,7 @@ function buildAnnounceRoomMembersOnyxData(policyID: string, accountIDs: number[]
}

function setWorkspaceAutoReporting(policyID: string, enabled: boolean, frequency: ValueOf<typeof CONST.POLICY.AUTO_REPORTING_FREQUENCIES>) {
const policy = ReportUtils.getPolicy(policyID);
const policy = getPolicy(policyID);
const optimisticData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.MERGE,
Expand Down Expand Up @@ -532,7 +542,7 @@ function setWorkspaceAutoReporting(policyID: string, enabled: boolean, frequency
}

function setWorkspaceAutoReportingFrequency(policyID: string, frequency: ValueOf<typeof CONST.POLICY.AUTO_REPORTING_FREQUENCIES>) {
const policy = ReportUtils.getPolicy(policyID);
const policy = getPolicy(policyID);

const optimisticData: OnyxUpdate[] = [
{
Expand Down Expand Up @@ -573,7 +583,7 @@ function setWorkspaceAutoReportingFrequency(policyID: string, frequency: ValueOf

function setWorkspaceAutoReportingMonthlyOffset(policyID: string, autoReportingOffset: number | ValueOf<typeof CONST.POLICY.AUTO_REPORTING_OFFSET>) {
const value = JSON.stringify({autoReportingOffset: autoReportingOffset.toString()});
const policy = ReportUtils.getPolicy(policyID);
const policy = getPolicy(policyID);

const optimisticData: OnyxUpdate[] = [
{
Expand Down Expand Up @@ -613,7 +623,7 @@ function setWorkspaceAutoReportingMonthlyOffset(policyID: string, autoReportingO
}

function setWorkspaceApprovalMode(policyID: string, approver: string, approvalMode: ValueOf<typeof CONST.POLICY.APPROVAL_MODE>) {
const policy = ReportUtils.getPolicy(policyID);
const policy = getPolicy(policyID);

const value = {
approver,
Expand Down Expand Up @@ -666,7 +676,7 @@ function setWorkspaceApprovalMode(policyID: string, approver: string, approvalMo
}

function setWorkspacePayer(policyID: string, reimburserEmail: string, reimburserAccountID: number) {
const policy = ReportUtils.getPolicy(policyID);
const policy = getPolicy(policyID);

const optimisticData: OnyxUpdate[] = [
{
Expand Down Expand Up @@ -715,7 +725,7 @@ function clearPolicyErrorField(policyID: string, fieldName: string) {
}

function setWorkspaceReimbursement(policyID: string, reimbursementChoice: ValueOf<typeof CONST.POLICY.REIMBURSEMENT_CHOICES>, reimburserAccountID: number, reimburserEmail: string) {
const policy = ReportUtils.getPolicy(policyID);
const policy = getPolicy(policyID);

const optimisticData: OnyxUpdate[] = [
{
Expand Down Expand Up @@ -822,7 +832,8 @@ function removeMembers(accountIDs: number[], policyID: string) {
}

const membersListKey = `${ONYXKEYS.COLLECTION.POLICY_MEMBERS}${policyID}` as const;
const policy = ReportUtils.getPolicy(policyID);
const policy = getPolicy(policyID);

const workspaceChats = ReportUtils.getWorkspaceChats(policyID, accountIDs);
const optimisticClosedReportActions = workspaceChats.map(() => ReportUtils.buildOptimisticClosedReportAction(sessionEmail, policy.name, CONST.REPORT.ARCHIVE_REASON.REMOVED_FROM_POLICY));

Expand Down Expand Up @@ -1023,7 +1034,7 @@ function updateWorkspaceMembersRole(policyID: string, accountIDs: number[], newR
}

function requestWorkspaceOwnerChange(policyID: string) {
const policy = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`] ?? ({} as Policy);
const policy = getPolicy(policyID);
const ownershipChecks = {...policyOwnershipChecks?.[policyID]} ?? {};

const changeOwnerErrors = Object.keys(policy?.errorFields?.changeOwner ?? {});
Expand Down Expand Up @@ -3985,7 +3996,7 @@ function enablePolicyTaxes(policyID: string, enabled: boolean) {
},
],
};
const policy = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`];
const policy = getPolicy(policyID);
const shouldAddDefaultTaxRatesData = (!policy?.taxRates || isEmptyObject(policy.taxRates)) && enabled;
const onyxData: OnyxData = {
optimisticData: [
Expand Down Expand Up @@ -4044,7 +4055,7 @@ function enablePolicyTaxes(policyID: string, enabled: boolean) {
}

function enablePolicyWorkflows(policyID: string, enabled: boolean) {
const policy = ReportUtils.getPolicy(policyID);
const policy = getPolicy(policyID);
const onyxData: OnyxData = {
optimisticData: [
{
Expand Down Expand Up @@ -4696,7 +4707,7 @@ function deletePolicyDistanceRates(policyID: string, customUnit: CustomUnit, rat
}

function setPolicyCustomTaxName(policyID: string, customTaxName: string) {
const policy = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`];
const policy = getPolicy(policyID);
const originalCustomTaxName = policy?.taxRates?.name;
const onyxData: OnyxData = {
optimisticData: [
Expand Down Expand Up @@ -4748,7 +4759,7 @@ function setPolicyCustomTaxName(policyID: string, customTaxName: string) {
}

function setWorkspaceCurrencyDefault(policyID: string, taxCode: string) {
const policy = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`];
const policy = getPolicy(policyID);
const originalDefaultExternalID = policy?.taxRates?.defaultExternalID;
const onyxData: OnyxData = {
optimisticData: [
Expand Down Expand Up @@ -4800,7 +4811,7 @@ function setWorkspaceCurrencyDefault(policyID: string, taxCode: string) {
}

function setForeignCurrencyDefault(policyID: string, taxCode: string) {
const policy = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`];
const policy = getPolicy(policyID);
const originalDefaultForeignCurrencyID = policy?.taxRates?.foreignTaxDefault;
const onyxData: OnyxData = {
optimisticData: [
Expand Down
23 changes: 10 additions & 13 deletions src/pages/workspace/WorkspaceJoinUserPage.tsx
Original file line number Diff line number Diff line change
@@ -1,36 +1,34 @@
import type {StackScreenProps} from '@react-navigation/stack';
import React, {useEffect, useRef} from 'react';
import {withOnyx} from 'react-native-onyx';
import type {OnyxCollection} from 'react-native-onyx';
import type {OnyxEntry} from 'react-native-onyx';
import FullScreenLoadingIndicator from '@components/FullscreenLoadingIndicator';
import ScreenWrapper from '@components/ScreenWrapper';
import useThemeStyles from '@hooks/useThemeStyles';
import navigateAfterJoinRequest from '@libs/navigateAfterJoinRequest';
import * as PolicyUtils from '@libs/PolicyUtils';
import * as ReportUtils from '@libs/ReportUtils';
import Navigation from '@navigation/Navigation';
import type {AuthScreensParamList} from '@navigation/types';
import * as PolicyAction from '@userActions/Policy';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type SCREENS from '@src/SCREENS';
import type {Policy} from '@src/types/onyx';
import {isEmptyObject} from '@src/types/utils/EmptyObject';

type WorkspaceJoinUserPageOnyxProps = {
/** The list of this user's policies */
policies: OnyxCollection<Policy>;
/** The policy of the report */
policy: OnyxEntry<Policy>;
};

type WorkspaceJoinUserPageRoute = {route: StackScreenProps<AuthScreensParamList, typeof SCREENS.WORKSPACE_JOIN_USER>['route']};
type WorkspaceJoinUserPageProps = WorkspaceJoinUserPageRoute & WorkspaceJoinUserPageOnyxProps;

let isJoinLinkUsed = false;

function WorkspaceJoinUserPage({route, policies}: WorkspaceJoinUserPageProps) {
function WorkspaceJoinUserPage({route, policy}: WorkspaceJoinUserPageProps) {
const styles = useThemeStyles();
const policyID = route?.params?.policyID;
const inviterEmail = route?.params?.email;
const policy = ReportUtils.getPolicy(policyID);
const isUnmounted = useRef(false);

useEffect(() => {
Expand All @@ -41,11 +39,10 @@ function WorkspaceJoinUserPage({route, policies}: WorkspaceJoinUserPageProps) {
}, []);

useEffect(() => {
if (!policy || !policies || isUnmounted.current || isJoinLinkUsed) {
if (!policy || isUnmounted.current || isJoinLinkUsed) {
return;
}
const isPolicyMember = PolicyUtils.isPolicyMember(policyID, policies as Record<string, Policy>);
if (isPolicyMember) {
if (!isEmptyObject(policy)) {
Navigation.isNavigationReady().then(() => {
Navigation.goBack(undefined, false, true);
Navigation.navigate(ROUTES.WORKSPACE_INITIAL.getRoute(policyID ?? ''));
Expand All @@ -60,7 +57,7 @@ function WorkspaceJoinUserPage({route, policies}: WorkspaceJoinUserPageProps) {
}
navigateAfterJoinRequest();
});
}, [policy, policyID, policies, inviterEmail]);
}, [policy, policyID, inviterEmail]);

useEffect(
() => () => {
Expand All @@ -78,7 +75,7 @@ function WorkspaceJoinUserPage({route, policies}: WorkspaceJoinUserPageProps) {

WorkspaceJoinUserPage.displayName = 'WorkspaceJoinUserPage';
export default withOnyx<WorkspaceJoinUserPageProps, WorkspaceJoinUserPageOnyxProps>({
policies: {
key: ONYXKEYS.COLLECTION.POLICY,
policy: {
key: ({route}) => `${ONYXKEYS.COLLECTION.POLICY}${route?.params?.policyID}`,
},
})(WorkspaceJoinUserPage);
Loading

0 comments on commit 7cdb17b

Please sign in to comment.