mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-12 08:17:28 +00:00
delete propogation
This commit is contained in:
100
src/space/handlers/disable-space.handler.ts
Normal file
100
src/space/handlers/disable-space.handler.ts
Normal file
@ -0,0 +1,100 @@
|
||||
import { SpaceEntity } from '@app/common/modules/space';
|
||||
import { HttpException, HttpStatus } from '@nestjs/common';
|
||||
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs';
|
||||
import { DeviceService } from 'src/device/services';
|
||||
import { UserSpaceService } from 'src/users/services';
|
||||
import { DataSource } from 'typeorm';
|
||||
import { DisableSpaceCommand } from '../commands';
|
||||
import {
|
||||
SubSpaceService,
|
||||
SpaceLinkService,
|
||||
SpaceSceneService,
|
||||
} from '../services';
|
||||
import { TagService } from '../services/tag';
|
||||
|
||||
@CommandHandler(DisableSpaceCommand)
|
||||
export class DisableSpaceHandler
|
||||
implements ICommandHandler<DisableSpaceCommand>
|
||||
{
|
||||
constructor(
|
||||
private readonly subSpaceService: SubSpaceService,
|
||||
private readonly userService: UserSpaceService,
|
||||
private readonly tagService: TagService,
|
||||
private readonly deviceService: DeviceService,
|
||||
private readonly spaceLinkService: SpaceLinkService,
|
||||
private readonly sceneService: SpaceSceneService,
|
||||
private readonly dataSource: DataSource,
|
||||
) {}
|
||||
|
||||
async execute(command: DisableSpaceCommand): Promise<void> {
|
||||
const queryRunner = this.dataSource.createQueryRunner();
|
||||
|
||||
await queryRunner.connect();
|
||||
await queryRunner.startTransaction();
|
||||
|
||||
try {
|
||||
const { spaceUuid, orphanSpace } = command.param;
|
||||
|
||||
const space = await queryRunner.manager.findOne(SpaceEntity, {
|
||||
where: { uuid: spaceUuid, disabled: false },
|
||||
relations: [
|
||||
'subspaces',
|
||||
'parent',
|
||||
'tags',
|
||||
'devices',
|
||||
'outgoingConnections',
|
||||
'incomingConnections',
|
||||
'scenes',
|
||||
'children',
|
||||
'userSpaces',
|
||||
],
|
||||
});
|
||||
|
||||
if (!space) {
|
||||
throw new HttpException(
|
||||
`Space with UUID ${spaceUuid} not found`,
|
||||
HttpStatus.NOT_FOUND,
|
||||
);
|
||||
}
|
||||
|
||||
if (space.children && space.children.length > 0) {
|
||||
for (const child of space.children) {
|
||||
await this.execute(
|
||||
new DisableSpaceCommand({ spaceUuid: child.uuid, orphanSpace }),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const tagUuids = space.tags?.map((tag) => tag.uuid) || [];
|
||||
const subspaceDtos =
|
||||
space.subspaces?.map((subspace) => ({
|
||||
subspaceUuid: subspace.uuid,
|
||||
})) || [];
|
||||
const deletionTasks = [
|
||||
this.subSpaceService.deleteSubspaces(subspaceDtos, queryRunner),
|
||||
this.userService.deleteUserSpace(space.uuid),
|
||||
this.tagService.deleteTags(tagUuids, queryRunner),
|
||||
this.deviceService.deleteDevice(
|
||||
space.devices,
|
||||
orphanSpace,
|
||||
queryRunner,
|
||||
),
|
||||
this.spaceLinkService.deleteSpaceLink(space, queryRunner),
|
||||
this.sceneService.deleteScenes(space, queryRunner),
|
||||
];
|
||||
|
||||
await Promise.all(deletionTasks);
|
||||
|
||||
// Mark space as disabled
|
||||
space.disabled = true;
|
||||
await queryRunner.manager.save(space);
|
||||
|
||||
await queryRunner.commitTransaction();
|
||||
} catch (error) {
|
||||
await queryRunner.rollbackTransaction();
|
||||
console.error(`Failed to disable space: ${error.message}`);
|
||||
} finally {
|
||||
await queryRunner.release();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user