fix: add unique validation on subspaces in update space dto (#460)

This commit is contained in:
ZaydSkaff
2025-07-09 11:33:04 +03:00
committed by GitHub
parent 5cf45c30f4
commit 2589e391ed
4 changed files with 19 additions and 4 deletions

View File

@ -2,6 +2,7 @@ import { ORPHAN_SPACE_NAME } from '@app/common/constants/orphan-constant';
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { Type } from 'class-transformer';
import {
ArrayUnique,
IsArray,
IsNumber,
IsOptional,
@ -49,6 +50,20 @@ export class UpdateSpaceDto {
description: 'List of subspace modifications',
type: [UpdateSubspaceDto],
})
@ArrayUnique((subspace) => subspace.subspaceName, {
message(validationArguments) {
const subspaces = validationArguments.value;
const nameCounts = subspaces.reduce((acc, curr) => {
acc[curr.subspaceName] = (acc[curr.subspaceName] || 0) + 1;
return acc;
}, {});
// Find duplicates
const duplicates = Object.keys(nameCounts).filter(
(name) => nameCounts[name] > 1,
);
return `Duplicate subspace names found: ${duplicates.join(', ')}`;
},
})
@IsOptional()
@IsArray()
@ValidateNested({ each: true })

View File

@ -426,7 +426,7 @@ export class SpaceService {
}
}
async disableSpace(space: SpaceEntity, orphanSpace: SpaceEntity) {
private async disableSpace(space: SpaceEntity, orphanSpace: SpaceEntity) {
await this.commandBus.execute(
new DisableSpaceCommand({ spaceUuid: space.uuid, orphanSpace }),
);

View File

@ -23,7 +23,7 @@ export class SubspaceProductAllocationService {
// spaceAllocationsToExclude?: SpaceProductAllocationEntity[],
): Promise<void> {
try {
if (!allocationsData.length) return;
if (!allocationsData?.length) return;
const allocations: SubspaceProductAllocationEntity[] = [];
@ -112,7 +112,7 @@ export class SubspaceProductAllocationService {
);
// Create the product-tag mapping based on the processed tags
const productTagMapping = subspace.productAllocations.map(
const productTagMapping = subspace.productAllocations?.map(
({ tagUuid, tagName, productUuid }) => {
const inputTag = tagUuid
? createdTagsByUUID.get(tagUuid)

View File

@ -39,7 +39,7 @@ export class SubSpaceService {
private readonly subspaceProductAllocationService: SubspaceProductAllocationService,
) {}
async createSubspaces(
private async createSubspaces(
subspaceData: Array<{
subspaceName: string;
space: SpaceEntity;