mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-16 18:56:22 +00:00
finished spaces validation endpoint
This commit is contained in:
@ -1,5 +1,10 @@
|
||||
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||
import {
|
||||
BadRequestException,
|
||||
HttpException,
|
||||
HttpStatus,
|
||||
Injectable,
|
||||
} from '@nestjs/common';
|
||||
import { CommunityService } from '../../community/services';
|
||||
import { ProjectService } from '../../project/services';
|
||||
import {
|
||||
@ -10,6 +15,10 @@ 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';
|
||||
|
||||
@Injectable()
|
||||
export class ValidationService {
|
||||
@ -22,7 +31,34 @@ export class ValidationService {
|
||||
private readonly spaceModelRepository: SpaceModelRepository,
|
||||
private readonly deviceRepository: DeviceRepository,
|
||||
) {}
|
||||
async validateSpacesWithDevicesOrSubspaces(
|
||||
validateSpacesDto: ValidateSpacesDto,
|
||||
projectParam: ProjectParam,
|
||||
) {
|
||||
const { spacesUuids } = validateSpacesDto;
|
||||
const { projectUuid } = projectParam;
|
||||
await this.communityService.validateProject(projectUuid);
|
||||
const spaces = await this.spaceRepository.find({
|
||||
where: { uuid: In(spacesUuids), disabled: false },
|
||||
relations: ['devices', 'subspaces'],
|
||||
});
|
||||
|
||||
const hasInvalidSpaces = spaces.some(
|
||||
(space) => space.devices.length > 0 || space.subspaces.length > 0,
|
||||
);
|
||||
|
||||
if (hasInvalidSpaces) {
|
||||
throw new BadRequestException(
|
||||
'Selected spaces already have linked space model / sub-spaces and devices',
|
||||
);
|
||||
}
|
||||
|
||||
return new SuccessResponseDto({
|
||||
statusCode: HttpStatus.OK,
|
||||
message:
|
||||
'Validation completed successfully. No spaces have linked devices or sub-spaces.',
|
||||
});
|
||||
}
|
||||
async validateCommunityAndProject(
|
||||
communityUuid: string,
|
||||
projectUuid: string,
|
||||
|
Reference in New Issue
Block a user