fix: check if subspaces not exists in update space

This commit is contained in:
Mhd Zayd Skaff
2025-07-09 15:23:40 +03:00
parent 83be80d9f6
commit 6da79481fb
2 changed files with 25 additions and 13 deletions

View File

@ -50,11 +50,12 @@ export class UpdateSpaceDto {
description: 'List of subspace modifications', description: 'List of subspace modifications',
type: [UpdateSubspaceDto], type: [UpdateSubspaceDto],
}) })
@ArrayUnique((subspace) => subspace.subspaceName, { @ArrayUnique((subspace) => subspace.subspaceName ?? subspace.uuid, {
message(validationArguments) { message(validationArguments) {
const subspaces = validationArguments.value; const subspaces = validationArguments.value;
const nameCounts = subspaces.reduce((acc, curr) => { 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; return acc;
}, {}); }, {});
// Find duplicates // Find duplicates

View File

@ -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( const updatedSubspaces: SubspaceEntity[] = await queryRunner.manager.save(
SubspaceEntity, SubspaceEntity,
[ newSubspaces,
...newSubspaces,
...subspaceDtos
.filter((dto) => dto.uuid)
.map((dto) => ({
subspaceName: dto.subspaceName,
space,
})),
],
); );
const allSubspaces = [...updatedSubspaces, ...existingSubspaces];
// create or update allocations for the subspaces // create or update allocations for the subspaces
if (updatedSubspaces.length > 0) { if (allSubspaces.length > 0) {
await this.subspaceProductAllocationService.updateSubspaceProductAllocationsV2( await this.subspaceProductAllocationService.updateSubspaceProductAllocationsV2(
subspaceDtos.map((dto) => ({ subspaceDtos.map((dto) => ({
...dto, ...dto,
uuid: uuid:
dto.uuid || dto.uuid ||
updatedSubspaces.find((s) => s.subspaceName === dto.subspaceName) allSubspaces.find((s) => s.subspaceName === dto.subspaceName)
?.uuid, ?.uuid,
})), })),
projectUuid, projectUuid,