Refactor/space management (#404)

* refactor: reducing used queries on get communities (#385)

* refactor: fix create space logic (#394)

* Remove unique constraint on subspace and product in SubspaceProductAllocationEntity; update product relation to nullable in NewTagEntity

* refactor: fix create space logic

* device model updated to include the fixes and final columns

* updated space models to include suggested fixes, update final logic and column names

* task: removing old references of the old tag-product relation

* task: remove old use of tags

* task: remove old tag & tag model usage

* refactor: delete space

* task: remove unused functions

* fix lint rule
This commit is contained in:
ZaydSkaff
2025-06-11 13:15:21 +03:00
committed by GitHub
parent 4f5e1b23f6
commit 8503ee728d
64 changed files with 2314 additions and 4372 deletions

View File

@ -1,3 +1,12 @@
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
import { CommunityRepository } from '@app/common/modules/community/repositories';
import { DeviceRepository } from '@app/common/modules/device/repositories';
import { ProjectRepository } from '@app/common/modules/project/repositiories';
import {
SpaceModelEntity,
SpaceModelRepository,
} from '@app/common/modules/space-model';
import { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
import { SpaceRepository } from '@app/common/modules/space/repositories';
import {
BadRequestException,
@ -7,25 +16,11 @@ import {
Inject,
Injectable,
} from '@nestjs/common';
import { In } from 'typeorm';
import { CommunityService } from '../../community/services';
import { ProjectService } from '../../project/services';
import {
SpaceModelEntity,
SpaceModelRepository,
} from '@app/common/modules/space-model';
import { ProjectRepository } from '@app/common/modules/project/repositiories';
import { CommunityRepository } from '@app/common/modules/community/repositories';
import { DeviceRepository } from '@app/common/modules/device/repositories';
import { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
import { ValidateSpacesDto } from '../dtos/validation.space.dto';
import { ProjectParam } from '../dtos';
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
import { In } from 'typeorm';
import {
ORPHAN_COMMUNITY_NAME,
ORPHAN_SPACE_NAME,
} from '@app/common/constants/orphan-constant';
import { ProjectEntity } from '@app/common/modules/project/entities';
import { ValidateSpacesDto } from '../dtos/validation.space.dto';
@Injectable()
export class ValidationService {
@ -130,9 +125,7 @@ export class ValidationService {
'subspaces',
'productAllocations',
'productAllocations.product',
'productAllocations.tags',
'subspaces.productAllocations',
'subspaces.productAllocations.tags',
'subspaces.productAllocations.product',
'subspaces.devices',
'spaceModel',
@ -146,13 +139,13 @@ export class ValidationService {
);
}
const devices = await this.deviceRepository.find({
where: { spaceDevice: { uuid: spaceUuid } },
select: ['uuid', 'deviceTuyaUuid', 'isActive', 'createdAt', 'updatedAt'],
relations: ['productDevice', 'subspace'],
});
// const devices = await this.deviceRepository.find({
// where: { spaceDevice: { uuid: spaceUuid } },
// select: ['uuid', 'deviceTuyaUuid', 'isActive', 'createdAt', 'updatedAt'],
// relations: ['productDevice', 'subspace'],
// });
space.devices = devices;
// space.devices = devices;
return space;
}
@ -191,8 +184,8 @@ export class ValidationService {
'subspaceProductAllocations',
)
.leftJoinAndSelect(
'subspaceProductAllocations.tags',
'subspaceAllocationTags',
'subspaceProductAllocations.tag',
'subspaceAllocationTag',
)
.leftJoinAndSelect(
'subspaceProductAllocations.product',
@ -203,7 +196,7 @@ export class ValidationService {
'productAllocations.product',
'productAllocationProduct',
)
.leftJoinAndSelect('productAllocations.tags', 'productAllocationTags')
.leftJoinAndSelect('productAllocations.tag', 'productAllocationTag')
.andWhere('spaceModel.disabled = :disabled', { disabled: false })
.where('spaceModel.uuid = :uuid', { uuid: spaceModelUuid });
@ -219,39 +212,6 @@ export class ValidationService {
return spaceModel;
}
async getFullSpaceHierarchy(
space: SpaceEntity,
): Promise<{ uuid: string; spaceName: string }[]> {
try {
// Fetch only the relevant spaces, starting with the target space
const targetSpace = await this.spaceRepository.findOne({
where: { uuid: space.uuid },
relations: ['parent', 'children'],
});
// Fetch only the ancestors of the target space
const ancestors = await this.fetchAncestors(targetSpace);
// Optionally, fetch descendants if required
const descendants = await this.fetchDescendants(targetSpace);
const fullHierarchy = [...ancestors, targetSpace, ...descendants].map(
(space) => ({
uuid: space.uuid,
spaceName: space.spaceName,
}),
);
return fullHierarchy;
} catch (error) {
console.error('Error fetching space hierarchy:', error.message);
throw new HttpException(
'Error fetching space hierarchy',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
private async fetchAncestors(space: SpaceEntity): Promise<SpaceEntity[]> {
const ancestors: SpaceEntity[] = [];
@ -275,27 +235,6 @@ export class ValidationService {
return ancestors.reverse();
}
private async fetchDescendants(space: SpaceEntity): Promise<SpaceEntity[]> {
const descendants: SpaceEntity[] = [];
// Fetch the immediate children of the current space
const children = await this.spaceRepository.find({
where: { parent: { uuid: space.uuid } },
relations: ['children'], // To continue fetching downwards
});
for (const child of children) {
// Add the child to the descendants list
descendants.push(child);
// Recursively fetch the child's descendants
const childDescendants = await this.fetchDescendants(child);
descendants.push(...childDescendants);
}
return descendants;
}
async getParentHierarchy(
space: SpaceEntity,
): Promise<{ uuid: string; spaceName: string }[]> {
@ -323,24 +262,4 @@ export class ValidationService {
);
}
}
async getOrphanSpace(project: ProjectEntity): Promise<SpaceEntity> {
const orphanSpace = await this.spaceRepository.findOne({
where: {
community: {
name: `${ORPHAN_COMMUNITY_NAME}-${project.name}`,
},
spaceName: ORPHAN_SPACE_NAME,
},
});
if (!orphanSpace) {
throw new HttpException(
`Orphan space not found in community ${project.name}`,
HttpStatus.NOT_FOUND,
);
}
return orphanSpace;
}
}