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() @ApiBearerAuth()
@UseGuards(JwtAuthGuard, CheckFourAndSixSceneDeviceTypeGuard) @UseGuards(JwtAuthGuard, CheckFourAndSixSceneDeviceTypeGuard)
@Get(':deviceUuid/scenes/switch') @Get(':deviceUuid/scenes')
async getSceneBySceneDevice( async getScenesBySceneDevice(
@Param('deviceUuid') deviceUuid: string, @Param('deviceUuid') deviceUuid: string,
@Query() getSceneFourSceneDeviceDto: GetSceneFourSceneDeviceDto, @Query() getSceneFourSceneDeviceDto: GetSceneFourSceneDeviceDto,
) { ) {
return await this.deviceService.getSceneBySceneDevice( return await this.deviceService.getScenesBySceneDevice(
deviceUuid, deviceUuid,
getSceneFourSceneDeviceDto, 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 { ApiProperty } from '@nestjs/swagger';
import { IsArray, IsNotEmpty, IsString } from 'class-validator'; import { IsArray, IsNotEmpty, IsOptional, IsString } from 'class-validator';
export class ControlDeviceDto { export class ControlDeviceDto {
@ApiProperty({ @ApiProperty({
@ -57,9 +57,9 @@ export class BatchFactoryResetDevicesDto {
export class GetSceneFourSceneDeviceDto { export class GetSceneFourSceneDeviceDto {
@ApiProperty({ @ApiProperty({
description: 'switchName', description: 'switchName',
required: true, required: false,
}) })
@IsString() @IsString()
@IsNotEmpty() @IsOptional()
public switchName: string; public switchName?: string;
} }

View File

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