mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-16 18:56:22 +00:00
added count validation
This commit is contained in:
@ -5,6 +5,7 @@ import {
|
||||
IsArray,
|
||||
ValidateNested,
|
||||
IsInt,
|
||||
ArrayNotEmpty,
|
||||
} from 'class-validator';
|
||||
import { Type } from 'class-transformer';
|
||||
import { CreateSpaceProductItemModelDto } from './create-space-product-item-model.dto';
|
||||
@ -31,6 +32,7 @@ export class CreateSpaceProductModelDto {
|
||||
type: [CreateSpaceProductItemModelDto],
|
||||
})
|
||||
@IsArray()
|
||||
@ArrayNotEmpty()
|
||||
@ValidateNested({ each: true })
|
||||
@Type(() => CreateSpaceProductItemModelDto)
|
||||
items: CreateSpaceProductItemModelDto[];
|
||||
|
@ -39,6 +39,8 @@ export class SpaceProductModelService {
|
||||
spaceProductModelDto: CreateSpaceProductModelDto,
|
||||
spaceModel: SpaceModelEntity,
|
||||
) {
|
||||
this.validateCount(spaceProductModelDto);
|
||||
|
||||
const product = await this.productRepository.findOneBy({
|
||||
uuid: spaceProductModelDto.productId,
|
||||
});
|
||||
@ -62,4 +64,14 @@ export class SpaceProductModelService {
|
||||
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