mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 16:34:55 +00:00
product item model
This commit is contained in:
@ -57,4 +57,23 @@ export abstract class BaseProductItemService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected async saveProductItems<T>(
|
||||||
|
productItems: T[],
|
||||||
|
targetRepository: any,
|
||||||
|
queryRunner: QueryRunner,
|
||||||
|
): Promise<T[]> {
|
||||||
|
try {
|
||||||
|
const savedItem = await queryRunner.manager.save(
|
||||||
|
targetRepository,
|
||||||
|
productItems,
|
||||||
|
);
|
||||||
|
return savedItem;
|
||||||
|
} catch (error) {
|
||||||
|
throw new HttpException(
|
||||||
|
error.message || 'An error occurred while creating product items.',
|
||||||
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -21,6 +21,12 @@ export interface IModifySubspaceModelInterface {
|
|||||||
delete?: IDeletedSubsaceModelInterface[];
|
delete?: IDeletedSubsaceModelInterface[];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface IModifiedProductItemsModelsInterface {
|
||||||
|
new?: SubspaceProductItemModelEntity[];
|
||||||
|
update?: SubspaceProductItemModelEntity[];
|
||||||
|
delete?: string[];
|
||||||
|
}
|
||||||
|
|
||||||
export interface IUpdateSubspaceModelInterface {
|
export interface IUpdateSubspaceModelInterface {
|
||||||
subspaceName?: string;
|
subspaceName?: string;
|
||||||
uuid: string;
|
uuid: string;
|
||||||
|
|||||||
@ -7,7 +7,13 @@ import {
|
|||||||
SubspaceProductModelEntity,
|
SubspaceProductModelEntity,
|
||||||
} from '@app/common/modules/space-model';
|
} from '@app/common/modules/space-model';
|
||||||
import { BaseProductItemService } from '../../common';
|
import { BaseProductItemService } from '../../common';
|
||||||
import { CreateProductItemModelDto } from '../../dtos';
|
import {
|
||||||
|
CreateProductItemModelDto,
|
||||||
|
DeleteProductItemModelDto,
|
||||||
|
ProductItemModelModificationDto,
|
||||||
|
UpdateProductItemModelDto,
|
||||||
|
} from '../../dtos';
|
||||||
|
import { IModifiedProductItemsModelsInterface } from 'src/space-model/interfaces';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class SubspaceProductItemModelService extends BaseProductItemService {
|
export class SubspaceProductItemModelService extends BaseProductItemService {
|
||||||
@ -17,7 +23,7 @@ export class SubspaceProductItemModelService extends BaseProductItemService {
|
|||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
async createProdutItemModel(
|
async createProductItemModel(
|
||||||
itemModelDtos: CreateProductItemModelDto[],
|
itemModelDtos: CreateProductItemModelDto[],
|
||||||
subspaceProductModel: SubspaceProductModelEntity,
|
subspaceProductModel: SubspaceProductModelEntity,
|
||||||
spaceModel: SpaceModelEntity,
|
spaceModel: SpaceModelEntity,
|
||||||
@ -29,7 +35,9 @@ export class SubspaceProductItemModelService extends BaseProductItemService {
|
|||||||
HttpStatus.BAD_REQUEST,
|
HttpStatus.BAD_REQUEST,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
await this.validateTags(itemModelDtos, queryRunner, spaceModel);
|
await this.validateTags(itemModelDtos, queryRunner, spaceModel);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const productItems = itemModelDtos.map((dto) =>
|
const productItems = itemModelDtos.map((dto) =>
|
||||||
queryRunner.manager.create(this.subspaceProductItemRepository.target, {
|
queryRunner.manager.create(this.subspaceProductItemRepository.target, {
|
||||||
@ -37,19 +45,131 @@ export class SubspaceProductItemModelService extends BaseProductItemService {
|
|||||||
subspaceProductModel,
|
subspaceProductModel,
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
return await this.saveProductItems(
|
||||||
await queryRunner.manager.save(productItems);
|
productItems,
|
||||||
return productItems;
|
this.subspaceProductItemRepository,
|
||||||
|
queryRunner,
|
||||||
|
);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error instanceof HttpException) {
|
this.handleException(
|
||||||
throw error;
|
error,
|
||||||
}
|
'An unexpected error occurred while creating product items.',
|
||||||
|
|
||||||
throw new HttpException(
|
|
||||||
error.message ||
|
|
||||||
'An unexpected error occurred while creating product items.',
|
|
||||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async updateProductItemModel(
|
||||||
|
dtos: UpdateProductItemModelDto[],
|
||||||
|
spaceModel: SpaceModelEntity,
|
||||||
|
queryRunner: QueryRunner,
|
||||||
|
): Promise<SubspaceProductItemModelEntity[]> {
|
||||||
|
try {
|
||||||
|
await this.validateTags(dtos, queryRunner, spaceModel);
|
||||||
|
|
||||||
|
const productItemModels = await Promise.all(
|
||||||
|
dtos.map(async (dto) => {
|
||||||
|
const productItemModel = await this.findOne(dto.productModelUuid);
|
||||||
|
productItemModel.tag = dto.tag;
|
||||||
|
return productItemModel;
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
return (await this.saveProductItems(
|
||||||
|
productItemModels,
|
||||||
|
this.subspaceProductItemRepository,
|
||||||
|
queryRunner,
|
||||||
|
)) as SubspaceProductItemModelEntity[];
|
||||||
|
} catch (error) {
|
||||||
|
this.handleException(
|
||||||
|
error,
|
||||||
|
'Failed to save Subspace Product Item Model.',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async deleteProductModel(
|
||||||
|
dtos: DeleteProductItemModelDto[],
|
||||||
|
queryRunner: QueryRunner,
|
||||||
|
): Promise<string[]> {
|
||||||
|
try {
|
||||||
|
const productItemModels = await Promise.all(
|
||||||
|
dtos.map(async (dto) => {
|
||||||
|
const productItemModel = await this.findOne(dto.productModelUuid);
|
||||||
|
productItemModel.disabled = true;
|
||||||
|
return productItemModel;
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
const response = (await this.saveProductItems(
|
||||||
|
productItemModels,
|
||||||
|
this.subspaceProductItemRepository,
|
||||||
|
queryRunner,
|
||||||
|
)) as SubspaceProductItemModelEntity[];
|
||||||
|
|
||||||
|
return response.map((item) => item.uuid);
|
||||||
|
} catch (error) {
|
||||||
|
this.handleException(error, 'Failed to modify SpaceModels.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async findOne(uuid: string): Promise<SubspaceProductItemModelEntity> {
|
||||||
|
const productItemModel = await this.subspaceProductItemRepository.findOne({
|
||||||
|
where: { uuid },
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!productItemModel) {
|
||||||
|
throw new HttpException(
|
||||||
|
`Product item model not found for ${uuid}`,
|
||||||
|
HttpStatus.NOT_FOUND,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return productItemModel;
|
||||||
|
}
|
||||||
|
|
||||||
|
async modifyProductItemModel(
|
||||||
|
dto: ProductItemModelModificationDto,
|
||||||
|
productModel: SubspaceProductModelEntity,
|
||||||
|
spaceModel: SpaceModelEntity,
|
||||||
|
queryRunner: QueryRunner,
|
||||||
|
): Promise<IModifiedProductItemsModelsInterface> {
|
||||||
|
const productItemModels: IModifiedProductItemsModelsInterface = {};
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (dto.add) {
|
||||||
|
productItemModels.new = await this.createProductItemModel(
|
||||||
|
dto.add,
|
||||||
|
productModel,
|
||||||
|
spaceModel,
|
||||||
|
queryRunner,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (dto.update) {
|
||||||
|
productItemModels.update = await this.updateProductItemModel(
|
||||||
|
dto.update,
|
||||||
|
spaceModel,
|
||||||
|
queryRunner,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (dto.delete) {
|
||||||
|
productItemModels.delete = await this.deleteProductModel(
|
||||||
|
dto.delete,
|
||||||
|
queryRunner,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return productItemModels;
|
||||||
|
} catch (error) {
|
||||||
|
this.handleException(error, 'Failed to modify SpaceModels.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private handleException(error: unknown, defaultMessage: string): never {
|
||||||
|
if (error instanceof HttpException) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
throw new HttpException(
|
||||||
|
(error as Error).message || defaultMessage,
|
||||||
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -50,7 +50,7 @@ export class SubspaceProductModelService extends BaseProductModelService {
|
|||||||
spaceProductModelDtos.map(async (dto, index) => {
|
spaceProductModelDtos.map(async (dto, index) => {
|
||||||
const savedModel = savedProductModels[index];
|
const savedModel = savedProductModels[index];
|
||||||
const productItemModels =
|
const productItemModels =
|
||||||
await this.subspaceProductItemModelService.createProdutItemModel(
|
await this.subspaceProductItemModelService.createProductItemModel(
|
||||||
dto.items,
|
dto.items,
|
||||||
savedModel,
|
savedModel,
|
||||||
spaceModel,
|
spaceModel,
|
||||||
|
|||||||
Reference in New Issue
Block a user