Skip to content

Commit

Permalink
feat: implement dummy methods
Browse files Browse the repository at this point in the history
  • Loading branch information
sebtiz13 committed Jun 14, 2023
1 parent 0849ea0 commit 19c03d9
Show file tree
Hide file tree
Showing 17 changed files with 840 additions and 20 deletions.
6 changes: 6 additions & 0 deletions lib/modules/asset/AssetModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,24 @@ import { Module } from "../shared/Module";
import { AssetHistoryService } from "./AssetHistoryService";
import { AssetsController } from "./AssetsController";
import { AssetService } from "./AssetService";
import { AssetsGroupsController } from "./AssetsGroupsController";
import { RoleAssetsAdmin } from "./roles/RoleAssetsAdmin";
import { RoleAssetsReader } from "./roles/RoleAssetsReader";

export class AssetModule extends Module {
private assetService: AssetService;
private assetHistoryService: AssetHistoryService;
private assetController: AssetsController;
private assetGroupsController: AssetsGroupsController;

public async init(): Promise<void> {
this.assetHistoryService = new AssetHistoryService(this.plugin);
this.assetService = new AssetService(this.plugin, this.assetHistoryService);
this.assetController = new AssetsController(this.plugin, this.assetService);
this.assetGroupsController = new AssetsGroupsController(this.plugin);

this.plugin.api["device-manager/assetsGroup"] =
this.assetGroupsController.definition;

this.plugin.api["device-manager/assets"] = this.assetController.definition;

Expand Down
259 changes: 259 additions & 0 deletions lib/modules/asset/AssetsGroupsController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,259 @@
import {
BadRequestError,
ControllerDefinition,
EmbeddedSDK,
KuzzleRequest,
User,
} from "kuzzle";

import { DeviceManagerPlugin, InternalCollection } from "../plugin";
import {
AssetsGroupsBodyRequest,
ApiGroupAddAssetsRequest,
ApiGroupAddAssetsResult,
ApiGroupCreateResult,
ApiGroupDeleteResult,
ApiGroupGetResult,
ApiGroupSearchResult,
ApiGroupUpdateResult,
} from "./types/AssetGroupsAPI";
import {
AssetsGroupsBody,
AssetsGroupContent,
} from "./types/AssetGroupContent";

export class AssetsGroupsController {
definition: ControllerDefinition;

constructor(private plugin: DeviceManagerPlugin) {
/* eslint-disable sort-keys */
this.definition = {
actions: {
create: {
handler: this.create.bind(this),
http: [
{
path: "device-manager/:engineId/assetsGroups/:_id",
verb: "post",
},
],
},
get: {
handler: this.get.bind(this),
http: [
{ path: "device-manager/:engineId/assetsGroups/:_id", verb: "get" },
],
},
update: {
handler: this.update.bind(this),
http: [
{ path: "device-manager/:engineId/assetsGroups/:_id", verb: "put" },
],
},
delete: {
handler: this.delete.bind(this),
http: [
{
path: "device-manager/:engineId/assetsGroups/:_id",
verb: "delete",
},
],
},
search: {
handler: this.search.bind(this),
http: [
{
path: "device-manager/:engineId/assetsGroups/_search",
verb: "get",
},
{
path: "device-manager/:engineId/assetsGroups/_search",
verb: "post",
},
],
},
addAsset: {
handler: this.addAsset.bind(this),
http: [
{
path: "device-manager/:engineId/assetsGroups/:_id/addAsset",
verb: "post",
},
],
},
},
};
/* eslint-enable sort-keys */
}

private get sdk() {
return this.plugin.context.accessors.sdk;
}

private get as() {
return (user: User | null): EmbeddedSDK => {
if (user?._id) {
return this.sdk.as(user, { checkRights: true });
}
return this.sdk;
};
}

async checkParent(
engineId: string,
body: AssetsGroupsBodyRequest
): Promise<void> {
if (typeof body.parent !== "string") {
return;
}
const parentExist = await this.sdk.document.exists(
engineId,
InternalCollection.ASSETS_GROUPS,
body.parent
);
if (!parentExist) {
throw new BadRequestError(
`The parent group "${body.parent}" does not exist`
);
}
}

async checkChildren(
engineId: string,
body: AssetsGroupsBodyRequest
): Promise<void> {
if (!Array.isArray(body.children)) {
throw new BadRequestError("The Children property should be an array");
}

for (const childrenId of body.children) {
const childrenExist = await this.sdk.document.exists(
engineId,
InternalCollection.ASSETS_GROUPS,
childrenId
);
if (!childrenExist) {
throw new BadRequestError(
`The children group "${childrenId}" does not exist`
);
}
}
}

async create(request: KuzzleRequest): Promise<ApiGroupCreateResult> {
const engineId = request.getString("engineId");
const _id = request.getId();
const body = request.getBody() as AssetsGroupsBodyRequest;

await this.checkParent(engineId, body);

return this.as(request.getUser()).document.create<AssetsGroupsBody>(
engineId,
InternalCollection.ASSETS_GROUPS,
{
children: [],
name: body.name,
parent: body.parent ?? null,
},
_id
);
}

async get(request: KuzzleRequest): Promise<ApiGroupGetResult> {
const engineId = request.getString("engineId");
const _id = request.getId();

return this.as(request.getUser()).document.get<AssetsGroupsBody>(
engineId,
InternalCollection.ASSETS_GROUPS,
_id
);
}

async update(request: KuzzleRequest): Promise<ApiGroupUpdateResult> {
const engineId = request.getString("engineId");
const _id = request.getId();
const body = request.getBody() as AssetsGroupsBodyRequest;

await this.checkParent(engineId, body);
await this.checkChildren(engineId, body);

return this.as(request.getUser()).document.update<AssetsGroupsBody>(
engineId,
InternalCollection.ASSETS_GROUPS,
_id,
{ parent: null, ...body },
{ source: true }
);
}

async delete(request: KuzzleRequest): Promise<ApiGroupDeleteResult> {
const engineId = request.getString("engineId");
const _id = request.getId();

await this.as(request.getUser()).document.delete(
engineId,
InternalCollection.ASSETS_GROUPS,
_id
);
}

async search(request: KuzzleRequest): Promise<ApiGroupSearchResult> {
const engineId = request.getString("engineId");
const {
searchBody,
from,
size,
scrollTTL: scroll,
} = request.getSearchParams();
const lang = request.getLangParam();

return this.as(request.getUser()).document.search<AssetsGroupContent>(
engineId,
InternalCollection.ASSETS_GROUPS,
searchBody,
{ from, lang, scroll, size }
);
}

async addAsset(request: KuzzleRequest): Promise<ApiGroupAddAssetsResult> {
const engineId = request.getString("engineId");
const _id = request.getId();
const body = request.getBody() as ApiGroupAddAssetsRequest["body"];

// ? Get document to check if really exists, even if not indexed
await this.sdk.document.get(
engineId,
InternalCollection.ASSETS_GROUPS,
_id
);

const assets = [];
for (const assetId of body.assetIds) {
const assetContent = (
await this.sdk.document.get(
engineId,
InternalCollection.ASSETS,
assetId
)
)._source;

if (!Array.isArray(assetContent.groups)) {
assetContent.groups = [];
}

assetContent.groups.push(_id);

assets.push({
_id: assetId,
body: assetContent,
});
}

return this.sdk.document.mReplace(
engineId,
InternalCollection.ASSETS,
assets
);
}
}
31 changes: 31 additions & 0 deletions lib/modules/asset/collections/assetsGroupsMapping.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { CollectionMappings } from "kuzzle";

export const assetGroupsMappings: CollectionMappings = {
dynamic: "strict",
properties: {
children: {
fields: {
text: {
type: "text",
},
},
type: "keyword",
},
name: {
fields: {
text: {
type: "text",
},
},
type: "keyword",
},
parent: {
fields: {
text: {
type: "text",
},
},
type: "keyword",
},
},
};
8 changes: 7 additions & 1 deletion lib/modules/asset/collections/assetsMappings.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { CollectionMappings } from "kuzzle";

/**
* Base mappings for the "assets" collection.
*
* Those mappings does not contains the `measures` and `metadata` mappings.
*/
export const assetsMappings = {
export const assetsMappings: CollectionMappings = {
dynamic: "strict",
properties: {
model: {
Expand All @@ -14,6 +16,10 @@ export const assetsMappings = {
type: "keyword",
fields: { text: { type: "text" } },
},
groups: {
type: "keyword",
fields: { text: { type: "text" } },
},

metadata: {
properties: {
Expand Down
1 change: 1 addition & 0 deletions lib/modules/asset/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from "./collections/assetsMappings";
export * from "./collections/assetsHistoryMappings";
export * from "./collections/assetsGroupsMapping";
export * from "./types/AssetContent";
export * from "./types/AssetHistoryContent";
export * from "./types/AssetEvents";
Expand Down
4 changes: 4 additions & 0 deletions lib/modules/asset/types/AssetContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ export interface AssetContent<
*/
measureNames: Array<{ asset: string; device: string; type: string }>;
}>;
/**
* Id's of asset groups
*/
groups: string[];
}

/**
Expand Down
9 changes: 9 additions & 0 deletions lib/modules/asset/types/AssetGroupContent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { KDocumentContent } from "kuzzle-sdk";

export interface AssetsGroupsBody {
name: string;
children: string[];
parent: string | null;
}

export type AssetsGroupContent = AssetsGroupsBody & KDocumentContent;
Loading

0 comments on commit 19c03d9

Please sign in to comment.