remove x & y from space, add validation on space naming

This commit is contained in:
Mhd Zayd Skaff
2025-07-14 16:03:48 +03:00
parent a9cb1b6704
commit 2c3b985594
4 changed files with 36 additions and 32 deletions
@@ -75,13 +75,6 @@ export class SpaceEntity extends AbstractEntity<SpaceDto> {
}) })
subspaces?: SubspaceEntity[]; 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( @OneToMany(
() => DeviceEntity, () => DeviceEntity,
(devicesSpaceEntity) => devicesSpaceEntity.spaceDevice, (devicesSpaceEntity) => devicesSpaceEntity.spaceDevice,
-9
View File
@@ -6,7 +6,6 @@ import {
IsArray, IsArray,
IsMongoId, IsMongoId,
IsNotEmpty, IsNotEmpty,
IsNumber,
IsOptional, IsOptional,
IsString, IsString,
IsUUID, IsUUID,
@@ -48,14 +47,6 @@ export class AddSpaceDto {
@IsOptional() @IsOptional()
public icon?: string; 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({ @ApiProperty({
description: 'UUID of the Space Model', description: 'UUID of the Space Model',
example: 'd290f1ee-6c54-4b01-90e6-d701748f0851', example: 'd290f1ee-6c54-4b01-90e6-d701748f0851',
-11
View File
@@ -4,7 +4,6 @@ import { Type } from 'class-transformer';
import { import {
ArrayUnique, ArrayUnique,
IsArray, IsArray,
IsNumber,
IsOptional, IsOptional,
IsString, IsString,
NotEquals, NotEquals,
@@ -36,16 +35,6 @@ export class UpdateSpaceDto {
@IsOptional() @IsOptional()
public icon?: string; 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({ @ApiPropertyOptional({
description: 'List of subspace modifications', description: 'List of subspace modifications',
type: [UpdateSubspaceDto], type: [UpdateSubspaceDto],
+36 -5
View File
@@ -93,6 +93,9 @@ export class SpaceService {
parentUuid && !isRecursiveCall parentUuid && !isRecursiveCall
? await this.validationService.validateSpace(parentUuid) ? await this.validationService.validateSpace(parentUuid)
: null; : null;
if (parent) {
await this.validateNamingConflict(addSpaceDto.spaceName, parent);
}
const spaceModel = spaceModelUuid const spaceModel = spaceModelUuid
? await this.validationService.validateSpaceModel(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 // todo: find a better way to handle this instead of naming every key
spaceName: addSpaceDto.spaceName, spaceName: addSpaceDto.spaceName,
icon: addSpaceDto.icon, icon: addSpaceDto.icon,
x: addSpaceDto.x,
y: addSpaceDto.y,
spaceModel, spaceModel,
parent: isRecursiveCall parent: isRecursiveCall
? recursiveCallParentEntity ? recursiveCallParentEntity
@@ -505,6 +506,8 @@ export class SpaceService {
spaceUuid, spaceUuid,
); );
await this.validateNamingConflict(updateSpaceDto.spaceName, space, true);
if (space.spaceModel && !updateSpaceDto.spaceModelUuid) { if (space.spaceModel && !updateSpaceDto.spaceModelUuid) {
await queryRunner.manager.update(SpaceEntity, space.uuid, { await queryRunner.manager.update(SpaceEntity, space.uuid, {
spaceModel: null, spaceModel: null,
@@ -655,13 +658,11 @@ export class SpaceService {
updateSpaceDto: UpdateSpaceDto, updateSpaceDto: UpdateSpaceDto,
queryRunner: QueryRunner, queryRunner: QueryRunner,
): Promise<void> { ): Promise<void> {
const { spaceName, x, y, icon } = updateSpaceDto; const { spaceName, icon } = updateSpaceDto;
const updateFields: Partial<SpaceEntity> = {}; const updateFields: Partial<SpaceEntity> = {};
if (spaceName) updateFields.spaceName = spaceName; if (spaceName) updateFields.spaceName = spaceName;
if (x !== undefined) updateFields.x = x;
if (y !== undefined) updateFields.y = y;
if (icon) updateFields.icon = icon; if (icon) updateFields.icon = icon;
if (Object.keys(updateFields).length > 0) { if (Object.keys(updateFields).length > 0) {
@@ -828,4 +829,34 @@ export class SpaceService {
queryRunner, queryRunner,
); );
} }
async validateNamingConflict(
newSpaceName: string,
parent: SpaceEntity,
isUpdate: boolean = false,
): Promise<void> {
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,
);
}
}
}
} }