Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor UpdatePolicyConnectionConfiguration to be 1:1:1 (Part 1) #46234

Merged
merged 14 commits into from
Aug 13, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import useLocalize from '@hooks/useLocalize';
import useNetwork from '@hooks/useNetwork';
import useThemeStyles from '@hooks/useThemeStyles';
import {removePolicyConnection} from '@libs/actions/connections';
import getQuickBooksOnlineSetupLink from '@libs/actions/connections/QuickBooksOnline';
import {getQuickbooksOnlineSetupLink} from '@libs/actions/connections/QuickbooksOnline';
import * as PolicyAction from '@userActions/Policy/Policy';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
Expand Down Expand Up @@ -87,7 +87,7 @@ function ConnectToQuickbooksOnlineButton({
<WebView
ref={webViewRef}
source={{
uri: getQuickBooksOnlineSetupLink(policyID),
uri: getQuickbooksOnlineSetupLink(policyID),
headers: {
Cookie: `authToken=${authToken}`,
},
Expand Down
6 changes: 3 additions & 3 deletions src/components/ConnectToQuickbooksOnlineButton/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import useLocalize from '@hooks/useLocalize';
import useNetwork from '@hooks/useNetwork';
import useThemeStyles from '@hooks/useThemeStyles';
import {removePolicyConnection} from '@libs/actions/connections';
import getQuickBooksOnlineSetupLink from '@libs/actions/connections/QuickBooksOnline';
import {getQuickbooksOnlineSetupLink} from '@libs/actions/connections/QuickbooksOnline';
import * as Link from '@userActions/Link';
import * as PolicyAction from '@userActions/Policy/Policy';
import CONST from '@src/CONST';
Expand All @@ -30,7 +30,7 @@ function ConnectToQuickbooksOnlineButton({policyID, shouldDisconnectIntegrationB
}
// Since QBO doesn't support Taxes, we should disable them from the LHN when connecting to QBO
PolicyAction.enablePolicyTaxes(policyID, false);
Link.openLink(getQuickBooksOnlineSetupLink(policyID), environmentURL);
Link.openLink(getQuickbooksOnlineSetupLink(policyID), environmentURL);
}}
isDisabled={isOffline}
text={translate('workspace.accounting.setup')}
Expand All @@ -43,7 +43,7 @@ function ConnectToQuickbooksOnlineButton({policyID, shouldDisconnectIntegrationB
// Since QBO doesn't support Taxes, we should disable them from the LHN when connecting to QBO
PolicyAction.enablePolicyTaxes(policyID, false);
removePolicyConnection(policyID, integrationToDisconnect);
Link.openLink(getQuickBooksOnlineSetupLink(policyID), environmentURL);
Link.openLink(getQuickbooksOnlineSetupLink(policyID), environmentURL);
setIsDisconnectModalOpen(false);
}}
integrationToConnect={CONST.POLICY.CONNECTIONS.NAME.QBO}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
type UpdateQuickbooksOnlineEnableNewCategoriesParams = {
policyID: string;
settingValue: string;
idempotencyKey: string;
};

export default UpdateQuickbooksOnlineEnableNewCategoriesParams;
1 change: 1 addition & 0 deletions src/libs/API/parameters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ export type {default as AddSubscriptionPaymentCardParams} from './AddSubscriptio
export type {default as SetPolicyCustomTaxNameParams} from './SetPolicyCustomTaxNameParams';
export type {default as SetPolicyForeignCurrencyDefaultParams} from './SetPolicyForeignCurrencyDefaultParams';
export type {default as SetPolicyCurrencyDefaultParams} from './SetPolicyCurrencyDefaultParams';
export type {default as UpdateQuickbooksOnlineEnableNewCategoriesParams} from './UpdateQuickbooksOnlineEnableNewCategoriesParams';
export type {default as UpdatePolicyConnectionConfigParams} from './UpdatePolicyConnectionConfigParams';
export type {default as UpdateManyPolicyConnectionConfigurationsParams} from './UpdateManyPolicyConnectionConfigurationsParams';
export type {default as RemovePolicyConnectionParams} from './RemovePolicyConnectionParams';
Expand Down
3 changes: 2 additions & 1 deletion src/libs/API/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ const WRITE_COMMANDS = {
DECLINE_JOIN_REQUEST: 'DeclineJoinRequest',
CREATE_POLICY_TAX: 'CreatePolicyTax',
UPDATE_POLICY_CONNECTION_CONFIG: 'UpdatePolicyConnectionConfiguration',
UPDATE_QUICKBOOKS_ONLINE_ENABLE_NEW_CATEGORIES: 'UpdateQuickbooksOnlineEnableNewCategories',
UPDATE_MANY_POLICY_CONNECTION_CONFIGS: 'UpdateManyPolicyConnectionConfigurations',
REMOVE_POLICY_CONNECTION: 'RemovePolicyConnection',
SET_POLICY_TAXES_ENABLED: 'SetPolicyTaxesEnabled',
Expand Down Expand Up @@ -534,7 +535,7 @@ type WriteCommandParameters = {
[WRITE_COMMANDS.REQUEST_EXPENSIFY_CARD_LIMIT_INCREASE]: Parameters.RequestExpensifyCardLimitIncreaseParams;
[WRITE_COMMANDS.CLEAR_OUTSTANDING_BALANCE]: null;
[WRITE_COMMANDS.CANCEL_BILLING_SUBSCRIPTION]: Parameters.CancelBillingSubscriptionParams;

[WRITE_COMMANDS.UPDATE_QUICKBOOKS_ONLINE_ENABLE_NEW_CATEGORIES]: Parameters.UpdateQuickbooksOnlineEnableNewCategoriesParams;
[WRITE_COMMANDS.UPDATE_POLICY_CONNECTION_CONFIG]: Parameters.UpdatePolicyConnectionConfigParams;
[WRITE_COMMANDS.UPDATE_MANY_POLICY_CONNECTION_CONFIGS]: Parameters.UpdateManyPolicyConnectionConfigurationsParams;
[WRITE_COMMANDS.REMOVE_POLICY_CONNECTION]: Parameters.RemovePolicyConnectionParams;
Expand Down
14 changes: 0 additions & 14 deletions src/libs/actions/connections/QuickBooksOnline.ts

This file was deleted.

96 changes: 96 additions & 0 deletions src/libs/actions/connections/QuickbooksOnline.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import type {OnyxUpdate} from 'react-native-onyx';
import Onyx from 'react-native-onyx';
import * as API from '@libs/API';
import type {ConnectPolicyToAccountingIntegrationParams} from '@libs/API/parameters';
import type UpdateQuickbooksOnlineEnableNewCategoriesParams from '@libs/API/parameters/UpdateQuickbooksOnlineEnableNewCategoriesParams';
import {READ_COMMANDS, WRITE_COMMANDS} from '@libs/API/types';
import {getCommandURL} from '@libs/ApiUtils';
import * as ErrorUtils from '@libs/ErrorUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';

function getQuickbooksOnlineSetupLink(policyID: string) {
Copy link
Contributor Author

@dangrous dangrous Jul 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getQuickbooksOnlineSetupLink is the same function as it was before - I renamed the file to match spelling; haven't changed this piece at all.

const params: ConnectPolicyToAccountingIntegrationParams = {policyID};
const commandURL = getCommandURL({
command: READ_COMMANDS.CONNECT_POLICY_TO_QUICKBOOKS_ONLINE,
shouldSkipWebProxy: true,
});
return commandURL + new URLSearchParams(params).toString();
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From here down is new

function updateQuickbooksOnlineEnableNewCategories(policyID: string, settingValue: boolean) {
const optimisticData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
value: {
connections: {
[CONST.POLICY.CONNECTIONS.NAME.QBO]: {
config: {
[CONST.QUICK_BOOKS_CONFIG.ENABLE_NEW_CATEGORIES]: settingValue ?? null,
pendingFields: {
[CONST.QUICK_BOOKS_CONFIG.ENABLE_NEW_CATEGORIES]: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE,
},
errorFields: {
[CONST.QUICK_BOOKS_CONFIG.ENABLE_NEW_CATEGORIES]: null,
},
},
},
},
},
},
];

const failureData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
value: {
connections: {
[CONST.POLICY.CONNECTIONS.NAME.QBO]: {
config: {
[CONST.QUICK_BOOKS_CONFIG.ENABLE_NEW_CATEGORIES]: settingValue ?? null,
pendingFields: {
[CONST.QUICK_BOOKS_CONFIG.ENABLE_NEW_CATEGORIES]: null,
},
errorFields: {
[CONST.QUICK_BOOKS_CONFIG.ENABLE_NEW_CATEGORIES]: ErrorUtils.getMicroSecondOnyxErrorWithTranslationKey('common.genericErrorMessage'),
},
},
},
},
},
},
];

const successData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
value: {
connections: {
[CONST.POLICY.CONNECTIONS.NAME.QBO]: {
config: {
[CONST.QUICK_BOOKS_CONFIG.ENABLE_NEW_CATEGORIES]: settingValue ?? null,
pendingFields: {
[CONST.QUICK_BOOKS_CONFIG.ENABLE_NEW_CATEGORIES]: null,
},
errorFields: {
[CONST.QUICK_BOOKS_CONFIG.ENABLE_NEW_CATEGORIES]: null,
},
},
},
},
},
},
];

const parameters: UpdateQuickbooksOnlineEnableNewCategoriesParams = {
policyID,
settingValue: JSON.stringify(settingValue),
idempotencyKey: String(CONST.QUICK_BOOKS_CONFIG.ENABLE_NEW_CATEGORIES),
};
API.write(WRITE_COMMANDS.UPDATE_QUICKBOOKS_ONLINE_ENABLE_NEW_CATEGORIES, parameters, {optimisticData, failureData, successData});
}

export {getQuickbooksOnlineSetupLink, updateQuickbooksOnlineEnableNewCategories};
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Switch from '@components/Switch';
import Text from '@components/Text';
import useLocalize from '@hooks/useLocalize';
import useThemeStyles from '@hooks/useThemeStyles';
import * as Connections from '@libs/actions/connections';
import * as QuickbooksOnline from '@libs/actions/connections/QuickbooksOnline';
import type {WithPolicyProps} from '@pages/workspace/withPolicy';
import withPolicyConnections from '@pages/workspace/withPolicyConnections';
import variables from '@styles/variables';
Expand Down Expand Up @@ -59,9 +59,7 @@ function QuickbooksChartOfAccountsPage({policy}: WithPolicyProps) {
<Switch
accessibilityLabel={translate('workspace.accounting.accounts')}
isOn={!!enableNewCategories}
onToggle={() =>
Connections.updatePolicyConnectionConfig(policyID, CONST.POLICY.CONNECTIONS.NAME.QBO, CONST.QUICK_BOOKS_CONFIG.ENABLE_NEW_CATEGORIES, !enableNewCategories)
}
onToggle={() => QuickbooksOnline.updateQuickbooksOnlineEnableNewCategories(policyID, !enableNewCategories)}
/>
</View>
</OfflineWithFeedback>
Expand Down
Loading