Skip to content

Commit

Permalink
fix(device-metadata): fetch device's metadata from assigned tenant wh…
Browse files Browse the repository at this point in the history
…en available (#374)

* fix(measure): fetch tenant device content if exist

* Update lib/modules/decoder/PayloadService.ts

---------

Co-authored-by: Eric <[email protected]>
  • Loading branch information
cyrng and etrousset authored Sep 27, 2024
1 parent aa11885 commit acfd8a0
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions lib/modules/decoder/PayloadService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,20 @@ export class PayloadService extends BaseService {
),
);

// Due to the existence of a "devices" collection in the tenant index and a platform index,
// we need to fetch the device content from the associated tenant if it exists.
const updatedDevices = await Promise.all(
devices.map((device) =>
device._source.engineId && device._source.engineId.trim() !== ""
? this.sdk.document.get<DeviceContent>(
device._source.engineId,
InternalCollection.DEVICES,
device._id,
)
: device,
),
);

// If we have unknown devices, let's check if we should register them
if (errors.length > 0) {
const { _source } = await this.sdk.document.get(
Expand All @@ -186,7 +200,7 @@ export class PayloadService extends BaseService {
const newDevices = await this.provisionDevices(deviceModel, errors, {
refresh,
});
devices.push(...newDevices);
updatedDevices.push(...newDevices);
} else {
this.app.log.info(
`Skipping new devices "${errors.join(
Expand All @@ -196,7 +210,7 @@ export class PayloadService extends BaseService {
}
}

return devices;
return updatedDevices;
}

private async provisionDevices(
Expand Down

0 comments on commit acfd8a0

Please sign in to comment.