disabling space

This commit is contained in:
hannathkadher
2024-12-25 16:23:09 +04:00
parent f9fe209076
commit f7b75a630a
12 changed files with 198 additions and 27 deletions
+2 -2
View File
@@ -67,8 +67,8 @@ export class SpaceController {
description: ControllerRoute.SPACE.ACTIONS.DELETE_SPACE_DESCRIPTION,
})
@Delete('/:spaceUuid')
async deleteSpace(@Param() params: GetSpaceParam): Promise<BaseResponseDto> {
return this.spaceService.delete(params);
async deleteSpace(@Param() params: GetSpaceParam){
return await this.spaceService.deleteSpace(params);
}
@ApiBearerAuth()
@@ -92,4 +92,35 @@ export class SpaceLinkService {
);
}
}
async deleteSpaceLink(
space: SpaceEntity,
queryRunner: QueryRunner,
): Promise<void> {
try {
const spaceLinks = await queryRunner.manager.find(SpaceLinkEntity, {
where: [
{ startSpace: space, disabled: false },
{ endSpace: space, disabled: false },
],
});
if (spaceLinks.length === 0) {
return;
}
const linkIds = spaceLinks.map((link) => link.uuid);
await queryRunner.manager
.createQueryBuilder()
.update(SpaceLinkEntity)
.set({ disabled: true })
.whereInIds(linkIds)
.execute();
} catch (error) {
throw new HttpException(
`Failed to disable space links for the given space: ${error.message}`,
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
}
+31
View File
@@ -5,6 +5,9 @@ import { SceneService } from '../../scene/services';
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
import { GetSceneDto } from '../../scene/dtos';
import { ValidationService } from './space-validation.service';
import { SpaceEntity } from '@app/common/modules/space';
import { QueryRunner } from 'typeorm';
import { SceneEntity } from '@app/common/modules/scene/entities';
@Injectable()
export class SpaceSceneService {
@@ -48,4 +51,32 @@ export class SpaceSceneService {
}
}
}
async deleteScenes(
space: SpaceEntity,
queryRunner: QueryRunner,
): Promise<void> {
try {
const scenes = await queryRunner.manager.find(SceneEntity, {
where: { space },
});
if (scenes.length === 0) {
return;
}
const sceneUuids = scenes.map((scene) => scene.uuid);
await Promise.all(
sceneUuids.map((uuid) =>
this.sceneSevice.deleteScene({ sceneUuid: uuid }),
),
);
} catch (error) {
throw new HttpException(
`Failed to delete scenes: ${error.message}`,
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
}
@@ -1,4 +1,3 @@
import { CommunityEntity } from '@app/common/modules/community/entities';
import { SpaceEntity } from '@app/common/modules/space/entities';
import { SpaceRepository } from '@app/common/modules/space/repositories';
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
@@ -21,16 +20,17 @@ export class ValidationService {
async validateCommunityAndProject(
communityUuid: string,
projectUuid: string,
): Promise<CommunityEntity> {
await this.projectService.findOne(projectUuid);
) {
const project = await this.projectService.findOne(projectUuid);
const community = await this.communityService.getCommunityById({
communityUuid,
projectUuid,
});
return community.data;
return { community: community.data, project: project };
}
async validateSpaceWithinCommunityAndProject(
communityUuid: string,
projectUuid: string,
+55 -12
View File
@@ -24,6 +24,9 @@ import { ValidationService } from './space-validation.service';
import { ORPHAN_SPACE_NAME } from '@app/common/constants/orphan-constant';
import { TagService } from './tag';
import { SpaceModelService } from 'src/space-model/services';
import { UserSpaceService } from 'src/users/services';
import { DeviceService } from 'src/device/services';
import { SpaceSceneService } from './space-scene.service';
@Injectable()
export class SpaceService {
@@ -35,6 +38,9 @@ export class SpaceService {
private readonly validationService: ValidationService,
private readonly tagService: TagService,
private readonly spaceModelService: SpaceModelService,
private readonly userService: UserSpaceService,
private readonly deviceService: DeviceService,
private readonly sceneService: SpaceSceneService,
) {}
async createSpace(
@@ -363,33 +369,70 @@ export class SpaceService {
}
async deleteSpace(params: GetSpaceParam) {
const queryRunner = this.dataSource.createQueryRunner();
const { spaceUuid } = params;
try {
const { communityUuid, spaceUuid, projectUuid } = params;
const space =
await this.validationService.validateSpaceWithinCommunityAndProject(
communityUuid,
projectUuid,
spaceUuid,
);
await queryRunner.connect();
await queryRunner.startTransaction();
const spaces = await this.spaceRepository.find({
where: { parent: { uuid: spaceUuid }, disabled: false },
relations: ['parent', 'children'], // Include parent and children relations
relations: [
'parent',
'children',
'subspaces',
'tags',
'subspaces.tags',
'devices',
], // Include parent and children relations
});
console.log(spaces);
// space.disabled = true;
await this.spaceRepository.update({ uuid: space.uuid }, space);
//this.disableSpace(space, orphanSpace);
return spaces;
} catch (error) {
if (error instanceof HttpException) {
throw error;
}
throw new HttpException(
'An error occurred while deleting the space',
`An error occurred while deleting the space ${error}`,
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
async disableSpace(space: SpaceEntity, orphanSpace: SpaceEntity) {
const queryRunner = this.dataSource.createQueryRunner();
await queryRunner.connect();
await queryRunner.startTransaction();
try {
const deleteSubspaceDtos = space.subspaces?.map((subspace) => ({
subspaceUuid: subspace.uuid,
}));
const deleteSpaceTagsDtos = space.tags?.map((tag) => tag.uuid);
await this.userService.deleteUserSpace(space.uuid);
await this.subSpaceService.deleteSubspaces(
deleteSubspaceDtos,
queryRunner,
);
await this.tagService.deleteTags(deleteSpaceTagsDtos, queryRunner);
await this.deviceService.deleteDevice(
space.devices,
orphanSpace,
queryRunner,
);
await this.spaceLinkService.deleteSpaceLink(space, queryRunner);
await this.sceneService.deleteScenes(space, queryRunner);
} catch (error) {
await queryRunner.rollbackTransaction();
} finally {
await queryRunner.release();
}
}
private updateSpaceProperties(
space: SpaceEntity,
updateSpaceDto: UpdateSpaceDto,
@@ -19,7 +19,6 @@ import { In, QueryRunner } from 'typeorm';
import {
SpaceEntity,
SubspaceEntity,
TagEntity,
} from '@app/common/modules/space/entities';
import { SubspaceModelEntity } from '@app/common/modules/space-model';
import { ValidationService } from '../space-validation.service';
+11 -1
View File
@@ -28,7 +28,10 @@ import {
UserRepository,
UserSpaceRepository,
} from '@app/common/modules/user/repositories';
import { DeviceRepository } from '@app/common/modules/device/repositories';
import {
DeviceRepository,
DeviceUserPermissionRepository,
} from '@app/common/modules/device/repositories';
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
import { ProductRepository } from '@app/common/modules/product/repositories';
import { SceneService } from '../scene/services';
@@ -55,6 +58,9 @@ import {
SubSpaceModelService,
TagModelService,
} from 'src/space-model/services';
import { UserSpaceService } from 'src/users/services';
import { UserDevicePermissionService } from 'src/user-device-permission/services';
import { PermissionTypeRepository } from '@app/common/modules/permission/repositories';
@Module({
imports: [ConfigModule, SpaceRepositoryModule, CommunityModule],
@@ -100,6 +106,10 @@ import {
ProjectRepository,
SpaceModelRepository,
SubspaceModelRepository,
UserSpaceService,
UserDevicePermissionService,
DeviceUserPermissionRepository,
PermissionTypeRepository,
],
exports: [SpaceService],
})