mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-10 07:07:21 +00:00

* task: add getCommunitiesV2 * task: update getOneSpace API to match revamp structure * refactor: implement modifications to pace management APIs * refactor: remove space link
93 lines
2.8 KiB
TypeScript
93 lines
2.8 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 { 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 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',
|
|
'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.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();
|
|
}
|
|
}
|
|
}
|