finished scene icons

This commit is contained in:
faris Aljohari
2024-10-22 02:13:53 -05:00
parent 2f23812200
commit c2958498e5
12 changed files with 324 additions and 32 deletions
+105 -25
View File
@@ -5,7 +5,12 @@ import {
BadRequestException,
} from '@nestjs/common';
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 { ConfigService } from '@nestjs/config';
import { TuyaContext } from '@tuya/tuya-connector-nodejs';
@@ -14,12 +19,15 @@ import { DeviceService } from 'src/device/services';
import {
AddTapToRunSceneInterface,
DeleteTapToRunSceneInterface,
GetTapToRunSceneByUnitInterface,
SceneDetailsResult,
} from '../interface/scene.interface';
import { convertKeysToCamelCase } from '@app/common/helper/camelCaseConverter';
import { SpaceType } from '@app/common/constants/space-type.enum';
import { ActionExecutorEnum } from '@app/common/constants/automation.enum';
import {
SceneIconRepository,
SceneRepository,
} from '@app/common/modules/scene/repositories';
@Injectable()
export class SceneService {
@@ -27,6 +35,8 @@ export class SceneService {
constructor(
private readonly configService: ConfigService,
private readonly spaceRepository: SpaceRepository,
private readonly sceneIconRepository: SceneIconRepository,
private readonly sceneRepository: SceneRepository,
private readonly deviceService: DeviceService,
) {
const accessKey = this.configService.get<string>('auth-config.ACCESS_KEY');
@@ -42,6 +52,7 @@ export class SceneService {
async addTapToRunScene(
addSceneTapToRunDto: AddSceneTapToRunDto,
spaceTuyaId = null,
unitUuid?: string,
) {
try {
let unitSpaceTuyaId;
@@ -89,6 +100,15 @@ export class SceneService {
});
if (!response.success) {
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 {
id: response.result.id,
@@ -134,33 +154,32 @@ export class SceneService {
}
}
}
async getTapToRunSceneByUnit(unitUuid: string) {
async getTapToRunSceneByUnit(unitUuid: string, inHomePage: GetSceneDto) {
try {
const unit = await this.getUnitByUuid(unitUuid);
if (!unit.spaceTuyaUuid) {
throw new BadRequestException('Invalid unit UUID');
}
const showInHomePage = inHomePage?.showInHomePage;
const path = `/v2.0/cloud/scene/rule?space_id=${unit.spaceTuyaUuid}&type=scene`;
const response: GetTapToRunSceneByUnitInterface = await this.tuya.request(
{
method: 'GET',
path,
const scenesData = await this.sceneRepository.find({
where: {
unitUuid,
...(showInHomePage ? { showInHomePage } : {}),
},
});
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) {
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',
};
});
return scenes;
} catch (err) {
if (err instanceof BadRequestException) {
throw err; // Re-throw BadRequestException
@@ -197,6 +216,8 @@ export class SceneService {
if (!response.success) {
throw new HttpException('Scene not found', HttpStatus.NOT_FOUND);
} else {
await this.sceneRepository.delete({ sceneTuyaUuid: sceneId });
}
return response;
@@ -273,11 +294,17 @@ export class SceneService {
}
}
}
const scene = await this.sceneRepository.findOne({
where: { sceneTuyaUuid: sceneId },
relations: ['sceneIcon'],
});
return {
id: responseData.id,
name: responseData.name,
status: responseData.status,
icon: scene.sceneIcon?.icon,
iconUuid: scene.sceneIcon?.uuid,
showInHome: scene.showInHomePage,
type: 'tap_to_run',
actions: actions,
...(withSpaceId && { spaceId: responseData.spaceId }),
@@ -334,13 +361,32 @@ export class SceneService {
const addSceneTapToRunDto: AddSceneTapToRunDto = {
...updateSceneTapToRunDto,
unitUuid: null,
iconUuid: updateSceneTapToRunDto.iconUuid,
showInHomePage: updateSceneTapToRunDto.showInHomePage,
};
const scene = await this.sceneRepository.findOne({
where: { sceneTuyaUuid: sceneId },
});
const newTapToRunScene = await this.addTapToRunScene(
addSceneTapToRunDto,
spaceTuyaId.spaceId,
scene.unitUuid,
);
if (newTapToRunScene.id) {
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;
}
} 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,
);
}
}
}