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