Files
backend/src/space/handlers/disable-space.handler.ts
ZaydSkaff 8503ee728d Refactor/space management (#404)
* refactor: reducing used queries on get communities (#385)

* refactor: fix create space logic (#394)

* Remove unique constraint on subspace and product in SubspaceProductAllocationEntity; update product relation to nullable in NewTagEntity

* refactor: fix create space logic

* device model updated to include the fixes and final columns

* updated space models to include suggested fixes, update final logic and column names

* task: removing old references of the old tag-product relation

* task: remove old use of tags

* task: remove old tag & tag model usage

* refactor: delete space

* task: remove unused functions

* fix lint rule
2025-06-11 13:15:21 +03:00

101 lines
3.0 KiB
TypeScript

import { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
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 {
SpaceLinkService,
SpaceSceneService,
SubSpaceService,
} from '../services';
@CommandHandler(DisableSpaceCommand)
export class DisableSpaceHandler
implements ICommandHandler<DisableSpaceCommand>
{
constructor(
private readonly subSpaceService: SubSpaceService,
private readonly userService: UserSpaceService,
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',
'devices',
'outgoingConnections',
'incomingConnections',
'scenes',
'children',
'userSpaces',
'productAllocations',
'productAllocations.tag',
'productAllocations.product',
],
});
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.productAllocations?.map(({ tag }) => tag.uuid) || [];
/* const subspaceDtos =
space.subspaces?.map((subspace) => ({
subspaceUuid: subspace.uuid,
})) || []; */
const deletionTasks = [
this.userService.deleteUserSpace(space.uuid),
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();
}
}
}