mirror of
https://github.com/SyncrowIOT/backend.git
synced 2026-10-23 11:07:55 +00:00
Refactor controlDevice method for better readability
This commit is contained in:
@@ -16,7 +16,6 @@ export class CheckProductUuidForAllDevicesGuard implements CanActivate {
|
||||
|
||||
try {
|
||||
const { deviceUuids } = req.body;
|
||||
console.log(deviceUuids);
|
||||
|
||||
await this.checkAllDevicesHaveSameProductUuid(deviceUuids);
|
||||
|
||||
@@ -30,25 +29,27 @@ export class CheckProductUuidForAllDevicesGuard implements CanActivate {
|
||||
async checkAllDevicesHaveSameProductUuid(deviceUuids: string[]) {
|
||||
const firstDevice = await this.deviceRepository.findOne({
|
||||
where: { uuid: deviceUuids[0] },
|
||||
relations: ['productDevice'],
|
||||
});
|
||||
|
||||
if (!firstDevice) {
|
||||
throw new BadRequestException('First device not found');
|
||||
}
|
||||
|
||||
const firstProductUuid = firstDevice.productDevice.uuid;
|
||||
const firstProductType = firstDevice.productDevice.prodType;
|
||||
|
||||
for (let i = 1; i < deviceUuids.length; i++) {
|
||||
const device = await this.deviceRepository.findOne({
|
||||
where: { uuid: deviceUuids[i] },
|
||||
relations: ['productDevice'],
|
||||
});
|
||||
|
||||
if (!device) {
|
||||
throw new BadRequestException(`Device ${deviceUuids[i]} not found`);
|
||||
}
|
||||
|
||||
if (device.productDevice.uuid !== firstProductUuid) {
|
||||
throw new BadRequestException(`Devices have different product UUIDs`);
|
||||
if (device.productDevice.prodType !== firstProductType) {
|
||||
throw new BadRequestException(`Devices have different product types`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,8 +32,6 @@ export class CheckRoomGuard implements CanActivate {
|
||||
const req = context.switchToHttp().getRequest();
|
||||
|
||||
try {
|
||||
console.log(req.body);
|
||||
|
||||
if (req.query && req.query.roomUuid) {
|
||||
const { roomUuid } = req.query;
|
||||
await this.checkRoomIsFound(roomUuid);
|
||||
@@ -66,14 +64,11 @@ export class CheckRoomGuard implements CanActivate {
|
||||
}
|
||||
}
|
||||
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');
|
||||
|
||||
Reference in New Issue
Block a user