diff --git a/lib/modules/asset/AssetHistoryService.ts b/lib/modules/asset/AssetHistoryService.ts index 191add01..d669cd1c 100644 --- a/lib/modules/asset/AssetHistoryService.ts +++ b/lib/modules/asset/AssetHistoryService.ts @@ -1,12 +1,7 @@ -import { PluginContext } from "kuzzle"; import { mCreateRequest } from "kuzzle-sdk"; -import { - DeviceManagerConfiguration, - DeviceManagerPlugin, - InternalCollection, -} from "../plugin"; -import { onAsk } from "../shared"; +import { DeviceManagerPlugin, InternalCollection } from "../plugin"; +import { onAsk, BaseService } from "../shared"; import { AskAssetHistoryAdd } from "./types/AssetEvents"; import { @@ -14,17 +9,9 @@ import { AssetHistoryEvent, } from "./types/AssetHistoryContent"; -export class AssetHistoryService { - private context: PluginContext; - private config: DeviceManagerConfiguration; - - private get sdk() { - return this.context.accessors.sdk; - } - +export class AssetHistoryService extends BaseService { constructor(plugin: DeviceManagerPlugin) { - this.context = plugin.context; - this.config = plugin.config; + super(plugin); onAsk>( "ask:device-manager:asset:history:add", diff --git a/lib/modules/asset/AssetService.ts b/lib/modules/asset/AssetService.ts index 461be2ec..05669225 100644 --- a/lib/modules/asset/AssetService.ts +++ b/lib/modules/asset/AssetService.ts @@ -1,4 +1,9 @@ -import { Backend, BadRequestError, PluginContext, User } from "kuzzle"; +import { + BadRequestError, + EventGenericDocumentBeforeWrite, + KuzzleRequest, + User, +} from "kuzzle"; import { BaseRequest, DocumentSearchResult, @@ -19,7 +24,6 @@ import { import { AskModelAssetGet, AssetModelContent } from "../model"; import { AskEngineList, - DeviceManagerConfiguration, DeviceManagerPlugin, InternalCollection, } from "../plugin"; @@ -30,6 +34,7 @@ import { flattenObject, lock, onAsk, + BaseService, } from "../shared"; import { AssetHistoryService } from "./AssetHistoryService"; @@ -46,35 +51,15 @@ import { } from "./types/AssetHistoryContent"; import { ApiAssetMigrateTenantResult } from "./types/AssetApi"; -export class AssetService { - private context: PluginContext; - private config: DeviceManagerConfiguration; +export class AssetService extends BaseService { private assetHistoryService: AssetHistoryService; - private get sdk() { - return this.context.accessors.sdk; - } - - private get app(): Backend { - return global.app; - } - - private get impersonatedSdk() { - return (user: User) => { - if (user?._id) { - return this.sdk.as(user, { checkRights: false }); - } - - return this.sdk; - }; - } - constructor( plugin: DeviceManagerPlugin, assetHistoryService: AssetHistoryService ) { - this.context = plugin.context; - this.config = plugin.config; + super(plugin); + this.assetHistoryService = assetHistoryService; this.registerAskEvents(); @@ -327,7 +312,7 @@ export class AssetService { errors = errors.concat(...assets.errors); if (assets.successes.length === 0) { - this.context.log.error("No assets found to migrate"); + this.app.log.error("No assets found to migrate"); return { errors, successes }; } diff --git a/lib/modules/decoder/PayloadService.ts b/lib/modules/decoder/PayloadService.ts index f24be551..f82bef83 100644 --- a/lib/modules/decoder/PayloadService.ts +++ b/lib/modules/decoder/PayloadService.ts @@ -1,15 +1,11 @@ -import { Backend, BadRequestError, KuzzleRequest, PluginContext } from "kuzzle"; +import { BadRequestError, KuzzleRequest } from "kuzzle"; import { JSONObject, KDocument } from "kuzzle-sdk"; import { v4 as uuidv4 } from "uuid"; -import { - DeviceManagerPlugin, - DeviceManagerConfiguration, - InternalCollection, -} from "../plugin"; +import { DeviceManagerPlugin, InternalCollection } from "../plugin"; import { DeviceContent, DeviceSerializer } from "../device"; import { AskMeasureIngest, DecodedMeasurement } from "../measure"; -import { ask, onAsk } from "../shared"; +import { BaseService, ask, onAsk } from "../shared"; import { DecodedPayload } from "./DecodedPayload"; import { Decoder } from "./Decoder"; @@ -17,21 +13,9 @@ import { AskPayloadReceiveFormated } from "./types/PayloadEvents"; import { DecodingState } from "./DecodingState"; import { SkipError } from "./SkipError"; -export class PayloadService { - private config: DeviceManagerConfiguration; - private context: PluginContext; - - private get sdk() { - return this.context.accessors.sdk; - } - - private get app(): Backend { - return global.app; - } - +export class PayloadService extends BaseService { constructor(plugin: DeviceManagerPlugin) { - this.config = plugin.config as any; - this.context = plugin.context; + super(plugin); onAsk( "ask:device-manager:payload:receive-formated", @@ -162,7 +146,7 @@ export class PayloadService { uuid ); } catch (error) { - this.context.log.error( + this.app.log.error( `Cannot save the payload from "${deviceModel}": ${error}` ); } @@ -203,7 +187,7 @@ export class PayloadService { }); devices.push(...newDevices); } else { - this.context.log.info( + this.app.log.info( `Skipping new devices "${errors.join( ", " )}". Auto-provisioning is disabled.` @@ -249,7 +233,7 @@ export class PayloadService { ); for (const error of errors) { - this.context.log.error( + this.app.log.error( `Cannot create device "${error.document._id}": ${error.reason}` ); } @@ -281,7 +265,7 @@ export class PayloadService { filter.push({ term: { deviceModel } }); } - const deleted = await this.context.accessors.sdk.bulk.deleteByQuery( + const deleted = await this.sdk.bulk.deleteByQuery( this.config.adminIndex, "payloads", { query: { bool: { filter } } } diff --git a/lib/modules/device/DeviceService.ts b/lib/modules/device/DeviceService.ts index 03ee6c04..1b65a778 100644 --- a/lib/modules/device/DeviceService.ts +++ b/lib/modules/device/DeviceService.ts @@ -1,4 +1,4 @@ -import { Backend, BadRequestError, Plugin, PluginContext, User } from "kuzzle"; +import { BadRequestError, User } from "kuzzle"; import { JSONObject, KDocument, KHit, SearchResult } from "kuzzle-sdk"; import { @@ -7,8 +7,8 @@ import { AssetHistoryEventLink, AssetHistoryEventUnlink, } from "./../asset"; -import { InternalCollection, DeviceManagerConfiguration } from "../plugin"; -import { Metadata, lock, ask, onAsk } from "../shared"; +import { InternalCollection, DeviceManagerPlugin } from "../plugin"; +import { Metadata, lock, ask, onAsk, BaseService } from "../shared"; import { AskModelAssetGet, AskModelDeviceGet, @@ -32,31 +32,10 @@ import { AskPayloadReceiveFormated } from "../decoder/types/PayloadEvents"; type MeasureName = { asset: string; device: string; type: string }; -export class DeviceService { - private config: DeviceManagerConfiguration; - private context: PluginContext; +export class DeviceService extends BaseService { + constructor(plugin: DeviceManagerPlugin) { + super(plugin); - private get sdk() { - return this.context.accessors.sdk; - } - - private get app(): Backend { - return global.app; - } - - private get impersonatedSdk() { - return (user: User) => { - if (user?._id) { - return this.sdk.as(user, { checkRights: false }); - } - - return this.sdk; - }; - } - - constructor(plugin: Plugin) { - this.config = plugin.config as any; - this.context = plugin.context; this.registerAskEvents(); } diff --git a/lib/modules/measure/MeasureService.ts b/lib/modules/measure/MeasureService.ts index 1ebae59a..60675238 100644 --- a/lib/modules/measure/MeasureService.ts +++ b/lib/modules/measure/MeasureService.ts @@ -1,10 +1,4 @@ -import { - Backend, - BadRequestError, - JSONObject, - KDocument, - PluginContext, -} from "kuzzle"; +import { BadRequestError, JSONObject, KDocument } from "kuzzle"; import _ from "lodash"; import { @@ -16,12 +10,16 @@ import { AssetSerializer, } from "../asset"; import { DeviceContent } from "../device"; +import { DeviceManagerPlugin, InternalCollection } from "../plugin"; import { - DeviceManagerConfiguration, - DeviceManagerPlugin, - InternalCollection, -} from "../plugin"; -import { ask, keepStack, lock, Metadata, objectDiff, onAsk } from "../shared"; + ask, + BaseService, + keepStack, + lock, + Metadata, + objectDiff, + onAsk, +} from "../shared"; import { DecodedMeasurement, MeasureContent } from "./types/MeasureContent"; import { @@ -34,21 +32,9 @@ import { TenantEventMeasureProcessBefore, } from "./types/MeasureEvents"; -export class MeasureService { - private config: DeviceManagerConfiguration; - private context: PluginContext; - - private get sdk() { - return this.context.accessors.sdk; - } - - private get app(): Backend { - return global.app; - } - +export class MeasureService extends BaseService { constructor(plugin: DeviceManagerPlugin) { - this.config = plugin.config as any; - this.context = plugin.context; + super(plugin); onAsk( "device-manager:measures:ingest", diff --git a/lib/modules/model/ModelService.ts b/lib/modules/model/ModelService.ts index a10cc6cf..b1ebff08 100644 --- a/lib/modules/model/ModelService.ts +++ b/lib/modules/model/ModelService.ts @@ -1,21 +1,15 @@ -import { - BadRequestError, - Inflector, - NotFoundError, - PluginContext, -} from "kuzzle"; +import { BadRequestError, Inflector, NotFoundError } from "kuzzle"; import { JSONObject, KDocument } from "kuzzle-sdk"; import { AskEngineUpdateAll, - DeviceManagerConfiguration, DeviceManagerPlugin, InternalCollection, } from "../plugin"; import { ask, onAsk } from "../shared/utils/ask"; import { AskAssetRefreshModel } from "../asset"; -import { flattenObject } from "../shared/utils/flattenObject"; +import { BaseService, flattenObject } from "../shared"; import { ModelSerializer } from "./ModelSerializer"; import { AssetModelContent, @@ -28,17 +22,9 @@ import { AskModelMeasureGet, } from "./types/ModelEvents"; -export class ModelService { - private config: DeviceManagerConfiguration; - private context: PluginContext; - - private get sdk() { - return this.context.accessors.sdk; - } - +export class ModelService extends BaseService { constructor(plugin: DeviceManagerPlugin) { - this.config = plugin.config as any; - this.context = plugin.context; + super(plugin); this.registerAskEvents(); } diff --git a/lib/modules/shared/services/BaseService.ts b/lib/modules/shared/services/BaseService.ts new file mode 100644 index 00000000..7828f9dc --- /dev/null +++ b/lib/modules/shared/services/BaseService.ts @@ -0,0 +1,29 @@ +import { Backend, EmbeddedSDK, User } from "kuzzle"; + +import { DeviceManagerPlugin, DeviceManagerConfiguration } from "../../plugin"; + +export abstract class BaseService { + constructor(private plugin: DeviceManagerPlugin) {} + + protected get app(): Backend { + return global.app; + } + + protected get sdk(): EmbeddedSDK { + return this.plugin.context.accessors.sdk; + } + + protected get config(): DeviceManagerConfiguration { + return this.plugin.config; + } + + protected get impersonatedSdk() { + return (user: User) => { + if (user?._id) { + return this.sdk.as(user, { checkRights: false }); + } + + return this.sdk; + }; + } +} diff --git a/lib/modules/shared/services/index.ts b/lib/modules/shared/services/index.ts index 2fb46bbf..df182beb 100644 --- a/lib/modules/shared/services/index.ts +++ b/lib/modules/shared/services/index.ts @@ -1,2 +1,3 @@ export * from "./AbstractExporter"; +export * from "./BaseService"; export * from "./DigitalTwinExporter";