From 1f18f50923cc01a38c4454441d33bcb7a5762c40 Mon Sep 17 00:00:00 2001 From: hannathkadher Date: Thu, 6 Mar 2025 15:49:14 +0400 Subject: [PATCH] adding tag in space model will add tag to spaces --- .../space-model-product-allocation.service.ts | 21 ++++++ .../services/space-model.service.ts | 26 +++++++ .../space-product-allocation.service.ts | 70 ++++++++++++++++++- 3 files changed, 115 insertions(+), 2 deletions(-) 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 19f2c5a..2828ecb 100644 --- a/src/space-model/services/space-model-product-allocation.service.ts +++ b/src/space-model/services/space-model-product-allocation.service.ts @@ -13,12 +13,17 @@ import { ModifySubspaceModelDto, ModifyTagModelDto } from '../dtos'; 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 { SpaceProductAllocationService } from 'src/space/services/space-product-allocation.service'; +import { SpaceEntity } from '@app/common/modules/space/entities/space.entity'; @Injectable() export class SpaceModelProductAllocationService { constructor( private readonly tagService: NewTagService, private readonly spaceModelProductAllocationRepository: SpaceModelProductAllocationRepoitory, + private readonly spaceRepository: SpaceRepository, + private readonly spaceProductAllocationService: SpaceProductAllocationService, ) {} async createProductAllocations( @@ -27,6 +32,7 @@ export class SpaceModelProductAllocationService { tags: ProcessTagDto[], queryRunner?: QueryRunner, modifySubspaceModels?: ModifySubspaceModelDto[], + spaces?: SpaceEntity[], ): Promise { try { if (!tags.length) return []; @@ -102,12 +108,23 @@ export class SpaceModelProductAllocationService { } else if (!allocation.tags.some((t) => t.uuid === tag.uuid)) { allocation.tags.push(tag); await this.saveAllocation(allocation, queryRunner); + await this.spaceProductAllocationService.addTagToAllocationFromModel( + allocation, + queryRunner, + tag, + spaces, + ); } } } if (productAllocations.length > 0) { await this.saveAllocations(productAllocations, queryRunner); + await this.spaceProductAllocationService.createAllocationFromModel( + productAllocations, + queryRunner, + spaces, + ); } return productAllocations; @@ -122,6 +139,7 @@ export class SpaceModelProductAllocationService { spaceModel: SpaceModelEntity, queryRunner: QueryRunner, modifySubspaceModels?: ModifySubspaceModelDto[], + spaces?: SpaceEntity[], ): Promise { try { const addDtos = dtos.filter((dto) => dto.action === ModifyAction.ADD); @@ -172,6 +190,7 @@ export class SpaceModelProductAllocationService { spaceModel, queryRunner, modifySubspaceModels, + spaces, ), this.processDeleteActions(filteredDtos, queryRunner), ]); @@ -186,6 +205,7 @@ export class SpaceModelProductAllocationService { spaceModel: SpaceModelEntity, queryRunner: QueryRunner, modifySubspaceModels?: ModifySubspaceModelDto[], + spaces?: SpaceEntity[], ): Promise { const addDtos: ProcessTagDto[] = dtos .filter((dto) => dto.action === ModifyAction.ADD) @@ -202,6 +222,7 @@ export class SpaceModelProductAllocationService { addDtos, queryRunner, modifySubspaceModels, + spaces, ); } } diff --git a/src/space-model/services/space-model.service.ts b/src/space-model/services/space-model.service.ts index 47a905c..94d215e 100644 --- a/src/space-model/services/space-model.service.ts +++ b/src/space-model/services/space-model.service.ts @@ -198,6 +198,7 @@ export class SpaceModelService { try { await queryRunner.startTransaction(); + const spaces = await this.fetchModelSpaces(spaceModel); const { modelName } = dto; if (modelName) { @@ -230,6 +231,7 @@ export class SpaceModelService { spaceModel, queryRunner, dto.subspaceModels, + spaces, ); } await queryRunner.commitTransaction(); @@ -318,6 +320,30 @@ export class SpaceModelService { } } + async fetchModelSpaces( + spaceModel: SpaceModelEntity, + queryRunner?: QueryRunner, + ) { + const spaces = await (queryRunner + ? queryRunner.manager.find(SpaceEntity, { + where: { + spaceModel: { + uuid: spaceModel.uuid, + }, + disabled: false, + }, + }) + : this.spaceRepository.find({ + where: { + spaceModel: { + uuid: spaceModel.uuid, + }, + disabled: false, + }, + })); + return spaces; + } + async linkSpaceModel( params: SpaceModelParam, dto: LinkSpacesToModelDto, diff --git a/src/space/services/space-product-allocation.service.ts b/src/space/services/space-product-allocation.service.ts index eb48b4f..94afd62 100644 --- a/src/space/services/space-product-allocation.service.ts +++ b/src/space/services/space-product-allocation.service.ts @@ -12,6 +12,7 @@ import { ModifyTagDto } from '../dtos/tag/modify-tag.dto'; 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'; @Injectable() export class SpaceProductAllocationService { @@ -108,6 +109,71 @@ export class SpaceProductAllocationService { ); } } + + async createAllocationFromModel( + modelAllocations: SpaceModelProductAllocationEntity[], + queryRunner: QueryRunner, + spaces?: SpaceEntity[], + ) { + if (!spaces || spaces.length === 0 || !modelAllocations.length) return; + + const allocations: SpaceProductAllocationEntity[] = []; + + for (const space of spaces) { + for (const modelAllocation of modelAllocations) { + const allocation = queryRunner.manager.create( + SpaceProductAllocationEntity, + { + space, + product: modelAllocation.product, + tags: modelAllocation.tags, + inheritedFromModel: modelAllocation, + }, + ); + allocations.push(allocation); + } + } + + if (allocations.length > 0) { + await queryRunner.manager.save(SpaceProductAllocationEntity, allocations); + } + } + + async addTagToAllocationFromModel( + modelAllocation: SpaceModelProductAllocationEntity, + queryRunner: QueryRunner, + tag: NewTagEntity, + spaces?: SpaceEntity[], + ) { + try { + if (!spaces || spaces.length === 0 || !modelAllocation) return; + + const spaceAllocations = await queryRunner.manager.find( + SpaceProductAllocationEntity, + { + where: { inheritedFromModel: { uuid: modelAllocation.uuid } }, + relations: ['tags'], + }, + ); + + if (spaceAllocations.length === 0) return; + + for (const allocation of spaceAllocations) { + allocation.tags.push(tag); + } + + await queryRunner.manager.save( + SpaceProductAllocationEntity, + spaceAllocations, + ); + } catch (error) { + throw new HttpException( + 'Failed to add tag to allocation from model', + HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + } + async updateSpaceProductAllocations( dtos: ModifyTagDto[], projectUuid: string, @@ -285,7 +351,7 @@ export class SpaceProductAllocationService { }); } - private createNewAllocation( + createNewAllocation( space: SpaceEntity, tag: NewTagEntity, queryRunner?: QueryRunner, @@ -312,7 +378,7 @@ export class SpaceProductAllocationService { : await this.spaceProductAllocationRepository.save(allocation); } - private async saveAllocations( + async saveAllocations( allocations: SpaceProductAllocationEntity[], queryRunner?: QueryRunner, ) {