remove duplication code

This commit is contained in:
faris Aljohari
2024-11-20 00:34:12 -06:00
parent 1fe5ba70b9
commit 16444c625a
3 changed files with 44 additions and 58 deletions

View File

@ -232,20 +232,14 @@ export class DeviceController {
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard, CheckFourAndSixSceneDeviceTypeGuard)
@Get(':deviceUuid/scenes/switch')
async getSceneBySceneDevice(
@Get(':deviceUuid/scenes')
async getScenesBySceneDevice(
@Param('deviceUuid') deviceUuid: string,
@Query() getSceneFourSceneDeviceDto: GetSceneFourSceneDeviceDto,
) {
return await this.deviceService.getSceneBySceneDevice(
return await this.deviceService.getScenesBySceneDevice(
deviceUuid,
getSceneFourSceneDeviceDto,
);
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard, CheckFourAndSixSceneDeviceTypeGuard)
@Get(':deviceUuid/scenes')
async getScenesBySceneDevice(@Param('deviceUuid') deviceUuid: string) {
return await this.deviceService.getScenesBySceneDevice(deviceUuid);
}
}

View File

@ -1,5 +1,5 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsArray, IsNotEmpty, IsString } from 'class-validator';
import { IsArray, IsNotEmpty, IsOptional, IsString } from 'class-validator';
export class ControlDeviceDto {
@ApiProperty({
@ -57,9 +57,9 @@ export class BatchFactoryResetDevicesDto {
export class GetSceneFourSceneDeviceDto {
@ApiProperty({
description: 'switchName',
required: true,
required: false,
})
@IsString()
@IsNotEmpty()
public switchName: string;
@IsOptional()
public switchName?: string;
}

View File

@ -1319,11 +1319,13 @@ export class DeviceService {
}
}
async getSceneBySceneDevice(
async getScenesBySceneDevice(
deviceUuid: string,
getSceneFourSceneDeviceDto: GetSceneFourSceneDeviceDto,
) {
): Promise<any> {
try {
if (getSceneFourSceneDeviceDto.switchName) {
// Query for a single record directly when switchName is provided
const sceneDevice = await this.sceneDeviceRepository.findOne({
where: {
device: { uuid: deviceUuid },
@ -1332,46 +1334,14 @@ export class DeviceService {
},
relations: ['device', 'scene'],
});
if (sceneDevice.uuid) {
const SceneDetails = await this.sceneService.getSceneByUuid(
sceneDevice.scene.uuid,
);
return {
switchName: sceneDevice.switchName,
createdAt: sceneDevice.createdAt,
updatedAt: sceneDevice.updatedAt,
deviceUuid: sceneDevice.device.uuid,
scene: {
...SceneDetails.data,
},
};
}
} catch (error) {
throw new HttpException(
error.message || 'Scene device not found',
error.status || HttpStatus.NOT_FOUND,
);
}
}
async getScenesBySceneDevice(deviceUuid: string): Promise<any[]> {
try {
const sceneDevices = await this.sceneDeviceRepository.find({
where: { device: { uuid: deviceUuid } },
relations: ['device', 'scene'],
});
if (!sceneDevices.length) {
if (!sceneDevice) {
throw new HttpException(
'No scenes found for the device',
`No scene found for device with UUID ${deviceUuid} and switch name ${getSceneFourSceneDeviceDto.switchName}`,
HttpStatus.NOT_FOUND,
);
}
const results = await Promise.all(
sceneDevices.map(async (sceneDevice) => {
if (!sceneDevice.scene?.uuid) return null;
try {
const sceneDetails = await this.sceneService.getSceneByUuid(
sceneDevice.scene.uuid,
);
@ -1383,16 +1353,38 @@ export class DeviceService {
deviceUuid: sceneDevice.device.uuid,
scene: sceneDetails.data,
};
} catch (error) {
}
// Query for multiple records if switchName is not provided
const sceneDevices = await this.sceneDeviceRepository.find({
where: { device: { uuid: deviceUuid } },
relations: ['device', 'scene'],
});
if (!sceneDevices.length) {
throw new HttpException(
'Failed to fetch scene details',
HttpStatus.INTERNAL_SERVER_ERROR,
`No scenes found for device with UUID ${deviceUuid}`,
HttpStatus.NOT_FOUND,
);
}
const results = await Promise.all(
sceneDevices.map(async (sceneDevice) => {
const sceneDetails = await this.sceneService.getSceneByUuid(
sceneDevice.scene.uuid,
);
return {
switchName: sceneDevice.switchName,
createdAt: sceneDevice.createdAt,
updatedAt: sceneDevice.updatedAt,
deviceUuid: sceneDevice.device.uuid,
scene: sceneDetails.data,
};
}),
);
return results.filter(Boolean);
return results;
} catch (error) {
throw new HttpException(
error.message || 'Failed to fetch scenes for device',