Merge pull request #470 from SyncrowIOT/fix/remove-x-y-from-space

SP-1853: Space Fixes
This commit is contained in:
raf-dev1
2025-07-23 09:02:54 +03:00
committed by GitHub
4 changed files with 36 additions and 32 deletions

View File

@ -91,13 +91,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,

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',

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],

View File

@ -96,6 +96,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)
@ -105,8 +108,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
@ -657,6 +658,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,
@ -807,13 +810,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) {
@ -980,4 +981,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,
);
}
}
}
} }