removed count from all entities

This commit is contained in:
hannathkadher
2024-12-20 14:27:25 +04:00
parent c78eeff7e6
commit 9245c47ef5
16 changed files with 7 additions and 132 deletions

View File

@ -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;

View File

@ -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],

View File

@ -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],

View File

@ -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;
}

View File

@ -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,
},
);

View File

@ -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) {}
}

View File

@ -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],

View File

@ -31,7 +31,6 @@ export class SpaceProductService {
queryRunner.manager.create(SpaceProductEntity, {
space: space,
product: spaceProductModel.product,
productCount: spaceProductModel.productCount,
spaceProductModel: spaceProductModel,
}),
);
@ -150,16 +149,13 @@ 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);
}
updatedProducts.push(existingProduct);
}
if (updatedProducts.length > 0) {
@ -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;

View File

@ -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,
);
}
}
}