mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 10:54:55 +00:00
removed count from all entities
This commit is contained in:
@ -1,5 +1,5 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { IsNotEmpty, IsNumber, IsString } from 'class-validator';
|
||||
import { IsNotEmpty, IsString } from 'class-validator';
|
||||
import { SpaceProductItemModelDto } from './space-product-item-model.dto';
|
||||
|
||||
export class SpaceProductModelDto {
|
||||
@ -7,10 +7,6 @@ export class SpaceProductModelDto {
|
||||
@IsNotEmpty()
|
||||
uuid: string;
|
||||
|
||||
@IsNumber()
|
||||
@IsNotEmpty()
|
||||
productCount: number;
|
||||
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
productUuid: string;
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { IsNotEmpty, IsNumber, IsString } from 'class-validator';
|
||||
import { IsNotEmpty, IsString } from 'class-validator';
|
||||
import { SubspaceProductItemModelDto } from './subspace-product-item-model.dto';
|
||||
|
||||
export class SubpaceProductModelDto {
|
||||
@ -7,10 +7,6 @@ export class SubpaceProductModelDto {
|
||||
@IsNotEmpty()
|
||||
uuid: string;
|
||||
|
||||
@IsNumber()
|
||||
@IsNotEmpty()
|
||||
productCount: number;
|
||||
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
productUuid: string;
|
||||
|
||||
@ -8,12 +8,6 @@ import { SpaceProductEntity } from '../../space/entities';
|
||||
|
||||
@Entity({ name: 'space-product-model' })
|
||||
export class SpaceProductModelEntity extends AbstractEntity<SpaceProductModelDto> {
|
||||
@Column({
|
||||
nullable: false,
|
||||
type: 'int',
|
||||
})
|
||||
productCount: number;
|
||||
|
||||
@ManyToOne(
|
||||
() => SpaceModelEntity,
|
||||
(spaceModel) => spaceModel.spaceProductModels,
|
||||
|
||||
@ -8,12 +8,6 @@ import { SubspaceProductItemModelEntity } from './subspace-product-item-model.en
|
||||
|
||||
@Entity({ name: 'subspace-product-model' })
|
||||
export class SubspaceProductModelEntity extends AbstractEntity<SubpaceProductModelDto> {
|
||||
@Column({
|
||||
nullable: false,
|
||||
type: 'int',
|
||||
})
|
||||
productCount: number;
|
||||
|
||||
@Column({
|
||||
nullable: false,
|
||||
default: false,
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { IsString, IsNotEmpty, IsNumber } from 'class-validator';
|
||||
import { IsString, IsNotEmpty } from 'class-validator';
|
||||
import { SpaceProductItemDto } from './space-product-item.dto';
|
||||
|
||||
export class SpaceProductModelDto {
|
||||
@ -7,10 +7,6 @@ export class SpaceProductModelDto {
|
||||
@IsNotEmpty()
|
||||
uuid: string;
|
||||
|
||||
@IsNumber()
|
||||
@IsNotEmpty()
|
||||
productCount: number;
|
||||
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
productUuid: string;
|
||||
|
||||
@ -21,12 +21,6 @@ export class SpaceProductEntity extends AbstractEntity<SpaceProductEntity> {
|
||||
@JoinColumn({ name: 'product_uuid' })
|
||||
product: ProductEntity;
|
||||
|
||||
@Column({
|
||||
nullable: false,
|
||||
type: 'int',
|
||||
})
|
||||
productCount: number;
|
||||
|
||||
@Column({
|
||||
nullable: false,
|
||||
default: false,
|
||||
|
||||
@ -15,12 +15,6 @@ export class SubspaceProductEntity extends AbstractEntity<SpaceProductModelDto>
|
||||
})
|
||||
public uuid: string;
|
||||
|
||||
@Column({
|
||||
nullable: false,
|
||||
type: 'int',
|
||||
})
|
||||
productCount: number;
|
||||
|
||||
@Column({
|
||||
nullable: false,
|
||||
default: false,
|
||||
|
||||
@ -1,22 +1,8 @@
|
||||
import { HttpException, HttpStatus } from '@nestjs/common';
|
||||
import { CreateProductModelDto } from '../../dtos';
|
||||
import { ProductService } from '../../../product/services';
|
||||
|
||||
export abstract class BaseProductModelService {
|
||||
constructor(private readonly productService: ProductService) {}
|
||||
|
||||
protected async validateProductCount(
|
||||
dto: CreateProductModelDto,
|
||||
): Promise<void> {
|
||||
const productItemCount = dto.items.length;
|
||||
if (dto.productCount !== productItemCount) {
|
||||
throw new HttpException(
|
||||
`Product count (${dto.productCount}) does not match the number of items (${productItemCount}) for product ID ${dto.productUuid}.`,
|
||||
HttpStatus.BAD_REQUEST,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
protected async getProduct(productId: string) {
|
||||
const product = await this.productService.findOne(productId);
|
||||
return product.data;
|
||||
|
||||
@ -4,7 +4,6 @@ import {
|
||||
IsString,
|
||||
IsArray,
|
||||
ValidateNested,
|
||||
IsInt,
|
||||
ArrayNotEmpty,
|
||||
} from 'class-validator';
|
||||
import { Type } from 'class-transformer';
|
||||
@ -19,14 +18,6 @@ export class CreateProductModelDto {
|
||||
@IsString()
|
||||
productUuid: string;
|
||||
|
||||
@ApiProperty({
|
||||
description: 'Number of products in the model',
|
||||
example: 3,
|
||||
})
|
||||
@IsNotEmpty()
|
||||
@IsInt()
|
||||
productCount: number;
|
||||
|
||||
@ApiProperty({
|
||||
description: 'Specific names for each product item',
|
||||
type: [CreateProductItemModelDto],
|
||||
|
||||
@ -1,24 +1,10 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { Type } from 'class-transformer';
|
||||
import {
|
||||
IsNotEmpty,
|
||||
IsInt,
|
||||
IsArray,
|
||||
ArrayNotEmpty,
|
||||
ValidateNested,
|
||||
} from 'class-validator';
|
||||
import { IsArray, ArrayNotEmpty, ValidateNested } from 'class-validator';
|
||||
import { BaseProductModelDto } from './base-product-model.dto';
|
||||
import { ProductItemModelModificationDto } from '../product-item-model-dtos';
|
||||
|
||||
export class UpdateProductModelDto extends BaseProductModelDto {
|
||||
@ApiProperty({
|
||||
description: 'Number of products to be modified in the model',
|
||||
example: 3,
|
||||
})
|
||||
@IsNotEmpty()
|
||||
@IsInt()
|
||||
productCount: number;
|
||||
|
||||
@ApiProperty({
|
||||
description: 'Update product item',
|
||||
type: [ProductItemModelModificationDto],
|
||||
|
||||
@ -189,14 +189,13 @@ export class PropogateSubspaceHandler
|
||||
const subspaceProduct = this.productRepository.create({
|
||||
product: productModel.productModel.product,
|
||||
subspace,
|
||||
productCount: productModel.productModel.productCount,
|
||||
model: productModel.productModel,
|
||||
});
|
||||
|
||||
const createdSubspaceProduct =
|
||||
await this.productRepository.save(subspaceProduct);
|
||||
this.logger.log(
|
||||
`Product added to subspace ${subspace.id} with count ${createdSubspaceProduct.productCount}`,
|
||||
`Product added to subspace ${subspace.id} with count ${createdSubspaceProduct.items.length}`,
|
||||
);
|
||||
return createdSubspaceProduct;
|
||||
}
|
||||
|
||||
@ -27,13 +27,11 @@ export class SpaceProductModelService extends BaseProductModelService {
|
||||
try {
|
||||
const productModels = await Promise.all(
|
||||
spaceProductModelDtos.map(async (dto) => {
|
||||
this.validateProductCount(dto);
|
||||
const product = await this.getProduct(dto.productUuid);
|
||||
return queryRunner.manager.create(
|
||||
this.spaceProductModelRepository.target,
|
||||
{
|
||||
product,
|
||||
productCount: dto.productCount,
|
||||
spaceModel,
|
||||
},
|
||||
);
|
||||
|
||||
@ -41,13 +41,11 @@ export class SubspaceProductModelService extends BaseProductModelService {
|
||||
|
||||
const productModels = await Promise.all(
|
||||
spaceProductModelDtos.map(async (dto) => {
|
||||
this.validateProductCount(dto);
|
||||
const product = await this.getProduct(dto.productUuid);
|
||||
return queryRunner.manager.create(
|
||||
this.subpaceProductModelRepository.target,
|
||||
{
|
||||
product,
|
||||
productCount: dto.productCount,
|
||||
subspaceModel,
|
||||
},
|
||||
);
|
||||
@ -89,18 +87,6 @@ export class SubspaceProductModelService extends BaseProductModelService {
|
||||
try {
|
||||
for (const dto of dtos) {
|
||||
await this.findOne(dto.productModelUuid);
|
||||
const newCount = dto.productCount;
|
||||
if (
|
||||
dto.items.add.length +
|
||||
dto.items.delete.length +
|
||||
dto.items.delete.length !==
|
||||
newCount
|
||||
) {
|
||||
throw new HttpException(
|
||||
`Invalid list of items`,
|
||||
HttpStatus.BAD_REQUEST,
|
||||
);
|
||||
}
|
||||
}
|
||||
} catch (error) {}
|
||||
}
|
||||
|
||||
@ -30,12 +30,6 @@ export class ProductAssignmentDto {
|
||||
@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],
|
||||
|
||||
@ -31,7 +31,6 @@ export class SpaceProductService {
|
||||
queryRunner.manager.create(SpaceProductEntity, {
|
||||
space: space,
|
||||
product: spaceProductModel.product,
|
||||
productCount: spaceProductModel.productCount,
|
||||
spaceProductModel: spaceProductModel,
|
||||
}),
|
||||
);
|
||||
@ -150,17 +149,14 @@ export class SpaceProductService {
|
||||
): Promise<SpaceProductEntity[]> {
|
||||
const updatedProducts = [];
|
||||
|
||||
for (const { productId, count } of uniqueProducts) {
|
||||
for (const { productId } of uniqueProducts) {
|
||||
productEntities.get(productId);
|
||||
const existingProduct = existingSpaceProducts.find(
|
||||
(spaceProduct) => spaceProduct.product.uuid === productId,
|
||||
);
|
||||
|
||||
if (existingProduct && existingProduct.productCount !== count) {
|
||||
existingProduct.productCount = count;
|
||||
updatedProducts.push(existingProduct);
|
||||
}
|
||||
}
|
||||
|
||||
if (updatedProducts.length > 0) {
|
||||
await queryRunner.manager.save(SpaceProductEntity, updatedProducts);
|
||||
@ -180,13 +176,11 @@ export class SpaceProductService {
|
||||
for (const uniqueSpaceProduct of uniqueSpaceProducts) {
|
||||
const product = productEntities.get(uniqueSpaceProduct.productId);
|
||||
await this.getProduct(uniqueSpaceProduct.productId);
|
||||
this.validateProductCount(uniqueSpaceProduct);
|
||||
|
||||
newProducts.push(
|
||||
queryRunner.manager.create(SpaceProductEntity, {
|
||||
space,
|
||||
product,
|
||||
productCount: uniqueSpaceProduct.count,
|
||||
}),
|
||||
);
|
||||
}
|
||||
@ -209,16 +203,6 @@ export class SpaceProductService {
|
||||
return newProducts;
|
||||
}
|
||||
|
||||
private validateProductCount(dto: ProductAssignmentDto) {
|
||||
const productItemCount = dto.items.length;
|
||||
if (dto.count !== productItemCount) {
|
||||
throw new HttpException(
|
||||
`Product count (${dto.count}) does not match the number of items (${productItemCount}) for product ID ${dto.productId}.`,
|
||||
HttpStatus.BAD_REQUEST,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async getProduct(productId: string): Promise<ProductEntity> {
|
||||
const product = await this.productService.findOne(productId);
|
||||
return product.data;
|
||||
|
||||
@ -64,7 +64,6 @@ export class SubspaceProductService {
|
||||
return {
|
||||
subspace,
|
||||
product: productModel.product,
|
||||
productCount: productModel.productCount,
|
||||
model: productModel,
|
||||
};
|
||||
}
|
||||
@ -78,13 +77,10 @@ export class SubspaceProductService {
|
||||
try {
|
||||
const newSpaceProducts = await Promise.all(
|
||||
productDtos.map(async (dto) => {
|
||||
this.validateProductCount(dto);
|
||||
|
||||
const product = await this.getProduct(dto.productId);
|
||||
return queryRunner.manager.create(SubspaceProductEntity, {
|
||||
subspace,
|
||||
product,
|
||||
productCount: dto.count,
|
||||
});
|
||||
}),
|
||||
);
|
||||
@ -116,13 +112,4 @@ export class SubspaceProductService {
|
||||
const product = await this.productService.findOne(productId);
|
||||
return product.data;
|
||||
}
|
||||
|
||||
async validateProductCount(dto: ProductAssignmentDto) {
|
||||
if (dto.count !== dto.items.length) {
|
||||
throw new HttpException(
|
||||
'Producy item and count doesnot match',
|
||||
HttpStatus.BAD_REQUEST,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user