From 2c3b9855941063c944d2208cdff7548ae528de12 Mon Sep 17 00:00:00 2001 From: Mhd Zayd Skaff Date: Mon, 14 Jul 2025 16:03:48 +0300 Subject: [PATCH] remove x & y from space, add validation on space naming --- .../modules/space/entities/space.entity.ts | 7 ---- src/space/dtos/add.space.dto.ts | 9 ---- src/space/dtos/update.space.dto.ts | 11 ----- src/space/services/space.service.ts | 41 ++++++++++++++++--- 4 files changed, 36 insertions(+), 32 deletions(-) diff --git a/libs/common/src/modules/space/entities/space.entity.ts b/libs/common/src/modules/space/entities/space.entity.ts index 6a04865..111cae1 100644 --- a/libs/common/src/modules/space/entities/space.entity.ts +++ b/libs/common/src/modules/space/entities/space.entity.ts @@ -75,13 +75,6 @@ export class SpaceEntity extends AbstractEntity { }) subspaces?: SubspaceEntity[]; - // Position columns - @Column({ type: 'float', nullable: false, default: 0 }) - public x: number; // X coordinate for position - - @Column({ type: 'float', nullable: false, default: 0 }) - public y: number; // Y coordinate for position - @OneToMany( () => DeviceEntity, (devicesSpaceEntity) => devicesSpaceEntity.spaceDevice, diff --git a/src/space/dtos/add.space.dto.ts b/src/space/dtos/add.space.dto.ts index b3b4fcc..005faa8 100644 --- a/src/space/dtos/add.space.dto.ts +++ b/src/space/dtos/add.space.dto.ts @@ -6,7 +6,6 @@ import { IsArray, IsMongoId, IsNotEmpty, - IsNumber, IsOptional, IsString, IsUUID, @@ -48,14 +47,6 @@ export class AddSpaceDto { @IsOptional() public icon?: string; - @ApiProperty({ description: 'X position on canvas', example: 120 }) - @IsNumber() - x: number; - - @ApiProperty({ description: 'Y position on canvas', example: 200 }) - @IsNumber() - y: number; - @ApiProperty({ description: 'UUID of the Space Model', example: 'd290f1ee-6c54-4b01-90e6-d701748f0851', diff --git a/src/space/dtos/update.space.dto.ts b/src/space/dtos/update.space.dto.ts index 5f1fbcd..dbdbcba 100644 --- a/src/space/dtos/update.space.dto.ts +++ b/src/space/dtos/update.space.dto.ts @@ -4,7 +4,6 @@ import { Type } from 'class-transformer'; import { ArrayUnique, IsArray, - IsNumber, IsOptional, IsString, NotEquals, @@ -36,16 +35,6 @@ export class UpdateSpaceDto { @IsOptional() public icon?: string; - @ApiProperty({ description: 'X position on canvas', example: 120 }) - @IsNumber() - @IsOptional() - x?: number; - - @ApiProperty({ description: 'Y position on canvas', example: 200 }) - @IsNumber() - @IsOptional() - y?: number; - @ApiPropertyOptional({ description: 'List of subspace modifications', type: [UpdateSubspaceDto], diff --git a/src/space/services/space.service.ts b/src/space/services/space.service.ts index 126a3ec..f9a8a48 100644 --- a/src/space/services/space.service.ts +++ b/src/space/services/space.service.ts @@ -93,6 +93,9 @@ export class SpaceService { parentUuid && !isRecursiveCall ? await this.validationService.validateSpace(parentUuid) : null; + if (parent) { + await this.validateNamingConflict(addSpaceDto.spaceName, parent); + } const spaceModel = spaceModelUuid ? await this.validationService.validateSpaceModel(spaceModelUuid) @@ -102,8 +105,6 @@ export class SpaceService { // todo: find a better way to handle this instead of naming every key spaceName: addSpaceDto.spaceName, icon: addSpaceDto.icon, - x: addSpaceDto.x, - y: addSpaceDto.y, spaceModel, parent: isRecursiveCall ? recursiveCallParentEntity @@ -505,6 +506,8 @@ export class SpaceService { spaceUuid, ); + await this.validateNamingConflict(updateSpaceDto.spaceName, space, true); + if (space.spaceModel && !updateSpaceDto.spaceModelUuid) { await queryRunner.manager.update(SpaceEntity, space.uuid, { spaceModel: null, @@ -655,13 +658,11 @@ export class SpaceService { updateSpaceDto: UpdateSpaceDto, queryRunner: QueryRunner, ): Promise { - const { spaceName, x, y, icon } = updateSpaceDto; + const { spaceName, icon } = updateSpaceDto; const updateFields: Partial = {}; if (spaceName) updateFields.spaceName = spaceName; - if (x !== undefined) updateFields.x = x; - if (y !== undefined) updateFields.y = y; if (icon) updateFields.icon = icon; if (Object.keys(updateFields).length > 0) { @@ -828,4 +829,34 @@ export class SpaceService { queryRunner, ); } + + async validateNamingConflict( + newSpaceName: string, + parent: SpaceEntity, + isUpdate: boolean = false, + ): Promise { + if (!isUpdate && parent.spaceName === newSpaceName) { + throw new HttpException( + `Space can't be created with the same name as its parent space`, + HttpStatus.BAD_REQUEST, + ); + } + if (parent.children?.some((child) => child.spaceName === newSpaceName)) { + throw new HttpException( + `Space name cannot be the same as one of its siblings/children`, + HttpStatus.BAD_REQUEST, + ); + } + if (isUpdate) { + const sibling = await this.spaceRepository.exists({ + where: { spaceName: newSpaceName, parent: { uuid: parent.uuid } }, + }); + if (sibling) { + throw new HttpException( + `Space name cannot be the same as one of its siblings/children`, + HttpStatus.BAD_REQUEST, + ); + } + } + } }