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

Update UpdatePolicyConnectionConfiguration to be 1:1:1 Part-3 #47628

Merged
10 changes: 10 additions & 0 deletions src/libs/API/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,11 @@ const WRITE_COMMANDS = {
UPDATE_QUICKBOOKS_ONLINE_ENABLE_NEW_CATEGORIES: 'UpdateQuickbooksOnlineEnableNewCategories',
UPDATE_QUICKBOOKS_ONLINE_AUTO_CREATE_VENDOR: 'UpdateQuickbooksOnlineAutoCreateVendor',
UPDATE_QUICKBOOKS_ONLINE_REIMBURSABLE_EXPENSES_ACCOUNT: 'UpdateQuickbooksOnlineReimbursableExpensesAccount',
UPDATE_QUICKBOOKS_ONLINE_SYNC_TAX: 'UpdateQuickbooksOnlineSyncTax',
UPDATE_QUICKBOOKS_ONLINE_SYNC_LOCATIONS: 'UpdateQuickbooksOnlineSyncLocations',
UPDATE_QUICKBOOKS_ONLINE_SYNC_CUSTOMERS: 'UpdateQuickbooksOnlineSyncCustomers',
UPDATE_QUICKBOOKS_ONLINE_SYNC_CLASSES: 'UpdateQuickbooksOnlineSyncClasses',
UPDATE_QUICKBOOKS_ONLINE_NON_REIMBURSABLE_BILL_DEFAULT_VENDOR: 'UpdateQuickbooksOnlineNonReimbursableBillDefaultVendor',
UPDATE_MANY_POLICY_CONNECTION_CONFIGS: 'UpdateManyPolicyConnectionConfigurations',
REMOVE_POLICY_CONNECTION: 'RemovePolicyConnection',
SET_POLICY_TAXES_ENABLED: 'SetPolicyTaxesEnabled',
Expand Down Expand Up @@ -558,6 +563,11 @@ type WriteCommandParameters = {
[WRITE_COMMANDS.CLEAR_OUTSTANDING_BALANCE]: null;
[WRITE_COMMANDS.CANCEL_BILLING_SUBSCRIPTION]: Parameters.CancelBillingSubscriptionParams;
[WRITE_COMMANDS.UPDATE_QUICKBOOKS_ONLINE_ENABLE_NEW_CATEGORIES]: Parameters.UpdateQuickbooksOnlineGenericTypeParams;
[WRITE_COMMANDS.UPDATE_QUICKBOOKS_ONLINE_SYNC_TAX]: Parameters.UpdateQuickbooksOnlineGenericTypeParams;
[WRITE_COMMANDS.UPDATE_QUICKBOOKS_ONLINE_SYNC_LOCATIONS]: Parameters.UpdateQuickbooksOnlineGenericTypeParams;
[WRITE_COMMANDS.UPDATE_QUICKBOOKS_ONLINE_SYNC_CUSTOMERS]: Parameters.UpdateQuickbooksOnlineGenericTypeParams;
[WRITE_COMMANDS.UPDATE_QUICKBOOKS_ONLINE_SYNC_CLASSES]: Parameters.UpdateQuickbooksOnlineGenericTypeParams;
[WRITE_COMMANDS.UPDATE_QUICKBOOKS_ONLINE_NON_REIMBURSABLE_BILL_DEFAULT_VENDOR]: Parameters.UpdateQuickbooksOnlineGenericTypeParams;
[WRITE_COMMANDS.UPDATE_QUICKBOOKS_ONLINE_AUTO_CREATE_VENDOR]: Parameters.UpdateQuickbooksOnlineGenericTypeParams;
[WRITE_COMMANDS.UPDATE_QUICKBOOKS_ONLINE_REIMBURSABLE_EXPENSES_ACCOUNT]: Parameters.UpdateQuickbooksOnlineGenericTypeParams;
[WRITE_COMMANDS.UPDATE_POLICY_CONNECTION_CONFIG]: Parameters.UpdatePolicyConnectionConfigParams;
Expand Down
145 changes: 143 additions & 2 deletions src/libs/actions/connections/QuickbooksOnline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {getCommandURL} from '@libs/ApiUtils';
import * as ErrorUtils from '@libs/ErrorUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {ConnectionName, Connections} from '@src/types/onyx/Policy';
import type {ConnectionName, Connections, IntegrationEntityMap} from '@src/types/onyx/Policy';

type ConnectionNameExceptNetSuite = Exclude<ConnectionName, typeof CONST.POLICY.CONNECTIONS.NAME.NETSUITE>;

Expand All @@ -21,6 +21,83 @@ function getQuickbooksOnlineSetupLink(policyID: string) {
return commandURL + new URLSearchParams(params).toString();
}

function updateQuickbooksOnyxData<TSettingName extends keyof Connections['quickbooksOnline']['config']>(
policyID: string,
settingName: TSettingName,
settingValue: Partial<Connections['quickbooksOnline']['config'][TSettingName]>,
) {
const optimisticData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
value: {
connections: {
[CONST.POLICY.CONNECTIONS.NAME.QBO]: {
config: {
[settingName]: settingValue ?? null,
pendingFields: {
[settingName]: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE,
},
errorFields: {
[settingName]: null,
},
},
},
},
},
},
];

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

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

function updateQuickbooksOnlineEnableNewCategories(policyID: string, settingValue: boolean) {
const optimisticData: OnyxUpdate[] = [
{
Expand Down Expand Up @@ -249,4 +326,68 @@ function updateQuickbooksOnlineReimbursableExpensesAccount<TConnectionName exten
API.write(WRITE_COMMANDS.UPDATE_QUICKBOOKS_ONLINE_REIMBURSABLE_EXPENSES_ACCOUNT, parameters, {optimisticData, failureData, successData});
}

export {getQuickbooksOnlineSetupLink, updateQuickbooksOnlineEnableNewCategories, updateQuickbooksOnlineAutoCreateVendor, updateQuickbooksOnlineReimbursableExpensesAccount};
function updateQuickbooksOnlineSyncLocations(policyID: string, settingValue: IntegrationEntityMap) {
const onyxData = updateQuickbooksOnyxData(policyID, CONST.QUICK_BOOKS_CONFIG.SYNC_LOCATIONS, settingValue);

const parameters: UpdateQuickbooksOnlineGenericTypeParams = {
policyID,
settingValue: JSON.stringify(settingValue),
idempotencyKey: String(CONST.QUICK_BOOKS_CONFIG.SYNC_LOCATIONS),
};
API.write(WRITE_COMMANDS.UPDATE_QUICKBOOKS_ONLINE_SYNC_LOCATIONS, parameters, onyxData);
}

function updateQuickbooksOnlineSyncCustomers(policyID: string, settingValue: IntegrationEntityMap) {
const onyxData = updateQuickbooksOnyxData(policyID, CONST.QUICK_BOOKS_CONFIG.SYNC_CUSTOMERS, settingValue);

const parameters: UpdateQuickbooksOnlineGenericTypeParams = {
policyID,
settingValue: JSON.stringify(settingValue),
idempotencyKey: String(CONST.QUICK_BOOKS_CONFIG.SYNC_CUSTOMERS),
};
API.write(WRITE_COMMANDS.UPDATE_QUICKBOOKS_ONLINE_SYNC_CUSTOMERS, parameters, onyxData);
}

function updateQuickbooksOnlineSyncClasses(policyID: string, settingValue: IntegrationEntityMap) {
const onyxData = updateQuickbooksOnyxData(policyID, CONST.QUICK_BOOKS_CONFIG.SYNC_CLASSES, settingValue);
const parameters: UpdateQuickbooksOnlineGenericTypeParams = {
policyID,
settingValue: JSON.stringify(settingValue),
idempotencyKey: String(CONST.QUICK_BOOKS_CONFIG.SYNC_CLASSES),
};
API.write(WRITE_COMMANDS.UPDATE_QUICKBOOKS_ONLINE_SYNC_CLASSES, parameters, onyxData);
}

function updateQuickbooksOnlineNonReimbursableBillDefaultVendor(policyID: string, settingValue: string) {
const onyxData = updateQuickbooksOnyxData(policyID, CONST.QUICK_BOOKS_CONFIG.NON_REIMBURSABLE_BILL_DEFAULT_VENDOR, settingValue);

const parameters: UpdateQuickbooksOnlineGenericTypeParams = {
policyID,
settingValue: JSON.stringify(settingValue),
idempotencyKey: String(CONST.QUICK_BOOKS_CONFIG.NON_REIMBURSABLE_BILL_DEFAULT_VENDOR),
};
API.write(WRITE_COMMANDS.UPDATE_QUICKBOOKS_ONLINE_NON_REIMBURSABLE_BILL_DEFAULT_VENDOR, parameters, onyxData);
}

function updateQuickbooksOnlineSyncTax(policyID: string, settingValue: boolean) {
const onyxData = updateQuickbooksOnyxData(policyID, CONST.QUICK_BOOKS_CONFIG.SYNC_TAX, settingValue);

const parameters: UpdateQuickbooksOnlineGenericTypeParams = {
policyID,
settingValue: JSON.stringify(settingValue),
idempotencyKey: String(CONST.QUICK_BOOKS_CONFIG.SYNC_TAX),
};
API.write(WRITE_COMMANDS.UPDATE_QUICKBOOKS_ONLINE_SYNC_TAX, parameters, onyxData);
}

export {
getQuickbooksOnlineSetupLink,
updateQuickbooksOnlineEnableNewCategories,
updateQuickbooksOnlineAutoCreateVendor,
updateQuickbooksOnlineReimbursableExpensesAccount,
updateQuickbooksOnlineNonReimbursableBillDefaultVendor,
updateQuickbooksOnlineSyncTax,
updateQuickbooksOnlineSyncClasses,
updateQuickbooksOnlineSyncLocations,
updateQuickbooksOnlineSyncCustomers,
};
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import RadioListItem from '@components/SelectionList/RadioListItem';
import type {ListItem} from '@components/SelectionList/types';
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 Navigation from '@navigation/Navigation';
import AccessOrNotFoundWrapper from '@pages/workspace/AccessOrNotFoundWrapper';
import type {WithPolicyConnectionsProps} from '@pages/workspace/withPolicyConnections';
Expand Down Expand Up @@ -42,7 +42,7 @@ function QuickbooksNonReimbursableDefaultVendorSelectPage({policy}: WithPolicyCo
const selectVendor = useCallback(
(row: CardListItem) => {
if (row.value !== nonReimbursableBillDefaultVendor) {
Connections.updatePolicyConnectionConfig(policyID, CONST.POLICY.CONNECTIONS.NAME.QBO, CONST.QUICK_BOOKS_CONFIG.NON_REIMBURSABLE_BILL_DEFAULT_VENDOR, row.value);
QuickbooksOnline.updateQuickbooksOnlineNonReimbursableBillDefaultVendor(policyID, row.value);
}
Navigation.goBack(ROUTES.POLICY_ACCOUNTING_QUICKBOOKS_ONLINE_COMPANY_CARD_EXPENSE_ACCOUNT.getRoute(policyID));
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,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 AccessOrNotFoundWrapper from '@pages/workspace/AccessOrNotFoundWrapper';
import type {WithPolicyProps} from '@pages/workspace/withPolicy';
import withPolicyConnections from '@pages/workspace/withPolicyConnections';
Expand Down Expand Up @@ -48,10 +48,8 @@ function QuickbooksClassesPage({policy}: WithPolicyProps) {
accessibilityLabel={translate('workspace.qbo.classes')}
isOn={isSwitchOn}
onToggle={() =>
Connections.updatePolicyConnectionConfig(
QuickbooksOnline.updateQuickbooksOnlineSyncClasses(
policyID,
CONST.POLICY.CONNECTIONS.NAME.QBO,
CONST.QUICK_BOOKS_CONFIG.SYNC_CLASSES,
isSwitchOn ? CONST.INTEGRATION_ENTITY_MAP_TYPES.NONE : CONST.INTEGRATION_ENTITY_MAP_TYPES.TAG,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,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 AccessOrNotFoundWrapper from '@pages/workspace/AccessOrNotFoundWrapper';
import type {WithPolicyProps} from '@pages/workspace/withPolicy';
import withPolicyConnections from '@pages/workspace/withPolicyConnections';
Expand Down Expand Up @@ -47,10 +47,8 @@ function QuickbooksCustomersPage({policy}: WithPolicyProps) {
accessibilityLabel={translate('workspace.qbo.customers')}
isOn={isSwitchOn}
onToggle={() =>
Connections.updatePolicyConnectionConfig(
QuickbooksOnline.updateQuickbooksOnlineSyncCustomers(
policyID,
CONST.POLICY.CONNECTIONS.NAME.QBO,
CONST.QUICK_BOOKS_CONFIG.SYNC_CUSTOMERS,
isSwitchOn ? CONST.INTEGRATION_ENTITY_MAP_TYPES.NONE : CONST.INTEGRATION_ENTITY_MAP_TYPES.TAG,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,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 AccessOrNotFoundWrapper from '@pages/workspace/AccessOrNotFoundWrapper';
import type {WithPolicyProps} from '@pages/workspace/withPolicy';
import withPolicyConnections from '@pages/workspace/withPolicyConnections';
Expand Down Expand Up @@ -52,10 +52,8 @@ function QuickbooksLocationsPage({policy}: WithPolicyProps) {
accessibilityLabel={translate('workspace.qbo.locations')}
isOn={isSwitchOn}
onToggle={() =>
Connections.updatePolicyConnectionConfig(
QuickbooksOnline.updateQuickbooksOnlineSyncLocations(
policyID,
CONST.POLICY.CONNECTIONS.NAME.QBO,
CONST.QUICK_BOOKS_CONFIG.SYNC_LOCATIONS,
isSwitchOn ? CONST.INTEGRATION_ENTITY_MAP_TYPES.NONE : CONST.INTEGRATION_ENTITY_MAP_TYPES.TAG,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,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 AccessOrNotFoundWrapper from '@pages/workspace/AccessOrNotFoundWrapper';
import type {WithPolicyProps} from '@pages/workspace/withPolicy';
import withPolicyConnections from '@pages/workspace/withPolicyConnections';
Expand Down Expand Up @@ -45,7 +45,7 @@ function QuickbooksTaxesPage({policy}: WithPolicyProps) {
<Switch
accessibilityLabel={translate('workspace.accounting.taxes')}
isOn={!!syncTax}
onToggle={() => Connections.updatePolicyConnectionConfig(policyID, CONST.POLICY.CONNECTIONS.NAME.QBO, CONST.QUICK_BOOKS_CONFIG.SYNC_TAX, !syncTax)}
onToggle={() => QuickbooksOnline.updateQuickbooksOnlineSyncTax(policyID, !syncTax)}
disabled={!syncTax && isJournalExportEntity}
/>
</View>
Expand Down
Loading