From 6da79481fb329a97ba616049a91343bc8a545bb5 Mon Sep 17 00:00:00 2001 From: Mhd Zayd Skaff Date: Wed, 9 Jul 2025 15:23:40 +0300 Subject: [PATCH] fix: check if subspaces not exists in update space --- src/space/dtos/update.space.dto.ts | 5 +-- .../services/subspace/subspace.service.ts | 33 ++++++++++++------- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/src/space/dtos/update.space.dto.ts b/src/space/dtos/update.space.dto.ts index 0491aa1..5f1fbcd 100644 --- a/src/space/dtos/update.space.dto.ts +++ b/src/space/dtos/update.space.dto.ts @@ -50,11 +50,12 @@ export class UpdateSpaceDto { description: 'List of subspace modifications', type: [UpdateSubspaceDto], }) - @ArrayUnique((subspace) => subspace.subspaceName, { + @ArrayUnique((subspace) => subspace.subspaceName ?? subspace.uuid, { message(validationArguments) { const subspaces = validationArguments.value; const nameCounts = subspaces.reduce((acc, curr) => { - acc[curr.subspaceName] = (acc[curr.subspaceName] || 0) + 1; + acc[curr.subspaceName ?? curr.uuid] = + (acc[curr.subspaceName ?? curr.uuid] || 0) + 1; return acc; }, {}); // Find duplicates diff --git a/src/space/services/subspace/subspace.service.ts b/src/space/services/subspace/subspace.service.ts index 15bda14..64d56c9 100644 --- a/src/space/services/subspace/subspace.service.ts +++ b/src/space/services/subspace/subspace.service.ts @@ -342,26 +342,37 @@ export class SubSpaceService { })), ); + const existingSubspaces = await this.subspaceRepository.find({ + where: { + uuid: In( + subspaceDtos.filter((dto) => dto.uuid).map((dto) => dto.uuid), + ), + }, + }); + + if ( + existingSubspaces.length !== + subspaceDtos.filter((dto) => dto.uuid).length + ) { + throw new HttpException( + `Some subspaces with provided UUIDs do not exist in the space.`, + HttpStatus.NOT_FOUND, + ); + } + const updatedSubspaces: SubspaceEntity[] = await queryRunner.manager.save( SubspaceEntity, - [ - ...newSubspaces, - ...subspaceDtos - .filter((dto) => dto.uuid) - .map((dto) => ({ - subspaceName: dto.subspaceName, - space, - })), - ], + newSubspaces, ); + const allSubspaces = [...updatedSubspaces, ...existingSubspaces]; // create or update allocations for the subspaces - if (updatedSubspaces.length > 0) { + if (allSubspaces.length > 0) { await this.subspaceProductAllocationService.updateSubspaceProductAllocationsV2( subspaceDtos.map((dto) => ({ ...dto, uuid: dto.uuid || - updatedSubspaces.find((s) => s.subspaceName === dto.subspaceName) + allSubspaces.find((s) => s.subspaceName === dto.subspaceName) ?.uuid, })), projectUuid,