update product model

This commit is contained in:
hannathkadher
2024-12-20 14:16:23 +04:00
parent 61c17c2e95
commit c78eeff7e6
4 changed files with 81 additions and 8 deletions

View File

@ -12,7 +12,7 @@ import { ProductItemModelModificationDto } from '../product-item-model-dtos';
export class UpdateProductModelDto extends BaseProductModelDto {
@ApiProperty({
description: 'Number of products in the model',
description: 'Number of products to be modified in the model',
example: 3,
})
@IsNotEmpty()
@ -27,5 +27,5 @@ export class UpdateProductModelDto extends BaseProductModelDto {
@ArrayNotEmpty()
@ValidateNested({ each: true })
@Type(() => ProductItemModelModificationDto)
items: ProductItemModelModificationDto[];
items: ProductItemModelModificationDto;
}

View File

@ -35,3 +35,7 @@ export interface IUpdateSubspaceModelInterface {
export interface IDeletedSubsaceModelInterface {
uuid: string;
}
export interface IModifiedProductModelsInterface {
add?: ProductModelInterface[];
}

View File

@ -159,7 +159,7 @@ export class SubspaceProductItemModelService extends BaseProductItemService {
}
return productItemModels;
} catch (error) {
this.handleException(error, 'Failed to modify SpaceModels.');
this.handleException(error, 'Failed to modify Product Item Models.');
}
}

View File

@ -1,14 +1,24 @@
import {
SpaceModelEntity,
SubspaceModelEntity,
SubspaceProductModelEntity,
SubspaceProductModelRepository,
} from '@app/common/modules/space-model';
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { SubspaceProductItemModelService } from './subspace-product-item-model.service';
import { CreateProductModelDto } from '../../dtos';
import { QueryRunner } from 'typeorm';
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { SubspaceProductItemModelService } from './subspace-product-item-model.service';
import {
CreateProductModelDto,
ProductModelModificationDto,
UpdateProductModelDto,
} from '../../dtos';
import { BaseProductModelService } from '../../common';
import { ProductService } from 'src/product/services';
import {
IModifiedProductModelsInterface,
ProductModelInterface,
} from '../../interfaces';
@Injectable()
export class SubspaceProductModelService extends BaseProductModelService {
@ -25,7 +35,7 @@ export class SubspaceProductModelService extends BaseProductModelService {
spaceModel: SpaceModelEntity,
subspaceModel: SubspaceModelEntity,
queryRunner: QueryRunner,
) {
): Promise<ProductModelInterface[]> {
try {
if (!spaceProductModelDtos?.length) return;
@ -59,7 +69,7 @@ export class SubspaceProductModelService extends BaseProductModelService {
return {
productModel: savedModel,
productItemModels,
};
} as ProductModelInterface;
}),
);
return newProductModels;
@ -74,4 +84,63 @@ export class SubspaceProductModelService extends BaseProductModelService {
);
}
}
async updateSubspaceProductModels(dtos: UpdateProductModelDto[]) {
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) {}
}
async modifySubspaceProductModels(
dto: ProductModelModificationDto,
spaceModel: SpaceModelEntity,
subspaceModel: SubspaceModelEntity,
queryRunner: QueryRunner,
) {
const productItemModels: IModifiedProductModelsInterface = {};
try {
productItemModels.add = await this.createSubspaceProductModels(
dto.add,
spaceModel,
subspaceModel,
queryRunner,
);
} catch (error) {
if (error instanceof HttpException) {
throw error;
}
throw new HttpException(
'Failed to modify Subspace product model',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
async findOne(uuid: string): Promise<SubspaceProductModelEntity> {
const productModel = await this.subpaceProductModelRepository.findOne({
where: {
uuid,
},
});
if (!productModel)
throw new HttpException(
`Subspace Product model with uuid ${uuid} not found`,
HttpStatus.NOT_FOUND,
);
return productModel;
}
}