Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(digitalTwin): implement generic document pipes triggers #321

Merged
merged 6 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
105 changes: 50 additions & 55 deletions lib/modules/asset/AssetService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BadRequestError, User } from "kuzzle";
import { BadRequestError, KuzzleRequest, User } from "kuzzle";
import {
BaseRequest,
DocumentSearchResult,
Expand Down Expand Up @@ -30,6 +30,7 @@ import {
lock,
onAsk,
BaseService,
SearchParams,
} from "../shared";

import { AssetHistoryService } from "./AssetHistoryService";
Expand Down Expand Up @@ -69,41 +70,43 @@ export class AssetService extends BaseService {

public async get(
engineId: string,
assetId: string
assetId: string,
request: KuzzleRequest
): Promise<KDocument<AssetContent>> {
return this.sdk.document.get<AssetContent>(
return this.getDocument<AssetContent>(request, assetId, {
collection: InternalCollection.ASSETS,
engineId,
InternalCollection.ASSETS,
assetId
);
});
}

/**
* Updates an asset metadata
*/
public async update(
user: User,
engineId: string,
assetId: string,
metadata: Metadata,
{ refresh }: { refresh: any }
request: KuzzleRequest
): Promise<KDocument<AssetContent>> {
return lock(`asset:${engineId}:${assetId}`, async () => {
const asset = await this.get(engineId, assetId);
const asset = await this.get(engineId, assetId, request);

const updatedPayload = await this.app.trigger<EventAssetUpdateBefore>(
"device-manager:asset:update:before",
{ asset, metadata }
);

const updatedAsset = await this.impersonatedSdk(
user
).document.update<AssetContent>(
engineId,
InternalCollection.ASSETS,
assetId,
{ metadata: updatedPayload.metadata },
{ refresh, source: true }
const updatedAsset = await this.updateDocument<AssetContent>(
request,
{
_id: assetId,
_source: { metadata: updatedPayload.metadata },
},
{
collection: InternalCollection.ASSETS,
engineId,
},
{ source: true }
);

await this.assetHistoryService.add<AssetHistoryEventMetadata>(engineId, [
Expand Down Expand Up @@ -133,12 +136,11 @@ export class AssetService extends BaseService {
}

public async create(
user: User,
engineId: string,
model: string,
reference: string,
metadata: JSONObject,
{ refresh }: { refresh: any }
request: KuzzleRequest
): Promise<KDocument<AssetContent>> {
const assetId = AssetSerializer.id(model, reference);

Expand Down Expand Up @@ -167,20 +169,24 @@ export class AssetService extends BaseService {
measures[name] = null;
}

const asset = await this.impersonatedSdk(
user
).document.create<AssetContent>(
engineId,
InternalCollection.ASSETS,
const asset = await this.createDocument<AssetContent>(
request,
{
linkedDevices: [],
measures,
metadata: { ...assetMetadata, ...metadata },
model,
reference,
_id: assetId,
_source: {
groups: [],
lastMeasuredAt: null,
linkedDevices: [],
measures,
metadata: { ...assetMetadata, ...metadata },
model,
reference,
},
},
assetId,
{ refresh }
{
collection: InternalCollection.ASSETS,
engineId,
}
);

await this.assetHistoryService.add<AssetHistoryEventMetadata>(engineId, [
Expand All @@ -202,13 +208,15 @@ export class AssetService extends BaseService {
}

public async delete(
user: User,
engineId: string,
assetId: string,
{ refresh, strict }: { refresh: any; strict: boolean }
request: KuzzleRequest
) {
const user = request.getUser();
const strict = request.getBoolean("strict");

return lock<void>(`asset:${engineId}:${assetId}`, async () => {
const asset = await this.get(engineId, assetId);
const asset = await this.get(engineId, assetId, request);

if (strict && asset._source.linkedDevices.length !== 0) {
throw new BadRequestError(
Expand All @@ -223,35 +231,22 @@ export class AssetService extends BaseService {
);
}

await this.sdk.document.delete(
await this.deleteDocument(request, assetId, {
collection: InternalCollection.ASSETS,
engineId,
InternalCollection.ASSETS,
assetId,
{
refresh,
}
);
});
});
}

public async search(
engineId: string,
searchBody: JSONObject,
{
from,
size,
scroll,
lang,
}: { from?: number; size?: number; scroll?: string; lang?: string }
searchParams: SearchParams,
request: KuzzleRequest
): Promise<SearchResult<KHit<AssetContent>>> {
const result = await this.sdk.document.search<AssetContent>(
return await this.searchDocument<AssetContent>(request, searchParams, {
collection: InternalCollection.ASSETS,
engineId,
InternalCollection.ASSETS,
searchBody,
{ from, lang, scroll, size }
);

return result;
});
}

public async migrateTenant(
Expand Down
43 changes: 9 additions & 34 deletions lib/modules/asset/AssetsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export class AssetsController {
const assetId = request.getId();
const engineId = request.getString("engineId");

const asset = await this.assetService.get(engineId, assetId);
const asset = await this.assetService.get(engineId, assetId, request);

return AssetSerializer.serialize(asset);
}
Expand All @@ -133,16 +133,12 @@ export class AssetsController {
const assetId = request.getId();
const engineId = request.getString("engineId");
const metadata = request.getBodyObject("metadata");
const refresh = request.getRefresh();

const updatedAsset = await this.assetService.update(
request.getUser(),
engineId,
assetId,
metadata,
{
refresh,
}
request
);

return AssetSerializer.serialize(updatedAsset);
Expand All @@ -153,17 +149,13 @@ export class AssetsController {
const model = request.getBodyString("model");
const reference = request.getBodyString("reference");
const metadata = request.getBodyObject("metadata", {});
const refresh = request.getRefresh();

const asset = await this.assetService.create(
request.getUser(),
engineId,
model,
reference,
metadata,
{
refresh,
}
request
);

return AssetSerializer.serialize(asset);
Expand All @@ -172,33 +164,16 @@ export class AssetsController {
async delete(request: KuzzleRequest): Promise<ApiAssetDeleteResult> {
const engineId = request.getString("engineId");
const assetId = request.getId();
const refresh = request.getRefresh();
const strict = request.getBoolean("strict");

await this.assetService.delete(request.getUser(), engineId, assetId, {
refresh,
strict,
});
await this.assetService.delete(engineId, assetId, request);
}

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

const result = await this.assetService.search(engineId, searchBody, {
from,
lang,
scroll,
size,
});

return result;
return await this.assetService.search(
request.getString("engineId"),
request.getSearchParams(),
request
);
}

async getMeasures(
Expand Down