diff --git a/src/space-model/services/space-model.service.ts b/src/space-model/services/space-model.service.ts index 723267e..245fffa 100644 --- a/src/space-model/services/space-model.service.ts +++ b/src/space-model/services/space-model.service.ts @@ -117,6 +117,12 @@ export class SpaceModelService { pageable.where = { project: { uuid: param.projectUuid }, disabled: false, + subspaceModels: { + disabled: false, + }, + tags: { + disabled: false, + }, }; pageable.include = 'subspaceModels,tags,subspaceModels.tags,tags.product,subspaceModels.tags.product'; @@ -201,7 +207,6 @@ export class SpaceModelService { return new SuccessResponseDto({ message: 'SpaceModel updated successfully', - data: spaceModel, }); } catch (error) { await queryRunner.rollbackTransaction(); diff --git a/src/space-model/services/subspace/subspace-model.service.ts b/src/space-model/services/subspace/subspace-model.service.ts index 9a52565..9b1c5d2 100644 --- a/src/space-model/services/subspace/subspace-model.service.ts +++ b/src/space-model/services/subspace/subspace-model.service.ts @@ -5,7 +5,7 @@ import { } from '@app/common/modules/space-model'; import { HttpException, HttpStatus, Injectable } from '@nestjs/common'; import { CreateSubspaceModelDto, CreateTagModelDto } from '../../dtos'; -import { In, QueryRunner } from 'typeorm'; +import { Not, QueryRunner } from 'typeorm'; import { IDeletedSubsaceModelInterface, ModifySubspaceModelPayload, @@ -123,6 +123,7 @@ export class SubSpaceModelService { }; try { for (const subspace of subspaceDtos) { + console.log(subspace.action); switch (subspace.action) { case ModifyAction.ADD: const subspaceModel = await this.handleAddAction( @@ -258,7 +259,7 @@ export class SubSpaceModelService { private async findOne(subspaceUuid: string): Promise { const subspace = await this.subspaceModelRepository.findOne({ - where: { uuid: subspaceUuid }, + where: { uuid: subspaceUuid, disabled: false }, relations: ['tags', 'spaceModel'], }); if (!subspace) { @@ -299,59 +300,52 @@ export class SubSpaceModelService { } } + private async checkDuplicateNames( + subspaceName: string, + spaceModelUuid: string, + excludeUuid?: string, + ): Promise { + const duplicateSubspace = await this.subspaceModelRepository.findOne({ + where: { + subspaceName, + spaceModel: { + uuid: spaceModelUuid, + }, + ...(excludeUuid && { uuid: Not(excludeUuid) }), + }, + }); + + if (duplicateSubspace) { + throw new HttpException( + `A subspace with the name '${subspaceName}' already exists within the same space model. ${spaceModelUuid}`, + HttpStatus.CONFLICT, + ); + } + } + private async validateName( names: string[], spaceModel: SpaceModelEntity, ): Promise { - try { - const seenNames = new Set(); - const duplicateNames = new Set(); + const seenNames = new Set(); + const duplicateNames = new Set(); - // Check for duplicate names within the input array - for (const name of names) { - if (!seenNames.add(name)) { - duplicateNames.add(name); - } + for (const name of names) { + if (!seenNames.add(name)) { + duplicateNames.add(name); } + } - if (duplicateNames.size > 0) { - throw new HttpException( - `Duplicate subspace model names found: ${[...duplicateNames].join(', ')}`, - HttpStatus.CONFLICT, - ); - } - - // Check for existing names in the database - const existingNames = await this.subspaceModelRepository.find({ - select: ['subspaceName'], - where: { - subspaceName: In([...seenNames]), - spaceModel: { - uuid: spaceModel.uuid, - }, - }, - }); - - if (existingNames.length > 0) { - const existingNamesList = existingNames - .map((e) => e.subspaceName) - .join(', '); - throw new HttpException( - `Subspace model names already exist in the space: ${existingNamesList}`, - HttpStatus.BAD_REQUEST, - ); - } - } catch (error) { - if (error instanceof HttpException) { - throw error; // Rethrow known HttpExceptions - } - - // Handle unexpected errors + if (duplicateNames.size > 0) { throw new HttpException( - `An error occurred while validating subspace model names: ${error.message}`, - HttpStatus.INTERNAL_SERVER_ERROR, + `Duplicate subspace model names found: ${[...duplicateNames].join(', ')}`, + HttpStatus.CONFLICT, ); } + + for (const name of names) { + await this.checkDuplicateNames(name, spaceModel.uuid); + } } private async updateSubspaceName( @@ -360,6 +354,12 @@ export class SubSpaceModelService { subspaceName?: string, ): Promise { if (subspaceName) { + await this.checkDuplicateNames( + subspaceName, + subSpaceModel.spaceModel.uuid, + subSpaceModel.uuid, + ); + subSpaceModel.subspaceName = subspaceName; await queryRunner.manager.save(subSpaceModel); } diff --git a/src/space-model/services/tag-model.service.ts b/src/space-model/services/tag-model.service.ts index b251b28..c364816 100644 --- a/src/space-model/services/tag-model.service.ts +++ b/src/space-model/services/tag-model.service.ts @@ -96,7 +96,11 @@ export class TagModelService { async deleteTags(tagUuids: string[], queryRunner: QueryRunner) { try { const deletePromises = tagUuids.map((id) => - queryRunner.manager.softDelete(this.tagModelRepository.target, id), + queryRunner.manager.update( + this.tagModelRepository.target, + { uuid: id }, + { disabled: true }, + ), ); await Promise.all(deletePromises); @@ -276,7 +280,7 @@ export class TagModelService { async getTagByUuid(uuid: string): Promise { const tag = await this.tagModelRepository.findOne({ - where: { uuid }, + where: { uuid, disabled: false }, relations: ['product'], }); if (!tag) {