feat(device): add endpoint to add device to user

This commit is contained in:
faris Aljohari
2024-05-29 23:29:53 +03:00
parent 93bab79d31
commit 423533df04
9 changed files with 223 additions and 65 deletions
+89
View 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',
});
}
}
}
+1 -1
View File
@@ -74,7 +74,7 @@ export class CheckGroupGuard implements CanActivate {
} else {
response.status(HttpStatus.BAD_REQUEST).json({
statusCode: HttpStatus.BAD_REQUEST,
message: 'Invalid UUID',
message: error.message || 'Invalid UUID',
});
}
}
+11 -23
View File
@@ -4,29 +4,17 @@ import {
Injectable,
HttpStatus,
} from '@nestjs/common';
import { TuyaContext } from '@tuya/tuya-connector-nodejs';
import { SpaceRepository } from '@app/common/modules/space/repositories';
import { BadRequestException, NotFoundException } from '@nestjs/common';
import { DeviceRepository } from '@app/common/modules/device/repositories';
import { ConfigService } from '@nestjs/config';
@Injectable()
export class CheckRoomGuard implements CanActivate {
private tuya: TuyaContext;
constructor(
private readonly configService: ConfigService,
private readonly spaceRepository: SpaceRepository,
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();
@@ -35,10 +23,10 @@ export class CheckRoomGuard implements CanActivate {
if (req.query && req.query.roomUuid) {
const { roomUuid } = req.query;
await this.checkRoomIsFound(roomUuid);
} else if (req.body && req.body.roomUuid && req.body.deviceTuyaUuid) {
const { roomUuid, deviceTuyaUuid } = req.body;
} else if (req.body && req.body.roomUuid && req.body.deviceUuid) {
const { roomUuid, deviceUuid } = req.body;
await this.checkRoomIsFound(roomUuid);
await this.checkDeviceIsFoundFromTuya(deviceTuyaUuid);
await this.checkDeviceIsFound(deviceUuid);
} else {
throw new BadRequestException('Invalid request parameters');
}
@@ -63,14 +51,14 @@ export class CheckRoomGuard implements CanActivate {
throw new NotFoundException('Room not found');
}
}
async checkDeviceIsFoundFromTuya(deviceTuyaUuid: string) {
const path = `/v1.1/iot-03/devices/${deviceTuyaUuid}`;
const response = await this.tuya.request({
method: 'GET',
path,
async checkDeviceIsFound(deviceUuid: string) {
const response = await this.deviceRepository.findOne({
where: {
uuid: deviceUuid,
},
});
if (!response.success) {
if (!response.uuid) {
throw new NotFoundException('Device not found');
}
}
@@ -88,7 +76,7 @@ export class CheckRoomGuard implements CanActivate {
} else {
response.status(HttpStatus.BAD_REQUEST).json({
statusCode: HttpStatus.BAD_REQUEST,
message: 'Invalid UUID',
message: error.message || 'Invalid UUID',
});
}
}