diff --git a/src/space-model/handlers/propogate-space-model-deletion.handler.ts b/src/space-model/handlers/propogate-space-model-deletion.handler.ts index 31efeba..318635e 100644 --- a/src/space-model/handlers/propogate-space-model-deletion.handler.ts +++ b/src/space-model/handlers/propogate-space-model-deletion.handler.ts @@ -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 { @@ -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) { diff --git a/src/space/services/space-product-allocation.service.ts b/src/space/services/space-product-allocation.service.ts index 52ebdfd..8b8acb1 100644 --- a/src/space/services/space-product-allocation.service.ts +++ b/src/space/services/space-product-allocation.service.ts @@ -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, diff --git a/src/space/services/space.service.ts b/src/space/services/space.service.ts index 2be315c..844c42f 100644 --- a/src/space/services/space.service.ts +++ b/src/space/services/space.service.ts @@ -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) { diff --git a/src/space/services/subspace/subspace-product-allocation.service.ts b/src/space/services/subspace/subspace-product-allocation.service.ts index 162620d..e610c0a 100644 --- a/src/space/services/subspace/subspace-product-allocation.service.ts +++ b/src/space/services/subspace/subspace-product-allocation.service.ts @@ -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, diff --git a/src/space/services/subspace/subspace.service.ts b/src/space/services/subspace/subspace.service.ts index b4febbe..500ea69 100644 --- a/src/space/services/subspace/subspace.service.ts +++ b/src/space/services/subspace/subspace.service.ts @@ -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 { - 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, ); }