mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-15 18:27:05 +00:00
feat(device): add endpoint to add device to user
This commit is contained in:
@ -9,6 +9,10 @@ export class DeviceDto {
|
|||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
spaceUuid: string;
|
spaceUuid: string;
|
||||||
|
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
userUuid: string;
|
||||||
|
|
||||||
@IsString()
|
@IsString()
|
||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
deviceTuyaUuid: string;
|
deviceTuyaUuid: string;
|
||||||
|
@ -5,9 +5,10 @@ import { GroupDeviceEntity } from '../../group-device/entities';
|
|||||||
import { SpaceEntity } from '../../space/entities';
|
import { SpaceEntity } from '../../space/entities';
|
||||||
import { ProductEntity } from '../../product/entities';
|
import { ProductEntity } from '../../product/entities';
|
||||||
import { DeviceUserPermissionEntity } from '../../device-user-permission/entities';
|
import { DeviceUserPermissionEntity } from '../../device-user-permission/entities';
|
||||||
|
import { UserEntity } from '../../user/entities';
|
||||||
|
|
||||||
@Entity({ name: 'device' })
|
@Entity({ name: 'device' })
|
||||||
@Unique(['spaceDevice', 'deviceTuyaUuid'])
|
@Unique(['deviceTuyaUuid'])
|
||||||
export class DeviceEntity extends AbstractEntity<DeviceDto> {
|
export class DeviceEntity extends AbstractEntity<DeviceDto> {
|
||||||
@Column({
|
@Column({
|
||||||
nullable: false,
|
nullable: false,
|
||||||
@ -20,6 +21,9 @@ export class DeviceEntity extends AbstractEntity<DeviceDto> {
|
|||||||
})
|
})
|
||||||
isActive: true;
|
isActive: true;
|
||||||
|
|
||||||
|
@ManyToOne(() => UserEntity, (user) => user.userSpaces, { nullable: false })
|
||||||
|
user: UserEntity;
|
||||||
|
|
||||||
@OneToMany(
|
@OneToMany(
|
||||||
() => DeviceUserPermissionEntity,
|
() => DeviceUserPermissionEntity,
|
||||||
(permission) => permission.device,
|
(permission) => permission.device,
|
||||||
@ -36,7 +40,7 @@ export class DeviceEntity extends AbstractEntity<DeviceDto> {
|
|||||||
userGroupDevices: GroupDeviceEntity[];
|
userGroupDevices: GroupDeviceEntity[];
|
||||||
|
|
||||||
@ManyToOne(() => SpaceEntity, (space) => space.devicesSpaceEntity, {
|
@ManyToOne(() => SpaceEntity, (space) => space.devicesSpaceEntity, {
|
||||||
nullable: false,
|
nullable: true,
|
||||||
})
|
})
|
||||||
spaceDevice: SpaceEntity;
|
spaceDevice: SpaceEntity;
|
||||||
|
|
||||||
|
@ -4,6 +4,7 @@ import { UserDto } from '../dtos';
|
|||||||
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
||||||
import { UserSpaceEntity } from '../../user-space/entities';
|
import { UserSpaceEntity } from '../../user-space/entities';
|
||||||
import { UserRoleEntity } from '../../user-role/entities';
|
import { UserRoleEntity } from '../../user-role/entities';
|
||||||
|
import { DeviceEntity } from '../../device/entities';
|
||||||
|
|
||||||
@Entity({ name: 'user' })
|
@Entity({ name: 'user' })
|
||||||
export class UserEntity extends AbstractEntity<UserDto> {
|
export class UserEntity extends AbstractEntity<UserDto> {
|
||||||
@ -53,6 +54,9 @@ export class UserEntity extends AbstractEntity<UserDto> {
|
|||||||
@OneToMany(() => UserSpaceEntity, (userSpace) => userSpace.user)
|
@OneToMany(() => UserSpaceEntity, (userSpace) => userSpace.user)
|
||||||
userSpaces: UserSpaceEntity[];
|
userSpaces: UserSpaceEntity[];
|
||||||
|
|
||||||
|
@OneToMany(() => DeviceEntity, (userDevice) => userDevice.user)
|
||||||
|
userDevice: DeviceEntity[];
|
||||||
|
|
||||||
@OneToMany(
|
@OneToMany(
|
||||||
() => DeviceUserPermissionEntity,
|
() => DeviceUserPermissionEntity,
|
||||||
(userPermission) => userPermission.user,
|
(userPermission) => userPermission.user,
|
||||||
|
@ -10,11 +10,13 @@ import {
|
|||||||
HttpStatus,
|
HttpStatus,
|
||||||
UseGuards,
|
UseGuards,
|
||||||
Req,
|
Req,
|
||||||
|
Put,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
|
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
|
||||||
import {
|
import {
|
||||||
|
AddDeviceDto,
|
||||||
AddDeviceInGroupDto,
|
AddDeviceInGroupDto,
|
||||||
AddDeviceInRoomDto,
|
UpdateDeviceInRoomDto,
|
||||||
} from '../dtos/add.device.dto';
|
} from '../dtos/add.device.dto';
|
||||||
import {
|
import {
|
||||||
GetDeviceByGroupIdDto,
|
GetDeviceByGroupIdDto,
|
||||||
@ -26,6 +28,7 @@ import { CheckGroupGuard } from 'src/guards/group.guard';
|
|||||||
import { CheckUserHavePermission } from 'src/guards/user.device.permission.guard';
|
import { CheckUserHavePermission } from 'src/guards/user.device.permission.guard';
|
||||||
import { CheckUserHaveControllablePermission } from 'src/guards/user.device.controllable.permission.guard';
|
import { CheckUserHaveControllablePermission } from 'src/guards/user.device.controllable.permission.guard';
|
||||||
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
|
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
|
||||||
|
import { CheckDeviceGuard } from 'src/guards/device.guard';
|
||||||
|
|
||||||
@ApiTags('Device Module')
|
@ApiTags('Device Module')
|
||||||
@Controller({
|
@Controller({
|
||||||
@ -34,7 +37,26 @@ import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
|
|||||||
})
|
})
|
||||||
export class DeviceController {
|
export class DeviceController {
|
||||||
constructor(private readonly deviceService: DeviceService) {}
|
constructor(private readonly deviceService: DeviceService) {}
|
||||||
|
@ApiBearerAuth()
|
||||||
|
@UseGuards(JwtAuthGuard, CheckDeviceGuard)
|
||||||
|
@Post()
|
||||||
|
async addDevice(@Body() addDeviceDto: AddDeviceDto) {
|
||||||
|
try {
|
||||||
|
const device = await this.deviceService.addDevice(addDeviceDto);
|
||||||
|
|
||||||
|
return {
|
||||||
|
statusCode: HttpStatus.CREATED,
|
||||||
|
success: true,
|
||||||
|
message: 'device added successfully',
|
||||||
|
data: device,
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
throw new HttpException(
|
||||||
|
error.message || 'Internal server error',
|
||||||
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(JwtAuthGuard, CheckRoomGuard)
|
@UseGuards(JwtAuthGuard, CheckRoomGuard)
|
||||||
@Get('room')
|
@Get('room')
|
||||||
@ -58,16 +80,19 @@ export class DeviceController {
|
|||||||
|
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(JwtAuthGuard, CheckRoomGuard)
|
@UseGuards(JwtAuthGuard, CheckRoomGuard)
|
||||||
@Post('room')
|
@Put('room')
|
||||||
async addDeviceInRoom(@Body() addDeviceInRoomDto: AddDeviceInRoomDto) {
|
async updateDeviceInRoom(
|
||||||
|
@Body() updateDeviceInRoomDto: UpdateDeviceInRoomDto,
|
||||||
|
) {
|
||||||
try {
|
try {
|
||||||
const device =
|
const device = await this.deviceService.updateDeviceInRoom(
|
||||||
await this.deviceService.addDeviceInRoom(addDeviceInRoomDto);
|
updateDeviceInRoomDto,
|
||||||
|
);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
statusCode: HttpStatus.CREATED,
|
statusCode: HttpStatus.CREATED,
|
||||||
success: true,
|
success: true,
|
||||||
message: 'device added in room successfully',
|
message: 'device updated in room successfully',
|
||||||
data: device,
|
data: device,
|
||||||
};
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import { ApiProperty } from '@nestjs/swagger';
|
import { ApiProperty } from '@nestjs/swagger';
|
||||||
import { IsNotEmpty, IsString } from 'class-validator';
|
import { IsNotEmpty, IsString } from 'class-validator';
|
||||||
|
|
||||||
export class AddDeviceInRoomDto {
|
export class AddDeviceDto {
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
description: 'deviceTuyaUuid',
|
description: 'deviceTuyaUuid',
|
||||||
required: true,
|
required: true,
|
||||||
@ -10,6 +10,23 @@ export class AddDeviceInRoomDto {
|
|||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
public deviceTuyaUuid: string;
|
public deviceTuyaUuid: string;
|
||||||
|
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'userUuid',
|
||||||
|
required: true,
|
||||||
|
})
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public userUuid: string;
|
||||||
|
}
|
||||||
|
export class UpdateDeviceInRoomDto {
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'deviceUuid',
|
||||||
|
required: true,
|
||||||
|
})
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public deviceUuid: string;
|
||||||
|
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
description: 'roomUuid',
|
description: 'roomUuid',
|
||||||
required: true,
|
required: true,
|
||||||
|
@ -8,8 +8,9 @@ import {
|
|||||||
import { TuyaContext } from '@tuya/tuya-connector-nodejs';
|
import { TuyaContext } from '@tuya/tuya-connector-nodejs';
|
||||||
import { ConfigService } from '@nestjs/config';
|
import { ConfigService } from '@nestjs/config';
|
||||||
import {
|
import {
|
||||||
|
AddDeviceDto,
|
||||||
AddDeviceInGroupDto,
|
AddDeviceInGroupDto,
|
||||||
AddDeviceInRoomDto,
|
UpdateDeviceInRoomDto,
|
||||||
} from '../dtos/add.device.dto';
|
} from '../dtos/add.device.dto';
|
||||||
import {
|
import {
|
||||||
DeviceInstructionResponse,
|
DeviceInstructionResponse,
|
||||||
@ -47,6 +48,38 @@ export class DeviceService {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async addDevice(addDeviceDto: AddDeviceDto) {
|
||||||
|
try {
|
||||||
|
const device = await this.getDeviceDetailsByDeviceIdTuya(
|
||||||
|
addDeviceDto.deviceTuyaUuid,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!device.productUuid) {
|
||||||
|
throw new Error('Product UUID is missing for the device.');
|
||||||
|
}
|
||||||
|
|
||||||
|
return await this.deviceRepository.save({
|
||||||
|
deviceTuyaUuid: addDeviceDto.deviceTuyaUuid,
|
||||||
|
productDevice: { uuid: device.productUuid },
|
||||||
|
user: {
|
||||||
|
uuid: addDeviceDto.userUuid,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
if (error.code === '23505') {
|
||||||
|
throw new HttpException(
|
||||||
|
'Device already exists',
|
||||||
|
HttpStatus.BAD_REQUEST,
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
throw new HttpException(
|
||||||
|
error.message || 'Failed to add device in room',
|
||||||
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async getDevicesByRoomId(
|
async getDevicesByRoomId(
|
||||||
getDeviceByRoomUuidDto: GetDeviceByRoomUuidDto,
|
getDeviceByRoomUuidDto: GetDeviceByRoomUuidDto,
|
||||||
userUuid: string,
|
userUuid: string,
|
||||||
@ -92,7 +125,31 @@ export class DeviceService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
async updateDeviceInRoom(updateDeviceInRoomDto: UpdateDeviceInRoomDto) {
|
||||||
|
try {
|
||||||
|
await this.deviceRepository.update(
|
||||||
|
{ uuid: updateDeviceInRoomDto.deviceUuid },
|
||||||
|
{
|
||||||
|
spaceDevice: { uuid: updateDeviceInRoomDto.roomUuid },
|
||||||
|
},
|
||||||
|
);
|
||||||
|
const device = await this.deviceRepository.findOne({
|
||||||
|
where: {
|
||||||
|
uuid: updateDeviceInRoomDto.deviceUuid,
|
||||||
|
},
|
||||||
|
relations: ['spaceDevice'],
|
||||||
|
});
|
||||||
|
return {
|
||||||
|
uuid: device.uuid,
|
||||||
|
roomUuid: device.spaceDevice.uuid,
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
throw new HttpException(
|
||||||
|
'Failed to add device in room',
|
||||||
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
async getDevicesByGroupId(
|
async getDevicesByGroupId(
|
||||||
getDeviceByGroupIdDto: GetDeviceByGroupIdDto,
|
getDeviceByGroupIdDto: GetDeviceByGroupIdDto,
|
||||||
userUuid: string,
|
userUuid: string,
|
||||||
@ -126,7 +183,6 @@ export class DeviceService {
|
|||||||
uuid: device.device.uuid,
|
uuid: device.device.uuid,
|
||||||
productUuid: device.device.productDevice.uuid,
|
productUuid: device.device.productDevice.uuid,
|
||||||
productType: device.device.productDevice.prodType,
|
productType: device.device.productDevice.prodType,
|
||||||
permissionType: device.device.permission[0].permissionType.type,
|
|
||||||
} as GetDeviceDetailsInterface;
|
} as GetDeviceDetailsInterface;
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
@ -139,35 +195,6 @@ export class DeviceService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
async addDeviceInRoom(addDeviceInRoomDto: AddDeviceInRoomDto) {
|
|
||||||
try {
|
|
||||||
const device = await this.getDeviceDetailsByDeviceIdTuya(
|
|
||||||
addDeviceInRoomDto.deviceTuyaUuid,
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!device.productUuid) {
|
|
||||||
throw new Error('Product UUID is missing for the device.');
|
|
||||||
}
|
|
||||||
|
|
||||||
return await this.deviceRepository.save({
|
|
||||||
deviceTuyaUuid: addDeviceInRoomDto.deviceTuyaUuid,
|
|
||||||
spaceDevice: { uuid: addDeviceInRoomDto.roomUuid },
|
|
||||||
productDevice: { uuid: device.productUuid },
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
if (error.code === '23505') {
|
|
||||||
throw new HttpException(
|
|
||||||
'Device already exists in the room',
|
|
||||||
HttpStatus.BAD_REQUEST,
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
throw new HttpException(
|
|
||||||
'Failed to add device in room',
|
|
||||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async addDeviceInGroup(addDeviceInGroupDto: AddDeviceInGroupDto) {
|
async addDeviceInGroup(addDeviceInGroupDto: AddDeviceInGroupDto) {
|
||||||
try {
|
try {
|
||||||
|
89
src/guards/device.guard.ts
Normal file
89
src/guards/device.guard.ts
Normal file
@ -0,0 +1,89 @@
|
|||||||
|
import {
|
||||||
|
CanActivate,
|
||||||
|
ExecutionContext,
|
||||||
|
Injectable,
|
||||||
|
HttpStatus,
|
||||||
|
} from '@nestjs/common';
|
||||||
|
import { TuyaContext } from '@tuya/tuya-connector-nodejs';
|
||||||
|
|
||||||
|
import { BadRequestException, NotFoundException } from '@nestjs/common';
|
||||||
|
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
||||||
|
import { ConfigService } from '@nestjs/config';
|
||||||
|
import { UserRepository } from '@app/common/modules/user/repositories';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class CheckDeviceGuard implements CanActivate {
|
||||||
|
private tuya: TuyaContext;
|
||||||
|
constructor(
|
||||||
|
private readonly configService: ConfigService,
|
||||||
|
private readonly userRepository: UserRepository,
|
||||||
|
private readonly deviceRepository: DeviceRepository,
|
||||||
|
) {
|
||||||
|
const accessKey = this.configService.get<string>('auth-config.ACCESS_KEY');
|
||||||
|
const secretKey = this.configService.get<string>('auth-config.SECRET_KEY');
|
||||||
|
this.tuya = new TuyaContext({
|
||||||
|
baseUrl: 'https://openapi.tuyaeu.com',
|
||||||
|
accessKey,
|
||||||
|
secretKey,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async canActivate(context: ExecutionContext): Promise<boolean> {
|
||||||
|
const req = context.switchToHttp().getRequest();
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (req.body && req.body.userUuid && req.body.deviceTuyaUuid) {
|
||||||
|
const { userUuid, deviceTuyaUuid } = req.body;
|
||||||
|
await this.checkUserIsFound(userUuid);
|
||||||
|
await this.checkDeviceIsFoundFromTuya(deviceTuyaUuid);
|
||||||
|
} else {
|
||||||
|
throw new BadRequestException('Invalid request parameters');
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
} catch (error) {
|
||||||
|
this.handleGuardError(error, context);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async checkUserIsFound(userUuid: string) {
|
||||||
|
const user = await this.userRepository.findOne({
|
||||||
|
where: {
|
||||||
|
uuid: userUuid,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
if (!user) {
|
||||||
|
throw new NotFoundException('User not found');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
async checkDeviceIsFoundFromTuya(deviceTuyaUuid: string) {
|
||||||
|
const path = `/v1.1/iot-03/devices/${deviceTuyaUuid}`;
|
||||||
|
const response = await this.tuya.request({
|
||||||
|
method: 'GET',
|
||||||
|
path,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.success) {
|
||||||
|
throw new NotFoundException('Device not found from Tuya');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private handleGuardError(error: Error, context: ExecutionContext) {
|
||||||
|
const response = context.switchToHttp().getResponse();
|
||||||
|
if (error instanceof NotFoundException) {
|
||||||
|
response
|
||||||
|
.status(HttpStatus.NOT_FOUND)
|
||||||
|
.json({ statusCode: HttpStatus.NOT_FOUND, message: error.message });
|
||||||
|
} else if (error instanceof BadRequestException) {
|
||||||
|
response
|
||||||
|
.status(HttpStatus.BAD_REQUEST)
|
||||||
|
.json({ statusCode: HttpStatus.BAD_REQUEST, message: error.message });
|
||||||
|
} else {
|
||||||
|
response.status(HttpStatus.BAD_REQUEST).json({
|
||||||
|
statusCode: HttpStatus.BAD_REQUEST,
|
||||||
|
message: error.message || 'Invalid UUID',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -74,7 +74,7 @@ export class CheckGroupGuard implements CanActivate {
|
|||||||
} else {
|
} else {
|
||||||
response.status(HttpStatus.BAD_REQUEST).json({
|
response.status(HttpStatus.BAD_REQUEST).json({
|
||||||
statusCode: HttpStatus.BAD_REQUEST,
|
statusCode: HttpStatus.BAD_REQUEST,
|
||||||
message: 'Invalid UUID',
|
message: error.message || 'Invalid UUID',
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,29 +4,17 @@ import {
|
|||||||
Injectable,
|
Injectable,
|
||||||
HttpStatus,
|
HttpStatus,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { TuyaContext } from '@tuya/tuya-connector-nodejs';
|
|
||||||
|
|
||||||
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||||
import { BadRequestException, NotFoundException } from '@nestjs/common';
|
import { BadRequestException, NotFoundException } from '@nestjs/common';
|
||||||
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
||||||
import { ConfigService } from '@nestjs/config';
|
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class CheckRoomGuard implements CanActivate {
|
export class CheckRoomGuard implements CanActivate {
|
||||||
private tuya: TuyaContext;
|
|
||||||
constructor(
|
constructor(
|
||||||
private readonly configService: ConfigService,
|
|
||||||
private readonly spaceRepository: SpaceRepository,
|
private readonly spaceRepository: SpaceRepository,
|
||||||
private readonly deviceRepository: DeviceRepository,
|
private readonly deviceRepository: DeviceRepository,
|
||||||
) {
|
) {}
|
||||||
const accessKey = this.configService.get<string>('auth-config.ACCESS_KEY');
|
|
||||||
const secretKey = this.configService.get<string>('auth-config.SECRET_KEY');
|
|
||||||
this.tuya = new TuyaContext({
|
|
||||||
baseUrl: 'https://openapi.tuyaeu.com',
|
|
||||||
accessKey,
|
|
||||||
secretKey,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async canActivate(context: ExecutionContext): Promise<boolean> {
|
async canActivate(context: ExecutionContext): Promise<boolean> {
|
||||||
const req = context.switchToHttp().getRequest();
|
const req = context.switchToHttp().getRequest();
|
||||||
@ -35,10 +23,10 @@ export class CheckRoomGuard implements CanActivate {
|
|||||||
if (req.query && req.query.roomUuid) {
|
if (req.query && req.query.roomUuid) {
|
||||||
const { roomUuid } = req.query;
|
const { roomUuid } = req.query;
|
||||||
await this.checkRoomIsFound(roomUuid);
|
await this.checkRoomIsFound(roomUuid);
|
||||||
} else if (req.body && req.body.roomUuid && req.body.deviceTuyaUuid) {
|
} else if (req.body && req.body.roomUuid && req.body.deviceUuid) {
|
||||||
const { roomUuid, deviceTuyaUuid } = req.body;
|
const { roomUuid, deviceUuid } = req.body;
|
||||||
await this.checkRoomIsFound(roomUuid);
|
await this.checkRoomIsFound(roomUuid);
|
||||||
await this.checkDeviceIsFoundFromTuya(deviceTuyaUuid);
|
await this.checkDeviceIsFound(deviceUuid);
|
||||||
} else {
|
} else {
|
||||||
throw new BadRequestException('Invalid request parameters');
|
throw new BadRequestException('Invalid request parameters');
|
||||||
}
|
}
|
||||||
@ -63,14 +51,14 @@ export class CheckRoomGuard implements CanActivate {
|
|||||||
throw new NotFoundException('Room not found');
|
throw new NotFoundException('Room not found');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
async checkDeviceIsFoundFromTuya(deviceTuyaUuid: string) {
|
async checkDeviceIsFound(deviceUuid: string) {
|
||||||
const path = `/v1.1/iot-03/devices/${deviceTuyaUuid}`;
|
const response = await this.deviceRepository.findOne({
|
||||||
const response = await this.tuya.request({
|
where: {
|
||||||
method: 'GET',
|
uuid: deviceUuid,
|
||||||
path,
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!response.success) {
|
if (!response.uuid) {
|
||||||
throw new NotFoundException('Device not found');
|
throw new NotFoundException('Device not found');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -88,7 +76,7 @@ export class CheckRoomGuard implements CanActivate {
|
|||||||
} else {
|
} else {
|
||||||
response.status(HttpStatus.BAD_REQUEST).json({
|
response.status(HttpStatus.BAD_REQUEST).json({
|
||||||
statusCode: HttpStatus.BAD_REQUEST,
|
statusCode: HttpStatus.BAD_REQUEST,
|
||||||
message: 'Invalid UUID',
|
message: error.message || 'Invalid UUID',
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user