Files
backend/src/space/services/space-validation.service.ts
T
2025-02-03 00:43:38 +04:00

129 lines
3.9 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';
import { ProjectRepository } from '@app/common/modules/project/repositiories';
import { CommunityRepository } from '@app/common/modules/community/repositories';
import { DeviceRepository } from '@app/common/modules/device/repositories';
@Injectable()
export class ValidationService {
constructor(
private readonly projectService: ProjectService,
private readonly communityService: CommunityService,
private readonly spaceRepository: SpaceRepository,
private readonly projectRepository: ProjectRepository,
private readonly communityRepository: CommunityRepository,
private readonly spaceModelRepository: SpaceModelRepository,
private readonly deviceRepository: DeviceRepository,
) {}
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 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,
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: [
'parent',
'children',
'subspaces',
'tags',
'subspaces.tags',
'subspaces.devices',
],
});
if (!space) {
throw new HttpException(
`Space with UUID ${spaceUuid} not found`,
HttpStatus.NOT_FOUND,
);
}
const devices = await this.deviceRepository.find({
where: { spaceDevice: { uuid: spaceUuid } },
select: ['uuid', 'deviceTuyaUuid', 'isActive', 'createdAt', 'updatedAt'],
relations: ['productDevice', 'tag', 'subspace'],
});
space.devices = devices;
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;
}
}