Files
backend/src/space/services/space-validation.service.ts
hannathkadher f7b75a630a disabling space
2024-12-25 16:23:09 +04:00

82 lines
2.3 KiB
TypeScript

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<SpaceEntity> {
await this.validateCommunityAndProject(communityUuid, projectUuid);
const space = await this.validateSpace(spaceUuid);
return space;
}
async validateSpace(spaceUuid: string): Promise<SpaceEntity> {
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<SpaceModelEntity> {
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;
}
}