mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-16 18:56:22 +00:00
fixing propagation
This commit is contained in:
@ -6,7 +6,6 @@ import {
|
||||
Unique,
|
||||
Index,
|
||||
JoinColumn,
|
||||
OneToOne,
|
||||
JoinTable,
|
||||
} from 'typeorm';
|
||||
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
||||
@ -18,7 +17,6 @@ import { PermissionTypeEntity } from '../../permission/entities';
|
||||
import { SceneDeviceEntity } from '../../scene-device/entities';
|
||||
import { SpaceEntity } from '../../space/entities/space.entity';
|
||||
import { SubspaceEntity } from '../../space/entities/subspace/subspace.entity';
|
||||
import { TagEntity } from '../../space/entities/tag.entity';
|
||||
import { NewTagEntity } from '../../tag';
|
||||
|
||||
@Entity({ name: 'device' })
|
||||
@ -79,14 +77,10 @@ export class DeviceEntity extends AbstractEntity<DeviceDto> {
|
||||
@OneToMany(() => SceneDeviceEntity, (sceneDevice) => sceneDevice.device, {})
|
||||
sceneDevices: SceneDeviceEntity[];
|
||||
|
||||
@OneToOne(() => TagEntity, (tag) => tag.device, {
|
||||
nullable: true,
|
||||
})
|
||||
tag: TagEntity;
|
||||
|
||||
@OneToMany(() => NewTagEntity, (tag) => tag.devices)
|
||||
@JoinTable({ name: 'device_tags' })
|
||||
public tags: NewTagEntity[];
|
||||
public tag: NewTagEntity[];
|
||||
|
||||
constructor(partial: Partial<DeviceEntity>) {
|
||||
super();
|
||||
|
@ -1546,4 +1546,21 @@ export class DeviceService {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async moveDevicesToSpace(
|
||||
targetSpace: SpaceEntity,
|
||||
deviceIds: string[],
|
||||
): Promise<void> {
|
||||
if (!deviceIds || deviceIds.length === 0) {
|
||||
throw new HttpException(
|
||||
'No device IDs provided for transfer',
|
||||
HttpStatus.BAD_REQUEST,
|
||||
);
|
||||
}
|
||||
|
||||
await this.deviceRepository.update(
|
||||
{ uuid: In(deviceIds) },
|
||||
{ spaceDevice: targetSpace },
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -104,7 +104,7 @@ export class PropogateUpdateSpaceModelHandler
|
||||
for (const space of spaces) {
|
||||
await this.subSpaceService.createSubSpaceFromModel(
|
||||
subspaceModels,
|
||||
space,
|
||||
[space],
|
||||
queryRunner,
|
||||
);
|
||||
}
|
||||
|
@ -16,6 +16,7 @@ import { ProductEntity } from '@app/common/modules/product/entities';
|
||||
import { SpaceRepository } from '@app/common/modules/space';
|
||||
import { SpaceProductAllocationService } from 'src/space/services/space-product-allocation.service';
|
||||
import { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
|
||||
import { ProjectEntity } from '@app/common/modules/project/entities';
|
||||
|
||||
@Injectable()
|
||||
export class SpaceModelProductAllocationService {
|
||||
@ -141,7 +142,7 @@ export class SpaceModelProductAllocationService {
|
||||
|
||||
async updateProductAllocations(
|
||||
dtos: ModifyTagModelDto[],
|
||||
projectUuid: string,
|
||||
project: ProjectEntity,
|
||||
spaceModel: SpaceModelEntity,
|
||||
queryRunner: QueryRunner,
|
||||
modifySubspaceModels?: ModifySubspaceModelDto[],
|
||||
@ -161,7 +162,7 @@ export class SpaceModelProductAllocationService {
|
||||
|
||||
const processedTags = await this.tagService.processTags(
|
||||
addTagDtos,
|
||||
projectUuid,
|
||||
project.uuid,
|
||||
queryRunner,
|
||||
);
|
||||
const addTagUuidMap = new Map<string, ModifyTagModelDto>();
|
||||
@ -192,13 +193,19 @@ export class SpaceModelProductAllocationService {
|
||||
await Promise.all([
|
||||
this.processAddActions(
|
||||
filteredDtos,
|
||||
projectUuid,
|
||||
project.uuid,
|
||||
spaceModel,
|
||||
queryRunner,
|
||||
modifySubspaceModels,
|
||||
spaces,
|
||||
),
|
||||
this.processDeleteActions(filteredDtos, queryRunner, spaceModel),
|
||||
this.processDeleteActions(
|
||||
filteredDtos,
|
||||
queryRunner,
|
||||
spaceModel,
|
||||
project,
|
||||
spaces,
|
||||
),
|
||||
]);
|
||||
} catch (error) {
|
||||
throw this.handleError(error, 'Error while updating product allocations');
|
||||
@ -304,6 +311,8 @@ export class SpaceModelProductAllocationService {
|
||||
dtos: ModifyTagModelDto[],
|
||||
queryRunner: QueryRunner,
|
||||
spaceModel: SpaceModelEntity,
|
||||
project: ProjectEntity,
|
||||
spaces?: SpaceEntity[],
|
||||
): Promise<SpaceModelProductAllocationEntity[]> {
|
||||
try {
|
||||
if (!dtos || dtos.length === 0) {
|
||||
@ -325,7 +334,11 @@ export class SpaceModelProductAllocationService {
|
||||
uuid: spaceModel.uuid,
|
||||
},
|
||||
},
|
||||
relations: ['tags'],
|
||||
relations: [
|
||||
'tags',
|
||||
'inheritedSpaceAllocations',
|
||||
'inheritedSpaceAllocations.tags',
|
||||
],
|
||||
},
|
||||
);
|
||||
|
||||
@ -365,6 +378,14 @@ export class SpaceModelProductAllocationService {
|
||||
);
|
||||
}
|
||||
|
||||
await this.spaceProductAllocationService.propagateDeleteToInheritedAllocations(
|
||||
queryRunner,
|
||||
allocationsToUpdate,
|
||||
tagUuidsToDelete,
|
||||
project,
|
||||
spaces,
|
||||
);
|
||||
|
||||
await queryRunner.manager
|
||||
.createQueryBuilder()
|
||||
.delete()
|
||||
|
@ -189,7 +189,7 @@ export class SpaceModelService {
|
||||
|
||||
async update(dto: UpdateSpaceModelDto, param: SpaceModelParam) {
|
||||
const queryRunner = this.dataSource.createQueryRunner();
|
||||
await this.validateProject(param.projectUuid);
|
||||
const project = await this.validateProject(param.projectUuid);
|
||||
const spaceModel = await this.validateSpaceModel(
|
||||
param.spaceModelUuid,
|
||||
param.projectUuid,
|
||||
@ -227,7 +227,7 @@ export class SpaceModelService {
|
||||
if (dto.tags) {
|
||||
await this.spaceModelProductAllocationService.updateProductAllocations(
|
||||
dto.tags,
|
||||
param.projectUuid,
|
||||
project,
|
||||
spaceModel,
|
||||
queryRunner,
|
||||
dto.subspaceModels,
|
||||
|
@ -14,6 +14,8 @@ import { ProcessTagDto } from 'src/tags/dtos';
|
||||
import { TagService } from 'src/tags/services';
|
||||
import { SubspaceModelProductAllocationService } from './subspace-model-product-allocation.service';
|
||||
import { ISingleSubspaceModel } from 'src/space-model/interfaces';
|
||||
import { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
|
||||
import { SubSpaceService } from 'src/space/services/subspace';
|
||||
|
||||
@Injectable()
|
||||
export class SubSpaceModelService {
|
||||
@ -21,6 +23,7 @@ export class SubSpaceModelService {
|
||||
private readonly subspaceModelRepository: SubspaceModelRepository,
|
||||
private readonly tagService: TagService,
|
||||
private readonly productAllocationService: SubspaceModelProductAllocationService,
|
||||
private readonly subspaceService: SubSpaceService,
|
||||
) {}
|
||||
|
||||
async createModels(
|
||||
@ -74,6 +77,7 @@ export class SubSpaceModelService {
|
||||
dtos: ModifySubspaceModelDto[],
|
||||
spaceModel: SpaceModelEntity,
|
||||
queryRunner: QueryRunner,
|
||||
spaces?: SpaceEntity[],
|
||||
): Promise<ISingleSubspaceModel[]> {
|
||||
if (!dtos.length) return [];
|
||||
|
||||
@ -89,6 +93,14 @@ export class SubSpaceModelService {
|
||||
|
||||
const savedSubspaces = await queryRunner.manager.save(subspaceEntities);
|
||||
|
||||
if (spaces) {
|
||||
await this.subspaceService.createSubSpaceFromModel(
|
||||
savedSubspaces,
|
||||
spaces,
|
||||
queryRunner,
|
||||
);
|
||||
}
|
||||
|
||||
return savedSubspaces.map((subspace, index) => ({
|
||||
subspaceModel: subspace,
|
||||
action: ModifyAction.ADD,
|
||||
@ -102,6 +114,7 @@ export class SubSpaceModelService {
|
||||
queryRunner: QueryRunner,
|
||||
projectUuid: string,
|
||||
spaceTagUpdateDtos?: ModifyTagModelDto[],
|
||||
spaces?: SpaceEntity[],
|
||||
) {
|
||||
try {
|
||||
if (!dtos || dtos.length === 0) {
|
||||
@ -125,6 +138,7 @@ export class SubSpaceModelService {
|
||||
addDtos,
|
||||
spaceModel,
|
||||
queryRunner,
|
||||
spaces,
|
||||
);
|
||||
const combineModels = [...addedModels, ...updatedModels];
|
||||
|
||||
|
@ -46,6 +46,16 @@ import { SpaceModelProductAllocationService } from './services/space-model-produ
|
||||
import { SubspaceModelProductAllocationService } from './services/subspace/subspace-model-product-allocation.service';
|
||||
import { SpaceProductAllocationService } from 'src/space/services/space-product-allocation.service';
|
||||
import { SubspaceProductAllocationService } from 'src/space/services/subspace/subspace-product-allocation.service';
|
||||
import { DeviceService } from 'src/device/services';
|
||||
import { SceneDeviceRepository } from '@app/common/modules/scene-device/repositories';
|
||||
import { DeviceStatusFirebaseService } from '@app/common/firebase/devices-status/services/devices-status.service';
|
||||
import { SceneService } from 'src/scene/services';
|
||||
import { DeviceStatusLogRepository } from '@app/common/modules/device-status-log/repositories';
|
||||
import {
|
||||
SceneIconRepository,
|
||||
SceneRepository,
|
||||
} from '@app/common/modules/scene/repositories';
|
||||
import { AutomationRepository } from '@app/common/modules/automation/repositories';
|
||||
|
||||
const CommandHandlers = [
|
||||
PropogateUpdateSpaceModelHandler,
|
||||
@ -80,6 +90,14 @@ const CommandHandlers = [
|
||||
SpaceLinkRepository,
|
||||
InviteSpaceRepository,
|
||||
NewTagService,
|
||||
DeviceService,
|
||||
DeviceStatusFirebaseService,
|
||||
DeviceStatusLogRepository,
|
||||
SceneService,
|
||||
SceneIconRepository,
|
||||
SceneDeviceRepository,
|
||||
SceneRepository,
|
||||
AutomationRepository,
|
||||
SpaceModelProductAllocationRepoitory,
|
||||
SubspaceModelProductAllocationRepoitory,
|
||||
NewTagRepository,
|
||||
@ -90,6 +108,7 @@ const CommandHandlers = [
|
||||
SubspaceProductAllocationService,
|
||||
SpaceProductAllocationRepository,
|
||||
SubspaceProductAllocationRepository,
|
||||
SubSpaceService,
|
||||
],
|
||||
exports: [CqrsModule, SpaceModelService],
|
||||
})
|
||||
|
@ -13,12 +13,16 @@ import { ModifySubspaceDto } from '../dtos';
|
||||
import { ProcessTagDto } from 'src/tags/dtos';
|
||||
import { TagService as NewTagService } from 'src/tags/services';
|
||||
import { SpaceModelProductAllocationEntity } from '@app/common/modules/space-model';
|
||||
import { DeviceEntity } from '@app/common/modules/device/entities';
|
||||
import { ProjectEntity } from '@app/common/modules/project/entities';
|
||||
import { ValidationService } from './space-validation.service';
|
||||
|
||||
@Injectable()
|
||||
export class SpaceProductAllocationService {
|
||||
constructor(
|
||||
private readonly tagService: NewTagService,
|
||||
private readonly spaceProductAllocationRepository: SpaceProductAllocationRepository,
|
||||
private readonly spaceService: ValidationService,
|
||||
) {}
|
||||
|
||||
async createSpaceProductAllocations(
|
||||
@ -220,6 +224,114 @@ export class SpaceProductAllocationService {
|
||||
}
|
||||
}
|
||||
|
||||
async propagateDeleteToInheritedAllocations(
|
||||
queryRunner: QueryRunner,
|
||||
allocationsToUpdate: SpaceModelProductAllocationEntity[],
|
||||
tagUuidsToDelete: string[],
|
||||
project: ProjectEntity,
|
||||
spaces?: SpaceEntity[],
|
||||
): Promise<void> {
|
||||
try {
|
||||
const inheritedAllocationUpdates: SpaceProductAllocationEntity[] = [];
|
||||
const inheritedAllocationsToDelete: SpaceProductAllocationEntity[] = [];
|
||||
|
||||
for (const allocation of allocationsToUpdate) {
|
||||
for (const inheritedAllocation of allocation.inheritedSpaceAllocations) {
|
||||
const updatedInheritedTags = inheritedAllocation.tags.filter(
|
||||
(tag) => !tagUuidsToDelete.includes(tag.uuid),
|
||||
);
|
||||
|
||||
if (updatedInheritedTags.length === inheritedAllocation.tags.length) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (updatedInheritedTags.length === 0) {
|
||||
inheritedAllocationsToDelete.push(inheritedAllocation);
|
||||
} else {
|
||||
inheritedAllocation.tags = updatedInheritedTags;
|
||||
inheritedAllocationUpdates.push(inheritedAllocation);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (inheritedAllocationUpdates.length > 0) {
|
||||
await queryRunner.manager.save(
|
||||
SpaceProductAllocationEntity,
|
||||
inheritedAllocationUpdates,
|
||||
);
|
||||
}
|
||||
|
||||
if (inheritedAllocationsToDelete.length > 0) {
|
||||
await queryRunner.manager.remove(
|
||||
SpaceProductAllocationEntity,
|
||||
inheritedAllocationsToDelete,
|
||||
);
|
||||
}
|
||||
|
||||
if (spaces && spaces.length > 0) {
|
||||
await this.moveDevicesToOrphanSpace(
|
||||
queryRunner,
|
||||
spaces,
|
||||
tagUuidsToDelete,
|
||||
project,
|
||||
);
|
||||
}
|
||||
|
||||
await queryRunner.manager
|
||||
.createQueryBuilder()
|
||||
.delete()
|
||||
.from('space_product_tags')
|
||||
.where(
|
||||
'space_product_allocation_uuid NOT IN (' +
|
||||
queryRunner.manager
|
||||
.createQueryBuilder()
|
||||
.select('allocation.uuid')
|
||||
.from(SpaceProductAllocationEntity, 'allocation')
|
||||
.getQuery() +
|
||||
')',
|
||||
)
|
||||
.execute();
|
||||
} catch (error) {
|
||||
throw this.handleError(
|
||||
error,
|
||||
`Failed to propagate tag deletion to inherited allocations`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async moveDevicesToOrphanSpace(
|
||||
queryRunner: QueryRunner,
|
||||
spaces: SpaceEntity[],
|
||||
tagUuidsToDelete: string[],
|
||||
project: ProjectEntity,
|
||||
): Promise<void> {
|
||||
try {
|
||||
const orphanSpace = await this.spaceService.getOrphanSpace(project);
|
||||
|
||||
const devicesToMove = await queryRunner.manager
|
||||
.createQueryBuilder(DeviceEntity, 'device')
|
||||
.leftJoinAndSelect('device.tag', 'tag')
|
||||
.where('device.spaceDevice IN (:...spaceUuids)', {
|
||||
spaceUuids: spaces.map((space) => space.uuid),
|
||||
})
|
||||
.andWhere('tag.uuid IN (:...tagUuidsToDelete)', { tagUuidsToDelete })
|
||||
.getMany();
|
||||
|
||||
if (devicesToMove.length === 0) return;
|
||||
|
||||
await queryRunner.manager
|
||||
.createQueryBuilder()
|
||||
.update(DeviceEntity)
|
||||
.set({ spaceDevice: orphanSpace })
|
||||
.where('uuid IN (:...deviceUuids)', {
|
||||
deviceUuids: devicesToMove.map((device) => device.uuid),
|
||||
})
|
||||
.execute();
|
||||
} catch (error) {
|
||||
throw this.handleError(error, `Failed to move devices to orphan space`);
|
||||
}
|
||||
}
|
||||
|
||||
private async processDeleteActions(
|
||||
dtos: ModifyTagDto[],
|
||||
queryRunner: QueryRunner,
|
||||
|
@ -19,6 +19,11 @@ import { ValidateSpacesDto } from '../dtos/validation.space.dto';
|
||||
import { ProjectParam } from '../dtos';
|
||||
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
|
||||
import { In } from 'typeorm';
|
||||
import {
|
||||
ORPHAN_COMMUNITY_NAME,
|
||||
ORPHAN_SPACE_NAME,
|
||||
} from '@app/common/constants/orphan-constant';
|
||||
import { ProjectEntity } from '@app/common/modules/project/entities';
|
||||
|
||||
@Injectable()
|
||||
export class ValidationService {
|
||||
@ -286,4 +291,24 @@ export class ValidationService {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async getOrphanSpace(project: ProjectEntity): Promise<SpaceEntity> {
|
||||
const orphanSpace = await this.spaceRepository.findOne({
|
||||
where: {
|
||||
community: {
|
||||
name: `${ORPHAN_COMMUNITY_NAME}-${project.name}`,
|
||||
},
|
||||
spaceName: ORPHAN_SPACE_NAME,
|
||||
},
|
||||
});
|
||||
|
||||
if (!orphanSpace) {
|
||||
throw new HttpException(
|
||||
`Orphan space not found in community ${project.name}`,
|
||||
HttpStatus.NOT_FOUND,
|
||||
);
|
||||
}
|
||||
|
||||
return orphanSpace;
|
||||
}
|
||||
}
|
||||
|
@ -38,6 +38,9 @@ import { ProcessTagDto } from 'src/tags/dtos';
|
||||
import { SpaceProductAllocationService } from './space-product-allocation.service';
|
||||
import { SubspaceProductAllocationService } from './subspace/subspace-product-allocation.service';
|
||||
import { SubspaceEntity } from '@app/common/modules/space/entities/subspace/subspace.entity';
|
||||
import { ProjectEntity } from '@app/common/modules/project/entities';
|
||||
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
||||
import { DeviceService } from 'src/device/services';
|
||||
@Injectable()
|
||||
export class SpaceService {
|
||||
constructor(
|
||||
@ -53,6 +56,8 @@ export class SpaceService {
|
||||
private commandBus: CommandBus,
|
||||
private readonly spaceProductAllocationService: SpaceProductAllocationService,
|
||||
private readonly subspaceProductAllocationService: SubspaceProductAllocationService,
|
||||
private readonly deviceRepository: DeviceRepository,
|
||||
private readonly deviceSevice: DeviceService,
|
||||
) {}
|
||||
|
||||
async createSpace(
|
||||
@ -186,40 +191,6 @@ export class SpaceService {
|
||||
}
|
||||
}
|
||||
}
|
||||
async createFromModel(
|
||||
spaceModelUuid: string,
|
||||
queryRunner: QueryRunner,
|
||||
space: SpaceEntity,
|
||||
) {
|
||||
try {
|
||||
const spaceModel =
|
||||
await this.spaceModelService.validateSpaceModel(spaceModelUuid);
|
||||
|
||||
space.spaceModel = spaceModel;
|
||||
await queryRunner.manager.save(SpaceEntity, space);
|
||||
|
||||
await this.subSpaceService.createSubSpaceFromModel(
|
||||
spaceModel.subspaceModels,
|
||||
space,
|
||||
queryRunner,
|
||||
);
|
||||
|
||||
await this.tagService.createTagsFromModel(
|
||||
queryRunner,
|
||||
spaceModel.tags,
|
||||
space,
|
||||
null,
|
||||
);
|
||||
} catch (error) {
|
||||
if (error instanceof HttpException) {
|
||||
throw error;
|
||||
}
|
||||
throw new HttpException(
|
||||
'An error occurred while creating the space from space model',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async getSpacesHierarchyForCommunity(
|
||||
params: CommunitySpaceParam,
|
||||
|
@ -70,29 +70,23 @@ export class SubSpaceService {
|
||||
|
||||
async createSubSpaceFromModel(
|
||||
subspaceModels: SubspaceModelEntity[],
|
||||
space: SpaceEntity,
|
||||
spaces: SpaceEntity[],
|
||||
queryRunner: QueryRunner,
|
||||
): Promise<void> {
|
||||
if (!subspaceModels?.length) return;
|
||||
if (!subspaceModels?.length || !spaces?.length) return;
|
||||
|
||||
const subspaceData = subspaceModels.map((subSpaceModel) => ({
|
||||
const subspaceData = [];
|
||||
|
||||
for (const space of spaces) {
|
||||
for (const subSpaceModel of subspaceModels) {
|
||||
subspaceData.push({
|
||||
subspaceName: subSpaceModel.subspaceName,
|
||||
space,
|
||||
subSpaceModel,
|
||||
}));
|
||||
|
||||
const subspaces = await this.createSubspaces(subspaceData, queryRunner);
|
||||
|
||||
await Promise.all(
|
||||
subspaceModels.map((model, index) =>
|
||||
this.tagService.createTagsFromModel(
|
||||
queryRunner,
|
||||
model.tags || [],
|
||||
null,
|
||||
subspaces[index],
|
||||
),
|
||||
),
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
await this.createSubspaces(subspaceData, queryRunner);
|
||||
}
|
||||
|
||||
async createSubspacesFromDto(
|
||||
|
@ -118,6 +118,7 @@ export const CommandHandlers = [DisableSpaceHandler];
|
||||
UserRepository,
|
||||
SpaceUserService,
|
||||
SpaceSceneService,
|
||||
DeviceService,
|
||||
SceneService,
|
||||
SceneIconRepository,
|
||||
SceneRepository,
|
||||
|
Reference in New Issue
Block a user