From d059171b724a5015b8bfc07df06f90d43d3d65c5 Mon Sep 17 00:00:00 2001 From: hannathkadher Date: Tue, 4 Mar 2025 00:44:22 +0400 Subject: [PATCH 1/3] fixed issues in create space model --- ...ubspace-model-product-allocation.entity.ts | 5 +- .../space-model-product-allocation.service.ts | 22 ++-- .../services/space-model.service.ts | 8 +- ...bspace-model-product-allocation.service.ts | 16 +-- src/space/services/tag/tag.service.ts | 6 +- src/tags/services/tags.service.ts | 115 +++++++++++------- 6 files changed, 102 insertions(+), 70 deletions(-) diff --git a/libs/common/src/modules/space-model/entities/subspace-model/subspace-model-product-allocation.entity.ts b/libs/common/src/modules/space-model/entities/subspace-model/subspace-model-product-allocation.entity.ts index 902bb60..f955723 100644 --- a/libs/common/src/modules/space-model/entities/subspace-model/subspace-model-product-allocation.entity.ts +++ b/libs/common/src/modules/space-model/entities/subspace-model/subspace-model-product-allocation.entity.ts @@ -27,7 +27,10 @@ export class SubspaceModelProductAllocationEntity extends AbstractEntity ProductEntity, { nullable: false, onDelete: 'CASCADE' }) public product: ProductEntity; - @ManyToMany(() => NewTagEntity, (tag) => tag.subspaceModelAllocations) + @ManyToMany(() => NewTagEntity, (tag) => tag.subspaceModelAllocations, { + cascade: true, + onDelete: 'CASCADE', + }) @JoinTable({ name: 'subspace_model_product_tags' }) public tags: NewTagEntity[]; 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 3d20b28..d45c1a7 100644 --- a/src/space-model/services/space-model-product-allocation.service.ts +++ b/src/space-model/services/space-model-product-allocation.service.ts @@ -28,6 +28,7 @@ export class SpaceModelProductAllocationService { queryRunner?: QueryRunner, modifySubspaceModels?: ModifySubspaceModelDto[], ): Promise { + try { if (!tags.length) return []; @@ -37,6 +38,8 @@ export class SpaceModelProductAllocationService { queryRunner, ); + + const productAllocations: SpaceModelProductAllocationEntity[] = []; const existingAllocations = new Map< string, @@ -344,26 +347,17 @@ export class SpaceModelProductAllocationService { async clearAllAllocations(spaceModelUuid: string, queryRunner: QueryRunner) { try { - await queryRunner.manager - .createQueryBuilder() - .delete() - .from(SpaceModelProductAllocationEntity, 'allocation') - .relation(SpaceModelProductAllocationEntity, 'tags') - .of( - await queryRunner.manager.find(SpaceModelProductAllocationEntity, { - where: { spaceModel: { uuid: spaceModelUuid } }, - }), - ) - .execute(); - await queryRunner.manager .createQueryBuilder() .delete() .from(SpaceModelProductAllocationEntity) - .where('spaceModelUuid = :spaceModelUuid', { spaceModelUuid }) + .where('space_model_uuid = :spaceModelUuid', { spaceModelUuid }) .execute(); } catch (error) { - throw this.handleError(error, `Failed to clear all allocations`); + throw this.handleError( + error, + `Failed to clear all allocations in the space model product allocation`, + ); } } } diff --git a/src/space-model/services/space-model.service.ts b/src/space-model/services/space-model.service.ts index f60cb7e..a0e1532 100644 --- a/src/space-model/services/space-model.service.ts +++ b/src/space-model/services/space-model.service.ts @@ -593,6 +593,12 @@ export class SpaceModelService { } tagUuidSet.add(tag.uuid); } else { + if (!tag.name || !tag.productUuid) { + throw new HttpException( + `Tag name and product should not be null.`, + HttpStatus.BAD_REQUEST, + ); + } const tagKey = `${tag.name}-${tag.productUuid}`; if (tagNameProductSet.has(tagKey)) { throw new HttpException( @@ -668,7 +674,7 @@ export class SpaceModelService { uuid: tag.uuid, createdAt: tag.createdAt, updatedAt: tag.updatedAt, - tag: tag.tag, + name: tag.name, disabled: tag.disabled, product: tag.product ? { diff --git a/src/space-model/services/subspace/subspace-model-product-allocation.service.ts b/src/space-model/services/subspace/subspace-model-product-allocation.service.ts index 20eb1dc..ef69f41 100644 --- a/src/space-model/services/subspace/subspace-model-product-allocation.service.ts +++ b/src/space-model/services/subspace/subspace-model-product-allocation.service.ts @@ -493,16 +493,16 @@ export class SubspaceModelProductAllocationService { await queryRunner.manager .createQueryBuilder() .delete() - .from('subspace_model_product_tags') // Replace with entity name if you have one - .where( - '"subspace_model_product_allocation_uuid" IN (:...subspaceIds)', - { - subspaceIds, - }, - ) + .from(SubspaceModelProductAllocationEntity) + .where('"subspace_model_uuid" IN (:...subspaceIds)', { + subspaceIds, + }) .execute(); } catch (error) { - throw this.handleError(error, `Failed to clear all allocations`); + throw this.handleError( + error, + `Failed to clear all allocations subspace model product allocation`, + ); } } diff --git a/src/space/services/tag/tag.service.ts b/src/space/services/tag/tag.service.ts index 323e770..f40785b 100644 --- a/src/space/services/tag/tag.service.ts +++ b/src/space/services/tag/tag.service.ts @@ -135,15 +135,11 @@ export class TagService { return tag; } catch (error) { - console.error( - `Error moving tag with UUID ${tagDto.uuid}: ${error.message}`, - ); - throw error; // Re-throw the error to propagate it to the parent Promise.all + throw error; } }), ); } catch (error) { - console.error(`Error in moveTags: ${error.message}`); throw new HttpException( `Failed to move tags due to an unexpected error: ${error.message}`, HttpStatus.INTERNAL_SERVER_ERROR, diff --git a/src/tags/services/tags.service.ts b/src/tags/services/tags.service.ts index 964e98c..f1fe09d 100644 --- a/src/tags/services/tags.service.ts +++ b/src/tags/services/tags.service.ts @@ -72,33 +72,43 @@ export class TagService { queryRunner?: QueryRunner, ): Promise { try { - if (!tags || tags.length === 0) return []; + if (!tags || tags.length === 0) { + return []; + } const newTags: CreateTagDto[] = []; const existingTagUuids: string[] = []; + // Separate new tags and existing tag UUIDs for (const tag of tags) { - tag.uuid ? existingTagUuids.push(tag.uuid) : newTags.push(tag); + if (tag.uuid) { + existingTagUuids.push(tag.uuid); + } else { + newTags.push(tag); + } } - const existingTags = existingTagUuids.length - ? await (queryRunner - ? queryRunner.manager.find(NewTagEntity, { - where: { - uuid: In(existingTagUuids), - project: { uuid: projectUuid }, - }, - relations: ['product'], - }) - : this.tagRepository.find({ - where: { - uuid: In(existingTagUuids), - project: { uuid: projectUuid }, - }, - relations: ['product'], - })) - : []; + let existingTags: NewTagEntity[] = []; + if (existingTagUuids.length > 0) { + existingTags = await (queryRunner + ? queryRunner.manager.find(NewTagEntity, { + where: { + uuid: In(existingTagUuids), + project: { uuid: projectUuid }, + }, + relations: ['product'], + }) + : this.tagRepository.find({ + where: { + uuid: In(existingTagUuids), + project: { uuid: projectUuid }, + }, + relations: ['product'], + })); + } + + // Check for missing UUIDs if (existingTags.length !== existingTagUuids.length) { const foundUuids = new Set(existingTags.map((tag) => tag.uuid)); const missingUuids = existingTagUuids.filter( @@ -111,15 +121,18 @@ export class TagService { ); } - const createdTags = - newTags.length > 0 - ? await this.bulkCreateTags( - { projectUuid, tags: newTags }, - queryRunner, - ) - : []; + let createdTags: NewTagEntity[] = []; - return [...existingTags, ...createdTags]; + if (newTags.length > 0) { + createdTags = await this.bulkCreateTags( + { projectUuid, tags: newTags }, + queryRunner, + ); + } + + const allTags = [...existingTags, ...createdTags]; + + return allTags; } catch (error) { throw new HttpException( error instanceof HttpException @@ -138,28 +151,49 @@ export class TagService { ): Promise { try { const { projectUuid, tags } = dto; + const newTags: NewTagEntity[] = []; const project = await this.getProjectByUuid(projectUuid); + // Extract unique product UUIDs const productUuids = Array.from( new Set(tags.map((tag) => tag.productUuid)), ); const productMap = await this.getProductMap(productUuids); - const existingTagNames = new Set( - await this.getExistingTagNames(projectUuid), - ); + // Fetch existing tag names for this project + const existingTags = await this.tagRepository.find({ + where: { project: { uuid: projectUuid } }, + relations: ['product'], + }); - const newTags: NewTagEntity[] = tags - .filter((tag) => !existingTagNames.has(tag.name)) - .map((tag) => - this.tagRepository.create({ - name: tag.name, - product: productMap.get(tag.productUuid), - project, - }), - ); + // Convert existing tags into a Map for quick lookup + const existingTagMap = new Map(); + existingTags.forEach((tag) => { + existingTagMap.set(tag.name, tag.product?.uuid || null); + }); + + for (const tag of tags) { + const existingProductUuid = existingTagMap.get(tag.name); + + if (existingProductUuid) { + if (existingProductUuid !== tag.productUuid) { + throw new HttpException( + `Tag "${tag.name}" already exists but is associated with a different product.`, + HttpStatus.CONFLICT, + ); + } + } else { + newTags.push( + this.tagRepository.create({ + name: tag.name, + product: productMap.get(tag.productUuid), + project, + }), + ); + } + } if (newTags.length > 0) { if (queryRunner) { @@ -167,12 +201,11 @@ export class TagService { } else { await this.tagRepository.save(newTags); } + } else { } return newTags; } catch (error) { - console.log('error1', error); - throw new HttpException( error instanceof HttpException ? error.message From 34f4b61ae0a5742b06c9f0488f98e231ca1d10e9 Mon Sep 17 00:00:00 2001 From: hannathkadher Date: Tue, 4 Mar 2025 00:45:42 +0400 Subject: [PATCH 2/3] changed description of controller --- src/space-model/controllers/space-model.controller.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/space-model/controllers/space-model.controller.ts b/src/space-model/controllers/space-model.controller.ts index 8ac607c..6f23c3e 100644 --- a/src/space-model/controllers/space-model.controller.ts +++ b/src/space-model/controllers/space-model.controller.ts @@ -113,9 +113,9 @@ export class SpaceModelController { @UseGuards(PermissionsGuard) @Permissions('SPACE_MODEL_LINK') @ApiOperation({ - summary: ControllerRoute.SPACE_MODEL.ACTIONS.DELETE_SPACE_MODEL_SUMMARY, + summary: ControllerRoute.SPACE_MODEL.ACTIONS.LINK_SPACE_MODEL_SUMMARY, description: - ControllerRoute.SPACE_MODEL.ACTIONS.DELETE_SPACE_MODEL_DESCRIPTION, + ControllerRoute.SPACE_MODEL.ACTIONS.LINK_SPACE_MODEL_DESCRIPTION, }) @Post(':spaceModelUuid/spaces/link') async link( From 553cdb28bf9c29d1cc777b2fc5ac7f3ef37ae25b Mon Sep 17 00:00:00 2001 From: hannathkadher Date: Tue, 4 Mar 2025 00:45:53 +0400 Subject: [PATCH 3/3] prettify --- .../services/space-model-product-allocation.service.ts | 3 --- 1 file changed, 3 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 d45c1a7..6e56c3d 100644 --- a/src/space-model/services/space-model-product-allocation.service.ts +++ b/src/space-model/services/space-model-product-allocation.service.ts @@ -28,7 +28,6 @@ export class SpaceModelProductAllocationService { queryRunner?: QueryRunner, modifySubspaceModels?: ModifySubspaceModelDto[], ): Promise { - try { if (!tags.length) return []; @@ -38,8 +37,6 @@ export class SpaceModelProductAllocationService { queryRunner, ); - - const productAllocations: SpaceModelProductAllocationEntity[] = []; const existingAllocations = new Map< string,