import { SpaceEntity } from '@app/common/modules/space/entities'; import { SpaceRepository } from '@app/common/modules/space/repositories'; import { HttpException, HttpStatus, Injectable } from '@nestjs/common'; import { CommunityService } from '../../community/services'; import { ProjectService } from '../../project/services'; import { SpaceModelEntity, SpaceModelRepository, } from '@app/common/modules/space-model'; @Injectable() export class ValidationService { constructor( private readonly projectService: ProjectService, private readonly communityService: CommunityService, private readonly spaceRepository: SpaceRepository, private readonly spaceModelRepository: SpaceModelRepository, ) {} async validateCommunityAndProject( communityUuid: string, projectUuid: string, ) { const project = await this.projectService.findOne(projectUuid); const community = await this.communityService.getCommunityById({ communityUuid, projectUuid, }); return { community: community.data, project: project }; } async validateSpaceWithinCommunityAndProject( communityUuid: string, projectUuid: string, spaceUuid?: string, ): Promise { await this.validateCommunityAndProject(communityUuid, projectUuid); const space = await this.validateSpace(spaceUuid); return space; } async validateSpace(spaceUuid: string): Promise { const space = await this.spaceRepository.findOne({ where: { uuid: spaceUuid, disabled: false }, relations: ['subspaces', 'tags'], }); if (!space) { throw new HttpException( `Space with UUID ${spaceUuid} not found`, HttpStatus.NOT_FOUND, ); } return space; } async validateSpaceModel(spaceModelUuid: string): Promise { const spaceModel = await this.spaceModelRepository.findOne({ where: { uuid: spaceModelUuid }, relations: [ 'subspaceModels', 'subspaceModels.tags', 'tags', 'subspaceModels.tags.product', 'tags.product', ], }); if (!spaceModel) { throw new HttpException( `Space model with UUID ${spaceModelUuid} not found`, HttpStatus.NOT_FOUND, ); } return spaceModel; } }