mirror of
https://github.com/SyncrowIOT/backend.git
synced 2026-10-23 14:17:55 +00:00
delete subspace devices
This commit is contained in:
@@ -49,6 +49,7 @@ export class ValidationService {
|
||||
'subspaces',
|
||||
'tags',
|
||||
'subspaces.tags',
|
||||
'subspaces.devices',
|
||||
'devices',
|
||||
],
|
||||
});
|
||||
|
||||
@@ -9,6 +9,8 @@ import { ProductRepository } from '@app/common/modules/product/repositories';
|
||||
import { GetDeviceDetailsInterface } from '../../../device/interfaces/get.device.interface';
|
||||
import { ValidationService } from '../space-validation.service';
|
||||
import { SubspaceRepository } from '@app/common/modules/space/repositories/subspace.repository';
|
||||
import { In, QueryRunner } from 'typeorm';
|
||||
import { DeviceEntity } from '@app/common/modules/device/entities';
|
||||
|
||||
@Injectable()
|
||||
export class SubspaceDeviceService {
|
||||
@@ -173,6 +175,30 @@ export class SubspaceDeviceService {
|
||||
return device;
|
||||
}
|
||||
|
||||
async deleteSubspaceDevices(
|
||||
devices: DeviceEntity[],
|
||||
queryRunner: QueryRunner,
|
||||
): Promise<void> {
|
||||
const deviceUuids = devices.map((device) => device.uuid);
|
||||
|
||||
try {
|
||||
if (deviceUuids.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
await queryRunner.manager.update(
|
||||
this.deviceRepository.target,
|
||||
{ uuid: In(deviceUuids) },
|
||||
{ subspace: null },
|
||||
);
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
`Failed to delete devices with IDs ${deviceUuids.join(', ')}: ${error.message}`,
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private throwNotFound(entity: string, uuid: string) {
|
||||
throw new HttpException(
|
||||
`${entity} with ID ${uuid} not found`,
|
||||
|
||||
@@ -25,6 +25,7 @@ import { ValidationService } from '../space-validation.service';
|
||||
import { SubspaceRepository } from '@app/common/modules/space/repositories/subspace.repository';
|
||||
import { TagService } from '../tag';
|
||||
import { ModifyAction } from '@app/common/constants/modify-action.enum';
|
||||
import { SubspaceDeviceService } from './subspace-device.service';
|
||||
|
||||
@Injectable()
|
||||
export class SubSpaceService {
|
||||
@@ -32,6 +33,7 @@ export class SubSpaceService {
|
||||
private readonly subspaceRepository: SubspaceRepository,
|
||||
private readonly validationService: ValidationService,
|
||||
private readonly tagService: TagService,
|
||||
public readonly deviceService: SubspaceDeviceService,
|
||||
) {}
|
||||
|
||||
async createSubspaces(
|
||||
@@ -280,7 +282,7 @@ export class SubSpaceService {
|
||||
const deleteResults: { uuid: string }[] = [];
|
||||
|
||||
for (const dto of deleteDtos) {
|
||||
const subspaceModel = await this.findOne(dto.subspaceUuid);
|
||||
const subspace = await this.findOne(dto.subspaceUuid);
|
||||
|
||||
await queryRunner.manager.update(
|
||||
this.subspaceRepository.target,
|
||||
@@ -288,8 +290,8 @@ export class SubSpaceService {
|
||||
{ disabled: true },
|
||||
);
|
||||
|
||||
if (subspaceModel.tags?.length) {
|
||||
const modifyTagDtos = subspaceModel.tags.map((tag) => ({
|
||||
if (subspace.tags?.length) {
|
||||
const modifyTagDtos = subspace.tags.map((tag) => ({
|
||||
uuid: tag.uuid,
|
||||
action: ModifyAction.DELETE,
|
||||
}));
|
||||
@@ -297,10 +299,16 @@ export class SubSpaceService {
|
||||
modifyTagDtos,
|
||||
queryRunner,
|
||||
null,
|
||||
subspaceModel,
|
||||
subspace,
|
||||
);
|
||||
}
|
||||
|
||||
if (subspace.devices)
|
||||
await this.deviceService.deleteSubspaceDevices(
|
||||
subspace.devices,
|
||||
queryRunner,
|
||||
);
|
||||
|
||||
deleteResults.push({ uuid: dto.subspaceUuid });
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user