Add endpoint to fetch all devices with detailed information

This commit is contained in:
faris Aljohari
2024-08-21 23:26:37 +03:00
parent 5cc3e88371
commit a9d0e06c02
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,
);
}
}
} }