Refactor/space management (#404)

* refactor: reducing used queries on get communities (#385)

* refactor: fix create space logic (#394)

* Remove unique constraint on subspace and product in SubspaceProductAllocationEntity; update product relation to nullable in NewTagEntity

* refactor: fix create space logic

* device model updated to include the fixes and final columns

* updated space models to include suggested fixes, update final logic and column names

* task: removing old references of the old tag-product relation

* task: remove old use of tags

* task: remove old tag & tag model usage

* refactor: delete space

* task: remove unused functions

* fix lint rule
This commit is contained in:
ZaydSkaff
2025-06-11 13:15:21 +03:00
committed by GitHub
parent 4f5e1b23f6
commit 8503ee728d
64 changed files with 2314 additions and 4372 deletions

View File

@ -1,51 +1,30 @@
import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { TypeOrmModule } from '@nestjs/typeorm';
import { SnakeNamingStrategy } from './strategies';
import { UserEntity } from '../modules/user/entities/user.entity';
import { UserSessionEntity } from '../modules/session/entities/session.entity';
import { UserOtpEntity } from '../modules/user/entities';
import { ProductEntity } from '../modules/product/entities';
import { DeviceEntity } from '../modules/device/entities';
import { PermissionTypeEntity } from '../modules/permission/entities';
import { ProductEntity } from '../modules/product/entities';
import { UserSessionEntity } from '../modules/session/entities/session.entity';
import { UserOtpEntity } from '../modules/user/entities';
import { UserEntity } from '../modules/user/entities/user.entity';
import { SnakeNamingStrategy } from './strategies';
import { UserSpaceEntity } from '../modules/user/entities';
import { DeviceUserPermissionEntity } from '../modules/device/entities';
import { RoleTypeEntity } from '../modules/role-type/entities';
import { UserNotificationEntity } from '../modules/user/entities';
import { DeviceNotificationEntity } from '../modules/device/entities';
import { RegionEntity } from '../modules/region/entities';
import { TimeZoneEntity } from '../modules/timezone/entities';
import { VisitorPasswordEntity } from '../modules/visitor-password/entities';
import { TypeOrmWinstonLogger } from '@app/common/logger/services/typeorm.logger';
import { createLogger } from 'winston';
import { winstonLoggerOptions } from '../logger/services/winston.logger';
import { AqiSpaceDailyPollutantStatsEntity } from '../modules/aqi/entities';
import { AutomationEntity } from '../modules/automation/entities';
import { ClientEntity } from '../modules/client/entities';
import { CommunityEntity } from '../modules/community/entities';
import { DeviceStatusLogEntity } from '../modules/device-status-log/entities';
import { SceneEntity, SceneIconEntity } from '../modules/scene/entities';
import { SceneDeviceEntity } from '../modules/scene-device/entities';
import { ProjectEntity } from '../modules/project/entities';
import {
SpaceModelEntity,
SubspaceModelEntity,
TagModel,
SpaceModelProductAllocationEntity,
SubspaceModelProductAllocationEntity,
} from '../modules/space-model/entities';
DeviceNotificationEntity,
DeviceUserPermissionEntity,
} from '../modules/device/entities';
import {
InviteUserEntity,
InviteUserSpaceEntity,
} from '../modules/Invite-user/entities';
import { InviteSpaceEntity } from '../modules/space/entities/invite-space.entity';
import { AutomationEntity } from '../modules/automation/entities';
import { SpaceProductAllocationEntity } from '../modules/space/entities/space-product-allocation.entity';
import { NewTagEntity } from '../modules/tag/entities/tag.entity';
import { SpaceEntity } from '../modules/space/entities/space.entity';
import { SpaceLinkEntity } from '../modules/space/entities/space-link.entity';
import { SubspaceProductAllocationEntity } from '../modules/space/entities/subspace/subspace-product-allocation.entity';
import { SubspaceEntity } from '../modules/space/entities/subspace/subspace.entity';
import { TagEntity } from '../modules/space/entities/tag.entity';
import { ClientEntity } from '../modules/client/entities';
import { TypeOrmWinstonLogger } from '@app/common/logger/services/typeorm.logger';
import { createLogger } from 'winston';
import { winstonLoggerOptions } from '../logger/services/winston.logger';
import {
PowerClampDailyEntity,
PowerClampHourlyEntity,
@ -55,7 +34,30 @@ import {
PresenceSensorDailyDeviceEntity,
PresenceSensorDailySpaceEntity,
} from '../modules/presence-sensor/entities';
import { AqiSpaceDailyPollutantStatsEntity } from '../modules/aqi/entities';
import { ProjectEntity } from '../modules/project/entities';
import { RegionEntity } from '../modules/region/entities';
import { RoleTypeEntity } from '../modules/role-type/entities';
import { SceneDeviceEntity } from '../modules/scene-device/entities';
import { SceneEntity, SceneIconEntity } from '../modules/scene/entities';
import {
SpaceModelEntity,
SpaceModelProductAllocationEntity,
SubspaceModelEntity,
SubspaceModelProductAllocationEntity,
} from '../modules/space-model/entities';
import { InviteSpaceEntity } from '../modules/space/entities/invite-space.entity';
import { SpaceLinkEntity } from '../modules/space/entities/space-link.entity';
import { SpaceProductAllocationEntity } from '../modules/space/entities/space-product-allocation.entity';
import { SpaceEntity } from '../modules/space/entities/space.entity';
import { SubspaceProductAllocationEntity } from '../modules/space/entities/subspace/subspace-product-allocation.entity';
import { SubspaceEntity } from '../modules/space/entities/subspace/subspace.entity';
import { NewTagEntity } from '../modules/tag/entities/tag.entity';
import { TimeZoneEntity } from '../modules/timezone/entities';
import {
UserNotificationEntity,
UserSpaceEntity,
} from '../modules/user/entities';
import { VisitorPasswordEntity } from '../modules/visitor-password/entities';
@Module({
imports: [
TypeOrmModule.forRootAsync({
@ -86,7 +88,6 @@ import { AqiSpaceDailyPollutantStatsEntity } from '../modules/aqi/entities';
SpaceEntity,
SpaceLinkEntity,
SubspaceEntity,
TagEntity,
UserSpaceEntity,
DeviceUserPermissionEntity,
RoleTypeEntity,
@ -101,7 +102,6 @@ import { AqiSpaceDailyPollutantStatsEntity } from '../modules/aqi/entities';
SceneDeviceEntity,
SpaceModelEntity,
SubspaceModelEntity,
TagModel,
InviteUserEntity,
InviteUserSpaceEntity,
InviteSpaceEntity,

View File

@ -78,8 +78,8 @@ export class DeviceEntity extends AbstractEntity<DeviceDto> {
@OneToMany(() => SceneDeviceEntity, (sceneDevice) => sceneDevice.device, {})
sceneDevices: SceneDeviceEntity[];
@OneToMany(() => NewTagEntity, (tag) => tag.devices)
// @JoinTable({ name: 'device_tags' })
@ManyToOne(() => NewTagEntity, (tag) => tag.devices)
@JoinColumn({ name: 'tag_uuid' })
public tag: NewTagEntity;
@OneToMany(() => PowerClampHourlyEntity, (powerClamp) => powerClamp.device)
powerClampHourly: PowerClampHourlyEntity[];

View File

@ -1,10 +1,7 @@
import { Column, Entity, OneToMany } from 'typeorm';
import { ProductDto } from '../dtos';
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
import { DeviceEntity } from '../../device/entities';
import { TagModel } from '../../space-model';
import { TagEntity } from '../../space/entities/tag.entity';
import { NewTagEntity } from '../../tag/entities';
import { ProductDto } from '../dtos';
@Entity({ name: 'product' })
export class ProductEntity extends AbstractEntity<ProductDto> {
@Column({
@ -28,15 +25,6 @@ export class ProductEntity extends AbstractEntity<ProductDto> {
})
public prodType: string;
@OneToMany(() => NewTagEntity, (tag) => tag.product, { cascade: true })
public newTags: NewTagEntity[];
@OneToMany(() => TagModel, (tag) => tag.product)
tagModels: TagModel[];
@OneToMany(() => TagEntity, (tag) => tag.product)
tags: TagEntity[];
@OneToMany(
() => DeviceEntity,
(devicesProductEntity) => devicesProductEntity.productDevice,

View File

@ -1,21 +0,0 @@
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-product-allocation.entity';
export * from './space-model.entity';
export * from './subspace-model';
export * from './tag-model.entity';
export * from './space-model-product-allocation.entity';

View File

@ -1,18 +1,12 @@
import {
Entity,
Column,
ManyToOne,
ManyToMany,
JoinTable,
OneToMany,
} from 'typeorm';
import { SpaceModelEntity } from './space-model.entity';
import { NewTagEntity } from '../../tag/entities/tag.entity';
import { Column, Entity, ManyToOne, OneToMany, Unique } from 'typeorm';
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
import { ProductEntity } from '../../product/entities/product.entity';
import { SpaceProductAllocationEntity } from '../../space/entities/space-product-allocation.entity';
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
import { NewTagEntity } from '../../tag/entities/tag.entity';
import { SpaceModelEntity } from './space-model.entity';
@Entity({ name: 'space_model_product_allocation' })
@Unique(['spaceModel', 'product', 'tag'])
export class SpaceModelProductAllocationEntity extends AbstractEntity<SpaceModelProductAllocationEntity> {
@Column({
type: 'uuid',
@ -31,9 +25,8 @@ export class SpaceModelProductAllocationEntity extends AbstractEntity<SpaceModel
@ManyToOne(() => ProductEntity, { nullable: false, onDelete: 'CASCADE' })
public product: ProductEntity;
@ManyToMany(() => NewTagEntity, { cascade: true, onDelete: 'CASCADE' })
@JoinTable({ name: 'space_model_product_tags' })
public tags: NewTagEntity[];
@ManyToOne(() => NewTagEntity, { nullable: true, onDelete: 'CASCADE' })
public tag: NewTagEntity;
@OneToMany(
() => SpaceProductAllocationEntity,

View File

@ -1,11 +1,10 @@
import { Entity, Column, OneToMany, ManyToOne, JoinColumn } from 'typeorm';
import { Column, Entity, JoinColumn, ManyToOne, OneToMany } from 'typeorm';
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
import { SpaceModelDto } from '../dtos';
import { SubspaceModelEntity } from './subspace-model';
import { ProjectEntity } from '../../project/entities';
import { TagModel } from './tag-model.entity';
import { SpaceModelProductAllocationEntity } from './space-model-product-allocation.entity';
import { SpaceEntity } from '../../space/entities/space.entity';
import { SpaceModelDto } from '../dtos';
import { SpaceModelProductAllocationEntity } from './space-model-product-allocation.entity';
import { SubspaceModelEntity } from './subspace-model';
@Entity({ name: 'space-model' })
export class SpaceModelEntity extends AbstractEntity<SpaceModelDto> {
@ -49,9 +48,6 @@ export class SpaceModelEntity extends AbstractEntity<SpaceModelDto> {
})
public spaces: SpaceEntity[];
@OneToMany(() => TagModel, (tag) => tag.spaceModel)
tags: TagModel[];
@OneToMany(
() => SpaceModelProductAllocationEntity,
(allocation) => allocation.spaceModel,

View File

@ -1,11 +1,12 @@
import { Entity, Column, ManyToOne, ManyToMany, JoinTable } from 'typeorm';
import { SubspaceModelEntity } from './subspace-model.entity';
import { AbstractEntity } from '@app/common/modules/abstract/entities/abstract.entity';
import { ProductEntity } from '@app/common/modules/product/entities/product.entity';
import { NewTagEntity } from '@app/common/modules/tag/entities/tag.entity';
import { Column, Entity, ManyToOne, Unique } from 'typeorm';
import { SubspaceModelProductAllocationDto } from '../../dtos/subspace-model/subspace-model-product-allocation.dto';
import { AbstractEntity } from '@app/common/modules/abstract/entities/abstract.entity';
import { SubspaceModelEntity } from './subspace-model.entity';
@Entity({ name: 'subspace_model_product_allocation' })
@Unique(['subspaceModel', 'product', 'tag'])
export class SubspaceModelProductAllocationEntity extends AbstractEntity<SubspaceModelProductAllocationDto> {
@Column({
type: 'uuid',
@ -27,12 +28,8 @@ export class SubspaceModelProductAllocationEntity extends AbstractEntity<Subspac
@ManyToOne(() => ProductEntity, { nullable: false, onDelete: 'CASCADE' })
public product: ProductEntity;
@ManyToMany(() => NewTagEntity, (tag) => tag.subspaceModelAllocations, {
cascade: true,
onDelete: 'CASCADE',
})
@JoinTable({ name: 'subspace_model_product_tags' })
public tags: NewTagEntity[];
@ManyToOne(() => NewTagEntity, { nullable: true, onDelete: 'CASCADE' })
public tag: NewTagEntity;
constructor(partial: Partial<SubspaceModelProductAllocationEntity>) {
super();

View File

@ -1,10 +1,9 @@
import { AbstractEntity } from '@app/common/modules/abstract/entities/abstract.entity';
import { SubspaceEntity } from '@app/common/modules/space/entities/subspace/subspace.entity';
import { Column, Entity, ManyToOne, OneToMany } from 'typeorm';
import { SubSpaceModelDto } from '../../dtos';
import { SpaceModelEntity } from '../space-model.entity';
import { TagModel } from '../tag-model.entity';
import { SubspaceModelProductAllocationEntity } from './subspace-model-product-allocation.entity';
import { SubspaceEntity } from '@app/common/modules/space/entities/subspace/subspace.entity';
@Entity({ name: 'subspace-model' })
export class SubspaceModelEntity extends AbstractEntity<SubSpaceModelDto> {
@ -41,9 +40,6 @@ export class SubspaceModelEntity extends AbstractEntity<SubSpaceModelDto> {
})
public disabled: boolean;
@OneToMany(() => TagModel, (tag) => tag.subspaceModel)
tags: TagModel[];
@OneToMany(
() => SubspaceModelProductAllocationEntity,
(allocation) => allocation.subspaceModel,

View File

@ -1,38 +0,0 @@
import { Column, Entity, JoinColumn, ManyToOne, OneToMany } 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' })
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,11 +1,10 @@
import { DataSource, Repository } from 'typeorm';
import { Injectable } from '@nestjs/common';
import { DataSource, Repository } from 'typeorm';
import {
SpaceModelEntity,
SpaceModelProductAllocationEntity,
SubspaceModelEntity,
SubspaceModelProductAllocationEntity,
TagModel,
} from '../entities';
@Injectable()
@ -21,13 +20,6 @@ export class SubspaceModelRepository extends Repository<SubspaceModelEntity> {
}
}
@Injectable()
export class TagModelRepository extends Repository<TagModel> {
constructor(private dataSource: DataSource) {
super(TagModel, dataSource.createEntityManager());
}
}
@Injectable()
export class SpaceModelProductAllocationRepoitory extends Repository<SpaceModelProductAllocationEntity> {
constructor(private dataSource: DataSource) {

View File

@ -1,13 +1,11 @@
import { TypeOrmModule } from '@nestjs/typeorm';
import { SpaceModelEntity, SubspaceModelEntity, TagModel } from './entities';
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { SpaceModelEntity, SubspaceModelEntity } from './entities';
@Module({
providers: [],
exports: [],
controllers: [],
imports: [
TypeOrmModule.forFeature([SpaceModelEntity, SubspaceModelEntity, TagModel]),
],
imports: [TypeOrmModule.forFeature([SpaceModelEntity, SubspaceModelEntity])],
})
export class SpaceModelRepositoryModule {}

View File

@ -1,12 +1,13 @@
import { Entity, Column, ManyToOne, ManyToMany, JoinTable } from 'typeorm';
import { SpaceEntity } from './space.entity';
import { SpaceModelProductAllocationEntity } from '../../space-model/entities/space-model-product-allocation.entity';
import { ProductEntity } from '../../product/entities/product.entity';
import { NewTagEntity } from '../../tag/entities/tag.entity';
import { Column, Entity, ManyToOne, Unique } from 'typeorm';
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
import { ProductEntity } from '../../product/entities/product.entity';
import { SpaceModelProductAllocationEntity } from '../../space-model/entities/space-model-product-allocation.entity';
import { NewTagEntity } from '../../tag/entities/tag.entity';
import { SpaceProductAllocationDto } from '../dtos/space-product-allocation.dto';
import { SpaceEntity } from './space.entity';
@Entity({ name: 'space_product_allocation' })
@Unique(['space', 'product', 'tag'], {})
export class SpaceProductAllocationEntity extends AbstractEntity<SpaceProductAllocationDto> {
@Column({
type: 'uuid',
@ -30,9 +31,8 @@ export class SpaceProductAllocationEntity extends AbstractEntity<SpaceProductAll
@ManyToOne(() => ProductEntity, { nullable: false, onDelete: 'CASCADE' })
public product: ProductEntity;
@ManyToMany(() => NewTagEntity)
@JoinTable({ name: 'space_product_tags' })
public tags: NewTagEntity[];
@ManyToOne(() => NewTagEntity, { nullable: true, onDelete: 'CASCADE' })
public tag: NewTagEntity;
constructor(partial: Partial<SpaceProductAllocationEntity>) {
super();

View File

@ -1,20 +1,13 @@
import {
Entity,
Column,
ManyToOne,
ManyToMany,
JoinTable,
Unique,
} from 'typeorm';
import { SubspaceEntity } from './subspace.entity';
import { AbstractEntity } from '@app/common/modules/abstract/entities/abstract.entity';
import { ProductEntity } from '@app/common/modules/product/entities';
import { SubspaceModelProductAllocationEntity } from '@app/common/modules/space-model';
import { NewTagEntity } from '@app/common/modules/tag/entities/tag.entity';
import { AbstractEntity } from '@app/common/modules/abstract/entities/abstract.entity';
import { Column, Entity, ManyToOne, Unique } from 'typeorm';
import { SubspaceProductAllocationDto } from '../../dtos/subspace-product-allocation.dto';
import { SubspaceEntity } from './subspace.entity';
@Entity({ name: 'subspace_product_allocation' })
// @Unique(['subspace', 'product'])
@Unique(['subspace', 'product', 'tag'])
export class SubspaceProductAllocationEntity extends AbstractEntity<SubspaceProductAllocationDto> {
@Column({
type: 'uuid',
@ -38,9 +31,8 @@ export class SubspaceProductAllocationEntity extends AbstractEntity<SubspaceProd
@ManyToOne(() => ProductEntity, { nullable: false, onDelete: 'CASCADE' })
public product: ProductEntity;
@ManyToMany(() => NewTagEntity)
@JoinTable({ name: 'subspace_product_tags' })
public tags: NewTagEntity[];
@ManyToOne(() => NewTagEntity, { nullable: true, onDelete: 'CASCADE' })
public tag: NewTagEntity;
constructor(partial: Partial<SubspaceProductAllocationEntity>) {
super();

View File

@ -4,7 +4,6 @@ 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 { TagEntity } from '../tag.entity';
import { SubspaceProductAllocationEntity } from './subspace-product-allocation.entity';
@Entity({ name: 'subspace' })
@ -43,9 +42,6 @@ export class SubspaceEntity extends AbstractEntity<SubspaceDto> {
})
subSpaceModel?: SubspaceModelEntity;
@OneToMany(() => TagEntity, (tag) => tag.subspace)
tags: TagEntity[];
@OneToMany(
() => SubspaceProductAllocationEntity,
(allocation) => allocation.subspace,

View File

@ -1,41 +0,0 @@
import { Entity, Column, ManyToOne, JoinColumn, 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 { DeviceEntity } from '../../device/entities';
import { SubspaceEntity } from './subspace/subspace.entity';
@Entity({ name: 'tag' })
export class TagEntity extends AbstractEntity<TagDto> {
@Column({ type: 'varchar', length: 255, nullable: true })
tag: string;
@ManyToOne(() => TagModel, (model) => model.tags, {
nullable: true,
})
model: TagModel;
@ManyToOne(() => ProductEntity, (product) => product.tags, {
nullable: false,
})
product: ProductEntity;
@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,
})
@JoinColumn({ name: 'device_id' })
device: DeviceEntity;
}

View File

@ -1,10 +1,9 @@
import { DataSource, Repository } from 'typeorm';
import { Injectable } from '@nestjs/common';
import { DataSource, Repository } from 'typeorm';
import { InviteSpaceEntity } from '../entities/invite-space.entity';
import { SpaceLinkEntity } from '../entities/space-link.entity';
import { SpaceEntity } from '../entities/space.entity';
import { TagEntity } from '../entities/tag.entity';
import { SpaceProductAllocationEntity } from '../entities/space-product-allocation.entity';
import { SpaceEntity } from '../entities/space.entity';
@Injectable()
export class SpaceRepository extends Repository<SpaceEntity> {
@ -20,13 +19,6 @@ export class SpaceLinkRepository extends Repository<SpaceLinkEntity> {
}
}
@Injectable()
export class TagRepository extends Repository<TagEntity> {
constructor(private dataSource: DataSource) {
super(TagEntity, dataSource.createEntityManager());
}
}
@Injectable()
export class InviteSpaceRepository extends Repository<InviteSpaceEntity> {
constructor(private dataSource: DataSource) {

View File

@ -6,7 +6,6 @@ import { SpaceProductAllocationEntity } from './entities/space-product-allocatio
import { SpaceEntity } from './entities/space.entity';
import { SubspaceProductAllocationEntity } from './entities/subspace/subspace-product-allocation.entity';
import { SubspaceEntity } from './entities/subspace/subspace.entity';
import { TagEntity } from './entities/tag.entity';
@Module({
providers: [],
@ -16,7 +15,6 @@ import { TagEntity } from './entities/tag.entity';
TypeOrmModule.forFeature([
SpaceEntity,
SubspaceEntity,
TagEntity,
InviteSpaceEntity,
SpaceProductAllocationEntity,
SubspaceProductAllocationEntity,

View File

@ -1,11 +1,10 @@
import { Entity, Column, ManyToOne, Unique, ManyToMany } from 'typeorm';
import { ProductEntity } from '../../product/entities';
import { ProjectEntity } from '../../project/entities';
import { Column, Entity, ManyToOne, OneToMany, Unique } from 'typeorm';
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
import { NewTagDto } from '../dtos/tag.dto';
import { DeviceEntity } from '../../device/entities/device.entity';
import { ProjectEntity } from '../../project/entities';
import { SpaceModelProductAllocationEntity } from '../../space-model/entities/space-model-product-allocation.entity';
import { SubspaceModelProductAllocationEntity } from '../../space-model/entities/subspace-model/subspace-model-product-allocation.entity';
import { DeviceEntity } from '../../device/entities/device.entity';
import { NewTagDto } from '../dtos/tag.dto';
@Entity({ name: 'new_tag' })
@Unique(['name', 'project'])
@ -24,31 +23,25 @@ export class NewTagEntity extends AbstractEntity<NewTagDto> {
})
name: string;
@ManyToOne(() => ProductEntity, (product) => product.newTags, {
nullable: true,
onDelete: 'CASCADE',
})
public product: ProductEntity;
@ManyToOne(() => ProjectEntity, (project) => project.tags, {
nullable: false,
onDelete: 'CASCADE',
})
public project: ProjectEntity;
@ManyToMany(
@OneToMany(
() => SpaceModelProductAllocationEntity,
(allocation) => allocation.tags,
(allocation) => allocation.tag,
)
public spaceModelAllocations: SpaceModelProductAllocationEntity[];
@ManyToMany(
@OneToMany(
() => SubspaceModelProductAllocationEntity,
(allocation) => allocation.tags,
(allocation) => allocation.tag,
)
public subspaceModelAllocations: SubspaceModelProductAllocationEntity[];
@ManyToOne(() => DeviceEntity, (device) => device.tag)
@OneToMany(() => DeviceEntity, (device) => device.tag)
public devices: DeviceEntity[];
constructor(partial: Partial<NewTagEntity>) {

View File

@ -10,6 +10,7 @@
"format": "prettier --write \"apps/**/*.ts\" \"libs/**/*.ts\"",
"start": "npm run test && node dist/main",
"start:dev": "npm run test && npx nest start --watch",
"dev": "npx nest start --watch",
"start:debug": "npm run test && npx nest start --debug --watch",
"start:prod": "npm run test && node dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",

View File

@ -1,45 +1,44 @@
import { SeederModule } from '@app/common/seed/seeder.module';
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import config from './config';
import { APP_INTERCEPTOR } from '@nestjs/core';
import { WinstonModule } from 'nest-winston';
import { AuthenticationModule } from './auth/auth.module';
import { UserModule } from './users/user.module';
import { GroupModule } from './group/group.module';
import { DeviceModule } from './device/device.module';
import { UserDevicePermissionModule } from './user-device-permission/user-device-permission.module';
import { CommunityModule } from './community/community.module';
import { SeederModule } from '@app/common/seed/seeder.module';
import { UserNotificationModule } from './user-notification/user-notification.module';
import { DeviceMessagesSubscriptionModule } from './device-messages/device-messages.module';
import { SceneModule } from './scene/scene.module';
import { DoorLockModule } from './door-lock/door.lock.module';
import { APP_GUARD, APP_INTERCEPTOR } from '@nestjs/core';
import { LoggingInterceptor } from './interceptors/logging.interceptor';
import { AutomationModule } from './automation/automation.module';
import { RegionModule } from './region/region.module';
import { TimeZoneModule } from './timezone/timezone.module';
import { VisitorPasswordModule } from './vistor-password/visitor-password.module';
import { ScheduleModule } from './schedule/schedule.module';
import { SpaceModule } from './space/space.module';
import { ProductModule } from './product';
import { ProjectModule } from './project';
import { SpaceModelModule } from './space-model';
import { InviteUserModule } from './invite-user/invite-user.module';
import { PermissionModule } from './permission/permission.module';
import { RoleModule } from './role/role.module';
import { TermsConditionsModule } from './terms-conditions/terms-conditions.module';
import { PrivacyPolicyModule } from './privacy-policy/privacy-policy.module';
import { TagModule } from './tags/tags.module';
import { ClientModule } from './client/client.module';
import { DeviceCommissionModule } from './commission-device/commission-device.module';
import { PowerClampModule } from './power-clamp/power-clamp.module';
import { WinstonModule } from 'nest-winston';
import { ThrottlerGuard, ThrottlerModule } from '@nestjs/throttler';
import { CommunityModule } from './community/community.module';
import config from './config';
import { DeviceMessagesSubscriptionModule } from './device-messages/device-messages.module';
import { DeviceModule } from './device/device.module';
import { DoorLockModule } from './door-lock/door.lock.module';
import { GroupModule } from './group/group.module';
import { HealthModule } from './health/health.module';
import { LoggingInterceptor } from './interceptors/logging.interceptor';
import { InviteUserModule } from './invite-user/invite-user.module';
import { PermissionModule } from './permission/permission.module';
import { PowerClampModule } from './power-clamp/power-clamp.module';
import { PrivacyPolicyModule } from './privacy-policy/privacy-policy.module';
import { ProductModule } from './product';
import { ProjectModule } from './project';
import { RegionModule } from './region/region.module';
import { RoleModule } from './role/role.module';
import { SceneModule } from './scene/scene.module';
import { ScheduleModule } from './schedule/schedule.module';
import { SpaceModelModule } from './space-model';
import { SpaceModule } from './space/space.module';
import { TagModule } from './tags/tags.module';
import { TermsConditionsModule } from './terms-conditions/terms-conditions.module';
import { TimeZoneModule } from './timezone/timezone.module';
import { UserDevicePermissionModule } from './user-device-permission/user-device-permission.module';
import { UserNotificationModule } from './user-notification/user-notification.module';
import { UserModule } from './users/user.module';
import { VisitorPasswordModule } from './vistor-password/visitor-password.module';
import { winstonLoggerOptions } from '../libs/common/src/logger/services/winston.logger';
import { AqiModule } from './aqi/aqi.module';
import { OccupancyModule } from './occupancy/occupancy.module';
import { WeatherModule } from './weather/weather.module';
import { AqiModule } from './aqi/aqi.module';
@Module({
imports: [
ConfigModule.forRoot({

View File

@ -1,17 +1,19 @@
import * as fs from 'fs';
import * as csv from 'csv-parser';
import * as fs from 'fs';
import { ProjectParam } from '@app/common/dto/project-param.dto';
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
import { CommunityRepository } from '@app/common/modules/community/repositories';
import { DeviceRepository } from '@app/common/modules/device/repositories';
import { ProjectRepository } from '@app/common/modules/project/repositiories';
import { SpaceRepository } from '@app/common/modules/space';
import { SpaceProductAllocationEntity } from '@app/common/modules/space/entities/space-product-allocation.entity';
import { SubspaceProductAllocationEntity } from '@app/common/modules/space/entities/subspace/subspace-product-allocation.entity';
import { SubspaceEntity } from '@app/common/modules/space/entities/subspace/subspace.entity';
import { SubspaceRepository } from '@app/common/modules/space/repositories/subspace.repository';
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { DeviceService } from 'src/device/services';
import { CommunityRepository } from '@app/common/modules/community/repositories';
import { SpaceRepository } from '@app/common/modules/space';
import { SubspaceRepository } from '@app/common/modules/space/repositories/subspace.repository';
import { SubspaceEntity } from '@app/common/modules/space/entities/subspace/subspace.entity';
import { DeviceRepository } from '@app/common/modules/device/repositories';
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
import { ProjectParam } from '@app/common/dto/project-param.dto';
import { ProjectRepository } from '@app/common/modules/project/repositiories';
@Injectable()
export class DeviceCommissionService {
@ -118,7 +120,7 @@ export class DeviceCommissionService {
where: { uuid: spaceId },
relations: [
'productAllocations',
'productAllocations.tags',
'productAllocations.tag',
'productAllocations.product',
],
});
@ -135,7 +137,7 @@ export class DeviceCommissionService {
where: { uuid: subspaceId },
relations: [
'productAllocations',
'productAllocations.tags',
'productAllocations.tag',
'productAllocations.product',
],
});
@ -151,19 +153,23 @@ export class DeviceCommissionService {
subspace?.productAllocations || space.productAllocations;
const match = allocations
.flatMap((pa) =>
(pa.tags || []).map((tag) => ({ product: pa.product, tag })),
.map(
({
product,
tag,
}:
| SpaceProductAllocationEntity
| SubspaceProductAllocationEntity) => ({ product, tag }),
)
.find(({ tag }) => tag.name === tagName);
.find(
({ tag, product }) =>
tag.name === tagName && product.name === productName,
);
if (!match) {
console.error(`No matching tag found for Device ID: ${rawDeviceId}`);
failureCount.value++;
return;
}
if (match.product.name !== productName) {
console.error(`Product name mismatch for Device ID: ${rawDeviceId}`);
console.error(
`No matching tag-product combination found for Device ID: ${rawDeviceId}`,
);
failureCount.value++;
return;
}

View File

@ -8,7 +8,6 @@ import {
SpaceLinkRepository,
SpaceProductAllocationRepository,
SpaceRepository,
TagRepository,
} from '@app/common/modules/space/repositories';
import { UserSpaceRepository } from '@app/common/modules/user/repositories';
import { UserRepositoryModule } from '@app/common/modules/user/user.repository.module';
@ -87,6 +86,7 @@ import { AqiDataService } from '@app/common/helper/services/aqi.data.service';
SpaceProductAllocationService,
SpaceLinkRepository,
SubspaceRepository,
// Todo: find out why this is needed
TagService,
SubspaceDeviceService,
SubspaceProductAllocationService,
@ -98,7 +98,6 @@ import { AqiDataService } from '@app/common/helper/services/aqi.data.service';
SpaceModelProductAllocationService,
SpaceProductAllocationRepository,
SubspaceProductAllocationRepository,
TagRepository,
SubspaceModelRepository,
SubspaceModelProductAllocationService,
SpaceModelProductAllocationRepoitory,

View File

@ -1,29 +1,30 @@
import {
Injectable,
HttpException,
HttpStatus,
NotFoundException,
} from '@nestjs/common';
import { AddCommunityDto, GetCommunityParams, ProjectParam } from '../dtos';
import { UpdateCommunityNameDto } from '../dtos/update.community.dto';
import { ORPHAN_COMMUNITY_NAME } from '@app/common/constants/orphan-constant';
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
import { PageResponse } from '@app/common/dto/pagination.response.dto';
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
import {
ExtendedTypeORMCustomModelFindAllQuery,
TypeORMCustomModel,
} from '@app/common/models/typeOrmCustom.model';
import { PageResponse } from '@app/common/dto/pagination.response.dto';
import { CommunityRepository } from '@app/common/modules/community/repositories';
import { CommunityDto } from '@app/common/modules/community/dtos';
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
import { ProjectRepository } from '@app/common/modules/project/repositiories';
import { ORPHAN_COMMUNITY_NAME } from '@app/common/constants/orphan-constant';
import { ILike, In, Not } from 'typeorm';
import { SpaceService } from 'src/space/services';
import { SpaceRepository } from '@app/common/modules/space';
import { CommunityEntity } from '@app/common/modules/community/entities';
import { CommunityRepository } from '@app/common/modules/community/repositories';
import { DeviceEntity } from '@app/common/modules/device/entities';
import { ProjectRepository } from '@app/common/modules/project/repositiories';
import { SpaceRepository } from '@app/common/modules/space';
import { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
import { addSpaceUuidToDevices } from '@app/common/util/device-utils';
import {
HttpException,
HttpStatus,
Injectable,
NotFoundException,
} from '@nestjs/common';
import { SpaceService } from 'src/space/services';
import { SelectQueryBuilder } from 'typeorm';
import { AddCommunityDto, GetCommunityParams, ProjectParam } from '../dtos';
import { UpdateCommunityNameDto } from '../dtos/update.community.dto';
@Injectable()
export class CommunityService {
@ -93,56 +94,35 @@ export class CommunityService {
}
async getCommunities(
param: ProjectParam,
{ projectUuid }: ProjectParam,
pageable: Partial<ExtendedTypeORMCustomModelFindAllQuery>,
): Promise<BaseResponseDto> {
try {
const project = await this.validateProject(param.projectUuid);
pageable.modelName = 'community';
pageable.where = {
project: { uuid: param.projectUuid },
name: Not(`${ORPHAN_COMMUNITY_NAME}-${project.name}`),
};
const project = await this.validateProject(projectUuid);
/**
* TODO: removing this breaks the code (should be fixed when refactoring @see TypeORMCustomModel
*/
pageable.where = {};
let qb: undefined | SelectQueryBuilder<CommunityEntity> = undefined;
if (pageable.search) {
const matchingCommunities = await this.communityRepository.find({
where: {
project: { uuid: param.projectUuid },
name: ILike(`%${pageable.search}%`),
},
});
const matchingSpaces = await this.spaceRepository.find({
where: {
spaceName: ILike(`%${pageable.search}%`),
disabled: false,
community: { project: { uuid: param.projectUuid } },
},
relations: ['community'],
});
const spaceCommunityUuids = [
...new Set(matchingSpaces.map((space) => space.community.uuid)),
];
const allMatchedCommunityUuids = [
...new Set([
...matchingCommunities.map((c) => c.uuid),
...spaceCommunityUuids,
]),
];
pageable.where = {
...pageable.where,
uuid: In(allMatchedCommunityUuids),
};
qb = this.communityRepository
.createQueryBuilder('c')
.leftJoin('c.spaces', 's')
.where('c.project = :projectUuid', { projectUuid })
.andWhere(`c.name != '${ORPHAN_COMMUNITY_NAME}-${project.name}'`)
.andWhere('s.disabled = false')
.andWhere(
`c.name ILIKE '%${pageable.search}%' OR s.space_name ILIKE '%${pageable.search}%'`,
)
.distinct(true);
}
const customModel = TypeORMCustomModel(this.communityRepository);
const { baseResponseDto, paginationResponseDto } =
await customModel.findAll(pageable);
await customModel.findAll({ ...pageable, modelName: 'community' }, qb);
// todo: refactor this to minimize the number of queries
if (pageable.includeSpaces) {
const communitiesWithSpaces = await Promise.all(
baseResponseDto.data.map(async (community: CommunityDto) => {
@ -150,7 +130,7 @@ export class CommunityService {
await this.spaceService.getSpacesHierarchyForCommunity(
{
communityUuid: community.uuid,
projectUuid: param.projectUuid,
projectUuid: projectUuid,
},
{
onlyWithDevices: false,

View File

@ -1,39 +1,46 @@
import { ORPHAN_SPACE_NAME } from './../../../libs/common/src/constants/orphan-constant';
import { ProductRepository } from './../../../libs/common/src/modules/product/repositories/product.repository';
import { AUTOMATION_CONFIG } from '@app/common/constants/automation.enum';
import { BatchDeviceTypeEnum } from '@app/common/constants/batch-device.enum';
import { BatteryStatus } from '@app/common/constants/battery-status.enum';
import { DeviceStatuses } from '@app/common/constants/device-status.enum';
import { DeviceTypeEnum } from '@app/common/constants/device-type.enum';
import { CommonErrorCodes } from '@app/common/constants/error-codes.enum';
import { ProductType } from '@app/common/constants/product-type.enum';
import { SceneSwitchesTypeEnum } from '@app/common/constants/scene-switch-type.enum';
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
import { DeviceStatusFirebaseService } from '@app/common/firebase/devices-status/services/devices-status.service';
import { convertKeysToCamelCase } from '@app/common/helper/camelCaseConverter';
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
import { CommunityRepository } from '@app/common/modules/community/repositories';
import { DeviceEntity } from '@app/common/modules/device/entities';
import { DeviceRepository } from '@app/common/modules/device/repositories';
import { ProjectEntity } from '@app/common/modules/project/entities';
import { ProjectRepository } from '@app/common/modules/project/repositiories';
import { SceneDeviceRepository } from '@app/common/modules/scene-device/repositories';
import { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
import { SpaceRepository } from '@app/common/modules/space/repositories';
import { addSpaceUuidToDevices } from '@app/common/util/device-utils';
import {
Injectable,
HttpException,
HttpStatus,
NotFoundException,
BadRequestException,
forwardRef,
HttpException,
HttpStatus,
Inject,
Injectable,
NotFoundException,
} from '@nestjs/common';
import { TuyaContext } from '@tuya/tuya-connector-nodejs';
import { ConfigService } from '@nestjs/config';
import { TuyaContext } from '@tuya/tuya-connector-nodejs';
import { AddAutomationDto } from 'src/automation/dtos';
import { SceneService } from 'src/scene/services';
import { In, Not, QueryRunner } from 'typeorm';
import { ProjectParam } from '../dtos';
import {
AddDeviceDto,
AddSceneToFourSceneDeviceDto,
UpdateDeviceDto,
AssignDeviceToSpaceDto,
UpdateDeviceDto,
} from '../dtos/add.device.dto';
import {
DeviceInstructionResponse,
GetDeviceDetailsFunctionsInterface,
GetDeviceDetailsFunctionsStatusInterface,
GetDeviceDetailsInterface,
GetMacAddressInterface,
GetPowerClampFunctionsStatusInterface,
controlDeviceInterface,
getDeviceLogsInterface,
updateDeviceFirmwareInterface,
} from '../interfaces/get.device.interface';
import {
GetDeviceBySpaceUuidDto,
GetDeviceLogsDto,
GetDevicesBySpaceOrCommunityDto,
GetDoorLockDevices,
} from '../dtos/get.device.dto';
import {
BatchControlDevicesDto,
BatchFactoryResetDevicesDto,
@ -41,33 +48,29 @@ import {
ControlDeviceDto,
GetSceneFourSceneDeviceDto,
} from '../dtos/control.device.dto';
import { convertKeysToCamelCase } from '@app/common/helper/camelCaseConverter';
import { DeviceRepository } from '@app/common/modules/device/repositories';
import { In, Not, 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';
import { DeviceStatuses } from '@app/common/constants/device-status.enum';
import { CommonErrorCodes } from '@app/common/constants/error-codes.enum';
import { BatteryStatus } from '@app/common/constants/battery-status.enum';
import { SceneService } from 'src/scene/services';
import { AddAutomationDto } from 'src/automation/dtos';
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
import { SceneDeviceRepository } from '@app/common/modules/scene-device/repositories';
import { SceneSwitchesTypeEnum } from '@app/common/constants/scene-switch-type.enum';
import { AUTOMATION_CONFIG } from '@app/common/constants/automation.enum';
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';
import { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
import { ProjectRepository } from '@app/common/modules/project/repositiories';
import { ProjectParam } from '../dtos';
import { BatchDeviceTypeEnum } from '@app/common/constants/batch-device.enum';
import { DeviceTypeEnum } from '@app/common/constants/device-type.enum';
import { CommunityRepository } from '@app/common/modules/community/repositories';
import { addSpaceUuidToDevices } from '@app/common/util/device-utils';
import { DeviceSceneParamDto } from '../dtos/device.param.dto';
import {
GetDeviceLogsDto,
GetDevicesBySpaceOrCommunityDto,
GetDoorLockDevices,
} from '../dtos/get.device.dto';
import {
controlDeviceInterface,
DeviceInstructionResponse,
GetDeviceDetailsFunctionsInterface,
GetDeviceDetailsFunctionsStatusInterface,
GetDeviceDetailsInterface,
getDeviceLogsInterface,
GetMacAddressInterface,
GetPowerClampFunctionsStatusInterface,
updateDeviceFirmwareInterface,
} from '../interfaces/get.device.interface';
import {
ORPHAN_COMMUNITY_NAME,
ORPHAN_SPACE_NAME,
} from './../../../libs/common/src/constants/orphan-constant';
import { ProductRepository } from './../../../libs/common/src/modules/product/repositories/product.repository';
@Injectable()
export class DeviceService {
@ -198,46 +201,6 @@ export class DeviceService {
}
}
async getDevicesBySpaceId(
getDeviceBySpaceUuidDto: GetDeviceBySpaceUuidDto,
): Promise<GetDeviceDetailsInterface[]> {
try {
const devices = await this.deviceRepository.find({
where: {
spaceDevice: { uuid: getDeviceBySpaceUuidDto.spaceUuid },
isActive: true,
},
relations: [
'spaceDevice',
'productDevice',
'permission',
'permission.permissionType',
],
});
const devicesData = await Promise.all(
devices.map(async (device) => {
return {
haveRoom: device.spaceDevice ? true : false,
productUuid: device.productDevice.uuid,
productType: device.productDevice.prodType,
permissionType: device.permission[0].permissionType.type,
...(await this.getDeviceDetailsByDeviceIdTuya(
device.deviceTuyaUuid,
)),
uuid: device.uuid,
} as GetDeviceDetailsInterface;
}),
);
return devicesData;
} catch (error) {
// Handle the error here
throw new HttpException(
'Error fetching devices by space',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
async transferDeviceInSpaces(
assignDeviceToSpaceDto: AssignDeviceToSpaceDto,
projectUuid: string,
@ -1199,39 +1162,6 @@ export class DeviceService {
}
}
async getFullSpaceHierarchy(
space: SpaceEntity,
): Promise<{ uuid: string; spaceName: string }[]> {
try {
// Fetch only the relevant spaces, starting with the target space
const targetSpace = await this.spaceRepository.findOne({
where: { uuid: space.uuid },
relations: ['parent', 'children'],
});
// Fetch only the ancestors of the target space
const ancestors = await this.fetchAncestors(targetSpace);
// Optionally, fetch descendants if required
const descendants = await this.fetchDescendants(targetSpace);
const fullHierarchy = [...ancestors, targetSpace, ...descendants].map(
(space) => ({
uuid: space.uuid,
spaceName: space.spaceName,
}),
);
return fullHierarchy;
} catch (error) {
console.error('Error fetching space hierarchy:', error.message);
throw new HttpException(
'Error fetching space hierarchy',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
async getPowerClampInstructionStatus(deviceDetails: any) {
try {
const deviceStatus = await this.getPowerClampInstructionStatusTuya(
@ -1331,27 +1261,6 @@ export class DeviceService {
return ancestors.reverse();
}
private async fetchDescendants(space: SpaceEntity): Promise<SpaceEntity[]> {
const descendants: SpaceEntity[] = [];
// Fetch the immediate children of the current space
const children = await this.spaceRepository.find({
where: { parent: { uuid: space.uuid } },
relations: ['children'], // To continue fetching downwards
});
for (const child of children) {
// Add the child to the descendants list
descendants.push(child);
// Recursively fetch the child's descendants
const childDescendants = await this.fetchDescendants(child);
descendants.push(...childDescendants);
}
return descendants;
}
async addSceneToSceneDevice(
deviceUuid: string,
addSceneToFourSceneDeviceDto: AddSceneToFourSceneDeviceDto,
@ -1654,22 +1563,6 @@ export class DeviceService {
}
}
async moveDevicesToSpace(
targetSpace: SpaceEntity,
deviceIds: string[],
): Promise<void> {
if (!deviceIds || deviceIds.length === 0) {
throw new HttpException(
'No device IDs provided for transfer',
HttpStatus.BAD_REQUEST,
);
}
await this.deviceRepository.update(
{ uuid: In(deviceIds) },
{ spaceDevice: targetSpace },
);
}
async getDoorLockDevices(projectUuid: string) {
await this.validateProject(projectUuid);
@ -1860,4 +1753,39 @@ export class DeviceService {
return allDevices;
}
async addDevicesToOrphanSpace(
space: SpaceEntity,
project: ProjectEntity,
queryRunner: QueryRunner,
) {
const spaceRepository = queryRunner.manager.getRepository(SpaceEntity);
const deviceRepository = queryRunner.manager.getRepository(DeviceEntity);
try {
const orphanSpace = await spaceRepository.findOne({
where: {
community: {
name: `${ORPHAN_COMMUNITY_NAME}-${project.name}`,
},
spaceName: ORPHAN_SPACE_NAME,
},
});
if (!orphanSpace) {
throw new HttpException(
`Orphan space not found in community ${project.name}`,
HttpStatus.NOT_FOUND,
);
}
await deviceRepository.update(
{ uuid: In(space.devices.map((device) => device.uuid)) },
{ spaceDevice: orphanSpace },
);
} catch (error) {
throw new Error(
`Failed to add devices to orphan spaces: ${error.message}`,
);
}
}
}

View File

@ -1,18 +1,75 @@
import { Module } from '@nestjs/common';
import { InviteUserService } from './services/invite-user.service';
import { InviteUserController } from './controllers/invite-user.controller';
import { ConfigModule } from '@nestjs/config';
import { InviteUserController } from './controllers/invite-user.controller';
import { InviteUserService } from './services/invite-user.service';
import { DeviceStatusFirebaseService } from '@app/common/firebase/devices-status/services/devices-status.service';
import { AqiDataService } from '@app/common/helper/services/aqi.data.service';
import { OccupancyService } from '@app/common/helper/services/occupancy.service';
import { PowerClampService } from '@app/common/helper/services/power.clamp.service';
import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service';
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
import { AutomationRepository } from '@app/common/modules/automation/repositories';
import { CommunityRepository } from '@app/common/modules/community/repositories';
import { DeviceStatusLogRepository } from '@app/common/modules/device-status-log/repositories';
import {
UserRepository,
UserSpaceRepository,
} from '@app/common/modules/user/repositories';
DeviceRepository,
DeviceUserPermissionRepository,
} from '@app/common/modules/device/repositories';
import { InviteUserRepositoryModule } from '@app/common/modules/Invite-user/Invite-user.repository.module';
import {
InviteUserRepository,
InviteUserSpaceRepository,
} from '@app/common/modules/Invite-user/repositiories';
import { PermissionTypeRepository } from '@app/common/modules/permission/repositories';
import {
PowerClampDailyRepository,
PowerClampHourlyRepository,
PowerClampMonthlyRepository,
} from '@app/common/modules/power-clamp/repositories';
import { ProductRepository } from '@app/common/modules/product/repositories';
import { ProjectRepository } from '@app/common/modules/project/repositiories';
import { RegionRepository } from '@app/common/modules/region/repositories';
import { RoleTypeRepository } from '@app/common/modules/role-type/repositories';
import { SceneDeviceRepository } from '@app/common/modules/scene-device/repositories';
import {
SceneIconRepository,
SceneRepository,
} from '@app/common/modules/scene/repositories';
import {
InviteSpaceRepository,
SpaceLinkRepository,
SpaceProductAllocationRepository,
SpaceRepository,
} from '@app/common/modules/space';
import {
SpaceModelProductAllocationRepoitory,
SpaceModelRepository,
SubspaceModelProductAllocationRepoitory,
SubspaceModelRepository,
} from '@app/common/modules/space-model';
import {
SubspaceProductAllocationRepository,
SubspaceRepository,
} from '@app/common/modules/space/repositories/subspace.repository';
import { NewTagRepository } from '@app/common/modules/tag/repositories/tag-repository';
import { TimeZoneRepository } from '@app/common/modules/timezone/repositories';
import {
UserRepository,
UserSpaceRepository,
} from '@app/common/modules/user/repositories';
import { EmailService } from '@app/common/util/email.service';
import { CommunityModule } from 'src/community/community.module';
import { CommunityService } from 'src/community/services';
import { DeviceService } from 'src/device/services';
import { ProjectUserService } from 'src/project/services/project-user.service';
import { SceneService } from 'src/scene/services';
import {
SpaceModelService,
SubSpaceModelService,
} from 'src/space-model/services';
import { SpaceModelProductAllocationService } from 'src/space-model/services/space-model-product-allocation.service';
import { SubspaceModelProductAllocationService } from 'src/space-model/services/subspace/subspace-model-product-allocation.service';
import {
SpaceLinkService,
SpaceService,
@ -21,70 +78,11 @@ import {
SubSpaceService,
ValidationService,
} from 'src/space/services';
import { CommunityService } from 'src/community/services';
import {
InviteSpaceRepository,
SpaceLinkRepository,
SpaceProductAllocationRepository,
SpaceRepository,
TagRepository,
} from '@app/common/modules/space';
import { SpaceModelRepository } from '@app/common/modules/space-model';
import { CommunityRepository } from '@app/common/modules/community/repositories';
import { ProjectRepository } from '@app/common/modules/project/repositiories';
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
import { UserService, UserSpaceService } from 'src/users/services';
import { UserDevicePermissionService } from 'src/user-device-permission/services';
import {
DeviceRepository,
DeviceUserPermissionRepository,
} from '@app/common/modules/device/repositories';
import { PermissionTypeRepository } from '@app/common/modules/permission/repositories';
import { ProjectUserService } from 'src/project/services/project-user.service';
import { RoleTypeRepository } from '@app/common/modules/role-type/repositories';
import { RegionRepository } from '@app/common/modules/region/repositories';
import { TimeZoneRepository } from '@app/common/modules/timezone/repositories';
import { CommunityModule } from 'src/community/community.module';
import { TagService as NewTagService } from 'src/tags/services';
import { TagService } from 'src/space/services/tag';
import {
SpaceModelService,
SubSpaceModelService,
} from 'src/space-model/services';
import { SpaceProductAllocationService } from 'src/space/services/space-product-allocation.service';
import {
SubspaceProductAllocationRepository,
SubspaceRepository,
} from '@app/common/modules/space/repositories/subspace.repository';
import { SubspaceProductAllocationService } from 'src/space/services/subspace/subspace-product-allocation.service';
import {
SpaceModelProductAllocationRepoitory,
SubspaceModelProductAllocationRepoitory,
SubspaceModelRepository,
} from '@app/common/modules/space-model';
import { NewTagRepository } from '@app/common/modules/tag/repositories/tag-repository';
import { ProductRepository } from '@app/common/modules/product/repositories';
import { SpaceModelProductAllocationService } from 'src/space-model/services/space-model-product-allocation.service';
import { SubspaceModelProductAllocationService } from 'src/space-model/services/subspace/subspace-model-product-allocation.service';
import { DeviceService } from 'src/device/services';
import { SceneDeviceRepository } from '@app/common/modules/scene-device/repositories';
import { DeviceStatusFirebaseService } from '@app/common/firebase/devices-status/services/devices-status.service';
import { SceneService } from 'src/scene/services';
import { DeviceStatusLogRepository } from '@app/common/modules/device-status-log/repositories';
import {
SceneIconRepository,
SceneRepository,
} from '@app/common/modules/scene/repositories';
import { AutomationRepository } from '@app/common/modules/automation/repositories';
import {
PowerClampHourlyRepository,
PowerClampDailyRepository,
PowerClampMonthlyRepository,
} from '@app/common/modules/power-clamp/repositories';
import { PowerClampService } from '@app/common/helper/services/power.clamp.service';
import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service';
import { OccupancyService } from '@app/common/helper/services/occupancy.service';
import { AqiDataService } from '@app/common/helper/services/aqi.data.service';
import { TagService as NewTagService } from 'src/tags/services';
import { UserDevicePermissionService } from 'src/user-device-permission/services';
import { UserService, UserSpaceService } from 'src/users/services';
@Module({
imports: [ConfigModule, InviteUserRepositoryModule, CommunityModule],
@ -125,7 +123,6 @@ import { AqiDataService } from '@app/common/helper/services/aqi.data.service';
SpaceProductAllocationService,
SpaceLinkRepository,
SubspaceRepository,
TagService,
SubspaceDeviceService,
SubspaceProductAllocationService,
SpaceModelRepository,
@ -136,7 +133,6 @@ import { AqiDataService } from '@app/common/helper/services/aqi.data.service';
SpaceModelProductAllocationService,
SpaceProductAllocationRepository,
SubspaceProductAllocationRepository,
TagRepository,
SubspaceModelRepository,
SubspaceModelProductAllocationService,
SpaceModelProductAllocationRepoitory,

View File

@ -1,13 +1,52 @@
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { PowerClampService as PowerClamp } from './services/power-clamp.service';
import { PowerClampController } from './controllers';
import { DeviceStatusFirebaseService } from '@app/common/firebase/devices-status/services/devices-status.service';
import { OccupancyService } from '@app/common/helper/services/occupancy.service';
import { PowerClampService } from '@app/common/helper/services/power.clamp.service';
import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service';
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
import { AutomationRepository } from '@app/common/modules/automation/repositories';
import { CommunityRepository } from '@app/common/modules/community/repositories';
import { DeviceStatusLogRepository } from '@app/common/modules/device-status-log/repositories';
import { DeviceRepository } from '@app/common/modules/device/repositories';
import {
PowerClampDailyRepository,
PowerClampHourlyRepository,
PowerClampMonthlyRepository,
} from '@app/common/modules/power-clamp/repositories';
import { DeviceRepository } from '@app/common/modules/device/repositories';
import { ProductRepository } from '@app/common/modules/product/repositories';
import { ProjectRepository } from '@app/common/modules/project/repositiories';
import { SceneDeviceRepository } from '@app/common/modules/scene-device/repositories';
import {
SceneIconRepository,
SceneRepository,
} from '@app/common/modules/scene/repositories';
import {
InviteSpaceRepository,
SpaceLinkRepository,
SpaceProductAllocationRepository,
SpaceRepository,
} from '@app/common/modules/space';
import {
SpaceModelProductAllocationRepoitory,
SpaceModelRepository,
SubspaceModelProductAllocationRepoitory,
SubspaceModelRepository,
} from '@app/common/modules/space-model';
import {
SubspaceProductAllocationRepository,
SubspaceRepository,
} from '@app/common/modules/space/repositories/subspace.repository';
import { NewTagRepository } from '@app/common/modules/tag/repositories/tag-repository';
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { CommunityService } from 'src/community/services';
import { DeviceService } from 'src/device/services';
import { SceneService } from 'src/scene/services';
import {
SpaceModelService,
SubSpaceModelService,
} from 'src/space-model/services';
import { SpaceModelProductAllocationService } from 'src/space-model/services/space-model-product-allocation.service';
import { SubspaceModelProductAllocationService } from 'src/space-model/services/subspace/subspace-model-product-allocation.service';
import {
SpaceDeviceService,
SpaceLinkService,
@ -16,51 +55,11 @@ import {
SubSpaceService,
ValidationService,
} from 'src/space/services';
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
import { DeviceService } from 'src/device/services';
import {
InviteSpaceRepository,
SpaceLinkRepository,
SpaceProductAllocationRepository,
SpaceRepository,
TagRepository,
} from '@app/common/modules/space';
import { CommunityService } from 'src/community/services';
import { ProjectRepository } from '@app/common/modules/project/repositiories';
import { CommunityRepository } from '@app/common/modules/community/repositories';
import {
SpaceModelProductAllocationRepoitory,
SpaceModelRepository,
SubspaceModelProductAllocationRepoitory,
SubspaceModelRepository,
} from '@app/common/modules/space-model';
import { SceneDeviceRepository } from '@app/common/modules/scene-device/repositories';
import { ProductRepository } from '@app/common/modules/product/repositories';
import { DeviceStatusFirebaseService } from '@app/common/firebase/devices-status/services/devices-status.service';
import { SceneService } from 'src/scene/services';
import { PowerClampService } from '@app/common/helper/services/power.clamp.service';
import { DeviceStatusLogRepository } from '@app/common/modules/device-status-log/repositories';
import {
SceneIconRepository,
SceneRepository,
} from '@app/common/modules/scene/repositories';
import { AutomationRepository } from '@app/common/modules/automation/repositories';
import { TagService } from 'src/tags/services';
import {
SpaceModelService,
SubSpaceModelService,
} from 'src/space-model/services';
import { SpaceProductAllocationService } from 'src/space/services/space-product-allocation.service';
import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service';
import {
SubspaceProductAllocationRepository,
SubspaceRepository,
} from '@app/common/modules/space/repositories/subspace.repository';
import { SubspaceProductAllocationService } from 'src/space/services/subspace/subspace-product-allocation.service';
import { NewTagRepository } from '@app/common/modules/tag/repositories/tag-repository';
import { SpaceModelProductAllocationService } from 'src/space-model/services/space-model-product-allocation.service';
import { SubspaceModelProductAllocationService } from 'src/space-model/services/subspace/subspace-model-product-allocation.service';
import { OccupancyService } from '@app/common/helper/services/occupancy.service';
import { TagService } from 'src/tags/services';
import { PowerClampController } from './controllers';
import { PowerClampService as PowerClamp } from './services/power-clamp.service';
import { AqiDataService } from '@app/common/helper/services/aqi.data.service';
@Module({
imports: [ConfigModule],
@ -106,7 +105,6 @@ import { AqiDataService } from '@app/common/helper/services/aqi.data.service';
SpaceModelProductAllocationService,
SpaceProductAllocationRepository,
SubspaceProductAllocationRepository,
TagRepository,
SubspaceModelRepository,
SubspaceModelProductAllocationService,
SpaceModelProductAllocationRepoitory,

View File

@ -1,24 +1,58 @@
import { Global, Module } from '@nestjs/common';
import { CqrsModule } from '@nestjs/cqrs';
import { ProjectController } from './controllers';
import { ProjectService } from './services';
import { DeviceStatusFirebaseService } from '@app/common/firebase/devices-status/services/devices-status.service';
import { AqiDataService } from '@app/common/helper/services/aqi.data.service';
import { OccupancyService } from '@app/common/helper/services/occupancy.service';
import { PowerClampService } from '@app/common/helper/services/power.clamp.service';
import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service';
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
import { AutomationRepository } from '@app/common/modules/automation/repositories';
import { CommunityRepository } from '@app/common/modules/community/repositories';
import { DeviceStatusLogRepository } from '@app/common/modules/device-status-log/repositories';
import { DeviceRepository } from '@app/common/modules/device/repositories';
import { InviteUserRepository } from '@app/common/modules/Invite-user/repositiories';
import {
PowerClampDailyRepository,
PowerClampHourlyRepository,
PowerClampMonthlyRepository,
} from '@app/common/modules/power-clamp/repositories';
import { ProductRepository } from '@app/common/modules/product/repositories';
import { ProjectRepository } from '@app/common/modules/project/repositiories';
import { CreateOrphanSpaceHandler } from './handler';
import { SceneDeviceRepository } from '@app/common/modules/scene-device/repositories';
import {
SceneIconRepository,
SceneRepository,
} from '@app/common/modules/scene/repositories';
import {
InviteSpaceRepository,
SpaceLinkRepository,
SpaceProductAllocationRepository,
SpaceRepository,
TagRepository,
} from '@app/common/modules/space';
import { CommunityRepository } from '@app/common/modules/community/repositories';
import { InviteUserRepository } from '@app/common/modules/Invite-user/repositiories';
import { ProjectUserController } from './controllers/project-user.controller';
import { ProjectUserService } from './services/project-user.service';
import {
SpaceModelProductAllocationRepoitory,
SpaceModelRepository,
SubspaceModelProductAllocationRepoitory,
SubspaceModelRepository,
} from '@app/common/modules/space-model';
import {
SubspaceProductAllocationRepository,
SubspaceRepository,
} from '@app/common/modules/space/repositories/subspace.repository';
import { NewTagRepository } from '@app/common/modules/tag/repositories/tag-repository';
import {
UserRepository,
UserSpaceRepository,
} from '@app/common/modules/user/repositories';
import { Global, Module } from '@nestjs/common';
import { CqrsModule } from '@nestjs/cqrs';
import { CommunityService } from 'src/community/services';
import { DeviceService } from 'src/device/services';
import { SceneService } from 'src/scene/services';
import {
SpaceModelService,
SubSpaceModelService,
} from 'src/space-model/services';
import { SpaceModelProductAllocationService } from 'src/space-model/services/space-model-product-allocation.service';
import { SubspaceModelProductAllocationService } from 'src/space-model/services/subspace/subspace-model-product-allocation.service';
import {
SpaceLinkService,
SpaceService,
@ -26,49 +60,14 @@ import {
SubSpaceService,
ValidationService,
} from 'src/space/services';
import { TagService } from 'src/tags/services';
import {
SpaceModelService,
SubSpaceModelService,
} from 'src/space-model/services';
import { DeviceService } from 'src/device/services';
import { SpaceProductAllocationService } from 'src/space/services/space-product-allocation.service';
import {
SubspaceProductAllocationRepository,
SubspaceRepository,
} from '@app/common/modules/space/repositories/subspace.repository';
import { SubspaceProductAllocationService } from 'src/space/services/subspace/subspace-product-allocation.service';
import { CommunityService } from 'src/community/services';
import {
SpaceModelProductAllocationRepoitory,
SpaceModelRepository,
SubspaceModelProductAllocationRepoitory,
SubspaceModelRepository,
} from '@app/common/modules/space-model';
import { DeviceRepository } from '@app/common/modules/device/repositories';
import { NewTagRepository } from '@app/common/modules/tag/repositories/tag-repository';
import { ProductRepository } from '@app/common/modules/product/repositories';
import { SpaceModelProductAllocationService } from 'src/space-model/services/space-model-product-allocation.service';
import { SceneDeviceRepository } from '@app/common/modules/scene-device/repositories';
import { DeviceStatusFirebaseService } from '@app/common/firebase/devices-status/services/devices-status.service';
import { SceneService } from 'src/scene/services';
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
import { SubspaceModelProductAllocationService } from 'src/space-model/services/subspace/subspace-model-product-allocation.service';
import { DeviceStatusLogRepository } from '@app/common/modules/device-status-log/repositories';
import {
SceneIconRepository,
SceneRepository,
} from '@app/common/modules/scene/repositories';
import { AutomationRepository } from '@app/common/modules/automation/repositories';
import { PowerClampService } from '@app/common/helper/services/power.clamp.service';
import {
PowerClampDailyRepository,
PowerClampHourlyRepository,
PowerClampMonthlyRepository,
} from '@app/common/modules/power-clamp/repositories';
import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service';
import { OccupancyService } from '@app/common/helper/services/occupancy.service';
import { AqiDataService } from '@app/common/helper/services/aqi.data.service';
import { TagService } from 'src/tags/services';
import { ProjectController } from './controllers';
import { ProjectUserController } from './controllers/project-user.controller';
import { CreateOrphanSpaceHandler } from './handler';
import { ProjectService } from './services';
import { ProjectUserService } from './services/project-user.service';
const CommandHandlers = [CreateOrphanSpaceHandler];
@ -112,7 +111,6 @@ const CommandHandlers = [CreateOrphanSpaceHandler];
DeviceStatusFirebaseService,
SceneService,
TuyaService,
TagRepository,
SubspaceModelRepository,
SubspaceModelProductAllocationService,
SpaceModelProductAllocationRepoitory,

View File

@ -1,4 +1,17 @@
import { ORPHAN_COMMUNITY_NAME } from '@app/common/constants/orphan-constant';
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
import { PageResponse } from '@app/common/dto/pagination.response.dto';
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
import {
TypeORMCustomModel,
TypeORMCustomModelFindAllQuery,
} from '@app/common/models/typeOrmCustom.model';
import { ProjectDto } from '@app/common/modules/project/dtos';
import { ProjectEntity } from '@app/common/modules/project/entities';
import { ProjectRepository } from '@app/common/modules/project/repositiories';
import { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
import { UserRepository } from '@app/common/modules/user/repositories';
import { format } from '@fast-csv/format';
import {
forwardRef,
HttpException,
@ -6,24 +19,11 @@ import {
Inject,
Injectable,
} from '@nestjs/common';
import { CreateProjectDto, GetProjectParam } from '../dto';
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
import { ProjectEntity } from '@app/common/modules/project/entities';
import {
TypeORMCustomModel,
TypeORMCustomModelFindAllQuery,
} from '@app/common/models/typeOrmCustom.model';
import { ProjectDto } from '@app/common/modules/project/dtos';
import { PageResponse } from '@app/common/dto/pagination.response.dto';
import { CommandBus } from '@nestjs/cqrs';
import { CreateOrphanSpaceCommand } from '../command/create-orphan-space-command';
import { UserRepository } from '@app/common/modules/user/repositories';
import { format } from '@fast-csv/format';
import { PassThrough } from 'stream';
import { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
import { SpaceService } from 'src/space/services';
import { ORPHAN_COMMUNITY_NAME } from '@app/common/constants/orphan-constant';
import { PassThrough } from 'stream';
import { CreateOrphanSpaceCommand } from '../command/create-orphan-space-command';
import { CreateProjectDto, GetProjectParam } from '../dto';
@Injectable()
export class ProjectService {
@ -236,11 +236,11 @@ export class ProjectService {
'communities.spaces.parent',
'communities.spaces.productAllocations',
'communities.spaces.productAllocations.product',
'communities.spaces.productAllocations.tags',
'communities.spaces.productAllocations.tag',
'communities.spaces.subspaces',
'communities.spaces.subspaces.productAllocations',
'communities.spaces.subspaces.productAllocations.product',
'communities.spaces.subspaces.productAllocations.tags',
'communities.spaces.subspaces.productAllocations.tag',
],
});
@ -303,52 +303,38 @@ export class ProjectService {
if (subspace.disabled) continue;
for (const productAllocation of subspace.productAllocations || []) {
for (const tag of productAllocation.tags || []) {
csvStream.write({
'Device ID': '',
'Community Name': space.community?.name || '',
'Space Name': space.spaceName,
'Space Location': spaceLocation,
'Subspace Name': subspace.subspaceName || '',
Tag: tag.name,
'Product Name': productAllocation.product.name || '',
'Community UUID': space.community?.uuid || '',
'Space UUID': space.uuid,
'Subspace UUID': subspace.uuid,
});
}
}
}
for (const productAllocation of space.productAllocations || []) {
for (const tag of productAllocation.tags || []) {
csvStream.write({
'Device ID': '',
'Community Name': space.community?.name || '',
'Space Name': space.spaceName,
'Space Location': spaceLocation,
'Subspace Name': '',
Tag: tag.name,
'Subspace Name': subspace.subspaceName || '',
Tag: productAllocation.tag.name,
'Product Name': productAllocation.product.name || '',
'Community UUID': space.community?.uuid || '',
'Space UUID': space.uuid,
'Subspace UUID': '',
'Subspace UUID': subspace.uuid,
});
}
}
for (const productAllocation of space.productAllocations || []) {
csvStream.write({
'Device ID': '',
'Community Name': space.community?.name || '',
'Space Name': space.spaceName,
'Space Location': spaceLocation,
'Subspace Name': '',
Tag: productAllocation.tag.name,
'Product Name': productAllocation.product.name || '',
'Community UUID': space.community?.uuid || '',
'Space UUID': space.uuid,
'Subspace UUID': '',
});
}
}
csvStream.end();
return stream;
}
getSpaceLocation(space: SpaceEntity): string {
const names = [];
let current = space.parent;
while (current) {
names.unshift(current.spaceName);
current = current.parent;
}
return names.join(' > ');
}
}

View File

@ -1,7 +1,6 @@
export * from './create-space-model.dto';
export * from './link-space-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';
export * from './link-space-model.dto';
export * from './update-space-model.dto';

View File

@ -1,14 +1,14 @@
import { ModifyAction } from '@app/common/constants/modify-action.enum';
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { Type } from 'class-transformer';
import {
IsString,
IsOptional,
IsArray,
ValidateNested,
IsEnum,
IsOptional,
IsString,
ValidateNested,
} from 'class-validator';
import { ModifyTagModelDto } from '../tag-model-dtos';
import { ModifyAction } from '@app/common/constants/modify-action.enum';
import { ModifyTagDto } from 'src/space/dtos/tag/modify-tag.dto';
export class ModifySubspaceModelDto {
@ApiProperty({
@ -37,11 +37,11 @@ export class ModifySubspaceModelDto {
@ApiPropertyOptional({
description:
'List of tag modifications (add/update/delete) for the subspace',
type: [ModifyTagModelDto],
type: [ModifyTagDto],
})
@IsOptional()
@IsArray()
@ValidateNested({ each: true })
@Type(() => ModifyTagModelDto)
tags?: ModifyTagModelDto[];
@Type(() => ModifyTagDto)
tags?: ModifyTagDto[];
}

View File

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

View File

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

View File

@ -1,46 +0,0 @@
import { ModifyAction } from '@app/common/constants/modify-action.enum';
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { IsString, IsOptional, IsEnum, IsUUID } 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 new tag',
example: '123e4567-e89b-12d3-a456-426614174000',
})
@IsOptional()
@IsUUID()
newTagUuid: string;
@ApiPropertyOptional({
description:
'UUID of an existing tag (required for update/delete, optional for add)',
example: 'a1b2c3d4-5678-90ef-abcd-1234567890ef',
})
@IsOptional()
@IsUUID()
tagUuid?: string;
@ApiPropertyOptional({
description: 'Name of the tag (required for add/update)',
example: 'Temperature Sensor',
})
@IsOptional()
@IsString()
name?: string;
@ApiPropertyOptional({
description:
'UUID of the product associated with the tag (required for add)',
example: 'c789a91e-549a-4753-9006-02f89e8170e0',
})
@IsOptional()
@IsUUID()
productUuid?: string;
}

View File

@ -1,21 +0,0 @@
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

@ -1,48 +1,13 @@
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 { IsArray, IsOptional, IsString, ValidateNested } from 'class-validator';
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[];
}
import { CreateSubspaceModelDto } from './subspaces-model-dtos/create-subspace-model.dto';
import { ModifyTagDto } from 'src/space/dtos/tag/modify-tag.dto';
export class UpdateSpaceModelDto {
@ApiProperty({
@ -66,11 +31,11 @@ export class UpdateSpaceModelDto {
@ApiPropertyOptional({
description:
'List of tag modifications (add/update/delete) for the space model',
type: [ModifyTagModelDto],
type: [ModifyTagDto],
})
@IsOptional()
@IsArray()
@ValidateNested({ each: true })
@Type(() => ModifyTagModelDto)
tags?: ModifyTagModelDto[];
@Type(() => ModifyTagDto)
tags?: ModifyTagDto[];
}

View File

@ -1,11 +1,11 @@
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs';
import { PropogateUpdateSpaceModelProductAllocationCommand } from '../commands';
import { SpaceProductAllocationRepository } from '@app/common/modules/space';
import { SubspaceModelProductAllocationRepoitory } from '@app/common/modules/space-model';
import {
SubspaceRepository,
SubspaceProductAllocationRepository,
SubspaceRepository,
} from '@app/common/modules/space/repositories/subspace.repository';
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs';
import { PropogateUpdateSpaceModelProductAllocationCommand } from '../commands';
@CommandHandler(PropogateUpdateSpaceModelProductAllocationCommand)
export class PropogateUpdateSpaceModelProductAllocationHandler
@ -31,89 +31,89 @@ export class PropogateUpdateSpaceModelProductAllocationHandler
console.log(`Processing ${updatedAllocations.length} allocations...`);
for (const allocation of updatedAllocations) {
try {
if (allocation.allocation) {
const spaceAllocations = await this.spaceProductRepository.find({
where: { uuid: allocation.allocation.uuid },
relations: ['tags'],
});
// for (const allocation of updatedAllocations) {
// try {
// if (allocation.allocation) {
// const spaceAllocations = await this.spaceProductRepository.find({
// where: { uuid: allocation.allocation.uuid },
// relations: ['tags'],
// });
if (!spaceAllocations || spaceAllocations.length === 0) {
console.warn(
`No space allocations found for UUID: ${allocation.allocation.uuid}`,
);
continue;
}
// if (!spaceAllocations || spaceAllocations.length === 0) {
// console.warn(
// `No space allocations found for UUID: ${allocation.allocation.uuid}`,
// );
// continue;
// }
if (allocation.tagsAdded?.length) {
for (const spaceAllocation of spaceAllocations) {
spaceAllocation.tags.push(...allocation.tagsAdded);
}
await this.spaceProductRepository.save(spaceAllocations);
console.log(
`Added tags to ${spaceAllocations.length} space allocations.`,
);
}
// if (allocation.tagsAdded?.length) {
// for (const spaceAllocation of spaceAllocations) {
// spaceAllocation.tags.push(...allocation.tagsAdded);
// }
// await this.spaceProductRepository.save(spaceAllocations);
// console.log(
// `Added tags to ${spaceAllocations.length} space allocations.`,
// );
// }
if (allocation.tagsRemoved?.length) {
const tagsToRemoveUUIDs = new Set(
allocation.tagsRemoved.map((tag) => tag.uuid),
);
// if (allocation.tagsRemoved?.length) {
// const tagsToRemoveUUIDs = new Set(
// allocation.tagsRemoved.map((tag) => tag.uuid),
// );
for (const spaceAllocation of spaceAllocations) {
spaceAllocation.tags = spaceAllocation.tags.filter(
(tag) => !tagsToRemoveUUIDs.has(tag.uuid),
);
}
await this.spaceProductRepository.save(spaceAllocations);
console.log(
`Removed tags from ${spaceAllocations.length} space allocations.`,
);
}
}
// for (const spaceAllocation of spaceAllocations) {
// spaceAllocation.tags = spaceAllocation.tags.filter(
// (tag) => !tagsToRemoveUUIDs.has(tag.uuid),
// );
// }
// await this.spaceProductRepository.save(spaceAllocations);
// console.log(
// `Removed tags from ${spaceAllocations.length} space allocations.`,
// );
// }
// }
if (allocation.deletedAllocation) {
const spaceAllocations = await this.spaceProductRepository.find({
where: { uuid: allocation.deletedAllocation.uuid },
relations: ['tags'],
});
// if (allocation.deletedAllocation) {
// const spaceAllocations = await this.spaceProductRepository.find({
// where: { uuid: allocation.deletedAllocation.uuid },
// relations: ['tags'],
// });
if (!spaceAllocations || spaceAllocations.length === 0) {
console.warn(
`No space allocations found to delete for UUID: ${allocation.deletedAllocation.uuid}`,
);
continue;
}
// if (!spaceAllocations || spaceAllocations.length === 0) {
// console.warn(
// `No space allocations found to delete for UUID: ${allocation.deletedAllocation.uuid}`,
// );
// continue;
// }
await this.spaceProductRepository.remove(spaceAllocations);
console.log(
`Deleted ${spaceAllocations.length} space allocations.`,
);
}
// await this.spaceProductRepository.remove(spaceAllocations);
// console.log(
// `Deleted ${spaceAllocations.length} space allocations.`,
// );
// }
if (allocation.newAllocation) {
const newAllocations = spaces.map((space) =>
this.spaceProductRepository.create({
space,
product: allocation.newAllocation.product,
tags: allocation.newAllocation.tags,
inheritedFromModel: allocation.newAllocation,
}),
);
// if (allocation.newAllocation) {
// const newAllocations = spaces.map((space) =>
// this.spaceProductRepository.create({
// space,
// product: allocation.newAllocation.product,
// tag: allocation.newAllocation.tag,
// inheritedFromModel: allocation.newAllocation,
// }),
// );
await this.spaceProductRepository.save(newAllocations);
console.log(
`Created ${newAllocations.length} new space allocations.`,
);
}
} catch (error) {
console.error(
`Error processing allocation update: ${JSON.stringify(allocation)}`,
error,
);
}
}
// await this.spaceProductRepository.save(newAllocations);
// console.log(
// `Created ${newAllocations.length} new space allocations.`,
// );
// }
// } catch (error) {
// console.error(
// `Error processing allocation update: ${JSON.stringify(allocation)}`,
// error,
// );
// }
// }
console.log('Finished processing all allocations.');
} catch (error) {

View File

@ -1,15 +1,14 @@
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs';
import { PropogateUpdateSpaceModelCommand } from '../commands';
import { ModifyAction } from '@app/common/constants/modify-action.enum';
import { SpaceProductAllocationRepository } from '@app/common/modules/space';
import { SubspaceModelProductAllocationRepoitory } from '@app/common/modules/space-model';
import { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
import {
SubspaceProductAllocationRepository,
SubspaceRepository,
} from '@app/common/modules/space/repositories/subspace.repository';
import { SubspaceModelProductAllocationRepoitory } from '@app/common/modules/space-model';
import { In } from 'typeorm';
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs';
import { PropogateUpdateSpaceModelCommand } from '../commands';
import { ISingleSubspaceModel } from '../interfaces';
import { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
import { ModifyAction } from '@app/common/constants/modify-action.enum';
import { IUpdatedAllocations } from '../interfaces/subspace-product-allocation-update-result.interface';
@CommandHandler(PropogateUpdateSpaceModelCommand)
@ -72,48 +71,48 @@ export class PropogateUpdateSpaceModelHandler
for (const allocation of allocations) {
if (!allocation) continue;
if (allocation.allocation) {
try {
const subspaceAllocations =
await this.subspaceProductRepository.find({
where: {
inheritedFromModel: { uuid: allocation.allocation.uuid },
},
relations: ['tags'],
});
// if (allocation.allocation) {
// try {
// const subspaceAllocations =
// await this.subspaceProductRepository.find({
// where: {
// inheritedFromModel: { uuid: allocation.allocation.uuid },
// },
// relations: ['tags'],
// });
if (!subspaceAllocations || subspaceAllocations.length === 0)
continue;
// if (!subspaceAllocations || subspaceAllocations.length === 0)
// continue;
if (allocation.tagsAdded?.length) {
for (const subspaceAllocation of subspaceAllocations) {
subspaceAllocation.tags.push(...allocation.tagsAdded);
}
await this.subspaceProductRepository.save(subspaceAllocations);
console.log(
`Added tags to ${subspaceAllocations.length} subspace allocations.`,
);
}
// if (allocation.tagsAdded?.length) {
// for (const subspaceAllocation of subspaceAllocations) {
// subspaceAllocation.tags.push(...allocation.tagsAdded);
// }
// await this.subspaceProductRepository.save(subspaceAllocations);
// console.log(
// `Added tags to ${subspaceAllocations.length} subspace allocations.`,
// );
// }
if (allocation.tagsRemoved?.length) {
const tagsToRemoveUUIDs = allocation.tagsRemoved.map(
(tag) => tag.uuid,
);
// if (allocation.tagsRemoved?.length) {
// const tagsToRemoveUUIDs = allocation.tagsRemoved.map(
// (tag) => tag.uuid,
// );
for (const subspaceAllocation of subspaceAllocations) {
subspaceAllocation.tags = subspaceAllocation.tags.filter(
(tag) => !tagsToRemoveUUIDs.includes(tag.uuid),
);
}
await this.subspaceProductRepository.save(subspaceAllocations);
console.log(
`Removed tags from ${subspaceAllocations.length} subspace allocations.`,
);
}
} catch (error) {
console.error('Error processing allocation update:', error);
}
}
// for (const subspaceAllocation of subspaceAllocations) {
// subspaceAllocation.tags = subspaceAllocation.tags.filter(
// (tag) => !tagsToRemoveUUIDs.includes(tag.uuid),
// );
// }
// await this.subspaceProductRepository.save(subspaceAllocations);
// console.log(
// `Removed tags from ${subspaceAllocations.length} subspace allocations.`,
// );
// }
// } catch (error) {
// console.error('Error processing allocation update:', error);
// }
// }
if (allocation.newAllocation) {
try {
@ -127,7 +126,7 @@ export class PropogateUpdateSpaceModelHandler
const newAllocations = subspaces.map((subspace) =>
this.subspaceProductRepository.create({
product: allocation.newAllocation.product,
tags: allocation.newAllocation.tags,
tag: allocation.newAllocation.tag,
subspace,
inheritedFromModel: allocation.newAllocation,
}),
@ -198,7 +197,7 @@ export class PropogateUpdateSpaceModelHandler
const subspaceAllocation = this.subspaceProductRepository.create({
subspace: subspace,
product: allocation.product,
tags: allocation.tags,
tag: allocation.tag,
inheritedFromModel: allocation,
});
await this.subspaceProductRepository.save(subspaceAllocation);
@ -211,67 +210,59 @@ export class PropogateUpdateSpaceModelHandler
subspaceModel: ISingleSubspaceModel,
spaces: SpaceEntity[],
) {
const subspaces = await this.subspaceRepository.find({
where: {
subSpaceModel: { uuid: subspaceModel.subspaceModel.uuid },
disabled: false,
},
relations: [
'productAllocations',
'productAllocations.product',
'productAllocations.tags',
],
});
if (!subspaces.length) {
return;
}
const allocationUuidsToRemove = subspaces.flatMap((subspace) =>
subspace.productAllocations.map((allocation) => allocation.uuid),
);
if (allocationUuidsToRemove.length) {
await this.subspaceProductRepository.delete(allocationUuidsToRemove);
}
await this.subspaceRepository.update(
{ uuid: In(subspaces.map((s) => s.uuid)) },
{ disabled: true },
);
const relocatedAllocations = subspaceModel.relocatedAllocations || [];
if (!relocatedAllocations.length) {
return;
}
for (const space of spaces) {
for (const { allocation, tags = [] } of relocatedAllocations) {
const spaceAllocation = await this.spaceProductRepository.findOne({
where: {
inheritedFromModel: { uuid: allocation.uuid },
space: { uuid: space.uuid },
},
relations: ['tags'],
});
if (spaceAllocation) {
if (tags.length) {
spaceAllocation.tags.push(...tags);
await this.spaceProductRepository.save(spaceAllocation);
}
} else {
const newSpaceAllocation = this.spaceProductRepository.create({
space,
inheritedFromModel: allocation,
tags: allocation.tags,
product: allocation.product,
});
await this.spaceProductRepository.save(newSpaceAllocation);
}
}
}
// const subspaces = await this.subspaceRepository.find({
// where: {
// subSpaceModel: { uuid: subspaceModel.subspaceModel.uuid },
// disabled: false,
// },
// relations: [
// 'productAllocations',
// 'productAllocations.product',
// 'productAllocations.tags',
// ],
// });
// if (!subspaces.length) {
// return;
// }
// const allocationUuidsToRemove = subspaces.flatMap((subspace) =>
// subspace.productAllocations.map((allocation) => allocation.uuid),
// );
// if (allocationUuidsToRemove.length) {
// await this.subspaceProductRepository.delete(allocationUuidsToRemove);
// }
// await this.subspaceRepository.update(
// { uuid: In(subspaces.map((s) => s.uuid)) },
// { disabled: true },
// );
// const relocatedAllocations = subspaceModel.relocatedAllocations || [];
// if (!relocatedAllocations.length) {
// return;
// }
// for (const space of spaces) {
// for (const { allocation, tags = [] } of relocatedAllocations) {
// const spaceAllocation = await this.spaceProductRepository.findOne({
// where: {
// inheritedFromModel: { uuid: allocation.uuid },
// space: { uuid: space.uuid },
// },
// relations: ['tags'],
// });
// if (spaceAllocation) {
// if (tags.length) {
// spaceAllocation.tags.push(...tags);
// await this.spaceProductRepository.save(spaceAllocation);
// }
// } else {
// const newSpaceAllocation = this.spaceProductRepository.create({
// space,
// inheritedFromModel: allocation,
// tag: allocation.tag,
// product: allocation.product,
// });
// await this.spaceProductRepository.save(newSpaceAllocation);
// }
// }
// }
}
async updateSubspaceModel(subspaceModel: ISingleSubspaceModel) {

View File

@ -1,4 +1,3 @@
export * from './update-subspace.interface';
export * from './modify-subspace.interface';
export * from './single-subspace.interface';
export * from './space-product-allocation.interface';
export * from './update-subspace.interface';

View File

@ -1,24 +0,0 @@
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

@ -1,11 +1,11 @@
import { ModifyAction } from '@app/common/constants/modify-action.enum';
import {
SpaceModelProductAllocationEntity,
SubspaceModelEntity,
} from '@app/common/modules/space-model';
import { ModifyTagModelDto } from '../dtos';
import { ModifyAction } from '@app/common/constants/modify-action.enum';
import { NewTagEntity } from '@app/common/modules/tag';
import { IUpdatedAllocations } from './subspace-product-allocation-update-result.interface';
import { ModifyTagDto } from 'src/space/dtos/tag/modify-tag.dto';
export interface IRelocatedAllocation {
allocation: SpaceModelProductAllocationEntity;
@ -14,7 +14,7 @@ export interface IRelocatedAllocation {
export interface ISingleSubspaceModel {
subspaceModel: SubspaceModelEntity;
action: ModifyAction;
tags?: ModifyTagModelDto[];
tags?: ModifyTagDto[];
relocatedAllocations?: IRelocatedAllocation[];
}

View File

@ -1,20 +1,19 @@
import { In, QueryRunner } from 'typeorm';
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { In, QueryRunner } from 'typeorm';
import { ProductEntity } from '@app/common/modules/product/entities';
import { ProjectEntity } from '@app/common/modules/project/entities';
import { SpaceRepository } from '@app/common/modules/space';
import {
SpaceModelEntity,
SpaceModelProductAllocationEntity,
SpaceModelProductAllocationRepoitory,
SubspaceModelProductAllocationEntity,
} from '@app/common/modules/space-model';
import { TagService as NewTagService } from 'src/tags/services';
import { ProcessTagDto } from 'src/tags/dtos';
import { ModifySubspaceModelDto, ModifyTagModelDto } from '../dtos';
import { ModifyAction } from '@app/common/constants/modify-action.enum';
import { NewTagEntity } from '@app/common/modules/tag';
import { ProductEntity } from '@app/common/modules/product/entities';
import { SpaceRepository } from '@app/common/modules/space';
import { ProjectEntity } from '@app/common/modules/project/entities';
import { ModifyTagDto } from 'src/space/dtos/tag/modify-tag.dto';
import { ProcessTagDto } from 'src/tags/dtos';
import { TagService as NewTagService } from 'src/tags/services';
import { ModifySubspaceModelDto } from '../dtos';
import { IUpdatedSpaceAllocations } from '../interfaces';
@Injectable()
@ -32,225 +31,220 @@ export class SpaceModelProductAllocationService {
queryRunner?: QueryRunner,
modifySubspaceModels?: ModifySubspaceModelDto[],
): Promise<IUpdatedSpaceAllocations[]> {
try {
if (!tags.length) return [];
// try {
if (!tags.length) return [];
const allocationUpdates: IUpdatedSpaceAllocations[] = [];
const allocationUpdates: IUpdatedSpaceAllocations[] = [];
return allocationUpdates;
// const processedTags = await this.tagService.processTags(
// tags,
// projectUuid,
// queryRunner,
// );
const processedTags = await this.tagService.processTags(
tags,
projectUuid,
queryRunner,
);
// const productAllocations: SpaceModelProductAllocationEntity[] = [];
// const existingAllocations = new Map<
// string,
// SpaceModelProductAllocationEntity
// >();
const productAllocations: SpaceModelProductAllocationEntity[] = [];
const existingAllocations = new Map<
string,
SpaceModelProductAllocationEntity
>();
// for (const tag of processedTags) {
// let isTagNeeded = true;
for (const tag of processedTags) {
let isTagNeeded = true;
// if (modifySubspaceModels) {
// const relatedSubspaces = await queryRunner.manager.find(
// SubspaceModelProductAllocationEntity,
// {
// where: {
// product: { uuid: tag.product.uuid },
// subspaceModel: { spaceModel: { uuid: spaceModel.uuid } },
// tags: { uuid: tag.uuid },
// },
// relations: ['subspaceModel', 'tags'],
// },
// );
if (modifySubspaceModels) {
const relatedSubspaces = await queryRunner.manager.find(
SubspaceModelProductAllocationEntity,
{
where: {
product: { uuid: tag.product.uuid },
subspaceModel: { spaceModel: { uuid: spaceModel.uuid } },
tags: { uuid: tag.uuid },
},
relations: ['subspaceModel', 'tags'],
},
);
// for (const subspaceWithTag of relatedSubspaces) {
// const modifyingSubspace = modifySubspaceModels.find(
// (subspace) =>
// subspace.action === ModifyAction.UPDATE &&
// subspace.uuid === subspaceWithTag.subspaceModel.uuid,
// );
for (const subspaceWithTag of relatedSubspaces) {
const modifyingSubspace = modifySubspaceModels.find(
(subspace) =>
subspace.action === ModifyAction.UPDATE &&
subspace.uuid === subspaceWithTag.subspaceModel.uuid,
);
// if (
// modifyingSubspace &&
// modifyingSubspace.tags &&
// modifyingSubspace.tags.some(
// (subspaceTag) =>
// subspaceTag.action === ModifyAction.DELETE &&
// subspaceTag.tagUuid === tag.uuid,
// )
// ) {
// isTagNeeded = true;
// break;
// }
// }
// }
if (
modifyingSubspace &&
modifyingSubspace.tags &&
modifyingSubspace.tags.some(
(subspaceTag) =>
subspaceTag.action === ModifyAction.DELETE &&
subspaceTag.tagUuid === tag.uuid,
)
) {
isTagNeeded = true;
break;
}
}
}
// if (isTagNeeded) {
// const hasTags = await this.validateTagWithinSpaceModel(
// queryRunner,
// tag,
// spaceModel,
// );
if (isTagNeeded) {
const hasTags = await this.validateTagWithinSpaceModel(
queryRunner,
tag,
spaceModel,
);
// if (hasTags) continue;
if (hasTags) continue;
// let allocation = existingAllocations.get(tag.product.uuid);
// if (!allocation) {
// allocation = await this.getAllocationByProduct(
// tag.product,
// spaceModel,
// queryRunner,
// );
// if (allocation) {
// existingAllocations.set(tag.product.uuid, allocation);
// }
// }
let allocation = existingAllocations.get(tag.product.uuid);
if (!allocation) {
allocation = await this.getAllocationByProduct(
tag.product,
spaceModel,
queryRunner,
);
if (allocation) {
existingAllocations.set(tag.product.uuid, allocation);
}
}
// if (!allocation) {
// allocation = this.createNewAllocation(spaceModel, tag, queryRunner);
// productAllocations.push(allocation);
// allocationUpdates.push({
// newAllocation: allocation,
// });
// } else if (!allocation.tags.some((t) => t.uuid === tag.uuid)) {
// allocation.tags.push(tag);
// allocationUpdates.push({
// allocation: allocation,
// tagsAdded: [tag],
// });
// await this.saveAllocation(allocation, queryRunner);
// }
// }
// }
if (!allocation) {
allocation = this.createNewAllocation(spaceModel, tag, queryRunner);
productAllocations.push(allocation);
allocationUpdates.push({
newAllocation: allocation,
});
} else if (!allocation.tags.some((t) => t.uuid === tag.uuid)) {
allocation.tags.push(tag);
allocationUpdates.push({
allocation: allocation,
tagsAdded: [tag],
});
await this.saveAllocation(allocation, queryRunner);
}
}
}
// if (productAllocations.length > 0) {
// await this.saveAllocations(productAllocations, queryRunner);
// }
if (productAllocations.length > 0) {
await this.saveAllocations(productAllocations, queryRunner);
}
return allocationUpdates;
} catch (error) {
throw this.handleError(error, 'Failed to create product allocations');
}
// return allocationUpdates;
// } catch (error) {
// throw this.handleError(error, 'Failed to create product allocations');
// }
}
async updateProductAllocations(
dtos: ModifyTagModelDto[],
dtos: ModifyTagDto[],
project: ProjectEntity,
spaceModel: SpaceModelEntity,
queryRunner: QueryRunner,
modifySubspaceModels?: ModifySubspaceModelDto[],
): Promise<IUpdatedSpaceAllocations[]> {
try {
const addDtos = dtos.filter((dto) => dto.action === ModifyAction.ADD);
const deleteDtos = dtos.filter(
(dto) => dto.action === ModifyAction.DELETE,
);
const addTagDtos: ProcessTagDto[] = addDtos.map((dto) => ({
name: dto.name,
productUuid: dto.productUuid,
uuid: dto.newTagUuid,
}));
// Process added tags
const processedTags = await this.tagService.processTags(
addTagDtos,
project.uuid,
queryRunner,
);
const addTagUuidMap = new Map<string, ModifyTagModelDto>();
processedTags.forEach((tag, index) => {
addTagUuidMap.set(tag.uuid, addDtos[index]);
});
const addTagUuids = new Set(processedTags.map((tag) => tag.uuid));
const deleteTagUuids = new Set(deleteDtos.map((dto) => dto.tagUuid));
const tagsToIgnore = new Set(
[...addTagUuids].filter((uuid) => deleteTagUuids.has(uuid)),
);
// Filter out tags that are added and deleted in the same request
const filteredDtos = dtos.filter(
(dto) =>
!(
tagsToIgnore.has(dto.tagUuid) ||
(dto.action === ModifyAction.ADD &&
tagsToIgnore.has(
[...addTagUuidMap.keys()].find(
(uuid) => addTagUuidMap.get(uuid) === dto,
),
))
),
);
// Process add and delete actions concurrently
const [updatedAllocations, deletedAllocations] = await Promise.all([
this.processAddActions(
filteredDtos,
project.uuid,
spaceModel,
queryRunner,
modifySubspaceModels,
),
this.processDeleteActions(filteredDtos, queryRunner, spaceModel),
]);
// Combine results and return
return [...updatedAllocations, ...deletedAllocations];
} catch (error) {
throw this.handleError(error, 'Error while updating product allocations');
}
const allocationUpdates: IUpdatedSpaceAllocations[] = [];
return allocationUpdates;
// try {
// const addDtos = dtos.filter((dto) => dto.action === ModifyAction.ADD);
// const deleteDtos = dtos.filter(
// (dto) => dto.action === ModifyAction.DELETE,
// );
// const addTagDtos: ProcessTagDto[] = addDtos.map((dto) => ({
// name: dto.name,
// productUuid: dto.productUuid,
// uuid: dto.newTagUuid,
// }));
// // Process added tags
// const processedTags = await this.tagService.processTags(
// addTagDtos,
// project.uuid,
// queryRunner,
// );
// const addTagUuidMap = new Map<string, ModifyTagModelDto>();
// processedTags.forEach((tag, index) => {
// addTagUuidMap.set(tag.uuid, addDtos[index]);
// });
// const addTagUuids = new Set(processedTags.map((tag) => tag.uuid));
// const deleteTagUuids = new Set(deleteDtos.map((dto) => dto.tagUuid));
// const tagsToIgnore = new Set(
// [...addTagUuids].filter((uuid) => deleteTagUuids.has(uuid)),
// );
// // Filter out tags that are added and deleted in the same request
// const filteredDtos = dtos.filter(
// (dto) =>
// !(
// tagsToIgnore.has(dto.tagUuid) ||
// (dto.action === ModifyAction.ADD &&
// tagsToIgnore.has(
// [...addTagUuidMap.keys()].find(
// (uuid) => addTagUuidMap.get(uuid) === dto,
// ),
// ))
// ),
// );
// // Process add and delete actions concurrently
// const [updatedAllocations, deletedAllocations] = await Promise.all([
// this.processAddActions(
// filteredDtos,
// project.uuid,
// spaceModel,
// queryRunner,
// modifySubspaceModels,
// ),
// this.processDeleteActions(filteredDtos, queryRunner, spaceModel),
// ]);
// // Combine results and return
// return [...updatedAllocations, ...deletedAllocations];
// } catch (error) {
// throw this.handleError(error, 'Error while updating product allocations');
// }
}
private async processAddActions(
dtos: ModifyTagModelDto[],
dtos: ModifyTagDto[],
projectUuid: string,
spaceModel: SpaceModelEntity,
queryRunner: QueryRunner,
modifySubspaceModels?: ModifySubspaceModelDto[],
): Promise<IUpdatedSpaceAllocations[]> {
let allocationUpdates: IUpdatedSpaceAllocations[] = [];
const addDtos: ProcessTagDto[] = dtos
.filter((dto) => dto.action === ModifyAction.ADD)
.map((dto) => ({
name: dto.name,
productUuid: dto.productUuid,
uuid: dto.newTagUuid,
}));
if (addDtos.length > 0) {
allocationUpdates = await this.createProductAllocations(
projectUuid,
spaceModel,
addDtos,
queryRunner,
modifySubspaceModels,
);
}
const allocationUpdates: IUpdatedSpaceAllocations[] = [];
return allocationUpdates;
// const addDtos: ProcessTagDto[] = dtos
// .filter((dto) => dto.action === ModifyAction.ADD)
// .map((dto) => ({
// name: dto.name,
// productUuid: dto.productUuid,
// uuid: dto.newTagUuid,
// }));
// if (addDtos.length > 0) {
// allocationUpdates = await this.createProductAllocations(
// projectUuid,
// spaceModel,
// addDtos,
// queryRunner,
// modifySubspaceModels,
// );
// }
// return allocationUpdates;
}
private createNewAllocation(
spaceModel: SpaceModelEntity,
tag: NewTagEntity,
queryRunner?: QueryRunner,
): SpaceModelProductAllocationEntity {
return queryRunner
? queryRunner.manager.create(SpaceModelProductAllocationEntity, {
spaceModel,
product: tag.product,
tags: [tag],
})
: this.spaceModelProductAllocationRepository.create({
spaceModel,
product: tag.product,
tags: [tag],
});
) {
// : SpaceModelProductAllocationEntity
// return queryRunner
// ? queryRunner.manager.create(SpaceModelProductAllocationEntity, {
// spaceModel,
// product: tag.product,
// tags: [tag],
// })
// : this.spaceModelProductAllocationRepository.create({
// spaceModel,
// product: tag.product,
// tags: [tag],
// });
}
private async getAllocationByProduct(
@ -309,7 +303,7 @@ export class SpaceModelProductAllocationService {
}
private async processDeleteActions(
dtos: ModifyTagModelDto[],
dtos: ModifyTagDto[],
queryRunner: QueryRunner,
spaceModel: SpaceModelEntity,
): Promise<IUpdatedSpaceAllocations[]> {
@ -317,10 +311,10 @@ export class SpaceModelProductAllocationService {
if (!dtos || dtos.length === 0) {
return;
}
let allocationUpdateToPropagate: IUpdatedSpaceAllocations[] = [];
const allocationUpdateToPropagate: IUpdatedSpaceAllocations[] = [];
const tagUuidsToDelete = dtos
.filter((dto) => dto.action === ModifyAction.DELETE && dto.tagUuid)
// .filter((dto) => dto.action === ModifyAction.DELETE && dto.tagUuid)
.map((dto) => dto.tagUuid);
if (tagUuidsToDelete.length === 0) return [];
@ -329,7 +323,7 @@ export class SpaceModelProductAllocationService {
SpaceModelProductAllocationEntity,
{
where: {
tags: { uuid: In(tagUuidsToDelete) },
tag: In(tagUuidsToDelete),
spaceModel: {
uuid: spaceModel.uuid,
},
@ -348,31 +342,28 @@ export class SpaceModelProductAllocationService {
const allocationUpdates: SpaceModelProductAllocationEntity[] = [];
for (const allocation of allocationsToUpdate) {
const updatedTags = allocation.tags.filter(
(tag) => !tagUuidsToDelete.includes(tag.uuid),
);
const deletedTags = allocation.tags.filter((tag) =>
tagUuidsToDelete.includes(tag.uuid),
);
if (updatedTags.length === allocation.tags.length) {
continue;
}
if (updatedTags.length === 0) {
deletedAllocations.push(allocation);
allocationUpdateToPropagate.push({
deletedAllocation: allocation,
});
} else {
allocation.tags = updatedTags;
allocationUpdates.push(allocation);
allocationUpdateToPropagate.push({
allocation: allocation,
tagsRemoved: deletedTags,
});
}
// const updatedTags = allocation.tags.filter(
// (tag) => !tagUuidsToDelete.includes(tag.uuid),
// );
// const deletedTags = allocation.tags.filter((tag) =>
// tagUuidsToDelete.includes(tag.uuid),
// );
// if (updatedTags.length === allocation.tags.length) {
// continue;
// }
// if (updatedTags.length === 0) {
// deletedAllocations.push(allocation);
// allocationUpdateToPropagate.push({
// deletedAllocation: allocation,
// });
// } else {
// allocation.tags = updatedTags;
// allocationUpdates.push(allocation);
// allocationUpdateToPropagate.push({
// allocation: allocation,
// tagsRemoved: deletedTags,
// });
// }
}
if (allocationUpdates.length > 0) {
@ -415,33 +406,34 @@ export class SpaceModelProductAllocationService {
tag: NewTagEntity,
spaceModel: SpaceModelEntity,
): Promise<boolean> {
const existingAllocationsForProduct = await queryRunner.manager.find(
SpaceModelProductAllocationEntity,
{
where: {
spaceModel: {
uuid: spaceModel.uuid,
},
product: {
uuid: tag.product.uuid,
},
},
relations: ['tags'],
},
);
return true;
// const existingAllocationsForProduct = await queryRunner.manager.find(
// SpaceModelProductAllocationEntity,
// {
// where: {
// spaceModel: {
// uuid: spaceModel.uuid,
// },
// product: {
// uuid: tag.product.uuid,
// },
// },
// relations: ['tags'],
// },
// );
const existingTagsForProduct = existingAllocationsForProduct.flatMap(
(allocation) => allocation.tags,
);
// const existingTagsForProduct = existingAllocationsForProduct.flatMap(
// (allocation) => allocation.tags,
// );
const isDuplicateTag = existingTagsForProduct.some(
(existingTag) => existingTag.uuid === tag.uuid,
);
// const isDuplicateTag = existingTagsForProduct.some(
// (existingTag) => existingTag.uuid === tag.uuid,
// );
if (isDuplicateTag) {
return true;
}
return false;
// if (isDuplicateTag) {
// return true;
// }
// return false;
}
async clearAllAllocations(spaceModelUuid: string, queryRunner: QueryRunner) {

View File

@ -1,58 +1,47 @@
import {
SpaceModelEntity,
SpaceModelProductAllocationEntity,
SpaceModelRepository,
SubspaceModelProductAllocationEntity,
} from '@app/common/modules/space-model';
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import {
CreateSpaceModelDto,
LinkSpacesToModelDto,
UpdateSpaceModelDto,
} from '../dtos';
import { ProjectParam } from 'src/community/dtos';
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
import { SubSpaceModelService } from './subspace/subspace-model.service';
import { DataSource, In, QueryRunner, SelectQueryBuilder } from 'typeorm';
import {
TypeORMCustomModel,
TypeORMCustomModelFindAllQuery,
} from '@app/common/models/typeOrmCustom.model';
import { SpaceModelParam } from '../dtos/space-model-param';
import { ProjectService } from 'src/project/services';
import { ProjectEntity } from '@app/common/modules/project/entities';
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
import {
SpaceProductAllocationRepository,
SpaceRepository,
} from '@app/common/modules/space';
import {
SpaceModelEntity,
SpaceModelRepository,
} from '@app/common/modules/space-model';
import { SpaceProductAllocationEntity } from '@app/common/modules/space/entities/space-product-allocation.entity';
import { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
import { SubspaceProductAllocationEntity } from '@app/common/modules/space/entities/subspace/subspace-product-allocation.entity';
import { SubspaceEntity } from '@app/common/modules/space/entities/subspace/subspace.entity';
import {
SubspaceProductAllocationRepository,
SubspaceRepository,
} from '@app/common/modules/space/repositories/subspace.repository';
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { CommandBus } from '@nestjs/cqrs';
import { ProjectParam } from 'src/community/dtos';
import { DeviceService } from 'src/device/services';
import { ProjectService } from 'src/project/services';
import { ProcessTagDto } from 'src/tags/dtos';
import { SpaceModelProductAllocationService } from './space-model-product-allocation.service';
import { DataSource, In, QueryRunner, SelectQueryBuilder } from 'typeorm';
import {
PropogateDeleteSpaceModelCommand,
PropogateUpdateSpaceModelCommand,
PropogateUpdateSpaceModelProductAllocationCommand,
} from '../commands';
import {
SpaceProductAllocationRepository,
SpaceRepository,
} from '@app/common/modules/space';
import { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
import {
SubspaceProductAllocationRepository,
SubspaceRepository,
} from '@app/common/modules/space/repositories/subspace.repository';
import {
ORPHAN_COMMUNITY_NAME,
ORPHAN_SPACE_NAME,
} from '@app/common/constants/orphan-constant';
import { DeviceRepository } from '@app/common/modules/device/repositories';
import { SpaceProductAllocationEntity } from '@app/common/modules/space/entities/space-product-allocation.entity';
import { SubspaceEntity } from '@app/common/modules/space/entities/subspace/subspace.entity';
import { SubspaceProductAllocationEntity } from '@app/common/modules/space/entities/subspace/subspace-product-allocation.entity';
import { DeviceEntity } from '@app/common/modules/device/entities';
import {
ISingleSubspaceModel,
ISubspaceModelUpdates,
IUpdatedSpaceAllocations,
} from '../interfaces';
CreateSpaceModelDto,
LinkSpacesToModelDto,
UpdateSpaceModelDto,
} from '../dtos';
import { SpaceModelParam } from '../dtos/space-model-param';
import { ISubspaceModelUpdates, IUpdatedSpaceAllocations } from '../interfaces';
import { SpaceModelProductAllocationService } from './space-model-product-allocation.service';
import { SubSpaceModelService } from './subspace';
@Injectable()
export class SpaceModelService {
@ -60,6 +49,7 @@ export class SpaceModelService {
private readonly dataSource: DataSource,
private readonly spaceModelRepository: SpaceModelRepository,
private readonly projectService: ProjectService,
private readonly deviceService: DeviceService,
private readonly subSpaceModelService: SubSpaceModelService,
private commandBus: CommandBus,
private readonly spaceModelProductAllocationService: SpaceModelProductAllocationService,
@ -67,7 +57,6 @@ export class SpaceModelService {
private readonly spaceProductAllocationRepository: SpaceProductAllocationRepository,
private readonly subspaceRepository: SubspaceRepository,
private readonly subspaceProductAllocationRepository: SubspaceProductAllocationRepository,
private readonly deviceRepository: DeviceRepository,
) {}
async createSpaceModel(
@ -161,7 +150,7 @@ export class SpaceModelService {
disabled: false,
};
pageable.include =
'subspaceModels.productAllocations,subspaceModelProductAllocations.tags,subspaceModels, productAllocations, productAllocations.tags';
'subspaceModels.productAllocations,subspaceModelProductAllocations.tag,subspaceModelProductAllocations.product,subspaceModels, productAllocations, productAllocations.tag,productAllocations.product';
const queryBuilder = this.buildSpaceModelQuery(param.projectUuid);
@ -231,7 +220,7 @@ export class SpaceModelService {
spaceModel,
queryRunner,
param.projectUuid,
dto.tags,
// dto.tags,
);
}
@ -386,10 +375,10 @@ export class SpaceModelService {
'productAllocations',
'subspaceModels',
'productAllocations.product',
'productAllocations.tags',
'productAllocations.tag',
'subspaceModels.productAllocations',
'subspaceModels.productAllocations.product',
'subspaceModels.productAllocations.tags',
'subspaceModels.productAllocations.tag',
],
});
@ -408,10 +397,10 @@ export class SpaceModelService {
'subspaces',
'productAllocations',
'productAllocations.product',
'productAllocations.tags',
'productAllocations.tag',
'subspaces.productAllocations',
'subspaces.productAllocations.product',
'subspaces.productAllocations.product.tags',
'subspaces.productAllocations.product.tag',
'community',
],
});
@ -433,7 +422,7 @@ export class SpaceModelService {
if (!hasDependencies && !space.spaceModel) {
await this.linkToSpace(space, spaceModel);
} else if (dto.overwrite) {
await this.overwriteSpace(space, project);
await this.removeSpaceOldSubspacesAndAllocations(space, project);
await this.linkToSpace(space, spaceModel);
}
}),
@ -470,10 +459,9 @@ export class SpaceModelService {
space,
inheritedFromModel: modelAllocation,
product: modelAllocation.product,
tags: modelAllocation.tags,
tag: modelAllocation.tag,
}),
);
if (queryRunner) {
await queryRunner.manager.save(
SpaceProductAllocationEntity,
@ -491,23 +479,20 @@ export class SpaceModelService {
subSpaceModel: subspaceModel,
space: space,
});
if (queryRunner) {
await queryRunner.manager.save(SubspaceEntity, subspace);
} else {
await this.subspaceRepository.save(subspace);
}
const subspaceAllocations = subspaceModel.productAllocations.map(
(modelAllocation) =>
this.subspaceProductAllocationRepository.create({
subspace,
inheritedFromModel: modelAllocation,
product: modelAllocation.product,
tags: modelAllocation.tags,
tag: modelAllocation.tag,
}),
);
if (subspaceAllocations.length) {
if (queryRunner) {
await queryRunner.manager.save(
@ -530,7 +515,7 @@ export class SpaceModelService {
}
}
async overwriteSpace(
async removeSpaceOldSubspacesAndAllocations(
space: SpaceEntity,
project: ProjectEntity,
queryRunner?: QueryRunner,
@ -548,14 +533,6 @@ export class SpaceModelService {
? queryRunner.manager.getRepository(SubspaceProductAllocationEntity)
: this.subspaceProductAllocationRepository;
const spaceRepository = queryRunner
? queryRunner.manager.getRepository(SpaceEntity)
: this.spaceRepository;
const deviceRepository = queryRunner
? queryRunner.manager.getRepository(DeviceEntity)
: this.deviceRepository;
if (space.productAllocations.length) {
await spaceProductAllocationRepository.delete({
uuid: In(
@ -584,26 +561,7 @@ export class SpaceModelService {
);
if (space.devices.length > 0) {
const orphanSpace = await spaceRepository.findOne({
where: {
community: {
name: `${ORPHAN_COMMUNITY_NAME}-${project.name}`,
},
spaceName: ORPHAN_SPACE_NAME,
},
});
if (!orphanSpace) {
throw new HttpException(
`Orphan space not found in community ${project.name}`,
HttpStatus.NOT_FOUND,
);
}
await deviceRepository.update(
{ uuid: In(space.devices.map((device) => device.uuid)) },
{ spaceDevice: orphanSpace },
);
this.deviceService.addDevicesToOrphanSpace(space, project, queryRunner);
}
} catch (error) {
throw new Error(
@ -612,19 +570,6 @@ export class SpaceModelService {
}
}
async validateName(modelName: string, projectUuid: string): Promise<void> {
const isModelExist = await this.spaceModelRepository.findOne({
where: { modelName, project: { uuid: projectUuid }, disabled: false },
});
if (isModelExist) {
throw new HttpException(
`Model name ${modelName} already exists in the project with UUID ${projectUuid}.`,
HttpStatus.CONFLICT,
);
}
}
async validateNameUsingQueryRunner(
modelName: string,
projectUuid: string,
@ -726,14 +671,20 @@ export class SpaceModelService {
'subspaceModelProductAllocations',
)
.leftJoinAndSelect(
'subspaceModelProductAllocations.tags',
'subspaceModelTags',
'subspaceModelProductAllocations.tag',
'subspaceModelTag',
)
.leftJoinAndSelect(
'subspaceModelProductAllocations.product',
'subspaceModelProduct',
)
.leftJoinAndSelect('subspaceModelTags.product', 'subspaceModelTagProduct')
.leftJoinAndSelect('spaceModel.productAllocations', 'productAllocations')
.leftJoinAndSelect('productAllocations.product', 'allocatedProduct')
.leftJoinAndSelect('productAllocations.tags', 'productTags')
.leftJoinAndSelect('productTags.product', 'productTagProduct')
.leftJoinAndSelect('productAllocations.tag', 'spaceModelTag')
.leftJoinAndSelect(
'productAllocations.product',
'productAllocationsProduct',
)
.where('spaceModel.disabled = false')
.andWhere('spaceModel.project = :projectUuid', { projectUuid });
}
@ -753,42 +704,12 @@ export class SpaceModelService {
updatedAt: subspace.updatedAt,
subspaceName: subspace.subspaceName,
disabled: subspace.disabled,
tags: this.extractTags(subspace.productAllocations),
productAllocations: subspace.productAllocations,
})),
tags: this.extractTags(spaceModel.productAllocations),
productAllocations: spaceModel.productAllocations,
}));
}
private extractTags(
productAllocations:
| SpaceModelProductAllocationEntity[]
| SubspaceModelProductAllocationEntity[]
| undefined,
): any[] {
if (!productAllocations) return [];
return productAllocations
.flatMap((allocation) => allocation.tags ?? [])
.map((tag) => ({
uuid: tag.uuid,
createdAt: tag.createdAt,
updatedAt: tag.updatedAt,
name: tag.name,
disabled: tag.disabled,
product: tag.product
? {
uuid: tag.product.uuid,
createdAt: tag.product.createdAt,
updatedAt: tag.product.updatedAt,
catName: tag.product.catName,
prodId: tag.product.prodId,
name: tag.product.name,
prodType: tag.product.prodType,
}
: null,
}));
}
private formatSpaceModelResponse(spaceModel: SpaceModelEntity): any {
return {
uuid: spaceModel.uuid,
@ -803,9 +724,9 @@ export class SpaceModelService {
updatedAt: subspace.updatedAt,
subspaceName: subspace.subspaceName,
disabled: subspace.disabled,
tags: this.extractTags(subspace.productAllocations),
productAllocations: subspace.productAllocations,
})) ?? [],
tags: this.extractTags(spaceModel.productAllocations),
productAllocations: spaceModel.productAllocations,
};
}
}

View File

@ -9,10 +9,9 @@ import {
} from '@app/common/modules/space-model';
import { NewTagEntity } from '@app/common/modules/tag';
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { ModifyTagModelDto } from 'src/space-model/dtos';
import { ISingleSubspaceModel } from 'src/space-model/interfaces';
import { IUpdatedAllocations } from 'src/space-model/interfaces/subspace-product-allocation-update-result.interface';
import { ProcessTagDto } from 'src/tags/dtos';
import { ModifyTagDto } from 'src/space/dtos/tag/modify-tag.dto';
import { TagService as NewTagService } from 'src/tags/services';
import { In, QueryRunner } from 'typeorm';
@ -31,172 +30,159 @@ export class SubspaceModelProductAllocationService {
queryRunner?: QueryRunner,
spaceAllocationsToExclude?: SpaceModelProductAllocationEntity[],
): Promise<IUpdatedAllocations[]> {
try {
const updatedAllocations: IUpdatedAllocations[] = [];
const allocations: SubspaceModelProductAllocationEntity[] = [];
for (const tag of tags) {
// Step 1: Check if this specific tag is already allocated at the space level
const existingTagInSpaceModel = await (queryRunner
? queryRunner.manager.findOne(SpaceModelProductAllocationEntity, {
where: {
spaceModel: {
uuid: spaceModel.uuid,
}, // Check at the space level
tags: { uuid: tag.uuid }, // Check for the specific tag
},
relations: ['tags'],
})
: this.spaceModelAllocationRepository.findOne({
where: {
spaceModel: {
uuid: spaceModel.uuid,
},
tags: { uuid: tag.uuid },
},
relations: ['tags', 'product'],
}));
const isExcluded = spaceAllocationsToExclude?.some(
(excludedAllocation) =>
excludedAllocation.product.uuid === tag.product.uuid &&
excludedAllocation.tags.some((t) => t.uuid === tag.uuid),
);
// If tag is found at the space level, prevent allocation at the subspace level
if (!isExcluded && existingTagInSpaceModel) {
throw new HttpException(
`Tag ${tag.uuid} (Product: ${tag.product.uuid}) is already allocated at the space level (${subspaceModel.spaceModel.uuid}). Cannot allocate the same tag in a subspace.`,
HttpStatus.BAD_REQUEST,
);
}
// Check if this specific tag is already allocated within another subspace of the same space
const existingTagInSameSpace = await (queryRunner
? queryRunner.manager.findOne(SubspaceModelProductAllocationEntity, {
where: {
product: { uuid: tag.product.uuid },
subspaceModel: { spaceModel: subspaceModel.spaceModel },
tags: { uuid: tag.uuid }, // Ensure the exact tag is checked
},
relations: ['subspaceModel', 'tags'],
})
: this.subspaceModelProductAllocationRepository.findOne({
where: {
product: { uuid: tag.product.uuid },
subspaceModel: { spaceModel: subspaceModel.spaceModel },
tags: { uuid: tag.uuid },
},
relations: ['subspaceModel', 'tags'],
}));
// Prevent duplicate allocation if tag exists in another subspace of the same space
if (
existingTagInSameSpace &&
existingTagInSameSpace.subspaceModel.uuid !== subspaceModel.uuid
) {
throw new HttpException(
`Tag ${tag.uuid} (Product: ${tag.product.uuid}) is already allocated in another subspace (${existingTagInSameSpace.subspaceModel.uuid}) within the same space (${subspaceModel.spaceModel.uuid}).`,
HttpStatus.BAD_REQUEST,
);
}
//Check if there are existing allocations for this product in the subspace
const existingAllocationsForProduct = await (queryRunner
? queryRunner.manager.find(SubspaceModelProductAllocationEntity, {
where: {
subspaceModel: { uuid: subspaceModel.uuid },
product: { uuid: tag.product.uuid },
},
relations: ['tags'],
})
: this.subspaceModelProductAllocationRepository.find({
where: {
subspaceModel: { uuid: subspaceModel.uuid },
product: { uuid: tag.product.uuid },
},
relations: ['tags'],
}));
//Flatten all existing tags for this product in the subspace
const existingTagsForProduct = existingAllocationsForProduct.flatMap(
(allocation) => allocation.tags,
);
// Check if the tag is already assigned to the same product in this subspace
const isDuplicateTag = existingTagsForProduct.some(
(existingTag) => existingTag.uuid === tag.uuid,
);
if (isDuplicateTag) {
throw new HttpException(
`Tag ${tag.uuid} is already allocated to product ${tag.product.uuid} within this subspace (${subspaceModel.uuid}).`,
HttpStatus.BAD_REQUEST,
);
}
// If no existing allocation, create a new one
if (existingAllocationsForProduct.length === 0) {
const allocation = queryRunner
? queryRunner.manager.create(SubspaceModelProductAllocationEntity, {
subspaceModel,
product: tag.product,
tags: [tag],
})
: this.subspaceModelProductAllocationRepository.create({
subspaceModel,
product: tag.product,
tags: [tag],
});
allocations.push(allocation);
} else {
//If allocation exists, add the tag to it
existingAllocationsForProduct[0].tags.push(tag);
if (queryRunner) {
await queryRunner.manager.save(
SubspaceModelProductAllocationEntity,
existingAllocationsForProduct[0],
);
} else {
await this.subspaceModelProductAllocationRepository.save(
existingAllocationsForProduct[0],
);
}
updatedAllocations.push({
allocation: existingAllocationsForProduct[0],
tagsAdded: [tag],
});
}
}
// Save newly created allocations
if (allocations.length > 0) {
if (queryRunner) {
await queryRunner.manager.save(
SubspaceModelProductAllocationEntity,
allocations,
);
} else {
await this.subspaceModelProductAllocationRepository.save(allocations);
}
allocations.forEach((allocation) => {
updatedAllocations.push({ newAllocation: allocation });
});
}
return updatedAllocations;
} catch (error) {
throw new HttpException(
`An unexpected error occurred while creating subspace product allocations ${error}`,
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
// try {
const updatedAllocations: IUpdatedAllocations[] = [];
return updatedAllocations;
// const allocations: SubspaceModelProductAllocationEntity[] = [];
// for (const tag of tags) {
// // Step 1: Check if this specific tag is already allocated at the space level
// const existingTagInSpaceModel = await (queryRunner
// ? queryRunner.manager.findOne(SpaceModelProductAllocationEntity, {
// where: {
// spaceModel: {
// uuid: spaceModel.uuid,
// }, // Check at the space level
// tags: { uuid: tag.uuid }, // Check for the specific tag
// },
// relations: ['tags'],
// })
// : this.spaceModelAllocationRepository.findOne({
// where: {
// spaceModel: {
// uuid: spaceModel.uuid,
// },
// tags: { uuid: tag.uuid },
// },
// relations: ['tags', 'product'],
// }));
// const isExcluded = spaceAllocationsToExclude?.some(
// (excludedAllocation) =>
// excludedAllocation.product.uuid === tag.product.uuid &&
// excludedAllocation.tags.some((t) => t.uuid === tag.uuid),
// );
// // If tag is found at the space level, prevent allocation at the subspace level
// if (!isExcluded && existingTagInSpaceModel) {
// throw new HttpException(
// `Tag ${tag.uuid} (Product: ${tag.product.uuid}) is already allocated at the space level (${subspaceModel.spaceModel.uuid}). Cannot allocate the same tag in a subspace.`,
// HttpStatus.BAD_REQUEST,
// );
// }
// // Check if this specific tag is already allocated within another subspace of the same space
// const existingTagInSameSpace = await (queryRunner
// ? queryRunner.manager.findOne(SubspaceModelProductAllocationEntity, {
// where: {
// product: { uuid: tag.product.uuid },
// subspaceModel: { spaceModel: subspaceModel.spaceModel },
// tags: { uuid: tag.uuid }, // Ensure the exact tag is checked
// },
// relations: ['subspaceModel', 'tags'],
// })
// : this.subspaceModelProductAllocationRepository.findOne({
// where: {
// product: { uuid: tag.product.uuid },
// subspaceModel: { spaceModel: subspaceModel.spaceModel },
// tags: { uuid: tag.uuid },
// },
// relations: ['subspaceModel', 'tags'],
// }));
// // Prevent duplicate allocation if tag exists in another subspace of the same space
// if (
// existingTagInSameSpace &&
// existingTagInSameSpace.subspaceModel.uuid !== subspaceModel.uuid
// ) {
// throw new HttpException(
// `Tag ${tag.uuid} (Product: ${tag.product.uuid}) is already allocated in another subspace (${existingTagInSameSpace.subspaceModel.uuid}) within the same space (${subspaceModel.spaceModel.uuid}).`,
// HttpStatus.BAD_REQUEST,
// );
// }
// //Check if there are existing allocations for this product in the subspace
// const existingAllocationsForProduct = await (queryRunner
// ? queryRunner.manager.find(SubspaceModelProductAllocationEntity, {
// where: {
// subspaceModel: { uuid: subspaceModel.uuid },
// product: { uuid: tag.product.uuid },
// },
// relations: ['tags'],
// })
// : this.subspaceModelProductAllocationRepository.find({
// where: {
// subspaceModel: { uuid: subspaceModel.uuid },
// product: { uuid: tag.product.uuid },
// },
// relations: ['tags'],
// }));
// //Flatten all existing tags for this product in the subspace
// const existingTagsForProduct = existingAllocationsForProduct.flatMap(
// (allocation) => allocation.tags,
// );
// // Check if the tag is already assigned to the same product in this subspace
// const isDuplicateTag = existingTagsForProduct.some(
// (existingTag) => existingTag.uuid === tag.uuid,
// );
// if (isDuplicateTag) {
// throw new HttpException(
// `Tag ${tag.uuid} is already allocated to product ${tag.product.uuid} within this subspace (${subspaceModel.uuid}).`,
// HttpStatus.BAD_REQUEST,
// );
// }
// // If no existing allocation, create a new one
// if (existingAllocationsForProduct.length === 0) {
// const allocation = queryRunner
// ? queryRunner.manager.create(SubspaceModelProductAllocationEntity, {
// subspaceModel,
// product: tag.product,
// tags: [tag],
// })
// : this.subspaceModelProductAllocationRepository.create({
// subspaceModel,
// product: tag.product,
// tags: [tag],
// });
// allocations.push(allocation);
// } else {
// //If allocation exists, add the tag to it
// existingAllocationsForProduct[0].tags.push(tag);
// if (queryRunner) {
// await queryRunner.manager.save(
// SubspaceModelProductAllocationEntity,
// existingAllocationsForProduct[0],
// );
// } else {
// await this.subspaceModelProductAllocationRepository.save(
// existingAllocationsForProduct[0],
// );
// }
// updatedAllocations.push({
// allocation: existingAllocationsForProduct[0],
// tagsAdded: [tag],
// });
// }
// }
// // Save newly created allocations
// if (allocations.length > 0) {
// if (queryRunner) {
// await queryRunner.manager.save(
// SubspaceModelProductAllocationEntity,
// allocations,
// );
// } else {
// await this.subspaceModelProductAllocationRepository.save(allocations);
// }
// allocations.forEach((allocation) => {
// updatedAllocations.push({ newAllocation: allocation });
// });
// }
// return updatedAllocations;
// } catch (error) {
// throw new HttpException(
// `An unexpected error occurred while creating subspace product allocations ${error}`,
// HttpStatus.INTERNAL_SERVER_ERROR,
// );
// }
}
async processDeleteActions(
dtos: ModifyTagModelDto[],
dtos: ModifyTagDto[],
queryRunner: QueryRunner,
): Promise<SubspaceModelProductAllocationEntity[]> {
try {
@ -205,7 +191,7 @@ export class SubspaceModelProductAllocationService {
}
const tagUuidsToDelete = dtos
.filter((dto) => dto.action === ModifyAction.DELETE && dto.tagUuid)
// .filter((dto) => dto.action === ModifyAction.DELETE && dto.tagUuid)
.map((dto) => dto.tagUuid);
if (tagUuidsToDelete.length === 0) return [];
@ -213,8 +199,7 @@ export class SubspaceModelProductAllocationService {
const allocationsToUpdate = await queryRunner.manager.find(
SubspaceModelProductAllocationEntity,
{
where: { tags: { uuid: In(tagUuidsToDelete) } },
relations: ['tags'],
where: { tag: In(tagUuidsToDelete) },
},
);
@ -224,20 +209,18 @@ export class SubspaceModelProductAllocationService {
const allocationUpdates: SubspaceModelProductAllocationEntity[] = [];
for (const allocation of allocationsToUpdate) {
const updatedTags = allocation.tags.filter(
(tag) => !tagUuidsToDelete.includes(tag.uuid),
);
if (updatedTags.length === allocation.tags.length) {
continue;
}
if (updatedTags.length === 0) {
deletedAllocations.push(allocation);
} else {
allocation.tags = updatedTags;
allocationUpdates.push(allocation);
}
// const updatedTags = allocation.tags.filter(
// (tag) => !tagUuidsToDelete.includes(tag.uuid),
// );
// if (updatedTags.length === allocation.tags.length) {
// continue;
// }
// if (updatedTags.length === 0) {
// deletedAllocations.push(allocation);
// } else {
// allocation.tags = updatedTags;
// allocationUpdates.push(allocation);
// }
}
if (allocationUpdates.length > 0) {
@ -280,254 +263,255 @@ export class SubspaceModelProductAllocationService {
projectUuid: string,
queryRunner: QueryRunner,
spaceModel: SpaceModelEntity,
spaceTagUpdateDtos?: ModifyTagModelDto[],
): Promise<IUpdatedAllocations[]> {
spaceTagUpdateDtos?: ModifyTagDto[],
) {
// : Promise<IUpdatedAllocations[]>
const spaceAllocationToExclude: SpaceModelProductAllocationEntity[] = [];
const updatedAllocations: IUpdatedAllocations[] = [];
return updatedAllocations;
// for (const subspaceModel of subspaceModels) {
// const tagDtos = subspaceModel.tags;
// if (tagDtos.length > 0) {
// const tagsToAddDto: ProcessTagDto[] = tagDtos
// .filter((dto) => dto.action === ModifyAction.ADD)
// .map((dto) => ({
// name: dto.name,
// productUuid: dto.productUuid,
// uuid: dto.newTagUuid,
// }));
for (const subspaceModel of subspaceModels) {
const tagDtos = subspaceModel.tags;
if (tagDtos.length > 0) {
const tagsToAddDto: ProcessTagDto[] = tagDtos
.filter((dto) => dto.action === ModifyAction.ADD)
.map((dto) => ({
name: dto.name,
productUuid: dto.productUuid,
uuid: dto.newTagUuid,
}));
// const tagsToDeleteDto = tagDtos.filter(
// (dto) => dto.action === ModifyAction.DELETE,
// );
const tagsToDeleteDto = tagDtos.filter(
(dto) => dto.action === ModifyAction.DELETE,
);
// if (tagsToAddDto.length > 0) {
// let processedTags = await this.tagService.processTags(
// tagsToAddDto,
// projectUuid,
// queryRunner,
// );
if (tagsToAddDto.length > 0) {
let processedTags = await this.tagService.processTags(
tagsToAddDto,
projectUuid,
queryRunner,
);
// for (const subspaceDto of subspaceModels) {
// if (
// subspaceDto !== subspaceModel &&
// subspaceDto.action === ModifyAction.UPDATE &&
// subspaceDto.tags
// ) {
// // Tag is deleted from one subspace and added in another subspace
// const deletedTags = subspaceDto.tags.filter(
// (tagDto) =>
// tagDto.action === ModifyAction.DELETE &&
// processedTags.some((tag) => tag.uuid === tagDto.tagUuid),
// );
for (const subspaceDto of subspaceModels) {
if (
subspaceDto !== subspaceModel &&
subspaceDto.action === ModifyAction.UPDATE &&
subspaceDto.tags
) {
// Tag is deleted from one subspace and added in another subspace
const deletedTags = subspaceDto.tags.filter(
(tagDto) =>
tagDto.action === ModifyAction.DELETE &&
processedTags.some((tag) => tag.uuid === tagDto.tagUuid),
);
// for (const deletedTag of deletedTags) {
// const allocation = await queryRunner.manager.findOne(
// SubspaceModelProductAllocationEntity,
// {
// where: {
// tags: {
// uuid: deletedTag.tagUuid,
// },
// subspaceModel: {
// uuid: subspaceDto.subspaceModel.uuid,
// },
// },
// relations: ['tags', 'product', 'subspaceModel'],
// },
// );
// if (allocation) {
// const isCommonTag = allocation.tags.some(
// (tag) => tag.uuid === deletedTag.tagUuid,
// );
for (const deletedTag of deletedTags) {
const allocation = await queryRunner.manager.findOne(
SubspaceModelProductAllocationEntity,
{
where: {
tags: {
uuid: deletedTag.tagUuid,
},
subspaceModel: {
uuid: subspaceDto.subspaceModel.uuid,
},
},
relations: ['tags', 'product', 'subspaceModel'],
},
);
if (allocation) {
const isCommonTag = allocation.tags.some(
(tag) => tag.uuid === deletedTag.tagUuid,
);
// if (allocation && isCommonTag) {
// const tagEntity = allocation.tags.find(
// (tag) => tag.uuid === deletedTag.tagUuid,
// );
if (allocation && isCommonTag) {
const tagEntity = allocation.tags.find(
(tag) => tag.uuid === deletedTag.tagUuid,
);
// allocation.tags = allocation.tags.filter(
// (tag) => tag.uuid !== deletedTag.tagUuid,
// );
allocation.tags = allocation.tags.filter(
(tag) => tag.uuid !== deletedTag.tagUuid,
);
// updatedAllocations.push({
// allocation,
// tagsRemoved: [tagEntity],
// });
updatedAllocations.push({
allocation,
tagsRemoved: [tagEntity],
});
// await queryRunner.manager.save(allocation);
await queryRunner.manager.save(allocation);
// const productAllocationExistInSubspace =
// await queryRunner.manager.findOne(
// SubspaceModelProductAllocationEntity,
// {
// where: {
// subspaceModel: {
// uuid: subspaceDto.subspaceModel.uuid,
// },
// product: { uuid: allocation.product.uuid },
// },
// relations: ['tags'],
// },
// );
const productAllocationExistInSubspace =
await queryRunner.manager.findOne(
SubspaceModelProductAllocationEntity,
{
where: {
subspaceModel: {
uuid: subspaceDto.subspaceModel.uuid,
},
product: { uuid: allocation.product.uuid },
},
relations: ['tags'],
},
);
// if (productAllocationExistInSubspace) {
// productAllocationExistInSubspace.tags.push(tagEntity);
if (productAllocationExistInSubspace) {
productAllocationExistInSubspace.tags.push(tagEntity);
// updatedAllocations.push({
// allocation: productAllocationExistInSubspace,
// tagsAdded: [tagEntity],
// });
updatedAllocations.push({
allocation: productAllocationExistInSubspace,
tagsAdded: [tagEntity],
});
// await queryRunner.manager.save(
// productAllocationExistInSubspace,
// );
// } else {
// const newProductAllocation = queryRunner.manager.create(
// SubspaceModelProductAllocationEntity,
// {
// subspaceModel: subspaceModel.subspaceModel,
// product: allocation.product,
// tags: [tagEntity],
// },
// );
await queryRunner.manager.save(
productAllocationExistInSubspace,
);
} else {
const newProductAllocation = queryRunner.manager.create(
SubspaceModelProductAllocationEntity,
{
subspaceModel: subspaceModel.subspaceModel,
product: allocation.product,
tags: [tagEntity],
},
);
// updatedAllocations.push({
// allocation: newProductAllocation,
// });
updatedAllocations.push({
allocation: newProductAllocation,
});
// await queryRunner.manager.save(newProductAllocation);
// }
await queryRunner.manager.save(newProductAllocation);
}
// // Remove the tag from processedTags to prevent duplication
// processedTags = processedTags.filter(
// (tag) => tag.uuid !== deletedTag.tagUuid,
// );
// Remove the tag from processedTags to prevent duplication
processedTags = processedTags.filter(
(tag) => tag.uuid !== deletedTag.tagUuid,
);
// // Remove the tag from subspaceDto.tags to ensure it's not processed again while processing other dtos.
// subspaceDto.tags = subspaceDto.tags.filter(
// (tagDto) => tagDto.tagUuid !== deletedTag.tagUuid,
// );
// }
// }
// }
// }
// if (
// subspaceDto !== subspaceModel &&
// subspaceDto.action === ModifyAction.DELETE
// ) {
// const allocation = await queryRunner.manager.findOne(
// SubspaceModelProductAllocationEntity,
// {
// where: {
// subspaceModel: { uuid: subspaceDto.subspaceModel.uuid },
// },
// relations: ['tags'],
// },
// );
// Remove the tag from subspaceDto.tags to ensure it's not processed again while processing other dtos.
subspaceDto.tags = subspaceDto.tags.filter(
(tagDto) => tagDto.tagUuid !== deletedTag.tagUuid,
);
}
}
}
}
if (
subspaceDto !== subspaceModel &&
subspaceDto.action === ModifyAction.DELETE
) {
const allocation = await queryRunner.manager.findOne(
SubspaceModelProductAllocationEntity,
{
where: {
subspaceModel: { uuid: subspaceDto.subspaceModel.uuid },
},
relations: ['tags'],
},
);
// const repeatedTags = allocation?.tags.filter((tag) =>
// processedTags.some(
// (processedTag) => processedTag.uuid === tag.uuid,
// ),
// );
// if (repeatedTags.length > 0) {
// allocation.tags = allocation.tags.filter(
// (tag) =>
// !repeatedTags.some(
// (repeatedTag) => repeatedTag.uuid === tag.uuid,
// ),
// );
const repeatedTags = allocation?.tags.filter((tag) =>
processedTags.some(
(processedTag) => processedTag.uuid === tag.uuid,
),
);
if (repeatedTags.length > 0) {
allocation.tags = allocation.tags.filter(
(tag) =>
!repeatedTags.some(
(repeatedTag) => repeatedTag.uuid === tag.uuid,
),
);
// updatedAllocations.push({
// allocation: allocation,
// tagsRemoved: repeatedTags,
// });
updatedAllocations.push({
allocation: allocation,
tagsRemoved: repeatedTags,
});
// await queryRunner.manager.save(allocation);
await queryRunner.manager.save(allocation);
// const productAllocationExistInSubspace =
// await queryRunner.manager.findOne(
// SubspaceModelProductAllocationEntity,
// {
// where: {
// subspaceModel: { uuid: subspaceDto.subspaceModel.uuid },
// product: { uuid: allocation.product.uuid },
// },
// relations: ['tags'],
// },
// );
const productAllocationExistInSubspace =
await queryRunner.manager.findOne(
SubspaceModelProductAllocationEntity,
{
where: {
subspaceModel: { uuid: subspaceDto.subspaceModel.uuid },
product: { uuid: allocation.product.uuid },
},
relations: ['tags'],
},
);
// if (productAllocationExistInSubspace) {
// updatedAllocations.push({
// allocation: productAllocationExistInSubspace,
// tagsAdded: repeatedTags,
// });
if (productAllocationExistInSubspace) {
updatedAllocations.push({
allocation: productAllocationExistInSubspace,
tagsAdded: repeatedTags,
});
// productAllocationExistInSubspace.tags.push(...repeatedTags);
// await queryRunner.manager.save(
// productAllocationExistInSubspace,
// );
// } else {
// const newProductAllocation = queryRunner.manager.create(
// SubspaceModelProductAllocationEntity,
// {
// subspaceModel: subspaceModel.subspaceModel,
// product: allocation.product,
// tags: repeatedTags,
// },
// );
productAllocationExistInSubspace.tags.push(...repeatedTags);
await queryRunner.manager.save(
productAllocationExistInSubspace,
);
} else {
const newProductAllocation = queryRunner.manager.create(
SubspaceModelProductAllocationEntity,
{
subspaceModel: subspaceModel.subspaceModel,
product: allocation.product,
tags: repeatedTags,
},
);
// updatedAllocations.push({
// newAllocation: newProductAllocation,
// });
updatedAllocations.push({
newAllocation: newProductAllocation,
});
// await queryRunner.manager.save(newProductAllocation);
// }
// }
// }
// }
// if (spaceTagUpdateDtos) {
// const deletedSpaceTags = spaceTagUpdateDtos.filter(
// (tagDto) =>
// tagDto.action === ModifyAction.DELETE &&
// processedTags.some((tag) => tag.uuid === tagDto.tagUuid),
// );
// for (const deletedTag of deletedSpaceTags) {
// const allocation = await queryRunner.manager.findOne(
// SpaceModelProductAllocationEntity,
// {
// where: {
// spaceModel: { uuid: spaceModel.uuid },
// tags: { uuid: deletedTag.tagUuid },
// },
// relations: ['tags', 'product'],
// },
// );
await queryRunner.manager.save(newProductAllocation);
}
}
}
}
if (spaceTagUpdateDtos) {
const deletedSpaceTags = spaceTagUpdateDtos.filter(
(tagDto) =>
tagDto.action === ModifyAction.DELETE &&
processedTags.some((tag) => tag.uuid === tagDto.tagUuid),
);
for (const deletedTag of deletedSpaceTags) {
const allocation = await queryRunner.manager.findOne(
SpaceModelProductAllocationEntity,
{
where: {
spaceModel: { uuid: spaceModel.uuid },
tags: { uuid: deletedTag.tagUuid },
},
relations: ['tags', 'product'],
},
);
// if (
// allocation &&
// allocation.tags.some((tag) => tag.uuid === deletedTag.tagUuid)
// ) {
// spaceAllocationToExclude.push(allocation);
// }
// }
// }
if (
allocation &&
allocation.tags.some((tag) => tag.uuid === deletedTag.tagUuid)
) {
spaceAllocationToExclude.push(allocation);
}
}
}
// Create new product allocations
const newAllocations = await this.createProductAllocations(
subspaceModel.subspaceModel,
spaceModel,
processedTags,
queryRunner,
spaceAllocationToExclude,
);
return [...updatedAllocations, ...newAllocations];
}
if (tagsToDeleteDto.length > 0) {
await this.processDeleteActions(tagsToDeleteDto, queryRunner);
}
}
}
// // Create new product allocations
// const newAllocations = await this.createProductAllocations(
// subspaceModel.subspaceModel,
// spaceModel,
// processedTags,
// queryRunner,
// spaceAllocationToExclude,
// );
// return [...updatedAllocations, ...newAllocations];
// }
// if (tagsToDeleteDto.length > 0) {
// await this.processDeleteActions(tagsToDeleteDto, queryRunner);
// }
// }
// }
}
async clearAllAllocations(subspaceIds: string[], queryRunner: QueryRunner) {

View File

@ -1,3 +1,4 @@
import { ModifyAction } from '@app/common/constants/modify-action.enum';
import {
SpaceModelEntity,
SpaceModelProductAllocationEntity,
@ -5,21 +6,21 @@ import {
SubspaceModelProductAllocationEntity,
SubspaceModelRepository,
} from '@app/common/modules/space-model';
import { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { CreateSubspaceModelDto, ModifyTagModelDto } from '../../dtos';
import { In, Not, QueryFailedError, QueryRunner } from 'typeorm';
import { ModifySubspaceModelDto } from 'src/space-model/dtos/subspaces-model-dtos';
import { ModifyAction } from '@app/common/constants/modify-action.enum';
import { ProcessTagDto } from 'src/tags/dtos';
import { TagService } from 'src/tags/services';
import { SubspaceModelProductAllocationService } from './subspace-model-product-allocation.service';
import {
IRelocatedAllocation,
ISingleSubspaceModel,
ISubspaceModelUpdates,
} from 'src/space-model/interfaces';
import { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
import { ModifyTagDto } from 'src/space/dtos/tag/modify-tag.dto';
import { SubSpaceService } from 'src/space/services/subspace';
import { ProcessTagDto } from 'src/tags/dtos';
import { TagService } from 'src/tags/services';
import { In, Not, QueryFailedError, QueryRunner } from 'typeorm';
import { CreateSubspaceModelDto } from '../../dtos';
import { SubspaceModelProductAllocationService } from './subspace-model-product-allocation.service';
@Injectable()
export class SubSpaceModelService {
@ -117,7 +118,7 @@ export class SubSpaceModelService {
spaceModel: SpaceModelEntity,
queryRunner: QueryRunner,
projectUuid: string,
spaceTagUpdateDtos?: ModifyTagModelDto[],
spaceTagUpdateDtos?: ModifyTagDto[],
spaces?: SpaceEntity[],
): Promise<ISubspaceModelUpdates> {
try {
@ -152,7 +153,7 @@ export class SubSpaceModelService {
projectUuid,
queryRunner,
spaceModel,
spaceTagUpdateDtos,
// spaceTagUpdateDtos,
);
const deletedSubspaces = await this.deleteSubspaceModels(
@ -188,7 +189,7 @@ export class SubSpaceModelService {
deleteDtos: ModifySubspaceModelDto[],
queryRunner: QueryRunner,
spaceModel: SpaceModelEntity,
spaceTagUpdateDtos?: ModifyTagModelDto[],
spaceTagUpdateDtos?: ModifyTagDto[],
): Promise<ISingleSubspaceModel[]> {
try {
if (!deleteDtos || deleteDtos.length === 0) {
@ -230,92 +231,82 @@ export class SubSpaceModelService {
>();
for (const allocation of allocationsToRemove) {
const product = allocation.product;
const tags = allocation.tags;
const subspaceUuid = allocation.subspaceModel.uuid;
const spaceAllocationKey = `${spaceModel.uuid}-${product.uuid}`;
if (!spaceAllocationsMap.has(spaceAllocationKey)) {
const spaceAllocation = await queryRunner.manager.findOne(
SpaceModelProductAllocationEntity,
{
where: {
spaceModel: { uuid: spaceModel.uuid },
product: { uuid: product.uuid },
},
relations: ['tags'],
},
);
if (spaceAllocation) {
spaceAllocationsMap.set(spaceAllocationKey, spaceAllocation);
}
}
const movedToAlreadyExistingSpaceAllocations: IRelocatedAllocation[] =
[];
const spaceAllocation = spaceAllocationsMap.get(spaceAllocationKey);
if (spaceAllocation) {
const existingTagUuids = new Set(
spaceAllocation.tags.map((tag) => tag.uuid),
);
const newTags = tags.filter(
(tag) => !existingTagUuids.has(tag.uuid),
);
if (newTags.length > 0) {
movedToAlreadyExistingSpaceAllocations.push({
tags: newTags,
allocation: spaceAllocation,
});
spaceAllocation.tags.push(...newTags);
await queryRunner.manager.save(spaceAllocation);
}
} else {
let tagsToAdd = [...tags];
if (spaceTagUpdateDtos && spaceTagUpdateDtos.length > 0) {
const spaceTagDtosToAdd = spaceTagUpdateDtos.filter(
(dto) => dto.action === ModifyAction.ADD,
);
tagsToAdd = tagsToAdd.filter(
(tag) =>
!spaceTagDtosToAdd.some(
(addDto) =>
(addDto.name && addDto.name === tag.name) ||
(addDto.newTagUuid && addDto.newTagUuid === tag.uuid),
),
);
}
if (tagsToAdd.length > 0) {
const newSpaceAllocation = queryRunner.manager.create(
SpaceModelProductAllocationEntity,
{
spaceModel: spaceModel,
product: product,
tags: tags,
},
);
movedToAlreadyExistingSpaceAllocations.push({
allocation: newSpaceAllocation,
tags: tags,
});
await queryRunner.manager.save(newSpaceAllocation);
}
}
if (movedToAlreadyExistingSpaceAllocations.length > 0) {
if (!relocatedAllocationsMap.has(subspaceUuid)) {
relocatedAllocationsMap.set(subspaceUuid, []);
}
relocatedAllocationsMap
.get(subspaceUuid)
.push(...movedToAlreadyExistingSpaceAllocations);
}
// const product = allocation.product;
// const tags = allocation.tags;
// const subspaceUuid = allocation.subspaceModel.uuid;
// const spaceAllocationKey = `${spaceModel.uuid}-${product.uuid}`;
// if (!spaceAllocationsMap.has(spaceAllocationKey)) {
// const spaceAllocation = await queryRunner.manager.findOne(
// SpaceModelProductAllocationEntity,
// {
// where: {
// spaceModel: { uuid: spaceModel.uuid },
// product: { uuid: product.uuid },
// },
// relations: ['tags'],
// },
// );
// if (spaceAllocation) {
// spaceAllocationsMap.set(spaceAllocationKey, spaceAllocation);
// }
// }
// const movedToAlreadyExistingSpaceAllocations: IRelocatedAllocation[] =
// [];
// const spaceAllocation = spaceAllocationsMap.get(spaceAllocationKey);
// if (spaceAllocation) {
// const existingTagUuids = new Set(
// spaceAllocation.tags.map((tag) => tag.uuid),
// );
// const newTags = tags.filter(
// (tag) => !existingTagUuids.has(tag.uuid),
// );
// if (newTags.length > 0) {
// movedToAlreadyExistingSpaceAllocations.push({
// tags: newTags,
// allocation: spaceAllocation,
// });
// spaceAllocation.tags.push(...newTags);
// await queryRunner.manager.save(spaceAllocation);
// }
// } else {
// let tagsToAdd = [...tags];
// if (spaceTagUpdateDtos && spaceTagUpdateDtos.length > 0) {
// const spaceTagDtosToAdd = spaceTagUpdateDtos.filter(
// (dto) => dto.action === ModifyAction.ADD,
// );
// tagsToAdd = tagsToAdd.filter(
// (tag) =>
// !spaceTagDtosToAdd.some(
// (addDto) =>
// (addDto.name && addDto.name === tag.name) ||
// (addDto.newTagUuid && addDto.newTagUuid === tag.uuid),
// ),
// );
// }
// if (tagsToAdd.length > 0) {
// const newSpaceAllocation = queryRunner.manager.create(
// SpaceModelProductAllocationEntity,
// {
// spaceModel: spaceModel,
// product: product,
// tags: tags,
// },
// );
// movedToAlreadyExistingSpaceAllocations.push({
// allocation: newSpaceAllocation,
// tags: tags,
// });
// await queryRunner.manager.save(newSpaceAllocation);
// }
// }
// if (movedToAlreadyExistingSpaceAllocations.length > 0) {
// if (!relocatedAllocationsMap.has(subspaceUuid)) {
// relocatedAllocationsMap.set(subspaceUuid, []);
// }
// relocatedAllocationsMap
// .get(subspaceUuid)
// .push(...movedToAlreadyExistingSpaceAllocations);
// }
}
await queryRunner.manager.remove(
@ -404,7 +395,7 @@ export class SubSpaceModelService {
const updatedSubspaces: {
subspaceModel: SubspaceModelEntity;
tags: ModifyTagModelDto[];
tags: ModifyTagDto[];
action: ModifyAction.UPDATE;
}[] = [];
@ -508,12 +499,6 @@ export class SubSpaceModelService {
return subspaceModels.flatMap((subspace) => subspace.tags || []);
}
extractTagsFromModifiedSubspaceModels(
subspaceModels: ModifySubspaceModelDto[],
): ModifyTagModelDto[] {
return subspaceModels.flatMap((subspace) => subspace.tags || []);
}
private async getSubspacesByUuids(
queryRunner: QueryRunner,
subspaceUuids: string[],

View File

@ -1,34 +1,50 @@
import { SpaceRepositoryModule } from '@app/common/modules/space/space.repository.module';
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { SpaceModelController } from './controllers';
import { SpaceModelService, SubSpaceModelService } from './services';
import { DeviceStatusFirebaseService } from '@app/common/firebase/devices-status/services/devices-status.service';
import { AqiDataService } from '@app/common/helper/services/aqi.data.service';
import { OccupancyService } from '@app/common/helper/services/occupancy.service';
import { PowerClampService } from '@app/common/helper/services/power.clamp.service';
import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service';
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
import { AutomationRepository } from '@app/common/modules/automation/repositories';
import { CommunityRepository } from '@app/common/modules/community/repositories';
import { DeviceStatusLogRepository } from '@app/common/modules/device-status-log/repositories';
import { DeviceRepository } from '@app/common/modules/device/repositories';
import {
SpaceModelProductAllocationRepoitory,
SpaceModelRepository,
SubspaceModelProductAllocationRepoitory,
SubspaceModelRepository,
TagModelRepository,
} from '@app/common/modules/space-model';
import { ProjectRepository } from '@app/common/modules/project/repositiories';
PowerClampDailyRepository,
PowerClampHourlyRepository,
PowerClampMonthlyRepository,
} from '@app/common/modules/power-clamp/repositories';
import { ProductRepository } from '@app/common/modules/product/repositories';
import { ProjectRepository } from '@app/common/modules/project/repositiories';
import { SceneDeviceRepository } from '@app/common/modules/scene-device/repositories';
import {
PropogateDeleteSpaceModelHandler,
PropogateUpdateSpaceModelHandler,
PropogateUpdateSpaceModelProductAllocationHandler,
} from './handlers';
import { CqrsModule } from '@nestjs/cqrs';
SceneIconRepository,
SceneRepository,
} from '@app/common/modules/scene/repositories';
import {
InviteSpaceRepository,
SpaceLinkRepository,
SpaceProductAllocationRepository,
SpaceRepository,
TagRepository,
} from '@app/common/modules/space';
import {
SpaceModelProductAllocationRepoitory,
SpaceModelRepository,
SubspaceModelProductAllocationRepoitory,
SubspaceModelRepository,
} from '@app/common/modules/space-model';
import {
SubspaceProductAllocationRepository,
SubspaceRepository,
} from '@app/common/modules/space/repositories/subspace.repository';
import { SpaceRepositoryModule } from '@app/common/modules/space/space.repository.module';
import { NewTagRepository } from '@app/common/modules/tag/repositories/tag-repository';
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { CqrsModule } from '@nestjs/cqrs';
import { CommunityModule } from 'src/community/community.module';
import { CommunityService } from 'src/community/services';
import { DeviceService } from 'src/device/services';
import { SceneService } from 'src/scene/services';
import {
SpaceLinkService,
SpaceService,
@ -36,37 +52,18 @@ import {
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';
import { TagService as NewTagService } from 'src/tags/services/tags.service';
import { NewTagRepository } from '@app/common/modules/tag/repositories/tag-repository';
import { SpaceModelProductAllocationService } from './services/space-model-product-allocation.service';
import { SubspaceModelProductAllocationService } from './services/subspace/subspace-model-product-allocation.service';
import { SpaceProductAllocationService } from 'src/space/services/space-product-allocation.service';
import { SubspaceProductAllocationService } from 'src/space/services/subspace/subspace-product-allocation.service';
import { DeviceService } from 'src/device/services';
import { SceneDeviceRepository } from '@app/common/modules/scene-device/repositories';
import { DeviceStatusFirebaseService } from '@app/common/firebase/devices-status/services/devices-status.service';
import { SceneService } from 'src/scene/services';
import { DeviceStatusLogRepository } from '@app/common/modules/device-status-log/repositories';
import { TagService as NewTagService } from 'src/tags/services/tags.service';
import { SpaceModelController } from './controllers';
import {
SceneIconRepository,
SceneRepository,
} from '@app/common/modules/scene/repositories';
import { AutomationRepository } from '@app/common/modules/automation/repositories';
import { CommunityModule } from 'src/community/community.module';
import { PowerClampService } from '@app/common/helper/services/power.clamp.service';
import {
PowerClampHourlyRepository,
PowerClampDailyRepository,
PowerClampMonthlyRepository,
} from '@app/common/modules/power-clamp/repositories';
import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service';
import { OccupancyService } from '@app/common/helper/services/occupancy.service';
import { AqiDataService } from '@app/common/helper/services/aqi.data.service';
PropogateDeleteSpaceModelHandler,
PropogateUpdateSpaceModelHandler,
PropogateUpdateSpaceModelProductAllocationHandler,
} from './handlers';
import { SpaceModelService, SubSpaceModelService } from './services';
import { SpaceModelProductAllocationService } from './services/space-model-product-allocation.service';
import { SubspaceModelProductAllocationService } from './services/subspace/subspace-model-product-allocation.service';
const CommandHandlers = [
PropogateUpdateSpaceModelHandler,
@ -88,13 +85,10 @@ const CommandHandlers = [
SubspaceModelRepository,
ProductRepository,
SubspaceRepository,
TagModelRepository,
SubSpaceService,
ValidationService,
TagService,
SubspaceDeviceService,
CommunityService,
TagRepository,
DeviceRepository,
TuyaService,
CommunityRepository,

View File

@ -1,16 +1,19 @@
import { ORPHAN_SPACE_NAME } from '@app/common/constants/orphan-constant';
import { ApiProperty } from '@nestjs/swagger';
import { Type } from 'class-transformer';
import {
ArrayUnique,
IsBoolean,
IsNotEmpty,
IsNumber,
IsOptional,
IsString,
IsUUID,
NotEquals,
ValidateNested,
} from 'class-validator';
import { AddSubspaceDto } from './subspace';
import { ProcessTagDto } from 'src/tags/dtos';
import { AddSubspaceDto } from './subspace';
export class AddSpaceDto {
@ApiProperty({
@ -19,6 +22,11 @@ export class AddSpaceDto {
})
@IsString()
@IsNotEmpty()
@NotEquals(ORPHAN_SPACE_NAME, {
message() {
return `Space name cannot be "${ORPHAN_SPACE_NAME}". Please choose a different name.`;
},
})
spaceName: string;
@ApiProperty({
@ -74,6 +82,20 @@ export class AddSpaceDto {
})
@IsOptional()
@ValidateNested({ each: true })
@ArrayUnique((subspace) => subspace.subspaceName, {
message(validationArguments) {
const subspaces = validationArguments.value;
const nameCounts = subspaces.reduce((acc, curr) => {
acc[curr.subspaceName] = (acc[curr.subspaceName] || 0) + 1;
return acc;
}, {});
// Find duplicates
const duplicates = Object.keys(nameCounts).filter(
(name) => nameCounts[name] > 1,
);
return `Duplicate subspace names found: ${duplicates.join(', ')}`;
},
})
@Type(() => AddSubspaceDto)
subspaces?: AddSubspaceDto[];

View File

@ -0,0 +1,29 @@
import { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
import { SubspaceEntity } from '@app/common/modules/space/entities/subspace/subspace.entity';
import { ProcessTagDto } from 'src/tags/dtos';
import { QueryRunner } from 'typeorm';
export enum AllocationsOwnerType {
SPACE = 'space',
SUBSPACE = 'subspace',
}
export class BaseCreateAllocationsDto {
tags: ProcessTagDto[];
projectUuid: string;
queryRunner: QueryRunner;
type: AllocationsOwnerType;
}
export class CreateSpaceAllocationsDto extends BaseCreateAllocationsDto {
space: SpaceEntity;
type: AllocationsOwnerType.SPACE;
}
export class CreateSubspaceAllocationsDto extends BaseCreateAllocationsDto {
subspace: SubspaceEntity;
type: AllocationsOwnerType.SUBSPACE;
}
export type CreateAllocationsDto =
| CreateSpaceAllocationsDto
| CreateSubspaceAllocationsDto;

View File

@ -1,47 +1,14 @@
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;
import { ApiPropertyOptional, PartialType } from '@nestjs/swagger';
import { IsOptional, IsUUID } from 'class-validator';
import { AddSubspaceDto } from './add.subspace.dto';
export class ModifySubspaceDto extends PartialType(AddSubspaceDto) {
@ApiPropertyOptional({
description: 'UUID of the subspace (required for update/delete)',
description:
'UUID of the subspace (will present if updating an existing subspace)',
example: '123e4567-e89b-12d3-a456-426614174000',
})
@IsOptional()
@IsString()
@IsUUID()
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

@ -1,26 +1,9 @@
import { ModifyAction } from '@app/common/constants/modify-action.enum';
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { IsEnum, IsOptional, IsString, IsUUID } from 'class-validator';
import { ApiPropertyOptional } from '@nestjs/swagger';
import { IsOptional, IsString, IsUUID } from 'class-validator';
export class ModifyTagDto {
@ApiProperty({
description: 'Action to perform: add, update, or delete',
example: ModifyAction.ADD,
})
@IsEnum(ModifyAction)
action: ModifyAction;
@ApiPropertyOptional({
description: 'UUID of the new tag',
example: '123e4567-e89b-12d3-a456-426614174000',
})
@IsOptional()
@IsUUID()
newTagUuid: string;
@ApiPropertyOptional({
description:
'UUID of an existing tag (required for update/delete, optional for add)',
description: 'UUID of an existing tag',
example: 'a1b2c3d4-5678-90ef-abcd-1234567890ef',
})
@IsOptional()
@ -28,7 +11,7 @@ export class ModifyTagDto {
tagUuid?: string;
@ApiPropertyOptional({
description: 'Name of the tag (required for add/update)',
description: 'Name of the tag',
example: 'Temperature Sensor',
})
@IsOptional()
@ -36,8 +19,7 @@ export class ModifyTagDto {
name?: string;
@ApiPropertyOptional({
description:
'UUID of the product associated with the tag (required for add)',
description: 'UUID of the product associated with the tag',
example: 'c789a91e-549a-4753-9006-02f89e8170e0',
})
@IsOptional()

View File

@ -1,13 +1,15 @@
import { ORPHAN_SPACE_NAME } from '@app/common/constants/orphan-constant';
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { Type } from 'class-transformer';
import {
IsArray,
IsNumber,
IsOptional,
IsString,
NotEquals,
ValidateNested,
} from 'class-validator';
import { ModifySubspaceDto } from './subspace';
import { Type } from 'class-transformer';
import { ModifyTagDto } from './tag/modify-tag.dto';
export class UpdateSpaceDto {
@ -17,6 +19,11 @@ export class UpdateSpaceDto {
})
@IsOptional()
@IsString()
@NotEquals(ORPHAN_SPACE_NAME, {
message() {
return `Space name cannot be "${ORPHAN_SPACE_NAME}". Please choose a different name.`;
},
})
spaceName?: string;
@ApiProperty({
@ -46,7 +53,7 @@ export class UpdateSpaceDto {
@IsArray()
@ValidateNested({ each: true })
@Type(() => ModifySubspaceDto)
subspace?: ModifySubspaceDto[];
subspaces?: ModifySubspaceDto[];
@ApiPropertyOptional({
description:
@ -58,6 +65,7 @@ export class UpdateSpaceDto {
@ValidateNested({ each: true })
@Type(() => ModifyTagDto)
tags?: ModifyTagDto[];
@ApiProperty({
description: 'UUID of the Space',
example: 'd290f1ee-6c54-4b01-90e6-d701748f0851',

View File

@ -1,3 +1,4 @@
import { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
import { HttpException, HttpStatus } from '@nestjs/common';
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs';
import { DeviceService } from 'src/device/services';
@ -5,12 +6,10 @@ import { UserSpaceService } from 'src/users/services';
import { DataSource } from 'typeorm';
import { DisableSpaceCommand } from '../commands';
import {
SubSpaceService,
SpaceLinkService,
SpaceSceneService,
SubSpaceService,
} from '../services';
import { TagService } from '../services/tag';
import { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
@CommandHandler(DisableSpaceCommand)
export class DisableSpaceHandler
@ -19,7 +18,6 @@ export class DisableSpaceHandler
constructor(
private readonly subSpaceService: SubSpaceService,
private readonly userService: UserSpaceService,
private readonly tagService: TagService,
private readonly deviceService: DeviceService,
private readonly spaceLinkService: SpaceLinkService,
private readonly sceneService: SpaceSceneService,
@ -46,6 +44,9 @@ export class DisableSpaceHandler
'scenes',
'children',
'userSpaces',
'productAllocations',
'productAllocations.tag',
'productAllocations.product',
],
});
@ -64,15 +65,15 @@ export class DisableSpaceHandler
}
}
const tagUuids = space.productAllocations?.map((tag) => tag.uuid) || [];
const tagUuids =
space.productAllocations?.map(({ tag }) => tag.uuid) || [];
/* const subspaceDtos =
space.subspaces?.map((subspace) => ({
subspaceUuid: subspace.uuid,
})) || []; */
const deletionTasks = [
// this.subSpaceService.deleteSubspaces(subspaceDtos, queryRunner),
this.userService.deleteUserSpace(space.uuid),
this.tagService.deleteTags(tagUuids, queryRunner),
this.deviceService.deleteDevice(
space.devices,
orphanSpace,

View File

@ -1,9 +0,0 @@
import { ModifyAction } from '@app/common/constants/modify-action.enum';
import { SubspaceEntity } from '@app/common/modules/space/entities/subspace/subspace.entity';
import { ModifyTagDto } from '../dtos/tag/modify-tag.dto';
export interface ISingleSubspace {
subspace: SubspaceEntity;
action: ModifyAction;
tags: ModifyTagDto[];
}

View File

@ -0,0 +1,6 @@
import { ProcessTagDto } from 'src/tags/dtos';
export interface UpdateSpaceAllocationDto {
uuid: string;
tags: ProcessTagDto[];
}

View File

@ -0,0 +1,62 @@
import { Injectable } from '@nestjs/common';
import { TagService } from 'src/tags/services';
import {
AllocationsOwnerType,
CreateAllocationsDto,
} from '../../dtos/create-allocations.dto';
import { SpaceProductAllocationService } from '../space-product-allocation.service';
import { SubspaceProductAllocationService } from '../subspace/subspace-product-allocation.service';
@Injectable()
export class ProductAllocationService {
constructor(
private readonly tagService: TagService,
private readonly spaceProductAllocationService: SpaceProductAllocationService,
private readonly subSpaceProductAllocationService: SubspaceProductAllocationService,
) {}
async createAllocations(dto: CreateAllocationsDto): Promise<void> {
const { projectUuid, queryRunner, tags, type } = dto;
const allocationsData = await this.tagService.processTags(
tags,
projectUuid,
queryRunner,
);
// Create a mapping of created tags by UUID and name for quick lookup
const createdTagsByUUID = new Map(allocationsData.map((t) => [t.uuid, t]));
const createdTagsByName = new Map(allocationsData.map((t) => [t.name, t]));
// Create the product-tag mapping based on the processed tags
const productTagMapping = tags.map(({ uuid, name, productUuid }) => {
const inputTag = uuid
? createdTagsByUUID.get(uuid)
: createdTagsByName.get(name);
return {
tag: inputTag?.uuid,
product: productUuid,
};
});
switch (type) {
case AllocationsOwnerType.SPACE: {
// todo: take to consideration original method implementation
await this.spaceProductAllocationService.createProductAllocations(
dto.space,
productTagMapping,
queryRunner,
);
break;
}
case AllocationsOwnerType.SUBSPACE: {
await this.subSpaceProductAllocationService.createProductAllocations(
dto.subspace,
productTagMapping,
queryRunner,
);
break;
}
}
}
}

View File

@ -1,113 +1,37 @@
import { ModifyAction } from '@app/common/constants/modify-action.enum';
import { ProductEntity } from '@app/common/modules/product/entities';
import { SpaceProductAllocationRepository } from '@app/common/modules/space';
import { SpaceProductAllocationEntity } from '@app/common/modules/space/entities/space-product-allocation.entity';
import { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
import { NewTagEntity } from '@app/common/modules/tag';
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { In, QueryRunner } from 'typeorm';
import { SubspaceProductAllocationEntity } from '@app/common/modules/space/entities/subspace/subspace-product-allocation.entity';
import { ModifyTagDto } from '../dtos/tag/modify-tag.dto';
import { ModifySubspaceDto } from '../dtos';
import { ProcessTagDto } from 'src/tags/dtos';
import { TagService as NewTagService } from 'src/tags/services';
import { SpaceModelProductAllocationEntity } from '@app/common/modules/space-model';
import { DeviceEntity } from '@app/common/modules/device/entities';
import { ProjectEntity } from '@app/common/modules/project/entities';
import { ValidationService } from './space-validation.service';
@Injectable()
export class SpaceProductAllocationService {
constructor(
private readonly tagService: NewTagService,
private readonly spaceProductAllocationRepository: SpaceProductAllocationRepository,
private readonly spaceService: ValidationService,
) {}
async createSpaceProductAllocations(
async createProductAllocations(
space: SpaceEntity,
processedTags: NewTagEntity[],
allocationsData: { product: string; tag: string }[],
queryRunner: QueryRunner,
modifySubspaces?: ModifySubspaceDto[],
): Promise<void> {
try {
if (!processedTags.length) return;
if (!allocationsData.length) return;
const productAllocations: SpaceProductAllocationEntity[] = [];
const existingAllocations = new Map<
string,
SpaceProductAllocationEntity
>();
for (const tag of processedTags) {
let isTagNeeded = true;
for (const allocationData of allocationsData) {
if (await this.isAllocationExist(queryRunner, allocationData, space))
continue;
if (modifySubspaces) {
const relatedSubspaces = await queryRunner.manager.find(
SubspaceProductAllocationEntity,
{
where: {
product: tag.product,
subspace: { space: { uuid: space.uuid } },
tags: { uuid: tag.uuid },
},
relations: ['subspace', 'tags'],
},
);
for (const subspaceWithTag of relatedSubspaces) {
const modifyingSubspace = modifySubspaces.find(
(subspace) =>
subspace.action === ModifyAction.UPDATE &&
subspace.uuid === subspaceWithTag.subspace.uuid,
);
if (
modifyingSubspace &&
modifyingSubspace.tags &&
modifyingSubspace.tags.some(
(subspaceTag) =>
subspaceTag.action === ModifyAction.DELETE &&
subspaceTag.tagUuid === tag.uuid,
)
) {
isTagNeeded = true;
break;
}
}
}
if (isTagNeeded) {
const isDuplicated = await this.validateTagWithinSpace(
queryRunner,
tag,
space,
);
if (isDuplicated) continue;
let allocation = existingAllocations.get(tag.product.uuid);
if (!allocation) {
allocation = await this.getAllocationByProduct(
tag.product,
space,
queryRunner,
);
if (allocation) {
existingAllocations.set(tag.product.uuid, allocation);
}
}
if (!allocation) {
allocation = this.createNewAllocation(space, tag, queryRunner);
productAllocations.push(allocation);
} else if (!allocation.tags.some((t) => t.uuid === tag.uuid)) {
allocation.tags.push(tag);
await this.saveAllocation(allocation, queryRunner);
}
}
const allocation = this.createNewAllocation(
space,
allocationData,
queryRunner,
);
productAllocations.push(allocation);
}
if (productAllocations.length > 0) {
await this.saveAllocations(productAllocations, queryRunner);
}
@ -118,97 +42,6 @@ export class SpaceProductAllocationService {
);
}
}
async createAllocationFromModel(
modelAllocations: SpaceModelProductAllocationEntity[],
queryRunner: QueryRunner,
spaces?: SpaceEntity[],
) {
if (!spaces || spaces.length === 0 || !modelAllocations.length) return;
const allocations: SpaceProductAllocationEntity[] = [];
for (const space of spaces) {
for (const modelAllocation of modelAllocations) {
const allocation = queryRunner.manager.create(
SpaceProductAllocationEntity,
{
space,
product: modelAllocation.product,
tags: modelAllocation.tags,
inheritedFromModel: modelAllocation,
},
);
allocations.push(allocation);
}
}
if (allocations.length > 0) {
await queryRunner.manager.save(SpaceProductAllocationEntity, allocations);
}
}
async addTagToAllocationFromModel(
modelAllocation: SpaceModelProductAllocationEntity,
queryRunner: QueryRunner,
tag: NewTagEntity,
spaces?: SpaceEntity[],
) {
try {
if (!spaces || spaces.length === 0 || !modelAllocation) return;
const spaceAllocations = await queryRunner.manager.find(
SpaceProductAllocationEntity,
{
where: { inheritedFromModel: { uuid: modelAllocation.uuid } },
relations: ['tags'],
},
);
if (spaceAllocations.length === 0) return;
for (const allocation of spaceAllocations) {
allocation.tags.push(tag);
}
await queryRunner.manager.save(
SpaceProductAllocationEntity,
spaceAllocations,
);
} catch (error) {
throw new HttpException(
'Failed to add tag to allocation from model',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
async updateSpaceProductAllocations(
dtos: ModifyTagDto[],
projectUuid: string,
space: SpaceEntity,
queryRunner: QueryRunner,
modifySubspace?: ModifySubspaceDto[],
): Promise<void> {
if (!dtos || dtos.length === 0) return;
try {
await Promise.all([
this.processAddActions(
dtos,
projectUuid,
space,
queryRunner,
modifySubspace,
),
this.processDeleteActions(dtos, queryRunner, space),
]);
} catch (error) {
throw this.handleError(error, 'Error while updating product allocations');
}
}
async unlinkModels(space: SpaceEntity, queryRunner: QueryRunner) {
try {
if (!space.productAllocations || space.productAllocations.length === 0)
@ -231,307 +64,30 @@ export class SpaceProductAllocationService {
}
}
async propagateDeleteToInheritedAllocations(
queryRunner: QueryRunner,
allocationsToUpdate: SpaceModelProductAllocationEntity[],
tagUuidsToDelete: string[],
project: ProjectEntity,
spaces?: SpaceEntity[],
): Promise<void> {
try {
const inheritedAllocationUpdates: SpaceProductAllocationEntity[] = [];
const inheritedAllocationsToDelete: SpaceProductAllocationEntity[] = [];
for (const allocation of allocationsToUpdate) {
for (const inheritedAllocation of allocation.inheritedSpaceAllocations) {
const updatedInheritedTags = inheritedAllocation.tags.filter(
(tag) => !tagUuidsToDelete.includes(tag.uuid),
);
if (updatedInheritedTags.length === inheritedAllocation.tags.length) {
continue;
}
if (updatedInheritedTags.length === 0) {
inheritedAllocationsToDelete.push(inheritedAllocation);
} else {
inheritedAllocation.tags = updatedInheritedTags;
inheritedAllocationUpdates.push(inheritedAllocation);
}
}
}
if (inheritedAllocationUpdates.length > 0) {
await queryRunner.manager.save(
SpaceProductAllocationEntity,
inheritedAllocationUpdates,
);
}
if (inheritedAllocationsToDelete.length > 0) {
await queryRunner.manager.remove(
SpaceProductAllocationEntity,
inheritedAllocationsToDelete,
);
}
if (spaces && spaces.length > 0) {
await this.moveDevicesToOrphanSpace(
queryRunner,
spaces,
tagUuidsToDelete,
project,
);
}
await queryRunner.manager
.createQueryBuilder()
.delete()
.from('space_product_tags')
.where(
'space_product_allocation_uuid NOT IN (' +
queryRunner.manager
.createQueryBuilder()
.select('allocation.uuid')
.from(SpaceProductAllocationEntity, 'allocation')
.getQuery() +
')',
)
.execute();
} catch (error) {
throw this.handleError(
error,
`Failed to propagate tag deletion to inherited allocations`,
);
}
}
async moveDevicesToOrphanSpace(
queryRunner: QueryRunner,
spaces: SpaceEntity[],
tagUuidsToDelete: string[],
project: ProjectEntity,
): Promise<void> {
try {
const orphanSpace = await this.spaceService.getOrphanSpace(project);
const devicesToMove = await queryRunner.manager
.createQueryBuilder(DeviceEntity, 'device')
.leftJoinAndSelect('device.tag', 'tag')
.where('device.spaceDevice IN (:...spaceUuids)', {
spaceUuids: spaces.map((space) => space.uuid),
})
.andWhere('tag.uuid IN (:...tagUuidsToDelete)', { tagUuidsToDelete })
.getMany();
if (devicesToMove.length === 0) return;
await queryRunner.manager
.createQueryBuilder()
.update(DeviceEntity)
.set({ spaceDevice: orphanSpace })
.where('uuid IN (:...deviceUuids)', {
deviceUuids: devicesToMove.map((device) => device.uuid),
})
.execute();
} catch (error) {
throw this.handleError(error, `Failed to move devices to orphan space`);
}
}
private async processDeleteActions(
dtos: ModifyTagDto[],
private async isAllocationExist(
queryRunner: QueryRunner,
allocation: { product: string; tag: string },
space: SpaceEntity,
): Promise<SpaceProductAllocationEntity[]> {
try {
if (!dtos || dtos.length === 0) return;
const tagUuidsToDelete = dtos
.filter((dto) => dto.action === ModifyAction.DELETE && dto.tagUuid)
.map((dto) => dto.tagUuid);
if (tagUuidsToDelete.length === 0) return [];
const allocationsToUpdate = await queryRunner.manager.find(
): Promise<boolean> {
const existingAllocations =
await queryRunner.manager.findOne<SpaceProductAllocationEntity>(
SpaceProductAllocationEntity,
{
where: {
tags: { uuid: In(tagUuidsToDelete) },
space: { uuid: space.uuid },
},
relations: ['tags'],
},
);
if (!allocationsToUpdate || allocationsToUpdate.length === 0) return [];
const deletedAllocations: SpaceProductAllocationEntity[] = [];
const allocationUpdates: SpaceProductAllocationEntity[] = [];
for (const allocation of allocationsToUpdate) {
const updatedTags = allocation.tags.filter(
(tag) => !tagUuidsToDelete.includes(tag.uuid),
);
if (updatedTags.length === allocation.tags.length) {
continue;
}
if (updatedTags.length === 0) {
deletedAllocations.push(allocation);
} else {
allocation.tags = updatedTags;
allocationUpdates.push(allocation);
}
}
if (allocationUpdates.length > 0) {
await queryRunner.manager.save(
SpaceProductAllocationEntity,
allocationUpdates,
);
}
if (deletedAllocations.length > 0) {
await queryRunner.manager.remove(
SpaceProductAllocationEntity,
deletedAllocations,
);
}
await queryRunner.manager
.createQueryBuilder()
.delete()
.from('space_product_tags')
.where(
'space_product_allocation_uuid NOT IN (' +
queryRunner.manager
.createQueryBuilder()
.select('allocation.uuid')
.from(SpaceProductAllocationEntity, 'allocation')
.getQuery() +
')',
)
.execute();
return deletedAllocations;
} catch (error) {
throw this.handleError(error, `Failed to delete tags in space`);
}
}
private async processAddActions(
dtos: ModifyTagDto[],
projectUuid: string,
space: SpaceEntity,
queryRunner: QueryRunner,
modifySubspace?: ModifySubspaceDto[],
): Promise<void> {
const addDtos: ProcessTagDto[] = dtos
.filter((dto) => dto.action === ModifyAction.ADD)
.map((dto) => ({
name: dto.name,
productUuid: dto.productUuid,
uuid: dto.newTagUuid,
}));
if (addDtos.length > 0) {
const processedTags = await this.tagService.processTags(
addDtos,
projectUuid,
queryRunner,
);
await this.createSpaceProductAllocations(
space,
processedTags,
queryRunner,
modifySubspace,
);
}
}
private async validateTagWithinSpace(
queryRunner: QueryRunner,
tag: NewTagEntity,
space: SpaceEntity,
): Promise<boolean> {
const existingAllocationsForProduct = await queryRunner.manager.find(
SpaceProductAllocationEntity,
{
where: {
space: {
uuid: space.uuid,
},
product: {
uuid: tag.product.uuid,
space: {
uuid: space.uuid,
},
tag: {
uuid: allocation.tag,
},
product: {
uuid: allocation.product,
},
},
},
relations: ['tags'],
},
);
);
if (
!existingAllocationsForProduct ||
existingAllocationsForProduct.length === 0
) {
return false;
}
const existingTagsForProduct = existingAllocationsForProduct.flatMap(
(allocation) => allocation.tags || [],
);
return existingTagsForProduct.some(
(existingTag) => existingTag.uuid === tag.uuid,
);
}
private async getAllocationByProduct(
product: ProductEntity,
space: SpaceEntity,
queryRunner?: QueryRunner,
): Promise<SpaceProductAllocationEntity | null> {
return queryRunner
? queryRunner.manager.findOne(SpaceProductAllocationEntity, {
where: {
space: { uuid: space.uuid },
product: { uuid: product.uuid },
},
relations: ['tags'],
})
: this.spaceProductAllocationRepository.findOne({
where: {
space: { uuid: space.uuid },
product: { uuid: product.uuid },
},
relations: ['tags'],
});
}
createNewAllocation(
space: SpaceEntity,
tag: NewTagEntity,
queryRunner?: QueryRunner,
): SpaceProductAllocationEntity {
return queryRunner
? queryRunner.manager.create(SpaceProductAllocationEntity, {
space,
product: tag.product,
tags: [tag],
})
: this.spaceProductAllocationRepository.create({
space,
product: tag.product,
tags: [tag],
});
}
private async saveAllocation(
allocation: SpaceProductAllocationEntity,
queryRunner?: QueryRunner,
) {
queryRunner
? await queryRunner.manager.save(SpaceProductAllocationEntity, allocation)
: await this.spaceProductAllocationRepository.save(allocation);
return existingAllocations ? true : false;
}
async saveAllocations(
@ -546,6 +102,34 @@ export class SpaceProductAllocationService {
: await this.spaceProductAllocationRepository.save(allocations);
}
async clearAllAllocations(spaceUuid: string, queryRunner: QueryRunner) {
try {
await queryRunner.manager.delete(SpaceProductAllocationEntity, {
space: { uuid: spaceUuid },
});
} catch (error) {
throw this.handleError(error, 'Failed to clear all allocations');
}
}
private createNewAllocation(
space: SpaceEntity,
allocationData: { product: string; tag: string },
queryRunner?: QueryRunner,
): SpaceProductAllocationEntity {
return queryRunner
? queryRunner.manager.create(SpaceProductAllocationEntity, {
space,
product: { uuid: allocationData.product },
tag: { uuid: allocationData.tag },
})
: this.spaceProductAllocationRepository.create({
space,
product: { uuid: allocationData.product },
tag: { uuid: allocationData.tag },
});
}
private handleError(error: any, message: string): HttpException {
return new HttpException(
error instanceof HttpException ? error.message : message,
@ -554,36 +138,4 @@ export class SpaceProductAllocationService {
: HttpStatus.INTERNAL_SERVER_ERROR,
);
}
async clearAllAllocations(spaceUuid: string, queryRunner: QueryRunner) {
try {
const allocationUuids = await queryRunner.manager
.createQueryBuilder(SpaceProductAllocationEntity, 'allocation')
.select('allocation.uuid')
.where('allocation.space_uuid = :spaceUuid', { spaceUuid })
.getRawMany()
.then((results) => results.map((r) => r.allocation_uuid));
if (allocationUuids.length === 0) {
return;
}
await queryRunner.manager
.createQueryBuilder()
.delete()
.from('space_product_tags')
.where('space_product_allocation_uuid IN (:...allocationUuids)', {
allocationUuids,
})
.execute();
await queryRunner.manager
.createQueryBuilder()
.delete()
.from(SpaceProductAllocationEntity)
.where('space_uuid = :spaceUuid', { spaceUuid })
.execute();
} catch (error) {
throw this.handleError(error, 'Failed to clear all allocations');
}
}
}

View File

@ -1,3 +1,12 @@
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
import { CommunityRepository } from '@app/common/modules/community/repositories';
import { DeviceRepository } from '@app/common/modules/device/repositories';
import { ProjectRepository } from '@app/common/modules/project/repositiories';
import {
SpaceModelEntity,
SpaceModelRepository,
} from '@app/common/modules/space-model';
import { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
import { SpaceRepository } from '@app/common/modules/space/repositories';
import {
BadRequestException,
@ -7,25 +16,11 @@ import {
Inject,
Injectable,
} from '@nestjs/common';
import { In } from 'typeorm';
import { CommunityService } from '../../community/services';
import { ProjectService } from '../../project/services';
import {
SpaceModelEntity,
SpaceModelRepository,
} from '@app/common/modules/space-model';
import { ProjectRepository } from '@app/common/modules/project/repositiories';
import { CommunityRepository } from '@app/common/modules/community/repositories';
import { DeviceRepository } from '@app/common/modules/device/repositories';
import { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
import { ValidateSpacesDto } from '../dtos/validation.space.dto';
import { ProjectParam } from '../dtos';
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
import { In } from 'typeorm';
import {
ORPHAN_COMMUNITY_NAME,
ORPHAN_SPACE_NAME,
} from '@app/common/constants/orphan-constant';
import { ProjectEntity } from '@app/common/modules/project/entities';
import { ValidateSpacesDto } from '../dtos/validation.space.dto';
@Injectable()
export class ValidationService {
@ -130,9 +125,7 @@ export class ValidationService {
'subspaces',
'productAllocations',
'productAllocations.product',
'productAllocations.tags',
'subspaces.productAllocations',
'subspaces.productAllocations.tags',
'subspaces.productAllocations.product',
'subspaces.devices',
'spaceModel',
@ -146,13 +139,13 @@ export class ValidationService {
);
}
const devices = await this.deviceRepository.find({
where: { spaceDevice: { uuid: spaceUuid } },
select: ['uuid', 'deviceTuyaUuid', 'isActive', 'createdAt', 'updatedAt'],
relations: ['productDevice', 'subspace'],
});
// const devices = await this.deviceRepository.find({
// where: { spaceDevice: { uuid: spaceUuid } },
// select: ['uuid', 'deviceTuyaUuid', 'isActive', 'createdAt', 'updatedAt'],
// relations: ['productDevice', 'subspace'],
// });
space.devices = devices;
// space.devices = devices;
return space;
}
@ -191,8 +184,8 @@ export class ValidationService {
'subspaceProductAllocations',
)
.leftJoinAndSelect(
'subspaceProductAllocations.tags',
'subspaceAllocationTags',
'subspaceProductAllocations.tag',
'subspaceAllocationTag',
)
.leftJoinAndSelect(
'subspaceProductAllocations.product',
@ -203,7 +196,7 @@ export class ValidationService {
'productAllocations.product',
'productAllocationProduct',
)
.leftJoinAndSelect('productAllocations.tags', 'productAllocationTags')
.leftJoinAndSelect('productAllocations.tag', 'productAllocationTag')
.andWhere('spaceModel.disabled = :disabled', { disabled: false })
.where('spaceModel.uuid = :uuid', { uuid: spaceModelUuid });
@ -219,39 +212,6 @@ export class ValidationService {
return spaceModel;
}
async getFullSpaceHierarchy(
space: SpaceEntity,
): Promise<{ uuid: string; spaceName: string }[]> {
try {
// Fetch only the relevant spaces, starting with the target space
const targetSpace = await this.spaceRepository.findOne({
where: { uuid: space.uuid },
relations: ['parent', 'children'],
});
// Fetch only the ancestors of the target space
const ancestors = await this.fetchAncestors(targetSpace);
// Optionally, fetch descendants if required
const descendants = await this.fetchDescendants(targetSpace);
const fullHierarchy = [...ancestors, targetSpace, ...descendants].map(
(space) => ({
uuid: space.uuid,
spaceName: space.spaceName,
}),
);
return fullHierarchy;
} catch (error) {
console.error('Error fetching space hierarchy:', error.message);
throw new HttpException(
'Error fetching space hierarchy',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
private async fetchAncestors(space: SpaceEntity): Promise<SpaceEntity[]> {
const ancestors: SpaceEntity[] = [];
@ -275,27 +235,6 @@ export class ValidationService {
return ancestors.reverse();
}
private async fetchDescendants(space: SpaceEntity): Promise<SpaceEntity[]> {
const descendants: SpaceEntity[] = [];
// Fetch the immediate children of the current space
const children = await this.spaceRepository.find({
where: { parent: { uuid: space.uuid } },
relations: ['children'], // To continue fetching downwards
});
for (const child of children) {
// Add the child to the descendants list
descendants.push(child);
// Recursively fetch the child's descendants
const childDescendants = await this.fetchDescendants(child);
descendants.push(...childDescendants);
}
return descendants;
}
async getParentHierarchy(
space: SpaceEntity,
): Promise<{ uuid: string; spaceName: string }[]> {
@ -323,24 +262,4 @@ export class ValidationService {
);
}
}
async getOrphanSpace(project: ProjectEntity): Promise<SpaceEntity> {
const orphanSpace = await this.spaceRepository.findOne({
where: {
community: {
name: `${ORPHAN_COMMUNITY_NAME}-${project.name}`,
},
spaceName: ORPHAN_SPACE_NAME,
},
});
if (!orphanSpace) {
throw new HttpException(
`Orphan space not found in community ${project.name}`,
HttpStatus.NOT_FOUND,
);
}
return orphanSpace;
}
}

View File

@ -1,3 +1,14 @@
import {
ORPHAN_COMMUNITY_NAME,
ORPHAN_SPACE_NAME,
} from '@app/common/constants/orphan-constant';
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
import { generateRandomString } from '@app/common/helper/randomString';
import { removeCircularReferences } from '@app/common/helper/removeCircularReferences';
import { SpaceProductAllocationEntity } from '@app/common/modules/space/entities/space-product-allocation.entity';
import { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
import { SubspaceEntity } from '@app/common/modules/space/entities/subspace/subspace.entity';
import {
InviteSpaceRepository,
SpaceRepository,
@ -8,36 +19,25 @@ import {
HttpStatus,
Injectable,
} from '@nestjs/common';
import { CommandBus } from '@nestjs/cqrs';
import { DeviceService } from 'src/device/services';
import { SpaceModelService } from 'src/space-model/services';
import { ProcessTagDto } from 'src/tags/dtos';
import { TagService } from 'src/tags/services/tags.service';
import { DataSource, In, Not, QueryRunner } from 'typeorm';
import { DisableSpaceCommand } from '../commands';
import {
AddSpaceDto,
AddSubspaceDto,
CommunitySpaceParam,
GetSpaceParam,
UpdateSpaceDto,
} from '../dtos';
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
import { generateRandomString } from '@app/common/helper/randomString';
import { SpaceLinkService } from './space-link';
import { SubSpaceService } from './subspace';
import { DataSource, QueryRunner } from 'typeorm';
import { ValidationService } from './space-validation.service';
import {
ORPHAN_COMMUNITY_NAME,
ORPHAN_SPACE_NAME,
} from '@app/common/constants/orphan-constant';
import { CommandBus } from '@nestjs/cqrs';
import { TagService as NewTagService } from 'src/tags/services/tags.service';
import { SpaceModelService } from 'src/space-model/services';
import { DisableSpaceCommand } from '../commands';
import { GetSpaceDto } from '../dtos/get.space.dto';
import { removeCircularReferences } from '@app/common/helper/removeCircularReferences';
import { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
import { ProcessTagDto } from 'src/tags/dtos';
import { SpaceProductAllocationService } from './space-product-allocation.service';
import { SubspaceEntity } from '@app/common/modules/space/entities/subspace/subspace.entity';
import { DeviceService } from 'src/device/services';
import { SpaceWithParentsDto } from '../dtos/space.parents.dto';
import { SpaceLinkService } from './space-link';
import { SpaceProductAllocationService } from './space-product-allocation.service';
import { ValidationService } from './space-validation.service';
import { SubSpaceService } from './subspace';
@Injectable()
export class SpaceService {
constructor(
@ -47,7 +47,7 @@ export class SpaceService {
private readonly spaceLinkService: SpaceLinkService,
private readonly subSpaceService: SubSpaceService,
private readonly validationService: ValidationService,
private readonly newTagService: NewTagService,
private readonly tagService: TagService,
private readonly spaceModelService: SpaceModelService,
private readonly deviceService: DeviceService,
private commandBus: CommandBus,
@ -62,13 +62,6 @@ export class SpaceService {
addSpaceDto;
const { communityUuid, projectUuid } = params;
if (addSpaceDto.spaceName === ORPHAN_SPACE_NAME) {
throw new HttpException(
`Name ${ORPHAN_SPACE_NAME} cannot be used`,
HttpStatus.BAD_REQUEST,
);
}
const queryRunner = this.dataSource.createQueryRunner();
await queryRunner.connect();
@ -80,7 +73,7 @@ export class SpaceService {
projectUuid,
);
this.validateSpaceCreation(addSpaceDto, spaceModelUuid);
this.validateSpaceCreationCriteria({ spaceModelUuid, subspaces, tags });
const parent = parentUuid
? await this.validationService.validateSpace(parentUuid)
@ -99,27 +92,24 @@ export class SpaceService {
});
const newSpace = await queryRunner.manager.save(space);
const subspaceTags =
this.subSpaceService.extractTagsFromSubspace(subspaces);
const allTags = [...tags, ...subspaceTags];
this.validateUniqueTags(allTags);
subspaces?.flatMap((subspace) => subspace.tags || []) || [];
this.checkDuplicateTags([...tags, ...subspaceTags]);
if (spaceModelUuid) {
const hasDependencies = subspaces?.length > 0 || tags?.length > 0;
if (!hasDependencies) {
await this.spaceModelService.linkToSpace(
newSpace,
spaceModel,
queryRunner,
);
} else if (hasDependencies) {
throw new HttpException(
`Space cannot be linked to a model because it has existing dependencies (subspaces or tags).`,
HttpStatus.BAD_REQUEST,
);
}
// no need to check for existing dependencies here as validateSpaceCreationCriteria
// ensures no tags or subspaces are present along with spaceModelUuid
await this.spaceModelService.linkToSpace(
newSpace,
spaceModel,
queryRunner,
);
}
await Promise.all([
// todo: remove this logic as we are not using space links anymore
direction && parent
? this.spaceLinkService.saveSpaceLink(
parent.uuid,
@ -129,16 +119,15 @@ export class SpaceService {
)
: Promise.resolve(),
subspaces?.length
? this.createSubspaces(
? this.subSpaceService.createSubspacesFromDto(
subspaces,
newSpace,
space,
queryRunner,
null,
projectUuid,
)
: Promise.resolve(),
tags?.length
? this.createTags(tags, projectUuid, queryRunner, newSpace)
? this.createAllocations(tags, projectUuid, queryRunner, newSpace)
: Promise.resolve(),
]);
@ -160,7 +149,7 @@ export class SpaceService {
await queryRunner.release();
}
}
private validateUniqueTags(allTags: ProcessTagDto[]) {
private checkDuplicateTags(allTags: ProcessTagDto[]) {
const tagUuidSet = new Set<string>();
const tagNameProductSet = new Set<string>();
@ -213,8 +202,8 @@ export class SpaceService {
{ incomingConnectionDisabled: false },
)
.leftJoinAndSelect('space.productAllocations', 'productAllocations')
// .leftJoinAndSelect('productAllocations.tags', 'tags')
// .leftJoinAndSelect('productAllocations.product', 'product')
.leftJoinAndSelect('productAllocations.tag', 'tag')
.leftJoinAndSelect('productAllocations.product', 'product')
.leftJoinAndSelect(
'space.subspaces',
'subspaces',
@ -225,11 +214,11 @@ export class SpaceService {
'subspaces.productAllocations',
'subspaceProductAllocations',
)
// .leftJoinAndSelect('subspaceProductAllocations.tags', 'subspaceTag')
// .leftJoinAndSelect(
// 'subspaceProductAllocations.product',
// 'subspaceProduct',
// )
.leftJoinAndSelect('subspaceProductAllocations.tag', 'subspaceTag')
.leftJoinAndSelect(
'subspaceProductAllocations.product',
'subspaceProduct',
)
.leftJoinAndSelect('space.spaceModel', 'spaceModel')
.where('space.community_id = :communityUuid', { communityUuid })
.andWhere('space.spaceName != :orphanSpaceName', {
@ -282,28 +271,7 @@ export class SpaceService {
}
}
private transformSpace(space) {
const { productAllocations, subspaces, ...restSpace } = space;
const tags = productAllocations.flatMap((pa) => pa.tags);
const transformedSubspaces = subspaces.map((subspace) => {
const {
productAllocations: subspaceProductAllocations,
...restSubspace
} = subspace;
const subspaceTags = subspaceProductAllocations.flatMap((pa) => pa.tags);
return {
...restSubspace,
tags: subspaceTags,
};
});
return {
...restSpace,
tags,
subspaces: transformedSubspaces,
};
}
// todo refactor this method to eliminate wrong use of tags
async findOne(params: GetSpaceParam): Promise<BaseResponseDto> {
const { communityUuid, spaceUuid, projectUuid } = params;
try {
@ -327,13 +295,9 @@ export class SpaceService {
'incomingConnections.disabled = :incomingConnectionDisabled',
{ incomingConnectionDisabled: false },
)
// .leftJoinAndSelect(
// 'space.tags',
// 'tags',
// 'tags.disabled = :tagDisabled',
// { tagDisabled: false },
// )
// .leftJoinAndSelect('tags.product', 'tagProduct')
.leftJoinAndSelect('space.productAllocations', 'productAllocations')
.leftJoinAndSelect('productAllocations.tag', 'spaceTag')
.leftJoinAndSelect('productAllocations.product', 'spaceProduct')
.leftJoinAndSelect(
'space.subspaces',
'subspaces',
@ -341,12 +305,14 @@ export class SpaceService {
{ subspaceDisabled: false },
)
.leftJoinAndSelect(
'subspaces.tags',
'subspaceTags',
'subspaceTags.disabled = :subspaceTagsDisabled',
{ subspaceTagsDisabled: false },
'subspaces.productAllocations',
'subspaceProductAllocations',
)
.leftJoinAndSelect('subspaceProductAllocations.tag', 'subspaceTag')
.leftJoinAndSelect(
'subspaceProductAllocations.product',
'subspaceProduct',
)
// .leftJoinAndSelect('subspaceTags.product', 'subspaceTagProduct')
.where('space.community_id = :communityUuid', { communityUuid })
.andWhere('space.spaceName != :orphanSpaceName', {
orphanSpaceName: ORPHAN_SPACE_NAME,
@ -456,7 +422,7 @@ export class SpaceService {
const { communityUuid, spaceUuid, projectUuid } = params;
const queryRunner = this.dataSource.createQueryRunner();
const hasSubspace = updateSpaceDto.subspace?.length > 0;
const hasSubspace = updateSpaceDto.subspaces?.length > 0;
const hasTags = updateSpaceDto.tags?.length > 0;
try {
@ -473,13 +439,6 @@ export class SpaceService {
spaceUuid,
);
if (space.spaceName === ORPHAN_SPACE_NAME) {
throw new HttpException(
`Space "${ORPHAN_SPACE_NAME}" cannot be updated`,
HttpStatus.BAD_REQUEST,
);
}
if (space.spaceModel && !updateSpaceDto.spaceModelUuid) {
await queryRunner.manager.update(SpaceEntity, space.uuid, {
spaceModel: null,
@ -512,7 +471,11 @@ export class SpaceService {
queryRunner,
);
} else if (hasDependencies) {
await this.spaceModelService.overwriteSpace(
// check for uuids that didn't change,
// get their device ids and check if they has a tag in device entity,
// if so move them ot the orphan space
await this.spaceModelService.removeSpaceOldSubspacesAndAllocations(
space,
project,
queryRunner,
@ -536,23 +499,45 @@ export class SpaceService {
);
}
if (hasSubspace) {
await this.subSpaceService.modifySubSpace(
updateSpaceDto.subspace,
if (updateSpaceDto.subspaces) {
await this.subSpaceService.updateSubspaceInSpace(
updateSpaceDto.subspaces,
queryRunner,
space,
projectUuid,
updateSpaceDto.tags,
);
}
if (updateSpaceDto.tags) {
await this.spaceProductAllocationService.updateSpaceProductAllocations(
updateSpaceDto.tags,
await queryRunner.manager.delete(SpaceProductAllocationEntity, {
space: { uuid: space.uuid },
tag: {
uuid: Not(
In(
updateSpaceDto.tags
.filter((tag) => tag.tagUuid)
.map((tag) => tag.tagUuid),
),
),
},
});
await this.createAllocations(
updateSpaceDto.tags.map((tag) => ({
name: tag.name,
uuid: tag.tagUuid,
productUuid: tag.productUuid,
})),
projectUuid,
space,
queryRunner,
updateSpaceDto.subspace,
space,
);
}
if (space.devices?.length) {
await this.deviceService.addDevicesToOrphanSpace(
space,
project,
queryRunner,
);
}
@ -636,7 +621,7 @@ export class SpaceService {
// Get all spaces that are children of the provided space, including the parent-child relations
const spaces = await this.spaceRepository.find({
where: { parent: { uuid: spaceUuid }, disabled: false },
relations: ['parent', 'children'], // Include parent and children relations
relations: ['parent', 'children'],
});
// Organize spaces into a hierarchical structure
@ -715,13 +700,13 @@ export class SpaceService {
return rootSpaces;
}
private validateSpaceCreation(
addSpaceDto: AddSpaceDto,
spaceModelUuid?: string,
) {
private validateSpaceCreationCriteria({
spaceModelUuid,
tags,
subspaces,
}: Pick<AddSpaceDto, 'spaceModelUuid' | 'tags' | 'subspaces'>): void {
const hasTagsOrSubspaces =
(addSpaceDto.tags && addSpaceDto.tags.length > 0) ||
(addSpaceDto.subspaces && addSpaceDto.subspaces.length > 0);
(tags && tags.length > 0) || (subspaces && subspaces.length > 0);
if (spaceModelUuid && hasTagsOrSubspaces) {
throw new HttpException(
@ -731,36 +716,36 @@ export class SpaceService {
}
}
private async createSubspaces(
subspaces: AddSubspaceDto[],
space: SpaceEntity,
queryRunner: QueryRunner,
tags: ProcessTagDto[],
projectUuid: string,
): Promise<void> {
space.subspaces = await this.subSpaceService.createSubspacesFromDto(
subspaces,
space,
queryRunner,
null,
projectUuid,
);
}
private async createTags(
private async createAllocations(
tags: ProcessTagDto[],
projectUuid: string,
queryRunner: QueryRunner,
space: SpaceEntity,
): Promise<void> {
const processedTags = await this.newTagService.processTags(
const allocationsData = await this.tagService.processTags(
tags,
projectUuid,
queryRunner,
);
await this.spaceProductAllocationService.createSpaceProductAllocations(
// Create a mapping of created tags by UUID and name for quick lookup
const createdTagsByUUID = new Map(allocationsData.map((t) => [t.uuid, t]));
const createdTagsByName = new Map(allocationsData.map((t) => [t.name, t]));
// Create the product-tag mapping based on the processed tags
const productTagMapping = tags.map(({ uuid, name, productUuid }) => {
const inputTag = uuid
? createdTagsByUUID.get(uuid)
: createdTagsByName.get(name);
return {
tag: inputTag?.uuid,
product: productUuid,
};
});
await this.spaceProductAllocationService.createProductAllocations(
space,
processedTags,
productTagMapping,
queryRunner,
);
}

View File

@ -1,14 +1,13 @@
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
import { DeviceEntity } from '@app/common/modules/device/entities';
import { DeviceRepository } from '@app/common/modules/device/repositories';
import { SubspaceRepository } from '@app/common/modules/space/repositories/subspace.repository';
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { DeviceService } from 'src/device/services';
import { In, QueryRunner } from 'typeorm';
import { DeviceSubSpaceParam, GetSubSpaceParam } from '../../dtos';
import { ValidationService } from '../space-validation.service';
import { SubspaceRepository } from '@app/common/modules/space/repositories/subspace.repository';
import { In, QueryRunner } from 'typeorm';
import { DeviceEntity } from '@app/common/modules/device/entities';
import { TagRepository } from '@app/common/modules/space';
import { DeviceService } from 'src/device/services';
@Injectable()
export class SubspaceDeviceService {
@ -17,7 +16,6 @@ export class SubspaceDeviceService {
private readonly deviceRepository: DeviceRepository,
private readonly deviceService: DeviceService,
private readonly validationService: ValidationService,
private readonly tagRepository: TagRepository,
) {}
async listDevicesInSubspace(
@ -202,21 +200,6 @@ export class SubspaceDeviceService {
);
}
async findNextTag(): Promise<number> {
const tags = await this.tagRepository.find({ select: ['tag'] });
const tagNumbers = tags
.map((t) => t.tag.match(/^Tag (\d+)$/))
.filter((match) => match)
.map((match) => parseInt(match[1]))
.sort((a, b) => a - b);
const nextTagNumber = tagNumbers.length
? tagNumbers[tagNumbers.length - 1] + 1
: 1;
return nextTagNumber;
}
private async findDeviceWithSubspaceAndTag(deviceUuid: string) {
return await this.deviceRepository.findOne({
where: { uuid: deviceUuid },

View File

@ -1,18 +1,11 @@
import { ModifyAction } from '@app/common/constants/modify-action.enum';
import { ProductEntity } from '@app/common/modules/product/entities';
import { SpaceProductAllocationRepository } from '@app/common/modules/space';
import { SpaceProductAllocationEntity } from '@app/common/modules/space/entities/space-product-allocation.entity';
import { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
import { SubspaceProductAllocationEntity } from '@app/common/modules/space/entities/subspace/subspace-product-allocation.entity';
import { SubspaceEntity } from '@app/common/modules/space/entities/subspace/subspace.entity';
import { SubspaceProductAllocationRepository } from '@app/common/modules/space/repositories/subspace.repository';
import { NewTagEntity } from '@app/common/modules/tag';
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { ModifyTagDto } from 'src/space/dtos/tag/modify-tag.dto';
import { ISingleSubspace } from 'src/space/interfaces/single-subspace.interface';
import { ProcessTagDto } from 'src/tags/dtos';
import { UpdateSpaceAllocationDto } from 'src/space/interfaces/update-subspace-allocation.dto';
import { TagService as NewTagService } from 'src/tags/services';
import { In, QueryRunner } from 'typeorm';
import { In, Not, QueryRunner } from 'typeorm';
@Injectable()
export class SubspaceProductAllocationService {
@ -23,44 +16,38 @@ export class SubspaceProductAllocationService {
private readonly subspaceProductAllocationRepository: SubspaceProductAllocationRepository,
) {}
async createSubspaceProductAllocations(
async createProductAllocations(
subspace: SubspaceEntity,
processedTags: NewTagEntity[],
allocationsData: { product: string; tag: string }[],
queryRunner?: QueryRunner,
spaceAllocationsToExclude?: SpaceProductAllocationEntity[],
// spaceAllocationsToExclude?: SpaceProductAllocationEntity[],
): Promise<void> {
try {
if (!processedTags.length) return;
if (!allocationsData.length) return;
const allocations: SubspaceProductAllocationEntity[] = [];
for (const tag of processedTags) {
await this.validateTagWithinSubspace(
queryRunner,
tag,
subspace,
spaceAllocationsToExclude,
);
for (const allocationData of allocationsData) {
// await this.validateTagWithinSubspace(
// queryRunner,
// allocationData.tag,
// subspace,
// spaceAllocationsToExclude,
// );
let allocation = await this.getAllocationByProduct(
tag.product,
subspace,
queryRunner,
);
if (!allocation) {
allocation = this.createNewSubspaceAllocation(
subspace,
tag,
queryRunner,
);
allocations.push(allocation);
} else if (!allocation.tags.some((t) => t.uuid === tag.uuid)) {
allocation.tags.push(tag);
await this.saveAllocation(allocation, queryRunner);
if (
await this.isAllocationExist(allocationData, subspace, queryRunner)
) {
continue;
}
}
const allocation = this.createNewSubspaceAllocation(
subspace,
allocationData,
queryRunner,
);
allocations.push(allocation);
}
if (allocations.length > 0) {
await this.saveAllocations(allocations, queryRunner);
}
@ -71,307 +58,130 @@ export class SubspaceProductAllocationService {
);
}
}
async updateSubspaceProductAllocations(
subspaces: ISingleSubspace[],
async updateSubspaceProductAllocationsV2(
subSpaces: UpdateSpaceAllocationDto[],
projectUuid: string,
queryRunner: QueryRunner,
space: SpaceEntity,
spaceTagUpdateDtos?: ModifyTagDto[],
) {
const spaceAllocationToExclude: SpaceProductAllocationEntity[] = [];
for (const subspace of subspaces) {
if (!subspace.tags || subspace.tags.length === 0) continue;
const tagDtos = subspace.tags;
const tagsToAddDto: ProcessTagDto[] = tagDtos
.filter((dto) => dto.action === ModifyAction.ADD)
.map((dto) => ({
name: dto.name,
productUuid: dto.productUuid,
uuid: dto.newTagUuid,
}));
await Promise.all(
subSpaces.map(async (subspace) => {
await queryRunner.manager.delete(SubspaceProductAllocationEntity, {
subspace: { uuid: subspace.uuid },
tag: {
uuid: Not(
In(
subspace.tags.filter((tag) => tag.uuid).map((tag) => tag.uuid),
),
),
},
});
const subspaceEntity = await queryRunner.manager.findOne(
SubspaceEntity,
{
where: { uuid: subspace.uuid },
},
);
const tagsToDeleteDto = tagDtos.filter(
(dto) => dto.action === ModifyAction.DELETE,
);
if (tagsToAddDto.length > 0) {
let processedTags = await this.tagService.processTags(
tagsToAddDto,
const processedTags = await this.tagService.processTags(
subspace.tags,
projectUuid,
queryRunner,
);
for (const subspaceDto of subspaces) {
if (
subspaceDto !== subspace &&
subspaceDto.action === ModifyAction.UPDATE &&
subspaceDto.tags
) {
const deletedTags = subspaceDto.tags.filter(
(tagDto) =>
tagDto.action === ModifyAction.DELETE &&
processedTags.some((tag) => tag.uuid === tagDto.tagUuid),
);
const createdTagsByUUID = new Map(
processedTags.map((t) => [t.uuid, t]),
);
const createdTagsByName = new Map(
processedTags.map((t) => [t.name, t]),
);
for (const deletedTag of deletedTags) {
const allocation = await queryRunner.manager.findOne(
SubspaceProductAllocationEntity,
{
where: {
subspace: { uuid: subspaceDto.subspace.uuid },
},
relations: ['tags', 'product', 'subspace'],
},
);
// Create the product-tag mapping based on the processed tags
const productTagMapping = subspace.tags.map(
({ uuid, name, productUuid }) => {
const inputTag = uuid
? createdTagsByUUID.get(uuid)
: createdTagsByName.get(name);
return {
tag: inputTag?.uuid,
product: productUuid,
};
},
);
const isCommonTag = allocation.tags.some(
(tag) => tag.uuid === deletedTag.tagUuid,
);
if (allocation && isCommonTag) {
const tagEntity = allocation.tags.find(
(tag) => tag.uuid === deletedTag.tagUuid,
);
allocation.tags = allocation.tags.filter(
(tag) => tag.uuid !== deletedTag.tagUuid,
);
await queryRunner.manager.save(allocation);
const productAllocationExistInSubspace =
await queryRunner.manager.findOne(
SubspaceProductAllocationEntity,
{
where: {
subspace: {
uuid: subspaceDto.subspace.uuid,
},
product: { uuid: allocation.product.uuid },
},
relations: ['tags'],
},
);
if (productAllocationExistInSubspace) {
productAllocationExistInSubspace.tags.push(tagEntity);
await queryRunner.manager.save(
productAllocationExistInSubspace,
);
} else {
const newProductAllocation = queryRunner.manager.create(
SubspaceProductAllocationEntity,
{
subspace: subspace.subspace,
product: allocation.product,
tags: [tagEntity],
},
);
await queryRunner.manager.save(newProductAllocation);
}
processedTags = processedTags.filter(
(tag) => tag.uuid !== deletedTag.tagUuid,
);
subspaceDto.tags = subspaceDto.tags.filter(
(tagDto) => tagDto.tagUuid !== deletedTag.tagUuid,
);
}
}
}
if (
subspaceDto !== subspace &&
subspaceDto.action === ModifyAction.DELETE
) {
const allocation = await queryRunner.manager.findOne(
SubspaceProductAllocationEntity,
{
where: {
subspace: { uuid: subspaceDto.subspace.uuid },
},
relations: ['tags'],
},
);
const repeatedTags = allocation?.tags.filter((tag) =>
processedTags.some(
(processedTag) => processedTag.uuid === tag.uuid,
),
);
if (repeatedTags.length > 0) {
allocation.tags = allocation.tags.filter(
(tag) =>
!repeatedTags.some(
(repeatedTag) => repeatedTag.uuid === tag.uuid,
),
);
await queryRunner.manager.save(allocation);
const productAllocationExistInSubspace =
await queryRunner.manager.findOne(
SubspaceProductAllocationEntity,
{
where: {
subspace: { uuid: subspaceDto.subspace.uuid },
product: { uuid: allocation.product.uuid },
},
relations: ['tags'],
},
);
if (productAllocationExistInSubspace) {
productAllocationExistInSubspace.tags.push(...repeatedTags);
await queryRunner.manager.save(
productAllocationExistInSubspace,
);
} else {
const newProductAllocation = queryRunner.manager.create(
SubspaceProductAllocationEntity,
{
subspace: subspace.subspace,
product: allocation.product,
tags: repeatedTags,
},
);
await queryRunner.manager.save(newProductAllocation);
}
const newAllocation = queryRunner.manager.create(
SubspaceProductAllocationEntity,
{
subspace: subspace.subspace,
product: allocation.product,
tags: repeatedTags,
},
);
await queryRunner.manager.save(newAllocation);
}
}
}
if (spaceTagUpdateDtos) {
const deletedSpaceTags = spaceTagUpdateDtos.filter(
(tagDto) =>
tagDto.action === ModifyAction.DELETE &&
processedTags.some((tag) => tag.uuid === tagDto.tagUuid),
);
for (const deletedTag of deletedSpaceTags) {
const allocation = await queryRunner.manager.findOne(
SpaceProductAllocationEntity,
{
where: {
space: { uuid: space.uuid },
tags: { uuid: deletedTag.tagUuid },
},
relations: ['tags', 'subspace'],
},
);
if (
allocation &&
allocation.tags.some((tag) => tag.uuid === deletedTag.tagUuid)
) {
spaceAllocationToExclude.push(allocation);
}
}
}
await this.createSubspaceProductAllocations(
subspace.subspace,
processedTags,
await this.createProductAllocations(
subspaceEntity,
productTagMapping,
queryRunner,
spaceAllocationToExclude,
);
}
if (tagsToDeleteDto.length > 0) {
await this.processDeleteActions(tagsToDeleteDto, queryRunner);
}
}
}),
);
}
async processDeleteActions(
dtos: ModifyTagDto[],
queryRunner: QueryRunner,
): Promise<SubspaceProductAllocationEntity[]> {
try {
if (!dtos || dtos.length === 0) {
throw new Error('No DTOs provided for deletion.');
}
const tagUuidsToDelete = dtos
.filter((dto) => dto.action === ModifyAction.DELETE && dto.tagUuid)
.map((dto) => dto.tagUuid);
if (tagUuidsToDelete.length === 0) return [];
const allocationsToUpdate = await queryRunner.manager.find(
SubspaceProductAllocationEntity,
{
where: { tags: { uuid: In(tagUuidsToDelete) } },
relations: ['tags'],
},
);
if (!allocationsToUpdate || allocationsToUpdate.length === 0) return [];
const deletedAllocations: SubspaceProductAllocationEntity[] = [];
const allocationUpdates: SubspaceProductAllocationEntity[] = [];
for (const allocation of allocationsToUpdate) {
const updatedTags = allocation.tags.filter(
(tag) => !tagUuidsToDelete.includes(tag.uuid),
);
if (updatedTags.length === allocation.tags.length) {
continue;
}
if (updatedTags.length === 0) {
deletedAllocations.push(allocation);
} else {
allocation.tags = updatedTags;
allocationUpdates.push(allocation);
}
}
if (allocationUpdates.length > 0) {
await queryRunner.manager.save(
SubspaceProductAllocationEntity,
allocationUpdates,
);
}
if (deletedAllocations.length > 0) {
await queryRunner.manager.remove(
SubspaceProductAllocationEntity,
deletedAllocations,
);
}
await queryRunner.manager
.createQueryBuilder()
.delete()
.from('subspace_product_tags')
.where(
'subspace_product_allocation_uuid NOT IN ' +
queryRunner.manager
.createQueryBuilder()
.select('allocation.uuid')
.from(SubspaceProductAllocationEntity, 'allocation')
.getQuery() +
')',
)
.execute();
return deletedAllocations;
} catch (error) {
throw this.handleError(error, `Failed to delete tags in subspace`);
}
}
// async processDeleteActions(dtos: ModifyTagDto[], queryRunner: QueryRunner) {
// // : Promise<SubspaceProductAllocationEntity[]>
// try {
// // if (!dtos || dtos.length === 0) {
// // throw new Error('No DTOs provided for deletion.');
// // }
// // const tagUuidsToDelete = dtos
// // .filter((dto) => dto.action === ModifyAction.DELETE && dto.tagUuid)
// // .map((dto) => dto.tagUuid);
// // if (tagUuidsToDelete.length === 0) return [];
// // const allocationsToUpdate = await queryRunner.manager.find(
// // SubspaceProductAllocationEntity,
// // {
// // where: { tag: In(tagUuidsToDelete) },
// // },
// // );
// // if (!allocationsToUpdate || allocationsToUpdate.length === 0) return [];
// // const deletedAllocations: SubspaceProductAllocationEntity[] = [];
// // const allocationUpdates: SubspaceProductAllocationEntity[] = [];
// // for (const allocation of allocationsToUpdate) {
// // const updatedTags = allocation.tags.filter(
// // (tag) => !tagUuidsToDelete.includes(tag.uuid),
// // );
// // if (updatedTags.length === allocation.tags.length) {
// // continue;
// // }
// // if (updatedTags.length === 0) {
// // deletedAllocations.push(allocation);
// // } else {
// // allocation.tags = updatedTags;
// // allocationUpdates.push(allocation);
// // }
// // }
// // if (allocationUpdates.length > 0) {
// // await queryRunner.manager.save(
// // SubspaceProductAllocationEntity,
// // allocationUpdates,
// // );
// // }
// // if (deletedAllocations.length > 0) {
// // await queryRunner.manager.remove(
// // SubspaceProductAllocationEntity,
// // deletedAllocations,
// // );
// // }
// // await queryRunner.manager
// // .createQueryBuilder()
// // .delete()
// // .from('subspace_product_tags')
// // .where(
// // 'subspace_product_allocation_uuid NOT IN ' +
// // queryRunner.manager
// // .createQueryBuilder()
// // .select('allocation.uuid')
// // .from(SubspaceProductAllocationEntity, 'allocation')
// // .getQuery() +
// // ')',
// // )
// // .execute();
// // return deletedAllocations;
// } catch (error) {
// throw this.handleError(error, `Failed to delete tags in subspace`);
// }
// }
async unlinkModels(
allocations: SubspaceProductAllocationEntity[],
@ -395,108 +205,106 @@ export class SubspaceProductAllocationService {
}
}
private async validateTagWithinSubspace(
queryRunner: QueryRunner | undefined,
tag: NewTagEntity,
subspace: SubspaceEntity,
spaceAllocationsToExclude?: SpaceProductAllocationEntity[],
): Promise<void> {
const existingTagInSpace = await (queryRunner
? queryRunner.manager.findOne(SpaceProductAllocationEntity, {
where: {
product: { uuid: tag.product.uuid },
space: { uuid: subspace.space.uuid },
tags: { uuid: tag.uuid },
},
relations: ['tags'],
})
: this.spaceProductAllocationRepository.findOne({
where: {
product: { uuid: tag.product.uuid },
space: { uuid: subspace.space.uuid },
tags: { uuid: tag.uuid },
},
relations: ['tags'],
}));
// private async validateTagWithinSubspace(
// queryRunner: QueryRunner | undefined,
// tag: NewTagEntity & { product: string },
// subspace: SubspaceEntity,
// spaceAllocationsToExclude?: SpaceProductAllocationEntity[],
// ): Promise<void> {
// // const existingTagInSpace = await (queryRunner
// // ? queryRunner.manager.findOne(SpaceProductAllocationEntity, {
// // where: {
// // product: { uuid: tag.product },
// // space: { uuid: subspace.space.uuid },
// // tag: { uuid: tag.uuid },
// // },
// // })
// // : this.spaceProductAllocationRepository.findOne({
// // where: {
// // product: { uuid: tag.product },
// // space: { uuid: subspace.space.uuid },
// // tag: { uuid: tag.uuid },
// // },
// // }));
// // const isExcluded = spaceAllocationsToExclude?.some(
// // (excludedAllocation) =>
// // excludedAllocation.product.uuid === tag.product &&
// // excludedAllocation.tags.some((t) => t.uuid === tag.uuid),
// // );
// // if (!isExcluded && existingTagInSpace) {
// // throw new HttpException(
// // `Tag ${tag.uuid} (Product: ${tag.product}) is already allocated at the space level (${subspace.space.uuid}). Cannot allocate the same tag in a subspace.`,
// // HttpStatus.BAD_REQUEST,
// // );
// // }
// // // ?: Check if the tag is already allocated in another "subspace" within the same space
// // const existingTagInSameSpace = await (queryRunner
// // ? queryRunner.manager.findOne(SubspaceProductAllocationEntity, {
// // where: {
// // product: { uuid: tag.product },
// // subspace: { space: subspace.space },
// // tag: { uuid: tag.uuid },
// // },
// // relations: ['subspace'],
// // })
// // : this.subspaceProductAllocationRepository.findOne({
// // where: {
// // product: { uuid: tag.product },
// // subspace: { space: subspace.space },
// // tag: { uuid: tag.uuid },
// // },
// // relations: ['subspace'],
// // }));
// // if (
// // existingTagInSameSpace &&
// // existingTagInSameSpace.subspace.uuid !== subspace.uuid
// // ) {
// // throw new HttpException(
// // `Tag ${tag.uuid} (Product: ${tag.product}) is already allocated in another subspace (${existingTagInSameSpace.subspace.uuid}) within the same space (${subspace.space.uuid}).`,
// // HttpStatus.BAD_REQUEST,
// // );
// // }
// }
const isExcluded = spaceAllocationsToExclude?.some(
(excludedAllocation) =>
excludedAllocation.product.uuid === tag.product.uuid &&
excludedAllocation.tags.some((t) => t.uuid === tag.uuid),
);
if (!isExcluded && existingTagInSpace) {
throw new HttpException(
`Tag ${tag.uuid} (Product: ${tag.product.uuid}) is already allocated at the space level (${subspace.space.uuid}). Cannot allocate the same tag in a subspace.`,
HttpStatus.BAD_REQUEST,
);
}
const existingTagInSameSpace = await (queryRunner
? queryRunner.manager.findOne(SubspaceProductAllocationEntity, {
where: {
product: { uuid: tag.product.uuid },
subspace: { space: subspace.space },
tags: { uuid: tag.uuid },
},
relations: ['subspace', 'tags'],
})
: this.subspaceProductAllocationRepository.findOne({
where: {
product: { uuid: tag.product.uuid },
subspace: { space: subspace.space },
tags: { uuid: tag.uuid },
},
relations: ['subspace', 'tags'],
}));
if (
existingTagInSameSpace &&
existingTagInSameSpace.subspace.uuid !== subspace.uuid
) {
throw new HttpException(
`Tag ${tag.uuid} (Product: ${tag.product.uuid}) is already allocated in another subspace (${existingTagInSameSpace.subspace.uuid}) within the same space (${subspace.space.uuid}).`,
HttpStatus.BAD_REQUEST,
);
}
}
private createNewSubspaceAllocation(
subspace: SubspaceEntity,
tag: NewTagEntity,
allocationData: { product: string; tag: string },
queryRunner?: QueryRunner,
): SubspaceProductAllocationEntity {
return queryRunner
? queryRunner.manager.create(SubspaceProductAllocationEntity, {
subspace,
product: tag.product,
tags: [tag],
product: { uuid: allocationData.product },
tag: { uuid: allocationData.tag },
})
: this.subspaceProductAllocationRepository.create({
subspace,
product: tag.product,
tags: [tag],
product: { uuid: allocationData.product },
tag: { uuid: allocationData.tag },
});
}
private async getAllocationByProduct(
product: ProductEntity,
private async isAllocationExist(
allocationData: { product: string; tag: string },
subspace: SubspaceEntity,
queryRunner?: QueryRunner,
): Promise<SubspaceProductAllocationEntity | null> {
return queryRunner
? queryRunner.manager.findOne(SubspaceProductAllocationEntity, {
): Promise<boolean> {
const allocation = queryRunner
? await queryRunner.manager.findOne(SubspaceProductAllocationEntity, {
where: {
subspace: { uuid: subspace.uuid },
product: { uuid: product.uuid },
product: { uuid: allocationData.product },
tag: { uuid: allocationData.tag },
},
relations: ['tags'],
})
: this.subspaceProductAllocationRepository.findOne({
: await this.subspaceProductAllocationRepository.findOne({
where: {
subspace: { uuid: subspace.uuid },
product: { uuid: product.uuid },
product: { uuid: allocationData.product },
tag: { uuid: allocationData.tag },
},
relations: ['tags'],
});
return !!allocation;
}
private async saveAllocation(
allocation: SubspaceProductAllocationEntity,
@ -535,34 +343,9 @@ export class SubspaceProductAllocationService {
}
async clearAllAllocations(subspaceUuids: string[], queryRunner: QueryRunner) {
try {
const allocationUuids = await queryRunner.manager
.createQueryBuilder(SubspaceProductAllocationEntity, 'allocation')
.select('allocation.uuid')
.where('allocation.subspace_uuid IN (:...subspaceUuids)', {
subspaceUuids,
})
.getRawMany()
.then((results) => results.map((r) => r.allocation_uuid));
if (allocationUuids.length === 0) {
return;
}
await queryRunner.manager
.createQueryBuilder()
.delete()
.from('subspace_product_tags')
.where('subspace_product_allocation_uuid IN (:...allocationUuids)', {
allocationUuids,
})
.execute();
await queryRunner.manager
.createQueryBuilder()
.delete()
.from(SubspaceProductAllocationEntity)
.where('subspace_uuid IN (:...subspaceUuids)', { subspaceUuids })
.execute();
await queryRunner.manager.delete(SubspaceProductAllocationEntity, {
subspace: { uuid: In(subspaceUuids) },
});
} catch (error) {
throw new HttpException(
error instanceof HttpException

View File

@ -1,32 +1,33 @@
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
import { PageResponse } from '@app/common/dto/pagination.response.dto';
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
import {
TypeORMCustomModel,
TypeORMCustomModelFindAllQuery,
} from '@app/common/models/typeOrmCustom.model';
import { SubspaceDto } from '@app/common/modules/space/dtos';
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { In, Not, QueryRunner } from 'typeorm';
import {
AddSubspaceDto,
GetSpaceParam,
GetSubSpaceParam,
ModifySubspaceDto,
} from '../../dtos';
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
import {
TypeORMCustomModel,
TypeORMCustomModelFindAllQuery,
} from '@app/common/models/typeOrmCustom.model';
import { PageResponse } from '@app/common/dto/pagination.response.dto';
import { SubspaceDto } from '@app/common/modules/space/dtos';
import { In, QueryRunner } from 'typeorm';
import { SubspaceModelEntity } from '@app/common/modules/space-model';
import { ValidationService } from '../space-validation.service';
import { SubspaceRepository } from '@app/common/modules/space/repositories/subspace.repository';
import { ModifyAction } from '@app/common/constants/modify-action.enum';
import { SubspaceDeviceService } from './subspace-device.service';
import { ModifyTagDto } from 'src/space/dtos/tag/modify-tag.dto';
import { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
import { SubspaceEntity } from '@app/common/modules/space/entities/subspace/subspace.entity';
import { ProcessTagDto } from 'src/tags/dtos';
import { TagService as NewTagService } from 'src/tags/services/tags.service';
import { SubspaceProductAllocationService } from './subspace-product-allocation.service';
import { SubspaceProductAllocationEntity } from '@app/common/modules/space/entities/subspace/subspace-product-allocation.entity';
import { SubspaceEntity } from '@app/common/modules/space/entities/subspace/subspace.entity';
import { SubspaceRepository } from '@app/common/modules/space/repositories/subspace.repository';
import {
AllocationsOwnerType,
CreateAllocationsDto,
} from 'src/space/dtos/create-allocations.dto';
import { TagService as NewTagService } from 'src/tags/services/tags.service';
import { ValidationService } from '../space-validation.service';
import { SubspaceDeviceService } from './subspace-device.service';
import { SubspaceProductAllocationService } from './subspace-product-allocation.service';
@Injectable()
export class SubSpaceService {
@ -55,9 +56,9 @@ export class SubSpaceService {
);
const subspaces = subspaceData.map((data) =>
queryRunner.manager.create(this.subspaceRepository.target, data),
queryRunner.manager.create(SubspaceEntity, data),
);
return await queryRunner.manager.save(subspaces);
return queryRunner.manager.save(subspaces);
} catch (error) {
throw new HttpException(
`An unexpected error occurred while creating subspaces. ${error}`,
@ -91,42 +92,30 @@ export class SubSpaceService {
addSubspaceDtos: AddSubspaceDto[],
space: SpaceEntity,
queryRunner: QueryRunner,
otherTags?: ProcessTagDto[],
projectUuid?: string,
): Promise<SubspaceEntity[]> {
try {
this.checkForDuplicateNames(
addSubspaceDtos.map(({ subspaceName }) => subspaceName),
const createdSubspaces = await this.createSubspaces(
addSubspaceDtos.map((dto) => ({
subspaceName: dto.subspaceName,
space,
})),
queryRunner,
);
const subspaceData = addSubspaceDtos.map((dto) => ({
subspaceName: dto.subspaceName,
space,
}));
const subspaces = await this.createSubspaces(subspaceData, queryRunner);
await Promise.all(
addSubspaceDtos.map(async (dto, index) => {
const subspace = subspaces[index];
const allTags = [...(dto.tags || []), ...(otherTags || [])];
if (allTags.length) {
const processedTags = await this.newTagService.processTags(
allTags,
projectUuid,
queryRunner,
);
await this.subspaceProductAllocationService.createSubspaceProductAllocations(
subspace,
processedTags,
queryRunner,
);
}
addSubspaceDtos.map(async ({ tags }, index) => {
// map the dto to the corresponding subspace
const subspace = createdSubspaces[index];
await this.createAllocations({
projectUuid,
queryRunner,
tags,
type: AllocationsOwnerType.SUBSPACE,
subspace,
});
}),
);
return subspaces;
return createdSubspaces;
} catch (error) {
if (error instanceof HttpException) {
throw error;
@ -312,65 +301,62 @@ export class SubSpaceService {
deleteResults.push({ uuid: dto.subspaceUuid });
}
return deleteResults;
return deleteResults;
} */
async modifySubSpace(
async updateSubspaceInSpace(
subspaceDtos: ModifySubspaceDto[],
queryRunner: QueryRunner,
space?: SpaceEntity,
projectUuid?: string,
spaceTagUpdateDtos?: ModifyTagDto[],
space: SpaceEntity,
projectUuid: string,
) {
if (!subspaceDtos || subspaceDtos.length === 0) {
return;
}
try {
const addedSubspaces = [];
const updatedSubspaces = [];
for (const subspace of subspaceDtos) {
switch (subspace.action) {
case ModifyAction.ADD:
const addedSubspace = await this.handleAddAction(
subspace,
space,
queryRunner,
);
if (addedSubspace) addedSubspaces.push(addedSubspace);
break;
case ModifyAction.UPDATE:
const updatedSubspace = await this.handleUpdateAction(
subspace,
queryRunner,
);
if (updatedSubspace) {
updatedSubspaces.push(updatedSubspace);
}
break;
case ModifyAction.DELETE:
await this.handleDeleteAction(subspace, queryRunner);
break;
default:
throw new HttpException(
`Invalid action "${subspace.action}".`,
HttpStatus.BAD_REQUEST,
);
}
}
const combinedSubspaces = [...addedSubspaces, ...updatedSubspaces].filter(
(subspace) => subspace !== undefined,
// disable subspaces that are not in the provided list & delte their allocations
await queryRunner.manager.update(
SubspaceEntity,
{
uuid: Not(
In(subspaceDtos.filter(({ uuid }) => uuid).map(({ uuid }) => uuid)),
),
space: { uuid: space.uuid },
},
{
disabled: true,
},
);
if (combinedSubspaces.length > 0) {
await this.subspaceProductAllocationService.updateSubspaceProductAllocations(
combinedSubspaces,
await queryRunner.manager.delete(SubspaceProductAllocationEntity, {
subspace: { uuid: Not(In(subspaceDtos.map((dto) => dto.uuid))) },
});
// create or update subspaces provided in the list
const newSubspaces = this.subspaceRepository.create(
subspaceDtos.filter((dto) => !dto.uuid),
);
const updatedSubspaces: SubspaceEntity[] = await queryRunner.manager.save(
SubspaceEntity,
[...newSubspaces, ...subspaceDtos.filter((dto) => dto.uuid)].map(
(subspace) => ({ ...subspace, space }),
),
);
// create or update allocations for the subspaces
if (updatedSubspaces.length > 0) {
await this.subspaceProductAllocationService.updateSubspaceProductAllocationsV2(
subspaceDtos.map((dto) => {
if (!dto.uuid) {
dto.uuid = updatedSubspaces.find(
(subspace) => subspace.subspaceName === dto.subspaceName,
)?.uuid;
}
return {
tags: dto.tags || [],
uuid: dto.uuid,
};
}),
projectUuid,
queryRunner,
space,
spaceTagUpdateDtos,
);
}
} catch (error) {
@ -427,70 +413,10 @@ export class SubSpaceService {
});
}
private async handleAddAction(
subspace: ModifySubspaceDto,
space: SpaceEntity,
queryRunner: QueryRunner,
): Promise<SubspaceEntity> {
const createTagDtos: ProcessTagDto[] =
subspace.tags?.map((tag) => ({
name: tag.name as string,
uuid: tag.tagUuid,
productUuid: tag.productUuid as string,
})) || [];
const subSpace = await this.createSubspacesFromDto(
[{ subspaceName: subspace.subspaceName, tags: createTagDtos }],
space,
queryRunner,
);
return subSpace[0];
}
private async handleUpdateAction(
modifyDto: ModifySubspaceDto,
queryRunner: QueryRunner,
): Promise<SubspaceEntity> {
const subspace = await this.findOne(modifyDto.uuid);
const updatedSubspace = await this.update(
queryRunner,
subspace,
modifyDto.subspaceName,
);
return updatedSubspace;
}
async update(
queryRunner: QueryRunner,
subspace: SubspaceEntity,
subspaceName?: string,
) {
return await this.updateSubspaceName(queryRunner, subspace, subspaceName);
}
async handleDeleteAction(
modifyDto: ModifySubspaceDto,
queryRunner: QueryRunner,
): Promise<void> {
const subspace = await this.findOne(modifyDto.uuid);
await queryRunner.manager.update(
this.subspaceRepository.target,
{ uuid: subspace.uuid },
{ disabled: true },
);
if (subspace.devices.length > 0) {
await this.deviceService.deleteSubspaceDevices(
subspace.devices,
queryRunner,
);
}
}
private async findOne(subspaceUuid: string): Promise<SubspaceEntity> {
const subspace = await this.subspaceRepository.findOne({
where: { uuid: subspaceUuid, disabled: false },
relations: ['tags', 'space', 'devices', 'tags.product', 'tags.device'],
relations: ['space', 'devices'],
});
if (!subspace) {
throw new HttpException(
@ -501,36 +427,6 @@ export class SubSpaceService {
return subspace;
}
async updateSubspaceName(
queryRunner: QueryRunner,
subSpace: SubspaceEntity,
subspaceName?: string,
): Promise<SubspaceEntity> {
if (subspaceName) {
subSpace.subspaceName = subspaceName;
return await queryRunner.manager.save(subSpace);
}
return subSpace;
}
private async checkForDuplicateNames(names: string[]): Promise<void> {
const seenNames = new Set<string>();
const duplicateNames = new Set<string>();
for (const name of names) {
if (!seenNames.add(name)) {
duplicateNames.add(name);
}
}
if (duplicateNames.size > 0) {
throw new HttpException(
`Duplicate subspace names found: ${[...duplicateNames].join(', ')}`,
HttpStatus.CONFLICT,
);
}
}
private async checkExistingNamesInSpace(
names: string[],
space: SpaceEntity,
@ -557,16 +453,6 @@ export class SubSpaceService {
}
}
private async validateName(
names: string[],
space: SpaceEntity,
): Promise<void> {
await this.checkForDuplicateNames(names);
await this.checkExistingNamesInSpace(names, space);
}
extractTagsFromSubspace(addSubspaceDto: AddSubspaceDto[]): ProcessTagDto[] {
return addSubspaceDto.flatMap((subspace) => subspace.tags || []);
}
async clearSubspaces(subspaceUuids: string[], queryRunner: QueryRunner) {
try {
await queryRunner.manager.update(
@ -590,4 +476,40 @@ export class SubSpaceService {
);
}
}
async createAllocations(dto: CreateAllocationsDto): Promise<void> {
const { projectUuid, queryRunner, tags, type } = dto;
const allocationsData = await this.newTagService.processTags(
tags,
projectUuid,
queryRunner,
);
// Create a mapping of created tags by UUID and name for quick lookup
const createdTagsByUUID = new Map(allocationsData.map((t) => [t.uuid, t]));
const createdTagsByName = new Map(allocationsData.map((t) => [t.name, t]));
// Create the product-tag mapping based on the processed tags
const productTagMapping = tags.map(({ uuid, name, productUuid }) => {
const inputTag = uuid
? createdTagsByUUID.get(uuid)
: createdTagsByName.get(name);
return {
tag: inputTag?.uuid,
product: productUuid,
};
});
switch (type) {
case AllocationsOwnerType.SUBSPACE: {
await this.subspaceProductAllocationService.createProductAllocations(
dto.subspace,
productTagMapping,
queryRunner,
);
break;
}
}
}
}

View File

@ -1,597 +1,8 @@
import { ModifyAction } from '@app/common/constants/modify-action.enum';
import { TagRepository } from '@app/common/modules/space';
import { TagModel } from '@app/common/modules/space-model';
import { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
import { SubspaceEntity } from '@app/common/modules/space/entities/subspace/subspace.entity';
import { TagEntity } from '@app/common/modules/space/entities/tag.entity';
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { ProductService } from 'src/product/services';
import { ModifySubspaceDto } from 'src/space/dtos';
import { ModifyTagDto } from 'src/space/dtos/tag/modify-tag.dto';
import { ProcessTagDto } from 'src/tags/dtos';
import { QueryRunner } from 'typeorm';
import { Injectable } from '@nestjs/common';
// todo: find out why we need to import this
// in community module in order for the whole system to work
@Injectable()
export class TagService {
constructor(
private readonly tagRepository: TagRepository,
private readonly productService: ProductService,
) {}
async createTags(
tags: ProcessTagDto[],
queryRunner: QueryRunner,
space?: SpaceEntity,
subspace?: SubspaceEntity,
additionalTags?: ProcessTagDto[],
tagsToDelete?: ModifyTagDto[],
): Promise<TagEntity[]> {
this.validateTagsInput(tags);
const combinedTags = this.combineTags(tags, additionalTags);
this.ensureNoDuplicateTags(combinedTags);
const tagEntitiesToCreate = tags.filter((tagDto) => !tagDto.uuid);
const tagEntitiesToUpdate = tags.filter((tagDto) => !!tagDto.uuid);
try {
const createdTags = await this.bulkSaveTags(
tagEntitiesToCreate,
queryRunner,
space,
subspace,
tagsToDelete,
);
const updatedTags = await this.moveTags(
tagEntitiesToUpdate,
queryRunner,
space,
subspace,
);
return [...createdTags, ...updatedTags];
} catch (error) {
throw this.handleUnexpectedError('Failed to save tags', error);
}
}
async bulkSaveTags(
tags: ProcessTagDto[],
queryRunner: QueryRunner,
space?: SpaceEntity,
subspace?: SubspaceEntity,
tagsToDelete?: ModifyTagDto[],
): Promise<TagEntity[]> {
if (!tags.length) {
return [];
}
const tagEntities = await Promise.all(
tags.map((tagDto) =>
this.prepareTagEntity(
tagDto,
queryRunner,
space,
subspace,
tagsToDelete,
),
),
);
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: ${error.message}`,
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
async moveTags(
tags: ProcessTagDto[],
queryRunner: QueryRunner,
space?: SpaceEntity,
subspace?: SubspaceEntity,
): Promise<TagEntity[]> {
if (!tags.length) {
return [];
}
try {
return await Promise.all(
tags.map(async (tagDto) => {
try {
const tag = await this.getTagByUuid(tagDto.uuid);
if (!tag) {
throw new HttpException(
`Tag with UUID ${tagDto.uuid} not found.`,
HttpStatus.NOT_FOUND,
);
}
if (subspace && subspace.space) {
await queryRunner.manager.update(
this.tagRepository.target,
{ uuid: tag.uuid },
{ subspace },
);
tag.subspace = subspace;
}
if (!subspace && space) {
await queryRunner.manager.update(
this.tagRepository.target,
{ uuid: tag.uuid },
{ subspace: null },
);
tag.subspace = null;
}
return tag;
} catch (error) {
throw error;
}
}),
);
} catch (error) {
throw new HttpException(
`Failed to move tags due to an unexpected error: ${error.message}`,
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
async createTagsFromModel(
queryRunner: QueryRunner,
tagModels: TagModel[],
space?: SpaceEntity,
subspace?: SubspaceEntity,
): Promise<void> {
if (!tagModels?.length) return;
const tags = tagModels.map((model) =>
queryRunner.manager.create(this.tagRepository.target, {
tag: model.tag,
space: space || undefined,
subspace: subspace || undefined,
product: model.product,
}),
);
await queryRunner.manager.save(tags);
}
async updateTag(
tag: ModifyTagDto,
queryRunner: QueryRunner,
space?: SpaceEntity,
subspace?: SubspaceEntity,
): Promise<TagEntity> {
try {
const existingTag = await this.getTagByUuid(tag.tagUuid);
const contextSpace = space ?? subspace?.space;
if (contextSpace && tag.name !== existingTag.tag) {
await this.checkTagReuse(
tag.name,
existingTag.product.uuid,
contextSpace,
);
}
return await queryRunner.manager.save(
Object.assign(existingTag, { tag: tag.name }),
);
} catch (error) {
throw this.handleUnexpectedError('Failed to update tags', error);
}
}
async updateTagsFromModel(
model: TagModel,
queryRunner: QueryRunner,
): Promise<void> {
try {
const tags = await this.tagRepository.find({
where: {
model: {
uuid: model.uuid,
},
},
});
if (!tags.length) return;
await queryRunner.manager.update(
this.tagRepository.target,
{ model: { uuid: model.uuid } },
{ tag: model.tag },
);
} catch (error) {
if (error instanceof HttpException) {
throw error;
}
throw new HttpException(
`Failed to update tags for model with UUID: ${model.uuid}. Reason: ${error.message}`,
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
async deleteTags(tagUuids: string[], queryRunner: QueryRunner) {
if (!tagUuids?.length) return;
try {
await Promise.all(
tagUuids.map((id) =>
queryRunner.manager.update(
this.tagRepository.target,
{ uuid: id },
{ disabled: true, device: null },
),
),
);
return { message: 'Tags deleted successfully', tagUuids };
} catch (error) {
throw this.handleUnexpectedError('Failed to delete tags', error);
}
}
async deleteTagFromModel(modelUuid: string, queryRunner: QueryRunner) {
try {
const tags = await this.tagRepository.find({
where: {
model: {
uuid: modelUuid,
},
},
});
if (!tags.length) return;
await queryRunner.manager.update(
this.tagRepository.target,
{ model: { uuid: modelUuid } },
{ disabled: true, device: null },
);
} catch (error) {
if (error instanceof HttpException) {
throw error;
}
throw new HttpException(
`Failed to update tags for model with UUID: ${modelUuid}. Reason: ${error.message}`,
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
async modifyTags(
tags: ModifyTagDto[],
queryRunner: QueryRunner,
space?: SpaceEntity,
subspace?: SubspaceEntity,
): Promise<void> {
if (!tags?.length) return;
try {
const tagsToDelete = tags.filter(
(tag) => tag.action === ModifyAction.DELETE,
);
await Promise.all(
tags.map(async (tag) => {
switch (tag.action) {
case ModifyAction.ADD:
await this.createTags(
[
{
name: tag.name,
productUuid: tag.productUuid,
uuid: tag.tagUuid,
},
],
queryRunner,
space,
subspace,
null,
tagsToDelete,
);
break;
case ModifyAction.UPDATE:
await this.updateTag(tag, queryRunner, space, subspace);
break;
case ModifyAction.DELETE:
await this.deleteTags([tag.tagUuid], queryRunner);
break;
default:
throw new HttpException(
`Invalid action "${tag.action}" provided.`,
HttpStatus.BAD_REQUEST,
);
}
}),
);
} catch (error) {
throw this.handleUnexpectedError('Failed to modify tags', error);
}
}
async unlinkModels(tags: TagEntity[], queryRunner: QueryRunner) {
if (!tags?.length) return;
try {
tags.forEach((tag) => {
tag.model = null;
});
await queryRunner.manager.save(tags);
} catch (error) {
throw this.handleUnexpectedError('Failed to unlink tag models', error);
}
}
private findDuplicateTags(tags: ProcessTagDto[]): string[] {
const seen = new Map<string, boolean>();
const duplicates: string[] = [];
tags.forEach((tagDto) => {
const key = `${tagDto.productUuid}-${tagDto.name}`;
if (seen.has(key)) {
duplicates.push(`${tagDto.name} for Product: ${tagDto.productUuid}`);
} else {
seen.set(key, true);
}
});
return duplicates;
}
private async checkTagReuse(
tag: string,
productUuid: string,
space: SpaceEntity,
tagsToDelete?: ModifyTagDto[],
): Promise<void> {
const { uuid: spaceUuid } = space;
const tagExists = await this.tagRepository.find({
where: [
{
tag,
product: { uuid: productUuid },
disabled: false,
},
{
tag,
subspace: { space: { uuid: spaceUuid } },
product: { uuid: productUuid },
disabled: false,
},
],
});
const filteredTagExists = tagExists.filter(
(existingTag) =>
!tagsToDelete?.some(
(deleteTag) => deleteTag.tagUuid === existingTag.uuid,
),
);
if (filteredTagExists.length > 0) {
throw new HttpException(`Tag can't be reused`, HttpStatus.CONFLICT);
}
}
private async prepareTagEntity(
tagDto: ProcessTagDto,
queryRunner: QueryRunner,
space?: SpaceEntity,
subspace?: SubspaceEntity,
tagsToDelete?: ModifyTagDto[],
): Promise<TagEntity> {
try {
const product = await this.productService.findOne(tagDto.productUuid);
if (!product) {
throw new HttpException(
`Product with UUID ${tagDto.productUuid} not found.`,
HttpStatus.NOT_FOUND,
);
}
if (space) {
await this.checkTagReuse(
tagDto.name,
tagDto.productUuid,
space,
tagsToDelete,
);
} else if (subspace && subspace.space) {
await this.checkTagReuse(
tagDto.name,
tagDto.productUuid,
subspace.space,
);
} else {
throw new HttpException(
`Invalid subspace or space provided.`,
HttpStatus.BAD_REQUEST,
);
}
return queryRunner.manager.create(TagEntity, {
tag: tagDto.name,
product: product.data,
space: space,
subspace: subspace,
});
} catch (error) {
if (error instanceof HttpException) {
throw error;
}
throw new HttpException(
`An error occurred while preparing the tag entity: ${error.message}`,
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
private async getTagByUuid(uuid: string): Promise<TagEntity> {
const tag = await this.tagRepository.findOne({
where: { uuid },
relations: ['product'],
});
if (!tag) {
throw new HttpException(
`Tag with ID ${uuid} not found.`,
HttpStatus.NOT_FOUND,
);
}
return tag;
}
private handleUnexpectedError(
message: string,
error: unknown,
): HttpException {
if (error instanceof HttpException) throw error;
return new HttpException(
`${message}: ${(error as Error)?.message || 'Unknown error'}`,
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
private combineTags(
primaryTags: ProcessTagDto[],
additionalTags?: ProcessTagDto[],
): ProcessTagDto[] {
return additionalTags ? [...primaryTags, ...additionalTags] : primaryTags;
}
private ensureNoDuplicateTags(tags: ProcessTagDto[]): void {
const duplicates = this.findDuplicateTags(tags);
if (duplicates.length > 0) {
throw new HttpException(
`Duplicate tags found: ${duplicates.join(', ')}`,
HttpStatus.BAD_REQUEST,
);
}
}
private validateTagsInput(tags: ProcessTagDto[]): void {
if (!tags?.length) {
return;
}
}
getSubspaceTagsToBeAdded(
spaceTags?: ModifyTagDto[],
subspaceModels?: ModifySubspaceDto[],
): ModifyTagDto[] {
if (!subspaceModels || subspaceModels.length === 0) {
return spaceTags;
}
const spaceTagsToDelete = spaceTags?.filter(
(tag) => tag.action === 'delete',
);
const tagsToAdd = subspaceModels.flatMap(
(subspace) => subspace.tags?.filter((tag) => tag.action === 'add') || [],
);
const commonTagUuids = new Set(
tagsToAdd
.filter((tagToAdd) =>
spaceTagsToDelete.some(
(tagToDelete) => tagToAdd.tagUuid === tagToDelete.tagUuid,
),
)
.map((tag) => tag.tagUuid),
);
const remainingTags = spaceTags.filter(
(tag) => !commonTagUuids.has(tag.tagUuid), // Exclude tags in commonTagUuids
);
return remainingTags;
}
getModifiedSubspaces(
spaceTags?: ModifyTagDto[],
subspaceModels?: ModifySubspaceDto[],
): ModifySubspaceDto[] {
if (!subspaceModels || subspaceModels.length === 0) {
return [];
}
// Extract tags marked for addition in spaceTags
const spaceTagsToAdd = spaceTags?.filter((tag) => tag.action === 'add');
const subspaceTagsToAdd = subspaceModels.flatMap(
(subspace) => subspace.tags?.filter((tag) => tag.action === 'add') || [],
);
const subspaceTagsToDelete = subspaceModels.flatMap(
(subspace) =>
subspace.tags?.filter((tag) => tag.action === 'delete') || [],
);
const subspaceTagsToDeleteUuids = new Set(
subspaceTagsToDelete.map((tag) => tag.tagUuid),
);
const commonTagsInSubspaces = subspaceTagsToAdd.filter((tag) =>
subspaceTagsToDeleteUuids.has(tag.tagUuid),
);
// Find UUIDs of tags that are common between spaceTagsToAdd and subspace tags marked for deletion
const commonTagUuids = new Set(
spaceTagsToAdd
.flatMap((tagToAdd) =>
subspaceModels.flatMap(
(subspace) =>
subspace.tags?.filter(
(tagToDelete) =>
tagToDelete.action === 'delete' &&
tagToAdd.tagUuid === tagToDelete.tagUuid,
) || [],
),
)
.map((tag) => tag.tagUuid),
);
// Modify subspaceModels by removing tags with UUIDs present in commonTagUuids
let modifiedSubspaces = subspaceModels.map((subspace) => ({
...subspace,
tags:
subspace.tags?.filter((tag) => !commonTagUuids.has(tag.tagUuid)) || [],
}));
modifiedSubspaces = modifiedSubspaces.map((subspace) => ({
...subspace,
tags:
subspace.tags?.filter(
(tag) =>
!(
tag.action === 'delete' &&
commonTagsInSubspaces.some(
(commonTag) => commonTag.tagUuid === tag.tagUuid,
)
),
) || [],
}));
return modifiedSubspaces;
}
constructor() {}
}

View File

@ -1,14 +1,82 @@
import { DeviceStatusFirebaseService } from '@app/common/firebase/devices-status/services/devices-status.service';
import { AqiDataService } from '@app/common/helper/services/aqi.data.service';
import { OccupancyService } from '@app/common/helper/services/occupancy.service';
import { PowerClampService } from '@app/common/helper/services/power.clamp.service';
import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service';
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
import {
InviteUserRepository,
InviteUserSpaceRepository,
} from '@app/common/modules/Invite-user/repositiories';
import { AutomationRepository } from '@app/common/modules/automation/repositories';
import { CommunityRepository } from '@app/common/modules/community/repositories';
import { DeviceStatusLogRepository } from '@app/common/modules/device-status-log/repositories';
import {
DeviceRepository,
DeviceUserPermissionRepository,
} from '@app/common/modules/device/repositories';
import { PermissionTypeRepository } from '@app/common/modules/permission/repositories';
import {
PowerClampDailyRepository,
PowerClampHourlyRepository,
PowerClampMonthlyRepository,
} from '@app/common/modules/power-clamp/repositories';
import { ProductRepository } from '@app/common/modules/product/repositories';
import { ProjectRepository } from '@app/common/modules/project/repositiories';
import { RegionRepository } from '@app/common/modules/region/repositories';
import { SceneDeviceRepository } from '@app/common/modules/scene-device/repositories';
import {
SceneIconRepository,
SceneRepository,
} from '@app/common/modules/scene/repositories';
import {
SpaceModelProductAllocationRepoitory,
SpaceModelRepository,
SubspaceModelProductAllocationRepoitory,
SubspaceModelRepository,
} from '@app/common/modules/space-model';
import {
InviteSpaceRepository,
SpaceLinkRepository,
SpaceProductAllocationRepository,
SpaceRepository,
} from '@app/common/modules/space/repositories';
import {
SubspaceProductAllocationRepository,
SubspaceRepository,
} from '@app/common/modules/space/repositories/subspace.repository';
import { SpaceRepositoryModule } from '@app/common/modules/space/space.repository.module';
import { NewTagRepository } from '@app/common/modules/tag/repositories/tag-repository';
import { TimeZoneRepository } from '@app/common/modules/timezone/repositories';
import {
UserRepository,
UserSpaceRepository,
} from '@app/common/modules/user/repositories';
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { CqrsModule } from '@nestjs/cqrs';
import { CommunityModule } from 'src/community/community.module';
import { DeviceService } from 'src/device/services';
import {
SpaceModelService,
SubSpaceModelService,
} from 'src/space-model/services';
import { SpaceModelProductAllocationService } from 'src/space-model/services/space-model-product-allocation.service';
import { SubspaceModelProductAllocationService } from 'src/space-model/services/subspace/subspace-model-product-allocation.service';
import { TagService as NewTagService } from 'src/tags/services/tags.service';
import { UserDevicePermissionService } from 'src/user-device-permission/services';
import { UserService, UserSpaceService } from 'src/users/services';
import { SceneService } from '../scene/services';
import {
SpaceController,
SpaceDeviceController,
SpaceSceneController,
SpaceUserController,
SubSpaceController,
SubSpaceDeviceController,
SpaceSceneController,
} from './controllers';
import { SpaceValidationController } from './controllers/space-validation.controller';
import { DisableSpaceHandler } from './handlers';
import {
SpaceDeviceService,
SpaceLinkService,
@ -17,81 +85,10 @@ import {
SpaceUserService,
SubspaceDeviceService,
SubSpaceService,
ValidationService,
} from './services';
import {
SpaceRepository,
SpaceLinkRepository,
TagRepository,
InviteSpaceRepository,
SpaceProductAllocationRepository,
} from '@app/common/modules/space/repositories';
import { CommunityRepository } from '@app/common/modules/community/repositories';
import {
UserRepository,
UserSpaceRepository,
} from '@app/common/modules/user/repositories';
import {
DeviceRepository,
DeviceUserPermissionRepository,
} from '@app/common/modules/device/repositories';
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
import { ProductRepository } from '@app/common/modules/product/repositories';
import { SceneService } from '../scene/services';
import {
SceneIconRepository,
SceneRepository,
} from '@app/common/modules/scene/repositories';
import { DeviceService } from 'src/device/services';
import { DeviceStatusFirebaseService } from '@app/common/firebase/devices-status/services/devices-status.service';
import { DeviceStatusLogRepository } from '@app/common/modules/device-status-log/repositories';
import { SceneDeviceRepository } from '@app/common/modules/scene-device/repositories';
import { ProjectRepository } from '@app/common/modules/project/repositiories';
import {
SpaceModelProductAllocationRepoitory,
SpaceModelRepository,
SubspaceModelProductAllocationRepoitory,
SubspaceModelRepository,
TagModelRepository,
} from '@app/common/modules/space-model';
import { CommunityModule } from 'src/community/community.module';
import { ValidationService } from './services';
import {
SubspaceProductAllocationRepository,
SubspaceRepository,
} from '@app/common/modules/space/repositories/subspace.repository';
import { TagService } from './services/tag';
import {
SpaceModelService,
SubSpaceModelService,
} from 'src/space-model/services';
import { UserService, UserSpaceService } from 'src/users/services';
import { UserDevicePermissionService } from 'src/user-device-permission/services';
import { PermissionTypeRepository } from '@app/common/modules/permission/repositories';
import { CqrsModule } from '@nestjs/cqrs';
import { DisableSpaceHandler } from './handlers';
import { RegionRepository } from '@app/common/modules/region/repositories';
import { TimeZoneRepository } from '@app/common/modules/timezone/repositories';
import {
InviteUserRepository,
InviteUserSpaceRepository,
} from '@app/common/modules/Invite-user/repositiories';
import { AutomationRepository } from '@app/common/modules/automation/repositories';
import { TagService as NewTagService } from 'src/tags/services/tags.service';
import { NewTagRepository } from '@app/common/modules/tag/repositories/tag-repository';
import { SpaceModelProductAllocationService } from 'src/space-model/services/space-model-product-allocation.service';
import { SubspaceModelProductAllocationService } from 'src/space-model/services/subspace/subspace-model-product-allocation.service';
import { SpaceProductAllocationService } from './services/space-product-allocation.service';
import { SubspaceProductAllocationService } from './services/subspace/subspace-product-allocation.service';
import { SpaceValidationController } from './controllers/space-validation.controller';
import { PowerClampService } from '@app/common/helper/services/power.clamp.service';
import {
PowerClampHourlyRepository,
PowerClampDailyRepository,
PowerClampMonthlyRepository,
} from '@app/common/modules/power-clamp/repositories';
import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service';
import { OccupancyService } from '@app/common/helper/services/occupancy.service';
import { AqiDataService } from '@app/common/helper/services/aqi.data.service';
export const CommandHandlers = [DisableSpaceHandler];
@ -108,11 +105,8 @@ export const CommandHandlers = [DisableSpaceHandler];
],
providers: [
ValidationService,
TagModelRepository,
TagRepository,
SpaceService,
TuyaService,
TagService,
ProductRepository,
SubSpaceService,
SpaceDeviceService,
@ -152,7 +146,6 @@ export const CommandHandlers = [DisableSpaceHandler];
InviteUserRepository,
InviteUserSpaceRepository,
AutomationRepository,
TagService,
NewTagService,
SpaceModelProductAllocationRepoitory,
SubspaceModelProductAllocationRepoitory,

View File

@ -1,22 +1,22 @@
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
import { ProjectParam } from '@app/common/dto/project-param.dto';
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
import { ProductEntity } from '@app/common/modules/product/entities';
import { ProductRepository } from '@app/common/modules/product/repositories';
import { ProjectEntity } from '@app/common/modules/project/entities';
import { ProjectRepository } from '@app/common/modules/project/repositiories';
import { NewTagEntity } from '@app/common/modules/tag';
import { NewTagRepository } from '@app/common/modules/tag/repositories/tag-repository';
import {
Injectable,
ConflictException,
NotFoundException,
HttpException,
HttpStatus,
Injectable,
NotFoundException,
} from '@nestjs/common';
import { CreateTagDto } from '../dtos/tags.dto';
import { ProductEntity } from '@app/common/modules/product/entities';
import { ProjectEntity } from '@app/common/modules/project/entities';
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
import { NewTagEntity } from '@app/common/modules/tag';
import { BulkCreateTagsDto, ProcessTagDto } from '../dtos';
import { In, QueryRunner } from 'typeorm';
import { ProjectParam } from '@app/common/dto/project-param.dto';
import { ProcessTagDto } from '../dtos';
import { CreateTagDto } from '../dtos/tags.dto';
@Injectable()
export class TagService {
@ -32,7 +32,7 @@ export class TagService {
const tags = await this.tagRepository.find({
where: { project: { uuid: projectUuid } },
relations: ['product', 'project'],
relations: ['project'],
});
return new SuccessResponseDto({
@ -66,74 +66,65 @@ export class TagService {
});
}
/**
* Processes an array of tag DTOs, creating or updating tags in the database.
* @param tagDtos - The array of tag DTOs to process.
* @param projectUuid - The UUID of the project to associate the tags with.
* @param queryRunner - Optional TypeORM query runner for transaction management.
* @returns An array of the processed tag entities.
*/
async processTags(
tagDtos: ProcessTagDto[],
projectUuid: string,
queryRunner?: QueryRunner,
): Promise<NewTagEntity[]> {
try {
const dbManager = queryRunner
? queryRunner.manager
: this.tagRepository.manager;
if (!tagDtos || tagDtos.length === 0) {
return [];
}
const newTagDtos: CreateTagDto[] = [];
const existingTagUuids: string[] = [];
let fetchedExistingTags: NewTagEntity[] = [];
const directlyFetchedTags: NewTagEntity[] = [];
const [tagsWithUuid, tagsWithoutUuid]: [
Pick<ProcessTagDto, 'uuid' | 'productUuid'>[],
Omit<ProcessTagDto, 'uuid'>[],
] = this.splitTagsByUuid(tagDtos);
// Separate existing tag UUIDs and new tag DTOs
for (const tagDto of tagDtos) {
if (tagDto.uuid) {
existingTagUuids.push(tagDto.uuid);
} else {
if (!tagDto.name || !tagDto.productUuid) {
throw new HttpException(
`Tag name or product UUID is missing`,
HttpStatus.BAD_REQUEST,
);
}
// create a set of unique existing tag names for the project
const upsertedTagsByNameResult = await dbManager.upsert(
NewTagEntity,
Array.from(
new Set<string>(tagsWithoutUuid.map((tag) => tag.name)).values(),
).map((name) => ({
name,
project: { uuid: projectUuid },
})),
['name', 'project'],
);
const existingTag = await queryRunner.manager.findOne(NewTagEntity, {
where: {
name: tagDto.name,
product: { uuid: tagDto.productUuid },
project: { uuid: projectUuid },
},
relations: ['product'],
});
if (!existingTag) {
newTagDtos.push(tagDto);
} else {
directlyFetchedTags.push(existingTag);
}
}
}
const createdTagsByName = await dbManager.find(NewTagEntity, {
where: {
uuid: In(upsertedTagsByNameResult.identifiers.map((id) => id.uuid)),
},
});
let foundByUuidTags: NewTagEntity[] = [];
// Fetch existing tags using UUIDs
if (existingTagUuids.length > 0) {
fetchedExistingTags = await (queryRunner
? queryRunner.manager.find(NewTagEntity, {
where: {
uuid: In(existingTagUuids),
project: { uuid: projectUuid },
},
relations: ['product'],
})
: this.tagRepository.find({
where: {
uuid: In(existingTagUuids),
project: { uuid: projectUuid },
},
relations: ['product'],
}));
if (tagsWithUuid.length) {
foundByUuidTags = await dbManager.find(NewTagEntity, {
where: {
uuid: In([...tagsWithUuid.map((tag) => tag.uuid)]),
project: { uuid: projectUuid },
},
});
}
// Ensure all provided UUIDs exist in the database
if (fetchedExistingTags.length !== existingTagUuids.length) {
const foundUuids = new Set(fetchedExistingTags.map((tag) => tag.uuid));
const missingUuids = existingTagUuids.filter(
(uuid) => !foundUuids.has(uuid),
if (foundByUuidTags.length !== tagsWithUuid.length) {
const foundUuids = new Set(foundByUuidTags.map((tag) => tag.uuid));
const missingUuids = tagsWithUuid.filter(
({ uuid }) => !foundUuids.has(uuid),
);
throw new HttpException(
@ -142,23 +133,7 @@ export class TagService {
);
}
let newlyCreatedTags: NewTagEntity[] = [];
if (newTagDtos.length > 0) {
newlyCreatedTags = await this.bulkCreateTags(
{ projectUuid, tags: newTagDtos },
queryRunner,
);
}
// Combine all found and created tags
const allTags = [
...fetchedExistingTags,
...newlyCreatedTags,
...directlyFetchedTags,
];
return allTags;
return [...foundByUuidTags, ...createdTagsByName];
} catch (error) {
console.error(error);
throw new HttpException(
@ -172,79 +147,6 @@ export class TagService {
}
}
async bulkCreateTags(
dto: BulkCreateTagsDto,
queryRunner?: QueryRunner,
): Promise<NewTagEntity[]> {
try {
const { projectUuid, tags } = dto;
const newTags: NewTagEntity[] = [];
const project = await this.getProjectByUuid(projectUuid);
// Extract unique product UUIDs
const productUuids = Array.from(
new Set(tags.map((tag) => tag.productUuid)),
);
const productMap = await this.getProductMap(productUuids);
// Fetch existing tag names for this project
const existingTags = await this.tagRepository.find({
where: { project: { uuid: projectUuid } },
relations: ['product'],
});
// Convert existing tags into a Map for quick lookup
const existingTagMap = new Map<string, string | null>();
existingTags.forEach((tag) => {
existingTagMap.set(tag.name, tag.product?.uuid || null);
});
for (const tag of tags) {
const existingProductUuid = existingTagMap.get(tag.name);
if (existingProductUuid) {
if (existingProductUuid !== tag.productUuid) {
throw new HttpException(
`Tag "${tag.name}" already exists but is associated with a different product.`,
HttpStatus.CONFLICT,
);
}
} else {
newTags.push(
this.tagRepository.create({
name: tag.name,
product: productMap.get(tag.productUuid),
project,
}),
);
}
}
if (newTags.length > 0) {
if (queryRunner) {
await queryRunner.manager.save(NewTagEntity, newTags);
} else {
await this.tagRepository.save(newTags);
}
} else {
}
return newTags;
} catch (error) {
console.error(error);
throw new HttpException(
error instanceof HttpException
? error.message
: 'An unexpected error occurred while creating tags',
error instanceof HttpException
? error.getStatus()
: HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
private async getProductByUuid(uuid: string): Promise<ProductEntity> {
const product = await this.productRepository.findOne({ where: { uuid } });
if (!product) {
@ -276,30 +178,25 @@ export class TagService {
}
}
private async getProductMap(
uuids: string[],
): Promise<Map<string, ProductEntity>> {
const products = await this.productRepository.find({
where: { uuid: In(uuids) },
});
if (products.length !== uuids.length) {
const foundUuids = new Set(products.map((p) => p.uuid));
const missingUuids = uuids.filter((id) => !foundUuids.has(id));
throw new NotFoundException(
`Products not found for UUIDs: ${missingUuids.join(', ')}`,
);
}
return new Map(products.map((product) => [product.uuid, product]));
}
private async getExistingTagNames(projectUuid: string): Promise<Set<string>> {
const tags = await this.tagRepository.find({
where: { project: { uuid: projectUuid } },
select: ['name'],
});
return new Set(tags.map((tag) => tag.name));
private splitTagsByUuid(
tagDtos: ProcessTagDto[],
): [ProcessTagDto[], ProcessTagDto[]] {
return tagDtos.reduce<[ProcessTagDto[], ProcessTagDto[]]>(
([withUuid, withoutUuid], tag) => {
if (tag.uuid) {
withUuid.push(tag);
} else {
if (!tag.name || !tag.productUuid) {
throw new HttpException(
`Tag name or product UUID is missing`,
HttpStatus.BAD_REQUEST,
);
}
withoutUuid.push(tag);
}
return [withUuid, withoutUuid];
},
[[], []],
);
}
}