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

fix(assetservice): can't replace metadata if not present in asset #384

Merged
merged 7 commits into from
Dec 5, 2024
17 changes: 15 additions & 2 deletions lib/modules/asset/AssetService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,26 @@ export class AssetService extends DigitalTwinService {
request: KuzzleRequest,
): Promise<KDocument<AssetContent>> {
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<AskModelAssetGet>(
"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<EventAssetUpdateBefore>(
"device-manager:asset:update:before",
{ asset, metadata },
Expand Down