mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-09 22:57:24 +00:00
fix: add unique validation on subspaces in update space dto (#460)
This commit is contained in:
@ -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 })
|
||||
|
@ -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 }),
|
||||
);
|
||||
|
@ -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)
|
||||
|
@ -39,7 +39,7 @@ export class SubSpaceService {
|
||||
private readonly subspaceProductAllocationService: SubspaceProductAllocationService,
|
||||
) {}
|
||||
|
||||
async createSubspaces(
|
||||
private async createSubspaces(
|
||||
subspaceData: Array<{
|
||||
subspaceName: string;
|
||||
space: SpaceEntity;
|
||||
|
Reference in New Issue
Block a user