mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-16 02:36:19 +00:00
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:
@ -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,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -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,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user