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

feat(deviceservice): hook on tenant deletion to unlink devices #381

Closed
Prev Previous commit
feat(deviceservice): add hook on tenant deletion to set its devices a…
…s orphans
QuentinRousselet committed Nov 21, 2024
commit b9164b2477fdd02cf82ca3f21ce659865ffe030e
37 changes: 37 additions & 0 deletions lib/modules/device/DeviceService.ts
Original file line number Diff line number Diff line change
@@ -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 } };
}),
);
},
);
}

/**