mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 19:24:53 +00:00
fixed unlinking space model on delete
This commit is contained in:
@ -4,7 +4,6 @@ import { Logger } from '@nestjs/common';
|
||||
import { PropogateDeleteSpaceModelCommand } from '../commands';
|
||||
import { SpaceRepository } from '@app/common/modules/space';
|
||||
import { SpaceService } from '../../space/services/space.service';
|
||||
import { DataSource } from 'typeorm';
|
||||
|
||||
@CommandHandler(PropogateDeleteSpaceModelCommand)
|
||||
export class PropogateDeleteSpaceModelHandler
|
||||
@ -15,7 +14,6 @@ export class PropogateDeleteSpaceModelHandler
|
||||
constructor(
|
||||
private readonly spaceRepository: SpaceRepository,
|
||||
private readonly spaceService: SpaceService,
|
||||
private readonly dataSource: DataSource,
|
||||
) {}
|
||||
|
||||
async execute(command: PropogateDeleteSpaceModelCommand): Promise<void> {
|
||||
@ -29,7 +27,11 @@ export class PropogateDeleteSpaceModelHandler
|
||||
uuid: spaceModel.uuid,
|
||||
},
|
||||
},
|
||||
relations: ['subspaces', 'tags', 'subspaces.tags'],
|
||||
relations: [
|
||||
'subspaces',
|
||||
'productAllocations',
|
||||
'subspaces.productAllocations',
|
||||
],
|
||||
});
|
||||
|
||||
for (const space of spaces) {
|
||||
|
||||
@ -197,6 +197,29 @@ export class SpaceProductAllocationService {
|
||||
throw this.handleError(error, 'Error while updating product allocations');
|
||||
}
|
||||
}
|
||||
|
||||
async unlinkModels(space: SpaceEntity, queryRunner: QueryRunner) {
|
||||
try {
|
||||
if (!space.productAllocations || space.productAllocations.length === 0)
|
||||
return;
|
||||
|
||||
const allocationUuids = space.productAllocations.map(
|
||||
(allocation) => allocation.uuid,
|
||||
);
|
||||
|
||||
await queryRunner.manager.update(
|
||||
SpaceProductAllocationEntity,
|
||||
{ uuid: In(allocationUuids) },
|
||||
{ inheritedFromModel: null },
|
||||
);
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
'Failed to unlink models',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private async processDeleteActions(
|
||||
dtos: ModifyTagDto[],
|
||||
queryRunner: QueryRunner,
|
||||
|
||||
@ -602,7 +602,10 @@ export class SpaceService {
|
||||
// Unlink subspaces and tags if they exist
|
||||
if (space.subspaces || space.tags) {
|
||||
if (space.tags) {
|
||||
await this.tagService.unlinkModels(space.tags, queryRunner);
|
||||
await this.spaceProductAllocationService.unlinkModels(
|
||||
space,
|
||||
queryRunner,
|
||||
);
|
||||
}
|
||||
|
||||
if (space.subspaces) {
|
||||
|
||||
@ -374,6 +374,29 @@ export class SubspaceProductAllocationService {
|
||||
throw this.handleError(error, `Failed to delete tags in subspace`);
|
||||
}
|
||||
}
|
||||
|
||||
async unlinkModels(
|
||||
allocations: SubspaceProductAllocationEntity[],
|
||||
queryRunner: QueryRunner,
|
||||
) {
|
||||
try {
|
||||
if (allocations.length === 0) return;
|
||||
|
||||
const allocationUuids = allocations.map((allocation) => allocation.uuid);
|
||||
|
||||
await queryRunner.manager.update(
|
||||
SubspaceProductAllocationEntity,
|
||||
{ uuid: In(allocationUuids) },
|
||||
{ inheritedFromModel: null },
|
||||
);
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
'Failed to unlink models',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private async validateTagWithinSubspace(
|
||||
queryRunner: QueryRunner | undefined,
|
||||
tag: NewTagEntity,
|
||||
|
||||
@ -27,6 +27,7 @@ import { SubspaceEntity } from '@app/common/modules/space/entities/subspace/subs
|
||||
import { ProcessTagDto } from 'src/tags/dtos';
|
||||
import { TagService as NewTagService } from 'src/tags/services/tags.service';
|
||||
import { SubspaceProductAllocationService } from './subspace-product-allocation.service';
|
||||
import { SubspaceProductAllocationEntity } from '@app/common/modules/space/entities/subspace/subspace-product-allocation.entity';
|
||||
|
||||
@Injectable()
|
||||
export class SubSpaceService {
|
||||
@ -377,22 +378,30 @@ export class SubSpaceService {
|
||||
subspaces: SubspaceEntity[],
|
||||
queryRunner: QueryRunner,
|
||||
): Promise<void> {
|
||||
if (!subspaces || subspaces.length === 0) {
|
||||
return;
|
||||
}
|
||||
if (!subspaces || subspaces.length === 0) return;
|
||||
|
||||
try {
|
||||
const allTags = subspaces.flatMap((subSpace) => {
|
||||
subSpace.subSpaceModel = null;
|
||||
return subSpace.tags || [];
|
||||
});
|
||||
const subspaceUuids = subspaces.map((subSpace) => subSpace.uuid);
|
||||
|
||||
await this.tagService.unlinkModels(allTags, queryRunner);
|
||||
const allocations: SubspaceProductAllocationEntity[] = subspaces.flatMap(
|
||||
(subspace) => subspace.productAllocations || [],
|
||||
);
|
||||
|
||||
await queryRunner.manager.save(subspaces);
|
||||
if (allocations.length > 0) {
|
||||
await this.subspaceProductAllocationService.unlinkModels(
|
||||
allocations,
|
||||
queryRunner,
|
||||
);
|
||||
}
|
||||
|
||||
await queryRunner.manager.update(
|
||||
SubspaceEntity,
|
||||
{ uuid: In(subspaceUuids) },
|
||||
{ subSpaceModel: null },
|
||||
);
|
||||
} catch (error) {
|
||||
if (error instanceof HttpException) throw error;
|
||||
throw new HttpException(
|
||||
`Failed to unlink subspace models: ${error.message}`,
|
||||
`Failed to unlink subspace models: ${error instanceof Error ? error.message : 'Unknown error'}`,
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user