Merge branch 'dev' of https://github.com/SyncrowIOT/backend into bugix/delete-subspace

This commit is contained in:
hannathkadher
2025-03-11 01:25:31 +04:00
5 changed files with 186 additions and 179 deletions

View File

@ -481,6 +481,7 @@ export class SpaceModelService {
await this.subspaceRepository.save(subspace); await this.subspaceRepository.save(subspace);
} }
const subspaceAllocations = subspaceModel.productAllocations.map( const subspaceAllocations = subspaceModel.productAllocations.map(
(modelAllocation) => (modelAllocation) =>
this.subspaceProductAllocationRepository.create({ this.subspaceProductAllocationRepository.create({

View File

@ -45,11 +45,14 @@ export class ValidationService {
await this.communityService.validateProject(projectUuid); await this.communityService.validateProject(projectUuid);
const spaces = await this.spaceRepository.find({ const spaces = await this.spaceRepository.find({
where: { uuid: In(spacesUuids), disabled: false }, where: { uuid: In(spacesUuids), disabled: false },
relations: ['devices', 'subspaces'], relations: ['devices', 'subspaces', 'productAllocations'],
}); });
const hasInvalidSpaces = spaces.some( const hasInvalidSpaces = spaces.some(
(space) => space.devices.length > 0 || space.subspaces.length > 0, (space) =>
space.devices.length > 0 ||
space.subspaces.length > 0 ||
space.productAllocations.length > 0,
); );
if (hasInvalidSpaces) { if (hasInvalidSpaces) {
@ -122,6 +125,12 @@ export class ValidationService {
'children', 'children',
'subspaces', 'subspaces',
'tags', 'tags',
'productAllocations',
'productAllocations.product',
'productAllocations.tags',
'subspaces.productAllocations',
'subspaces.productAllocations.tags',
'subspaces.productAllocations.product',
'subspaces.tags', 'subspaces.tags',
'subspaces.devices', 'subspaces.devices',
], ],
@ -172,6 +181,7 @@ export class ValidationService {
'subspaceModels', 'subspaceModels',
'subspaceModels.productAllocations', 'subspaceModels.productAllocations',
'subspaceModels.productAllocations.tags', 'subspaceModels.productAllocations.tags',
'subspaceModels.productAllocations.product',
'productAllocations.product', 'productAllocations.product',
'productAllocations.tags', 'productAllocations.tags',
], ],

View File

@ -530,7 +530,7 @@ export class SpaceService {
} }
throw new HttpException( throw new HttpException(
'An error occurred while updating the space', `An error occurred while updating the space: error ${error}`,
HttpStatus.INTERNAL_SERVER_ERROR, HttpStatus.INTERNAL_SERVER_ERROR,
); );
} finally { } finally {
@ -653,15 +653,9 @@ export class SpaceService {
private buildSpaceHierarchy(spaces: SpaceEntity[]): SpaceEntity[] { private buildSpaceHierarchy(spaces: SpaceEntity[]): SpaceEntity[] {
const map = new Map<string, SpaceEntity>(); const map = new Map<string, SpaceEntity>();
// Step 1: Create a map of spaces by UUID // Step 1: Create a map of spaces by UUID, without creating new instances
spaces.forEach((space) => { spaces.forEach((space) => {
map.set( map.set(space.uuid, space); // Use the existing space entity
space.uuid,
this.spaceRepository.create({
...space,
children: [], // Add children if needed
}),
);
}); });
// Step 2: Organize the hierarchy // Step 2: Organize the hierarchy
@ -669,9 +663,14 @@ export class SpaceService {
spaces.forEach((space) => { spaces.forEach((space) => {
if (space.parent && space.parent.uuid) { if (space.parent && space.parent.uuid) {
const parent = map.get(space.parent.uuid); const parent = map.get(space.parent.uuid);
parent?.children?.push(map.get(space.uuid)); if (parent) {
if (!parent.children) {
parent.children = [];
}
parent.children.push(space);
}
} else { } else {
rootSpaces.push(map.get(space.uuid)); rootSpaces.push(space);
} }
}); });

View File

@ -79,10 +79,9 @@ export class SubspaceProductAllocationService {
spaceTagUpdateDtos?: ModifyTagDto[], spaceTagUpdateDtos?: ModifyTagDto[],
) { ) {
const spaceAllocationToExclude: SpaceProductAllocationEntity[] = []; const spaceAllocationToExclude: SpaceProductAllocationEntity[] = [];
for (const subspace of subspaces) { for (const subspace of subspaces) {
if (!subspace.tags || subspace.tags.length === 0) continue;
const tagDtos = subspace.tags; const tagDtos = subspace.tags;
if (tagDtos.length > 0) {
const tagsToAddDto: ProcessTagDto[] = tagDtos const tagsToAddDto: ProcessTagDto[] = tagDtos
.filter((dto) => dto.action === ModifyAction.ADD) .filter((dto) => dto.action === ModifyAction.ADD)
.map((dto) => ({ .map((dto) => ({
@ -293,7 +292,6 @@ export class SubspaceProductAllocationService {
} }
} }
} }
}
async processDeleteActions( async processDeleteActions(
dtos: ModifyTagDto[], dtos: ModifyTagDto[],

View File

@ -324,6 +324,9 @@ export class SubSpaceService {
projectUuid?: string, projectUuid?: string,
spaceTagUpdateDtos?: ModifyTagDto[], spaceTagUpdateDtos?: ModifyTagDto[],
) { ) {
if (!subspaceDtos || subspaceDtos.length === 0) {
return;
}
const addedSubspaces = []; const addedSubspaces = [];
const updatedSubspaces = []; const updatedSubspaces = [];
@ -335,14 +338,17 @@ export class SubSpaceService {
space, space,
queryRunner, queryRunner,
); );
addedSubspaces.push(addedSubspace); if (addedSubspace) addedSubspaces.push(addedSubspace);
break; break;
case ModifyAction.UPDATE: case ModifyAction.UPDATE:
const updatedSubspace = await this.handleUpdateAction( const updatedSubspace = await this.handleUpdateAction(
subspace, subspace,
queryRunner, queryRunner,
); );
if (updatedSubspace) {
updatedSubspaces.push(updatedSubspace); updatedSubspaces.push(updatedSubspace);
}
break; break;
case ModifyAction.DELETE: case ModifyAction.DELETE:
await this.handleDeleteAction(subspace, queryRunner); await this.handleDeleteAction(subspace, queryRunner);
@ -355,7 +361,9 @@ export class SubSpaceService {
} }
} }
const combinedSubspaces = [...addedSubspaces, ...updatedSubspaces]; const combinedSubspaces = [...addedSubspaces, ...updatedSubspaces].filter(
(subspace) => subspace !== undefined,
);
if (combinedSubspaces.length > 0) { if (combinedSubspaces.length > 0) {
await this.subspaceProductAllocationService.updateSubspaceProductAllocations( await this.subspaceProductAllocationService.updateSubspaceProductAllocations(
@ -436,32 +444,22 @@ export class SubSpaceService {
private async handleUpdateAction( private async handleUpdateAction(
modifyDto: ModifySubspaceDto, modifyDto: ModifySubspaceDto,
queryRunner: QueryRunner, queryRunner: QueryRunner,
): Promise<void> { ): Promise<SubspaceEntity> {
const subspace = await this.findOne(modifyDto.uuid); const subspace = await this.findOne(modifyDto.uuid);
await this.update( const updatedSubspace = await this.update(
queryRunner, queryRunner,
subspace, subspace,
modifyDto.subspaceName, modifyDto.subspaceName,
modifyDto.tags,
); );
return updatedSubspace;
} }
async update( async update(
queryRunner: QueryRunner, queryRunner: QueryRunner,
subspace: SubspaceEntity, subspace: SubspaceEntity,
subspaceName?: string, subspaceName?: string,
modifyTagDto?: ModifyTagDto[],
) { ) {
await this.updateSubspaceName(queryRunner, subspace, subspaceName); return await this.updateSubspaceName(queryRunner, subspace, subspaceName);
if (modifyTagDto?.length) {
await this.tagService.modifyTags(
modifyTagDto,
queryRunner,
null,
subspace,
);
}
} }
async handleDeleteAction( async handleDeleteAction(
@ -517,11 +515,12 @@ export class SubSpaceService {
queryRunner: QueryRunner, queryRunner: QueryRunner,
subSpace: SubspaceEntity, subSpace: SubspaceEntity,
subspaceName?: string, subspaceName?: string,
): Promise<void> { ): Promise<SubspaceEntity> {
if (subspaceName) { if (subspaceName) {
subSpace.subspaceName = subspaceName; subSpace.subspaceName = subspaceName;
await queryRunner.manager.save(subSpace); return await queryRunner.manager.save(subSpace);
} }
return subSpace;
} }
private async checkForDuplicateNames(names: string[]): Promise<void> { private async checkForDuplicateNames(names: string[]): Promise<void> {