Skip to content

Commit

Permalink
fix: improve get internal devices documents
Browse files Browse the repository at this point in the history
  • Loading branch information
sebtiz13 committed Nov 6, 2023
1 parent c2fd1f6 commit 03ccb29
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions lib/modules/device/DeviceService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,14 @@ export class DeviceService extends BaseService {
});
}

private async getInternalDevices(deviceId: string) {
return this.sdk.document.get<DeviceContent>(
this.config.adminIndex,
InternalCollection.DEVICES,
deviceId
);
}

public async update(
engineId: string,
deviceId: string,
Expand Down Expand Up @@ -315,7 +323,7 @@ export class DeviceService extends BaseService {
request: KuzzleRequest
): Promise<KDocument<DeviceContent>> {
return lock(`device:attachEngine:${deviceId}`, async () => {
const device = await this.get(this.config.adminIndex, deviceId, request);
const device = await this.getInternalDevices(deviceId);

if (device._source.engineId) {
throw new BadRequestError(
Expand Down Expand Up @@ -367,7 +375,7 @@ export class DeviceService extends BaseService {
request: KuzzleRequest
): Promise<KDocument<DeviceContent>> {
return lock(`device:detachEngine:${deviceId}`, async () => {
const device = await this.get(this.config.adminIndex, deviceId, request);
const device = await this.getInternalDevices(deviceId);

this.checkAttachedToEngine(device);

Expand Down Expand Up @@ -427,7 +435,7 @@ export class DeviceService extends BaseService {
device: KDocument<DeviceContent>;
}> {
return lock(`device:linkAsset:${deviceId}`, async () => {
const device = await this.get(this.config.adminIndex, deviceId, request);
const device = await this.getInternalDevices(deviceId);
const engine = await this.getEngine(engineId);

this.checkAttachedToEngine(device);
Expand Down Expand Up @@ -682,7 +690,7 @@ export class DeviceService extends BaseService {
device: KDocument<DeviceContent>;
}> {
return lock(`device:unlinkAsset:${deviceId}`, async () => {
const device = await this.get(this.config.adminIndex, deviceId, request);
const device = await this.getInternalDevices(deviceId);
const engineId = device._source.engineId;

this.checkAttachedToEngine(device);
Expand Down

0 comments on commit 03ccb29

Please sign in to comment.