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(device-metadata): fetch device's metadata from assigned tenant when available #374

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
// it is need to fetch the device content associated to the tenant if it exists.
etrousset marked this conversation as resolved.
Show resolved Hide resolved
const updatedDevices = await Promise.all(
devices.map((device) =>
device._source.engineId && device._source.engineId.trim() !== ""
? this.sdk.document.get<DeviceContent>(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doing get request in a map does not seems good but if there is a better solution, I'll take it.

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
Loading