Merge pull request #85 from SyncrowIOT/SP-442-be-implement-endpoint-to-retrieve-all-devices-in-the-project

Add endpoint to fetch all devices with detailed information
This commit is contained in:
faris Aljohari
2024-08-21 23:28:56 +03:00
committed by GitHub
2 changed files with 55 additions and 0 deletions

View File

@ -223,4 +223,17 @@ export class DeviceController {
);
}
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Get()
async getAllDevices() {
try {
return await this.deviceService.getAllDevices();
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
}

View File

@ -620,4 +620,46 @@ export class DeviceService {
);
}
}
async getAllDevices(): Promise<GetDeviceDetailsInterface[]> {
try {
const devices = await this.deviceRepository.find({
relations: [
'spaceDevice.parent',
'productDevice',
'permission',
'permission.permissionType',
],
});
const devicesData = await Promise.all(
devices.map(async (device) => {
const spaceDevice = device?.spaceDevice;
const parentDevice = spaceDevice?.parent;
return {
room: {
uuid: spaceDevice?.uuid,
name: spaceDevice?.spaceName,
},
unit: {
uuid: parentDevice?.uuid,
name: parentDevice?.spaceName,
},
productUuid: device.productDevice.uuid,
productType: device.productDevice.prodType,
permissionType: device.permission[0].permissionType.type,
...(await this.getDeviceDetailsByDeviceIdTuya(
device.deviceTuyaUuid,
)),
uuid: device.uuid,
} as GetDeviceDetailsInterface;
}),
);
return devicesData;
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
}