Skip to content

Commit

Permalink
Merge pull request #52607 from mkzie2/mkzie2-issue/52397
Browse files Browse the repository at this point in the history
Add upgrade flow for report field feature in qbo/xero
  • Loading branch information
yuwenmemon authored Nov 18, 2024
2 parents 3a59027 + 5d6dfa3 commit 8e1b0fd
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 18 deletions.
10 changes: 10 additions & 0 deletions src/CONST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6087,6 +6087,16 @@ const CONST = {
ILLUSTRATION: 'illustration',
VIDEO: 'video',
},
REPORT_FIELDS_FEATURE: {
qbo: {
classes: 'report-fields-qbo-classes',
customers: 'report-fields-qbo-customers',
locations: 'report-fields-qbo-locations',
},
xero: {
mapping: 'report-fields-mapping',
},
},
get UPGRADE_FEATURE_INTRO_MAPPING() {
return {
reportFields: {
Expand Down
1 change: 1 addition & 0 deletions src/libs/Navigation/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ type SettingsNavigatorParamList = {
policyID: string;
featureName: string;
backTo?: Routes;
categoryId?: string;
};
[SCREENS.WORKSPACE.DOWNGRADE]: {
policyID: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import useThemeStyles from '@hooks/useThemeStyles';
import * as QuickbooksOnline from '@libs/actions/connections/QuickbooksOnline';
import * as ErrorUtils from '@libs/ErrorUtils';
import Navigation from '@libs/Navigation/Navigation';
import {settingsPendingAction} from '@libs/PolicyUtils';
import {isControlPolicy, settingsPendingAction} from '@libs/PolicyUtils';
import type {WithPolicyProps} from '@pages/workspace/withPolicy';
import withPolicyConnections from '@pages/workspace/withPolicyConnections';
import {clearQBOErrorField} from '@userActions/Policy/Policy';
Expand Down Expand Up @@ -45,11 +45,17 @@ function QuickbooksClassesDisplayedAsPage({policy}: WithPolicyProps) {
const selectDisplayedAs = useCallback(
(row: CardListItem) => {
if (row.value !== qboConfig?.syncClasses) {
if (row.value === CONST.INTEGRATION_ENTITY_MAP_TYPES.REPORT_FIELD && !isControlPolicy(policy)) {
Navigation.navigate(
ROUTES.WORKSPACE_UPGRADE.getRoute(policyID, CONST.REPORT_FIELDS_FEATURE.qbo.classes, ROUTES.POLICY_ACCOUNTING_QUICKBOOKS_ONLINE_CLASSES.getRoute(policyID)),
);
return;
}
QuickbooksOnline.updateQuickbooksOnlineSyncClasses(policyID, row.value, qboConfig?.syncClasses);
}
Navigation.goBack(ROUTES.POLICY_ACCOUNTING_QUICKBOOKS_ONLINE_CLASSES.getRoute(policyID));
},
[qboConfig?.syncClasses, policyID],
[qboConfig?.syncClasses, policyID, policy],
);

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import useThemeStyles from '@hooks/useThemeStyles';
import * as QuickbooksOnline from '@libs/actions/connections/QuickbooksOnline';
import * as ErrorUtils from '@libs/ErrorUtils';
import Navigation from '@libs/Navigation/Navigation';
import {settingsPendingAction} from '@libs/PolicyUtils';
import {isControlPolicy, settingsPendingAction} from '@libs/PolicyUtils';
import type {WithPolicyProps} from '@pages/workspace/withPolicy';
import withPolicyConnections from '@pages/workspace/withPolicyConnections';
import {clearQBOErrorField} from '@userActions/Policy/Policy';
Expand Down Expand Up @@ -45,11 +45,17 @@ function QuickbooksCustomersDisplayedAsPage({policy}: WithPolicyProps) {
const selectDisplayedAs = useCallback(
(row: CardListItem) => {
if (row.value !== qboConfig?.syncCustomers) {
if (row.value === CONST.INTEGRATION_ENTITY_MAP_TYPES.REPORT_FIELD && !isControlPolicy(policy)) {
Navigation.navigate(
ROUTES.WORKSPACE_UPGRADE.getRoute(policyID, CONST.REPORT_FIELDS_FEATURE.qbo.customers, ROUTES.POLICY_ACCOUNTING_QUICKBOOKS_ONLINE_CUSTOMERS.getRoute(policyID)),
);
return;
}
QuickbooksOnline.updateQuickbooksOnlineSyncCustomers(policyID, row.value, qboConfig?.syncCustomers);
}
Navigation.goBack(ROUTES.POLICY_ACCOUNTING_QUICKBOOKS_ONLINE_CUSTOMERS.getRoute(policyID));
},
[qboConfig?.syncCustomers, policyID],
[qboConfig?.syncCustomers, policyID, policy],
);

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import useThemeStyles from '@hooks/useThemeStyles';
import * as QuickbooksOnline from '@libs/actions/connections/QuickbooksOnline';
import * as ErrorUtils from '@libs/ErrorUtils';
import Navigation from '@libs/Navigation/Navigation';
import {settingsPendingAction} from '@libs/PolicyUtils';
import {isControlPolicy, settingsPendingAction} from '@libs/PolicyUtils';
import type {WithPolicyProps} from '@pages/workspace/withPolicy';
import withPolicyConnections from '@pages/workspace/withPolicyConnections';
import {clearQBOErrorField} from '@userActions/Policy/Policy';
Expand Down Expand Up @@ -52,11 +52,17 @@ function QuickbooksLocationsDisplayedAsPage({policy}: WithPolicyProps) {
const selectDisplayedAs = useCallback(
(row: CardListItem) => {
if (row.value !== qboConfig?.syncLocations) {
if (row.value === CONST.INTEGRATION_ENTITY_MAP_TYPES.REPORT_FIELD && !isControlPolicy(policy)) {
Navigation.navigate(
ROUTES.WORKSPACE_UPGRADE.getRoute(policyID, CONST.REPORT_FIELDS_FEATURE.qbo.locations, ROUTES.POLICY_ACCOUNTING_QUICKBOOKS_ONLINE_LOCATIONS.getRoute(policyID)),
);
return;
}
QuickbooksOnline.updateQuickbooksOnlineSyncLocations(policyID, row.value, qboConfig?.syncLocations);
}
Navigation.goBack(ROUTES.POLICY_ACCOUNTING_QUICKBOOKS_ONLINE_LOCATIONS.getRoute(policyID));
},
[qboConfig?.syncLocations, policyID],
[qboConfig?.syncLocations, policyID, policy],
);

return (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {useEffect} from 'react';
import React, {useCallback, useEffect} from 'react';
import {View} from 'react-native';
import ConnectionLayout from '@components/ConnectionLayout';
import MenuItemWithTopDescription from '@components/MenuItemWithTopDescription';
Expand All @@ -17,6 +17,7 @@ import ToggleSettingOptionRow from '@pages/workspace/workflows/ToggleSettingsOpt
import {clearQBOErrorField} from '@userActions/Policy/Policy';
import CONST from '@src/CONST';
import ROUTES from '@src/ROUTES';
import type {IntegrationEntityMap} from '@src/types/onyx/Policy';

function QuickbooksLocationsPage({policy}: WithPolicyProps) {
const {translate} = useLocalize();
Expand All @@ -27,13 +28,25 @@ function QuickbooksLocationsPage({policy}: WithPolicyProps) {
const isTagsSelected = qboConfig?.syncLocations === CONST.INTEGRATION_ENTITY_MAP_TYPES.TAG;
const shouldShowLineItemsRestriction = shouldShowLocationsLineItemsRestriction(qboConfig);

const updateQuickbooksOnlineSyncLocations = useCallback(
(settingValue: IntegrationEntityMap) => {
if (settingValue === CONST.INTEGRATION_ENTITY_MAP_TYPES.REPORT_FIELD && !PolicyUtils.isControlPolicy(policy)) {
Navigation.navigate(
ROUTES.WORKSPACE_UPGRADE.getRoute(policyID, CONST.REPORT_FIELDS_FEATURE.qbo.locations, ROUTES.POLICY_ACCOUNTING_QUICKBOOKS_ONLINE_LOCATIONS.getRoute(policyID)),
);
return;
}
QuickbooksOnline.updateQuickbooksOnlineSyncLocations(policyID, settingValue, qboConfig?.syncLocations);
},
[policy, policyID, qboConfig?.syncLocations],
);
// If we previously selected tags but now we have the line items restriction, we need to switch to report fields
useEffect(() => {
if (!shouldSwitchLocationsToReportFields(qboConfig)) {
return;
}
QuickbooksOnline.updateQuickbooksOnlineSyncLocations(policyID, CONST.INTEGRATION_ENTITY_MAP_TYPES.REPORT_FIELD, qboConfig?.syncLocations);
}, [qboConfig, policyID]);
updateQuickbooksOnlineSyncLocations(CONST.INTEGRATION_ENTITY_MAP_TYPES.REPORT_FIELD);
}, [qboConfig, updateQuickbooksOnlineSyncLocations]);

return (
<ConnectionLayout
Expand All @@ -52,15 +65,13 @@ function QuickbooksLocationsPage({policy}: WithPolicyProps) {
switchAccessibilityLabel={translate('workspace.qbo.locations')}
isActive={isSwitchOn}
onToggle={() =>
QuickbooksOnline.updateQuickbooksOnlineSyncLocations(
policyID,
updateQuickbooksOnlineSyncLocations(
// eslint-disable-next-line no-nested-ternary
isSwitchOn
? CONST.INTEGRATION_ENTITY_MAP_TYPES.NONE
: shouldShowLineItemsRestriction
? CONST.INTEGRATION_ENTITY_MAP_TYPES.REPORT_FIELD
: CONST.INTEGRATION_ENTITY_MAP_TYPES.TAG,
qboConfig?.syncLocations,
)
}
errors={ErrorUtils.getLatestErrorField(qboConfig, CONST.QUICKBOOKS_CONFIG.SYNC_LOCATIONS)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import useThemeStyles from '@hooks/useThemeStyles';
import * as Xero from '@libs/actions/connections/Xero';
import * as ErrorUtils from '@libs/ErrorUtils';
import Navigation from '@libs/Navigation/Navigation';
import {settingsPendingAction} from '@libs/PolicyUtils';
import {isControlPolicy, settingsPendingAction} from '@libs/PolicyUtils';
import type {WithPolicyProps} from '@pages/workspace/withPolicy';
import withPolicyConnections from '@pages/workspace/withPolicyConnections';
import * as Policy from '@userActions/Policy/Policy';
Expand Down Expand Up @@ -60,6 +60,15 @@ function XeroMapTrackingCategoryConfigurationPage({policy}: WithPolicyProps) {
const updateMapping = useCallback(
(option: {value: string}) => {
if (option.value !== categoryName) {
if (option.value === CONST.XERO_CONFIG.TRACKING_CATEGORY_OPTIONS.REPORT_FIELD && !isControlPolicy(policy)) {
const backToRoute = ROUTES.WORKSPACE_UPGRADE.getRoute(
policyID,
`${CONST.REPORT_FIELDS_FEATURE.xero.mapping}`,
ROUTES.POLICY_ACCOUNTING_XERO_TRACKING_CATEGORIES.getRoute(policyID),
);
Navigation.navigate(`${backToRoute}&categoryId=${categoryId}`);
return;
}
Xero.updateXeroMappings(
policyID,
categoryId ? {[`${CONST.XERO_CONFIG.TRACKING_CATEGORY_PREFIX}${categoryId}`]: option.value} : {},
Expand All @@ -68,7 +77,7 @@ function XeroMapTrackingCategoryConfigurationPage({policy}: WithPolicyProps) {
}
Navigation.goBack(ROUTES.POLICY_ACCOUNTING_XERO_TRACKING_CATEGORIES.getRoute(policyID));
},
[categoryId, categoryName, currentTrackingCategoryValue, policyID],
[categoryId, categoryName, currentTrackingCategoryValue, policy, policyID],
);

return (
Expand Down
76 changes: 72 additions & 4 deletions src/pages/workspace/upgrade/WorkspaceUpgradePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import ScreenWrapper from '@components/ScreenWrapper';
import useLocalize from '@hooks/useLocalize';
import useNetwork from '@hooks/useNetwork';
import useThemeStyles from '@hooks/useThemeStyles';
import * as QuickbooksOnline from '@libs/actions/connections/QuickbooksOnline';
import * as Xero from '@libs/actions/connections/Xero';
import Navigation from '@libs/Navigation/Navigation';
import type {SettingsNavigatorParamList} from '@libs/Navigation/types';
import * as PolicyUtils from '@libs/PolicyUtils';
Expand All @@ -21,19 +23,37 @@ import UpgradeIntro from './UpgradeIntro';

type WorkspaceUpgradePageProps = StackScreenProps<SettingsNavigatorParamList, typeof SCREENS.WORKSPACE.UPGRADE>;

function getFeatureNameAlias(featureName: string) {
switch (featureName) {
case CONST.REPORT_FIELDS_FEATURE.qbo.classes:
case CONST.REPORT_FIELDS_FEATURE.qbo.customers:
case CONST.REPORT_FIELDS_FEATURE.qbo.locations:
case CONST.REPORT_FIELDS_FEATURE.xero.mapping:
return CONST.UPGRADE_FEATURE_INTRO_MAPPING.reportFields.alias;
default: {
return featureName;
}
}
}

function WorkspaceUpgradePage({route}: WorkspaceUpgradePageProps) {
const navigation = useNavigation();
const styles = useThemeStyles();
const policyID = route.params.policyID;
const feature = Object.values(CONST.UPGRADE_FEATURE_INTRO_MAPPING).find((f) => f.alias === route.params.featureName);

const featureNameAlias = getFeatureNameAlias(route.params.featureName);

const feature = Object.values(CONST.UPGRADE_FEATURE_INTRO_MAPPING).find((f) => f.alias === featureNameAlias);
const {translate} = useLocalize();
const [policy] = useOnyx(`policy_${policyID}`);
const qboConfig = policy?.connections?.quickbooksOnline?.config;
const {isOffline} = useNetwork();

const canPerformUpgrade = !!feature && !!policy && PolicyUtils.isPolicyAdmin(policy);
const isUpgraded = React.useMemo(() => PolicyUtils.isControlPolicy(policy), [policy]);

const perDiemCustomUnit = PolicyUtils.getPerDiemCustomUnit(policy);
const categoryId = route.params?.categoryId;

const goBack = useCallback(() => {
if (!feature) {
Expand All @@ -47,6 +67,18 @@ function WorkspaceUpgradePage({route}: WorkspaceUpgradePageProps) {
}
return;
case CONST.UPGRADE_FEATURE_INTRO_MAPPING.reportFields.id:
switch (route.params.featureName) {
case CONST.UPGRADE_FEATURE_INTRO_MAPPING.reportFields.alias:
Navigation.dismissModal();
return Navigation.navigate(ROUTES.WORKSPACE_MORE_FEATURES.getRoute(policyID));
default: {
Navigation.goBack();
if (route.params.backTo) {
Navigation.navigate(route.params.backTo);
}
return;
}
}
case CONST.UPGRADE_FEATURE_INTRO_MAPPING.rules.id:
case CONST.UPGRADE_FEATURE_INTRO_MAPPING.companyCards.id:
case CONST.UPGRADE_FEATURE_INTRO_MAPPING.perDiem.id:
Expand All @@ -56,7 +88,7 @@ function WorkspaceUpgradePage({route}: WorkspaceUpgradePageProps) {
Navigation.dismissModal();
return route.params.backTo ? Navigation.navigate(route.params.backTo) : Navigation.goBack();
}
}, [feature, policyID, route.params.backTo]);
}, [feature, policyID, route.params.backTo, route.params.featureName]);

const upgradeToCorporate = () => {
if (!canPerformUpgrade) {
Expand All @@ -72,7 +104,32 @@ function WorkspaceUpgradePage({route}: WorkspaceUpgradePageProps) {
}
switch (feature.id) {
case CONST.UPGRADE_FEATURE_INTRO_MAPPING.reportFields.id:
Policy.enablePolicyReportFields(policyID, true, true);
switch (route.params.featureName) {
case CONST.REPORT_FIELDS_FEATURE.qbo.classes:
QuickbooksOnline.updateQuickbooksOnlineSyncClasses(policyID, CONST.INTEGRATION_ENTITY_MAP_TYPES.REPORT_FIELD, qboConfig?.syncClasses);
break;
case CONST.REPORT_FIELDS_FEATURE.qbo.customers:
QuickbooksOnline.updateQuickbooksOnlineSyncCustomers(policyID, CONST.INTEGRATION_ENTITY_MAP_TYPES.REPORT_FIELD, qboConfig?.syncCustomers);
break;
case CONST.REPORT_FIELDS_FEATURE.qbo.locations:
QuickbooksOnline.updateQuickbooksOnlineSyncLocations(policyID, CONST.INTEGRATION_ENTITY_MAP_TYPES.REPORT_FIELD, qboConfig?.syncLocations);
break;
case CONST.REPORT_FIELDS_FEATURE.xero.mapping: {
const {trackingCategories} = policy?.connections?.xero?.data ?? {};
const currentTrackingCategory = trackingCategories?.find((category) => category.id === categoryId);
const {mappings} = policy?.connections?.xero?.config ?? {};
const currentTrackingCategoryValue = currentTrackingCategory ? mappings?.[`${CONST.XERO_CONFIG.TRACKING_CATEGORY_PREFIX}${currentTrackingCategory.id}`] ?? '' : '';
Xero.updateXeroMappings(
policyID,
categoryId ? {[`${CONST.XERO_CONFIG.TRACKING_CATEGORY_PREFIX}${categoryId}`]: CONST.XERO_CONFIG.TRACKING_CATEGORY_OPTIONS.REPORT_FIELD} : {},
categoryId ? {[`${CONST.XERO_CONFIG.TRACKING_CATEGORY_PREFIX}${categoryId}`]: currentTrackingCategoryValue} : {},
);
break;
}
default: {
Policy.enablePolicyReportFields(policyID, true, true);
}
}
break;
case CONST.UPGRADE_FEATURE_INTRO_MAPPING.rules.id:
Policy.enablePolicyRules(policyID, true, true);
Expand All @@ -85,7 +142,18 @@ function WorkspaceUpgradePage({route}: WorkspaceUpgradePageProps) {
break;
default:
}
}, [feature, perDiemCustomUnit?.customUnitID, policyID]);
}, [
categoryId,
feature,
perDiemCustomUnit?.customUnitID,
policy?.connections?.xero?.config,
policy?.connections?.xero?.data,
policyID,
qboConfig?.syncClasses,
qboConfig?.syncCustomers,
qboConfig?.syncLocations,
route.params.featureName,
]);

useEffect(() => {
const unsubscribeListener = navigation.addListener('blur', () => {
Expand Down

0 comments on commit 8e1b0fd

Please sign in to comment.