Skip to content

Commit

Permalink
refactor: add BaseService
Browse files Browse the repository at this point in the history
  • Loading branch information
sebtiz13 committed Oct 24, 2023
1 parent b312f9e commit 3dd7dca
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 139 deletions.
21 changes: 4 additions & 17 deletions lib/modules/asset/AssetHistoryService.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,17 @@
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 {
AssetHistoryContent,
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<AskAssetHistoryAdd<AssetHistoryEvent>>(
"ask:device-manager:asset:history:add",
Expand Down
37 changes: 11 additions & 26 deletions lib/modules/asset/AssetService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { Backend, BadRequestError, PluginContext, User } from "kuzzle";
import {
BadRequestError,
EventGenericDocumentBeforeWrite,

Check warning on line 3 in lib/modules/asset/AssetService.ts

View workflow job for this annotation

GitHub Actions / Lint

'EventGenericDocumentBeforeWrite' is defined but never used
KuzzleRequest,

Check warning on line 4 in lib/modules/asset/AssetService.ts

View workflow job for this annotation

GitHub Actions / Lint

'KuzzleRequest' is defined but never used
User,
} from "kuzzle";
import {
BaseRequest,
DocumentSearchResult,
Expand All @@ -19,7 +24,6 @@ import {
import { AskModelAssetGet, AssetModelContent } from "../model";
import {
AskEngineList,
DeviceManagerConfiguration,
DeviceManagerPlugin,
InternalCollection,
} from "../plugin";
Expand All @@ -30,6 +34,7 @@ import {
flattenObject,
lock,
onAsk,
BaseService,
} from "../shared";

import { AssetHistoryService } from "./AssetHistoryService";
Expand All @@ -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();
Expand Down Expand Up @@ -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 };
}

Expand Down
34 changes: 9 additions & 25 deletions lib/modules/decoder/PayloadService.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,21 @@
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";
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<AskPayloadReceiveFormated>(
"ask:device-manager:payload:receive-formated",
Expand Down Expand Up @@ -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}`
);
}
Expand Down Expand Up @@ -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.`
Expand Down Expand Up @@ -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}`
);
}
Expand Down Expand Up @@ -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 } } }
Expand Down
33 changes: 6 additions & 27 deletions lib/modules/device/DeviceService.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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,
Expand All @@ -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();
}

Expand Down
38 changes: 12 additions & 26 deletions lib/modules/measure/MeasureService.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import {
Backend,
BadRequestError,
JSONObject,
KDocument,
PluginContext,
} from "kuzzle";
import { BadRequestError, JSONObject, KDocument } from "kuzzle";
import _ from "lodash";

import {
Expand All @@ -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 {
Expand All @@ -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<AskMeasureIngest>(
"device-manager:measures:ingest",
Expand Down
22 changes: 4 additions & 18 deletions lib/modules/model/ModelService.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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();
}
Expand Down
29 changes: 29 additions & 0 deletions lib/modules/shared/services/BaseService.ts
Original file line number Diff line number Diff line change
@@ -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;
};
}
}
1 change: 1 addition & 0 deletions lib/modules/shared/services/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./AbstractExporter";
export * from "./BaseService";
export * from "./DigitalTwinExporter";

0 comments on commit 3dd7dca

Please sign in to comment.