added space model details to space

This commit is contained in:
hannathkadher
2024-12-11 16:45:09 +04:00
parent bbd40253b8
commit a2ee7a0001
4 changed files with 73 additions and 19 deletions

View File

@ -17,7 +17,7 @@ export class CreateSpaceProductModelDto {
}) })
@IsNotEmpty() @IsNotEmpty()
@IsString() @IsString()
productId: string; productUuid: string;
@ApiProperty({ @ApiProperty({
description: 'Number of products in the model', description: 'Number of products in the model',

View File

@ -21,6 +21,12 @@ export class SpaceProductItemModelService {
) { ) {
await this.validateTags(itemModelDtos, queryRunner, spaceModel); await this.validateTags(itemModelDtos, queryRunner, spaceModel);
if (!spaceProductModel) {
throw new HttpException(
'Space product model is required to create product items.',
HttpStatus.BAD_REQUEST,
);
}
try { try {
const productItems = itemModelDtos.map((dto) => const productItems = itemModelDtos.map((dto) =>
queryRunner.manager.create(this.spaceProductItemRepository.target, { queryRunner.manager.create(this.spaceProductItemRepository.target, {

View File

@ -25,7 +25,7 @@ export class SpaceProductModelService {
const productModels = await Promise.all( const productModels = await Promise.all(
spaceProductModelDtos.map(async (dto) => { spaceProductModelDtos.map(async (dto) => {
this.validateProductCount(dto); this.validateProductCount(dto);
const product = await this.getProduct(dto.productId); const product = await this.getProduct(dto.productUuid);
return queryRunner.manager.create( return queryRunner.manager.create(
this.spaceProductModelRepository.target, this.spaceProductModelRepository.target,
{ {
@ -40,14 +40,15 @@ export class SpaceProductModelService {
const savedProductModels = await queryRunner.manager.save(productModels); const savedProductModels = await queryRunner.manager.save(productModels);
await Promise.all( await Promise.all(
spaceProductModelDtos.map((dto, index) => spaceProductModelDtos.map((dto, index) => {
this.spaceProductItemModelService.createProdutItemModel( const savedModel = savedProductModels[index];
return this.spaceProductItemModelService.createProdutItemModel(
dto.items, dto.items,
savedProductModels[index], savedModel, // Pass the saved model
spaceModel, spaceModel,
queryRunner, queryRunner,
), );
), }),
); );
} catch (error) { } catch (error) {
if (error instanceof HttpException) { if (error instanceof HttpException) {
@ -65,7 +66,7 @@ export class SpaceProductModelService {
const productItemCount = dto.items.length; const productItemCount = dto.items.length;
if (dto.productCount !== productItemCount) { if (dto.productCount !== productItemCount) {
throw new HttpException( throw new HttpException(
`Product count (${dto.productCount}) does not match the number of items (${productItemCount}) for product ID ${dto.productId}.`, `Product count (${dto.productCount}) does not match the number of items (${productItemCount}) for product ID ${dto.productUuid}.`,
HttpStatus.BAD_REQUEST, HttpStatus.BAD_REQUEST,
); );
} }

View File

@ -1,6 +1,7 @@
import { ApiProperty } from '@nestjs/swagger'; import { ApiProperty } from '@nestjs/swagger';
import { Type } from 'class-transformer'; import { Type } from 'class-transformer';
import { import {
ArrayNotEmpty,
IsArray, IsArray,
IsBoolean, IsBoolean,
IsNotEmpty, IsNotEmpty,
@ -11,6 +12,42 @@ import {
ValidateNested, ValidateNested,
} from 'class-validator'; } from 'class-validator';
export class CreateSpaceProductItemDto {
@ApiProperty({
description: 'Specific name for the product item',
example: 'Light 1',
})
@IsNotEmpty()
@IsString()
tag: string;
}
class ProductAssignmentDto {
@ApiProperty({
description: 'UUID of the product to be assigned',
example: 'prod-uuid-1234',
})
@IsNotEmpty()
productId: string;
@ApiProperty({
description: 'Number of items to assign for the product',
example: 3,
})
count: number;
@ApiProperty({
description: 'Specific names for each product item',
type: [CreateSpaceProductItemDto],
example: [{ tag: 'Light 1' }, { tag: 'Light 2' }, { tag: 'Light 3' }],
})
@IsArray()
@ArrayNotEmpty()
@ValidateNested({ each: true })
@Type(() => CreateSpaceProductItemDto)
items: CreateSpaceProductItemDto[];
}
export class AddSpaceDto { export class AddSpaceDto {
@ApiProperty({ @ApiProperty({
description: 'Name of the space (e.g., Floor 1, Unit 101)', description: 'Name of the space (e.g., Floor 1, Unit 101)',
@ -29,9 +66,14 @@ export class AddSpaceDto {
@IsOptional() @IsOptional()
parentUuid?: string; parentUuid?: string;
@ApiProperty({
description: 'Icon identifier for the space',
example: 'assets/location',
required: false,
})
@IsString() @IsString()
@IsOptional() @IsOptional()
public icon: string; public icon?: string;
@ApiProperty({ @ApiProperty({
description: 'Indicates whether the space is private or public', description: 'Indicates whether the space is private or public',
@ -49,16 +91,29 @@ export class AddSpaceDto {
@IsNumber() @IsNumber()
y: number; y: number;
@ApiProperty({
description: 'UUID of the Space',
example: 'd290f1ee-6c54-4b01-90e6-d701748f0851',
})
@IsString()
@IsOptional()
spaceModelUuid?: string;
@ApiProperty({ description: 'Y position on canvas', example: 200 }) @ApiProperty({ description: 'Y position on canvas', example: 200 })
@IsString() @IsString()
@IsOptional() @IsOptional()
direction: string; direction?: string;
@ApiProperty({
description: 'List of products assigned to this space',
type: [ProductAssignmentDto],
required: false,
})
@IsArray() @IsArray()
@ValidateNested({ each: true }) @ValidateNested({ each: true })
@IsOptional() @IsOptional()
@Type(() => ProductAssignmentDto) @Type(() => ProductAssignmentDto)
products: ProductAssignmentDto[]; products?: ProductAssignmentDto[];
} }
export class AddUserSpaceDto { export class AddUserSpaceDto {
@ -101,11 +156,3 @@ export class AddUserSpaceUsingCodeDto {
Object.assign(this, dto); Object.assign(this, dto);
} }
} }
class ProductAssignmentDto {
@IsNotEmpty()
productId: string;
@IsNotEmpty()
count: number;
}