Skip to content

Commit

Permalink
feat(assets-groups): implement add assets to group
Browse files Browse the repository at this point in the history
  • Loading branch information
sebtiz13 committed Jun 14, 2023
1 parent 54ebead commit 5c4d928
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 14 deletions.
33 changes: 26 additions & 7 deletions lib/modules/asset/AssetsGroupsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,13 +228,32 @@ export class AssetsGroupsController {
_id
);

// TODO implement addAsset on database
// eslint-disable-next-line no-console
console.debug({
_id,
action: request.getAction(),
body,
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
);
}
}
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
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
10 changes: 8 additions & 2 deletions lib/modules/asset/types/AssetGroupsAPI.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { JSONObject, KDocument, KHit, SearchResult } from "kuzzle-sdk";
import {
JSONObject,
KDocument,
KHit,
SearchResult,
mUpdateResponse,
} from "kuzzle-sdk";
import { AssetsGroupsBody, AssetsGroupContent } from "./AssetGroupContent";

// Make "parent" property to optional for request
Expand Down Expand Up @@ -57,4 +63,4 @@ export interface ApiGroupAddAssetsRequest extends GroupControllerRequest {
assetIds: string[];
};
}
export type ApiGroupAddAssetsResult = void;
export type ApiGroupAddAssetsResult = mUpdateResponse;
42 changes: 38 additions & 4 deletions tests/scenario/modules/assets/asset-group.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ describe("AssetsGroupsController", () => {
/^Document "bad-id" not found in "engine-ayse":"assets-groups".$/
);

const { error, status } = await sdk.query<ApiGroupAddAssetsRequest>({
const { result } = await sdk.query<ApiGroupAddAssetsRequest>({
controller: "device-manager/assetsGroup",
engineId: "engine-ayse",
action: "addAsset",
Expand All @@ -327,9 +327,43 @@ describe("AssetsGroupsController", () => {
},
});

expect({ error, status }).toStrictEqual({
error: null,
status: 200,
expect(result.errors).toHaveLength(0);

const assetsGroups = result.successes.map(({ _id, _source }) => ({
_id,
groups: _source.groups,
}));
expect(assetsGroups).toStrictEqual([
{ _id: "Container-linked1", groups: [assetGroupTestId] },
{ _id: "Container-linked2", groups: [assetGroupTestId] },
]);

// Add assets in an second group
const { result: result2 } = await sdk.query<ApiGroupAddAssetsRequest>({
controller: "device-manager/assetsGroup",
engineId: "engine-ayse",
action: "addAsset",
_id: assetGroupTestParentId,
body: {
assetIds: ["Container-linked1", "Container-linked2"],
},
});

expect(result2.errors).toHaveLength(0);

const assetsGroups2 = result2.successes.map(({ _id, _source }) => ({
_id,
groups: _source.groups,
}));
expect(assetsGroups2).toStrictEqual([
{
_id: "Container-linked1",
groups: [assetGroupTestId, assetGroupTestParentId],
},
{
_id: "Container-linked2",
groups: [assetGroupTestId, assetGroupTestParentId],
},
]);
});
});

0 comments on commit 5c4d928

Please sign in to comment.