diff --git a/src/device/controllers/device.controller.ts b/src/device/controllers/device.controller.ts index 9d16e2b..f9f9fab 100644 --- a/src/device/controllers/device.controller.ts +++ b/src/device/controllers/device.controller.ts @@ -40,9 +40,9 @@ export class DeviceController { @ApiBearerAuth() @UseGuards(JwtAuthGuard, CheckDeviceGuard) @Post() - async addDevice(@Body() addDeviceDto: AddDeviceDto) { + async addDeviceUser(@Body() addDeviceDto: AddDeviceDto) { try { - const device = await this.deviceService.addDevice(addDeviceDto); + const device = await this.deviceService.addDeviceUser(addDeviceDto); return { statusCode: HttpStatus.CREATED, @@ -58,6 +58,19 @@ export class DeviceController { } } @ApiBearerAuth() + @UseGuards(JwtAuthGuard) + @Get(':userUuid') + async getDevicesByUser(@Param('userUuid') userUuid: string) { + try { + return await this.deviceService.getDevicesByUser(userUuid); + } catch (error) { + throw new HttpException( + error.message || 'Internal server error', + error.status || HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + } + @ApiBearerAuth() @UseGuards(JwtAuthGuard, CheckRoomGuard) @Get('room') async getDevicesByRoomId( diff --git a/src/device/services/device.service.ts b/src/device/services/device.service.ts index b9e232e..bb90b98 100644 --- a/src/device/services/device.service.ts +++ b/src/device/services/device.service.ts @@ -48,7 +48,7 @@ export class DeviceService { }); } - async addDevice(addDeviceDto: AddDeviceDto) { + async addDeviceUser(addDeviceDto: AddDeviceDto) { try { const device = await this.getDeviceDetailsByDeviceIdTuya( addDeviceDto.deviceTuyaUuid, @@ -79,7 +79,50 @@ export class DeviceService { } } } + async getDevicesByUser( + userUuid: string, + ): Promise { + try { + const devices = await this.deviceRepository.find({ + where: { + user: { uuid: userUuid }, + permission: { + userUuid, + permissionType: { + type: In([PermissionType.READ, PermissionType.CONTROLLABLE]), + }, + }, + }, + relations: [ + 'spaceDevice', + 'productDevice', + 'permission', + 'permission.permissionType', + ], + }); + const devicesData = await Promise.all( + devices.map(async (device) => { + return { + ...(await this.getDeviceDetailsByDeviceIdTuya( + device.deviceTuyaUuid, + )), + uuid: device.uuid, + productUuid: device.productDevice.uuid, + productType: device.productDevice.prodType, + permissionType: device.permission[0].permissionType.type, + } as GetDeviceDetailsInterface; + }), + ); + return devicesData; + } catch (error) { + // Handle the error here + throw new HttpException( + 'User does not have any devices', + HttpStatus.NOT_FOUND, + ); + } + } async getDevicesByRoomId( getDeviceByRoomUuidDto: GetDeviceByRoomUuidDto, userUuid: string,