diff --git a/src/space-model/services/space-model-product-allocation.service.ts b/src/space-model/services/space-model-product-allocation.service.ts index 6a477bf..9e95a02 100644 --- a/src/space-model/services/space-model-product-allocation.service.ts +++ b/src/space-model/services/space-model-product-allocation.service.ts @@ -14,7 +14,6 @@ import { ModifyAction } from '@app/common/constants/modify-action.enum'; import { NewTagEntity } from '@app/common/modules/tag'; import { ProductEntity } from '@app/common/modules/product/entities'; import { SpaceRepository } from '@app/common/modules/space'; -import { SpaceEntity } from '@app/common/modules/space/entities/space.entity'; import { ProjectEntity } from '@app/common/modules/project/entities'; @Injectable() @@ -31,10 +30,11 @@ export class SpaceModelProductAllocationService { tags: ProcessTagDto[], queryRunner?: QueryRunner, modifySubspaceModels?: ModifySubspaceModelDto[], - spaces?: SpaceEntity[], ): Promise { try { - if (!tags.length) return []; + if (!tags.length) { + return []; + } const processedTags = await this.tagService.processTags( tags, @@ -81,6 +81,7 @@ export class SpaceModelProductAllocationService { ) ) { isTagNeeded = true; + break; } } @@ -93,7 +94,9 @@ export class SpaceModelProductAllocationService { spaceModel, ); - if (hasTags) continue; + if (hasTags) { + continue; + } let allocation = existingAllocations.get(tag.product.uuid); if (!allocation) { @@ -120,9 +123,11 @@ export class SpaceModelProductAllocationService { if (productAllocations.length > 0) { await this.saveAllocations(productAllocations, queryRunner); } - return productAllocations; } catch (error) { + console.error( + `[ERROR] Failed to create product allocations: ${error.message}`, + ); throw this.handleError(error, 'Failed to create product allocations'); } } @@ -133,7 +138,6 @@ export class SpaceModelProductAllocationService { spaceModel: SpaceModelEntity, queryRunner: QueryRunner, modifySubspaceModels?: ModifySubspaceModelDto[], - spaces?: SpaceEntity[], ): Promise { try { const addDtos = dtos.filter((dto) => dto.action === ModifyAction.ADD); @@ -184,15 +188,8 @@ export class SpaceModelProductAllocationService { spaceModel, queryRunner, modifySubspaceModels, - spaces, - ), - this.processDeleteActions( - filteredDtos, - queryRunner, - spaceModel, - project, - spaces, ), + this.processDeleteActions(filteredDtos, queryRunner, spaceModel), ]); } catch (error) { throw this.handleError(error, 'Error while updating product allocations'); @@ -205,7 +202,6 @@ export class SpaceModelProductAllocationService { spaceModel: SpaceModelEntity, queryRunner: QueryRunner, modifySubspaceModels?: ModifySubspaceModelDto[], - spaces?: SpaceEntity[], ): Promise { const addDtos: ProcessTagDto[] = dtos .filter((dto) => dto.action === ModifyAction.ADD) @@ -222,7 +218,6 @@ export class SpaceModelProductAllocationService { addDtos, queryRunner, modifySubspaceModels, - spaces, ); } } @@ -298,8 +293,6 @@ export class SpaceModelProductAllocationService { dtos: ModifyTagModelDto[], queryRunner: QueryRunner, spaceModel: SpaceModelEntity, - project: ProjectEntity, - spaces?: SpaceEntity[], ): Promise { try { if (!dtos || dtos.length === 0) { @@ -409,7 +402,7 @@ export class SpaceModelProductAllocationService { (allocation) => allocation.tags, ); - // Check if the tag is already assigned to the same product in this subspace + // Check if the tag is already assigned to the same product in this space const isDuplicateTag = existingTagsForProduct.some( (existingTag) => existingTag.uuid === tag.uuid, ); diff --git a/src/space-model/services/space-model.service.ts b/src/space-model/services/space-model.service.ts index 6efe5ff..e15c4aa 100644 --- a/src/space-model/services/space-model.service.ts +++ b/src/space-model/services/space-model.service.ts @@ -481,7 +481,6 @@ export class SpaceModelService { await this.subspaceRepository.save(subspace); } - const subspaceAllocations = subspaceModel.productAllocations.map( (modelAllocation) => this.subspaceProductAllocationRepository.create({ diff --git a/src/space-model/services/subspace/subspace-model.service.ts b/src/space-model/services/subspace/subspace-model.service.ts index b30ee69..82905ee 100644 --- a/src/space-model/services/subspace/subspace-model.service.ts +++ b/src/space-model/services/subspace/subspace-model.service.ts @@ -157,6 +157,7 @@ export class SubSpaceModelService { deleteDtos, queryRunner, spaceModel, + spaceTagUpdateDtos, ); return [ createdSubspaces ?? [], @@ -182,6 +183,7 @@ export class SubSpaceModelService { deleteDtos: ModifySubspaceModelDto[], queryRunner: QueryRunner, spaceModel: SpaceModelEntity, + spaceTagUpdateDtos?: ModifyTagModelDto[], ): Promise { try { if (!deleteDtos || deleteDtos.length === 0) { @@ -208,12 +210,14 @@ export class SubSpaceModelService { { disabled: true }, ); - const allocationsToRemove = subspaces.flatMap( - (subspace) => subspace.productAllocations, + const allocationsToRemove = subspaces.flatMap((subspace) => + (subspace.productAllocations || []).map((allocation) => ({ + ...allocation, + subspaceModel: subspace, + })), ); const relocatedAllocationsMap = new Map(); - if (allocationsToRemove.length > 0) { const spaceAllocationsMap = new Map< string, @@ -224,7 +228,6 @@ export class SubSpaceModelService { const product = allocation.product; const tags = allocation.tags; const subspaceUuid = allocation.subspaceModel.uuid; - const spaceAllocationKey = `${spaceModel.uuid}-${product.uuid}`; if (!spaceAllocationsMap.has(spaceAllocationKey)) { @@ -261,22 +264,43 @@ export class SubSpaceModelService { allocation: spaceAllocation, }); spaceAllocation.tags.push(...newTags); + await queryRunner.manager.save(spaceAllocation); } } else { - const newSpaceAllocation = queryRunner.manager.create( - SpaceModelProductAllocationEntity, - { - spaceModel: spaceModel, - product: product, + let tagsToAdd = [...tags]; + + if (spaceTagUpdateDtos && spaceTagUpdateDtos.length > 0) { + const spaceTagDtosToAdd = spaceTagUpdateDtos.filter( + (dto) => dto.action === ModifyAction.ADD, + ); + + tagsToAdd = tagsToAdd.filter( + (tag) => + !spaceTagDtosToAdd.some( + (addDto) => + (addDto.name && addDto.name === tag.name) || + (addDto.newTagUuid && addDto.newTagUuid === tag.uuid), + ), + ); + } + + if (tagsToAdd.length > 0) { + const newSpaceAllocation = queryRunner.manager.create( + SpaceModelProductAllocationEntity, + { + spaceModel: spaceModel, + product: product, + tags: tags, + }, + ); + + movedToAlreadyExistingSpaceAllocations.push({ + allocation: newSpaceAllocation, tags: tags, - }, - ); - movedToAlreadyExistingSpaceAllocations.push({ - allocation: newSpaceAllocation, - tags: tags, - }); - await queryRunner.manager.save(newSpaceAllocation); + }); + await queryRunner.manager.save(newSpaceAllocation); + } } if (movedToAlreadyExistingSpaceAllocations.length > 0) {