From a2ee7a000106b1d52fa578604bca4b4ceaa594c5 Mon Sep 17 00:00:00 2001 From: hannathkadher Date: Wed, 11 Dec 2024 16:45:09 +0400 Subject: [PATCH] added space model details to space --- .../dtos/create-space-product-model.dto.ts | 2 +- .../space-product-item-model.service.ts | 6 ++ .../services/space-product-model.service.ts | 15 ++-- src/space/dtos/add.space.dto.ts | 69 ++++++++++++++++--- 4 files changed, 73 insertions(+), 19 deletions(-) diff --git a/src/space-model/dtos/create-space-product-model.dto.ts b/src/space-model/dtos/create-space-product-model.dto.ts index 11d8bb5..9f251be 100644 --- a/src/space-model/dtos/create-space-product-model.dto.ts +++ b/src/space-model/dtos/create-space-product-model.dto.ts @@ -17,7 +17,7 @@ export class CreateSpaceProductModelDto { }) @IsNotEmpty() @IsString() - productId: string; + productUuid: string; @ApiProperty({ description: 'Number of products in the model', diff --git a/src/space-model/services/space-product-item-model.service.ts b/src/space-model/services/space-product-item-model.service.ts index 3ec0eb2..ac9c0d8 100644 --- a/src/space-model/services/space-product-item-model.service.ts +++ b/src/space-model/services/space-product-item-model.service.ts @@ -21,6 +21,12 @@ export class SpaceProductItemModelService { ) { await this.validateTags(itemModelDtos, queryRunner, spaceModel); + if (!spaceProductModel) { + throw new HttpException( + 'Space product model is required to create product items.', + HttpStatus.BAD_REQUEST, + ); + } try { const productItems = itemModelDtos.map((dto) => queryRunner.manager.create(this.spaceProductItemRepository.target, { diff --git a/src/space-model/services/space-product-model.service.ts b/src/space-model/services/space-product-model.service.ts index aa08a16..27bad29 100644 --- a/src/space-model/services/space-product-model.service.ts +++ b/src/space-model/services/space-product-model.service.ts @@ -25,7 +25,7 @@ export class SpaceProductModelService { const productModels = await Promise.all( spaceProductModelDtos.map(async (dto) => { this.validateProductCount(dto); - const product = await this.getProduct(dto.productId); + const product = await this.getProduct(dto.productUuid); return queryRunner.manager.create( this.spaceProductModelRepository.target, { @@ -40,14 +40,15 @@ export class SpaceProductModelService { const savedProductModels = await queryRunner.manager.save(productModels); await Promise.all( - spaceProductModelDtos.map((dto, index) => - this.spaceProductItemModelService.createProdutItemModel( + spaceProductModelDtos.map((dto, index) => { + const savedModel = savedProductModels[index]; + return this.spaceProductItemModelService.createProdutItemModel( dto.items, - savedProductModels[index], + savedModel, // Pass the saved model spaceModel, queryRunner, - ), - ), + ); + }), ); } catch (error) { if (error instanceof HttpException) { @@ -65,7 +66,7 @@ export class SpaceProductModelService { const productItemCount = dto.items.length; if (dto.productCount !== productItemCount) { throw new HttpException( - `Product count (${dto.productCount}) does not match the number of items (${productItemCount}) for product ID ${dto.productId}.`, + `Product count (${dto.productCount}) does not match the number of items (${productItemCount}) for product ID ${dto.productUuid}.`, HttpStatus.BAD_REQUEST, ); } diff --git a/src/space/dtos/add.space.dto.ts b/src/space/dtos/add.space.dto.ts index 554f17b..fe88c33 100644 --- a/src/space/dtos/add.space.dto.ts +++ b/src/space/dtos/add.space.dto.ts @@ -1,6 +1,7 @@ import { ApiProperty } from '@nestjs/swagger'; import { Type } from 'class-transformer'; import { + ArrayNotEmpty, IsArray, IsBoolean, IsNotEmpty, @@ -11,6 +12,42 @@ import { ValidateNested, } from 'class-validator'; +export class CreateSpaceProductItemDto { + @ApiProperty({ + description: 'Specific name for the product item', + example: 'Light 1', + }) + @IsNotEmpty() + @IsString() + tag: string; +} + +class ProductAssignmentDto { + @ApiProperty({ + description: 'UUID of the product to be assigned', + example: 'prod-uuid-1234', + }) + @IsNotEmpty() + productId: string; + + @ApiProperty({ + description: 'Number of items to assign for the product', + example: 3, + }) + count: number; + + @ApiProperty({ + description: 'Specific names for each product item', + type: [CreateSpaceProductItemDto], + example: [{ tag: 'Light 1' }, { tag: 'Light 2' }, { tag: 'Light 3' }], + }) + @IsArray() + @ArrayNotEmpty() + @ValidateNested({ each: true }) + @Type(() => CreateSpaceProductItemDto) + items: CreateSpaceProductItemDto[]; +} + export class AddSpaceDto { @ApiProperty({ description: 'Name of the space (e.g., Floor 1, Unit 101)', @@ -29,9 +66,14 @@ export class AddSpaceDto { @IsOptional() parentUuid?: string; + @ApiProperty({ + description: 'Icon identifier for the space', + example: 'assets/location', + required: false, + }) @IsString() @IsOptional() - public icon: string; + public icon?: string; @ApiProperty({ description: 'Indicates whether the space is private or public', @@ -49,16 +91,29 @@ export class AddSpaceDto { @IsNumber() y: number; + @ApiProperty({ + description: 'UUID of the Space', + example: 'd290f1ee-6c54-4b01-90e6-d701748f0851', + }) + @IsString() + @IsOptional() + spaceModelUuid?: string; + @ApiProperty({ description: 'Y position on canvas', example: 200 }) @IsString() @IsOptional() - direction: string; + direction?: string; + @ApiProperty({ + description: 'List of products assigned to this space', + type: [ProductAssignmentDto], + required: false, + }) @IsArray() @ValidateNested({ each: true }) @IsOptional() @Type(() => ProductAssignmentDto) - products: ProductAssignmentDto[]; + products?: ProductAssignmentDto[]; } export class AddUserSpaceDto { @@ -101,11 +156,3 @@ export class AddUserSpaceUsingCodeDto { Object.assign(this, dto); } } - -class ProductAssignmentDto { - @IsNotEmpty() - productId: string; - - @IsNotEmpty() - count: number; -}