mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-16 02:36:19 +00:00
finished scene icons
This commit is contained in:
@ -20,6 +20,7 @@ import { RegionEntity } from '../modules/region/entities';
|
|||||||
import { TimeZoneEntity } from '../modules/timezone/entities';
|
import { TimeZoneEntity } from '../modules/timezone/entities';
|
||||||
import { VisitorPasswordEntity } from '../modules/visitor-password/entities';
|
import { VisitorPasswordEntity } from '../modules/visitor-password/entities';
|
||||||
import { DeviceStatusLogEntity } from '../modules/device-status-log/entities';
|
import { DeviceStatusLogEntity } from '../modules/device-status-log/entities';
|
||||||
|
import { SceneEntity, SceneIconEntity } from '../modules/scene/entities';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
@ -54,6 +55,8 @@ import { DeviceStatusLogEntity } from '../modules/device-status-log/entities';
|
|||||||
TimeZoneEntity,
|
TimeZoneEntity,
|
||||||
VisitorPasswordEntity,
|
VisitorPasswordEntity,
|
||||||
DeviceStatusLogEntity,
|
DeviceStatusLogEntity,
|
||||||
|
SceneEntity,
|
||||||
|
SceneIconEntity,
|
||||||
],
|
],
|
||||||
namingStrategy: new SnakeNamingStrategy(),
|
namingStrategy: new SnakeNamingStrategy(),
|
||||||
synchronize: Boolean(JSON.parse(configService.get('DB_SYNC'))),
|
synchronize: Boolean(JSON.parse(configService.get('DB_SYNC'))),
|
||||||
|
1
libs/common/src/modules/scene/dtos/index.ts
Normal file
1
libs/common/src/modules/scene/dtos/index.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export * from './scene.dto';
|
30
libs/common/src/modules/scene/dtos/scene.dto.ts
Normal file
30
libs/common/src/modules/scene/dtos/scene.dto.ts
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
import { IsBoolean, IsNotEmpty, IsString } from 'class-validator';
|
||||||
|
|
||||||
|
export class SceneDto {
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public uuid: string;
|
||||||
|
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public sceneTuyaUuid: string;
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public unitUuid: string;
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public iconUuid: string;
|
||||||
|
|
||||||
|
@IsBoolean()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public inHomePage: boolean;
|
||||||
|
}
|
||||||
|
export class SceneIconDto {
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public uuid: string;
|
||||||
|
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public icon: string;
|
||||||
|
}
|
1
libs/common/src/modules/scene/entities/index.ts
Normal file
1
libs/common/src/modules/scene/entities/index.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export * from './scene.entity';
|
57
libs/common/src/modules/scene/entities/scene.entity.ts
Normal file
57
libs/common/src/modules/scene/entities/scene.entity.ts
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
import { Column, Entity, ManyToOne, OneToMany } from 'typeorm';
|
||||||
|
import { SceneDto, SceneIconDto } from '../dtos';
|
||||||
|
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
||||||
|
|
||||||
|
// Define SceneIconEntity before SceneEntity
|
||||||
|
@Entity({ name: 'scene-icon' })
|
||||||
|
export class SceneIconEntity extends AbstractEntity<SceneIconDto> {
|
||||||
|
@Column({
|
||||||
|
nullable: true,
|
||||||
|
type: 'text',
|
||||||
|
})
|
||||||
|
public icon: string;
|
||||||
|
|
||||||
|
@OneToMany(
|
||||||
|
() => SceneEntity,
|
||||||
|
(scenesIconEntity) => scenesIconEntity.sceneIcon,
|
||||||
|
)
|
||||||
|
scenesIconEntity: SceneEntity[];
|
||||||
|
|
||||||
|
constructor(partial: Partial<SceneIconEntity>) {
|
||||||
|
super();
|
||||||
|
Object.assign(this, partial);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Entity({ name: 'scene' })
|
||||||
|
export class SceneEntity extends AbstractEntity<SceneDto> {
|
||||||
|
@Column({
|
||||||
|
type: 'uuid',
|
||||||
|
default: () => 'gen_random_uuid()',
|
||||||
|
nullable: false,
|
||||||
|
})
|
||||||
|
public uuid: string;
|
||||||
|
|
||||||
|
@Column({
|
||||||
|
nullable: false,
|
||||||
|
})
|
||||||
|
sceneTuyaUuid: string;
|
||||||
|
@Column({
|
||||||
|
nullable: false,
|
||||||
|
})
|
||||||
|
unitUuid: string;
|
||||||
|
@Column({
|
||||||
|
nullable: false,
|
||||||
|
})
|
||||||
|
showInHomePage: boolean;
|
||||||
|
|
||||||
|
@ManyToOne(() => SceneIconEntity, (icon) => icon.scenesIconEntity, {
|
||||||
|
nullable: false,
|
||||||
|
})
|
||||||
|
sceneIcon: SceneIconEntity;
|
||||||
|
|
||||||
|
constructor(partial: Partial<SceneEntity>) {
|
||||||
|
super();
|
||||||
|
Object.assign(this, partial);
|
||||||
|
}
|
||||||
|
}
|
1
libs/common/src/modules/scene/repositories/index.ts
Normal file
1
libs/common/src/modules/scene/repositories/index.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export * from './scene.repository';
|
@ -0,0 +1,17 @@
|
|||||||
|
import { DataSource, Repository } from 'typeorm';
|
||||||
|
import { Injectable } from '@nestjs/common';
|
||||||
|
import { SceneEntity, SceneIconEntity } from '../entities';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class SceneRepository extends Repository<SceneEntity> {
|
||||||
|
constructor(private dataSource: DataSource) {
|
||||||
|
super(SceneEntity, dataSource.createEntityManager());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class SceneIconRepository extends Repository<SceneIconEntity> {
|
||||||
|
constructor(private dataSource: DataSource) {
|
||||||
|
super(SceneIconEntity, dataSource.createEntityManager());
|
||||||
|
}
|
||||||
|
}
|
11
libs/common/src/modules/scene/scene.repository.module.ts
Normal file
11
libs/common/src/modules/scene/scene.repository.module.ts
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
import { Module } from '@nestjs/common';
|
||||||
|
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||||
|
import { SceneEntity, SceneIconEntity } from './entities';
|
||||||
|
|
||||||
|
@Module({
|
||||||
|
providers: [],
|
||||||
|
exports: [],
|
||||||
|
controllers: [],
|
||||||
|
imports: [TypeOrmModule.forFeature([SceneEntity, SceneIconEntity])],
|
||||||
|
})
|
||||||
|
export class SceneRepositoryModule {}
|
@ -8,10 +8,16 @@ import {
|
|||||||
Param,
|
Param,
|
||||||
Post,
|
Post,
|
||||||
Put,
|
Put,
|
||||||
|
Query,
|
||||||
UseGuards,
|
UseGuards,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
|
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
|
||||||
import { AddSceneTapToRunDto, UpdateSceneTapToRunDto } from '../dtos/scene.dto';
|
import {
|
||||||
|
AddSceneIconDto,
|
||||||
|
AddSceneTapToRunDto,
|
||||||
|
GetSceneDto,
|
||||||
|
UpdateSceneTapToRunDto,
|
||||||
|
} from '../dtos/scene.dto';
|
||||||
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
|
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
|
||||||
import { EnableDisableStatusEnum } from '@app/common/constants/days.enum';
|
import { EnableDisableStatusEnum } from '@app/common/constants/days.enum';
|
||||||
|
|
||||||
@ -39,9 +45,14 @@ export class SceneController {
|
|||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard)
|
||||||
@Get('tap-to-run/:unitUuid')
|
@Get('tap-to-run/:unitUuid')
|
||||||
async getTapToRunSceneByUnit(@Param('unitUuid') unitUuid: string) {
|
async getTapToRunSceneByUnit(
|
||||||
const tapToRunScenes =
|
@Param('unitUuid') unitUuid: string,
|
||||||
await this.sceneService.getTapToRunSceneByUnit(unitUuid);
|
@Query() inHomePage: GetSceneDto,
|
||||||
|
) {
|
||||||
|
const tapToRunScenes = await this.sceneService.getTapToRunSceneByUnit(
|
||||||
|
unitUuid,
|
||||||
|
inHomePage,
|
||||||
|
);
|
||||||
return tapToRunScenes;
|
return tapToRunScenes;
|
||||||
}
|
}
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@ -95,4 +106,23 @@ export class SceneController {
|
|||||||
data: tapToRunScene,
|
data: tapToRunScene,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ApiBearerAuth()
|
||||||
|
@UseGuards(JwtAuthGuard)
|
||||||
|
@Post('icon')
|
||||||
|
async addSceneIcon(@Body() addSceneIconDto: AddSceneIconDto) {
|
||||||
|
const tapToRunScene = await this.sceneService.addSceneIcon(addSceneIconDto);
|
||||||
|
return {
|
||||||
|
statusCode: HttpStatus.CREATED,
|
||||||
|
success: true,
|
||||||
|
message: 'Icon added successfully',
|
||||||
|
data: tapToRunScene,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
@ApiBearerAuth()
|
||||||
|
@UseGuards(JwtAuthGuard)
|
||||||
|
@Get('icon')
|
||||||
|
async getAllIcons() {
|
||||||
|
const icons = await this.sceneService.getAllIcons();
|
||||||
|
return icons;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,8 +6,10 @@ import {
|
|||||||
ValidateNested,
|
ValidateNested,
|
||||||
IsOptional,
|
IsOptional,
|
||||||
IsNumber,
|
IsNumber,
|
||||||
|
IsBoolean,
|
||||||
} from 'class-validator';
|
} from 'class-validator';
|
||||||
import { Type } from 'class-transformer';
|
import { Transform, Type } from 'class-transformer';
|
||||||
|
import { BooleanValues } from '@app/common/constants/boolean-values.enum';
|
||||||
|
|
||||||
class ExecutorProperty {
|
class ExecutorProperty {
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
@ -70,7 +72,20 @@ export class AddSceneTapToRunDto {
|
|||||||
@IsString()
|
@IsString()
|
||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
public unitUuid: string;
|
public unitUuid: string;
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'Icon UUID',
|
||||||
|
required: true,
|
||||||
|
})
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public iconUuid: string;
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'show in home page',
|
||||||
|
required: true,
|
||||||
|
})
|
||||||
|
@IsBoolean()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public showInHomePage: boolean;
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
description: 'Scene name',
|
description: 'Scene name',
|
||||||
required: true,
|
required: true,
|
||||||
@ -109,7 +124,20 @@ export class UpdateSceneTapToRunDto {
|
|||||||
@IsString()
|
@IsString()
|
||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
public sceneName: string;
|
public sceneName: string;
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'Icon UUID',
|
||||||
|
required: true,
|
||||||
|
})
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public iconUuid: string;
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'show in home page',
|
||||||
|
required: true,
|
||||||
|
})
|
||||||
|
@IsBoolean()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public showInHomePage: boolean;
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
description: 'Decision expression',
|
description: 'Decision expression',
|
||||||
required: true,
|
required: true,
|
||||||
@ -132,3 +160,30 @@ export class UpdateSceneTapToRunDto {
|
|||||||
Object.assign(this, dto);
|
Object.assign(this, dto);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
export class AddSceneIconDto {
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'Icon',
|
||||||
|
required: true,
|
||||||
|
})
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public icon: string;
|
||||||
|
|
||||||
|
constructor(dto: Partial<AddSceneIconDto>) {
|
||||||
|
Object.assign(this, dto);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
export class GetSceneDto {
|
||||||
|
@ApiProperty({
|
||||||
|
example: true,
|
||||||
|
description: 'showInHomePage',
|
||||||
|
required: true,
|
||||||
|
default: false,
|
||||||
|
})
|
||||||
|
@IsNotEmpty()
|
||||||
|
@IsBoolean()
|
||||||
|
@Transform((value) => {
|
||||||
|
return value.obj.showInHomePage === BooleanValues.TRUE;
|
||||||
|
})
|
||||||
|
public showInHomePage: boolean = false;
|
||||||
|
}
|
||||||
|
@ -8,6 +8,10 @@ import { DeviceService } from 'src/device/services';
|
|||||||
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
||||||
import { ProductRepository } from '@app/common/modules/product/repositories';
|
import { ProductRepository } from '@app/common/modules/product/repositories';
|
||||||
import { DeviceStatusFirebaseModule } from '@app/common/firebase/devices-status/devices-status.module';
|
import { DeviceStatusFirebaseModule } from '@app/common/firebase/devices-status/devices-status.module';
|
||||||
|
import {
|
||||||
|
SceneIconRepository,
|
||||||
|
SceneRepository,
|
||||||
|
} from '@app/common/modules/scene/repositories';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [ConfigModule, SpaceRepositoryModule, DeviceStatusFirebaseModule],
|
imports: [ConfigModule, SpaceRepositoryModule, DeviceStatusFirebaseModule],
|
||||||
@ -18,6 +22,8 @@ import { DeviceStatusFirebaseModule } from '@app/common/firebase/devices-status/
|
|||||||
DeviceService,
|
DeviceService,
|
||||||
DeviceRepository,
|
DeviceRepository,
|
||||||
ProductRepository,
|
ProductRepository,
|
||||||
|
SceneIconRepository,
|
||||||
|
SceneRepository,
|
||||||
],
|
],
|
||||||
exports: [SceneService],
|
exports: [SceneService],
|
||||||
})
|
})
|
||||||
|
@ -5,7 +5,12 @@ import {
|
|||||||
BadRequestException,
|
BadRequestException,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||||
import { AddSceneTapToRunDto, UpdateSceneTapToRunDto } from '../dtos';
|
import {
|
||||||
|
AddSceneIconDto,
|
||||||
|
AddSceneTapToRunDto,
|
||||||
|
GetSceneDto,
|
||||||
|
UpdateSceneTapToRunDto,
|
||||||
|
} from '../dtos';
|
||||||
import { GetUnitByUuidInterface } from 'src/unit/interface/unit.interface';
|
import { GetUnitByUuidInterface } from 'src/unit/interface/unit.interface';
|
||||||
import { ConfigService } from '@nestjs/config';
|
import { ConfigService } from '@nestjs/config';
|
||||||
import { TuyaContext } from '@tuya/tuya-connector-nodejs';
|
import { TuyaContext } from '@tuya/tuya-connector-nodejs';
|
||||||
@ -14,12 +19,15 @@ import { DeviceService } from 'src/device/services';
|
|||||||
import {
|
import {
|
||||||
AddTapToRunSceneInterface,
|
AddTapToRunSceneInterface,
|
||||||
DeleteTapToRunSceneInterface,
|
DeleteTapToRunSceneInterface,
|
||||||
GetTapToRunSceneByUnitInterface,
|
|
||||||
SceneDetailsResult,
|
SceneDetailsResult,
|
||||||
} from '../interface/scene.interface';
|
} from '../interface/scene.interface';
|
||||||
import { convertKeysToCamelCase } from '@app/common/helper/camelCaseConverter';
|
import { convertKeysToCamelCase } from '@app/common/helper/camelCaseConverter';
|
||||||
import { SpaceType } from '@app/common/constants/space-type.enum';
|
import { SpaceType } from '@app/common/constants/space-type.enum';
|
||||||
import { ActionExecutorEnum } from '@app/common/constants/automation.enum';
|
import { ActionExecutorEnum } from '@app/common/constants/automation.enum';
|
||||||
|
import {
|
||||||
|
SceneIconRepository,
|
||||||
|
SceneRepository,
|
||||||
|
} from '@app/common/modules/scene/repositories';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class SceneService {
|
export class SceneService {
|
||||||
@ -27,6 +35,8 @@ export class SceneService {
|
|||||||
constructor(
|
constructor(
|
||||||
private readonly configService: ConfigService,
|
private readonly configService: ConfigService,
|
||||||
private readonly spaceRepository: SpaceRepository,
|
private readonly spaceRepository: SpaceRepository,
|
||||||
|
private readonly sceneIconRepository: SceneIconRepository,
|
||||||
|
private readonly sceneRepository: SceneRepository,
|
||||||
private readonly deviceService: DeviceService,
|
private readonly deviceService: DeviceService,
|
||||||
) {
|
) {
|
||||||
const accessKey = this.configService.get<string>('auth-config.ACCESS_KEY');
|
const accessKey = this.configService.get<string>('auth-config.ACCESS_KEY');
|
||||||
@ -42,6 +52,7 @@ export class SceneService {
|
|||||||
async addTapToRunScene(
|
async addTapToRunScene(
|
||||||
addSceneTapToRunDto: AddSceneTapToRunDto,
|
addSceneTapToRunDto: AddSceneTapToRunDto,
|
||||||
spaceTuyaId = null,
|
spaceTuyaId = null,
|
||||||
|
unitUuid?: string,
|
||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
let unitSpaceTuyaId;
|
let unitSpaceTuyaId;
|
||||||
@ -89,6 +100,15 @@ export class SceneService {
|
|||||||
});
|
});
|
||||||
if (!response.success) {
|
if (!response.success) {
|
||||||
throw new HttpException(response.msg, HttpStatus.BAD_REQUEST);
|
throw new HttpException(response.msg, HttpStatus.BAD_REQUEST);
|
||||||
|
} else {
|
||||||
|
await this.sceneRepository.save({
|
||||||
|
sceneTuyaUuid: response.result.id,
|
||||||
|
sceneIcon: {
|
||||||
|
uuid: addSceneTapToRunDto.iconUuid,
|
||||||
|
},
|
||||||
|
showInHomePage: addSceneTapToRunDto.showInHomePage,
|
||||||
|
unitUuid: unitUuid ? unitUuid : addSceneTapToRunDto.unitUuid,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
id: response.result.id,
|
id: response.result.id,
|
||||||
@ -134,33 +154,32 @@ export class SceneService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
async getTapToRunSceneByUnit(unitUuid: string) {
|
async getTapToRunSceneByUnit(unitUuid: string, inHomePage: GetSceneDto) {
|
||||||
try {
|
try {
|
||||||
const unit = await this.getUnitByUuid(unitUuid);
|
const showInHomePage = inHomePage?.showInHomePage;
|
||||||
if (!unit.spaceTuyaUuid) {
|
|
||||||
throw new BadRequestException('Invalid unit UUID');
|
|
||||||
}
|
|
||||||
|
|
||||||
const path = `/v2.0/cloud/scene/rule?space_id=${unit.spaceTuyaUuid}&type=scene`;
|
const scenesData = await this.sceneRepository.find({
|
||||||
const response: GetTapToRunSceneByUnitInterface = await this.tuya.request(
|
where: {
|
||||||
{
|
unitUuid,
|
||||||
method: 'GET',
|
...(showInHomePage ? { showInHomePage } : {}),
|
||||||
path,
|
|
||||||
},
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const scenes = await Promise.all(
|
||||||
|
scenesData.map(async (scene) => {
|
||||||
|
const sceneData = await this.getTapToRunSceneDetails(
|
||||||
|
scene.sceneTuyaUuid,
|
||||||
|
false,
|
||||||
|
);
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
|
const { actions, ...rest } = sceneData;
|
||||||
|
return {
|
||||||
|
rest,
|
||||||
|
};
|
||||||
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!response.success) {
|
return scenes;
|
||||||
throw new HttpException(response.msg, HttpStatus.BAD_REQUEST);
|
|
||||||
}
|
|
||||||
|
|
||||||
return response.result.list.map((item) => {
|
|
||||||
return {
|
|
||||||
id: item.id,
|
|
||||||
name: item.name,
|
|
||||||
status: item.status,
|
|
||||||
type: 'tap_to_run',
|
|
||||||
};
|
|
||||||
});
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err instanceof BadRequestException) {
|
if (err instanceof BadRequestException) {
|
||||||
throw err; // Re-throw BadRequestException
|
throw err; // Re-throw BadRequestException
|
||||||
@ -197,6 +216,8 @@ export class SceneService {
|
|||||||
|
|
||||||
if (!response.success) {
|
if (!response.success) {
|
||||||
throw new HttpException('Scene not found', HttpStatus.NOT_FOUND);
|
throw new HttpException('Scene not found', HttpStatus.NOT_FOUND);
|
||||||
|
} else {
|
||||||
|
await this.sceneRepository.delete({ sceneTuyaUuid: sceneId });
|
||||||
}
|
}
|
||||||
|
|
||||||
return response;
|
return response;
|
||||||
@ -273,11 +294,17 @@ export class SceneService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
const scene = await this.sceneRepository.findOne({
|
||||||
|
where: { sceneTuyaUuid: sceneId },
|
||||||
|
relations: ['sceneIcon'],
|
||||||
|
});
|
||||||
return {
|
return {
|
||||||
id: responseData.id,
|
id: responseData.id,
|
||||||
name: responseData.name,
|
name: responseData.name,
|
||||||
status: responseData.status,
|
status: responseData.status,
|
||||||
|
icon: scene.sceneIcon?.icon,
|
||||||
|
iconUuid: scene.sceneIcon?.uuid,
|
||||||
|
showInHome: scene.showInHomePage,
|
||||||
type: 'tap_to_run',
|
type: 'tap_to_run',
|
||||||
actions: actions,
|
actions: actions,
|
||||||
...(withSpaceId && { spaceId: responseData.spaceId }),
|
...(withSpaceId && { spaceId: responseData.spaceId }),
|
||||||
@ -334,13 +361,32 @@ export class SceneService {
|
|||||||
const addSceneTapToRunDto: AddSceneTapToRunDto = {
|
const addSceneTapToRunDto: AddSceneTapToRunDto = {
|
||||||
...updateSceneTapToRunDto,
|
...updateSceneTapToRunDto,
|
||||||
unitUuid: null,
|
unitUuid: null,
|
||||||
|
iconUuid: updateSceneTapToRunDto.iconUuid,
|
||||||
|
showInHomePage: updateSceneTapToRunDto.showInHomePage,
|
||||||
};
|
};
|
||||||
|
const scene = await this.sceneRepository.findOne({
|
||||||
|
where: { sceneTuyaUuid: sceneId },
|
||||||
|
});
|
||||||
|
|
||||||
const newTapToRunScene = await this.addTapToRunScene(
|
const newTapToRunScene = await this.addTapToRunScene(
|
||||||
addSceneTapToRunDto,
|
addSceneTapToRunDto,
|
||||||
spaceTuyaId.spaceId,
|
spaceTuyaId.spaceId,
|
||||||
|
scene.unitUuid,
|
||||||
);
|
);
|
||||||
if (newTapToRunScene.id) {
|
if (newTapToRunScene.id) {
|
||||||
await this.deleteTapToRunScene(null, sceneId, spaceTuyaId.spaceId);
|
await this.deleteTapToRunScene(null, sceneId, spaceTuyaId.spaceId);
|
||||||
|
|
||||||
|
await this.sceneRepository.update(
|
||||||
|
{ sceneTuyaUuid: sceneId },
|
||||||
|
{
|
||||||
|
sceneTuyaUuid: newTapToRunScene.id,
|
||||||
|
showInHomePage: addSceneTapToRunDto.showInHomePage,
|
||||||
|
sceneIcon: {
|
||||||
|
uuid: addSceneTapToRunDto.iconUuid,
|
||||||
|
},
|
||||||
|
unitUuid: scene.unitUuid,
|
||||||
|
},
|
||||||
|
);
|
||||||
return newTapToRunScene;
|
return newTapToRunScene;
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@ -354,4 +400,38 @@ export class SceneService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
async addSceneIcon(addSceneIconDto: AddSceneIconDto) {
|
||||||
|
try {
|
||||||
|
const icon = await this.sceneIconRepository.save({
|
||||||
|
icon: addSceneIconDto.icon,
|
||||||
|
});
|
||||||
|
return icon;
|
||||||
|
} catch (err) {
|
||||||
|
throw new HttpException(
|
||||||
|
{
|
||||||
|
status: HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
error: 'Failed to add scene icon',
|
||||||
|
message: err.message || 'Unexpected error occurred',
|
||||||
|
},
|
||||||
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
async getAllIcons() {
|
||||||
|
try {
|
||||||
|
const icons = await this.sceneIconRepository.find();
|
||||||
|
return icons;
|
||||||
|
} catch (err) {
|
||||||
|
// Improved error handling
|
||||||
|
throw new HttpException(
|
||||||
|
{
|
||||||
|
statusCode: HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
message: 'Failed to fetch icons',
|
||||||
|
error:
|
||||||
|
err.message || 'An unexpected error occurred while fetching icons',
|
||||||
|
},
|
||||||
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user