Skip to content

Commit

Permalink
refactor(devicemanagerengine): implement the logic in the onDelete of…
Browse files Browse the repository at this point in the history
… deviceManagerEngine
  • Loading branch information
QuentinRousselet committed Nov 22, 2024
1 parent b9164b2 commit 2497436
Showing 1 changed file with 37 additions and 2 deletions.
39 changes: 37 additions & 2 deletions lib/modules/plugin/DeviceManagerEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,15 +327,14 @@ export class DeviceManagerEngine extends AbstractEngine<DeviceManagerPlugin> {
InternalCollection.DEVICES,
InternalCollection.MEASURES,
];

await Promise.all(
collections.map(async (collection) => {
if (await this.sdk.collection.exists(index, collection)) {
await this.sdk.collection.delete(index, collection);
}
}),
);

await this.detachDevicesFromAdminIndex(index);
return {
collections,
};
Expand Down Expand Up @@ -654,4 +653,40 @@ export class DeviceManagerEngine extends AbstractEngine<DeviceManagerPlugin> {

return result.hits.map((elt) => elt._source);
}

/**
* Detach all devices of an index in the admin index
*
* @param engineId The target engine Id
* @returns {any}
*/
private async detachDevicesFromAdminIndex(engineId: string) {
const devices = [];

let result = await this.sdk.document.search(
this.adminIndex,
"devices",
{
_source: false,
query: { bool: { must: { term: { engineId } } } },
},
{
scroll: "2s",
size: 100,
},
);
while (result !== null) {
devices.push(...result.hits);
result = await result.next();
}
if (devices.length > 0) {
void this.sdk.document.mUpdate(
this.adminIndex,
"devices",
devices.map((device) => {
return { _id: device._id, body: { assetId: null, engineId: null } };
}),
);
}
}
}

0 comments on commit 2497436

Please sign in to comment.