mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 11:24:54 +00:00
added count validation
This commit is contained in:
@ -5,6 +5,7 @@ import {
|
|||||||
IsArray,
|
IsArray,
|
||||||
ValidateNested,
|
ValidateNested,
|
||||||
IsInt,
|
IsInt,
|
||||||
|
ArrayNotEmpty,
|
||||||
} from 'class-validator';
|
} from 'class-validator';
|
||||||
import { Type } from 'class-transformer';
|
import { Type } from 'class-transformer';
|
||||||
import { CreateSpaceProductItemModelDto } from './create-space-product-item-model.dto';
|
import { CreateSpaceProductItemModelDto } from './create-space-product-item-model.dto';
|
||||||
@ -31,6 +32,7 @@ export class CreateSpaceProductModelDto {
|
|||||||
type: [CreateSpaceProductItemModelDto],
|
type: [CreateSpaceProductItemModelDto],
|
||||||
})
|
})
|
||||||
@IsArray()
|
@IsArray()
|
||||||
|
@ArrayNotEmpty()
|
||||||
@ValidateNested({ each: true })
|
@ValidateNested({ each: true })
|
||||||
@Type(() => CreateSpaceProductItemModelDto)
|
@Type(() => CreateSpaceProductItemModelDto)
|
||||||
items: CreateSpaceProductItemModelDto[];
|
items: CreateSpaceProductItemModelDto[];
|
||||||
|
|||||||
@ -39,6 +39,8 @@ export class SpaceProductModelService {
|
|||||||
spaceProductModelDto: CreateSpaceProductModelDto,
|
spaceProductModelDto: CreateSpaceProductModelDto,
|
||||||
spaceModel: SpaceModelEntity,
|
spaceModel: SpaceModelEntity,
|
||||||
) {
|
) {
|
||||||
|
this.validateCount(spaceProductModelDto);
|
||||||
|
|
||||||
const product = await this.productRepository.findOneBy({
|
const product = await this.productRepository.findOneBy({
|
||||||
uuid: spaceProductModelDto.productId,
|
uuid: spaceProductModelDto.productId,
|
||||||
});
|
});
|
||||||
@ -62,4 +64,14 @@ export class SpaceProductModelService {
|
|||||||
spaceModel,
|
spaceModel,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private validateCount(spaceProductModelDto: CreateSpaceProductModelDto) {
|
||||||
|
const productItemCount = spaceProductModelDto.items.length;
|
||||||
|
if (spaceProductModelDto.productCount !== productItemCount) {
|
||||||
|
throw new HttpException(
|
||||||
|
`Product count (${spaceProductModelDto.productCount}) does not match the number of items (${productItemCount}) for product ID ${spaceProductModelDto.productId}.`,
|
||||||
|
HttpStatus.BAD_REQUEST,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user