Skip to content

Commit

Permalink
feat(deviceservice): add hook on tenant deletion to set its devices a…
Browse files Browse the repository at this point in the history
…s orphans
  • Loading branch information
QuentinRousselet committed Nov 21, 2024
1 parent 53b18fc commit b9164b2
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions lib/modules/device/DeviceService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,43 @@ export class DeviceService extends DigitalTwinService {
await this.attachEngine(engineId, deviceId, request);
},
);

/**
* On deletion of a tenant, remove association for all of
* its formerly linked devices in the admin index.
*/
this.app.hook.register(
"multi-tenancy/tenant:afterDelete",
async (request) => {
const { group, name } = request.input.args;
const tenantIndex = `tenant-${group}-${name}`;
const devices = [];

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

/**
Expand Down

0 comments on commit b9164b2

Please sign in to comment.