Merge pull request #178 from SyncrowIOT/feat/edit-space-model

Feat/edit space model
This commit is contained in:
hannathkadher
2024-12-30 14:44:15 +04:00
committed by GitHub
125 changed files with 3176 additions and 1932 deletions

View File

@ -9,13 +9,10 @@ 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';
import { SpaceProductItemRepository, SpaceRepository } from './modules/space';
import { SpaceRepository } from './modules/space';
import {
SpaceModelRepository,
SpaceProductModelRepository,
SubspaceModelRepository,
SubspaceProductItemModelRepository,
SubspaceProductModelRepository,
} from './modules/space-model';
import { SubspaceRepository } from './modules/space/repositories/subspace.repository';
@Module({
@ -28,11 +25,7 @@ import { SubspaceRepository } from './modules/space/repositories/subspace.reposi
SpaceRepository,
SubspaceRepository,
SubspaceModelRepository,
SubspaceProductModelRepository,
SubspaceProductItemModelRepository,
SpaceModelRepository,
SpaceProductModelRepository,
SpaceProductItemRepository,
],
exports: [
CommonService,
@ -45,10 +38,7 @@ import { SubspaceRepository } from './modules/space/repositories/subspace.reposi
SpaceRepository,
SubspaceRepository,
SubspaceModelRepository,
SubspaceProductModelRepository,
SubspaceProductItemModelRepository,
SpaceModelRepository,
SpaceProductModelRepository,
],
imports: [
ConfigModule.forRoot({

View File

@ -282,6 +282,14 @@ export class ControllerRoute {
public static readonly LIST_SPACE_MODEL_SUMMARY = 'List Space Models';
public static readonly LIST_SPACE_MODEL_DESCRIPTION =
'This endpoint allows you to retrieve a list of space models within a specified project. Each space model includes its structure, associated subspaces, products, and product items.';
public static readonly UPDATE_SPACE_MODEL_SUMMARY = 'Update Space Model';
public static readonly UPDATE_SPACE_MODEL_DESCRIPTION =
'This endpoint allows you to update a Space Model attributesas well as manage its associated Subspaces and Device';
public static readonly DELETE_SPACE_MODEL_SUMMARY = 'Delete Space Model';
public static readonly DELETE_SPACE_MODEL_DESCRIPTION =
'This endpoint allows you to delete a specified Space Model within a project. Deleting a Space Model disables the model and all its associated subspaces and tags, ensuring they are no longer active but remain in the system for auditing.';
};
};

View File

@ -0,0 +1,5 @@
export enum ModifyAction {
ADD = 'add',
UPDATE = 'update',
DELETE = 'delete',
}

View File

@ -12,7 +12,8 @@ export const PermissionMapping = {
'ADD',
'UPDATE',
'DELETE',
'MODULE_ADD',
'MODEL_ADD',
'MODEL_DELETE',
'MODEL_VIEW',
'ASSIGN_USER_TO_SPACE',
'DELETE_USER_FROM_SPACE',

View File

@ -16,8 +16,10 @@ export const RolePermissions = {
'SPACE_ADD',
'SPACE_UPDATE',
'SPACE_DELETE',
'SPACE_MODULE_ADD',
'SPACE_MODEL_ADD',
'SPACE_MODEL_VIEW',
'SPACE_MODEL_UPDATE',
'SPACE_MODEL_DELETE',
'ASSIGN_USER_TO_SPACE',
'DELETE_USER_FROM_SPACE',
'SUBSPACE_VIEW',
@ -60,8 +62,10 @@ export const RolePermissions = {
'SPACE_ADD',
'SPACE_UPDATE',
'SPACE_DELETE',
'SPACE_MODULE_ADD',
'SPACE_MODEL_ADD',
'SPACE_MODEL_VIEW',
'SPACE_MODEL_UPDATE',
'SPACE_MODEL_DELETE',
'ASSIGN_USER_TO_SPACE',
'DELETE_USER_FROM_SPACE',
'SUBSPACE_VIEW',

View File

@ -11,10 +11,8 @@ import { PermissionTypeEntity } from '../modules/permission/entities';
import {
SpaceEntity,
SpaceLinkEntity,
SpaceProductItemEntity,
SubspaceEntity,
SubspaceProductEntity,
SubspaceProductItemEntity,
TagEntity,
} from '../modules/space/entities';
import { UserSpaceEntity } from '../modules/user/entities';
import { DeviceUserPermissionEntity } from '../modules/device/entities';
@ -28,15 +26,11 @@ 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';
import { ProjectEntity } from '../modules/project/entities';
import {
SpaceModelEntity,
SpaceProductItemModelEntity,
SpaceProductModelEntity,
SubspaceModelEntity,
SubspaceProductItemModelEntity,
SubspaceProductModelEntity,
TagModel,
} from '../modules/space-model/entities';
import {
InviteUserEntity,
@ -68,7 +62,7 @@ import {
SpaceEntity,
SpaceLinkEntity,
SubspaceEntity,
SpaceProductEntity,
TagEntity,
UserSpaceEntity,
DeviceUserPermissionEntity,
RoleTypeEntity,
@ -82,15 +76,8 @@ import {
SceneIconEntity,
SceneDeviceEntity,
SpaceModelEntity,
SpaceProductModelEntity,
SpaceProductItemModelEntity,
SubspaceModelEntity,
SpaceProductEntity,
SpaceProductItemEntity,
SubspaceProductModelEntity,
SubspaceProductItemModelEntity,
SubspaceProductEntity,
SubspaceProductItemEntity,
TagModel,
InviteUserEntity,
InviteUserSpaceEntity,
],

View File

@ -6,10 +6,11 @@ import {
Unique,
Index,
JoinColumn,
OneToOne,
} from 'typeorm';
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
import { DeviceDto, DeviceUserPermissionDto } from '../dtos/device.dto';
import { SpaceEntity, SubspaceEntity } from '../../space/entities';
import { SpaceEntity, SubspaceEntity, TagEntity } from '../../space/entities';
import { ProductEntity } from '../../product/entities';
import { UserEntity } from '../../user/entities';
import { DeviceNotificationDto } from '../dtos';
@ -74,6 +75,11 @@ export class DeviceEntity extends AbstractEntity<DeviceDto> {
@OneToMany(() => SceneDeviceEntity, (sceneDevice) => sceneDevice.device, {})
sceneDevices: SceneDeviceEntity[];
@OneToOne(() => TagEntity, (tag) => tag.device, {
nullable: true,
})
tag: TagEntity;
constructor(partial: Partial<DeviceEntity>) {
super();
Object.assign(this, partial);
@ -102,6 +108,7 @@ export class DeviceNotificationEntity extends AbstractEntity<DeviceNotificationD
nullable: false,
})
user: UserEntity;
constructor(partial: Partial<DeviceNotificationEntity>) {
super();
Object.assign(this, partial);

View File

@ -2,10 +2,8 @@ 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';
import { SpaceProductModelEntity } from '../../space-model/entities';
import { SubspaceProductModelEntity } from '../../space-model/entities/subspace-model/subspace-product-model.entity';
import { TagModel } from '../../space-model';
import { TagEntity } from '../../space/entities/tag.entity';
@Entity({ name: 'product' })
export class ProductEntity extends AbstractEntity<ProductDto> {
@Column({
@ -29,20 +27,11 @@ export class ProductEntity extends AbstractEntity<ProductDto> {
})
public prodType: string;
@OneToMany(() => SpaceProductEntity, (spaceProduct) => spaceProduct.product)
spaceProducts: SpaceProductEntity[];
@OneToMany(() => TagModel, (tag) => tag.product)
tagModels: TagModel[];
@OneToMany(
() => SpaceProductModelEntity,
(spaceProductModel) => spaceProductModel.product,
)
spaceProductModels: SpaceProductModelEntity[];
@OneToMany(
() => SubspaceProductModelEntity,
(subspaceProductModel) => subspaceProductModel.product,
)
subpaceProductModels: SubspaceProductModelEntity[];
@OneToMany(() => TagEntity, (tag) => tag.product)
tags: TagEntity[];
@OneToMany(
() => DeviceEntity,

View File

@ -44,6 +44,12 @@ export class SceneDeviceEntity extends AbstractEntity<SceneDeviceDto> {
@JoinColumn({ name: 'scene_uuid' })
scene: SceneEntity;
@Column({
nullable: false,
default: false,
})
public disabled: boolean;
constructor(partial: Partial<SceneDeviceEntity>) {
super();
Object.assign(this, partial);

View File

@ -59,6 +59,12 @@ export class SceneEntity extends AbstractEntity<SceneDto> {
@JoinColumn({ name: 'space_uuid' })
space: SpaceEntity;
@Column({
nullable: false,
default: false,
})
public disabled: boolean;
@ManyToOne(() => SceneIconEntity, (icon) => icon.scenesIconEntity, {
nullable: false,
})

View File

@ -1,4 +1,2 @@
export * from './subspace-model';
export * from './space-model.dto';
export * from './space-product-item-model.dto';
export * from './space-product-model.dto';

View File

@ -1,15 +0,0 @@
import { IsString, IsNotEmpty } from 'class-validator';
export class SpaceProductItemModelDto {
@IsString()
@IsNotEmpty()
uuid: string;
@IsString()
@IsNotEmpty()
tag: string;
@IsString()
@IsNotEmpty()
spaceProductModelUuid: string;
}

View File

@ -1,23 +0,0 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty, IsNumber, IsString } from 'class-validator';
import { SpaceProductItemModelDto } from './space-product-item-model.dto';
export class SpaceProductModelDto {
@IsString()
@IsNotEmpty()
uuid: string;
@IsNumber()
@IsNotEmpty()
productCount: number;
@IsString()
@IsNotEmpty()
productUuid: string;
@ApiProperty({
description: 'List of individual items with specific names for the product',
type: [SpaceProductItemModelDto],
})
items: SpaceProductItemModelDto[];
}

View File

@ -1,3 +1 @@
export * from './subspace-model.dto';
export * from './subspace-product-item-model.dto';
export * from './subspace-product-model.dto';

View File

@ -1,15 +0,0 @@
import { IsString, IsNotEmpty } from 'class-validator';
export class SubspaceProductItemModelDto {
@IsString()
@IsNotEmpty()
uuid: string;
@IsString()
@IsNotEmpty()
tag: string;
@IsString()
@IsNotEmpty()
subspaceProductModelUuid: string;
}

View File

@ -1,23 +0,0 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty, IsNumber, IsString } from 'class-validator';
import { SubspaceProductItemModelDto } from './subspace-product-item-model.dto';
export class SubpaceProductModelDto {
@IsString()
@IsNotEmpty()
uuid: string;
@IsNumber()
@IsNotEmpty()
productCount: number;
@IsString()
@IsNotEmpty()
productUuid: string;
@ApiProperty({
description: 'List of individual items with specific names for the product',
type: [SubspaceProductItemModelDto],
})
items: SubspaceProductItemModelDto[];
}

View File

@ -0,0 +1,21 @@
import { IsNotEmpty, IsString } from 'class-validator';
export class TagModelDto {
@IsString()
@IsNotEmpty()
public uuid: string;
@IsString()
@IsNotEmpty()
public name: string;
@IsString()
@IsNotEmpty()
public productUuid: string;
@IsString()
spaceModelUuid: string;
@IsString()
subspaceModelUuid: string;
}

View File

@ -1,4 +1,3 @@
export * from './space-model.entity';
export * from './space-product-item-model.entity';
export * from './space-product-model.entity';
export * from './subspace-model';
export * from './tag-model.entity';

View File

@ -9,9 +9,9 @@ import {
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
import { SpaceModelDto } from '../dtos';
import { SubspaceModelEntity } from './subspace-model';
import { SpaceProductModelEntity } from './space-product-model.entity';
import { ProjectEntity } from '../../project/entities';
import { SpaceEntity } from '../../space/entities';
import { TagModel } from './tag-model.entity';
@Entity({ name: 'space-model' })
@Unique(['modelName', 'project'])
@ -28,6 +28,12 @@ export class SpaceModelEntity extends AbstractEntity<SpaceModelDto> {
})
public modelName: string;
@Column({
nullable: false,
default: false,
})
public disabled: boolean;
@ManyToOne(() => ProjectEntity, (project) => project.spaceModels, {
nullable: false,
onDelete: 'CASCADE',
@ -45,17 +51,16 @@ export class SpaceModelEntity extends AbstractEntity<SpaceModelDto> {
)
public subspaceModels: SubspaceModelEntity[];
@OneToMany(
() => SpaceProductModelEntity,
(productModel) => productModel.spaceModel,
{
nullable: true,
},
)
public spaceProductModels: SpaceProductModelEntity[];
@OneToMany(() => SpaceEntity, (space) => space.spaceModel, {
cascade: true,
})
public spaces: SpaceEntity[];
@OneToMany(() => TagModel, (tag) => tag.spaceModel)
tags: TagModel[];
constructor(partial: Partial<SpaceModelEntity>) {
super();
Object.assign(this, partial);
}
}

View File

@ -1,29 +0,0 @@
import { Entity, Column, ManyToOne, OneToMany } from 'typeorm';
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
import { SpaceProductItemModelDto } from '../dtos';
import { SpaceProductModelEntity } from './space-product-model.entity';
import { SpaceProductItemEntity } from '../../space/entities';
@Entity({ name: 'space-product-item-model' })
export class SpaceProductItemModelEntity extends AbstractEntity<SpaceProductItemModelDto> {
@Column({
nullable: false,
})
public tag: string;
@ManyToOne(
() => SpaceProductModelEntity,
(spaceProductModel) => spaceProductModel.items,
{
nullable: false,
},
)
public spaceProductModel: SpaceProductModelEntity;
@OneToMany(
() => SpaceProductItemEntity,
(spaceProductItem) => spaceProductItem.spaceProductItemModel,
{ cascade: true },
)
public items: SpaceProductItemEntity[];
}

View File

@ -1,50 +0,0 @@
import { Entity, Column, ManyToOne, OneToMany } from 'typeorm';
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
import { ProductEntity } from '../../product/entities';
import { SpaceModelEntity } from './space-model.entity';
import { SpaceProductItemModelEntity } from './space-product-item-model.entity';
import { SpaceProductModelDto } from '../dtos';
import { SpaceProductEntity } from '../../space/entities';
@Entity({ name: 'space-product-model' })
export class SpaceProductModelEntity extends AbstractEntity<SpaceProductModelDto> {
@Column({
nullable: false,
type: 'int',
})
productCount: number;
@ManyToOne(
() => SpaceModelEntity,
(spaceModel) => spaceModel.spaceProductModels,
{
nullable: false,
onDelete: 'CASCADE',
},
)
public spaceModel: SpaceModelEntity;
@ManyToOne(() => ProductEntity, (product) => product.spaceProductModels, {
nullable: false,
onDelete: 'CASCADE',
})
public product: ProductEntity;
@OneToMany(
() => SpaceProductItemModelEntity,
(item) => item.spaceProductModel,
{
cascade: true,
},
)
public items: SpaceProductItemModelEntity[];
@OneToMany(
() => SpaceProductEntity,
(spaceProduct) => spaceProduct.spaceProductModel,
{
cascade: true,
},
)
public spaceProducts: SpaceProductEntity[];
}

View File

@ -1,3 +1,2 @@
export * from './subspace-model.entity';
export * from './subspace-product-item-model.entity';
export * from './subspace-product-model.entity';

View File

@ -3,7 +3,7 @@ import { Column, Entity, ManyToOne, OneToMany, Unique } from 'typeorm';
import { SubSpaceModelDto } from '../../dtos';
import { SpaceModelEntity } from '../space-model.entity';
import { SubspaceEntity } from '@app/common/modules/space/entities';
import { SubspaceProductModelEntity } from './subspace-product-model.entity';
import { TagModel } from '../tag-model.entity';
@Entity({ name: 'subspace-model' })
@Unique(['subspaceName', 'spaceModel'])
@ -30,17 +30,17 @@ export class SubspaceModelEntity extends AbstractEntity<SubSpaceModelDto> {
)
public spaceModel: SpaceModelEntity;
@OneToMany(() => SubspaceEntity, (space) => space.subSpaceModel, {
@OneToMany(() => SubspaceEntity, (subspace) => subspace.subSpaceModel, {
cascade: true,
})
public spaces: SubspaceEntity[];
public subspaceModel: SubspaceEntity[];
@OneToMany(
() => SubspaceProductModelEntity,
(productModel) => productModel.subspaceModel,
{
nullable: true,
},
)
public productModels: SubspaceProductModelEntity[];
@Column({
nullable: false,
default: false,
})
public disabled: boolean;
@OneToMany(() => TagModel, (tag) => tag.subspaceModel)
tags: TagModel[];
}

View File

@ -1,27 +0,0 @@
import { AbstractEntity } from '@app/common/modules/abstract/entities/abstract.entity';
import { Entity, Column, ManyToOne, OneToMany } from 'typeorm';
import { SubspaceProductItemModelDto } from '../../dtos';
import { SubspaceProductModelEntity } from './subspace-product-model.entity';
import { SubspaceProductItemEntity } from '@app/common/modules/space/entities';
@Entity({ name: 'subspace-product-item-model' })
export class SubspaceProductItemModelEntity extends AbstractEntity<SubspaceProductItemModelDto> {
@Column({
nullable: false,
})
public tag: string;
@ManyToOne(
() => SubspaceProductModelEntity,
(productModel) => productModel.itemModels,
{
nullable: false,
},
)
public subspaceProductModel: SubspaceProductModelEntity;
@OneToMany(() => SubspaceProductItemEntity, (item) => item.subspaceProduct, {
nullable: true,
})
items: SubspaceProductItemEntity[];
}

View File

@ -1,44 +0,0 @@
import { Entity, Column, ManyToOne, OneToMany } from 'typeorm';
import { SubpaceProductModelDto } from '../../dtos';
import { AbstractEntity } from '@app/common/modules/abstract/entities/abstract.entity';
import { SubspaceModelEntity } from './subspace-model.entity';
import { ProductEntity } from '@app/common/modules/product/entities';
import { SubspaceProductEntity } from '@app/common/modules/space/entities';
import { SubspaceProductItemModelEntity } from './subspace-product-item-model.entity';
@Entity({ name: 'subspace-product-model' })
export class SubspaceProductModelEntity extends AbstractEntity<SubpaceProductModelDto> {
@Column({
nullable: false,
type: 'int',
})
productCount: number;
@ManyToOne(
() => SubspaceModelEntity,
(spaceModel) => spaceModel.productModels,
{
nullable: false,
},
)
public subspaceModel: SubspaceModelEntity;
@ManyToOne(() => ProductEntity, (product) => product.subpaceProductModels, {
nullable: false,
})
public product: ProductEntity;
@OneToMany(() => SubspaceProductEntity, (product) => product.model, {
nullable: true,
})
public subspaceProducts: SubspaceProductEntity[];
@OneToMany(
() => SubspaceProductItemModelEntity,
(product) => product.subspaceProductModel,
{
nullable: true,
},
)
public itemModels: SubspaceProductItemModelEntity[];
}

View File

@ -0,0 +1,46 @@
import {
Column,
Entity,
JoinColumn,
ManyToOne,
OneToMany,
Unique,
} from 'typeorm';
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
import { TagModelDto } from '../dtos/tag-model.dto';
import { SpaceModelEntity } from './space-model.entity';
import { SubspaceModelEntity } from './subspace-model';
import { ProductEntity } from '../../product/entities';
import { TagEntity } from '../../space/entities/tag.entity';
@Entity({ name: 'tag_model' })
@Unique(['tag', 'product', 'spaceModel', 'subspaceModel'])
export class TagModel extends AbstractEntity<TagModelDto> {
@Column({ type: 'varchar', length: 255 })
tag: string;
@ManyToOne(() => ProductEntity, (product) => product.tagModels, {
nullable: false,
})
@JoinColumn({ name: 'product_id' })
product: ProductEntity;
@ManyToOne(() => SpaceModelEntity, (space) => space.tags, { nullable: true })
@JoinColumn({ name: 'space_model_id' })
spaceModel: SpaceModelEntity;
@ManyToOne(() => SubspaceModelEntity, (subspace) => subspace.tags, {
nullable: true,
})
@JoinColumn({ name: 'subspace_model_id' })
subspaceModel: SubspaceModelEntity;
@Column({
nullable: false,
default: false,
})
public disabled: boolean;
@OneToMany(() => TagEntity, (tag) => tag.model)
tags: TagEntity[];
}

View File

@ -1,13 +1,6 @@
import { DataSource, Repository } from 'typeorm';
import { Injectable } from '@nestjs/common';
import {
SpaceModelEntity,
SpaceProductItemModelEntity,
SpaceProductModelEntity,
SubspaceModelEntity,
SubspaceProductItemModelEntity,
SubspaceProductModelEntity,
} from '../entities';
import { SpaceModelEntity, SubspaceModelEntity, TagModel } from '../entities';
@Injectable()
export class SpaceModelRepository extends Repository<SpaceModelEntity> {
@ -23,28 +16,8 @@ export class SubspaceModelRepository extends Repository<SubspaceModelEntity> {
}
@Injectable()
export class SubspaceProductModelRepository extends Repository<SubspaceProductModelEntity> {
export class TagModelRepository extends Repository<TagModel> {
constructor(private dataSource: DataSource) {
super(SubspaceProductModelEntity, dataSource.createEntityManager());
}
}
@Injectable()
export class SubspaceProductItemModelRepository extends Repository<SubspaceProductItemModelEntity> {
constructor(private dataSource: DataSource) {
super(SubspaceProductItemModelEntity, dataSource.createEntityManager());
}
}
@Injectable()
export class SpaceProductModelRepository extends Repository<SpaceProductModelEntity> {
constructor(private dataSource: DataSource) {
super(SpaceProductModelEntity, dataSource.createEntityManager());
}
}
@Injectable()
export class SpaceProductItemModelRepository extends Repository<SpaceProductItemModelEntity> {
constructor(private dataSource: DataSource) {
super(SpaceProductItemModelEntity, dataSource.createEntityManager());
super(TagModel, dataSource.createEntityManager());
}
}

View File

@ -1,10 +1,5 @@
import { TypeOrmModule } from '@nestjs/typeorm';
import {
SpaceModelEntity,
SpaceProductItemModelEntity,
SpaceProductModelEntity,
SubspaceModelEntity,
} from './entities';
import { SpaceModelEntity, SubspaceModelEntity, TagModel } from './entities';
import { Module } from '@nestjs/common';
@Module({
@ -12,12 +7,7 @@ import { Module } from '@nestjs/common';
exports: [],
controllers: [],
imports: [
TypeOrmModule.forFeature([
SpaceModelEntity,
SubspaceModelEntity,
SpaceProductModelEntity,
SpaceProductItemModelEntity,
]),
TypeOrmModule.forFeature([SpaceModelEntity, SubspaceModelEntity, TagModel]),
],
})
export class SpaceModelRepositoryModule {}

View File

@ -1,4 +1,3 @@
export * from './space.dto';
export * from './subspace.dto';
export * from './space-product-item.dto';
export * from './space-product.dto';
export * from './tag.dto';

View File

@ -1,15 +0,0 @@
import { IsString, IsNotEmpty } from 'class-validator';
export class SpaceProductItemDto {
@IsString()
@IsNotEmpty()
uuid: string;
@IsString()
@IsNotEmpty()
tag: string;
@IsString()
@IsNotEmpty()
spaceProductUuid: string;
}

View File

@ -1,23 +0,0 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsString, IsNotEmpty, IsNumber } from 'class-validator';
import { SpaceProductItemDto } from './space-product-item.dto';
export class SpaceProductModelDto {
@IsString()
@IsNotEmpty()
uuid: string;
@IsNumber()
@IsNotEmpty()
productCount: number;
@IsString()
@IsNotEmpty()
productUuid: string;
@ApiProperty({
description: 'List of individual items with specific names for the product',
type: [SpaceProductItemDto],
})
items: SpaceProductItemDto[];
}

View File

@ -0,0 +1,21 @@
import { IsNotEmpty, IsString } from 'class-validator';
export class TagDto {
@IsString()
@IsNotEmpty()
public uuid: string;
@IsString()
@IsNotEmpty()
public name: string;
@IsString()
@IsNotEmpty()
public productUuid: string;
@IsString()
spaceUuid: string;
@IsString()
subspaceUuid: string;
}

View File

@ -1,5 +1,4 @@
export * from './space.entity';
export * from './subspace';
export * from './space-product.entity';
export * from './space-product-item.entity';
export * from './space-link.entity';
export * from './tag.entity';

View File

@ -13,6 +13,12 @@ export class SpaceLinkEntity extends AbstractEntity {
@JoinColumn({ name: 'end_space_id' })
public endSpace: SpaceEntity;
@Column({
nullable: false,
default: false,
})
public disabled: boolean;
@Column({
nullable: false,
enum: Object.values(Direction),

View File

@ -1,27 +0,0 @@
import { Column, Entity, ManyToOne } from 'typeorm';
import { SpaceProductEntity } from './space-product.entity';
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
import { SpaceProductItemDto } from '../dtos';
import { SpaceProductItemModelEntity } from '../../space-model';
@Entity({ name: 'space-product-item' })
export class SpaceProductItemEntity extends AbstractEntity<SpaceProductItemDto> {
@Column({
nullable: false,
})
public tag: string;
@ManyToOne(() => SpaceProductEntity, (spaceProduct) => spaceProduct.items, {
nullable: false,
})
public spaceProduct: SpaceProductEntity;
@ManyToOne(
() => SpaceProductItemModelEntity,
(spaceProductItemModel) => spaceProductItemModel.items,
{
nullable: true,
},
)
public spaceProductItemModel?: SpaceProductItemModelEntity;
}

View File

@ -1,49 +0,0 @@
import { Column, Entity, ManyToOne, JoinColumn, OneToMany } from 'typeorm';
import { SpaceEntity } from './space.entity';
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
import { ProductEntity } from '../../product/entities';
import { SpaceProductItemEntity } from './space-product-item.entity';
import { SpaceProductModelEntity } from '../../space-model';
@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: true,
type: 'int',
})
productCount: number;
@OneToMany(() => SpaceProductItemEntity, (item) => item.spaceProduct, {
cascade: true,
})
public items: SpaceProductItemEntity[];
@ManyToOne(
() => SpaceProductModelEntity,
(spaceProductModel) => spaceProductModel.spaceProducts,
{
nullable: true,
},
)
@JoinColumn({ name: 'space_product_model_uuid' })
public spaceProductModel?: SpaceProductModelEntity;
constructor(partial: Partial<SpaceProductEntity>) {
super();
Object.assign(this, partial);
}
}

View File

@ -13,10 +13,10 @@ import { DeviceEntity } from '../../device/entities';
import { CommunityEntity } from '../../community/entities';
import { SubspaceEntity } from './subspace';
import { SpaceLinkEntity } from './space-link.entity';
import { SpaceProductEntity } from './space-product.entity';
import { SceneEntity } from '../../scene/entities';
import { SpaceModelEntity } from '../../space-model';
import { InviteUserSpaceEntity } from '../../Invite-user/entities';
import { TagEntity } from './tag.entity';
@Entity({ name: 'space' })
@Unique(['invitationCode'])
@ -60,6 +60,12 @@ export class SpaceEntity extends AbstractEntity<SpaceDto> {
@OneToMany(() => UserSpaceEntity, (userSpace) => userSpace.space)
userSpaces: UserSpaceEntity[];
@Column({
nullable: false,
default: false,
})
public disabled: boolean;
@OneToMany(() => SubspaceEntity, (subspace) => subspace.space, {
nullable: true,
})
@ -94,14 +100,12 @@ export class SpaceEntity extends AbstractEntity<SpaceDto> {
})
public icon: string;
@OneToMany(() => SpaceProductEntity, (spaceProduct) => spaceProduct.space)
spaceProducts: SpaceProductEntity[];
@OneToMany(() => SceneEntity, (scene) => scene.space)
scenes: SceneEntity[];
@ManyToOne(() => SpaceModelEntity, { nullable: true })
@JoinColumn({ name: 'space_model_uuid' })
@ManyToOne(() => SpaceModelEntity, (spaceModel) => spaceModel.spaces, {
nullable: true,
})
spaceModel?: SpaceModelEntity;
@OneToMany(
@ -109,6 +113,10 @@ export class SpaceEntity extends AbstractEntity<SpaceDto> {
(inviteUserSpace) => inviteUserSpace.space,
)
invitedUsers: InviteUserSpaceEntity[];
@OneToMany(() => TagEntity, (tag) => tag.space)
tags: TagEntity[];
constructor(partial: Partial<SpaceEntity>) {
super();
Object.assign(this, partial);

View File

@ -1,3 +1 @@
export * from './subspace.entity';
export * from './subspace-product.entity';
export * from './subspace-product-item.entity';

View File

@ -1,32 +0,0 @@
import { AbstractEntity } from '@app/common/modules/abstract/entities/abstract.entity';
import { SpaceProductItemDto } from '../../dtos';
import { Column, Entity, ManyToOne } from 'typeorm';
import { SubspaceProductEntity } from './subspace-product.entity';
import { SubspaceProductItemModelEntity } from '@app/common/modules/space-model';
@Entity({ name: 'subspace-product-item' })
export class SubspaceProductItemEntity extends AbstractEntity<SpaceProductItemDto> {
@Column({
nullable: false,
})
public tag: string;
@ManyToOne(
() => SubspaceProductEntity,
(subspaceProduct) => subspaceProduct.items,
{
nullable: false,
},
)
public subspaceProduct: SubspaceProductEntity;
@ManyToOne(() => SubspaceProductItemModelEntity, (model) => model.items, {
nullable: true,
})
model: SubspaceProductItemModelEntity;
constructor(partial: Partial<SubspaceProductItemEntity>) {
super();
Object.assign(this, partial);
}
}

View File

@ -1,47 +0,0 @@
import { ProductEntity } from '@app/common/modules/product/entities';
import { Column, Entity, ManyToOne, OneToMany } from 'typeorm';
import { SubspaceEntity } from './subspace.entity';
import { AbstractEntity } from '@app/common/modules/abstract/entities/abstract.entity';
import { SubspaceProductItemEntity } from './subspace-product-item.entity';
import { SubspaceProductModelEntity } from '@app/common/modules/space-model';
import { SpaceProductModelDto } from '../../dtos';
@Entity({ name: 'subspace-product' })
export class SubspaceProductEntity extends AbstractEntity<SpaceProductModelDto> {
@Column({
type: 'uuid',
default: () => 'gen_random_uuid()',
nullable: false,
})
public uuid: string;
@Column({
nullable: false,
type: 'int',
})
productCount: number;
@ManyToOne(() => SubspaceEntity, (subspace) => subspace.subspaceProducts, {
nullable: false,
})
public subspace: SubspaceEntity;
@ManyToOne(() => ProductEntity, (product) => product.subpaceProductModels, {
nullable: false,
})
public product: ProductEntity;
@OneToMany(() => SubspaceProductItemEntity, (item) => item.subspaceProduct, {
nullable: true,
})
public items: SubspaceProductItemEntity[];
@ManyToOne(
() => SubspaceProductModelEntity,
(model) => model.subspaceProducts,
{
nullable: true,
},
)
model: SubspaceProductModelEntity;
}

View File

@ -4,7 +4,7 @@ import { SubspaceModelEntity } from '@app/common/modules/space-model';
import { Column, Entity, JoinColumn, ManyToOne, OneToMany } from 'typeorm';
import { SubspaceDto } from '../../dtos';
import { SpaceEntity } from '../space.entity';
import { SubspaceProductEntity } from './subspace-product.entity';
import { TagEntity } from '../tag.entity';
@Entity({ name: 'subspace' })
export class SubspaceEntity extends AbstractEntity<SubspaceDto> {
@ -22,28 +22,28 @@ export class SubspaceEntity extends AbstractEntity<SubspaceDto> {
@ManyToOne(() => SpaceEntity, (space) => space.subspaces, {
nullable: false,
onDelete: 'CASCADE',
})
@JoinColumn({ name: 'space_uuid' })
space: SpaceEntity;
@Column({
nullable: false,
default: false,
})
public disabled: boolean;
@OneToMany(() => DeviceEntity, (device) => device.subspace, {
nullable: true,
})
devices: DeviceEntity[];
@ManyToOne(() => SubspaceModelEntity, { nullable: true })
@JoinColumn({ name: 'subspace_model_uuid' })
@ManyToOne(() => SubspaceModelEntity, (subspace) => subspace.subspaceModel, {
nullable: true,
})
subSpaceModel?: SubspaceModelEntity;
@OneToMany(
() => SubspaceProductEntity,
(subspaceProduct) => subspaceProduct.subspace,
{
nullable: true,
},
)
public subspaceProducts: SubspaceProductEntity[];
@OneToMany(() => TagEntity, (tag) => tag.subspace)
tags: TagEntity[];
constructor(partial: Partial<SubspaceEntity>) {
super();

View File

@ -0,0 +1,52 @@
import {
Entity,
Column,
ManyToOne,
JoinColumn,
Unique,
OneToOne,
} from 'typeorm';
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
import { ProductEntity } from '../../product/entities';
import { TagDto } from '../dtos';
import { TagModel } from '../../space-model/entities/tag-model.entity';
import { SpaceEntity } from './space.entity';
import { SubspaceEntity } from './subspace';
import { DeviceEntity } from '../../device/entities';
@Entity({ name: 'tag' })
@Unique(['tag', 'product', 'space', 'subspace'])
export class TagEntity extends AbstractEntity<TagDto> {
@Column({ type: 'varchar', length: 255 })
tag: string;
@ManyToOne(() => TagModel, (model) => model.tags, {
nullable: true,
})
model: TagModel;
@ManyToOne(() => ProductEntity, (product) => product.tags, {
nullable: false,
})
product: ProductEntity;
@ManyToOne(() => SpaceEntity, (space) => space.tags, { nullable: true })
space: SpaceEntity;
@ManyToOne(() => SubspaceEntity, (subspace) => subspace.tags, {
nullable: true,
})
@JoinColumn({ name: 'subspace_id' })
subspace: SubspaceEntity;
@Column({
nullable: false,
default: false,
})
public disabled: boolean;
@OneToOne(() => DeviceEntity, (device) => device.tag, {
nullable: true,
})
device: DeviceEntity;
}

View File

@ -1,11 +1,6 @@
import { DataSource, Repository } from 'typeorm';
import { Injectable } from '@nestjs/common';
import { SpaceProductEntity } from '../entities/space-product.entity';
import {
SpaceEntity,
SpaceLinkEntity,
SpaceProductItemEntity,
} from '../entities';
import { SpaceEntity, SpaceLinkEntity, TagEntity } from '../entities';
@Injectable()
export class SpaceRepository extends Repository<SpaceEntity> {
@ -20,16 +15,10 @@ 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());
}
}
@Injectable()
export class SpaceProductItemRepository extends Repository<SpaceProductItemEntity> {
export class TagRepository extends Repository<TagEntity> {
constructor(private dataSource: DataSource) {
super(SpaceProductItemEntity, dataSource.createEntityManager());
super(TagEntity, dataSource.createEntityManager());
}
}

View File

@ -1,9 +1,5 @@
import { DataSource, Repository } from 'typeorm';
import {
SubspaceEntity,
SubspaceProductEntity,
SubspaceProductItemEntity,
} from '../entities';
import { SubspaceEntity } from '../entities';
import { Injectable } from '@nestjs/common';
@Injectable()
@ -12,17 +8,3 @@ export class SubspaceRepository extends Repository<SubspaceEntity> {
super(SubspaceEntity, dataSource.createEntityManager());
}
}
@Injectable()
export class SubspaceProductRepository extends Repository<SubspaceProductEntity> {
constructor(private dataSource: DataSource) {
super(SubspaceProductEntity, dataSource.createEntityManager());
}
}
@Injectable()
export class SubspaceProductItemRepository extends Repository<SubspaceProductItemEntity> {
constructor(private dataSource: DataSource) {
super(SubspaceProductItemEntity, dataSource.createEntityManager());
}
}

View File

@ -1,17 +1,11 @@
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { SpaceEntity, SubspaceEntity, SubspaceProductEntity } from './entities';
import { SpaceEntity, SubspaceEntity, TagEntity } from './entities';
@Module({
providers: [],
exports: [],
controllers: [],
imports: [
TypeOrmModule.forFeature([
SpaceEntity,
SubspaceEntity,
SubspaceProductEntity,
]),
],
imports: [TypeOrmModule.forFeature([SpaceEntity, SubspaceEntity, TagEntity])],
})
export class SpaceRepositoryModule {}

View File

@ -41,7 +41,7 @@ import {
import { convertKeysToCamelCase } from '@app/common/helper/camelCaseConverter';
import { DeviceRepository } from '@app/common/modules/device/repositories';
import { PermissionType } from '@app/common/constants/permission-type.enum';
import { In } from 'typeorm';
import { In, QueryRunner } from 'typeorm';
import { ProductType } from '@app/common/constants/product-type.enum';
import { SpaceRepository } from '@app/common/modules/space/repositories';
import { DeviceStatusFirebaseService } from '@app/common/firebase/devices-status/services/devices-status.service';
@ -59,6 +59,7 @@ import { DeviceSceneParamDto } from '../dtos/device.param.dto';
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
import { DeleteSceneFromSceneDeviceDto } from '../dtos/delete.device.dto';
import { DeviceEntity } from '@app/common/modules/device/entities';
@Injectable()
export class DeviceService {
@ -83,6 +84,7 @@ export class DeviceService {
secretKey,
});
}
async getDeviceByDeviceUuid(
deviceUuid: string,
withProductDevice: boolean = true,
@ -98,6 +100,29 @@ export class DeviceService {
relations,
});
}
async deleteDevice(
devices: DeviceEntity[],
orphanSpace: SpaceEntity,
queryRunner: QueryRunner,
): Promise<void> {
try {
const deviceIds = devices.map((device) => device.uuid);
if (deviceIds.length > 0) {
await queryRunner.manager
.createQueryBuilder()
.update(DeviceEntity)
.set({ spaceDevice: orphanSpace })
.whereInIds(deviceIds)
.execute();
}
} catch (error) {
throw new HttpException(
`Failed to update devices to orphan space: ${error.message}`,
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
async getDeviceByDeviceTuyaUuid(deviceTuyaUuid: string) {
return await this.deviceRepository.findOne({

View File

@ -495,12 +495,16 @@ export class SceneService {
const space = await this.getSpaceByUuid(scene.space.uuid);
await this.delete(scene.sceneTuyaUuid, space.spaceTuyaUuid);
await this.sceneDeviceRepository.delete({
scene: { uuid: sceneUuid },
});
await this.sceneRepository.delete({
uuid: sceneUuid,
});
await this.sceneDeviceRepository.update(
{ uuid: sceneUuid },
{ disabled: true },
);
await this.sceneRepository.update(
{
uuid: sceneUuid,
},
{ disabled: true },
);
return new SuccessResponseDto({
message: `Scene with ID ${sceneUuid} deleted successfully`,
});

View File

@ -0,0 +1,2 @@
export * from './propogate-subspace-update-command';
export * from './propagate-space-model-deletion.command';

View File

@ -0,0 +1,9 @@
import { SpaceModelEntity } from '@app/common/modules/space-model';
export class PropogateDeleteSpaceModelCommand {
constructor(
public readonly param: {
spaceModel: SpaceModelEntity;
},
) {}
}

View File

@ -0,0 +1,12 @@
import { ICommand } from '@nestjs/cqrs';
import { SpaceModelEntity } from '@app/common/modules/space-model';
import { ModifyspaceModelPayload } from '../interfaces';
export class PropogateUpdateSpaceModelCommand implements ICommand {
constructor(
public readonly param: {
spaceModel: SpaceModelEntity;
modifiedSpaceModels: ModifyspaceModelPayload;
},
) {}
}

View File

@ -1 +0,0 @@
export * from './services';

View File

@ -1,60 +0,0 @@
import { HttpException, HttpStatus } from '@nestjs/common';
import { QueryRunner } from 'typeorm';
import { SpaceModelEntity } from '@app/common/modules/space-model';
import { CreateProductItemModelDto } from 'src/space-model/dtos';
export abstract class BaseProductItemService {
async validateTags(
itemModelDtos: CreateProductItemModelDto[],
queryRunner: QueryRunner,
spaceModel: SpaceModelEntity,
): Promise<void> {
const incomingTags = new Set(
itemModelDtos.map((item) => item.tag).filter(Boolean),
);
const duplicateTags = itemModelDtos
.map((item) => item.tag)
.filter((tag, index, array) => array.indexOf(tag) !== index);
if (duplicateTags.length > 0) {
throw new HttpException(
`Duplicate tags found in the request: ${[...new Set(duplicateTags)].join(', ')}`,
HttpStatus.BAD_REQUEST,
);
}
const existingTagsQuery = `
SELECT DISTINCT tag
FROM (
SELECT spi.tag
FROM "subspace-product-item-model" spi
INNER JOIN "subspace-product-model" spm ON spi.subspace_product_model_uuid = spm.uuid
INNER JOIN "subspace-model" sm ON spm.subspace_model_uuid = sm.uuid
WHERE sm.space_model_uuid = $1
UNION
SELECT spi.tag
FROM "space-product-item-model" spi
INNER JOIN "space-product-model" spm ON spi.space_product_model_uuid = spm.uuid
WHERE spm.space_model_uuid = $1
) AS combined_tags;
`;
const existingTags = await queryRunner.manager.query(existingTagsQuery, [
spaceModel.uuid,
]);
const existingTagsSet = new Set(
existingTags.map((row: { tag: string }) => row.tag),
);
const conflictingTags = [...incomingTags].filter((tag) =>
existingTagsSet.has(tag),
);
if (conflictingTags.length > 0) {
throw new HttpException(
`Tags already exist in the model: ${conflictingTags.join(', ')}`,
HttpStatus.CONFLICT,
);
}
}
}

View File

@ -1,24 +0,0 @@
import { HttpException, HttpStatus } from '@nestjs/common';
import { CreateSpaceProductModelDto } from 'src/space-model/dtos';
import { ProductService } from '../../../product/services';
export abstract class BaseProductModelService {
constructor(private readonly productService: ProductService) {}
protected async validateProductCount(
dto: CreateSpaceProductModelDto,
): Promise<void> {
const productItemCount = dto.items.length;
if (dto.productCount !== productItemCount) {
throw new HttpException(
`Product count (${dto.productCount}) does not match the number of items (${productItemCount}) for product ID ${dto.productUuid}.`,
HttpStatus.BAD_REQUEST,
);
}
}
protected async getProduct(productId: string) {
const product = await this.productService.findOne(productId);
return product.data;
}
}

View File

@ -1,2 +0,0 @@
export * from './base-product-item-model.service';
export * from './base-product-model.service';

View File

@ -2,15 +2,21 @@ import { ControllerRoute } from '@app/common/constants/controller-route';
import {
Body,
Controller,
Delete,
Get,
Param,
Post,
Put,
Query,
UseGuards,
} from '@nestjs/common';
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
import { SpaceModelService } from '../services';
import { CreateSpaceModelDto } from '../dtos';
import {
CreateSpaceModelDto,
SpaceModelParam,
UpdateSpaceModelDto,
} from '../dtos';
import { ProjectParam } from 'src/community/dtos';
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
import { PermissionsGuard } from 'src/guards/permissions.guard';
@ -27,7 +33,7 @@ export class SpaceModelController {
@ApiBearerAuth()
@UseGuards(PermissionsGuard)
@Permissions('SPACE_MODULE_ADD')
@Permissions('SPACE_MODEL_ADD')
@ApiOperation({
summary: ControllerRoute.SPACE_MODEL.ACTIONS.CREATE_SPACE_MODEL_SUMMARY,
description:
@ -59,4 +65,33 @@ export class SpaceModelController {
): Promise<BaseResponseDto> {
return await this.spaceModelService.list(projectParam, query);
}
@ApiBearerAuth()
@UseGuards(PermissionsGuard)
@Permissions('SPACE_MODEL_UPDATE')
@ApiOperation({
summary: ControllerRoute.SPACE_MODEL.ACTIONS.UPDATE_SPACE_MODEL_SUMMARY,
description:
ControllerRoute.SPACE_MODEL.ACTIONS.UPDATE_SPACE_MODEL_DESCRIPTION,
})
@Put(':spaceModelUuid')
async update(
@Body() dto: UpdateSpaceModelDto,
@Param() param: SpaceModelParam,
): Promise<BaseResponseDto> {
return await this.spaceModelService.update(dto, param);
}
@ApiBearerAuth()
@UseGuards(PermissionsGuard)
@Permissions('SPACE_MODEL_DELETE')
@ApiOperation({
summary: ControllerRoute.SPACE_MODEL.ACTIONS.DELETE_SPACE_MODEL_SUMMARY,
description:
ControllerRoute.SPACE_MODEL.ACTIONS.DELETE_SPACE_MODEL_DESCRIPTION,
})
@Delete(':spaceModelUuid')
async delete(@Param() param: SpaceModelParam): Promise<BaseResponseDto> {
return await this.spaceModelService.deleteSpaceModel(param);
}
}

View File

@ -1,8 +1,8 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty, IsString, IsArray, ValidateNested } from 'class-validator';
import { Type } from 'class-transformer';
import { CreateSubspaceModelDto } from './create-subspace-model.dto';
import { CreateSpaceProductModelDto } from './create-space-product-model.dto';
import { CreateSubspaceModelDto } from './subspaces-model-dtos/create-subspace-model.dto';
import { CreateTagModelDto } from './tag-model-dtos/create-tag-model.dto';
export class CreateSpaceModelDto {
@ApiProperty({
@ -23,11 +23,11 @@ export class CreateSpaceModelDto {
subspaceModels?: CreateSubspaceModelDto[];
@ApiProperty({
description: 'List of products included in the model',
type: [CreateSpaceProductModelDto],
description: 'List of tags associated with the space model',
type: [CreateTagModelDto],
})
@IsArray()
@ValidateNested({ each: true })
@Type(() => CreateSpaceProductModelDto)
spaceProductModels?: CreateSpaceProductModelDto[];
@Type(() => CreateTagModelDto)
tags?: CreateTagModelDto[];
}

View File

@ -1,12 +0,0 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty, IsString } from 'class-validator';
export class CreateProductItemModelDto {
@ApiProperty({
description: 'Specific name for the product item',
example: 'Light 1',
})
@IsNotEmpty()
@IsString()
tag: string;
}

View File

@ -1,39 +0,0 @@
import { ApiProperty } from '@nestjs/swagger';
import {
IsNotEmpty,
IsString,
IsArray,
ValidateNested,
IsInt,
ArrayNotEmpty,
} from 'class-validator';
import { Type } from 'class-transformer';
import { CreateProductItemModelDto } from './create-space-product-item-model.dto';
export class CreateSpaceProductModelDto {
@ApiProperty({
description: 'ID of the product associated with the model',
example: 'product-uuid',
})
@IsNotEmpty()
@IsString()
productUuid: string;
@ApiProperty({
description: 'Number of products in the model',
example: 3,
})
@IsNotEmpty()
@IsInt()
productCount: number;
@ApiProperty({
description: 'Specific names for each product item',
type: [CreateProductItemModelDto],
})
@IsArray()
@ArrayNotEmpty()
@ValidateNested({ each: true })
@Type(() => CreateProductItemModelDto)
items: CreateProductItemModelDto[];
}

View File

@ -1,30 +0,0 @@
import { ApiProperty } from '@nestjs/swagger';
import {
IsArray,
IsNotEmpty,
IsOptional,
IsString,
ValidateNested,
} from 'class-validator';
import { CreateSpaceProductModelDto } from './create-space-product-model.dto';
import { Type } from 'class-transformer';
export class CreateSubspaceModelDto {
@ApiProperty({
description: 'Name of the subspace',
example: 'Living Room',
})
@IsNotEmpty()
@IsString()
subspaceName: string;
@ApiProperty({
description: 'List of products included in the model',
type: [CreateSpaceProductModelDto],
})
@IsArray()
@IsOptional()
@ValidateNested({ each: true })
@Type(() => CreateSpaceProductModelDto)
spaceProductModels?: CreateSpaceProductModelDto[];
}

View File

@ -1,5 +1,6 @@
export * from './create-space-model.dto';
export * from './create-space-product-item-model.dto';
export * from './create-space-product-model.dto';
export * from './create-subspace-model.dto';
export * from './project-param.dto';
export * from './update-space-model.dto';
export * from './space-model-param';
export * from './subspaces-model-dtos';
export * from './tag-model-dtos';

View File

@ -1,7 +1,7 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsUUID } from 'class-validator';
export class projectParam {
export class ProjectParam {
@ApiProperty({
description: 'UUID of the Project',
example: 'd290f1ee-6c54-4b01-90e6-d701748f0851',

View File

@ -0,0 +1,11 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsUUID } from 'class-validator';
import { ProjectParam } from './project-param.dto';
export class SpaceModelParam extends ProjectParam {
@ApiProperty({
description: 'UUID of the Space',
example: 'd290f1ee-6c54-4b01-90e6-d701748f0851',
})
@IsUUID()
spaceModelUuid: string;
}

View File

@ -0,0 +1,23 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsArray, IsNotEmpty, IsString, ValidateNested } from 'class-validator';
import { CreateTagModelDto } from '../tag-model-dtos/create-tag-model.dto';
import { Type } from 'class-transformer';
export class CreateSubspaceModelDto {
@ApiProperty({
description: 'Name of the subspace',
example: 'Living Room',
})
@IsNotEmpty()
@IsString()
subspaceName: string;
@ApiProperty({
description: 'List of tag models associated with the subspace',
type: [CreateTagModelDto],
})
@IsArray()
@ValidateNested({ each: true })
@Type(() => CreateTagModelDto)
tags?: CreateTagModelDto[];
}

View File

@ -0,0 +1,12 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty, IsString } from 'class-validator';
export class DeleteSubspaceModelDto {
@ApiProperty({
description: 'Uuid of the subspace model need to be deleted',
example: '982fc3a3-64dc-4afb-a5b5-65ee8fef0424',
})
@IsNotEmpty()
@IsString()
subspaceUuid: string;
}

View File

@ -0,0 +1,4 @@
export * from './delete-subspace-model.dto';
export * from './create-subspace-model.dto';
export * from './update-subspace-model.dto';
export * from './modify-subspace-model.dto';

View File

@ -0,0 +1,47 @@
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { Type } from 'class-transformer';
import {
IsString,
IsOptional,
IsArray,
ValidateNested,
IsEnum,
} from 'class-validator';
import { ModifyTagModelDto } from '../tag-model-dtos';
import { ModifyAction } from '@app/common/constants/modify-action.enum';
export class ModifySubspaceModelDto {
@ApiProperty({
description: 'Action to perform: add, update, or delete',
example: ModifyAction.ADD,
})
@IsEnum(ModifyAction)
action: ModifyAction;
@ApiPropertyOptional({
description: 'UUID of the subspace (required for update/delete)',
example: '123e4567-e89b-12d3-a456-426614174000',
})
@IsOptional()
@IsString()
uuid?: string;
@ApiPropertyOptional({
description: 'Name of the subspace (required for add/update)',
example: 'Living Room',
})
@IsOptional()
@IsString()
subspaceName?: string;
@ApiPropertyOptional({
description:
'List of tag modifications (add/update/delete) for the subspace',
type: [ModifyTagModelDto],
})
@IsOptional()
@IsArray()
@ValidateNested({ each: true })
@Type(() => ModifyTagModelDto)
tags?: ModifyTagModelDto[];
}

View File

@ -0,0 +1,16 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty, IsString } from 'class-validator';
export class UpdateSubspaceModelDto {
@ApiProperty({
description: 'Name of the subspace',
example: 'Living Room',
})
@IsNotEmpty()
@IsString()
subspaceName?: string;
@IsNotEmpty()
@IsString()
subspaceUuid: string;
}

View File

@ -0,0 +1,20 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty, IsString } from 'class-validator';
export class CreateTagModelDto {
@ApiProperty({
description: 'Tag models associated with the space or subspace models',
example: 'Temperature Control',
})
@IsNotEmpty()
@IsString()
tag: string;
@ApiProperty({
description: 'ID of the product associated with the tag',
example: '123e4567-e89b-12d3-a456-426614174000',
})
@IsNotEmpty()
@IsString()
productUuid: string;
}

View File

@ -0,0 +1,3 @@
export * from './create-tag-model.dto';
export * from './update-tag-model.dto';
export * from './modify-tag-model.dto';

View File

@ -0,0 +1,37 @@
import { ModifyAction } from '@app/common/constants/modify-action.enum';
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { IsString, IsOptional, IsEnum } from 'class-validator';
export class ModifyTagModelDto {
@ApiProperty({
description: 'Action to perform: add, update, or delete',
example: ModifyAction.ADD,
})
@IsEnum(ModifyAction)
action: ModifyAction;
@ApiPropertyOptional({
description: 'UUID of the tag model (required for update/delete)',
example: '123e4567-e89b-12d3-a456-426614174000',
})
@IsOptional()
@IsString()
uuid?: string;
@ApiPropertyOptional({
description: 'Name of the tag model (required for add/update)',
example: 'Temperature Sensor',
})
@IsOptional()
@IsString()
tag?: string;
@ApiPropertyOptional({
description:
'UUID of the product associated with the tag (required for add)',
example: 'c789a91e-549a-4753-9006-02f89e8170e0',
})
@IsOptional()
@IsString()
productUuid?: string;
}

View File

@ -0,0 +1,21 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty, IsOptional, IsString, IsUUID } from 'class-validator';
export class UpdateTagModelDto {
@ApiProperty({
description: 'UUID of the tag to be updated',
example: '123e4567-e89b-12d3-a456-426614174000',
})
@IsNotEmpty()
@IsUUID()
uuid: string;
@ApiProperty({
description: 'Updated name of the tag',
example: 'Updated Tag Name',
required: false,
})
@IsOptional()
@IsString()
tag?: string;
}

View File

@ -0,0 +1,76 @@
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { IsArray, IsOptional, IsString, ValidateNested } from 'class-validator';
import { CreateSubspaceModelDto } from './subspaces-model-dtos/create-subspace-model.dto';
import { Type } from 'class-transformer';
import {
DeleteSubspaceModelDto,
ModifySubspaceModelDto,
UpdateSubspaceModelDto,
} from './subspaces-model-dtos';
import { ModifyTagModelDto } from './tag-model-dtos';
export class ModifySubspacesModelDto {
@ApiProperty({
description: 'List of subspaces to add',
type: [CreateSubspaceModelDto],
required: false,
})
@IsOptional()
@IsArray()
@ValidateNested({ each: true })
@Type(() => CreateSubspaceModelDto)
add?: CreateSubspaceModelDto[];
@ApiProperty({
description: 'List of subspaces to add',
type: [CreateSubspaceModelDto],
required: false,
})
@IsOptional()
@IsArray()
@ValidateNested({ each: true })
@Type(() => UpdateSubspaceModelDto)
update?: UpdateSubspaceModelDto[];
@ApiProperty({
description: 'List of subspaces to delete',
type: [DeleteSubspaceModelDto],
required: false,
})
@IsOptional()
@IsArray()
@ValidateNested({ each: true })
@Type(() => DeleteSubspaceModelDto)
delete?: DeleteSubspaceModelDto[];
}
export class UpdateSpaceModelDto {
@ApiProperty({
description: 'Updated name of the space model',
example: 'New Space Model Name',
})
@IsOptional()
@IsString()
modelName?: string;
@ApiPropertyOptional({
description: 'List of subspace modifications (add/update/delete)',
type: [ModifySubspaceModelDto],
})
@IsOptional()
@IsArray()
@ValidateNested({ each: true })
@Type(() => ModifySubspaceModelDto)
subspaceModels?: ModifySubspaceModelDto[];
@ApiPropertyOptional({
description:
'List of tag modifications (add/update/delete) for the space model',
type: [ModifyTagModelDto],
})
@IsOptional()
@IsArray()
@ValidateNested({ each: true })
@Type(() => ModifyTagModelDto)
tags?: ModifyTagModelDto[];
}

View File

@ -0,0 +1,2 @@
export * from './propate-subspace-handler';
export * from './propogate-space-model-deletion.handler';

View File

@ -0,0 +1,250 @@
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs';
import { PropogateUpdateSpaceModelCommand } from '../commands';
import { SpaceEntity, SpaceRepository } from '@app/common/modules/space';
import { SubspaceRepository } from '@app/common/modules/space/repositories/subspace.repository';
import {
SpaceModelEntity,
SubspaceModelEntity,
TagModel,
} from '@app/common/modules/space-model';
import { DataSource, QueryRunner } from 'typeorm';
import { SubSpaceService } from 'src/space/services';
import { TagService } from 'src/space/services/tag';
import { TagModelService } from '../services';
import { UpdatedSubspaceModelPayload } from '../interfaces';
import { ModifyAction } from '@app/common/constants/modify-action.enum';
import { ModifySubspaceDto } from 'src/space/dtos';
@CommandHandler(PropogateUpdateSpaceModelCommand)
export class PropogateUpdateSpaceModelHandler
implements ICommandHandler<PropogateUpdateSpaceModelCommand>
{
constructor(
private readonly spaceRepository: SpaceRepository,
private readonly subspaceRepository: SubspaceRepository,
private readonly dataSource: DataSource,
private readonly subSpaceService: SubSpaceService,
private readonly tagService: TagService,
private readonly tagModelService: TagModelService,
) {}
async execute(command: PropogateUpdateSpaceModelCommand): Promise<void> {
const { spaceModel, modifiedSpaceModels } = command.param;
const queryRunner = this.dataSource.createQueryRunner();
try {
await queryRunner.connect();
await queryRunner.startTransaction();
const spaces = await this.spaceRepository.find({
where: { spaceModel },
});
if (
modifiedSpaceModels.modifiedSubspaceModels.addedSubspaceModels.length >
0
) {
await this.addSubspaceModels(
modifiedSpaceModels.modifiedSubspaceModels.addedSubspaceModels,
spaces,
queryRunner,
);
} else if (
modifiedSpaceModels.modifiedSubspaceModels.updatedSubspaceModels
.length > 0
) {
await this.updateSubspaceModels(
modifiedSpaceModels.modifiedSubspaceModels.updatedSubspaceModels,
queryRunner,
);
}
if (
modifiedSpaceModels.modifiedSubspaceModels.deletedSubspaceModels?.length
) {
const dtos: ModifySubspaceDto[] =
modifiedSpaceModels.modifiedSubspaceModels.deletedSubspaceModels.map(
(model) => ({
action: ModifyAction.DELETE,
uuid: model,
}),
);
await this.subSpaceService.modifySubSpace(dtos, queryRunner);
}
if (modifiedSpaceModels.modifiedTags.added.length > 0) {
await this.createTags(
modifiedSpaceModels.modifiedTags.added,
queryRunner,
null,
spaceModel,
);
}
if (modifiedSpaceModels.modifiedTags.updated.length > 0) {
await this.updateTags(
modifiedSpaceModels.modifiedTags.updated,
queryRunner,
);
}
if (modifiedSpaceModels.modifiedTags.deleted.length > 0) {
await this.deleteTags(
modifiedSpaceModels.modifiedTags.deleted,
queryRunner,
);
}
await queryRunner.commitTransaction();
} catch (error) {
await queryRunner.rollbackTransaction();
throw error;
} finally {
await queryRunner.release();
}
}
async addSubspaceModels(
subspaceModels: SubspaceModelEntity[],
spaces: SpaceEntity[],
queryRunner: QueryRunner,
) {
for (const space of spaces) {
await this.subSpaceService.createSubSpaceFromModel(
subspaceModels,
space,
queryRunner,
);
}
}
async updateSubspaceModels(
subspaceModels: UpdatedSubspaceModelPayload[],
queryRunner: QueryRunner,
): Promise<void> {
const subspaceUpdatePromises = subspaceModels.map(async (model) => {
const {
updated: tagsToUpdate,
deleted: tagsToDelete,
added: tagsToAdd,
} = model.modifiedTags;
// Perform tag operations concurrently
await Promise.all([
tagsToUpdate?.length && this.updateTags(tagsToUpdate, queryRunner),
tagsToDelete?.length && this.deleteTags(tagsToDelete, queryRunner),
tagsToAdd?.length &&
this.createTags(
tagsToAdd,
queryRunner,
model.subspaceModelUuid,
null,
),
]);
// Update subspace names
const subspaces = await queryRunner.manager.find(
this.subspaceRepository.target,
{
where: {
subSpaceModel: {
uuid: model.subspaceModelUuid,
},
},
},
);
if (subspaces.length > 0) {
const updateSubspacePromises = subspaces.map((subspace) =>
queryRunner.manager.update(
this.subspaceRepository.target,
{ uuid: subspace.uuid },
{ subspaceName: model.subspaceName },
),
);
await Promise.all(updateSubspacePromises);
}
});
// Wait for all subspace model updates to complete
await Promise.all(subspaceUpdatePromises);
}
async updateTags(models: TagModel[], queryRunner: QueryRunner) {
if (!models?.length) return;
const updatePromises = models.map((model) =>
this.tagService.updateTagsFromModel(model, queryRunner),
);
await Promise.all(updatePromises);
}
async deleteTags(uuids: string[], queryRunner: QueryRunner) {
const deletePromises = uuids.map((uuid) =>
this.tagService.deleteTagFromModel(uuid, queryRunner),
);
await Promise.all(deletePromises);
}
async createTags(
models: TagModel[],
queryRunner: QueryRunner,
subspaceModelUuid?: string,
spaceModel?: SpaceModelEntity,
): Promise<void> {
if (!models.length) {
return;
}
if (subspaceModelUuid) {
await this.processSubspaces(subspaceModelUuid, models, queryRunner);
}
if (spaceModel) {
await this.processSpaces(spaceModel.uuid, models, queryRunner);
}
}
private async processSubspaces(
subspaceModelUuid: string,
models: TagModel[],
queryRunner: QueryRunner,
): Promise<void> {
const subspaces = await this.subspaceRepository.find({
where: {
subSpaceModel: {
uuid: subspaceModelUuid,
},
},
});
if (subspaces.length > 0) {
const subspacePromises = subspaces.map((subspace) =>
this.tagService.createTagsFromModel(
queryRunner,
models,
null,
subspace,
),
);
await Promise.all(subspacePromises);
}
}
private async processSpaces(
spaceModelUuid: string,
models: TagModel[],
queryRunner: QueryRunner,
): Promise<void> {
const spaces = await this.spaceRepository.find({
where: {
spaceModel: {
uuid: spaceModelUuid,
},
},
});
if (spaces.length > 0) {
const spacePromises = spaces.map((space) =>
this.tagService.createTagsFromModel(queryRunner, models, space, null),
);
await Promise.all(spacePromises);
}
}
}

View File

@ -0,0 +1,57 @@
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs';
import { Logger } from '@nestjs/common';
import { PropogateDeleteSpaceModelCommand } from '../commands';
import { SpaceRepository } from '@app/common/modules/space';
import { SpaceService } from '../../space/services/space.service';
import { DataSource } from 'typeorm';
@CommandHandler(PropogateDeleteSpaceModelCommand)
export class PropogateDeleteSpaceModelHandler
implements ICommandHandler<PropogateDeleteSpaceModelCommand>
{
private readonly logger = new Logger(PropogateDeleteSpaceModelHandler.name);
constructor(
private readonly spaceRepository: SpaceRepository,
private readonly spaceService: SpaceService,
private readonly dataSource: DataSource,
) {}
async execute(command: PropogateDeleteSpaceModelCommand): Promise<void> {
const { spaceModel } = command.param;
const queryRunner = this.dataSource.createQueryRunner();
try {
await queryRunner.connect();
await queryRunner.startTransaction();
const spaces = await this.spaceRepository.find({
where: {
spaceModel: {
uuid: spaceModel.uuid,
},
},
relations: ['subspaces', 'tags', 'subspaces.tags'],
});
for (const space of spaces) {
try {
await this.spaceService.unlinkSpaceFromModel(space, queryRunner);
} catch (innerError) {
this.logger.error(
`Error unlinking space model for space with UUID ${space.uuid}:`,
innerError.stack || innerError,
);
}
}
} catch (error) {
await queryRunner.rollbackTransaction();
this.logger.error(
'Error propagating delete space model:',
error.stack || error,
);
} finally {
await queryRunner.release();
}
}
}

View File

@ -0,0 +1,2 @@
export * from './update-subspace.interface';
export * from './modify-subspace.interface';

View File

@ -0,0 +1,24 @@
import { SubspaceModelEntity, TagModel } from '@app/common/modules/space-model';
export interface ModifyspaceModelPayload {
modifiedSubspaceModels?: ModifySubspaceModelPayload;
modifiedTags?: ModifiedTagsModelPayload;
}
export interface ModifySubspaceModelPayload {
addedSubspaceModels?: SubspaceModelEntity[];
updatedSubspaceModels?: UpdatedSubspaceModelPayload[];
deletedSubspaceModels?: string[];
}
export interface UpdatedSubspaceModelPayload {
subspaceName?: string;
modifiedTags?: ModifiedTagsModelPayload;
subspaceModelUuid: string;
}
export interface ModifiedTagsModelPayload {
added?: TagModel[];
updated?: TagModel[];
deleted?: string[];
}

View File

@ -0,0 +1,39 @@
import { SubspaceModelEntity } from '@app/common/modules/space-model';
export interface AddSubspaceModelInterface {
subspaceModel: SubspaceModelEntity;
}
export interface ProductModelInterface {}
export interface IModifySubspaceModelInterface {
spaceModelUuid: string;
new?: AddSubspaceModelInterface[];
update?: IUpdateSubspaceModelInterface[];
delete?: IDeletedSubsaceModelInterface[];
}
export interface IModifiedProductItemsModelsInterface {
delete?: string[];
}
export interface IUpdateSubspaceModelInterface {
subspaceName?: string;
uuid: string;
productModels?: IModifiedProductItemsModelsInterface[];
}
export interface IDeletedSubsaceModelInterface {
uuid: string;
}
export interface IUpdatedProductModelInterface {
productModelUuid: string;
productModifiedItemModel: IModifiedProductItemsModelsInterface;
}
export interface IModifiedProductModelsInterface {
add?: ProductModelInterface[];
update?: IUpdatedProductModelInterface[];
delete?: string[];
}

View File

@ -1,4 +1,3 @@
export * from './space-model.service';
export * from './space-product-item-model.service';
export * from './space-product-model.service';
export * from './subspace';
export * from './tag-model.service';

View File

@ -1,11 +1,12 @@
import { SpaceModelRepository } from '@app/common/modules/space-model';
import {
SpaceModelEntity,
SpaceModelRepository,
} from '@app/common/modules/space-model';
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { CreateSpaceModelDto } from '../dtos';
import { ProjectRepository } from '@app/common/modules/project/repositiories';
import { CreateSpaceModelDto, UpdateSpaceModelDto } from '../dtos';
import { ProjectParam } from 'src/community/dtos';
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
import { SubSpaceModelService } from './subspace/subspace-model.service';
import { SpaceProductModelService } from './space-product-model.service';
import { DataSource } from 'typeorm';
import {
TypeORMCustomModel,
@ -13,63 +14,69 @@ import {
} from '@app/common/models/typeOrmCustom.model';
import { PageResponse } from '@app/common/dto/pagination.response.dto';
import { SpaceModelDto } from '@app/common/modules/space-model/dtos';
import { SpaceModelParam } from '../dtos/space-model-param';
import { ProjectService } from 'src/project/services';
import { ProjectEntity } from '@app/common/modules/project/entities';
import { TagModelService } from './tag-model.service';
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
import { CommandBus } from '@nestjs/cqrs';
import { PropogateUpdateSpaceModelCommand } from '../commands';
import {
ModifiedTagsModelPayload,
ModifySubspaceModelPayload,
} from '../interfaces';
@Injectable()
export class SpaceModelService {
constructor(
private readonly dataSource: DataSource,
private readonly spaceModelRepository: SpaceModelRepository,
private readonly projectRepository: ProjectRepository,
private readonly projectService: ProjectService,
private readonly subSpaceModelService: SubSpaceModelService,
private readonly spaceProductModelService: SpaceProductModelService,
private readonly tagModelService: TagModelService,
private commandBus: CommandBus,
) {}
async createSpaceModel(
createSpaceModelDto: CreateSpaceModelDto,
params: ProjectParam,
) {
const { modelName, subspaceModels, spaceProductModels } =
createSpaceModelDto;
const project = await this.validateProject(params.projectUuid);
): Promise<BaseResponseDto> {
const { modelName, subspaceModels, tags } = createSpaceModelDto;
const queryRunner = this.dataSource.createQueryRunner();
await queryRunner.connect();
await queryRunner.startTransaction();
try {
const isModelExist = await this.validateName(
modelName,
params.projectUuid,
);
if (isModelExist) {
throw new HttpException(
`Model name "${modelName}" already exists in this project ${project.name}.`,
HttpStatus.CONFLICT,
);
}
const project = await this.validateProject(params.projectUuid);
await this.validateName(modelName, params.projectUuid);
const spaceModel = this.spaceModelRepository.create({
modelName,
project,
});
const savedSpaceModel = await queryRunner.manager.save(spaceModel);
if (subspaceModels) {
await this.subSpaceModelService.createSubSpaceModels(
subspaceModels,
savedSpaceModel,
if (subspaceModels?.length) {
savedSpaceModel.subspaceModels =
await this.subSpaceModelService.createSubSpaceModels(
subspaceModels,
savedSpaceModel,
queryRunner,
tags,
);
}
if (tags?.length) {
savedSpaceModel.tags = await this.tagModelService.createTags(
tags,
queryRunner,
savedSpaceModel,
null,
);
}
if (spaceProductModels) {
await this.spaceProductModelService.createSpaceProductModels(
spaceProductModels,
savedSpaceModel,
queryRunner,
);
}
await queryRunner.commitTransaction();
return new SuccessResponseDto({
@ -80,14 +87,16 @@ export class SpaceModelService {
} catch (error) {
await queryRunner.rollbackTransaction();
if (error instanceof HttpException) {
throw error;
}
const errorMessage =
error instanceof HttpException
? error.message
: 'An unexpected error occurred';
const statusCode =
error instanceof HttpException
? error.getStatus()
: HttpStatus.INTERNAL_SERVER_ERROR;
throw new HttpException(
error.message || `An unexpected error occurred`,
HttpStatus.INTERNAL_SERVER_ERROR,
);
throw new HttpException(errorMessage, statusCode);
} finally {
await queryRunner.release();
}
@ -104,8 +113,7 @@ export class SpaceModelService {
pageable.where = {
project: { uuid: param.projectUuid },
};
pageable.include =
'subspaceModels,spaceProductModels,subspaceModels.productModels,subspaceModels.productModels.itemModels,spaceProductModels.items';
pageable.include = 'subspaceModels,tags,subspaceModels.tags';
const customModel = TypeORMCustomModel(this.spaceModelRepository);
@ -125,26 +133,159 @@ export class SpaceModelService {
}
}
async validateProject(projectUuid: string) {
const project = await this.projectRepository.findOne({
where: {
uuid: projectUuid,
},
async validateProject(projectUuid: string): Promise<ProjectEntity> {
return await this.projectService.findOne(projectUuid);
}
async update(dto: UpdateSpaceModelDto, param: SpaceModelParam) {
await this.validateProject(param.projectUuid);
const spaceModel = await this.validateSpaceModel(param.spaceModelUuid);
const queryRunner = this.dataSource.createQueryRunner();
await queryRunner.connect();
await queryRunner.startTransaction();
let modifiedSubspaceModels: ModifySubspaceModelPayload = {};
let modifiedTagsModelPayload: ModifiedTagsModelPayload = {};
try {
const { modelName } = dto;
if (modelName) {
await this.validateName(modelName, param.projectUuid);
spaceModel.modelName = modelName;
await queryRunner.manager.save(spaceModel);
}
if (dto.subspaceModels) {
modifiedSubspaceModels =
await this.subSpaceModelService.modifySubSpaceModels(
dto.subspaceModels,
spaceModel,
queryRunner,
);
}
if (dto.tags) {
modifiedTagsModelPayload = await this.tagModelService.modifyTags(
dto.tags,
queryRunner,
spaceModel,
);
}
await queryRunner.commitTransaction();
await this.commandBus.execute(
new PropogateUpdateSpaceModelCommand({
spaceModel: spaceModel,
modifiedSpaceModels: {
modifiedSubspaceModels,
modifiedTags: modifiedTagsModelPayload,
},
}),
);
return new SuccessResponseDto({
message: 'SpaceModel updated successfully',
data: spaceModel,
});
} catch (error) {
await queryRunner.rollbackTransaction();
throw new HttpException(
error.message || 'Failed to update SpaceModel',
HttpStatus.INTERNAL_SERVER_ERROR,
);
} finally {
await queryRunner.release();
}
}
async deleteSpaceModel(param: SpaceModelParam): Promise<BaseResponseDto> {
const queryRunner = this.dataSource.createQueryRunner();
await queryRunner.connect();
await queryRunner.startTransaction();
try {
await this.validateProject(param.projectUuid);
const spaceModel = await this.validateSpaceModel(param.spaceModelUuid);
if (spaceModel.subspaceModels?.length) {
const deleteSubspaceDtos = spaceModel.subspaceModels.map(
(subspace) => ({
subspaceUuid: subspace.uuid,
}),
);
await this.subSpaceModelService.deleteSubspaceModels(
deleteSubspaceDtos,
queryRunner,
);
}
if (spaceModel.tags?.length) {
const deleteSpaceTagsDtos = spaceModel.tags.map((tag) => tag.uuid);
await this.tagModelService.deleteTags(deleteSpaceTagsDtos, queryRunner);
}
await queryRunner.manager.update(
this.spaceModelRepository.target,
{ uuid: param.spaceModelUuid },
{ disabled: true },
);
await queryRunner.commitTransaction();
return new SuccessResponseDto({
message: `SpaceModel with UUID ${param.spaceModelUuid} deleted successfully.`,
statusCode: HttpStatus.OK,
});
} catch (error) {
await queryRunner.rollbackTransaction();
const errorMessage =
error instanceof HttpException
? error.message
: 'An unexpected error occurred while deleting the SpaceModel';
const statusCode =
error instanceof HttpException
? error.getStatus()
: HttpStatus.INTERNAL_SERVER_ERROR;
throw new HttpException(errorMessage, statusCode);
} finally {
await queryRunner.release();
}
}
async validateName(modelName: string, projectUuid: string): Promise<void> {
const isModelExist = await this.spaceModelRepository.findOne({
where: { modelName, project: { uuid: projectUuid }, disabled: false },
});
if (!project) {
if (isModelExist) {
throw new HttpException(
`Project with uuid ${projectUuid} not found`,
HttpStatus.NOT_FOUND,
`Model name ${modelName} already exists in the project with UUID ${projectUuid}.`,
HttpStatus.CONFLICT,
);
}
return project;
}
async validateName(modelName: string, projectUuid: string): Promise<boolean> {
const isModelExist = await this.spaceModelRepository.exists({
where: { modelName, project: { uuid: projectUuid } },
async validateSpaceModel(uuid: string): Promise<SpaceModelEntity> {
const spaceModel = await this.spaceModelRepository.findOne({
where: {
uuid,
disabled: false,
},
relations: [
'subspaceModels',
'tags',
'tags.product',
'subspaceModels.tags',
'subspaceModels.tags.product',
],
});
return isModelExist;
if (!spaceModel) {
throw new HttpException('space model not found', HttpStatus.NOT_FOUND);
}
return spaceModel;
}
}

View File

@ -1,47 +0,0 @@
import {
SpaceModelEntity,
SpaceProductItemModelRepository,
SpaceProductModelEntity,
} from '@app/common/modules/space-model';
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { CreateProductItemModelDto } from '../dtos';
import { QueryRunner } from 'typeorm';
import { BaseProductItemService } from '../common';
@Injectable()
export class SpaceProductItemModelService extends BaseProductItemService {
constructor(
private readonly spaceProductItemRepository: SpaceProductItemModelRepository,
) {
super();
}
async createProdutItemModel(
itemModelDtos: CreateProductItemModelDto[],
spaceProductModel: SpaceProductModelEntity,
spaceModel: SpaceModelEntity,
queryRunner: QueryRunner,
) {
await this.validateTags(itemModelDtos, queryRunner, spaceModel);
try {
const productItems = itemModelDtos.map((dto) =>
queryRunner.manager.create(this.spaceProductItemRepository.target, {
tag: dto.tag,
spaceProductModel,
}),
);
await queryRunner.manager.save(productItems);
} catch (error) {
if (error instanceof HttpException) {
throw error;
}
throw new HttpException(
error.message ||
'An unexpected error occurred while creating product items.',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
}

View File

@ -1,67 +0,0 @@
import { QueryRunner } from 'typeorm';
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { CreateSpaceProductModelDto } from '../dtos';
import { SpaceProductItemModelService } from './space-product-item-model.service';
import { BaseProductModelService } from '../common';
import { ProductService } from 'src/product/services';
import {
SpaceModelEntity,
SpaceProductModelRepository,
} from '@app/common/modules/space-model';
@Injectable()
export class SpaceProductModelService extends BaseProductModelService {
constructor(
private readonly spaceProductModelRepository: SpaceProductModelRepository,
private readonly spaceProductItemModelService: SpaceProductItemModelService,
productService: ProductService,
) {
super(productService);
}
async createSpaceProductModels(
spaceProductModelDtos: CreateSpaceProductModelDto[],
spaceModel: SpaceModelEntity,
queryRunner: QueryRunner,
) {
try {
const productModels = await Promise.all(
spaceProductModelDtos.map(async (dto) => {
this.validateProductCount(dto);
const product = await this.getProduct(dto.productUuid);
return queryRunner.manager.create(
this.spaceProductModelRepository.target,
{
product,
productCount: dto.productCount,
spaceModel,
},
);
}),
);
const savedProductModels = await queryRunner.manager.save(productModels);
await Promise.all(
spaceProductModelDtos.map((dto, index) => {
const savedModel = savedProductModels[index];
return this.spaceProductItemModelService.createProdutItemModel(
dto.items,
savedModel, // Pass the saved model
spaceModel,
queryRunner,
);
}),
);
} catch (error) {
if (error instanceof HttpException) {
throw error;
}
throw new HttpException(
error.message ||
'An unexpected error occurred while creating product models.',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
}

View File

@ -1,3 +1 @@
export * from './subspace-model.service';
export * from './subspace-product-item-model.service';
export * from './subspace-product-model.service';

View File

@ -1,88 +1,302 @@
import {
SpaceModelEntity,
SubspaceModelEntity,
SubspaceModelRepository,
} from '@app/common/modules/space-model';
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { CreateSubspaceModelDto } from '../../dtos';
import { QueryRunner } from 'typeorm';
import { SubspaceProductModelService } from './subspace-product-model.service';
import { CreateSubspaceModelDto, CreateTagModelDto } from '../../dtos';
import { In, QueryRunner } from 'typeorm';
import {
IDeletedSubsaceModelInterface,
ModifySubspaceModelPayload,
UpdatedSubspaceModelPayload,
} from 'src/space-model/interfaces';
import {
DeleteSubspaceModelDto,
ModifySubspaceModelDto,
} from 'src/space-model/dtos/subspaces-model-dtos';
import { TagModelService } from '../tag-model.service';
import { ModifyAction } from '@app/common/constants/modify-action.enum';
@Injectable()
export class SubSpaceModelService {
constructor(
private readonly subspaceModelRepository: SubspaceModelRepository,
private readonly subSpaceProducetModelService: SubspaceProductModelService,
private readonly tagModelService: TagModelService,
) {}
async createSubSpaceModels(
subSpaceModelDtos: CreateSubspaceModelDto[],
spaceModel: SpaceModelEntity,
queryRunner: QueryRunner,
) {
this.validateInputDtos(subSpaceModelDtos);
otherTags?: CreateTagModelDto[],
): Promise<SubspaceModelEntity[]> {
this.validateInputDtos(subSpaceModelDtos, spaceModel);
try {
const subspaces = subSpaceModelDtos.map((subspaceDto) =>
queryRunner.manager.create(this.subspaceModelRepository.target, {
subspaceName: subspaceDto.subspaceName,
spaceModel: spaceModel,
}),
);
const subspaces = subSpaceModelDtos.map((subspaceDto) =>
queryRunner.manager.create(this.subspaceModelRepository.target, {
subspaceName: subspaceDto.subspaceName,
spaceModel,
}),
);
await queryRunner.manager.save(subspaces);
const savedSubspaces = await queryRunner.manager.save(subspaces);
await Promise.all(
subSpaceModelDtos.map((dto, index) => {
const subspaceModel = subspaces[index];
return this.subSpaceProducetModelService.createSubspaceProductModels(
dto.spaceProductModels,
spaceModel,
subspaceModel,
await Promise.all(
subSpaceModelDtos.map(async (dto, index) => {
const subspace = savedSubspaces[index];
const otherDtoTags = subSpaceModelDtos
.filter((_, i) => i !== index)
.flatMap((otherDto) => otherDto.tags || []);
if (dto.tags?.length) {
subspace.tags = await this.tagModelService.createTags(
dto.tags,
queryRunner,
null,
subspace,
[...(otherTags || []), ...otherDtoTags],
);
}),
}
}),
);
return savedSubspaces;
}
async deleteSubspaceModels(
deleteDtos: DeleteSubspaceModelDto[],
queryRunner: QueryRunner,
): Promise<IDeletedSubsaceModelInterface[]> {
const deleteResults: IDeletedSubsaceModelInterface[] = [];
for (const dto of deleteDtos) {
const subspaceModel = await this.findOne(dto.subspaceUuid);
await queryRunner.manager.update(
this.subspaceModelRepository.target,
{ uuid: dto.subspaceUuid },
{ disabled: true },
);
} catch (error) {
if (error instanceof HttpException) {
throw error;
if (subspaceModel.tags?.length) {
const modifyTagDtos = subspaceModel.tags.map((tag) => ({
uuid: tag.uuid,
action: ModifyAction.DELETE,
}));
await this.tagModelService.modifyTags(
modifyTagDtos,
queryRunner,
null,
subspaceModel,
);
}
throw new HttpException(
error.message || `An unexpected error occurred`,
HttpStatus.INTERNAL_SERVER_ERROR,
deleteResults.push({ uuid: dto.subspaceUuid });
}
return deleteResults;
}
async modifySubSpaceModels(
subspaceDtos: ModifySubspaceModelDto[],
spaceModel: SpaceModelEntity,
queryRunner: QueryRunner,
): Promise<ModifySubspaceModelPayload> {
const modifiedSubspaceModels: ModifySubspaceModelPayload = {};
for (const subspace of subspaceDtos) {
switch (subspace.action) {
case ModifyAction.ADD:
const subspaceModel = await this.handleAddAction(
subspace,
spaceModel,
queryRunner,
);
modifiedSubspaceModels.addedSubspaceModels.push(subspaceModel);
break;
case ModifyAction.UPDATE:
const updatedSubspaceModel = await this.handleUpdateAction(
subspace,
queryRunner,
);
modifiedSubspaceModels.updatedSubspaceModels.push(
updatedSubspaceModel,
);
break;
case ModifyAction.DELETE:
await this.handleDeleteAction(subspace, queryRunner);
modifiedSubspaceModels.deletedSubspaceModels.push(subspace.uuid);
break;
default:
throw new HttpException(
`Invalid action "${subspace.action}".`,
HttpStatus.BAD_REQUEST,
);
}
}
return modifiedSubspaceModels;
}
private async handleAddAction(
subspace: ModifySubspaceModelDto,
spaceModel: SpaceModelEntity,
queryRunner: QueryRunner,
): Promise<SubspaceModelEntity> {
const createTagDtos: CreateTagModelDto[] =
subspace.tags?.map((tag) => ({
tag: tag.tag,
productUuid: tag.productUuid,
})) || [];
const [createdSubspaceModel] = await this.createSubSpaceModels(
[
{
subspaceName: subspace.subspaceName,
tags: createTagDtos,
},
],
spaceModel,
queryRunner,
);
return createdSubspaceModel;
}
private async handleUpdateAction(
modifyDto: ModifySubspaceModelDto,
queryRunner: QueryRunner,
): Promise<UpdatedSubspaceModelPayload> {
const updatePayload: UpdatedSubspaceModelPayload = {
subspaceModelUuid: modifyDto.uuid,
};
const subspace = await this.findOne(modifyDto.uuid);
await this.updateSubspaceName(
queryRunner,
subspace,
modifyDto.subspaceName,
);
updatePayload.subspaceName = modifyDto.subspaceName;
if (modifyDto.tags?.length) {
updatePayload.modifiedTags = await this.tagModelService.modifyTags(
modifyDto.tags,
queryRunner,
null,
subspace,
);
}
return updatePayload;
}
private async handleDeleteAction(
subspace: ModifySubspaceModelDto,
queryRunner: QueryRunner,
) {
const subspaceModel = await this.findOne(subspace.uuid);
await queryRunner.manager.update(
this.subspaceModelRepository.target,
{ uuid: subspace.uuid },
{ disabled: true },
);
if (subspaceModel.tags?.length) {
const modifyTagDtos = subspaceModel.tags.map((tag) => ({
uuid: tag.uuid,
action: ModifyAction.DELETE,
}));
await this.tagModelService.modifyTags(
modifyTagDtos,
queryRunner,
null,
subspaceModel,
);
}
}
private validateInputDtos(subSpaceModelDtos: CreateSubspaceModelDto[]) {
private async findOne(subspaceUuid: string): Promise<SubspaceModelEntity> {
const subspace = await this.subspaceModelRepository.findOne({
where: { uuid: subspaceUuid },
relations: ['tags'],
});
if (!subspace) {
throw new HttpException(
`SubspaceModel with UUID ${subspaceUuid} not found.`,
HttpStatus.NOT_FOUND,
);
}
return subspace;
}
private validateInputDtos(
subSpaceModelDtos: CreateSubspaceModelDto[],
spaceModel: SpaceModelEntity,
): void {
if (subSpaceModelDtos.length === 0) {
throw new HttpException(
'Subspace models cannot be empty.',
HttpStatus.BAD_REQUEST,
);
}
const incomingNames = subSpaceModelDtos.map((dto) => dto.subspaceName);
this.validateName(incomingNames);
this.validateName(
subSpaceModelDtos.map((dto) => dto.subspaceName),
spaceModel,
);
}
private validateName(names: string[]) {
private async validateName(
names: string[],
spaceModel: SpaceModelEntity,
): Promise<void> {
const seenNames = new Set<string>();
const duplicateNames = new Set<string>();
for (const name of names) {
if (seenNames.has(name)) {
if (!seenNames.add(name)) {
duplicateNames.add(name);
} else {
seenNames.add(name);
}
}
if (duplicateNames.size > 0) {
throw new HttpException(
`Duplicate subspace names found in request: ${[...duplicateNames].join(', ')}`,
`Duplicate subspace model names found: ${[...duplicateNames].join(', ')}`,
HttpStatus.CONFLICT,
);
}
const existingNames = await this.subspaceModelRepository.find({
select: ['subspaceName'],
where: {
subspaceName: In([...seenNames]),
spaceModel: {
uuid: spaceModel.uuid,
},
},
});
if (existingNames.length > 0) {
const existingNamesList = existingNames
.map((e) => e.subspaceName)
.join(', ');
throw new HttpException(
`Subspace model names already exist in the space: ${existingNamesList}`,
HttpStatus.BAD_REQUEST,
);
}
}
private async updateSubspaceName(
queryRunner: QueryRunner,
subSpaceModel: SubspaceModelEntity,
subspaceName?: string,
): Promise<void> {
if (subspaceName) {
subSpaceModel.subspaceName = subspaceName;
await queryRunner.manager.save(subSpaceModel);
}
}
}

View File

@ -1,53 +0,0 @@
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { QueryRunner } from 'typeorm';
import {
SpaceModelEntity,
SubspaceProductItemModelRepository,
SubspaceProductModelEntity,
} from '@app/common/modules/space-model';
import { BaseProductItemService } from '../../common';
import { CreateProductItemModelDto } from '../../dtos';
@Injectable()
export class SubspaceProductItemModelService extends BaseProductItemService {
constructor(
private readonly subspaceProductItemRepository: SubspaceProductItemModelRepository,
) {
super();
}
async createProdutItemModel(
itemModelDtos: CreateProductItemModelDto[],
subspaceProductModel: SubspaceProductModelEntity,
spaceModel: SpaceModelEntity,
queryRunner: QueryRunner,
) {
if (!subspaceProductModel) {
throw new HttpException(
'The spaceProductModel parameter is required but was not provided.',
HttpStatus.BAD_REQUEST,
);
}
await this.validateTags(itemModelDtos, queryRunner, spaceModel);
try {
const productItems = itemModelDtos.map((dto) =>
queryRunner.manager.create(this.subspaceProductItemRepository.target, {
tag: dto.tag,
subspaceProductModel,
}),
);
await queryRunner.manager.save(productItems);
} catch (error) {
if (error instanceof HttpException) {
throw error;
}
throw new HttpException(
error.message ||
'An unexpected error occurred while creating product items.',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
}

View File

@ -1,69 +0,0 @@
import {
SpaceModelEntity,
SubspaceModelEntity,
SubspaceProductModelRepository,
} from '@app/common/modules/space-model';
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { SubspaceProductItemModelService } from './subspace-product-item-model.service';
import { CreateSpaceProductModelDto } from '../../dtos';
import { QueryRunner } from 'typeorm';
import { BaseProductModelService } from '../../common';
import { ProductService } from 'src/product/services';
@Injectable()
export class SubspaceProductModelService extends BaseProductModelService {
constructor(
private readonly subpaceProductModelRepository: SubspaceProductModelRepository,
productService: ProductService,
private readonly subspaceProductItemModelService: SubspaceProductItemModelService,
) {
super(productService);
}
async createSubspaceProductModels(
spaceProductModelDtos: CreateSpaceProductModelDto[],
spaceModel: SpaceModelEntity,
subspaceModel: SubspaceModelEntity,
queryRunner: QueryRunner,
) {
try {
const productModels = await Promise.all(
spaceProductModelDtos.map(async (dto) => {
this.validateProductCount(dto);
const product = await this.getProduct(dto.productUuid);
return queryRunner.manager.create(
this.subpaceProductModelRepository.target,
{
product,
productCount: dto.productCount,
subspaceModel,
},
);
}),
);
const savedProductModels = await queryRunner.manager.save(productModels);
await Promise.all(
spaceProductModelDtos.map((dto, index) => {
const savedModel = savedProductModels[index];
return this.subspaceProductItemModelService.createProdutItemModel(
dto.items,
savedModel, // Pass the saved model
spaceModel,
queryRunner,
);
}),
);
} catch (error) {
if (error instanceof HttpException) {
throw error;
}
throw new HttpException(
error.message ||
'An unexpected error occurred while creating product models.',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
}

View File

@ -0,0 +1,295 @@
import { Injectable, HttpException, HttpStatus } from '@nestjs/common';
import { QueryRunner } from 'typeorm';
import {
SpaceModelEntity,
TagModel,
} from '@app/common/modules/space-model/entities';
import { SubspaceModelEntity } from '@app/common/modules/space-model/entities';
import { TagModelRepository } from '@app/common/modules/space-model';
import { CreateTagModelDto, ModifyTagModelDto } from '../dtos';
import { ProductService } from 'src/product/services';
import { ModifyAction } from '@app/common/constants/modify-action.enum';
import { ModifiedTagsModelPayload } from '../interfaces';
@Injectable()
export class TagModelService {
constructor(
private readonly tagModelRepository: TagModelRepository,
private readonly productService: ProductService,
) {}
async createTags(
tags: CreateTagModelDto[],
queryRunner: QueryRunner,
spaceModel?: SpaceModelEntity,
subspaceModel?: SubspaceModelEntity,
additionalTags?: CreateTagModelDto[],
): Promise<TagModel[]> {
if (!tags.length) {
throw new HttpException('Tags cannot be empty.', HttpStatus.BAD_REQUEST);
}
const combinedTags = additionalTags ? [...tags, ...additionalTags] : tags;
const duplicateTags = this.findDuplicateTags(combinedTags);
if (duplicateTags.length > 0) {
throw new HttpException(
`Duplicate tags found for the same product: ${duplicateTags.join(', ')}`,
HttpStatus.BAD_REQUEST,
);
}
const tagEntities = await Promise.all(
tags.map(async (tagDto) =>
this.prepareTagEntity(tagDto, queryRunner, spaceModel, subspaceModel),
),
);
try {
return await queryRunner.manager.save(tagEntities);
} catch (error) {
if (error instanceof HttpException) {
throw error;
}
throw new HttpException(
'Failed to save tag models due to an unexpected error.',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
async updateTag(
tag: ModifyTagModelDto,
queryRunner: QueryRunner,
spaceModel?: SpaceModelEntity,
subspaceModel?: SubspaceModelEntity,
): Promise<TagModel> {
try {
const existingTag = await this.getTagByUuid(tag.uuid);
if (spaceModel) {
await this.checkTagReuse(tag.tag, existingTag.product.uuid, spaceModel);
} else {
await this.checkTagReuse(
tag.tag,
existingTag.product.uuid,
subspaceModel.spaceModel,
);
}
if (tag.tag) {
existingTag.tag = tag.tag;
}
return await queryRunner.manager.save(existingTag);
} catch (error) {
if (error instanceof HttpException) {
throw error;
}
throw new HttpException(
error.message || 'Failed to update tags',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
async deleteTags(tagUuids: string[], queryRunner: QueryRunner) {
try {
const deletePromises = tagUuids.map((id) =>
queryRunner.manager.softDelete(this.tagModelRepository.target, id),
);
await Promise.all(deletePromises);
return { message: 'Tags deleted successfully', tagUuids };
} catch (error) {
if (error instanceof HttpException) {
throw error;
}
throw new HttpException(
error.message || 'Failed to delete tags',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
private findDuplicateTags(tags: CreateTagModelDto[]): string[] {
const seen = new Map<string, boolean>();
const duplicates: string[] = [];
tags.forEach((tagDto) => {
const key = `${tagDto.productUuid}-${tagDto.tag}`;
if (seen.has(key)) {
duplicates.push(`${tagDto.tag} for Product: ${tagDto.productUuid}`);
} else {
seen.set(key, true);
}
});
return duplicates;
}
async modifyTags(
tags: ModifyTagModelDto[],
queryRunner: QueryRunner,
spaceModel?: SpaceModelEntity,
subspaceModel?: SubspaceModelEntity,
): Promise<ModifiedTagsModelPayload> {
const modifiedTagModels: ModifiedTagsModelPayload = {};
try {
for (const tag of tags) {
if (tag.action === ModifyAction.ADD) {
const createTagDto: CreateTagModelDto = {
tag: tag.tag as string,
productUuid: tag.productUuid as string,
};
const newModel = await this.createTags(
[createTagDto],
queryRunner,
spaceModel,
subspaceModel,
);
modifiedTagModels.added.push(...newModel);
} else if (tag.action === ModifyAction.UPDATE) {
const updatedModel = await this.updateTag(
tag,
queryRunner,
spaceModel,
subspaceModel,
);
modifiedTagModels.updated.push(updatedModel);
} else if (tag.action === ModifyAction.DELETE) {
await queryRunner.manager.update(
this.tagModelRepository.target,
{ uuid: tag.uuid },
{ disabled: true },
);
modifiedTagModels.deleted.push(tag.uuid);
} else {
throw new HttpException(
`Invalid action "${tag.action}" provided.`,
HttpStatus.BAD_REQUEST,
);
}
}
return modifiedTagModels;
} catch (error) {
if (error instanceof HttpException) {
throw error;
}
throw new HttpException(
`An error occurred while modifying tag models: ${error.message}`,
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
private async checkTagReuse(
tag: string,
productUuid: string,
spaceModel: SpaceModelEntity,
): Promise<void> {
const tagExists = await this.tagModelRepository.exists({
where: [
{
tag,
spaceModel: { uuid: spaceModel.uuid },
product: { uuid: productUuid },
disabled: false,
},
{
tag,
subspaceModel: { spaceModel: { uuid: spaceModel.uuid } },
product: { uuid: productUuid },
disabled: false,
},
],
});
if (tagExists) {
throw new HttpException(`Tag can't be reused`, HttpStatus.CONFLICT);
}
}
private async prepareTagEntity(
tagDto: CreateTagModelDto,
queryRunner: QueryRunner,
spaceModel?: SpaceModelEntity,
subspaceModel?: SubspaceModelEntity,
): Promise<TagModel> {
const product = await this.productService.findOne(tagDto.productUuid);
if (!product) {
throw new HttpException(
`Product with UUID ${tagDto.productUuid} not found.`,
HttpStatus.NOT_FOUND,
);
}
if (spaceModel) {
await this.checkTagReuse(tagDto.tag, tagDto.productUuid, spaceModel);
} else {
await this.checkTagReuse(
tagDto.tag,
tagDto.productUuid,
subspaceModel.spaceModel,
);
}
return queryRunner.manager.create(TagModel, {
tag: tagDto.tag,
product: product.data,
spaceModel,
subspaceModel,
});
}
async getTagByUuid(uuid: string): Promise<TagModel> {
const tag = await this.tagModelRepository.findOne({
where: { uuid },
relations: ['product'],
});
if (!tag) {
throw new HttpException(
`Tag model with ID ${uuid} not found.`,
HttpStatus.NOT_FOUND,
);
}
return tag;
}
async getTagByName(
tag: string,
subspaceUuid?: string,
spaceUuid?: string,
): Promise<TagModel> {
const queryConditions: any = { tag };
if (spaceUuid) {
queryConditions.spaceModel = { uuid: spaceUuid };
} else if (subspaceUuid) {
queryConditions.subspaceModel = { uuid: subspaceUuid };
} else {
throw new HttpException(
'Either spaceUuid or subspaceUuid must be provided.',
HttpStatus.BAD_REQUEST,
);
}
queryConditions.disabled = false;
const existingTag = await this.tagModelRepository.findOne({
where: queryConditions,
relations: ['product'],
});
if (!existingTag) {
throw new HttpException(
`Tag model with tag "${tag}" not found.`,
HttpStatus.NOT_FOUND,
);
}
return existingTag;
}
}

View File

@ -4,42 +4,73 @@ import { ConfigModule } from '@nestjs/config';
import { SpaceModelController } from './controllers';
import {
SpaceModelService,
SpaceProductItemModelService,
SpaceProductModelService,
SubSpaceModelService,
SubspaceProductItemModelService,
TagModelService,
} from './services';
import {
SpaceModelRepository,
SpaceProductItemModelRepository,
SpaceProductModelRepository,
SubspaceModelRepository,
SubspaceProductItemModelRepository,
SubspaceProductModelRepository,
TagModelRepository,
} from '@app/common/modules/space-model';
import { ProjectRepository } from '@app/common/modules/project/repositiories';
import { ProductRepository } from '@app/common/modules/product/repositories';
import { SubspaceProductModelService } from './services/subspace/subspace-product-model.service';
import {
PropogateDeleteSpaceModelHandler,
PropogateUpdateSpaceModelHandler,
} from './handlers';
import { CqrsModule } from '@nestjs/cqrs';
import {
SpaceLinkRepository,
SpaceRepository,
TagRepository,
} from '@app/common/modules/space';
import { SubspaceRepository } from '@app/common/modules/space/repositories/subspace.repository';
import {
SpaceLinkService,
SpaceService,
SubspaceDeviceService,
SubSpaceService,
ValidationService,
} from 'src/space/services';
import { TagService } from 'src/space/services/tag';
import { CommunityService } from 'src/community/services';
import { DeviceRepository } from '@app/common/modules/device/repositories';
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
import { CommunityRepository } from '@app/common/modules/community/repositories';
const CommandHandlers = [
PropogateUpdateSpaceModelHandler,
PropogateDeleteSpaceModelHandler,
];
@Module({
imports: [ConfigModule, SpaceRepositoryModule],
imports: [ConfigModule, SpaceRepositoryModule, CqrsModule],
controllers: [SpaceModelController],
providers: [
...CommandHandlers,
SpaceModelService,
SpaceService,
SpaceModelRepository,
SpaceRepository,
ProjectRepository,
SubSpaceModelService,
SpaceProductModelService,
SubspaceModelRepository,
SpaceProductModelRepository,
ProductRepository,
SpaceProductItemModelService,
SpaceProductItemModelRepository,
SubspaceProductItemModelService,
SubspaceProductItemModelRepository,
SubspaceProductModelService,
SubspaceProductModelRepository,
SubspaceRepository,
TagModelService,
TagModelRepository,
SubSpaceService,
ValidationService,
TagService,
SubspaceDeviceService,
CommunityService,
TagRepository,
DeviceRepository,
TuyaService,
CommunityRepository,
SpaceLinkService,
SpaceLinkRepository,
],
exports: [],
exports: [CqrsModule, SpaceModelService],
})
export class SpaceModelModule {}

View File

@ -0,0 +1,10 @@
import { SpaceEntity } from '@app/common/modules/space';
export class DisableSpaceCommand {
constructor(
public readonly param: {
spaceUuid: string;
orphanSpace: SpaceEntity;
},
) {}
}

View File

@ -0,0 +1 @@
export * from './disable-space.command';

View File

@ -67,8 +67,8 @@ export class SpaceController {
description: ControllerRoute.SPACE.ACTIONS.DELETE_SPACE_DESCRIPTION,
})
@Delete('/:spaceUuid')
async deleteSpace(@Param() params: GetSpaceParam): Promise<BaseResponseDto> {
return this.spaceService.delete(params);
async deleteSpace(@Param() params: GetSpaceParam) {
return await this.spaceService.delete(params);
}
@ApiBearerAuth()

View File

@ -65,7 +65,7 @@ export class SubSpaceController {
})
@Get(':subSpaceUuid')
async findOne(@Param() params: GetSubSpaceParam): Promise<BaseResponseDto> {
return this.subSpaceService.findOne(params);
return this.subSpaceService.getOne(params);
}
@ApiBearerAuth()

View File

@ -11,41 +11,7 @@ import {
ValidateNested,
} from 'class-validator';
import { AddSubspaceDto } from './subspace';
export class CreateSpaceProductItemDto {
@ApiProperty({
description: 'Specific name for the product item',
example: 'Light 1',
})
@IsNotEmpty()
@IsString()
tag: string;
}
export class ProductAssignmentDto {
@ApiProperty({
description: 'UUID of the product to be assigned',
example: 'prod-uuid-1234',
})
@IsNotEmpty()
productId: string;
@ApiProperty({
description: 'Number of items to assign for the product',
example: 3,
})
count: number;
@ApiProperty({
description: 'Specific names for each product item',
type: [CreateSpaceProductItemDto],
example: [{ tag: 'Light 1' }, { tag: 'Light 2' }, { tag: 'Light 3' }],
})
@IsArray()
@ValidateNested({ each: true })
@Type(() => CreateSpaceProductItemDto)
items: CreateSpaceProductItemDto[];
}
import { CreateTagDto } from './tag';
export class AddSpaceDto {
@ApiProperty({
@ -103,26 +69,22 @@ export class AddSpaceDto {
@IsOptional()
direction?: string;
@ApiProperty({
description: 'List of products assigned to this space',
type: [ProductAssignmentDto],
required: false,
})
@IsArray()
@ValidateNested({ each: true })
@IsOptional()
@Type(() => ProductAssignmentDto)
products?: ProductAssignmentDto[];
@ApiProperty({
description: 'List of subspaces included in the model',
type: [AddSubspaceDto],
})
@IsOptional()
@IsArray()
@ValidateNested({ each: true })
@Type(() => AddSubspaceDto)
subspaces?: AddSubspaceDto[];
@ApiProperty({
description: 'List of tags associated with the space model',
type: [CreateTagDto],
})
@ValidateNested({ each: true })
@Type(() => CreateTagDto)
tags?: CreateTagDto[];
}
export class AddUserSpaceDto {

View File

@ -5,3 +5,4 @@ export * from './user-space.param';
export * from './subspace';
export * from './project.param.dto';
export * from './update.space.dto';
export * from './tag';

View File

@ -1,13 +1,7 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsArray, IsNotEmpty, IsString, ValidateNested } from 'class-validator';
import { CreateTagDto } from '../tag';
import { Type } from 'class-transformer';
import {
IsArray,
IsNotEmpty,
IsOptional,
IsString,
ValidateNested,
} from 'class-validator';
import { ProductAssignmentDto } from '../add.space.dto';
export class AddSubspaceDto {
@ApiProperty({
@ -19,13 +13,11 @@ export class AddSubspaceDto {
subspaceName: string;
@ApiProperty({
description: 'List of products assigned to this space',
type: [ProductAssignmentDto],
required: false,
description: 'List of tags associated with the subspace',
type: [CreateTagDto],
})
@IsArray()
@ValidateNested({ each: true })
@IsOptional()
@Type(() => ProductAssignmentDto)
products?: ProductAssignmentDto[];
@Type(() => CreateTagDto)
tags?: CreateTagDto[];
}

View File

@ -0,0 +1,12 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty, IsString } from 'class-validator';
export class DeleteSubspaceDto {
@ApiProperty({
description: 'Uuid of the subspace model need to be deleted',
example: '982fc3a3-64dc-4afb-a5b5-65ee8fef0424',
})
@IsNotEmpty()
@IsString()
subspaceUuid: string;
}

View File

@ -1,3 +1,6 @@
export * from './add.subspace.dto';
export * from './get.subspace.param';
export * from './add.subspace-device.param';
export * from './update.subspace.dto';
export * from './delete.subspace.dto';
export * from './modify.subspace.dto';

View File

@ -0,0 +1,47 @@
import { ModifyAction } from '@app/common/constants/modify-action.enum';
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { Type } from 'class-transformer';
import {
IsEnum,
IsOptional,
IsString,
IsArray,
ValidateNested,
} from 'class-validator';
import { ModifyTagDto } from '../tag/modify-tag.dto';
export class ModifySubspaceDto {
@ApiProperty({
description: 'Action to perform: add, update, or delete',
example: ModifyAction.ADD,
})
@IsEnum(ModifyAction)
action: ModifyAction;
@ApiPropertyOptional({
description: 'UUID of the subspace (required for update/delete)',
example: '123e4567-e89b-12d3-a456-426614174000',
})
@IsOptional()
@IsString()
uuid?: string;
@ApiPropertyOptional({
description: 'Name of the subspace (required for add/update)',
example: 'Living Room',
})
@IsOptional()
@IsString()
subspaceName?: string;
@ApiPropertyOptional({
description:
'List of tag modifications (add/update/delete) for the subspace',
type: [ModifyTagDto],
})
@IsOptional()
@IsArray()
@ValidateNested({ each: true })
@Type(() => ModifyTagDto)
tags?: ModifyTagDto[];
}

View File

@ -0,0 +1,16 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty, IsString } from 'class-validator';
export class UpdateSubspaceDto {
@ApiProperty({
description: 'Name of the subspace',
example: 'Living Room',
})
@IsNotEmpty()
@IsString()
subspaceName?: string;
@IsNotEmpty()
@IsString()
subspaceUuid: string;
}

View File

@ -0,0 +1,20 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty, IsString } from 'class-validator';
export class CreateTagDto {
@ApiProperty({
description: 'Tag associated with the space or subspace',
example: 'Temperature Control',
})
@IsNotEmpty()
@IsString()
tag: string;
@ApiProperty({
description: 'ID of the product associated with the tag',
example: '123e4567-e89b-12d3-a456-426614174000',
})
@IsNotEmpty()
@IsString()
productUuid: string;
}

Some files were not shown because too many files have changed in this diff Show More