Skip to content

Commit

Permalink
feat(assetGroups): add lastUpdate on changes (#311)
Browse files Browse the repository at this point in the history
* feat(assetGroups): add lastUpdate on changes
* feat(assetGroupsLink): add date to asset link
* feat(assetGroups): update lastUpdate when change links
* fix(AssetsGroups): correct children group creation
wait parent update to prevent erratic result in tests
  • Loading branch information
sebtiz13 committed Aug 14, 2023
1 parent 99b1683 commit 36a4575
Show file tree
Hide file tree
Showing 9 changed files with 235 additions and 36 deletions.
72 changes: 62 additions & 10 deletions lib/modules/asset/AssetsGroupsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,12 +238,13 @@ export class AssetsGroupsController {
const children = parentGroup._source.children ?? [];
children.push(_id);

this.sdk.document.update<AssetsGroupsBody>(
await this.sdk.document.update<AssetsGroupsBody>(
engineId,
InternalCollection.ASSETS_GROUPS,
body.parent,
{
children,
lastUpdate: Date.now(),
}
);
}
Expand All @@ -253,6 +254,7 @@ export class AssetsGroupsController {
InternalCollection.ASSETS_GROUPS,
{
children: [],
lastUpdate: Date.now(),
name: body.name,
parent: body.parent ?? null,
},
Expand Down Expand Up @@ -284,7 +286,11 @@ export class AssetsGroupsController {
engineId,
InternalCollection.ASSETS_GROUPS,
_id,
{ parent: null, ...body },
{
parent: null,
...body,
lastUpdate: Date.now(),
},
{ source: true }
);
}
Expand Down Expand Up @@ -313,6 +319,7 @@ export class AssetsGroupsController {
assetGroup.parent,
{
children: parentGroup.children.filter((children) => children !== _id),
lastUpdate: Date.now(),
}
);
}
Expand All @@ -322,15 +329,18 @@ export class AssetsGroupsController {
InternalCollection.ASSETS_GROUPS,
assetGroup.children.map((childrenId) => ({
_id: childrenId,
body: { parent: null },
body: {
lastUpdate: Date.now(),
parent: null,
},
})),
{ strict: true }
);

const { hits: assets } = await this.sdk.document.search<AssetContent>(
engineId,
InternalCollection.ASSETS,
{ query: { equals: { groups: _id } } },
{ query: { equals: { "groups.id": _id } } },
{ lang: "koncorde" }
);

Expand All @@ -340,7 +350,9 @@ export class AssetsGroupsController {
assets.map((asset) => ({
_id: asset._id,
body: {
groups: asset._source.groups.filter((groupId) => groupId !== _id),
groups: asset._source.groups.filter(
({ id: groupId }) => groupId !== _id
),
},
})),
{ strict: true }
Expand Down Expand Up @@ -398,22 +410,45 @@ export class AssetsGroupsController {
}

if (assetGroup._source.parent !== null) {
assetContent.groups.push(assetGroup._source.parent);
assetContent.groups.push({
date: Date.now(),
id: assetGroup._source.parent,
});
}

assetContent.groups.push(_id);
assetContent.groups.push({
date: Date.now(),
id: _id,
});

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

return this.sdk.document.mReplace(
const assetsGroupsUpdate = await this.as(
request.getUser()
).document.update<AssetsGroupsBody>(
engineId,
InternalCollection.ASSETS_GROUPS,
_id,
{
lastUpdate: Date.now(),
},
{ source: true }
);

const update = await this.sdk.document.mReplace(
engineId,
InternalCollection.ASSETS,
assets
);

return {
...update,
assetsGroups: assetsGroupsUpdate,
};
}

async removeAsset(
Expand Down Expand Up @@ -449,7 +484,7 @@ export class AssetsGroupsController {
}

assetContent.groups = assetContent.groups.filter(
(group) => !removedGroups.includes(group)
({ id: groupId }) => !removedGroups.includes(groupId)
);

assets.push({
Expand All @@ -458,10 +493,27 @@ export class AssetsGroupsController {
});
}

return this.sdk.document.mReplace(
const assetsGroupsUpdate = await this.as(
request.getUser()
).document.update<AssetsGroupsBody>(
engineId,
InternalCollection.ASSETS_GROUPS,
_id,
{
lastUpdate: Date.now(),
},
{ source: true }
);

const update = await this.sdk.document.mReplace(
engineId,
InternalCollection.ASSETS,
assets
);

return {
...update,
assetsGroups: assetsGroupsUpdate,
};
}
}
3 changes: 3 additions & 0 deletions lib/modules/asset/collections/assetsGroupsMapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,8 @@ export const assetGroupsMappings: CollectionMappings = {
},
type: "keyword",
},
lastUpdate: {
type: "date",
},
},
};
9 changes: 7 additions & 2 deletions lib/modules/asset/collections/assetsMappings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@ export const assetsMappings: CollectionMappings = {
fields: { text: { type: "text" } },
},
groups: {
type: "keyword",
fields: { text: { type: "text" } },
properties: {
id: {
type: "keyword",
fields: { text: { type: "text" } },
},
date: { type: "date" },
},
},

metadata: {
Expand Down
5 changes: 4 additions & 1 deletion lib/modules/asset/types/AssetContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ export interface AssetContent<
/**
* Id's of asset groups
*/
groups: string[];
groups: Array<{
id: string;
date: number;
}>;
}

/**
Expand Down
1 change: 1 addition & 0 deletions lib/modules/asset/types/AssetGroupContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export interface AssetsGroupsBody {
name: string;
children: string[];
parent: string | null;
lastUpdate: number;
}

export type AssetsGroupContent = AssetsGroupsBody & KDocumentContent;
14 changes: 10 additions & 4 deletions lib/modules/asset/types/AssetGroupsApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@ import {
} from "kuzzle-sdk";
import { AssetsGroupsBody, AssetsGroupContent } from "./AssetGroupContent";

// Remove "lastUpdate" property for request
type AssetsGroupsRequest = Omit<AssetsGroupsBody, "lastUpdate">;
// Make "parent" property to optional for request
export type AssetsGroupsBodyRequest = Partial<AssetsGroupsBody> &
Omit<AssetsGroupsBody, "parent">;
export type AssetsGroupsBodyRequest = Partial<AssetsGroupsRequest> &
Omit<AssetsGroupsRequest, "parent">;

export type UpdateAssetLinkResponse = mUpdateResponse & {
assetsGroups: KDocument<AssetsGroupContent>;
};

interface GroupControllerRequest {
controller: "device-manager/assetsGroup";
Expand Down Expand Up @@ -63,7 +69,7 @@ export interface ApiGroupAddAssetsRequest extends GroupControllerRequest {
assetIds: string[];
};
}
export type ApiGroupAddAssetsResult = mUpdateResponse;
export type ApiGroupAddAssetsResult = UpdateAssetLinkResponse;

export interface ApiGroupRemoveAssetsRequest extends GroupControllerRequest {
action: "removeAsset";
Expand All @@ -72,4 +78,4 @@ export interface ApiGroupRemoveAssetsRequest extends GroupControllerRequest {
assetIds: string[];
};
}
export type ApiGroupRemoveAssetsResult = mUpdateResponse;
export type ApiGroupRemoveAssetsResult = UpdateAssetLinkResponse;
7 changes: 7 additions & 0 deletions tests/fixtures/assetsGroups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,42 +11,49 @@ export const assetGroupChildrenWithAssetId = "test-children-asset";
export const assetGroupTestBody: AssetsGroupsBody = {
name: "Test group",
children: [],
lastUpdate: Date.now(),
parent: null,
};

export const assetGroupTestParentBody1: AssetsGroupsBody = {
name: "Test parent 1",
children: [assetGroupTestChildrenId1],
lastUpdate: Date.now(),
parent: null,
};

export const assetGroupTestParentBody2: AssetsGroupsBody = {
name: "Test parent 2",
children: [assetGroupTestChildrenId2],
lastUpdate: Date.now(),
parent: null,
};

export const assetGroupTestChildrenBody1: AssetsGroupsBody = {
name: "Test children 1",
children: [],
lastUpdate: Date.now(),
parent: assetGroupTestParentId1,
};

export const assetGroupTestChildrenBody2: AssetsGroupsBody = {
name: "Test children 2",
children: [],
lastUpdate: Date.now(),
parent: assetGroupTestParentId2,
};

export const assetGroupParentWithAssetBody: AssetsGroupsBody = {
name: "Parent Group with asset",
children: [assetGroupChildrenWithAssetId],
lastUpdate: Date.now(),
parent: null,
};

export const assetGroupChildrenWithAssetBody: AssetsGroupsBody = {
name: "Children Group with asset",
children: [],
lastUpdate: Date.now(),
parent: assetGroupParentWithAssetId,
};

Expand Down
22 changes: 20 additions & 2 deletions tests/fixtures/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,16 @@ const assetAyseGrouped = {
height: 22,
},
linkedDevices: [],
groups: ["test-parent-asset", "test-children-asset"],
groups: [
{
id: "test-parent-asset",
date: Date.now(),
},
{
id: "test-children-asset",
date: Date.now(),
},
],
};
const assetAyseGroupedId = `${assetAyseGrouped.model}-${assetAyseGrouped.reference}`;

Expand All @@ -128,7 +137,16 @@ const assetAyseGrouped2 = {
height: 22,
},
linkedDevices: [],
groups: ["test-parent-asset", "test-children-asset"],
groups: [
{
id: "test-parent-asset",
date: Date.now(),
},
{
id: "test-children-asset",
date: Date.now(),
},
],
};
const assetAyseGroupedId2 = `${assetAyseGrouped2.model}-${assetAyseGrouped2.reference}`;

Expand Down
Loading

0 comments on commit 36a4575

Please sign in to comment.