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

Chore: convert lib/sandboxes and sandboxSync to TS #1328

Merged
merged 5 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
17 changes: 6 additions & 11 deletions commands/sandbox/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ const { EXIT_CODES } = require('../../lib/enums/exitCodes');
const { getAccountConfig, getEnv } = require('@hubspot/local-dev-lib/config');
const { uiFeatureHighlight, uiBetaTag } = require('../../lib/ui');
const {
sandboxTypeMap,
SANDBOX_TYPE_MAP,
getAvailableSyncTypes,
syncTypes,
SYNC_TYPES,
validateSandboxUsageLimits,
} = require('../../lib/sandboxes');
const { getValidEnv } = require('@hubspot/local-dev-lib/environment');
Expand Down Expand Up @@ -65,7 +65,7 @@ exports.handler = async options => {
let typePrompt;
let namePrompt;

if ((type && !sandboxTypeMap[type.toLowerCase()]) || !type) {
if ((type && !SANDBOX_TYPE_MAP[type.toLowerCase()]) || !type) {
if (!force) {
typePrompt = await sandboxTypePrompt();
} else {
Expand All @@ -74,7 +74,7 @@ exports.handler = async options => {
}
}
const sandboxType = type
? sandboxTypeMap[type.toLowerCase()]
? SANDBOX_TYPE_MAP[type.toLowerCase()]
: typePrompt.type;

// Check usage limits and exit if parent portal has no available sandboxes for the selected type
Expand Down Expand Up @@ -141,12 +141,7 @@ exports.handler = async options => {
const sandboxAccountConfig = getAccountConfig(result.sandbox.sandboxHubId);
// For v1 sandboxes, keep sync here. Once we migrate to v2, this will be handled by BE automatically
const handleSyncSandbox = async syncTasks => {
await syncSandbox({
accountConfig: sandboxAccountConfig,
parentAccountConfig: accountConfig,
env,
syncTasks,
});
await syncSandbox(sandboxAccountConfig, accountConfig, env, syncTasks);
};
try {
let availableSyncTasks = await getAvailableSyncTypes(
Expand All @@ -156,7 +151,7 @@ exports.handler = async options => {

if (!contactRecordsSyncPromptResult) {
availableSyncTasks = availableSyncTasks.filter(
t => t.type !== syncTypes.OBJECT_RECORDS
t => t.type !== SYNC_TYPES.OBJECT_RECORDS
);
}
await handleSyncSandbox(availableSyncTasks);
Expand Down
2 changes: 2 additions & 0 deletions lang/en.lyaml
Original file line number Diff line number Diff line change
Expand Up @@ -1420,6 +1420,7 @@ en:
failure:
invalidUser: "Couldn't create {{#bold}}{{ accountName }}{{/bold}} because your account has been removed from {{#bold}}{{ parentAccountName }}{{/bold}} or your permission set doesn't allow you to create the sandbox. To update your permissions, contact a super admin in {{#bold}}{{ parentAccountName }}{{/bold}}."
403Gating: "Couldn't create {{#bold}}{{ accountName }}{{/bold}} because {{#bold}}{{ parentAccountName }}{{/bold}} does not have access to development sandboxes. To opt in to the CRM Development Beta and use development sandboxes, visit https://app.hubspot.com/l/product-updates/in-beta?update=13899236."
usageLimitsFetch: "Unable to fetch sandbox usage limits. Please try again."
limit:
developer:
one: "{{#bold}}{{ accountName }}{{/bold}} reached the limit of {{ limit }} development sandbox.
Expand Down Expand Up @@ -1479,6 +1480,7 @@ en:
syncInProgress: "Couldn't run the sync because there's another sync in progress. Wait for the current sync to finish and then try again. To check the sync status, visit the sync activity log: {{ url }}."
notSuperAdmin: "Couldn't run the sync because you are not a super admin in {{ account }}. Ask the account owner for super admin access to the sandbox."
objectNotFound: "Couldn't sync the sandbox because {{#bold}}{{ account }}{{/bold}} may have been deleted through the UI. Run {{#bold}}hs sandbox delete{{/bold}} to remove this account from the config. "
syncTypeFetch: "Unable to fetch available sandbox sync types. Please try again."
errorHandlers:
index:
errorOccurred: "Error: {{ error }}"
Expand Down
9 changes: 6 additions & 3 deletions lib/buildAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ const {
HUBSPOT_ACCOUNT_TYPES,
} = require('@hubspot/local-dev-lib/constants/config');
const { createSandbox } = require('@hubspot/local-dev-lib/api/sandboxHubs');
const { sandboxApiTypeMap, handleSandboxCreateError } = require('./sandboxes');
const {
SANDBOX_API_TYPE_MAP,
handleSandboxCreateError,
} = require('./sandboxes');
const {
handleDeveloperTestAccountCreateError,
} = require('./developerTestAccounts');
Expand Down Expand Up @@ -132,7 +135,7 @@ async function buildNewAccount({
let resultAccountId;
try {
if (isSandbox) {
const sandboxApiType = sandboxApiTypeMap[accountType]; // API expects sandbox type as 1 or 2.
const sandboxApiType = SANDBOX_API_TYPE_MAP[accountType]; // API expects sandbox type as 1 or 2.

const { data } = await createSandbox(accountId, name, sandboxApiType);
result = { name, ...data };
Expand All @@ -159,7 +162,7 @@ async function buildNewAccount({
});

if (isSandbox) {
handleSandboxCreateError({ err, env, accountConfig, name, accountId });
handleSandboxCreateError(err, env, name, accountId);
}
if (isDeveloperTestAccount) {
handleDeveloperTestAccountCreateError(err, env, accountId, portalLimit);
Expand Down
10 changes: 5 additions & 5 deletions lib/localDev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,13 @@ const createSandboxForLocalDev = async (accountId, accountConfig, env) => {
sandboxAccountConfig
);
// For v1 sandboxes, keep sync here. Once we migrate to v2, this will be handled by BE automatically
await syncSandbox({
accountConfig: sandboxAccountConfig,
parentAccountConfig: accountConfig,
await syncSandbox(
sandboxAccountConfig,
accountConfig,
env,
syncTasks,
slimInfoMessage: true,
});
true
);
return targetAccountId;
} catch (err) {
logError(err);
Expand Down
76 changes: 36 additions & 40 deletions lib/sandboxSync.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,50 @@
// @ts-nocheck
const SpinniesManager = require('./ui/SpinniesManager');
const { getHubSpotWebsiteOrigin } = require('@hubspot/local-dev-lib/urls');
const { logger } = require('@hubspot/local-dev-lib/logger');
const { i18n } = require('./lang');
const { getAvailableSyncTypes } = require('./sandboxes');
const { initiateSync } = require('@hubspot/local-dev-lib/api/sandboxSync');
const {
debugError,
logError,
ApiErrorContext,
} = require('./errorHandlers/index');
const { isSpecifiedError } = require('@hubspot/local-dev-lib/errors/index');
const { getSandboxTypeAsString } = require('./sandboxes');
const { getAccountId } = require('@hubspot/local-dev-lib/config');
const {
getAccountIdentifier,
} = require('@hubspot/local-dev-lib/config/getAccountIdentifier');
const {
import SpinniesManager from './ui/SpinniesManager';
import { getHubSpotWebsiteOrigin } from '@hubspot/local-dev-lib/urls';
import { logger } from '@hubspot/local-dev-lib/logger';
import { initiateSync } from '@hubspot/local-dev-lib/api/sandboxSync';
import { isSpecifiedError } from '@hubspot/local-dev-lib/errors/index';
import { getAccountId } from '@hubspot/local-dev-lib/config';
import { getAccountIdentifier } from '@hubspot/local-dev-lib/config/getAccountIdentifier';
import { CLIAccount } from '@hubspot/local-dev-lib/types/Accounts';
import { Environment } from '@hubspot/local-dev-lib/types/Config';

import { i18n } from './lang';
import { getAvailableSyncTypes } from './sandboxes';
import { debugError, logError, ApiErrorContext } from './errorHandlers/index';
import { getSandboxTypeAsString } from './sandboxes';
import {
uiAccountDescription,
uiLine,
uiLink,
uiCommandDisabledBanner,
} = require('./ui');
const { isDevelopmentSandbox } = require('./accountTypes');
} from './ui';
import { isDevelopmentSandbox } from './accountTypes';
import { SandboxSyncTask } from '../types/Sandboxes';

const i18nKey = 'lib.sandbox.sync';

/**
* @param {Object} accountConfig - Account config of sandbox portal
* @param {Object} parentAccountConfig - Account config of parent portal
* @param {String} env - Environment (QA/Prod)
* @param {Array} syncTasks - Array of available sync tasks
* @returns
*/
const syncSandbox = async ({
accountConfig,
parentAccountConfig,
env,
syncTasks,
slimInfoMessage = false,
}) => {
export async function syncSandbox(
accountConfig: CLIAccount,
parentAccountConfig: CLIAccount,
env: Environment,
syncTasks: Array<SandboxSyncTask>,
slimInfoMessage = false
) {
const id = getAccountIdentifier(accountConfig);
const accountId = getAccountId(id);
const parentId = getAccountIdentifier(parentAccountConfig);
const parentAccountId = getAccountId(parentId);
const isDevSandbox = isDevelopmentSandbox(accountConfig);

if (!accountId || !parentAccountId) {
throw new Error(
i18n(`${i18nKey}.failure.invalidUser`, {
accountName: uiAccountDescription(accountId),
parentAccountName: uiAccountDescription(parentAccountId),
})
);
}

SpinniesManager.init({
succeedColor: 'white',
});
Expand Down Expand Up @@ -190,8 +190,4 @@ const syncSandbox = async ({
uiLine();
logger.log();
}
};

module.exports = {
syncSandbox,
};
}
Loading
Loading