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

QBO operations 1:1 Part 4 #48080

Merged
merged 8 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/libs/API/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,10 @@ const WRITE_COMMANDS = {
UPDATE_QUICKBOOKS_ONLINE_SYNC_CUSTOMERS: 'UpdateQuickbooksOnlineSyncCustomers',
UPDATE_QUICKBOOKS_ONLINE_SYNC_CLASSES: 'UpdateQuickbooksOnlineSyncClasses',
UPDATE_QUICKBOOKS_ONLINE_NON_REIMBURSABLE_BILL_DEFAULT_VENDOR: 'UpdateQuickbooksOnlineNonReimbursableBillDefaultVendor',
UPDATE_QUICKBOOKS_ONLINE_AUTO_SYNC: 'UpdateQuickbooksOnlineAutoSync',
UPDATE_QUICKBOOKS_ONLINE_SYNC_PEOPLE: 'UpdateQuickbooksOnlineSyncPeople',
UPDATE_QUICKBOOKS_ONLINE_REIMBURSEMENT_ACCOUNT_ID: 'UpdateQuickbooksOnlineReimbursementAccountID',
UPDATE_QUICKBOOKS_ONLINE_EXPORT: 'UpdateQuickbooksOnlineExport',
UPDATE_MANY_POLICY_CONNECTION_CONFIGS: 'UpdateManyPolicyConnectionConfigurations',
REMOVE_POLICY_CONNECTION: 'RemovePolicyConnection',
SET_POLICY_TAXES_ENABLED: 'SetPolicyTaxesEnabled',
Expand Down Expand Up @@ -637,6 +641,10 @@ type WriteCommandParameters = {
[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_REIMBURSABLE_EXPENSES_ACCOUNT]: Parameters.UpdateQuickbooksOnlineGenericTypeParams;
[WRITE_COMMANDS.UPDATE_QUICKBOOKS_ONLINE_AUTO_SYNC]: Parameters.UpdateQuickbooksOnlineGenericTypeParams;
[WRITE_COMMANDS.UPDATE_QUICKBOOKS_ONLINE_SYNC_PEOPLE]: Parameters.UpdateQuickbooksOnlineGenericTypeParams;
[WRITE_COMMANDS.UPDATE_QUICKBOOKS_ONLINE_REIMBURSEMENT_ACCOUNT_ID]: Parameters.UpdateQuickbooksOnlineGenericTypeParams;
[WRITE_COMMANDS.UPDATE_QUICKBOOKS_ONLINE_EXPORT]: Parameters.UpdateQuickbooksOnlineGenericTypeParams;
[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
126 changes: 126 additions & 0 deletions src/libs/actions/connections/QuickbooksOnline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,87 @@ function buildOnyxDataForMultipleQuickbooksConfigurations<TConfigUpdate extends
};
}

function updateQuickbooksOnlineAutoSync(policyID: string, settingValue: boolean) {
const optimisticData: OnyxUpdate[] = [
shubham1206agra marked this conversation as resolved.
Show resolved Hide resolved
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
value: {
connections: {
[CONST.POLICY.CONNECTIONS.NAME.QBO]: {
config: {
autoSync: {
enabled: settingValue ?? null,
},
pendingFields: {
[CONST.QUICKBOOKS_CONFIG.AUTO_SYNC]: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE,
},
errorFields: {
[CONST.QUICKBOOKS_CONFIG.AUTO_SYNC]: null,
},
},
},
},
},
},
];

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

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

const parameters: UpdateQuickbooksOnlineGenericTypeParams = {
policyID,
settingValue: JSON.stringify(settingValue),
idempotencyKey: String(CONST.QUICKBOOKS_CONFIG.AUTO_SYNC),
};
API.write(WRITE_COMMANDS.UPDATE_QUICKBOOKS_ONLINE_AUTO_SYNC, parameters, {optimisticData, failureData, successData});
}
shubham1206agra marked this conversation as resolved.
Show resolved Hide resolved

function buildOnyxDataForQuickbooksConfiguration<TSettingName extends keyof Connections['quickbooksOnline']['config']>(
policyID: string,
settingName: TSettingName,
Expand Down Expand Up @@ -192,6 +273,17 @@ function updateQuickbooksOnlineAutoCreateVendor<TConfigUpdate extends Partial<Co
API.write(WRITE_COMMANDS.UPDATE_QUICKBOOKS_ONLINE_AUTO_CREATE_VENDOR, parameters, onyxData);
}

function updateQuickbooksOnlineSyncPeople<TSettingValue extends Connections['quickbooksOnline']['config']['syncPeople']>(policyID: string, settingValue: TSettingValue) {
const onyxData = buildOnyxDataForQuickbooksConfiguration(policyID, CONST.QUICKBOOKS_CONFIG.SYNC_PEOPLE, settingValue, !settingValue);

const parameters: UpdateQuickbooksOnlineGenericTypeParams = {
policyID,
settingValue: JSON.stringify(settingValue),
idempotencyKey: String(CONST.QUICKBOOKS_CONFIG.SYNC_PEOPLE),
};
API.write(WRITE_COMMANDS.UPDATE_QUICKBOOKS_ONLINE_SYNC_PEOPLE, parameters, onyxData);
}

function updateQuickbooksOnlineReimbursableExpensesAccount<TSettingValue extends Connections['quickbooksOnline']['config']['reimbursableExpensesAccount']>(
policyID: string,
settingValue: TSettingValue,
Expand Down Expand Up @@ -277,11 +369,45 @@ function updateQuickbooksOnlineSyncTax<TSettingValue extends Connections['quickb
API.write(WRITE_COMMANDS.UPDATE_QUICKBOOKS_ONLINE_SYNC_TAX, parameters, onyxData);
}

function updateQuickbooksOnlineReimbursementAccountID<TSettingValue extends Connections['quickbooksOnline']['config']['reimbursementAccountID']>(
policyID: string,
settingValue: TSettingValue,
oldSettingValue?: TSettingValue,
) {
const onyxData = buildOnyxDataForQuickbooksConfiguration(policyID, CONST.QUICKBOOKS_CONFIG.REIMBURSEMENT_ACCOUNT_ID, settingValue, oldSettingValue);

const parameters: UpdateQuickbooksOnlineGenericTypeParams = {
policyID,
settingValue: JSON.stringify(settingValue),
idempotencyKey: String(CONST.QUICKBOOKS_CONFIG.REIMBURSEMENT_ACCOUNT_ID),
};
API.write(WRITE_COMMANDS.UPDATE_QUICKBOOKS_ONLINE_REIMBURSEMENT_ACCOUNT_ID, parameters, onyxData);
}

function updateQuickbooksOnlinePreferredExporter<TSettingValue extends Connections['quickbooksOnline']['config']['export']>(
policyID: string,
settingValue: TSettingValue,
oldSettingValue?: TSettingValue,
) {
const onyxData = buildOnyxDataForQuickbooksConfiguration(policyID, CONST.QUICKBOOKS_CONFIG.EXPORT, settingValue, oldSettingValue);

const parameters: UpdateQuickbooksOnlineGenericTypeParams = {
policyID,
settingValue: JSON.stringify(settingValue),
idempotencyKey: String(CONST.QUICKBOOKS_CONFIG.EXPORT),
};
API.write(WRITE_COMMANDS.UPDATE_QUICKBOOKS_ONLINE_EXPORT, parameters, onyxData);
}

export {
getQuickbooksOnlineSetupLink,
updateQuickbooksOnlineEnableNewCategories,
updateQuickbooksOnlineAutoCreateVendor,
updateQuickbooksOnlineReimbursableExpensesAccount,
updateQuickbooksOnlineAutoSync,
updateQuickbooksOnlineSyncPeople,
updateQuickbooksOnlineReimbursementAccountID,
updateQuickbooksOnlinePreferredExporter,
updateQuickbooksOnlineNonReimbursableBillDefaultVendor,
updateQuickbooksOnlineSyncTax,
updateQuickbooksOnlineSyncClasses,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import SelectionScreen from '@components/SelectionScreen';
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 * as ErrorUtils from '@libs/ErrorUtils';
import Navigation from '@libs/Navigation/Navigation';
import {settingsPendingAction} from '@libs/PolicyUtils';
Expand Down Expand Up @@ -55,7 +55,9 @@ function QuickbooksAccountSelectPage({policy}: WithPolicyConnectionsProps) {

const saveSelection = useCallback(
({value}: SelectorType) => {
Connections.updatePolicyConnectionConfig(policyID, CONST.POLICY.CONNECTIONS.NAME.QBO, CONST.QUICKBOOKS_CONFIG.REIMBURSEMENT_ACCOUNT_ID, value, qboConfig?.reimbursementAccountID);
if (value !== qboConfig?.reimbursementAccountID) {
QuickbooksOnline.updateQuickbooksOnlineReimbursementAccountID(policyID, value, qboConfig?.reimbursementAccountID);
}
Navigation.goBack(ROUTES.WORKSPACE_ACCOUNTING_QUICKBOOKS_ONLINE_ADVANCED.getRoute(policyID));
},
[policyID, qboConfig?.reimbursementAccountID],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,18 +88,7 @@ function QuickbooksAdvancedPage({policy}: WithPolicyConnectionsProps) {
subtitle: translate('workspace.qbo.advancedConfig.autoSyncDescription'),
switchAccessibilityLabel: translate('workspace.qbo.advancedConfig.autoSyncDescription'),
isActive: !!qboConfig?.autoSync?.enabled,
onToggle: () =>
Connections.updatePolicyConnectionConfig(
policyID,
CONST.POLICY.CONNECTIONS.NAME.QBO,
CONST.QUICKBOOKS_CONFIG.AUTO_SYNC,
{
enabled: !qboConfig?.autoSync?.enabled,
},
{
enabled: qboConfig?.autoSync?.enabled,
},
),
onToggle: () => QuickbooksOnline.updateQuickbooksOnlineAutoSync(policyID, !qboConfig?.autoSync?.enabled),
subscribedSetting: CONST.QUICKBOOKS_CONFIG.ENABLED,
errors: ErrorUtils.getLatestErrorField(qboConfig, CONST.QUICKBOOKS_CONFIG.ENABLED),
pendingAction: settingsPendingAction([CONST.QUICKBOOKS_CONFIG.ENABLED], qboConfig?.pendingFields),
Expand All @@ -109,8 +98,7 @@ function QuickbooksAdvancedPage({policy}: WithPolicyConnectionsProps) {
subtitle: translate('workspace.qbo.advancedConfig.inviteEmployeesDescription'),
switchAccessibilityLabel: translate('workspace.qbo.advancedConfig.inviteEmployeesDescription'),
isActive: !!qboConfig?.syncPeople,
onToggle: () =>
Connections.updatePolicyConnectionConfig(policyID, CONST.POLICY.CONNECTIONS.NAME.QBO, CONST.QUICKBOOKS_CONFIG.SYNC_PEOPLE, !qboConfig?.syncPeople, qboConfig?.syncPeople),
onToggle: () => QuickbooksOnline.updateQuickbooksOnlineSyncPeople(policyID, !qboConfig?.syncPeople),
subscribedSetting: CONST.QUICKBOOKS_CONFIG.SYNC_PEOPLE,
errors: ErrorUtils.getLatestErrorField(qboConfig, CONST.QUICKBOOKS_CONFIG.SYNC_PEOPLE),
pendingAction: settingsPendingAction([CONST.QUICKBOOKS_CONFIG.SYNC_PEOPLE], qboConfig?.pendingFields),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Text from '@components/Text';
import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails';
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 * as ErrorUtils from '@libs/ErrorUtils';
import * as PolicyUtils from '@libs/PolicyUtils';
import {getAdminEmployees} from '@libs/PolicyUtils';
Expand Down Expand Up @@ -54,13 +54,7 @@ function QuickbooksPreferredExporterConfigurationPage({policy}: WithPolicyConnec
const selectExporter = useCallback(
(row: CardListItem) => {
if (row.value !== qboConfig?.export?.exporter) {
Connections.updatePolicyConnectionConfig(
policyID,
CONST.POLICY.CONNECTIONS.NAME.QBO,
CONST.QUICKBOOKS_CONFIG.EXPORT,
{exporter: row.value},
{exporter: qboConfig?.export.exporter},
);
QuickbooksOnline.updateQuickbooksOnlinePreferredExporter(policyID, {exporter: row.value}, {exporter: qboConfig?.export.exporter ?? ''});
}
Navigation.goBack(ROUTES.POLICY_ACCOUNTING_QUICKBOOKS_ONLINE_PREFERRED_EXPORTER.getRoute(policyID));
},
Expand Down
Loading