From 4b55c4e39cb755d2dc8a17fd8e084857b46aa407 Mon Sep 17 00:00:00 2001 From: hannathkadher Date: Mon, 23 Dec 2024 08:53:01 +0400 Subject: [PATCH] service clean up --- src/space-model/common/index.ts | 1 - .../base-product-item-model.service.ts | 79 ------------------- .../services/base-product-model.service.ts | 10 --- src/space-model/common/services/index.ts | 2 - .../dtos/create-space-model.dto.ts | 10 +++ src/space-model/dtos/index.ts | 2 - .../create-subspace-model.dto.ts | 13 ++- .../tag-model-dtos/create-tag-model.dto.ts | 20 +++++ src/space-model/dtos/tag-model-dtos/index.ts | 0 9 files changed, 42 insertions(+), 95 deletions(-) delete mode 100644 src/space-model/common/index.ts delete mode 100644 src/space-model/common/services/base-product-item-model.service.ts delete mode 100644 src/space-model/common/services/base-product-model.service.ts delete mode 100644 src/space-model/common/services/index.ts create mode 100644 src/space-model/dtos/tag-model-dtos/create-tag-model.dto.ts create mode 100644 src/space-model/dtos/tag-model-dtos/index.ts diff --git a/src/space-model/common/index.ts b/src/space-model/common/index.ts deleted file mode 100644 index e371345..0000000 --- a/src/space-model/common/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './services'; diff --git a/src/space-model/common/services/base-product-item-model.service.ts b/src/space-model/common/services/base-product-item-model.service.ts deleted file mode 100644 index 4d67e6d..0000000 --- a/src/space-model/common/services/base-product-item-model.service.ts +++ /dev/null @@ -1,79 +0,0 @@ -import { HttpException, HttpStatus } from '@nestjs/common'; -import { QueryRunner } from 'typeorm'; -import { SpaceModelEntity } from '@app/common/modules/space-model'; -import { CreateProductItemModelDto } from 'src/space-model/dtos'; - -export abstract class BaseProductItemService { - async validateTags( - itemModelDtos: CreateProductItemModelDto[], - queryRunner: QueryRunner, - spaceModel: SpaceModelEntity, - ): Promise { - const incomingTags = new Set( - itemModelDtos.map((item) => item.tag).filter(Boolean), - ); - - const duplicateTags = itemModelDtos - .map((item) => item.tag) - .filter((tag, index, array) => array.indexOf(tag) !== index); - - if (duplicateTags.length > 0) { - throw new HttpException( - `Duplicate tags found in the request: ${[...new Set(duplicateTags)].join(', ')}`, - HttpStatus.BAD_REQUEST, - ); - } - - const existingTagsQuery = ` - SELECT DISTINCT tag - FROM ( - SELECT spi.tag - FROM "subspace-product-item-model" spi - INNER JOIN "subspace-product-model" spm ON spi.subspace_product_model_uuid = spm.uuid - INNER JOIN "subspace-model" sm ON spm.subspace_model_uuid = sm.uuid - WHERE sm.space_model_uuid = $1 - UNION - SELECT spi.tag - FROM "space-product-item-model" spi - INNER JOIN "space-product-model" spm ON spi.space_product_model_uuid = spm.uuid - WHERE spm.space_model_uuid = $1 - ) AS combined_tags; - `; - - const existingTags = await queryRunner.manager.query(existingTagsQuery, [ - spaceModel.uuid, - ]); - const existingTagsSet = new Set( - existingTags.map((row: { tag: string }) => row.tag), - ); - - const conflictingTags = [...incomingTags].filter((tag) => - existingTagsSet.has(tag), - ); - if (conflictingTags.length > 0) { - throw new HttpException( - `Tags already exist in the model: ${conflictingTags.join(', ')}`, - HttpStatus.CONFLICT, - ); - } - } - - protected async saveProductItems( - productItems: T[], - targetRepository: any, - queryRunner: QueryRunner, - ): Promise { - try { - const savedItem = await queryRunner.manager.save( - targetRepository, - productItems, - ); - return savedItem; - } catch (error) { - throw new HttpException( - error.message || 'An error occurred while creating product items.', - HttpStatus.INTERNAL_SERVER_ERROR, - ); - } - } -} diff --git a/src/space-model/common/services/base-product-model.service.ts b/src/space-model/common/services/base-product-model.service.ts deleted file mode 100644 index 6ff8d91..0000000 --- a/src/space-model/common/services/base-product-model.service.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { ProductService } from '../../../product/services'; - -export abstract class BaseProductModelService { - constructor(private readonly productService: ProductService) {} - - protected async getProduct(productId: string) { - const product = await this.productService.findOne(productId); - return product.data; - } -} diff --git a/src/space-model/common/services/index.ts b/src/space-model/common/services/index.ts deleted file mode 100644 index d1cc61e..0000000 --- a/src/space-model/common/services/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export * from './base-product-item-model.service'; -export * from './base-product-model.service'; diff --git a/src/space-model/dtos/create-space-model.dto.ts b/src/space-model/dtos/create-space-model.dto.ts index 6ffaf26..0c37779 100644 --- a/src/space-model/dtos/create-space-model.dto.ts +++ b/src/space-model/dtos/create-space-model.dto.ts @@ -2,6 +2,7 @@ import { ApiProperty } from '@nestjs/swagger'; import { IsNotEmpty, IsString, IsArray, ValidateNested } from 'class-validator'; import { Type } from 'class-transformer'; import { CreateSubspaceModelDto } from './subspaces-model-dtos/create-subspace-model.dto'; +import { CreateTagModelDto } from './tag-model-dtos/create-tag-model.dto'; export class CreateSpaceModelDto { @ApiProperty({ @@ -20,4 +21,13 @@ export class CreateSpaceModelDto { @ValidateNested({ each: true }) @Type(() => CreateSubspaceModelDto) subspaceModels?: CreateSubspaceModelDto[]; + + @ApiProperty({ + description: 'List of tags associated with the space model', + type: [CreateTagModelDto], + }) + @IsArray() + @ValidateNested({ each: true }) + @Type(() => CreateTagModelDto) + tags?: CreateTagModelDto[]; } diff --git a/src/space-model/dtos/index.ts b/src/space-model/dtos/index.ts index 045d4aa..11c49af 100644 --- a/src/space-model/dtos/index.ts +++ b/src/space-model/dtos/index.ts @@ -1,6 +1,4 @@ export * from './create-space-model.dto'; -export * from './product-item-model-dtos'; -export * from './product-model-dtos'; export * from './project-param.dto'; export * from './update-space-model.dto'; export * from './space-model-param'; diff --git a/src/space-model/dtos/subspaces-model-dtos/create-subspace-model.dto.ts b/src/space-model/dtos/subspaces-model-dtos/create-subspace-model.dto.ts index a27ad3b..24eacfb 100644 --- a/src/space-model/dtos/subspaces-model-dtos/create-subspace-model.dto.ts +++ b/src/space-model/dtos/subspaces-model-dtos/create-subspace-model.dto.ts @@ -1,5 +1,7 @@ import { ApiProperty } from '@nestjs/swagger'; -import { IsNotEmpty, IsString } from 'class-validator'; +import { IsArray, IsNotEmpty, IsString, ValidateNested } from 'class-validator'; +import { CreateTagModelDto } from '../tag-model-dtos/create-tag-model.dto'; +import { Type } from 'class-transformer'; export class CreateSubspaceModelDto { @ApiProperty({ @@ -9,4 +11,13 @@ export class CreateSubspaceModelDto { @IsNotEmpty() @IsString() subspaceName: string; + + @ApiProperty({ + description: 'List of tags associated with the subspace', + type: [CreateTagModelDto], + }) + @IsArray() + @ValidateNested({ each: true }) + @Type(() => CreateTagModelDto) + tags?: CreateTagModelDto[]; } diff --git a/src/space-model/dtos/tag-model-dtos/create-tag-model.dto.ts b/src/space-model/dtos/tag-model-dtos/create-tag-model.dto.ts new file mode 100644 index 0000000..65acf2a --- /dev/null +++ b/src/space-model/dtos/tag-model-dtos/create-tag-model.dto.ts @@ -0,0 +1,20 @@ +import { ApiProperty } from '@nestjs/swagger'; +import { IsNotEmpty, IsString } from 'class-validator'; + +export class CreateTagModelDto { + @ApiProperty({ + description: 'Tag associated with the space or subspace', + example: 'Temperature Control', + }) + @IsNotEmpty() + @IsString() + tag: string; + + @ApiProperty({ + description: 'ID of the product associated with the tag', + example: '123e4567-e89b-12d3-a456-426614174000', + }) + @IsNotEmpty() + @IsString() + productUuid: string; +} diff --git a/src/space-model/dtos/tag-model-dtos/index.ts b/src/space-model/dtos/tag-model-dtos/index.ts new file mode 100644 index 0000000..e69de29