diff --git a/src/device/controllers/device.controller.ts b/src/device/controllers/device.controller.ts index 5f1a7ae..e2aa32c 100644 --- a/src/device/controllers/device.controller.ts +++ b/src/device/controllers/device.controller.ts @@ -12,11 +12,8 @@ import { Put, } from '@nestjs/common'; import { ApiTags, ApiBearerAuth } from '@nestjs/swagger'; -import { AddDeviceDto, UpdateDeviceInRoomDto } from '../dtos/add.device.dto'; -import { - GetDeviceByRoomUuidDto, - GetDeviceLogsDto, -} from '../dtos/get.device.dto'; +import { AddDeviceDto, UpdateDeviceInSpaceDto } from '../dtos/add.device.dto'; +import { GetDeviceLogsDto } from '../dtos/get.device.dto'; import { ControlDeviceDto, BatchControlDevicesDto, @@ -30,7 +27,6 @@ import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard'; import { CheckDeviceGuard } from 'src/guards/device.guard'; import { SuperAdminRoleGuard } from 'src/guards/super.admin.role.guard'; import { EnableDisableStatusEnum } from '@app/common/constants/days.enum'; -import { SpaceType } from '@app/common/constants/space-type.enum'; @ApiTags('Device Module') @Controller({ @@ -58,33 +54,21 @@ export class DeviceController { async getDevicesByUser(@Param('userUuid') userUuid: string) { return await this.deviceService.getDevicesByUser(userUuid); } - @ApiBearerAuth() - @UseGuards(JwtAuthGuard, CheckRoomGuard) - @Get(SpaceType.ROOM) - async getDevicesByRoomId( - @Query() getDeviceByRoomUuidDto: GetDeviceByRoomUuidDto, - @Req() req: any, - ) { - const userUuid = req.user.uuid; - return await this.deviceService.getDevicesByRoomId( - getDeviceByRoomUuidDto, - userUuid, - ); - } + @ApiBearerAuth() @UseGuards(JwtAuthGuard) - @Get('unit/:unitUuid') - async getDevicesByUnitId(@Param('unitUuid') unitUuid: string) { - return await this.deviceService.getDevicesByUnitId(unitUuid); + @Get('space/:spaceUuid') + async getDevicesByUnitId(@Param('spaceUuid') spaceUuid: string) { + return await this.deviceService.getDevicesBySpaceUuid(spaceUuid); } @ApiBearerAuth() @UseGuards(JwtAuthGuard, CheckRoomGuard) - @Put('room') + @Put('space') async updateDeviceInRoom( - @Body() updateDeviceInRoomDto: UpdateDeviceInRoomDto, + @Body() updateDeviceInSpaceDto: UpdateDeviceInSpaceDto, ) { - const device = await this.deviceService.updateDeviceInRoom( - updateDeviceInRoomDto, + const device = await this.deviceService.updateDeviceInSpace( + updateDeviceInSpaceDto, ); return { diff --git a/src/device/dtos/add.device.dto.ts b/src/device/dtos/add.device.dto.ts index f732317..bff522f 100644 --- a/src/device/dtos/add.device.dto.ts +++ b/src/device/dtos/add.device.dto.ts @@ -18,7 +18,7 @@ export class AddDeviceDto { @IsNotEmpty() public userUuid: string; } -export class UpdateDeviceInRoomDto { +export class UpdateDeviceInSpaceDto { @ApiProperty({ description: 'deviceUuid', required: true, @@ -28,10 +28,10 @@ export class UpdateDeviceInRoomDto { public deviceUuid: string; @ApiProperty({ - description: 'roomUuid', + description: 'spaceUuid', required: true, }) @IsString() @IsNotEmpty() - public roomUuid: string; + public spaceUuid: string; } diff --git a/src/device/dtos/get.device.dto.ts b/src/device/dtos/get.device.dto.ts index 13204de..f3ce5cc 100644 --- a/src/device/dtos/get.device.dto.ts +++ b/src/device/dtos/get.device.dto.ts @@ -1,14 +1,14 @@ import { ApiProperty } from '@nestjs/swagger'; import { IsNotEmpty, IsOptional, IsString } from 'class-validator'; -export class GetDeviceByRoomUuidDto { +export class GetDeviceBySpaceUuidDto { @ApiProperty({ - description: 'roomUuid', + description: 'spaceUuid', required: true, }) @IsString() @IsNotEmpty() - public roomUuid: string; + public spaceUuid: string; } export class GetDeviceLogsDto { @ApiProperty({ diff --git a/src/device/services/device.service.ts b/src/device/services/device.service.ts index 2b37df8..9681632 100644 --- a/src/device/services/device.service.ts +++ b/src/device/services/device.service.ts @@ -8,7 +8,7 @@ import { } from '@nestjs/common'; import { TuyaContext } from '@tuya/tuya-connector-nodejs'; import { ConfigService } from '@nestjs/config'; -import { AddDeviceDto, UpdateDeviceInRoomDto } from '../dtos/add.device.dto'; +import { AddDeviceDto, UpdateDeviceInSpaceDto } from '../dtos/add.device.dto'; import { DeviceInstructionResponse, GetDeviceDetailsFunctionsInterface, @@ -19,7 +19,7 @@ import { updateDeviceFirmwareInterface, } from '../interfaces/get.device.interface'; import { - GetDeviceByRoomUuidDto, + GetDeviceBySpaceUuidDto, GetDeviceLogsDto, } from '../dtos/get.device.dto'; import { @@ -69,6 +69,7 @@ export class DeviceService { ...(withProductDevice && { relations: ['productDevice'] }), }); } + async getDeviceByDeviceTuyaUuid(deviceTuyaUuid: string) { return await this.deviceRepository.findOne({ where: { @@ -77,6 +78,7 @@ export class DeviceService { relations: ['productDevice'], }); } + async addDeviceUser(addDeviceDto: AddDeviceDto) { try { const device = await this.getDeviceDetailsByDeviceIdTuya( @@ -116,12 +118,13 @@ export class DeviceService { ); } else { throw new HttpException( - error.message || 'Failed to add device in room', + error.message || 'Failed to add device in space', error.status || HttpStatus.INTERNAL_SERVER_ERROR, ); } } } + async getDevicesByUser( userUuid: string, ): Promise { @@ -168,14 +171,15 @@ export class DeviceService { ); } } - async getDevicesByRoomId( - getDeviceByRoomUuidDto: GetDeviceByRoomUuidDto, + + async getDevicesBySpaceId( + getDeviceBySpaceUuidDto: GetDeviceBySpaceUuidDto, userUuid: string, ): Promise { try { const devices = await this.deviceRepository.find({ where: { - spaceDevice: { uuid: getDeviceByRoomUuidDto.roomUuid }, + spaceDevice: { uuid: getDeviceBySpaceUuidDto.spaceUuid }, isActive: true, permission: { userUuid, @@ -210,22 +214,22 @@ export class DeviceService { } catch (error) { // Handle the error here throw new HttpException( - 'Error fetching devices by room', + 'Error fetching devices by space', HttpStatus.INTERNAL_SERVER_ERROR, ); } } - async updateDeviceInRoom(updateDeviceInRoomDto: UpdateDeviceInRoomDto) { + async updateDeviceInSpace(updateDeviceInSpaceDto: UpdateDeviceInSpaceDto) { try { await this.deviceRepository.update( - { uuid: updateDeviceInRoomDto.deviceUuid }, + { uuid: updateDeviceInSpaceDto.deviceUuid }, { - spaceDevice: { uuid: updateDeviceInRoomDto.roomUuid }, + spaceDevice: { uuid: updateDeviceInSpaceDto.spaceUuid }, }, ); const device = await this.deviceRepository.findOne({ where: { - uuid: updateDeviceInRoomDto.deviceUuid, + uuid: updateDeviceInSpaceDto.deviceUuid, }, relations: ['spaceDevice', 'spaceDevice.parent'], }); @@ -238,15 +242,16 @@ export class DeviceService { return { uuid: device.uuid, - roomUuid: device.spaceDevice.uuid, + spaceUuid: device.spaceDevice.uuid, }; } catch (error) { throw new HttpException( - 'Failed to add device in room', + 'Failed to add device in space', HttpStatus.INTERNAL_SERVER_ERROR, ); } } + async transferDeviceInSpacesTuya( deviceId: string, spaceId: string, @@ -294,6 +299,7 @@ export class DeviceService { ); } } + async factoryResetDeviceTuya( deviceUuid: string, ): Promise { @@ -336,6 +342,7 @@ export class DeviceService { ); } } + async batchControlDevices(batchControlDevicesDto: BatchControlDevicesDto) { const { devicesUuid } = batchControlDevicesDto; @@ -777,12 +784,12 @@ export class DeviceService { ); } } - async getDevicesByUnitId(unitUuid: string) { + async getDevicesBySpaceUuid(SpaceUuid: string) { try { const spaces = await this.spaceRepository.find({ where: { parent: { - uuid: unitUuid, + uuid: SpaceUuid, }, devicesSpaceEntity: { isActive: true, @@ -813,7 +820,7 @@ export class DeviceService { return devicesData; } catch (error) { throw new HttpException( - 'This unit does not have any devices', + 'This space does not have any devices', HttpStatus.NOT_FOUND, ); } @@ -874,16 +881,12 @@ export class DeviceService { } } const spaceDevice = device?.spaceDevice; - const parentDevice = spaceDevice?.parent; return { - room: { + space: { 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,