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',
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

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(
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,