optimizing space call

This commit is contained in:
hannathkadher
2025-02-03 00:13:34 +04:00
parent 87c2722313
commit 7f69867e8a
3 changed files with 51 additions and 12 deletions

View File

@ -7,6 +7,8 @@ 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';
@Injectable()
export class ValidationService {
@ -14,6 +16,8 @@ export class ValidationService {
private readonly projectService: ProjectService,
private readonly communityService: CommunityService,
private readonly spaceRepository: SpaceRepository,
private readonly projectRepository: ProjectRepository,
private readonly communityRepository: CommunityRepository,
private readonly spaceModelRepository: SpaceModelRepository,
) {}
@ -30,6 +34,31 @@ export class ValidationService {
return { community: community.data, project: project };
}
async checkCommunityAndProjectSpaceExistence(
communityUuid: string,
projectUuid: string,
spaceUuid?: string,
): Promise<void> {
const [projectExists, communityExists, spaceExists] = await Promise.all([
this.projectRepository.exists({ where: { uuid: projectUuid } }),
this.communityRepository.exists({
where: { uuid: communityUuid, project: { uuid: projectUuid } },
}),
spaceUuid
? this.spaceRepository.exists({
where: { uuid: spaceUuid },
})
: Promise.resolve(true),
]);
if (!projectExists)
throw new HttpException(`Project not found`, HttpStatus.NOT_FOUND);
if (!communityExists)
throw new HttpException(`Community not found`, HttpStatus.NOT_FOUND);
if (spaceUuid && !spaceExists)
throw new HttpException(`Space not found`, HttpStatus.NOT_FOUND);
}
async validateSpaceWithinCommunityAndProject(
communityUuid: string,
projectUuid: string,
@ -50,10 +79,10 @@ export class ValidationService {
'tags',
'subspaces.tags',
'subspaces.devices',
'devices',
'devices.productDevice',
'devices.tag',
'devices.subspace',
//'devices',
//'devices.productDevice',
//'devices.tag',
// 'devices.subspace',
],
});