mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-16 02:36:19 +00:00
Merge branch 'feat/add-product-space' into feat/add-space-position
This commit is contained in:
@ -8,8 +8,15 @@ import config from './config';
|
||||
import { EmailService } from './util/email.service';
|
||||
import { ErrorMessageService } from 'src/error-message/error-message.service';
|
||||
import { TuyaService } from './integrations/tuya/services/tuya.service';
|
||||
import { SceneDeviceRepository } from './modules/scene-device/repositories';
|
||||
@Module({
|
||||
providers: [CommonService, EmailService, ErrorMessageService, TuyaService],
|
||||
providers: [
|
||||
CommonService,
|
||||
EmailService,
|
||||
ErrorMessageService,
|
||||
TuyaService,
|
||||
SceneDeviceRepository,
|
||||
],
|
||||
exports: [
|
||||
CommonService,
|
||||
TuyaService,
|
||||
@ -17,6 +24,7 @@ import { TuyaService } from './integrations/tuya/services/tuya.service';
|
||||
AuthModule,
|
||||
EmailService,
|
||||
ErrorMessageService,
|
||||
SceneDeviceRepository,
|
||||
],
|
||||
imports: [
|
||||
ConfigModule.forRoot({
|
||||
|
@ -7,3 +7,13 @@ export enum ActionExecutorEnum {
|
||||
export enum EntityTypeEnum {
|
||||
DEVICE_REPORT = 'device_report',
|
||||
}
|
||||
export const AUTOMATION_CONFIG = {
|
||||
DEFAULT_START_TIME: '00:00',
|
||||
DEFAULT_END_TIME: '23:59',
|
||||
DEFAULT_LOOPS: '1111111',
|
||||
DECISION_EXPR: 'and',
|
||||
CONDITION_TYPE: 'device_report',
|
||||
ACTION_EXECUTOR: 'rule_trigger',
|
||||
COMPARATOR: '==',
|
||||
SCENE_STATUS_VALUE: 'scene',
|
||||
};
|
||||
|
@ -16,4 +16,6 @@ export enum ProductType {
|
||||
GD = 'GD',
|
||||
CUR = 'CUR',
|
||||
PC = 'PC',
|
||||
FOUR_S = '4S',
|
||||
SIX_S = '6S',
|
||||
}
|
||||
|
8
libs/common/src/constants/scene-switch-type.enum.ts
Normal file
8
libs/common/src/constants/scene-switch-type.enum.ts
Normal file
@ -0,0 +1,8 @@
|
||||
export enum SceneSwitchesTypeEnum {
|
||||
SCENE_1 = 'scene_1',
|
||||
SCENE_2 = 'scene_2',
|
||||
SCENE_3 = 'scene_3',
|
||||
SCENE_4 = 'scene_4',
|
||||
SCENE_5 = 'scene_5',
|
||||
SCENE_6 = 'scene_6',
|
||||
}
|
@ -8,7 +8,11 @@ import { UserOtpEntity } from '../modules/user/entities';
|
||||
import { ProductEntity } from '../modules/product/entities';
|
||||
import { DeviceEntity } from '../modules/device/entities';
|
||||
import { PermissionTypeEntity } from '../modules/permission/entities';
|
||||
import { SpaceEntity, SpaceLinkEntity, SubspaceEntity } from '../modules/space/entities';
|
||||
import {
|
||||
SpaceEntity,
|
||||
SpaceLinkEntity,
|
||||
SubspaceEntity,
|
||||
} from '../modules/space/entities';
|
||||
import { UserSpaceEntity } from '../modules/user/entities';
|
||||
import { DeviceUserPermissionEntity } from '../modules/device/entities';
|
||||
import { UserRoleEntity } from '../modules/user/entities';
|
||||
@ -21,6 +25,8 @@ import { VisitorPasswordEntity } from '../modules/visitor-password/entities';
|
||||
import { CommunityEntity } from '../modules/community/entities';
|
||||
import { DeviceStatusLogEntity } from '../modules/device-status-log/entities';
|
||||
import { SceneEntity, SceneIconEntity } from '../modules/scene/entities';
|
||||
import { SceneDeviceEntity } from '../modules/scene-device/entities';
|
||||
import { SpaceProductEntity } from '../modules/space/entities/space-product.entity';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
@ -47,6 +53,7 @@ import { SceneEntity, SceneIconEntity } from '../modules/scene/entities';
|
||||
SpaceEntity,
|
||||
SpaceLinkEntity,
|
||||
SubspaceEntity,
|
||||
SpaceProductEntity,
|
||||
UserSpaceEntity,
|
||||
DeviceUserPermissionEntity,
|
||||
UserRoleEntity,
|
||||
@ -59,6 +66,7 @@ import { SceneEntity, SceneIconEntity } from '../modules/scene/entities';
|
||||
DeviceStatusLogEntity,
|
||||
SceneEntity,
|
||||
SceneIconEntity,
|
||||
SceneDeviceEntity,
|
||||
],
|
||||
namingStrategy: new SnakeNamingStrategy(),
|
||||
synchronize: Boolean(JSON.parse(configService.get('DB_SYNC'))),
|
||||
|
@ -1,4 +1,12 @@
|
||||
import { Column, Entity, ManyToOne, OneToMany, Unique, Index, JoinColumn } from 'typeorm';
|
||||
import {
|
||||
Column,
|
||||
Entity,
|
||||
ManyToOne,
|
||||
OneToMany,
|
||||
Unique,
|
||||
Index,
|
||||
JoinColumn,
|
||||
} from 'typeorm';
|
||||
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
||||
import { DeviceDto, DeviceUserPermissionDto } from '../dtos/device.dto';
|
||||
import { SpaceEntity, SubspaceEntity } from '../../space/entities';
|
||||
@ -6,6 +14,7 @@ import { ProductEntity } from '../../product/entities';
|
||||
import { UserEntity } from '../../user/entities';
|
||||
import { DeviceNotificationDto } from '../dtos';
|
||||
import { PermissionTypeEntity } from '../../permission/entities';
|
||||
import { SceneDeviceEntity } from '../../scene-device/entities';
|
||||
|
||||
@Entity({ name: 'device' })
|
||||
@Unique(['deviceTuyaUuid'])
|
||||
@ -62,6 +71,9 @@ export class DeviceEntity extends AbstractEntity<DeviceDto> {
|
||||
@Column({ nullable: false })
|
||||
uuid: string;
|
||||
|
||||
@OneToMany(() => SceneDeviceEntity, (sceneDevice) => sceneDevice.device, {})
|
||||
sceneDevices: SceneDeviceEntity[];
|
||||
|
||||
constructor(partial: Partial<DeviceEntity>) {
|
||||
super();
|
||||
Object.assign(this, partial);
|
||||
|
@ -2,6 +2,7 @@ import { Column, Entity, OneToMany } from 'typeorm';
|
||||
import { ProductDto } from '../dtos';
|
||||
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
||||
import { DeviceEntity } from '../../device/entities';
|
||||
import { SpaceProductEntity } from '../../space/entities/space-product.entity';
|
||||
|
||||
@Entity({ name: 'product' })
|
||||
export class ProductEntity extends AbstractEntity<ProductDto> {
|
||||
@ -26,6 +27,9 @@ export class ProductEntity extends AbstractEntity<ProductDto> {
|
||||
})
|
||||
public prodType: string;
|
||||
|
||||
@OneToMany(() => SpaceProductEntity, (spaceProduct) => spaceProduct.product)
|
||||
spaceProducts: SpaceProductEntity[];
|
||||
|
||||
@OneToMany(
|
||||
() => DeviceEntity,
|
||||
(devicesProductEntity) => devicesProductEntity.productDevice,
|
||||
|
1
libs/common/src/modules/scene-device/dtos/index.ts
Normal file
1
libs/common/src/modules/scene-device/dtos/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './scene-device.dto';
|
@ -0,0 +1,20 @@
|
||||
import { IsNotEmpty, IsString } from 'class-validator';
|
||||
|
||||
export class SceneDeviceDto {
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public uuid: string;
|
||||
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public deviceUuid: string;
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public sceneUuid: string;
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public switchName: string;
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public automationTuyaUuid: string;
|
||||
}
|
1
libs/common/src/modules/scene-device/entities/index.ts
Normal file
1
libs/common/src/modules/scene-device/entities/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './scene-device.entity';
|
@ -0,0 +1,51 @@
|
||||
import { Column, Entity, JoinColumn, ManyToOne, Unique } from 'typeorm';
|
||||
import { SceneDeviceDto } from '../dtos';
|
||||
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
||||
import { SceneSwitchesTypeEnum } from '@app/common/constants/scene-switch-type.enum';
|
||||
import { DeviceEntity } from '../../device/entities';
|
||||
import { SceneEntity } from '../../scene/entities';
|
||||
|
||||
@Entity({ name: 'scene-device' })
|
||||
@Unique(['device', 'switchName'])
|
||||
export class SceneDeviceEntity extends AbstractEntity<SceneDeviceDto> {
|
||||
@Column({
|
||||
type: 'uuid',
|
||||
default: () => 'gen_random_uuid()',
|
||||
nullable: false,
|
||||
})
|
||||
public uuid: string;
|
||||
|
||||
@ManyToOne(() => DeviceEntity, (device) => device.sceneDevices, {
|
||||
nullable: false,
|
||||
})
|
||||
@JoinColumn({ name: 'device_uuid' })
|
||||
device: DeviceEntity;
|
||||
|
||||
@Column({
|
||||
nullable: false,
|
||||
})
|
||||
sceneUuid: string;
|
||||
|
||||
@Column({
|
||||
nullable: false,
|
||||
type: 'enum',
|
||||
enum: SceneSwitchesTypeEnum,
|
||||
})
|
||||
switchName: SceneSwitchesTypeEnum;
|
||||
|
||||
@Column({
|
||||
nullable: false,
|
||||
})
|
||||
automationTuyaUuid: string;
|
||||
|
||||
@ManyToOne(() => SceneEntity, (scene) => scene.sceneDevices, {
|
||||
nullable: false,
|
||||
})
|
||||
@JoinColumn({ name: 'scene_uuid' })
|
||||
scene: SceneEntity;
|
||||
|
||||
constructor(partial: Partial<SceneDeviceEntity>) {
|
||||
super();
|
||||
Object.assign(this, partial);
|
||||
}
|
||||
}
|
@ -0,0 +1 @@
|
||||
export * from './scene-device.repository';
|
@ -0,0 +1,10 @@
|
||||
import { DataSource, Repository } from 'typeorm';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { SceneDeviceEntity } from '../entities';
|
||||
|
||||
@Injectable()
|
||||
export class SceneDeviceRepository extends Repository<SceneDeviceEntity> {
|
||||
constructor(private dataSource: DataSource) {
|
||||
super(SceneDeviceEntity, dataSource.createEntityManager());
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { SceneDeviceEntity } from './entities';
|
||||
|
||||
@Module({
|
||||
providers: [],
|
||||
exports: [],
|
||||
controllers: [],
|
||||
imports: [TypeOrmModule.forFeature([SceneDeviceEntity])],
|
||||
})
|
||||
export class SceneDeviceRepositoryModule {}
|
@ -2,6 +2,7 @@ import { Column, Entity, ManyToOne, OneToMany } from 'typeorm';
|
||||
import { SceneDto, SceneIconDto } from '../dtos';
|
||||
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
||||
import { SceneIconType } from '@app/common/constants/secne-icon-type.enum';
|
||||
import { SceneDeviceEntity } from '../../scene-device/entities';
|
||||
|
||||
// Define SceneIconEntity before SceneEntity
|
||||
@Entity({ name: 'scene-icon' })
|
||||
@ -59,6 +60,9 @@ export class SceneEntity extends AbstractEntity<SceneDto> {
|
||||
})
|
||||
sceneIcon: SceneIconEntity;
|
||||
|
||||
@OneToMany(() => SceneDeviceEntity, (sceneDevice) => sceneDevice.scene)
|
||||
sceneDevices: SceneDeviceEntity[];
|
||||
|
||||
constructor(partial: Partial<SceneEntity>) {
|
||||
super();
|
||||
Object.assign(this, partial);
|
||||
|
@ -0,0 +1,32 @@
|
||||
import { Column, Entity, ManyToOne, JoinColumn } from 'typeorm';
|
||||
import { SpaceEntity } from './space.entity';
|
||||
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
||||
import { ProductEntity } from '../../product/entities';
|
||||
|
||||
@Entity({ name: 'space-product' })
|
||||
export class SpaceProductEntity extends AbstractEntity<SpaceProductEntity> {
|
||||
@ManyToOne(() => SpaceEntity, (space) => space.spaceProducts, {
|
||||
nullable: false,
|
||||
onDelete: 'CASCADE',
|
||||
})
|
||||
@JoinColumn({ name: 'space_uuid' })
|
||||
space: SpaceEntity;
|
||||
|
||||
@ManyToOne(() => ProductEntity, (product) => product.spaceProducts, {
|
||||
nullable: false,
|
||||
onDelete: 'CASCADE',
|
||||
})
|
||||
@JoinColumn({ name: 'product_uuid' })
|
||||
product: ProductEntity;
|
||||
|
||||
@Column({
|
||||
nullable: false,
|
||||
type: 'int',
|
||||
})
|
||||
productCount: number;
|
||||
|
||||
constructor(partial: Partial<SpaceProductEntity>) {
|
||||
super();
|
||||
Object.assign(this, partial);
|
||||
}
|
||||
}
|
@ -13,6 +13,7 @@ import { DeviceEntity } from '../../device/entities';
|
||||
import { CommunityEntity } from '../../community/entities';
|
||||
import { SubspaceEntity } from './subspace.entity';
|
||||
import { SpaceLinkEntity } from './space-link.entity';
|
||||
import { SpaceProductEntity } from './space-product.entity';
|
||||
|
||||
@Entity({ name: 'space' })
|
||||
@Unique(['invitationCode'])
|
||||
@ -86,6 +87,8 @@ export class SpaceEntity extends AbstractEntity<SpaceDto> {
|
||||
type: 'text',
|
||||
})
|
||||
public icon: string;
|
||||
@OneToMany(() => SpaceProductEntity, (spaceProduct) => spaceProduct.space)
|
||||
spaceProducts: SpaceProductEntity[];
|
||||
|
||||
constructor(partial: Partial<SpaceEntity>) {
|
||||
super();
|
||||
|
@ -1,5 +1,6 @@
|
||||
import { DataSource, Repository } from 'typeorm';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { SpaceProductEntity } from '../entities/space-product.entity';
|
||||
import { SpaceEntity, SpaceLinkEntity, SubspaceEntity } from '../entities';
|
||||
|
||||
@Injectable()
|
||||
@ -21,3 +22,9 @@ export class SpaceLinkRepository extends Repository<SpaceLinkEntity> {
|
||||
super(SpaceLinkEntity, dataSource.createEntityManager());
|
||||
}
|
||||
}
|
||||
@Injectable()
|
||||
export class SpaceProductRepository extends Repository<SpaceProductEntity> {
|
||||
constructor(private dataSource: DataSource) {
|
||||
super(SpaceProductEntity, dataSource.createEntityManager());
|
||||
}
|
||||
}
|
||||
|
@ -9,6 +9,12 @@ import { DeviceRepository } from '@app/common/modules/device/repositories';
|
||||
import { ProductRepository } from '@app/common/modules/product/repositories';
|
||||
import { DeviceStatusFirebaseModule } from '@app/common/firebase/devices-status/devices-status.module';
|
||||
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
|
||||
import { SceneService } from 'src/scene/services';
|
||||
import {
|
||||
SceneIconRepository,
|
||||
SceneRepository,
|
||||
} from '@app/common/modules/scene/repositories';
|
||||
import { SceneDeviceRepository } from '@app/common/modules/scene-device/repositories';
|
||||
|
||||
@Module({
|
||||
imports: [ConfigModule, SpaceRepositoryModule, DeviceStatusFirebaseModule],
|
||||
@ -20,6 +26,10 @@ import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service
|
||||
DeviceService,
|
||||
DeviceRepository,
|
||||
ProductRepository,
|
||||
SceneService,
|
||||
SceneIconRepository,
|
||||
SceneRepository,
|
||||
SceneDeviceRepository,
|
||||
],
|
||||
exports: [AutomationService],
|
||||
})
|
||||
|
@ -30,6 +30,7 @@ import {
|
||||
} from '@app/common/constants/automation.enum';
|
||||
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
|
||||
import { ConvertedAction } from '@app/common/integrations/tuya/interfaces';
|
||||
import { SceneDeviceRepository } from '@app/common/modules/scene-device/repositories';
|
||||
|
||||
@Injectable()
|
||||
export class AutomationService {
|
||||
@ -39,6 +40,7 @@ export class AutomationService {
|
||||
private readonly spaceRepository: SpaceRepository,
|
||||
private readonly deviceService: DeviceService,
|
||||
private readonly tuyaService: TuyaService,
|
||||
private readonly sceneDeviceRepository: SceneDeviceRepository,
|
||||
) {
|
||||
const accessKey = this.configService.get<string>('auth-config.ACCESS_KEY');
|
||||
const secretKey = this.configService.get<string>('auth-config.SECRET_KEY');
|
||||
@ -304,6 +306,16 @@ export class AutomationService {
|
||||
HttpStatus.BAD_REQUEST,
|
||||
);
|
||||
}
|
||||
const existingSceneDevice = await this.sceneDeviceRepository.findOne({
|
||||
where: { automationTuyaUuid: automationUuid },
|
||||
});
|
||||
|
||||
if (existingSceneDevice) {
|
||||
await this.sceneDeviceRepository.delete({
|
||||
automationTuyaUuid: automationUuid,
|
||||
});
|
||||
}
|
||||
|
||||
const response = this.tuyaService.deleteAutomation(
|
||||
automation.spaceId,
|
||||
automationUuid,
|
||||
@ -323,6 +335,15 @@ export class AutomationService {
|
||||
|
||||
async delete(tuyaSpaceId: string, automationUuid: string) {
|
||||
try {
|
||||
const existingSceneDevice = await this.sceneDeviceRepository.findOne({
|
||||
where: { automationTuyaUuid: automationUuid },
|
||||
});
|
||||
|
||||
if (existingSceneDevice) {
|
||||
await this.sceneDeviceRepository.delete({
|
||||
automationTuyaUuid: automationUuid,
|
||||
});
|
||||
}
|
||||
const response = await this.tuyaService.deleteAutomation(
|
||||
tuyaSpaceId,
|
||||
automationUuid,
|
||||
|
@ -12,19 +12,26 @@ import {
|
||||
Put,
|
||||
} from '@nestjs/common';
|
||||
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
|
||||
import { AddDeviceDto, UpdateDeviceInSpaceDto } from '../dtos/add.device.dto';
|
||||
import {
|
||||
AddDeviceDto,
|
||||
AddSceneToFourSceneDeviceDto,
|
||||
UpdateDeviceDto,
|
||||
UpdateDeviceInSpaceDto,
|
||||
} from '../dtos/add.device.dto';
|
||||
import { GetDeviceLogsDto } from '../dtos/get.device.dto';
|
||||
import {
|
||||
ControlDeviceDto,
|
||||
BatchControlDevicesDto,
|
||||
BatchStatusDevicesDto,
|
||||
BatchFactoryResetDevicesDto,
|
||||
GetSceneFourSceneDeviceDto,
|
||||
} from '../dtos/control.device.dto';
|
||||
import { CheckRoomGuard } from 'src/guards/room.guard';
|
||||
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
|
||||
import { CheckDeviceGuard } from 'src/guards/device.guard';
|
||||
import { SuperAdminRoleGuard } from 'src/guards/super.admin.role.guard';
|
||||
import { EnableDisableStatusEnum } from '@app/common/constants/days.enum';
|
||||
import { CheckFourAndSixSceneDeviceTypeGuard } from 'src/guards/scene.device.type.guard';
|
||||
|
||||
@ApiTags('Device Module')
|
||||
@Controller({
|
||||
@ -90,6 +97,26 @@ export class DeviceController {
|
||||
userUuid,
|
||||
);
|
||||
}
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Put(':deviceUuid')
|
||||
async updateDevice(
|
||||
@Param('deviceUuid') deviceUuid: string,
|
||||
@Body() updateDeviceDto: UpdateDeviceDto,
|
||||
) {
|
||||
const device = await this.deviceService.updateDevice(
|
||||
deviceUuid,
|
||||
updateDeviceDto,
|
||||
);
|
||||
|
||||
return {
|
||||
statusCode: HttpStatus.CREATED,
|
||||
success: true,
|
||||
message: 'device updated successfully',
|
||||
data: device,
|
||||
};
|
||||
}
|
||||
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Get(':deviceUuid/functions')
|
||||
@ -184,4 +211,35 @@ export class DeviceController {
|
||||
powerClampUuid,
|
||||
);
|
||||
}
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard, CheckFourAndSixSceneDeviceTypeGuard)
|
||||
@Post(':deviceUuid/scenes')
|
||||
async addSceneToSceneDevice(
|
||||
@Param('deviceUuid') deviceUuid: string,
|
||||
@Body() addSceneToFourSceneDeviceDto: AddSceneToFourSceneDeviceDto,
|
||||
) {
|
||||
const device = await this.deviceService.addSceneToSceneDevice(
|
||||
deviceUuid,
|
||||
addSceneToFourSceneDeviceDto,
|
||||
);
|
||||
|
||||
return {
|
||||
statusCode: HttpStatus.CREATED,
|
||||
success: true,
|
||||
message: `scene added successfully to device ${deviceUuid}`,
|
||||
data: device,
|
||||
};
|
||||
}
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard, CheckFourAndSixSceneDeviceTypeGuard)
|
||||
@Get(':deviceUuid/scenes')
|
||||
async getScenesBySceneDevice(
|
||||
@Param('deviceUuid') deviceUuid: string,
|
||||
@Query() getSceneFourSceneDeviceDto: GetSceneFourSceneDeviceDto,
|
||||
) {
|
||||
return await this.deviceService.getScenesBySceneDevice(
|
||||
deviceUuid,
|
||||
getSceneFourSceneDeviceDto,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -11,9 +11,18 @@ import { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||
import { DeviceUserPermissionRepository } from '@app/common/modules/device/repositories';
|
||||
import { UserRepository } from '@app/common/modules/user/repositories';
|
||||
import { DeviceStatusFirebaseModule } from '@app/common/firebase/devices-status/devices-status.module';
|
||||
import { SpaceModule } from 'src/space/space.module';
|
||||
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
|
||||
import { SceneService } from 'src/scene/services';
|
||||
import {
|
||||
SceneIconRepository,
|
||||
SceneRepository,
|
||||
} from '@app/common/modules/scene/repositories';
|
||||
import { SceneDeviceRepository } from '@app/common/modules/scene-device/repositories';
|
||||
@Module({
|
||||
imports: [
|
||||
ConfigModule,
|
||||
SpaceModule,
|
||||
ProductRepositoryModule,
|
||||
DeviceRepositoryModule,
|
||||
DeviceStatusFirebaseModule,
|
||||
@ -27,6 +36,11 @@ import { DeviceStatusFirebaseModule } from '@app/common/firebase/devices-status/
|
||||
SpaceRepository,
|
||||
DeviceRepository,
|
||||
UserRepository,
|
||||
TuyaService,
|
||||
SceneService,
|
||||
SceneIconRepository,
|
||||
SceneRepository,
|
||||
SceneDeviceRepository,
|
||||
],
|
||||
exports: [DeviceService],
|
||||
})
|
||||
|
@ -1,5 +1,6 @@
|
||||
import { SceneSwitchesTypeEnum } from '@app/common/constants/scene-switch-type.enum';
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { IsNotEmpty, IsString } from 'class-validator';
|
||||
import { IsEnum, IsNotEmpty, IsString } from 'class-validator';
|
||||
|
||||
export class AddDeviceDto {
|
||||
@ApiProperty({
|
||||
@ -35,3 +36,36 @@ export class UpdateDeviceInSpaceDto {
|
||||
@IsNotEmpty()
|
||||
public spaceUuid: string;
|
||||
}
|
||||
export class AddSceneToFourSceneDeviceDto {
|
||||
@ApiProperty({
|
||||
description: 'switchName',
|
||||
required: true,
|
||||
})
|
||||
@IsEnum(SceneSwitchesTypeEnum)
|
||||
@IsNotEmpty()
|
||||
switchName: SceneSwitchesTypeEnum;
|
||||
|
||||
@ApiProperty({
|
||||
description: 'sceneUuid',
|
||||
required: true,
|
||||
})
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public sceneUuid: string;
|
||||
@ApiProperty({
|
||||
description: 'spaceUuid',
|
||||
required: true,
|
||||
})
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public spaceUuid: string;
|
||||
}
|
||||
export class UpdateDeviceDto {
|
||||
@ApiProperty({
|
||||
description: 'deviceName',
|
||||
required: true,
|
||||
})
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public deviceName: string;
|
||||
}
|
||||
|
@ -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({
|
||||
@ -54,3 +54,12 @@ export class BatchFactoryResetDevicesDto {
|
||||
@IsNotEmpty()
|
||||
public devicesUuid: [string];
|
||||
}
|
||||
export class GetSceneFourSceneDeviceDto {
|
||||
@ApiProperty({
|
||||
description: 'switchName',
|
||||
required: false,
|
||||
})
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
public switchName?: string;
|
||||
}
|
||||
|
@ -90,3 +90,9 @@ export interface GetPowerClampFunctionsStatusInterface {
|
||||
success: boolean;
|
||||
msg: string;
|
||||
}
|
||||
export interface GetMacAddressInterface {
|
||||
uuid: string;
|
||||
mac: string;
|
||||
sn: string;
|
||||
id: string;
|
||||
}
|
||||
|
@ -5,15 +5,23 @@ import {
|
||||
HttpStatus,
|
||||
NotFoundException,
|
||||
BadRequestException,
|
||||
forwardRef,
|
||||
Inject,
|
||||
} from '@nestjs/common';
|
||||
import { TuyaContext } from '@tuya/tuya-connector-nodejs';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { AddDeviceDto, UpdateDeviceInSpaceDto } from '../dtos/add.device.dto';
|
||||
import {
|
||||
AddDeviceDto,
|
||||
AddSceneToFourSceneDeviceDto,
|
||||
UpdateDeviceDto,
|
||||
UpdateDeviceInSpaceDto,
|
||||
} from '../dtos/add.device.dto';
|
||||
import {
|
||||
DeviceInstructionResponse,
|
||||
GetDeviceDetailsFunctionsInterface,
|
||||
GetDeviceDetailsFunctionsStatusInterface,
|
||||
GetDeviceDetailsInterface,
|
||||
GetMacAddressInterface,
|
||||
GetPowerClampFunctionsStatusInterface,
|
||||
controlDeviceInterface,
|
||||
getDeviceLogsInterface,
|
||||
@ -28,6 +36,7 @@ import {
|
||||
BatchFactoryResetDevicesDto,
|
||||
BatchStatusDevicesDto,
|
||||
ControlDeviceDto,
|
||||
GetSceneFourSceneDeviceDto,
|
||||
} from '../dtos/control.device.dto';
|
||||
import { convertKeysToCamelCase } from '@app/common/helper/camelCaseConverter';
|
||||
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
||||
@ -40,6 +49,12 @@ import { DeviceStatuses } from '@app/common/constants/device-status.enum';
|
||||
import { CommonErrorCodes } from '@app/common/constants/error-codes.enum';
|
||||
import { BatteryStatus } from '@app/common/constants/battery-status.enum';
|
||||
import { SpaceEntity } from '@app/common/modules/space/entities';
|
||||
import { SceneService } from 'src/scene/services';
|
||||
import { AddAutomationDto } from 'src/automation/dtos';
|
||||
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
|
||||
import { SceneDeviceRepository } from '@app/common/modules/scene-device/repositories';
|
||||
import { SceneSwitchesTypeEnum } from '@app/common/constants/scene-switch-type.enum';
|
||||
import { AUTOMATION_CONFIG } from '@app/common/constants/automation.enum';
|
||||
|
||||
@Injectable()
|
||||
export class DeviceService {
|
||||
@ -47,9 +62,13 @@ export class DeviceService {
|
||||
constructor(
|
||||
private readonly configService: ConfigService,
|
||||
private readonly deviceRepository: DeviceRepository,
|
||||
private readonly sceneDeviceRepository: SceneDeviceRepository,
|
||||
private readonly productRepository: ProductRepository,
|
||||
private readonly deviceStatusFirebaseService: DeviceStatusFirebaseService,
|
||||
private readonly spaceRepository: SpaceRepository,
|
||||
@Inject(forwardRef(() => SceneService))
|
||||
private readonly sceneService: SceneService,
|
||||
private readonly tuyaService: TuyaService,
|
||||
) {
|
||||
const accessKey = this.configService.get<string>('auth-config.ACCESS_KEY');
|
||||
const secretKey = this.configService.get<string>('auth-config.SECRET_KEY');
|
||||
@ -64,11 +83,15 @@ export class DeviceService {
|
||||
deviceUuid: string,
|
||||
withProductDevice: boolean = true,
|
||||
) {
|
||||
return await this.deviceRepository.findOne({
|
||||
where: {
|
||||
uuid: deviceUuid,
|
||||
},
|
||||
...(withProductDevice && { relations: ['productDevice'] }),
|
||||
const relations = ['subspace'];
|
||||
|
||||
if (withProductDevice) {
|
||||
relations.push('productDevice');
|
||||
}
|
||||
|
||||
return this.deviceRepository.findOne({
|
||||
where: { uuid: deviceUuid },
|
||||
relations,
|
||||
});
|
||||
}
|
||||
|
||||
@ -274,6 +297,26 @@ export class DeviceService {
|
||||
);
|
||||
}
|
||||
}
|
||||
async updateDeviceNameTuya(
|
||||
deviceId: string,
|
||||
deviceName: string,
|
||||
): Promise<controlDeviceInterface> {
|
||||
try {
|
||||
const path = `/v2.0/cloud/thing/${deviceId}/attribute`;
|
||||
const response = await this.tuya.request({
|
||||
method: 'POST',
|
||||
path,
|
||||
body: { type: 1, data: deviceName },
|
||||
});
|
||||
|
||||
return response as controlDeviceInterface;
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
'Error updating device name from Tuya',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
async controlDevice(controlDeviceDto: ControlDeviceDto, deviceUuid: string) {
|
||||
try {
|
||||
const deviceDetails = await this.getDeviceByDeviceUuid(deviceUuid, false);
|
||||
@ -519,6 +562,9 @@ export class DeviceService {
|
||||
const response = await this.getDeviceDetailsByDeviceIdTuya(
|
||||
deviceDetails.deviceTuyaUuid,
|
||||
);
|
||||
const macAddress = await this.getMacAddressByDeviceIdTuya(
|
||||
deviceDetails.deviceTuyaUuid,
|
||||
);
|
||||
|
||||
return {
|
||||
...response,
|
||||
@ -526,6 +572,8 @@ export class DeviceService {
|
||||
productUuid: deviceDetails.productDevice.uuid,
|
||||
productType: deviceDetails.productDevice.prodType,
|
||||
permissionType: userDevicePermission,
|
||||
macAddress: macAddress.mac,
|
||||
subspace: deviceDetails.subspace ? deviceDetails.subspace : {},
|
||||
};
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
@ -534,6 +582,27 @@ export class DeviceService {
|
||||
);
|
||||
}
|
||||
}
|
||||
async updateDevice(deviceUuid: string, updateDeviceDto: UpdateDeviceDto) {
|
||||
try {
|
||||
const device = await this.getDeviceByDeviceUuid(deviceUuid);
|
||||
if (device.deviceTuyaUuid) {
|
||||
await this.updateDeviceNameTuya(
|
||||
device.deviceTuyaUuid,
|
||||
updateDeviceDto.deviceName,
|
||||
);
|
||||
}
|
||||
|
||||
return {
|
||||
uuid: device.uuid,
|
||||
deviceName: updateDeviceDto.deviceName,
|
||||
};
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
'Error updating device',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
async getDeviceDetailsByDeviceIdTuya(
|
||||
deviceId: string,
|
||||
): Promise<GetDeviceDetailsInterface> {
|
||||
@ -566,7 +635,24 @@ export class DeviceService {
|
||||
);
|
||||
}
|
||||
}
|
||||
async getMacAddressByDeviceIdTuya(
|
||||
deviceId: string,
|
||||
): Promise<GetMacAddressInterface> {
|
||||
try {
|
||||
const path = `/v1.0/devices/factory-infos?device_ids=${deviceId}`;
|
||||
const response = await this.tuya.request({
|
||||
method: 'GET',
|
||||
path,
|
||||
});
|
||||
|
||||
return response.result[0];
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
'Error fetching mac address device from Tuya',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
async getDeviceInstructionByDeviceId(
|
||||
deviceUuid: string,
|
||||
): Promise<DeviceInstructionResponse> {
|
||||
@ -630,7 +716,6 @@ export class DeviceService {
|
||||
status: deviceStatus.result[0].status,
|
||||
};
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
throw new HttpException(
|
||||
'Error fetching device functions status',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
@ -1136,4 +1221,175 @@ export class DeviceService {
|
||||
|
||||
return descendants;
|
||||
}
|
||||
|
||||
async addSceneToSceneDevice(
|
||||
deviceUuid: string,
|
||||
addSceneToFourSceneDeviceDto: AddSceneToFourSceneDeviceDto,
|
||||
) {
|
||||
try {
|
||||
const { spaceUuid, sceneUuid, switchName } = addSceneToFourSceneDeviceDto;
|
||||
|
||||
if (!spaceUuid || !sceneUuid || !switchName) {
|
||||
throw new BadRequestException('Missing required fields in DTO');
|
||||
}
|
||||
|
||||
const [sceneData, spaceData, deviceData] = await Promise.all([
|
||||
this.sceneService.findScene(sceneUuid),
|
||||
this.sceneService.getSpaceByUuid(spaceUuid),
|
||||
this.getDeviceByDeviceUuid(deviceUuid),
|
||||
]);
|
||||
|
||||
const shortUuid = deviceUuid.slice(0, 6); // First 6 characters of the UUID
|
||||
const timestamp = Math.floor(Date.now() / 1000); // Current timestamp in seconds
|
||||
const automationName = `Auto_${shortUuid}_${timestamp}`;
|
||||
|
||||
const addAutomationData: AddAutomationDto = {
|
||||
spaceUuid: spaceData.spaceTuyaUuid,
|
||||
automationName,
|
||||
decisionExpr: AUTOMATION_CONFIG.DECISION_EXPR,
|
||||
effectiveTime: {
|
||||
start: AUTOMATION_CONFIG.DEFAULT_START_TIME,
|
||||
end: AUTOMATION_CONFIG.DEFAULT_END_TIME,
|
||||
loops: AUTOMATION_CONFIG.DEFAULT_LOOPS,
|
||||
},
|
||||
conditions: [
|
||||
{
|
||||
code: 1,
|
||||
entityId: deviceData.deviceTuyaUuid,
|
||||
entityType: AUTOMATION_CONFIG.CONDITION_TYPE,
|
||||
expr: {
|
||||
comparator: AUTOMATION_CONFIG.COMPARATOR,
|
||||
statusCode: switchName,
|
||||
statusValue: AUTOMATION_CONFIG.SCENE_STATUS_VALUE,
|
||||
},
|
||||
},
|
||||
],
|
||||
actions: [
|
||||
{
|
||||
actionExecutor: AUTOMATION_CONFIG.ACTION_EXECUTOR,
|
||||
entityId: sceneData.sceneTuyaUuid,
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
const automation = await this.tuyaService.createAutomation(
|
||||
addAutomationData.spaceUuid,
|
||||
addAutomationData.automationName,
|
||||
addAutomationData.effectiveTime,
|
||||
addAutomationData.decisionExpr,
|
||||
addAutomationData.conditions,
|
||||
addAutomationData.actions,
|
||||
);
|
||||
|
||||
if (automation.success) {
|
||||
const existingSceneDevice = await this.sceneDeviceRepository.findOne({
|
||||
where: {
|
||||
device: { uuid: deviceUuid },
|
||||
switchName: switchName,
|
||||
},
|
||||
relations: ['scene', 'device'],
|
||||
});
|
||||
|
||||
if (existingSceneDevice) {
|
||||
await this.tuyaService.deleteAutomation(
|
||||
spaceData.spaceTuyaUuid,
|
||||
existingSceneDevice.automationTuyaUuid,
|
||||
);
|
||||
|
||||
existingSceneDevice.automationTuyaUuid = automation.result.id;
|
||||
existingSceneDevice.scene = sceneData;
|
||||
existingSceneDevice.device = deviceData;
|
||||
existingSceneDevice.switchName = switchName;
|
||||
|
||||
return await this.sceneDeviceRepository.save(existingSceneDevice);
|
||||
} else {
|
||||
const sceneDevice = await this.sceneDeviceRepository.save({
|
||||
scene: sceneData,
|
||||
device: deviceData,
|
||||
automationTuyaUuid: automation.result.id,
|
||||
switchName: switchName,
|
||||
});
|
||||
return sceneDevice;
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
const errorMessage = err.message || 'Error creating automation';
|
||||
const errorStatus = err.status || HttpStatus.INTERNAL_SERVER_ERROR;
|
||||
throw new HttpException(errorMessage, errorStatus);
|
||||
}
|
||||
}
|
||||
|
||||
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 },
|
||||
switchName:
|
||||
getSceneFourSceneDeviceDto.switchName as SceneSwitchesTypeEnum,
|
||||
},
|
||||
relations: ['device', 'scene'],
|
||||
});
|
||||
|
||||
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,
|
||||
);
|
||||
|
||||
return {
|
||||
switchName: sceneDevice.switchName,
|
||||
createdAt: sceneDevice.createdAt,
|
||||
updatedAt: sceneDevice.updatedAt,
|
||||
deviceUuid: sceneDevice.device.uuid,
|
||||
scene: sceneDetails.data,
|
||||
};
|
||||
}
|
||||
|
||||
// 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(
|
||||
`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;
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
error.message || 'Failed to fetch scenes for device',
|
||||
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -11,6 +11,13 @@ import { ProductRepository } from '@app/common/modules/product/repositories';
|
||||
import { DeviceStatusFirebaseService } from '@app/common/firebase/devices-status/services/devices-status.service';
|
||||
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||
import { DeviceStatusLogRepository } from '@app/common/modules/device-status-log/repositories';
|
||||
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
|
||||
import { SceneService } from 'src/scene/services';
|
||||
import {
|
||||
SceneIconRepository,
|
||||
SceneRepository,
|
||||
} from '@app/common/modules/scene/repositories';
|
||||
import { SceneDeviceRepository } from '@app/common/modules/scene-device/repositories';
|
||||
@Module({
|
||||
imports: [ConfigModule, DeviceRepositoryModule],
|
||||
controllers: [DoorLockController],
|
||||
@ -24,6 +31,11 @@ import { DeviceStatusLogRepository } from '@app/common/modules/device-status-log
|
||||
DeviceStatusFirebaseService,
|
||||
SpaceRepository,
|
||||
DeviceStatusLogRepository,
|
||||
TuyaService,
|
||||
SceneService,
|
||||
SceneIconRepository,
|
||||
SceneRepository,
|
||||
SceneDeviceRepository,
|
||||
],
|
||||
exports: [DoorLockService],
|
||||
})
|
||||
|
@ -120,7 +120,6 @@ export class GroupService {
|
||||
throw new HttpException('No devices found', HttpStatus.NOT_FOUND);
|
||||
return devices.flat(); // Flatten the array since flatMap was used
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
throw new HttpException(
|
||||
'This space does not have any devices for the specified group name',
|
||||
HttpStatus.NOT_FOUND,
|
||||
|
42
src/guards/scene.device.type.guard.ts
Normal file
42
src/guards/scene.device.type.guard.ts
Normal file
@ -0,0 +1,42 @@
|
||||
import { ProductType } from '@app/common/constants/product-type.enum';
|
||||
import {
|
||||
Injectable,
|
||||
CanActivate,
|
||||
ExecutionContext,
|
||||
BadRequestException,
|
||||
HttpException,
|
||||
} from '@nestjs/common';
|
||||
import { DeviceService } from 'src/device/services';
|
||||
|
||||
@Injectable()
|
||||
export class CheckFourAndSixSceneDeviceTypeGuard implements CanActivate {
|
||||
constructor(private readonly deviceService: DeviceService) {}
|
||||
|
||||
async canActivate(context: ExecutionContext): Promise<boolean> {
|
||||
const request = context.switchToHttp().getRequest();
|
||||
const deviceUuid = request.params.deviceUuid;
|
||||
|
||||
if (!deviceUuid) {
|
||||
throw new BadRequestException('Device UUID is required');
|
||||
}
|
||||
|
||||
try {
|
||||
const deviceDetails =
|
||||
await this.deviceService.getDeviceByDeviceUuid(deviceUuid);
|
||||
|
||||
if (
|
||||
deviceDetails.productDevice.prodType !== ProductType.FOUR_S &&
|
||||
deviceDetails.productDevice.prodType !== ProductType.SIX_S
|
||||
) {
|
||||
throw new BadRequestException('The device type is not supported');
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
error.message || 'An error occurred',
|
||||
error.status || 500,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
@ -13,6 +13,7 @@ import {
|
||||
SceneRepository,
|
||||
} from '@app/common/modules/scene/repositories';
|
||||
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
|
||||
import { SceneDeviceRepository } from '@app/common/modules/scene-device/repositories';
|
||||
|
||||
@Module({
|
||||
imports: [ConfigModule, SpaceRepositoryModule, DeviceStatusFirebaseModule],
|
||||
@ -26,6 +27,7 @@ import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service
|
||||
ProductRepository,
|
||||
SceneIconRepository,
|
||||
SceneRepository,
|
||||
SceneDeviceRepository,
|
||||
],
|
||||
exports: [SceneService],
|
||||
})
|
||||
|
@ -3,6 +3,8 @@ import {
|
||||
HttpException,
|
||||
HttpStatus,
|
||||
BadRequestException,
|
||||
forwardRef,
|
||||
Inject,
|
||||
} from '@nestjs/common';
|
||||
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||
import {
|
||||
@ -14,7 +16,6 @@ import {
|
||||
UpdateSceneTapToRunDto,
|
||||
} from '../dtos';
|
||||
import { convertKeysToSnakeCase } from '@app/common/helper/snakeCaseConverter';
|
||||
import { DeviceService } from 'src/device/services';
|
||||
import {
|
||||
AddTapToRunSceneInterface,
|
||||
DeleteTapToRunSceneInterface,
|
||||
@ -37,6 +38,7 @@ import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
||||
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
|
||||
import { HttpStatusCode } from 'axios';
|
||||
import { ConvertedAction } from '@app/common/integrations/tuya/interfaces';
|
||||
import { DeviceService } from 'src/device/services';
|
||||
|
||||
@Injectable()
|
||||
export class SceneService {
|
||||
@ -44,8 +46,9 @@ export class SceneService {
|
||||
private readonly spaceRepository: SpaceRepository,
|
||||
private readonly sceneIconRepository: SceneIconRepository,
|
||||
private readonly sceneRepository: SceneRepository,
|
||||
private readonly deviceService: DeviceService,
|
||||
private readonly tuyaService: TuyaService,
|
||||
@Inject(forwardRef(() => DeviceService))
|
||||
private readonly deviceService: DeviceService,
|
||||
) {}
|
||||
|
||||
async createScene(
|
||||
@ -178,7 +181,10 @@ export class SceneService {
|
||||
});
|
||||
|
||||
if (!scenesData.length) {
|
||||
return [];
|
||||
throw new HttpException(
|
||||
`No scenes found for space UUID ${spaceUuid} with showInHomePage ${showInHomePage} `,
|
||||
HttpStatus.NOT_FOUND,
|
||||
);
|
||||
}
|
||||
|
||||
const scenes = await Promise.all(
|
||||
@ -438,7 +444,6 @@ export class SceneService {
|
||||
if (err instanceof BadRequestException) {
|
||||
throw err;
|
||||
} else {
|
||||
console.log(err);
|
||||
throw new HttpException(
|
||||
`An error occurred while retrieving scene details for ${scene.uuid}`,
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
@ -454,9 +459,6 @@ export class SceneService {
|
||||
const space = await this.getSpaceByUuid(scene.spaceUuid);
|
||||
|
||||
await this.delete(scene.sceneTuyaUuid, space.spaceTuyaUuid);
|
||||
|
||||
await this.sceneRepository.remove(scene);
|
||||
|
||||
return new SuccessResponseDto({
|
||||
message: `Scene with ID ${sceneUuid} deleted successfully`,
|
||||
});
|
||||
@ -564,7 +566,6 @@ export class SceneService {
|
||||
};
|
||||
} catch (err) {
|
||||
if (err instanceof BadRequestException) {
|
||||
console.log(err);
|
||||
throw err;
|
||||
} else {
|
||||
throw new HttpException(
|
||||
|
@ -1,11 +1,14 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { Type } from 'class-transformer';
|
||||
import {
|
||||
IsArray,
|
||||
IsBoolean,
|
||||
IsNotEmpty,
|
||||
IsNumber,
|
||||
IsOptional,
|
||||
IsString,
|
||||
IsUUID,
|
||||
ValidateNested,
|
||||
} from 'class-validator';
|
||||
|
||||
export class AddSpaceDto {
|
||||
@ -50,6 +53,11 @@ export class AddSpaceDto {
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
direction: string;
|
||||
|
||||
@IsArray()
|
||||
@ValidateNested({ each: true })
|
||||
@Type(() => ProductAssignmentDto)
|
||||
products: ProductAssignmentDto[];
|
||||
}
|
||||
|
||||
export class AddUserSpaceDto {
|
||||
@ -87,7 +95,16 @@ export class AddUserSpaceUsingCodeDto {
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public inviteCode: string;
|
||||
|
||||
constructor(dto: Partial<AddUserSpaceDto>) {
|
||||
Object.assign(this, dto);
|
||||
}
|
||||
}
|
||||
|
||||
class ProductAssignmentDto {
|
||||
@IsNotEmpty()
|
||||
productId: string;
|
||||
|
||||
@IsNotEmpty()
|
||||
count: number;
|
||||
}
|
||||
|
@ -4,3 +4,4 @@ export * from './space-device.service';
|
||||
export * from './subspace';
|
||||
export * from './space-link';
|
||||
export * from './space-scene.service';
|
||||
export * from './space-products';
|
||||
|
1
src/space/services/space-products/index.ts
Normal file
1
src/space/services/space-products/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './space-products.service';
|
95
src/space/services/space-products/space-products.service.ts
Normal file
95
src/space/services/space-products/space-products.service.ts
Normal file
@ -0,0 +1,95 @@
|
||||
import { Injectable, HttpException, HttpStatus } from '@nestjs/common';
|
||||
import { ProductRepository } from '@app/common/modules/product/repositories';
|
||||
import { SpaceEntity } from '@app/common/modules/space/entities';
|
||||
import { SpaceProductEntity } from '@app/common/modules/space/entities/space-product.entity';
|
||||
import { SpaceProductRepository } from '@app/common/modules/space/repositories';
|
||||
import { In } from 'typeorm';
|
||||
|
||||
@Injectable()
|
||||
export class SpaceProductService {
|
||||
constructor(
|
||||
private readonly productRepository: ProductRepository,
|
||||
private readonly spaceProductRepository: SpaceProductRepository,
|
||||
) {}
|
||||
|
||||
async assignProductsToSpace(
|
||||
space: SpaceEntity,
|
||||
products: { productId: string; count: number }[],
|
||||
): Promise<SpaceProductEntity[]> {
|
||||
try {
|
||||
const uniqueProducts = this.validateUniqueProducts(products);
|
||||
const productEntities = await this.getProductEntities(uniqueProducts);
|
||||
|
||||
const spaceProductEntities = uniqueProducts.map(
|
||||
({ productId, count }) => {
|
||||
const product = productEntities.get(productId);
|
||||
if (!product) {
|
||||
throw new HttpException(
|
||||
`Product with ID ${productId} not found`,
|
||||
HttpStatus.NOT_FOUND,
|
||||
);
|
||||
}
|
||||
|
||||
return this.spaceProductRepository.create({
|
||||
space,
|
||||
product,
|
||||
productCount: count,
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
return await this.spaceProductRepository.save(spaceProductEntities);
|
||||
} catch (error) {
|
||||
console.error('Error assigning products to space:', error);
|
||||
|
||||
if (!(error instanceof HttpException)) {
|
||||
throw new HttpException(
|
||||
'An error occurred while assigning products to the space',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
private validateUniqueProducts(
|
||||
products: { productId: string; count: number }[],
|
||||
): { productId: string; count: number }[] {
|
||||
const productIds = new Set();
|
||||
const uniqueProducts = [];
|
||||
|
||||
for (const product of products) {
|
||||
if (productIds.has(product.productId)) {
|
||||
throw new HttpException(
|
||||
`Duplicate product ID found: ${product.productId}`,
|
||||
HttpStatus.BAD_REQUEST,
|
||||
);
|
||||
}
|
||||
productIds.add(product.productId);
|
||||
uniqueProducts.push(product);
|
||||
}
|
||||
|
||||
return uniqueProducts;
|
||||
}
|
||||
|
||||
private async getProductEntities(
|
||||
products: { productId: string; count: number }[],
|
||||
): Promise<Map<string, any>> {
|
||||
try {
|
||||
const productIds = products.map((p) => p.productId);
|
||||
|
||||
const productEntities = await this.productRepository.find({
|
||||
where: { prodId: In(productIds) },
|
||||
});
|
||||
|
||||
return new Map(productEntities.map((p) => [p.prodId, p]));
|
||||
} catch (error) {
|
||||
console.error('Error fetching product entities:', error);
|
||||
throw new HttpException(
|
||||
'Failed to fetch product entities',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
@ -12,6 +12,7 @@ import { CommunityRepository } from '@app/common/modules/community/repositories'
|
||||
import { SpaceEntity } from '@app/common/modules/space/entities';
|
||||
import { generateRandomString } from '@app/common/helper/randomString';
|
||||
import { SpaceLinkService } from './space-link';
|
||||
import { SpaceProductService } from './space-products';
|
||||
|
||||
@Injectable()
|
||||
export class SpaceService {
|
||||
@ -19,13 +20,14 @@ export class SpaceService {
|
||||
private readonly spaceRepository: SpaceRepository,
|
||||
private readonly communityRepository: CommunityRepository,
|
||||
private readonly spaceLinkService: SpaceLinkService,
|
||||
private readonly spaceProductService: SpaceProductService,
|
||||
) {}
|
||||
|
||||
async createSpace(
|
||||
addSpaceDto: AddSpaceDto,
|
||||
communityId: string,
|
||||
): Promise<BaseResponseDto> {
|
||||
const { parentUuid, direction } = addSpaceDto;
|
||||
const { parentUuid, direction, products } = addSpaceDto;
|
||||
|
||||
const community = await this.validateCommunity(communityId);
|
||||
|
||||
@ -47,6 +49,13 @@ export class SpaceService {
|
||||
);
|
||||
}
|
||||
|
||||
if (products && products.length > 0) {
|
||||
await this.spaceProductService.assignProductsToSpace(
|
||||
newSpace,
|
||||
products,
|
||||
);
|
||||
}
|
||||
|
||||
return new SuccessResponseDto({
|
||||
statusCode: HttpStatus.CREATED,
|
||||
data: newSpace,
|
||||
|
@ -12,6 +12,7 @@ import {
|
||||
import {
|
||||
SpaceDeviceService,
|
||||
SpaceLinkService,
|
||||
SpaceProductService,
|
||||
SpaceSceneService,
|
||||
SpaceService,
|
||||
SpaceUserService,
|
||||
@ -19,6 +20,7 @@ import {
|
||||
SubSpaceService,
|
||||
} from './services';
|
||||
import {
|
||||
SpaceProductRepository,
|
||||
SpaceRepository,
|
||||
SubspaceRepository,
|
||||
SpaceLinkRepository,
|
||||
@ -39,6 +41,7 @@ import {
|
||||
import { DeviceService } from 'src/device/services';
|
||||
import { DeviceStatusFirebaseService } from '@app/common/firebase/devices-status/services/devices-status.service';
|
||||
import { DeviceStatusLogRepository } from '@app/common/modules/device-status-log/repositories';
|
||||
import { SceneDeviceRepository } from '@app/common/modules/scene-device/repositories';
|
||||
|
||||
@Module({
|
||||
imports: [ConfigModule, SpaceRepositoryModule],
|
||||
@ -73,6 +76,9 @@ import { DeviceStatusLogRepository } from '@app/common/modules/device-status-log
|
||||
DeviceService,
|
||||
DeviceStatusFirebaseService,
|
||||
DeviceStatusLogRepository,
|
||||
SceneDeviceRepository,
|
||||
SpaceProductService,
|
||||
SpaceProductRepository,
|
||||
],
|
||||
exports: [SpaceService],
|
||||
})
|
||||
|
@ -13,6 +13,13 @@ import { DeviceStatusFirebaseService } from '@app/common/firebase/devices-status
|
||||
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||
import { VisitorPasswordRepository } from '@app/common/modules/visitor-password/repositories';
|
||||
import { DeviceStatusLogRepository } from '@app/common/modules/device-status-log/repositories';
|
||||
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
|
||||
import { SceneService } from 'src/scene/services';
|
||||
import {
|
||||
SceneIconRepository,
|
||||
SceneRepository,
|
||||
} from '@app/common/modules/scene/repositories';
|
||||
import { SceneDeviceRepository } from '@app/common/modules/scene-device/repositories';
|
||||
@Module({
|
||||
imports: [ConfigModule, DeviceRepositoryModule, DoorLockModule],
|
||||
controllers: [VisitorPasswordController],
|
||||
@ -27,6 +34,11 @@ import { DeviceStatusLogRepository } from '@app/common/modules/device-status-log
|
||||
DeviceRepository,
|
||||
VisitorPasswordRepository,
|
||||
DeviceStatusLogRepository,
|
||||
TuyaService,
|
||||
SceneService,
|
||||
SceneIconRepository,
|
||||
SceneRepository,
|
||||
SceneDeviceRepository,
|
||||
],
|
||||
exports: [VisitorPasswordService],
|
||||
})
|
||||
|
Reference in New Issue
Block a user