UNPKG

@bitrix24/b24jssdk

Version:

Bitrix24 REST API JavaScript SDK

389 lines (386 loc) 11.6 kB
/** * @package @bitrix24/b24jssdk * @version 1.0.1 * @copyright (c) 2026 Bitrix24 * @license MIT * @see https://github.com/bitrix24/b24jssdk * @see https://bitrix24.github.io/b24jssdk/ */ import { ProfileManager } from './profile-manager.mjs'; import { AppManager } from './app-manager.mjs'; import { PaymentManager } from './payment-manager.mjs'; import { LicenseManager } from './license-manager.mjs'; import { CurrencyManager } from './currency-manager.mjs'; import { OptionsManager } from './options-manager.mjs'; import { Text } from '../tools/text.mjs'; import { LoadDataType, TypeSpecificUrl } from '../types/b24-helper.mjs'; import { PullClient } from '../pullClient/client.mjs'; import { LoggerFactory } from '../logger/logger-factory.mjs'; var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); class B24HelperManager { static { __name(this, "B24HelperManager"); } _b24; _isInit = false; _profile = null; _app = null; _payment = null; _license = null; _currency = null; _appOptions = null; _userOptions = null; _b24PullClient = null; _pullClientUnSubscribe = []; _pullClientModuleId = ""; _logger; constructor(b24) { this._logger = LoggerFactory.createNullLogger(); this._b24 = b24; } setLogger(logger) { this._logger = logger; if (null !== this._profile) { this._profile.setLogger(this.getLogger()); } if (null !== this._app) { this._app.setLogger(this.getLogger()); } if (null !== this._payment) { this._payment.setLogger(this.getLogger()); } if (null !== this._license) { this._license.setLogger(this.getLogger()); } if (null !== this._currency) { this._currency.setLogger(this.getLogger()); } if (null !== this._appOptions) { this._appOptions.setLogger(this.getLogger()); } if (null !== this._userOptions) { this._userOptions.setLogger(this.getLogger()); } } getLogger() { return this._logger; } destroy() { this._destroyPullClient(); } // region loadData //// async loadData(dataTypes = [LoadDataType.App, LoadDataType.Profile]) { const batchMethods = { [LoadDataType.App]: { method: "app.info" }, [LoadDataType.Profile]: { method: "profile" }, [LoadDataType.Currency]: [ { method: "crm.currency.base.get" }, { method: "crm.currency.list" } ], [LoadDataType.AppOptions]: { method: "app.option.get" }, [LoadDataType.UserOptions]: { method: "user.option.get" } }; const batchRequest = dataTypes.reduce( (acc, type) => { if (batchMethods[type]) { if (Array.isArray(batchMethods[type])) { for (const [index, row] of batchMethods[type].entries()) { acc[`get_${type}_${index}`] = row; } } else { acc[`get_${type}`] = batchMethods[type]; } } return acc; }, {} ); try { const response = await this._b24.callBatch(batchRequest); const data = response.getData(); if (data[`get_${LoadDataType.App}`]) { this._app = await this.parseAppData(data[`get_${LoadDataType.App}`]); this._payment = await this.parsePaymentData( data[`get_${LoadDataType.App}`] ); this._license = await this.parseLicenseData( data[`get_${LoadDataType.App}`] ); } if (data[`get_${LoadDataType.Profile}`]) { this._profile = await this.parseUserData( data[`get_${LoadDataType.Profile}`] ); } if (data[`get_${LoadDataType.Currency}_0`] && data[`get_${LoadDataType.Currency}_1`]) { this._currency = await this.parseCurrencyData({ currencyBase: data[`get_${LoadDataType.Currency}_0`], currencyList: data[`get_${LoadDataType.Currency}_1`] }); } if (data[`get_${LoadDataType.AppOptions}`]) { this._appOptions = await this.parseOptionsData( "app", data[`get_${LoadDataType.AppOptions}`] ); } if (data[`get_${LoadDataType.UserOptions}`]) { this._userOptions = await this.parseOptionsData( "user", data[`get_${LoadDataType.UserOptions}`] ); } this._isInit = true; } catch (error) { if (error instanceof Error) { throw error; } this.getLogger().error("Failed to load data", { error }); throw new Error("Failed to load data"); } } async parseUserData(profileData) { const manager = new ProfileManager(this._b24); manager.setLogger(this.getLogger()); return manager.initData({ id: Number(profileData.ID), isAdmin: profileData.ADMIN === true, lastName: profileData?.LAST_NAME || "", name: profileData?.NAME || "", gender: profileData?.PERSONAL_GENDER || "", photo: profileData?.PERSONAL_PHOTO || "", TimeZone: profileData?.TIME_ZONE || "", TimeZoneOffset: profileData?.TIME_ZONE_OFFSET }).then(() => { return manager; }); } async parseAppData(appData) { const manager = new AppManager(this._b24); manager.setLogger(this.getLogger()); return manager.initData({ id: Number.parseInt(appData.ID), code: appData.CODE, version: Number.parseInt(appData.VERSION), status: appData.STATUS, isInstalled: appData.INSTALLED }).then(() => { return manager; }); } async parsePaymentData(appData) { const manager = new PaymentManager(this._b24); manager.setLogger(this.getLogger()); return manager.initData({ isExpired: appData.PAYMENT_EXPIRED === "Y", days: Number.parseInt(appData.DAYS || "0") }).then(() => { return manager; }); } async parseLicenseData(appData) { const manager = new LicenseManager(this._b24); manager.setLogger(this.getLogger()); return manager.initData({ languageId: appData.LANGUAGE_ID, license: appData.LICENSE, licensePrevious: appData.LICENSE_PREVIOUS, licenseType: appData.LICENSE_TYPE, licenseFamily: appData.LICENSE_FAMILY, isSelfHosted: appData.LICENSE.includes("selfhosted") }).then(() => { return manager; }); } async parseCurrencyData(currencyData) { const manager = new CurrencyManager(this._b24); manager.setLogger(this.getLogger()); return manager.initData(currencyData).then(() => { return manager; }); } async parseOptionsData(type, optionsData) { const manager = new OptionsManager(this._b24, type); manager.setLogger(this.getLogger()); return manager.initData(optionsData).then(() => { return manager; }); } // endregion //// // region Get //// get isInit() { return this._isInit; } get forB24Form() { this.ensureInitialized(); if (null === this._profile) { throw new Error("B24HelperManager.profileInfo not initialized"); } if (null === this._app) { throw new Error("B24HelperManager.appInfo not initialized"); } return { app_code: this.appInfo.data.code, app_status: this.appInfo.data.status, payment_expired: this.paymentInfo.data.isExpired ? "Y" : "N", days: this.paymentInfo.data.days, b24_plan: this.licenseInfo.data.license, c_name: this.profileInfo.data.name, c_last_name: this.profileInfo.data.lastName, hostname: this.hostName }; } /** * Get the account address BX24 (https://your_domain.bitrix24.com) */ get hostName() { return this._b24.getTargetOrigin(); } get profileInfo() { this.ensureInitialized(); if (null === this._profile) { throw new Error("B24HelperManager.profileInfo not initialized"); } return this._profile; } get appInfo() { this.ensureInitialized(); if (null === this._app) { throw new Error("B24HelperManager.appInfo not initialized"); } return this._app; } get paymentInfo() { this.ensureInitialized(); if (null === this._payment) { throw new Error("B24HelperManager.paymentInfo not initialized"); } return this._payment; } get licenseInfo() { this.ensureInitialized(); if (null === this._license) { throw new Error("B24HelperManager.licenseInfo not initialized"); } return this._license; } get currency() { this.ensureInitialized(); if (null === this._currency) { throw new Error("B24HelperManager.currency not initialized"); } return this._currency; } get appOptions() { this.ensureInitialized(); if (null === this._appOptions) { throw new Error("B24HelperManager.appOptions not initialized"); } return this._appOptions; } get userOptions() { this.ensureInitialized(); if (null === this._userOptions) { throw new Error("B24HelperManager.userOptions not initialized"); } return this._userOptions; } // endregion //// // region Custom SelfHosted && Cloud //// get isSelfHosted() { return this.licenseInfo.data.isSelfHosted; } /** * Returns the increment step of fields of type ID * @memo in a cloud step = 2 in box step = 1 * * @returns {number} */ get primaryKeyIncrementValue() { if (this.isSelfHosted) { return 1; } return 2; } /** * Defines specific URLs for a Bitrix24 box or cloud */ get b24SpecificUrl() { if (this.isSelfHosted) { return { [TypeSpecificUrl.MainSettings]: "/configs/", [TypeSpecificUrl.UfList]: "/configs/userfield_list.php", [TypeSpecificUrl.UfPage]: "/configs/userfield.php" }; } return { [TypeSpecificUrl.MainSettings]: "/settings/configs/", [TypeSpecificUrl.UfList]: "/settings/configs/userfield_list.php", [TypeSpecificUrl.UfPage]: "/settings/configs/userfield.php" }; } // endregion //// // region Pull.Client //// usePullClient(prefix = "prefix", userId) { if (this._b24PullClient) { return this; } this.initializePullClient( typeof userId === "undefined" ? this.profileInfo.data.id || 0 : userId, prefix ); return this; } initializePullClient(userId, prefix = "prefix") { this._b24PullClient = new PullClient({ b24: this._b24, restApplication: this._b24.auth.getUniq(prefix), userId }); } subscribePullClient(callback, moduleId = "application") { if (!this._b24PullClient) { throw new Error("PullClient not init"); } this._pullClientModuleId = moduleId; this._pullClientUnSubscribe.push( this._b24PullClient.subscribe({ moduleId: this._pullClientModuleId, callback }) ); return this; } startPullClient() { if (!this._b24PullClient) { throw new Error("PullClient not init"); } this._b24PullClient.start().catch((error) => { this.getLogger().error(`${Text.getDateForLog()}: Pull not running`, { error }); }); } getModuleIdPullClient() { if (!this._b24PullClient) { throw new Error("PullClient not init"); } return this._pullClientModuleId; } _destroyPullClient() { for (const unsubscribeCallback of this._pullClientUnSubscribe) { unsubscribeCallback(); } this._b24PullClient?.destroy(); this._b24PullClient = null; } // endregion //// // region Tools //// ensureInitialized() { if (!this._isInit) { throw new Error("B24HelperManager not initialized"); } } // endregion //// } export { B24HelperManager }; //# sourceMappingURL=helper-manager.mjs.map