From 8af29cddfd4d8651d2fc54d937e8fbcc6c4f2417 Mon Sep 17 00:00:00 2001 From: hannathkadher Date: Wed, 11 Dec 2024 17:01:23 +0400 Subject: [PATCH] add validation to check both space model and products, subspaces are not created --- src/space/dtos/add.space.dto.ts | 22 +++++++++++++++++++++- src/space/services/space.service.ts | 20 +++++++++++++++++++- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/space/dtos/add.space.dto.ts b/src/space/dtos/add.space.dto.ts index fe88c33..d66fa87 100644 --- a/src/space/dtos/add.space.dto.ts +++ b/src/space/dtos/add.space.dto.ts @@ -22,7 +22,17 @@ export class CreateSpaceProductItemDto { tag: string; } -class ProductAssignmentDto { +export class CreateSubspaceDto { + @ApiProperty({ + description: 'Name of the subspace', + example: 'Living Room', + }) + @IsNotEmpty() + @IsString() + subspaceName: string; +} + +export class ProductAssignmentDto { @ApiProperty({ description: 'UUID of the product to be assigned', example: 'prod-uuid-1234', @@ -114,6 +124,16 @@ export class AddSpaceDto { @IsOptional() @Type(() => ProductAssignmentDto) products?: ProductAssignmentDto[]; + + @ApiProperty({ + description: 'List of subspaces included in the model', + type: [CreateSubspaceDto], + }) + @IsOptional() + @IsArray() + @ValidateNested({ each: true }) + @Type(() => CreateSubspaceDto) + subspaces?: CreateSubspaceDto[]; } export class AddUserSpaceDto { diff --git a/src/space/services/space.service.ts b/src/space/services/space.service.ts index 8189f8c..6fc35eb 100644 --- a/src/space/services/space.service.ts +++ b/src/space/services/space.service.ts @@ -9,6 +9,7 @@ import { AddSpaceDto, CommunitySpaceParam, GetSpaceParam, + ProductAssignmentDto, UpdateSpaceDto, } from '../dtos'; import { SuccessResponseDto } from '@app/common/dto/success.response.dto'; @@ -19,6 +20,7 @@ import { generateRandomString } from '@app/common/helper/randomString'; import { SpaceLinkService } from './space-link'; import { SpaceProductService } from './space-products'; import { ProjectRepository } from '@app/common/modules/project/repositiories'; +import { CreateSubspaceModelDto } from 'src/space-model/dtos'; @Injectable() export class SpaceService { @@ -34,11 +36,14 @@ export class SpaceService { addSpaceDto: AddSpaceDto, params: CommunitySpaceParam, ): Promise { - const { parentUuid, direction, products } = addSpaceDto; + const { parentUuid, direction, products, spaceModelUuid, subspaces } = + addSpaceDto; const { communityUuid, projectUuid } = params; await this.validateProject(projectUuid); + this.validateSpaceCreation(spaceModelUuid, products, subspaces); + const community = await this.validateCommunity(communityUuid); const parent = parentUuid ? await this.validateSpace(parentUuid) : null; @@ -344,4 +349,17 @@ export class SpaceService { HttpStatus.NOT_FOUND, ); } + + private validateSpaceCreation( + spaceModelUuid?: string, + products?: ProductAssignmentDto[], + subSpaces?: CreateSubspaceModelDto[], + ) { + if (spaceModelUuid && (products?.length || subSpaces?.length)) { + throw new HttpException( + 'Space model cannot be assigned with products or subspaces.', + HttpStatus.CONFLICT, + ); + } + } }