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