Skip to content

Commit

Permalink
feat(assetGroups): add lastUpdate on changes
Browse files Browse the repository at this point in the history
  • Loading branch information
sebtiz13 committed Jul 17, 2023
1 parent c49e5c7 commit 7681977
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 4 deletions.
14 changes: 12 additions & 2 deletions lib/modules/asset/AssetsGroupsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ export class AssetsGroupsController {
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,7 +329,10 @@ export class AssetsGroupsController {
InternalCollection.ASSETS_GROUPS,
assetGroup.children.map((childrenId) => ({
_id: childrenId,
body: { parent: null },
body: {
lastUpdate: Date.now(),
parent: null,
},
})),
{ strict: true }
);
Expand Down
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",
},
},
};
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;
6 changes: 4 additions & 2 deletions lib/modules/asset/types/AssetGroupsApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ 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">;

interface GroupControllerRequest {
controller: "device-manager/assetsGroup";
Expand Down
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
8 changes: 8 additions & 0 deletions tests/scenario/modules/assets/asset-group.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ jest.setTimeout(10000);

describe("AssetsGroupsController", () => {
const sdk = setupHooks();
const now = Date.now();

it("can create a group", async () => {
const missingBodyQuery: Omit<ApiGroupCreateRequest, "body"> = {
Expand Down Expand Up @@ -111,6 +112,7 @@ describe("AssetsGroupsController", () => {
children: [],
parent: null,
});
expect(assetGroupRoot._source.lastUpdate).toBeGreaterThanOrEqual(now);

const { result: assetGroupChildren } = await sdk.query<
ApiGroupCreateRequest,
Expand All @@ -135,13 +137,15 @@ describe("AssetsGroupsController", () => {
expect(rootGroup).toMatchObject({
children: ["children-group"],
});
expect(rootGroup.lastUpdate).toBeGreaterThanOrEqual(now);

expect(assetGroupChildren._id).toBe("children-group");
expect(assetGroupChildren._source).toMatchObject({
name: "children group",
children: [],
parent: "root-group",
});
expect(assetGroupChildren._source.lastUpdate).toBeGreaterThanOrEqual(now);

const { result: assetGroupWithoutIdSpecified } = await sdk.query<
ApiGroupCreateRequest,
Expand Down Expand Up @@ -275,6 +279,7 @@ describe("AssetsGroupsController", () => {
children: [],
parent: null,
});
expect(result._source.lastUpdate).toBeGreaterThanOrEqual(now);

const { result: resultChildren } = await sdk.query<ApiGroupUpdateRequest>({
controller: "device-manager/assetsGroup",
Expand All @@ -293,6 +298,7 @@ describe("AssetsGroupsController", () => {
children: [assetGroupTestChildrenId1],
parent: null,
});
expect(resultChildren._source.lastUpdate).toBeGreaterThanOrEqual(now);
});

it("can delete a group", async () => {
Expand Down Expand Up @@ -332,6 +338,7 @@ describe("AssetsGroupsController", () => {
expect(childrenGroup).toMatchObject({
parent: null,
});
expect(childrenGroup.lastUpdate).toBeGreaterThanOrEqual(now);

await sdk.query<ApiGroupDeleteRequest>({
controller: "device-manager/assetsGroup",
Expand All @@ -349,6 +356,7 @@ describe("AssetsGroupsController", () => {
expect(parentGroup).toMatchObject({
children: [],
});
expect(parentGroup.lastUpdate).toBeGreaterThanOrEqual(now);

await sdk.query<ApiGroupDeleteRequest>({
controller: "device-manager/assetsGroup",
Expand Down

0 comments on commit 7681977

Please sign in to comment.