mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 12:34:54 +00:00
update product model
This commit is contained in:
@ -12,7 +12,7 @@ import { ProductItemModelModificationDto } from '../product-item-model-dtos';
|
|||||||
|
|
||||||
export class UpdateProductModelDto extends BaseProductModelDto {
|
export class UpdateProductModelDto extends BaseProductModelDto {
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
description: 'Number of products in the model',
|
description: 'Number of products to be modified in the model',
|
||||||
example: 3,
|
example: 3,
|
||||||
})
|
})
|
||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
@ -27,5 +27,5 @@ export class UpdateProductModelDto extends BaseProductModelDto {
|
|||||||
@ArrayNotEmpty()
|
@ArrayNotEmpty()
|
||||||
@ValidateNested({ each: true })
|
@ValidateNested({ each: true })
|
||||||
@Type(() => ProductItemModelModificationDto)
|
@Type(() => ProductItemModelModificationDto)
|
||||||
items: ProductItemModelModificationDto[];
|
items: ProductItemModelModificationDto;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -35,3 +35,7 @@ export interface IUpdateSubspaceModelInterface {
|
|||||||
export interface IDeletedSubsaceModelInterface {
|
export interface IDeletedSubsaceModelInterface {
|
||||||
uuid: string;
|
uuid: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface IModifiedProductModelsInterface {
|
||||||
|
add?: ProductModelInterface[];
|
||||||
|
}
|
||||||
|
|||||||
@ -159,7 +159,7 @@ export class SubspaceProductItemModelService extends BaseProductItemService {
|
|||||||
}
|
}
|
||||||
return productItemModels;
|
return productItemModels;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.handleException(error, 'Failed to modify SpaceModels.');
|
this.handleException(error, 'Failed to modify Product Item Models.');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,14 +1,24 @@
|
|||||||
import {
|
import {
|
||||||
SpaceModelEntity,
|
SpaceModelEntity,
|
||||||
SubspaceModelEntity,
|
SubspaceModelEntity,
|
||||||
|
SubspaceProductModelEntity,
|
||||||
SubspaceProductModelRepository,
|
SubspaceProductModelRepository,
|
||||||
} from '@app/common/modules/space-model';
|
} 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 { 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 { BaseProductModelService } from '../../common';
|
||||||
import { ProductService } from 'src/product/services';
|
import { ProductService } from 'src/product/services';
|
||||||
|
import {
|
||||||
|
IModifiedProductModelsInterface,
|
||||||
|
ProductModelInterface,
|
||||||
|
} from '../../interfaces';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class SubspaceProductModelService extends BaseProductModelService {
|
export class SubspaceProductModelService extends BaseProductModelService {
|
||||||
@ -25,7 +35,7 @@ export class SubspaceProductModelService extends BaseProductModelService {
|
|||||||
spaceModel: SpaceModelEntity,
|
spaceModel: SpaceModelEntity,
|
||||||
subspaceModel: SubspaceModelEntity,
|
subspaceModel: SubspaceModelEntity,
|
||||||
queryRunner: QueryRunner,
|
queryRunner: QueryRunner,
|
||||||
) {
|
): Promise<ProductModelInterface[]> {
|
||||||
try {
|
try {
|
||||||
if (!spaceProductModelDtos?.length) return;
|
if (!spaceProductModelDtos?.length) return;
|
||||||
|
|
||||||
@ -59,7 +69,7 @@ export class SubspaceProductModelService extends BaseProductModelService {
|
|||||||
return {
|
return {
|
||||||
productModel: savedModel,
|
productModel: savedModel,
|
||||||
productItemModels,
|
productItemModels,
|
||||||
};
|
} as ProductModelInterface;
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
return newProductModels;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user