mirror of
https://github.com/SyncrowIOT/backend.git
synced 2026-10-23 10:47:54 +00:00
Refactor Device Module
This commit is contained in:
@@ -36,7 +36,7 @@ export class CheckProductUuidForAllDevicesGuard implements CanActivate {
|
||||
throw new BadRequestException('First device not found');
|
||||
}
|
||||
|
||||
const firstProductUuid = firstDevice.productUuid;
|
||||
const firstProductUuid = firstDevice.productDevice.uuid;
|
||||
|
||||
for (let i = 1; i < deviceUuids.length; i++) {
|
||||
const device = await this.deviceRepository.findOne({
|
||||
@@ -47,7 +47,7 @@ export class CheckProductUuidForAllDevicesGuard implements CanActivate {
|
||||
throw new BadRequestException(`Device ${deviceUuids[i]} not found`);
|
||||
}
|
||||
|
||||
if (device.productUuid !== firstProductUuid) {
|
||||
if (device.productDevice.uuid !== firstProductUuid) {
|
||||
throw new BadRequestException(`Devices have different product UUIDs`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
import {
|
||||
CanActivate,
|
||||
ExecutionContext,
|
||||
Injectable,
|
||||
HttpStatus,
|
||||
} from '@nestjs/common';
|
||||
|
||||
import { BadRequestException, NotFoundException } from '@nestjs/common';
|
||||
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
||||
import { GroupRepository } from '@app/common/modules/group/repositories';
|
||||
|
||||
@Injectable()
|
||||
export class CheckGroupGuard implements CanActivate {
|
||||
constructor(
|
||||
private readonly groupRepository: GroupRepository,
|
||||
private readonly deviceRepository: DeviceRepository,
|
||||
) {}
|
||||
|
||||
async canActivate(context: ExecutionContext): Promise<boolean> {
|
||||
const req = context.switchToHttp().getRequest();
|
||||
|
||||
try {
|
||||
if (req.query && req.query.groupUuid) {
|
||||
const { groupUuid } = req.query;
|
||||
await this.checkGroupIsFound(groupUuid);
|
||||
} else if (req.body && req.body.groupUuid && req.body.deviceUuid) {
|
||||
const { groupUuid, deviceUuid } = req.body;
|
||||
await this.checkGroupIsFound(groupUuid);
|
||||
await this.checkDeviceIsFound(deviceUuid);
|
||||
} else {
|
||||
throw new BadRequestException('Invalid request parameters');
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (error) {
|
||||
this.handleGuardError(error, context);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private async checkGroupIsFound(groupUuid: string) {
|
||||
const group = await this.groupRepository.findOne({
|
||||
where: {
|
||||
uuid: groupUuid,
|
||||
},
|
||||
});
|
||||
|
||||
if (!group) {
|
||||
throw new NotFoundException('Group not found');
|
||||
}
|
||||
}
|
||||
async checkDeviceIsFound(deviceUuid: string) {
|
||||
const device = await this.deviceRepository.findOne({
|
||||
where: {
|
||||
uuid: deviceUuid,
|
||||
},
|
||||
});
|
||||
|
||||
if (!device) {
|
||||
throw new NotFoundException('Device not found');
|
||||
}
|
||||
}
|
||||
|
||||
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: 'Invalid UUID',
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
import {
|
||||
CanActivate,
|
||||
ExecutionContext,
|
||||
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();
|
||||
|
||||
try {
|
||||
console.log(req.body);
|
||||
|
||||
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;
|
||||
await this.checkRoomIsFound(roomUuid);
|
||||
await this.checkDeviceIsFoundFromTuya(deviceTuyaUuid);
|
||||
} else {
|
||||
throw new BadRequestException('Invalid request parameters');
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (error) {
|
||||
this.handleGuardError(error, context);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private async checkRoomIsFound(roomUuid: string) {
|
||||
const room = await this.spaceRepository.findOne({
|
||||
where: {
|
||||
uuid: roomUuid,
|
||||
spaceType: {
|
||||
type: 'room',
|
||||
},
|
||||
},
|
||||
});
|
||||
if (!room) {
|
||||
throw new NotFoundException('Room not found');
|
||||
}
|
||||
}
|
||||
async checkDeviceIsFoundFromTuya(deviceTuyaUuid: string) {
|
||||
console.log('deviceTuyaUuid: ', deviceTuyaUuid);
|
||||
|
||||
const path = `/v1.1/iot-03/devices/${deviceTuyaUuid}`;
|
||||
const response = await this.tuya.request({
|
||||
method: 'GET',
|
||||
path,
|
||||
});
|
||||
console.log('checkDeviceIsFoundFromTuya: ', response);
|
||||
|
||||
if (!response.success) {
|
||||
throw new NotFoundException('Device not found');
|
||||
}
|
||||
}
|
||||
|
||||
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: 'Invalid UUID',
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user