From eb65c0a1ae65488033a5422ce0879e8820fe05fa Mon Sep 17 00:00:00 2001 From: QuentinRousselet <165774127+QuentinRousselet@users.noreply.github.com> Date: Thu, 5 Dec 2024 17:26:26 +0100 Subject: [PATCH] fix(assetservice): can't replace metadata if not present in asset (#384) --- lib/modules/asset/AssetService.ts | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/modules/asset/AssetService.ts b/lib/modules/asset/AssetService.ts index 992bf405..a4da4e6d 100644 --- a/lib/modules/asset/AssetService.ts +++ b/lib/modules/asset/AssetService.ts @@ -144,13 +144,26 @@ export class AssetService extends DigitalTwinService { request: KuzzleRequest, ): Promise> { const asset = await this.get(engineId, assetId, request); - + const unknownMetadata = {}; for (const key in metadata) { if (key in asset._source.metadata) { asset._source.metadata[key] = metadata[key]; + } else { + unknownMetadata[key] = metadata[key]; + } + } + // ? If metadata key is unknown on the asset we check that it exists in the assetModel mappings + if (Object.keys(unknownMetadata).length > 0) { + const assetModel = await ask( + "ask:device-manager:model:asset:get", + { engineGroup: engineId.split("-")[1], model: asset._source.model }, + ); + for (const key in unknownMetadata) { + if (key in assetModel.asset.metadataMappings) { + asset._source.metadata[key] = unknownMetadata[key]; + } } } - const updatedPayload = await this.app.trigger( "device-manager:asset:update:before", { asset, metadata },