diff --git a/libs/common/src/common.module.ts b/libs/common/src/common.module.ts index d63ff67..91e5507 100644 --- a/libs/common/src/common.module.ts +++ b/libs/common/src/common.module.ts @@ -9,7 +9,7 @@ import { EmailService } from './util/email.service'; import { ErrorMessageService } from 'src/error-message/error-message.service'; import { TuyaService } from './integrations/tuya/services/tuya.service'; import { SceneDeviceRepository } from './modules/scene-device/repositories'; -import { SpaceProductItemRepository, SpaceRepository } from './modules/space'; +import { SpaceRepository } from './modules/space'; import { SpaceModelRepository, SubspaceModelRepository, @@ -26,7 +26,6 @@ import { SubspaceRepository } from './modules/space/repositories/subspace.reposi SubspaceRepository, SubspaceModelRepository, SpaceModelRepository, - SpaceProductItemRepository, ], exports: [ CommonService, diff --git a/libs/common/src/database/database.module.ts b/libs/common/src/database/database.module.ts index 8fa7f07..7e8dbb6 100644 --- a/libs/common/src/database/database.module.ts +++ b/libs/common/src/database/database.module.ts @@ -11,10 +11,7 @@ import { PermissionTypeEntity } from '../modules/permission/entities'; import { SpaceEntity, SpaceLinkEntity, - SpaceProductItemEntity, SubspaceEntity, - SubspaceProductEntity, - SubspaceProductItemEntity, } from '../modules/space/entities'; import { UserSpaceEntity } from '../modules/user/entities'; import { DeviceUserPermissionEntity } from '../modules/device/entities'; @@ -28,7 +25,6 @@ import { CommunityEntity } from '../modules/community/entities'; import { DeviceStatusLogEntity } from '../modules/device-status-log/entities'; import { SceneEntity, SceneIconEntity } from '../modules/scene/entities'; import { SceneDeviceEntity } from '../modules/scene-device/entities'; -import { SpaceProductEntity } from '../modules/space/entities/space-product.entity'; import { ProjectEntity } from '../modules/project/entities'; import { SpaceModelEntity, @@ -65,7 +61,6 @@ import { SpaceEntity, SpaceLinkEntity, SubspaceEntity, - SpaceProductEntity, UserSpaceEntity, DeviceUserPermissionEntity, RoleTypeEntity, @@ -81,10 +76,6 @@ import { SpaceModelEntity, SubspaceModelEntity, TagModel, - SpaceProductEntity, - SpaceProductItemEntity, - SubspaceProductEntity, - SubspaceProductItemEntity, InviteUserEntity, InviteUserSpaceEntity, ], diff --git a/libs/common/src/modules/device/entities/device.entity.ts b/libs/common/src/modules/device/entities/device.entity.ts index ade7d99..9a75950 100644 --- a/libs/common/src/modules/device/entities/device.entity.ts +++ b/libs/common/src/modules/device/entities/device.entity.ts @@ -6,15 +6,10 @@ import { Unique, Index, JoinColumn, - OneToOne, } from 'typeorm'; import { AbstractEntity } from '../../abstract/entities/abstract.entity'; import { DeviceDto, DeviceUserPermissionDto } from '../dtos/device.dto'; -import { - SpaceEntity, - SpaceProductItemEntity, - SubspaceEntity, -} from '../../space/entities'; +import { SpaceEntity, SubspaceEntity } from '../../space/entities'; import { ProductEntity } from '../../product/entities'; import { UserEntity } from '../../user/entities'; import { DeviceNotificationDto } from '../dtos'; @@ -79,11 +74,6 @@ export class DeviceEntity extends AbstractEntity { @OneToMany(() => SceneDeviceEntity, (sceneDevice) => sceneDevice.device, {}) sceneDevices: SceneDeviceEntity[]; - @OneToOne(() => SpaceProductItemEntity, (tag) => tag.device, { - nullable: true, - }) - public tag?: SpaceProductItemEntity; - constructor(partial: Partial) { super(); Object.assign(this, partial); diff --git a/libs/common/src/modules/product/entities/product.entity.ts b/libs/common/src/modules/product/entities/product.entity.ts index 6de8ced..79ca211 100644 --- a/libs/common/src/modules/product/entities/product.entity.ts +++ b/libs/common/src/modules/product/entities/product.entity.ts @@ -2,7 +2,6 @@ import { Column, Entity, OneToMany } from 'typeorm'; import { ProductDto } from '../dtos'; import { AbstractEntity } from '../../abstract/entities/abstract.entity'; import { DeviceEntity } from '../../device/entities'; -import { SpaceProductEntity } from '../../space/entities/space-product.entity'; import { TagModel } from '../../space-model'; @Entity({ name: 'product' }) @@ -28,9 +27,6 @@ export class ProductEntity extends AbstractEntity { }) public prodType: string; - @OneToMany(() => SpaceProductEntity, (spaceProduct) => spaceProduct.product) - spaceProducts: SpaceProductEntity[]; - @OneToMany(() => TagModel, (tag) => tag.product) tagModels: TagModel[]; diff --git a/libs/common/src/modules/space/dtos/index.ts b/libs/common/src/modules/space/dtos/index.ts index b470336..fcc0fdd 100644 --- a/libs/common/src/modules/space/dtos/index.ts +++ b/libs/common/src/modules/space/dtos/index.ts @@ -1,4 +1,2 @@ export * from './space.dto'; export * from './subspace.dto'; -export * from './space-product-item.dto'; -export * from './space-product.dto'; diff --git a/libs/common/src/modules/space/dtos/space-product-item.dto.ts b/libs/common/src/modules/space/dtos/space-product-item.dto.ts deleted file mode 100644 index 8973c1a..0000000 --- a/libs/common/src/modules/space/dtos/space-product-item.dto.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { IsString, IsNotEmpty } from 'class-validator'; - -export class SpaceProductItemDto { - @IsString() - @IsNotEmpty() - uuid: string; - - @IsString() - @IsNotEmpty() - tag: string; - - @IsString() - @IsNotEmpty() - spaceProductUuid: string; -} diff --git a/libs/common/src/modules/space/dtos/space-product.dto.ts b/libs/common/src/modules/space/dtos/space-product.dto.ts deleted file mode 100644 index 92f5f71..0000000 --- a/libs/common/src/modules/space/dtos/space-product.dto.ts +++ /dev/null @@ -1,19 +0,0 @@ -import { ApiProperty } from '@nestjs/swagger'; -import { IsString, IsNotEmpty } from 'class-validator'; -import { SpaceProductItemDto } from './space-product-item.dto'; - -export class SpaceProductModelDto { - @IsString() - @IsNotEmpty() - uuid: string; - - @IsString() - @IsNotEmpty() - productUuid: string; - - @ApiProperty({ - description: 'List of individual items with specific names for the product', - type: [SpaceProductItemDto], - }) - items: SpaceProductItemDto[]; -} diff --git a/libs/common/src/modules/space/entities/index.ts b/libs/common/src/modules/space/entities/index.ts index f07ec93..1d25b03 100644 --- a/libs/common/src/modules/space/entities/index.ts +++ b/libs/common/src/modules/space/entities/index.ts @@ -1,5 +1,3 @@ export * from './space.entity'; export * from './subspace'; -export * from './space-product.entity'; -export * from './space-product-item.entity'; export * from './space-link.entity'; diff --git a/libs/common/src/modules/space/entities/space-product-item.entity.ts b/libs/common/src/modules/space/entities/space-product-item.entity.ts deleted file mode 100644 index 8f93264..0000000 --- a/libs/common/src/modules/space/entities/space-product-item.entity.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { Column, Entity, ManyToOne, OneToOne } from 'typeorm'; -import { SpaceProductEntity } from './space-product.entity'; -import { AbstractEntity } from '../../abstract/entities/abstract.entity'; -import { SpaceProductItemDto } from '../dtos'; -import { DeviceEntity } from '../../device/entities'; - -@Entity({ name: 'space-product-item' }) -export class SpaceProductItemEntity extends AbstractEntity { - @Column({ - nullable: false, - }) - public tag: string; - - @ManyToOne(() => SpaceProductEntity, (spaceProduct) => spaceProduct.items, { - nullable: false, - }) - public spaceProduct: SpaceProductEntity; - - @Column({ - nullable: false, - default: false, - }) - public disabled: boolean; - - @OneToOne(() => DeviceEntity, (device) => device.tag, { - nullable: true, - }) - public device?: DeviceEntity; -} diff --git a/libs/common/src/modules/space/entities/space-product.entity.ts b/libs/common/src/modules/space/entities/space-product.entity.ts deleted file mode 100644 index 4e333cd..0000000 --- a/libs/common/src/modules/space/entities/space-product.entity.ts +++ /dev/null @@ -1,38 +0,0 @@ -import { Column, Entity, ManyToOne, JoinColumn, OneToMany } from 'typeorm'; -import { SpaceEntity } from './space.entity'; -import { AbstractEntity } from '../../abstract/entities/abstract.entity'; -import { ProductEntity } from '../../product/entities'; -import { SpaceProductItemEntity } from './space-product-item.entity'; - -@Entity({ name: 'space-product' }) -export class SpaceProductEntity extends AbstractEntity { - @ManyToOne(() => SpaceEntity, (space) => space.spaceProducts, { - nullable: false, - onDelete: 'CASCADE', - }) - @JoinColumn({ name: 'space_uuid' }) - space: SpaceEntity; - - @ManyToOne(() => ProductEntity, (product) => product.spaceProducts, { - nullable: false, - onDelete: 'CASCADE', - }) - @JoinColumn({ name: 'product_uuid' }) - product: ProductEntity; - - @Column({ - nullable: false, - default: false, - }) - public disabled: boolean; - - @OneToMany(() => SpaceProductItemEntity, (item) => item.spaceProduct, { - cascade: true, - }) - public items: SpaceProductItemEntity[]; - - constructor(partial: Partial) { - super(); - Object.assign(this, partial); - } -} diff --git a/libs/common/src/modules/space/entities/space.entity.ts b/libs/common/src/modules/space/entities/space.entity.ts index d6133da..9061df1 100644 --- a/libs/common/src/modules/space/entities/space.entity.ts +++ b/libs/common/src/modules/space/entities/space.entity.ts @@ -13,7 +13,6 @@ import { DeviceEntity } from '../../device/entities'; import { CommunityEntity } from '../../community/entities'; import { SubspaceEntity } from './subspace'; import { SpaceLinkEntity } from './space-link.entity'; -import { SpaceProductEntity } from './space-product.entity'; import { SceneEntity } from '../../scene/entities'; import { SpaceModelEntity } from '../../space-model'; import { InviteUserSpaceEntity } from '../../Invite-user/entities'; @@ -100,9 +99,6 @@ export class SpaceEntity extends AbstractEntity { }) public icon: string; - @OneToMany(() => SpaceProductEntity, (spaceProduct) => spaceProduct.space) - spaceProducts: SpaceProductEntity[]; - @OneToMany(() => SceneEntity, (scene) => scene.space) scenes: SceneEntity[]; diff --git a/libs/common/src/modules/space/entities/subspace/index.ts b/libs/common/src/modules/space/entities/subspace/index.ts index 471b7b1..be13961 100644 --- a/libs/common/src/modules/space/entities/subspace/index.ts +++ b/libs/common/src/modules/space/entities/subspace/index.ts @@ -1,3 +1 @@ export * from './subspace.entity'; -export * from './subspace-product.entity'; -export * from './subspace-product-item.entity'; diff --git a/libs/common/src/modules/space/entities/subspace/subspace-product-item.entity.ts b/libs/common/src/modules/space/entities/subspace/subspace-product-item.entity.ts deleted file mode 100644 index eae8a75..0000000 --- a/libs/common/src/modules/space/entities/subspace/subspace-product-item.entity.ts +++ /dev/null @@ -1,32 +0,0 @@ -import { AbstractEntity } from '@app/common/modules/abstract/entities/abstract.entity'; -import { SpaceProductItemDto } from '../../dtos'; -import { Column, Entity, ManyToOne } from 'typeorm'; -import { SubspaceProductEntity } from './subspace-product.entity'; - -@Entity({ name: 'subspace-product-item' }) -export class SubspaceProductItemEntity extends AbstractEntity { - @Column({ - nullable: false, - }) - public tag: string; - - @ManyToOne( - () => SubspaceProductEntity, - (subspaceProduct) => subspaceProduct.items, - { - nullable: false, - }, - ) - public subspaceProduct: SubspaceProductEntity; - - @Column({ - nullable: false, - default: false, - }) - public disabled: boolean; - - constructor(partial: Partial) { - super(); - Object.assign(this, partial); - } -} diff --git a/libs/common/src/modules/space/entities/subspace/subspace-product.entity.ts b/libs/common/src/modules/space/entities/subspace/subspace-product.entity.ts deleted file mode 100644 index b5a16cc..0000000 --- a/libs/common/src/modules/space/entities/subspace/subspace-product.entity.ts +++ /dev/null @@ -1,38 +0,0 @@ -import { ProductEntity } from '@app/common/modules/product/entities'; -import { Column, Entity, ManyToOne, OneToMany } from 'typeorm'; -import { SubspaceEntity } from './subspace.entity'; -import { AbstractEntity } from '@app/common/modules/abstract/entities/abstract.entity'; -import { SubspaceProductItemEntity } from './subspace-product-item.entity'; -import { SpaceProductModelDto } from '../../dtos'; - -@Entity({ name: 'subspace-product' }) -export class SubspaceProductEntity extends AbstractEntity { - @Column({ - type: 'uuid', - default: () => 'gen_random_uuid()', - nullable: false, - }) - public uuid: string; - - @Column({ - nullable: false, - default: false, - }) - public disabled: boolean; - - @ManyToOne(() => SubspaceEntity, (subspace) => subspace.subspaceProducts, { - nullable: false, - }) - public subspace: SubspaceEntity; - - @ManyToOne(() => ProductEntity, (product) => product.spaceProducts, { - nullable: false, - }) - public product: ProductEntity; - - @OneToMany(() => SubspaceProductItemEntity, (item) => item.subspaceProduct, { - nullable: true, - }) - public items: SubspaceProductItemEntity[]; - -} diff --git a/libs/common/src/modules/space/entities/subspace/subspace.entity.ts b/libs/common/src/modules/space/entities/subspace/subspace.entity.ts index 6ad7751..036df46 100644 --- a/libs/common/src/modules/space/entities/subspace/subspace.entity.ts +++ b/libs/common/src/modules/space/entities/subspace/subspace.entity.ts @@ -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 { SubspaceProductEntity } from './subspace-product.entity'; @Entity({ name: 'subspace' }) export class SubspaceEntity extends AbstractEntity { @@ -41,15 +40,6 @@ export class SubspaceEntity extends AbstractEntity { @JoinColumn({ name: 'subspace_model_uuid' }) subSpaceModel?: SubspaceModelEntity; - @OneToMany( - () => SubspaceProductEntity, - (subspaceProduct) => subspaceProduct.subspace, - { - nullable: true, - }, - ) - public subspaceProducts: SubspaceProductEntity[]; - constructor(partial: Partial) { super(); Object.assign(this, partial); diff --git a/libs/common/src/modules/space/repositories/space.repository.ts b/libs/common/src/modules/space/repositories/space.repository.ts index 66f96a4..a769302 100644 --- a/libs/common/src/modules/space/repositories/space.repository.ts +++ b/libs/common/src/modules/space/repositories/space.repository.ts @@ -1,11 +1,6 @@ import { DataSource, Repository } from 'typeorm'; import { Injectable } from '@nestjs/common'; -import { SpaceProductEntity } from '../entities/space-product.entity'; -import { - SpaceEntity, - SpaceLinkEntity, - SpaceProductItemEntity, -} from '../entities'; +import { SpaceEntity, SpaceLinkEntity } from '../entities'; @Injectable() export class SpaceRepository extends Repository { @@ -20,16 +15,3 @@ export class SpaceLinkRepository extends Repository { super(SpaceLinkEntity, dataSource.createEntityManager()); } } -@Injectable() -export class SpaceProductRepository extends Repository { - constructor(private dataSource: DataSource) { - super(SpaceProductEntity, dataSource.createEntityManager()); - } -} - -@Injectable() -export class SpaceProductItemRepository extends Repository { - constructor(private dataSource: DataSource) { - super(SpaceProductItemEntity, dataSource.createEntityManager()); - } -} diff --git a/libs/common/src/modules/space/repositories/subspace.repository.ts b/libs/common/src/modules/space/repositories/subspace.repository.ts index 3682c05..5897510 100644 --- a/libs/common/src/modules/space/repositories/subspace.repository.ts +++ b/libs/common/src/modules/space/repositories/subspace.repository.ts @@ -1,9 +1,5 @@ import { DataSource, Repository } from 'typeorm'; -import { - SubspaceEntity, - SubspaceProductEntity, - SubspaceProductItemEntity, -} from '../entities'; +import { SubspaceEntity } from '../entities'; import { Injectable } from '@nestjs/common'; @Injectable() @@ -12,17 +8,3 @@ export class SubspaceRepository extends Repository { super(SubspaceEntity, dataSource.createEntityManager()); } } - -@Injectable() -export class SubspaceProductRepository extends Repository { - constructor(private dataSource: DataSource) { - super(SubspaceProductEntity, dataSource.createEntityManager()); - } -} - -@Injectable() -export class SubspaceProductItemRepository extends Repository { - constructor(private dataSource: DataSource) { - super(SubspaceProductItemEntity, dataSource.createEntityManager()); - } -} diff --git a/libs/common/src/modules/space/space.repository.module.ts b/libs/common/src/modules/space/space.repository.module.ts index b39f98d..90916c2 100644 --- a/libs/common/src/modules/space/space.repository.module.ts +++ b/libs/common/src/modules/space/space.repository.module.ts @@ -1,17 +1,11 @@ import { Module } from '@nestjs/common'; import { TypeOrmModule } from '@nestjs/typeorm'; -import { SpaceEntity, SubspaceEntity, SubspaceProductEntity } from './entities'; +import { SpaceEntity, SubspaceEntity } from './entities'; @Module({ providers: [], exports: [], controllers: [], - imports: [ - TypeOrmModule.forFeature([ - SpaceEntity, - SubspaceEntity, - SubspaceProductEntity, - ]), - ], + imports: [TypeOrmModule.forFeature([SpaceEntity, SubspaceEntity])], }) export class SpaceRepositoryModule {} diff --git a/src/space-model/space-model.module.ts b/src/space-model/space-model.module.ts index 09cc8dd..736245b 100644 --- a/src/space-model/space-model.module.ts +++ b/src/space-model/space-model.module.ts @@ -17,11 +17,7 @@ import { ProductRepository } from '@app/common/modules/product/repositories'; import { PropogateSubspaceHandler } from './handlers'; import { CqrsModule } from '@nestjs/cqrs'; import { SpaceRepository } from '@app/common/modules/space'; -import { - SubspaceProductItemRepository, - SubspaceProductRepository, - SubspaceRepository, -} from '@app/common/modules/space/repositories/subspace.repository'; +import { SubspaceRepository } from '@app/common/modules/space/repositories/subspace.repository'; const CommandHandlers = [PropogateSubspaceHandler]; @@ -38,8 +34,6 @@ const CommandHandlers = [PropogateSubspaceHandler]; SubspaceModelRepository, ProductRepository, SubspaceRepository, - SubspaceProductRepository, - SubspaceProductItemRepository, TagModelService, TagModelRepository, ], diff --git a/src/space/dtos/add.space.dto.ts b/src/space/dtos/add.space.dto.ts index 7c10c59..353acf2 100644 --- a/src/space/dtos/add.space.dto.ts +++ b/src/space/dtos/add.space.dto.ts @@ -12,35 +12,6 @@ import { } from 'class-validator'; import { AddSubspaceDto } from './subspace'; -export class CreateSpaceProductItemDto { - @ApiProperty({ - description: 'Specific name for the product item', - example: 'Light 1', - }) - @IsNotEmpty() - @IsString() - tag: string; -} - -export class ProductAssignmentDto { - @ApiProperty({ - description: 'UUID of the product to be assigned', - example: 'prod-uuid-1234', - }) - @IsNotEmpty() - productId: string; - - @ApiProperty({ - description: 'Specific names for each product item', - type: [CreateSpaceProductItemDto], - example: [{ tag: 'Light 1' }, { tag: 'Light 2' }, { tag: 'Light 3' }], - }) - @IsArray() - @ValidateNested({ each: true }) - @Type(() => CreateSpaceProductItemDto) - items: CreateSpaceProductItemDto[]; -} - export class AddSpaceDto { @ApiProperty({ description: 'Name of the space (e.g., Floor 1, Unit 101)', @@ -97,17 +68,6 @@ export class AddSpaceDto { @IsOptional() direction?: string; - @ApiProperty({ - description: 'List of products assigned to this space', - type: [ProductAssignmentDto], - required: false, - }) - @IsArray() - @ValidateNested({ each: true }) - @IsOptional() - @Type(() => ProductAssignmentDto) - products?: ProductAssignmentDto[]; - @ApiProperty({ description: 'List of subspaces included in the model', type: [AddSubspaceDto], diff --git a/src/space/dtos/subspace/add.subspace.dto.ts b/src/space/dtos/subspace/add.subspace.dto.ts index 6b5078b..a2b12e2 100644 --- a/src/space/dtos/subspace/add.subspace.dto.ts +++ b/src/space/dtos/subspace/add.subspace.dto.ts @@ -1,13 +1,5 @@ import { ApiProperty } from '@nestjs/swagger'; -import { Type } from 'class-transformer'; -import { - IsArray, - IsNotEmpty, - IsOptional, - IsString, - ValidateNested, -} from 'class-validator'; -import { ProductAssignmentDto } from '../add.space.dto'; +import { IsNotEmpty, IsString } from 'class-validator'; export class AddSubspaceDto { @ApiProperty({ @@ -17,15 +9,4 @@ export class AddSubspaceDto { @IsNotEmpty() @IsString() subspaceName: string; - - @ApiProperty({ - description: 'List of products assigned to this space', - type: [ProductAssignmentDto], - required: false, - }) - @IsArray() - @ValidateNested({ each: true }) - @IsOptional() - @Type(() => ProductAssignmentDto) - products?: ProductAssignmentDto[]; } diff --git a/src/space/services/index.ts b/src/space/services/index.ts index c67ccae..5f86e3d 100644 --- a/src/space/services/index.ts +++ b/src/space/services/index.ts @@ -4,6 +4,4 @@ export * from './space-device.service'; export * from './subspace'; export * from './space-link'; export * from './space-scene.service'; -export * from './space-products'; -export * from './space-product-items'; export * from './space-validation.service'; diff --git a/src/space/services/space-product-items/index.ts b/src/space/services/space-product-items/index.ts deleted file mode 100644 index fff8634..0000000 --- a/src/space/services/space-product-items/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './space-product-items.service'; diff --git a/src/space/services/space-product-items/space-product-items.service.ts b/src/space/services/space-product-items/space-product-items.service.ts deleted file mode 100644 index d9c141b..0000000 --- a/src/space/services/space-product-items/space-product-items.service.ts +++ /dev/null @@ -1,56 +0,0 @@ -import { - SpaceEntity, - SpaceProductEntity, - SpaceProductItemRepository, -} from '@app/common/modules/space'; -import { HttpException, HttpStatus, Injectable } from '@nestjs/common'; -import { CreateSpaceProductItemDto } from '../../dtos'; -import { QueryRunner } from 'typeorm'; -import { BaseProductItemService } from '../../common'; - -@Injectable() -export class SpaceProductItemService extends BaseProductItemService { - constructor( - private readonly spaceProductItemRepository: SpaceProductItemRepository, - ) { - super(); - } - - async createProductItem( - itemModelDtos: CreateSpaceProductItemDto[], - spaceProduct: SpaceProductEntity, - space: SpaceEntity, - queryRunner: QueryRunner, - ) { - if (!itemModelDtos?.length) return; - - const incomingTags = itemModelDtos.map((item) => item.tag); - - await this.validateTags(incomingTags, queryRunner, space.uuid); - - try { - const productItems = itemModelDtos.map((dto) => - queryRunner.manager.create(this.spaceProductItemRepository.target, { - tag: dto.tag, - spaceProduct, - }), - ); - - await this.saveProductItems( - productItems, - this.spaceProductItemRepository.target, - queryRunner, - ); - } catch (error) { - if (error instanceof HttpException) { - throw error; - } - - throw new HttpException( - error.message || - 'An unexpected error occurred while creating product items.', - HttpStatus.INTERNAL_SERVER_ERROR, - ); - } - } -} diff --git a/src/space/services/space-products/index.ts b/src/space/services/space-products/index.ts deleted file mode 100644 index d0b92d2..0000000 --- a/src/space/services/space-products/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './space-products.service'; diff --git a/src/space/services/space-products/space-products.service.ts b/src/space/services/space-products/space-products.service.ts deleted file mode 100644 index 4071428..0000000 --- a/src/space/services/space-products/space-products.service.ts +++ /dev/null @@ -1,176 +0,0 @@ -import { Injectable, HttpException, HttpStatus } from '@nestjs/common'; -import { ProductRepository } from '@app/common/modules/product/repositories'; -import { SpaceEntity } from '@app/common/modules/space/entities'; -import { SpaceProductEntity } from '@app/common/modules/space/entities/space-product.entity'; -import { In, QueryRunner } from 'typeorm'; -import { ProductAssignmentDto } from '../../dtos'; -import { SpaceProductItemService } from '../space-product-items'; -import { ProductEntity } from '@app/common/modules/product/entities'; -import { ProductService } from 'src/product/services'; - -@Injectable() -export class SpaceProductService { - constructor( - private readonly productRepository: ProductRepository, - private readonly spaceProductItemService: SpaceProductItemService, - private readonly productService: ProductService, - ) {} - - async assignProductsToSpace( - space: SpaceEntity, - products: ProductAssignmentDto[], - queryRunner: QueryRunner, - ): Promise { - let updatedProducts: SpaceProductEntity[] = []; - - try { - const uniqueProducts = this.validateUniqueProducts(products); - const productEntities = await this.getProductEntities(uniqueProducts); - const existingSpaceProducts = await this.getExistingSpaceProducts( - space, - queryRunner, - ); - - if (existingSpaceProducts) { - updatedProducts = await this.updateExistingProducts( - existingSpaceProducts, - uniqueProducts, - productEntities, - queryRunner, - ); - } - - const newProducts = await this.createNewProducts( - uniqueProducts, - productEntities, - space, - queryRunner, - ); - - return [...updatedProducts, ...newProducts]; - } catch (error) { - if (!(error instanceof HttpException)) { - throw new HttpException( - `An error occurred while assigning products to the space ${error}`, - HttpStatus.INTERNAL_SERVER_ERROR, - ); - } - throw error; - } - } - - private validateUniqueProducts( - products: ProductAssignmentDto[], - ): ProductAssignmentDto[] { - const productIds = new Set(); - const uniqueProducts = []; - - for (const product of products) { - if (productIds.has(product.productId)) { - throw new HttpException( - `Duplicate product ID found: ${product.productId}`, - HttpStatus.BAD_REQUEST, - ); - } - productIds.add(product.productId); - uniqueProducts.push(product); - } - - return uniqueProducts; - } - - private async getProductEntities( - products: ProductAssignmentDto[], - ): Promise> { - try { - const productIds = products.map((p) => p.productId); - const productEntities = await this.productRepository.find({ - where: { uuid: In(productIds) }, - }); - return new Map(productEntities.map((p) => [p.uuid, p])); - } catch (error) { - console.error('Error fetching product entities:', error); - throw new HttpException( - 'Failed to fetch product entities', - HttpStatus.INTERNAL_SERVER_ERROR, - ); - } - } - - private async getExistingSpaceProducts( - space: SpaceEntity, - queryRunner: QueryRunner, - ): Promise { - return queryRunner.manager.find(SpaceProductEntity, { - where: { space: { uuid: space.uuid } }, - relations: ['product'], - }); - } - - private async updateExistingProducts( - existingSpaceProducts: SpaceProductEntity[], - uniqueProducts: ProductAssignmentDto[], - productEntities: Map, - queryRunner: QueryRunner, - ): Promise { - const updatedProducts = []; - - for (const { productId } of uniqueProducts) { - productEntities.get(productId); - const existingProduct = existingSpaceProducts.find( - (spaceProduct) => spaceProduct.product.uuid === productId, - ); - - updatedProducts.push(existingProduct); - } - - if (updatedProducts.length > 0) { - await queryRunner.manager.save(SpaceProductEntity, updatedProducts); - } - - return updatedProducts; - } - - private async createNewProducts( - uniqueSpaceProducts: ProductAssignmentDto[], - productEntities: Map, - space: SpaceEntity, - queryRunner: QueryRunner, - ): Promise { - const newProducts = []; - - for (const uniqueSpaceProduct of uniqueSpaceProducts) { - const product = productEntities.get(uniqueSpaceProduct.productId); - await this.getProduct(uniqueSpaceProduct.productId); - - newProducts.push( - queryRunner.manager.create(SpaceProductEntity, { - space, - product, - }), - ); - } - if (newProducts.length > 0) { - await queryRunner.manager.save(SpaceProductEntity, newProducts); - - await Promise.all( - uniqueSpaceProducts.map((dto, index) => { - const spaceProduct = newProducts[index]; - return this.spaceProductItemService.createProductItem( - dto.items, - spaceProduct, - space, - queryRunner, - ); - }), - ); - } - - return newProducts; - } - - async getProduct(productId: string): Promise { - const product = await this.productService.findOne(productId); - return product.data; - } -} diff --git a/src/space/services/space.service.ts b/src/space/services/space.service.ts index 2cdf92a..2bd5308 100644 --- a/src/space/services/space.service.ts +++ b/src/space/services/space.service.ts @@ -17,7 +17,6 @@ import { BaseResponseDto } from '@app/common/dto/base.response.dto'; import { SpaceEntity } from '@app/common/modules/space/entities'; import { generateRandomString } from '@app/common/helper/randomString'; import { SpaceLinkService } from './space-link'; -import { SpaceProductService } from './space-products'; import { CreateSubspaceModelDto } from 'src/space-model/dtos'; import { SubSpaceService } from './subspace'; import { DataSource, Not } from 'typeorm'; @@ -30,7 +29,6 @@ export class SpaceService { private readonly dataSource: DataSource, private readonly spaceRepository: SpaceRepository, private readonly spaceLinkService: SpaceLinkService, - private readonly spaceProductService: SpaceProductService, private readonly subSpaceService: SubSpaceService, private readonly validationService: ValidationService, ) {} @@ -102,13 +100,6 @@ export class SpaceService { ); } - if (products && products.length > 0) { - await this.spaceProductService.assignProductsToSpace( - newSpace, - products, - queryRunner, - ); - } await queryRunner.commitTransaction(); return new SuccessResponseDto({ @@ -264,14 +255,9 @@ export class SpaceService { Object.assign(space, updateSpaceDto, { parent }); // Save the updated space - const updatedSpace = await queryRunner.manager.save(space); + await queryRunner.manager.save(space); if (products && products.length > 0) { - await this.spaceProductService.assignProductsToSpace( - updatedSpace, - products, - queryRunner, - ); } await queryRunner.commitTransaction(); diff --git a/src/space/services/subspace/index.ts b/src/space/services/subspace/index.ts index b51a84a..973d199 100644 --- a/src/space/services/subspace/index.ts +++ b/src/space/services/subspace/index.ts @@ -1,4 +1,2 @@ export * from './subspace.service'; export * from './subspace-device.service'; -export * from './subspace-product-item.service'; -export * from './subspace-product.service'; diff --git a/src/space/services/subspace/subspace-product-item.service.ts b/src/space/services/subspace/subspace-product-item.service.ts deleted file mode 100644 index 3eefb2f..0000000 --- a/src/space/services/subspace/subspace-product-item.service.ts +++ /dev/null @@ -1,50 +0,0 @@ -import { Injectable, HttpException, HttpStatus } from '@nestjs/common'; -import { QueryRunner } from 'typeorm'; - -import { - SpaceEntity, - SubspaceProductEntity, - SubspaceProductItemEntity, -} from '@app/common/modules/space'; -import { SubspaceProductItemRepository } from '@app/common/modules/space/repositories/subspace.repository'; -import { CreateSpaceProductItemDto } from '../../dtos'; -import { BaseProductItemService } from '../../common'; - -@Injectable() -export class SubspaceProductItemService extends BaseProductItemService { - constructor( - private readonly productItemRepository: SubspaceProductItemRepository, - ) { - super(); - } - - async createItemFromDtos( - product: SubspaceProductEntity, - itemDto: CreateSpaceProductItemDto[], - queryRunner: QueryRunner, - space: SpaceEntity, - ) { - if (!itemDto?.length) return; - const incomingTags = itemDto.map((item) => item.tag); - await this.validateTags(incomingTags, queryRunner, space.uuid); - - try { - const productItems = itemDto.map((dto) => - queryRunner.manager.create(SubspaceProductItemEntity, { - tag: dto.tag, - subspaceProduct: product, - }), - ); - - await queryRunner.manager.save( - this.productItemRepository.target, - productItems, - ); - } catch (error) { - throw new HttpException( - error.message || 'An error occurred while creating product items.', - HttpStatus.INTERNAL_SERVER_ERROR, - ); - } - } -} diff --git a/src/space/services/subspace/subspace-product.service.ts b/src/space/services/subspace/subspace-product.service.ts deleted file mode 100644 index 93ef045..0000000 --- a/src/space/services/subspace/subspace-product.service.ts +++ /dev/null @@ -1,65 +0,0 @@ -import { HttpException, HttpStatus, Injectable } from '@nestjs/common'; -import { QueryRunner } from 'typeorm'; - -import { - SpaceEntity, - SubspaceEntity, - SubspaceProductEntity, -} from '@app/common/modules/space'; -import { SubspaceProductItemService } from './subspace-product-item.service'; -import { ProductAssignmentDto } from 'src/space/dtos'; -import { ProductService } from 'src/product/services'; -import { ProductEntity } from '@app/common/modules/product/entities'; - -@Injectable() -export class SubspaceProductService { - constructor( - private readonly subspaceProductItemService: SubspaceProductItemService, - private readonly productService: ProductService, - ) {} - - async createFromDto( - productDtos: ProductAssignmentDto[], - subspace: SubspaceEntity, - queryRunner: QueryRunner, - space: SpaceEntity, - ): Promise { - try { - const newSpaceProducts = await Promise.all( - productDtos.map(async (dto) => { - const product = await this.getProduct(dto.productId); - return queryRunner.manager.create(SubspaceProductEntity, { - subspace, - product, - }); - }), - ); - - const subspaceProducts = await queryRunner.manager.save( - SubspaceProductEntity, - newSpaceProducts, - ); - - await Promise.all( - productDtos.map((dto, index) => - this.subspaceProductItemService.createItemFromDtos( - subspaceProducts[index], - dto.items, - queryRunner, - space, - ), - ), - ); - } catch (error) { - throw new HttpException( - `Failed to create subspace products from DTOs. Error: ${error.message}`, - HttpStatus.INTERNAL_SERVER_ERROR, - ); - } - } - - async getProduct(productId: string): Promise { - const product = await this.productService.findOne(productId); - return product.data; - } -} diff --git a/src/space/services/subspace/subspace.service.ts b/src/space/services/subspace/subspace.service.ts index 08e12e2..9f553fd 100644 --- a/src/space/services/subspace/subspace.service.ts +++ b/src/space/services/subspace/subspace.service.ts @@ -19,14 +19,12 @@ import { } from '@app/common/modules/space-model'; import { ValidationService } from '../space-validation.service'; import { SubspaceRepository } from '@app/common/modules/space/repositories/subspace.repository'; -import { SubspaceProductService } from './subspace-product.service'; @Injectable() export class SubSpaceService { constructor( private readonly subspaceRepository: SubspaceRepository, private readonly validationService: ValidationService, - private readonly productService: SubspaceProductService, ) {} async createSubspaces( @@ -82,17 +80,6 @@ export class SubSpaceService { const subspaces = await this.createSubspaces(subspaceData, queryRunner); - await Promise.all( - addSubspaceDtos.map((dto, index) => - this.productService.createFromDto( - dto.products, - subspaces[index], - queryRunner, - space, - ), - ), - ); - return subspaces; } catch (error) { throw new Error( diff --git a/src/space/space.module.ts b/src/space/space.module.ts index 81d99e2..5d16689 100644 --- a/src/space/space.module.ts +++ b/src/space/space.module.ts @@ -12,20 +12,15 @@ import { import { SpaceDeviceService, SpaceLinkService, - SpaceProductItemService, - SpaceProductService, SpaceSceneService, SpaceService, SpaceUserService, SubspaceDeviceService, - SubspaceProductItemService, SubSpaceService, } from './services'; import { - SpaceProductRepository, SpaceRepository, SpaceLinkRepository, - SpaceProductItemRepository, } from '@app/common/modules/space/repositories'; import { CommunityRepository } from '@app/common/modules/community/repositories'; import { @@ -48,12 +43,7 @@ import { ProjectRepository } from '@app/common/modules/project/repositiories'; import { SpaceModelRepository } from '@app/common/modules/space-model'; import { CommunityModule } from 'src/community/community.module'; import { ValidationService } from './services'; -import { - SubspaceProductItemRepository, - SubspaceProductRepository, - SubspaceRepository, -} from '@app/common/modules/space/repositories/subspace.repository'; -import { SubspaceProductService } from './services'; +import { SubspaceRepository } from '@app/common/modules/space/repositories/subspace.repository'; @Module({ imports: [ConfigModule, SpaceRepositoryModule, CommunityModule], @@ -75,9 +65,9 @@ import { SubspaceProductService } from './services'; SpaceLinkService, SubspaceDeviceService, SpaceRepository, + SubspaceRepository, DeviceRepository, CommunityRepository, - SubspaceRepository, SpaceLinkRepository, UserSpaceRepository, UserRepository, @@ -88,19 +78,11 @@ import { SubspaceProductService } from './services'; SceneRepository, DeviceService, DeviceStatusFirebaseService, - SubspaceProductItemRepository, DeviceStatusLogRepository, SceneDeviceRepository, - SpaceProductService, - SpaceProductRepository, + ProjectRepository, SpaceModelRepository, - SubspaceRepository, - SpaceProductItemService, - SpaceProductItemRepository, - SubspaceProductService, - SubspaceProductItemService, - SubspaceProductRepository, ], exports: [SpaceService], })