Skip to content

Commit

Permalink
Merge pull request #43608 from Expensify/dangrous-moveutilstopolicyutils
Browse files Browse the repository at this point in the history
Shift around utils to deduplicate code without circular dependencies
  • Loading branch information
luacmartins authored Jun 13, 2024
2 parents 06cde0a + 5a4acb6 commit 28672ce
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 85 deletions.
22 changes: 21 additions & 1 deletion src/libs/PolicyUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type {OnyxInputOrEntry, Policy, PolicyCategories, PolicyEmployeeList, PolicyTagList, PolicyTags, TaxRate} from '@src/types/onyx';
import type {PolicyFeatureName, Rate, Tenant} from '@src/types/onyx/Policy';
import type {CustomUnit, PolicyFeatureName, Rate, Tenant} from '@src/types/onyx/Policy';
import type PolicyEmployee from '@src/types/onyx/PolicyEmployee';
import type {EmptyObject} from '@src/types/utils/EmptyObject';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
Expand Down Expand Up @@ -477,6 +477,24 @@ const sortWorkspacesBySelected = (workspace1: WorkspaceDetails, workspace2: Work
return workspace1.name?.toLowerCase().localeCompare(workspace2.name?.toLowerCase() ?? '') ?? 0;
};

/**
* Takes removes pendingFields and errorFields from a customUnit
*/
function removePendingFieldsFromCustomUnit(customUnit: CustomUnit): CustomUnit {
const cleanedCustomUnit = {...customUnit};

delete cleanedCustomUnit.pendingFields;
delete cleanedCustomUnit.errorFields;

return cleanedCustomUnit;
}

function navigateWhenEnableFeature(policyID: string) {
setTimeout(() => {
Navigation.navigate(ROUTES.WORKSPACE_INITIAL.getRoute(policyID));
}, CONST.WORKSPACE_ENABLE_FEATURE_REDIRECT_DELAY);
}

export {
canEditTaxRate,
extractPolicyIDFromPath,
Expand Down Expand Up @@ -531,6 +549,8 @@ export {
getCustomUnit,
getCustomUnitRate,
sortWorkspacesBySelected,
removePendingFieldsFromCustomUnit,
navigateWhenEnableFeature,
};

export type {MemberEmailsToAccountIDs};
3 changes: 2 additions & 1 deletion src/libs/actions/Policy/Category.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import * as ErrorUtils from '@libs/ErrorUtils';
import getIsNarrowLayout from '@libs/getIsNarrowLayout';
import Log from '@libs/Log';
import * as OptionsListUtils from '@libs/OptionsListUtils';
import {navigateWhenEnableFeature, removePendingFieldsFromCustomUnit} from '@libs/PolicyUtils';
import * as ReportUtils from '@libs/ReportUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Policy, PolicyCategories, PolicyCategory, RecentlyUsedCategories, Report} from '@src/types/onyx';
import type {CustomUnit} from '@src/types/onyx/Policy';
import type {OnyxData} from '@src/types/onyx/Request';
import {navigateWhenEnableFeature, removePendingFieldsFromCustomUnit} from './Policy';

const allPolicies: OnyxCollection<Policy> = {};
Onyx.connect({
Expand Down Expand Up @@ -594,4 +594,5 @@ export {
enablePolicyCategories,
setPolicyDistanceRatesDefaultCategory,
deleteWorkspaceCategories,
buildOptimisticPolicyCategories,
};
2 changes: 1 addition & 1 deletion src/libs/actions/Policy/DistanceRate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ import type {
import {READ_COMMANDS, WRITE_COMMANDS} from '@libs/API/types';
import * as ErrorUtils from '@libs/ErrorUtils';
import getIsNarrowLayout from '@libs/getIsNarrowLayout';
import {navigateWhenEnableFeature, removePendingFieldsFromCustomUnit} from '@libs/PolicyUtils';
import * as ReportUtils from '@libs/ReportUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Policy, Report} from '@src/types/onyx';
import type {ErrorFields} from '@src/types/onyx/OnyxCommon';
import type {Attributes, CustomUnit, Rate} from '@src/types/onyx/Policy';
import type {OnyxData} from '@src/types/onyx/Request';
import {navigateWhenEnableFeature, removePendingFieldsFromCustomUnit} from './Policy';

type NewCustomUnit = {
customUnitID: string;
Expand Down
83 changes: 2 additions & 81 deletions src/libs/actions/Policy/Policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,23 @@ import DateUtils from '@libs/DateUtils';
import * as ErrorUtils from '@libs/ErrorUtils';
import getIsNarrowLayout from '@libs/getIsNarrowLayout';
import Log from '@libs/Log';
import Navigation from '@libs/Navigation/Navigation';
import * as NumberUtils from '@libs/NumberUtils';
import * as PhoneNumber from '@libs/PhoneNumber';
import * as PolicyUtils from '@libs/PolicyUtils';
import {navigateWhenEnableFeature} from '@libs/PolicyUtils';
import * as ReportActionsUtils from '@libs/ReportActionsUtils';
import * as ReportUtils from '@libs/ReportUtils';
import * as TransactionUtils from '@libs/TransactionUtils';
import type {PolicySelector} from '@pages/home/sidebar/SidebarScreen/FloatingActionButtonAndPopover';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type {InvitedEmailsToAccountIDs, PersonalDetailsList, Policy, PolicyCategory, ReimbursementAccount, Report, ReportAction, TaxRatesWithDefault, Transaction} from '@src/types/onyx';
import type {Errors} from '@src/types/onyx/OnyxCommon';
import type {Attributes, CompanyAddress, CustomUnit, Rate, TaxRate, Unit} from '@src/types/onyx/Policy';
import type {OnyxData} from '@src/types/onyx/Request';
import type {EmptyObject} from '@src/types/utils/EmptyObject';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
import {buildOptimisticPolicyCategories} from './Category';

type ReportCreationData = Record<
string,
Expand Down Expand Up @@ -1470,65 +1470,6 @@ function createDraftInitialWorkspace(policyOwnerEmail = '', policyName = '', pol
Onyx.update(optimisticData);
}

function buildOptimisticPolicyCategories(policyID: string, categories: readonly string[]) {
const optimisticCategoryMap = categories.reduce<Record<string, Partial<PolicyCategory>>>((acc, category) => {
acc[category] = {
name: category,
enabled: true,
errors: null,
pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD,
};
return acc;
}, {});

const successCategoryMap = categories.reduce<Record<string, Partial<PolicyCategory>>>((acc, category) => {
acc[category] = {
errors: null,
pendingAction: null,
};
return acc;
}, {});

const failureCategoryMap = categories.reduce<Record<string, Partial<PolicyCategory>>>((acc, category) => {
acc[category] = {
errors: ErrorUtils.getMicroSecondOnyxError('workspace.categories.createFailureMessage'),
pendingAction: null,
};
return acc;
}, {});

const onyxData: OnyxData = {
optimisticData: [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${policyID}`,
value: optimisticCategoryMap,
},
{
onyxMethod: Onyx.METHOD.SET,
key: `${ONYXKEYS.COLLECTION.POLICY_CATEGORIES_DRAFT}${policyID}`,
value: null,
},
],
successData: [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${policyID}`,
value: successCategoryMap,
},
],
failureData: [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${policyID}`,
value: failureCategoryMap,
},
],
};

return onyxData;
}

/**
* Generates onyx data for creating a new workspace
*
Expand Down Expand Up @@ -2485,12 +2426,6 @@ function createWorkspaceFromIOUPayment(iouReport: Report | EmptyObject): string
return policyID;
}

function navigateWhenEnableFeature(policyID: string) {
setTimeout(() => {
Navigation.navigate(ROUTES.WORKSPACE_INITIAL.getRoute(policyID));
}, CONST.WORKSPACE_ENABLE_FEATURE_REDIRECT_DELAY);
}

function enablePolicyConnections(policyID: string, enabled: boolean) {
const onyxData: OnyxData = {
optimisticData: [
Expand Down Expand Up @@ -2843,18 +2778,6 @@ function openPolicyMoreFeaturesPage(policyID: string) {
API.read(READ_COMMANDS.OPEN_POLICY_MORE_FEATURES_PAGE, params);
}

/**
* Takes removes pendingFields and errorFields from a customUnit
*/
function removePendingFieldsFromCustomUnit(customUnit: CustomUnit): CustomUnit {
const cleanedCustomUnit = {...customUnit};

delete cleanedCustomUnit.pendingFields;
delete cleanedCustomUnit.errorFields;

return cleanedCustomUnit;
}

function setPolicyCustomTaxName(policyID: string, customTaxName: string) {
const policy = getPolicy(policyID);
const originalCustomTaxName = policy?.taxRates?.name;
Expand Down Expand Up @@ -3072,8 +2995,6 @@ export {
getPrimaryPolicy,
createDraftWorkspace,
buildPolicyData,
navigateWhenEnableFeature,
removePendingFieldsFromCustomUnit,
createPolicyExpenseChats,
};

Expand Down
2 changes: 1 addition & 1 deletion src/libs/actions/Policy/Tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as ErrorUtils from '@libs/ErrorUtils';
import getIsNarrowLayout from '@libs/getIsNarrowLayout';
import Log from '@libs/Log';
import * as PolicyUtils from '@libs/PolicyUtils';
import {navigateWhenEnableFeature} from '@libs/PolicyUtils';
import * as ReportUtils from '@libs/ReportUtils';
import * as TransactionUtils from '@libs/TransactionUtils';
import CONST from '@src/CONST';
Expand All @@ -15,7 +16,6 @@ import type {Policy, PolicyTag, PolicyTagList, PolicyTags, RecentlyUsedTags, Rep
import type {OnyxValueWithOfflineFeedback} from '@src/types/onyx/OnyxCommon';
import type {Attributes, Rate} from '@src/types/onyx/Policy';
import type {OnyxData} from '@src/types/onyx/Request';
import {navigateWhenEnableFeature} from './Policy';

type NewCustomUnit = {
customUnitID: string;
Expand Down

0 comments on commit 28672ce

Please sign in to comment.