mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 18:14:54 +00:00
service clean up
This commit is contained in:
@ -2,11 +2,7 @@ import { CommandHandler, ICommandHandler } from '@nestjs/cqrs';
|
||||
import { PropogateSubspaceCommand } from '../commands';
|
||||
import { Logger } from '@nestjs/common';
|
||||
import { SpaceEntity, SpaceRepository } from '@app/common/modules/space';
|
||||
import {
|
||||
SubspaceProductItemRepository,
|
||||
SubspaceProductRepository,
|
||||
SubspaceRepository,
|
||||
} from '@app/common/modules/space/repositories/subspace.repository';
|
||||
import { SubspaceRepository } from '@app/common/modules/space/repositories/subspace.repository';
|
||||
import {
|
||||
IDeletedSubsaceModelInterface,
|
||||
IUpdateSubspaceModelInterface,
|
||||
@ -21,8 +17,6 @@ export class PropogateSubspaceHandler
|
||||
constructor(
|
||||
private readonly spaceRepository: SpaceRepository,
|
||||
private readonly subspaceRepository: SubspaceRepository,
|
||||
private readonly productRepository: SubspaceProductRepository,
|
||||
private readonly productItemRepository: SubspaceProductItemRepository,
|
||||
) {}
|
||||
|
||||
async execute(command: PropogateSubspaceCommand): Promise<void> {
|
||||
@ -130,13 +124,9 @@ export class PropogateSubspaceHandler
|
||||
for (const newSubspaceModel of newSubspaceModels) {
|
||||
for (const space of spaces) {
|
||||
try {
|
||||
const subspace = await this.createSubspace(newSubspaceModel, space);
|
||||
await this.createSubspace(newSubspaceModel, space);
|
||||
|
||||
if (newSubspaceModel.productModels?.length > 0) {
|
||||
await this.processProducts(
|
||||
newSubspaceModel.productModels,
|
||||
subspace,
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
this.logger.error(
|
||||
@ -162,69 +152,6 @@ export class PropogateSubspaceHandler
|
||||
return createdSubspace;
|
||||
}
|
||||
|
||||
private async processProducts(productModels: any[], subspace: any) {
|
||||
for (const productModel of productModels) {
|
||||
try {
|
||||
const subspaceProduct = await this.createSubspaceProduct(
|
||||
productModel,
|
||||
subspace,
|
||||
);
|
||||
|
||||
if (productModel.productItemModels?.length > 0) {
|
||||
await this.processProductItems(
|
||||
productModel.productItemModels,
|
||||
subspaceProduct,
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
this.logger.error(
|
||||
`Failed to create product for subspace ID: ${subspace.id}`,
|
||||
error.stack,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async createSubspaceProduct(productModel: any, subspace: any) {
|
||||
const subspaceProduct = this.productRepository.create({
|
||||
product: productModel.productModel.product,
|
||||
subspace,
|
||||
model: productModel.productModel,
|
||||
});
|
||||
|
||||
const createdSubspaceProduct =
|
||||
await this.productRepository.save(subspaceProduct);
|
||||
this.logger.log(
|
||||
`Product added to subspace ${subspace.id} with count ${createdSubspaceProduct.items.length}`,
|
||||
);
|
||||
return createdSubspaceProduct;
|
||||
}
|
||||
|
||||
private async processProductItems(
|
||||
productItemModels: any[],
|
||||
subspaceProduct: any,
|
||||
) {
|
||||
for (const productItemModel of productItemModels) {
|
||||
try {
|
||||
const subspaceProductItem = this.productItemRepository.create({
|
||||
tag: productItemModel.tag,
|
||||
subspaceProduct,
|
||||
model: productItemModel,
|
||||
});
|
||||
|
||||
await this.productItemRepository.save(subspaceProductItem);
|
||||
this.logger.log(
|
||||
`Product item added to subspace product ${subspaceProduct.id} with tag ${subspaceProductItem.tag}`,
|
||||
);
|
||||
} catch (error) {
|
||||
this.logger.error(
|
||||
`Failed to create product item for subspace product ID: ${subspaceProduct.id}`,
|
||||
error.stack,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async getSpacesByModel(uuid: string): Promise<SpaceEntity[]> {
|
||||
try {
|
||||
return await this.spaceRepository.find({
|
||||
|
||||
@ -1,18 +1,10 @@
|
||||
import {
|
||||
SubspaceModelEntity,
|
||||
SubspaceProductItemModelEntity,
|
||||
SubspaceProductModelEntity,
|
||||
} from '@app/common/modules/space-model';
|
||||
import { SubspaceModelEntity } from '@app/common/modules/space-model';
|
||||
|
||||
export interface AddSubspaceModelInterface {
|
||||
subspaceModel: SubspaceModelEntity;
|
||||
productModels: ProductModelInterface[];
|
||||
}
|
||||
|
||||
export interface ProductModelInterface {
|
||||
productModel: SubspaceProductModelEntity;
|
||||
productItemModels: SubspaceProductItemModelEntity[];
|
||||
}
|
||||
export interface ProductModelInterface {}
|
||||
|
||||
export interface IModifySubspaceModelInterface {
|
||||
spaceModelUuid: string;
|
||||
@ -22,8 +14,6 @@ export interface IModifySubspaceModelInterface {
|
||||
}
|
||||
|
||||
export interface IModifiedProductItemsModelsInterface {
|
||||
new?: SubspaceProductItemModelEntity[];
|
||||
update?: SubspaceProductItemModelEntity[];
|
||||
delete?: string[];
|
||||
}
|
||||
|
||||
|
||||
@ -1,4 +1,2 @@
|
||||
export * from './space-model.service';
|
||||
export * from './space-product-item-model.service';
|
||||
export * from './space-product-model.service';
|
||||
export * from './subspace';
|
||||
|
||||
@ -7,7 +7,6 @@ import { CreateSpaceModelDto, UpdateSpaceModelDto } from '../dtos';
|
||||
import { ProjectParam } from 'src/community/dtos';
|
||||
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
|
||||
import { SubSpaceModelService } from './subspace/subspace-model.service';
|
||||
import { SpaceProductModelService } from './space-product-model.service';
|
||||
import { DataSource } from 'typeorm';
|
||||
import {
|
||||
TypeORMCustomModel,
|
||||
@ -29,7 +28,6 @@ export class SpaceModelService {
|
||||
private readonly spaceModelRepository: SpaceModelRepository,
|
||||
private readonly projectService: ProjectService,
|
||||
private readonly subSpaceModelService: SubSpaceModelService,
|
||||
private readonly spaceProductModelService: SpaceProductModelService,
|
||||
private commandBus: CommandBus,
|
||||
) {}
|
||||
|
||||
@ -37,8 +35,7 @@ export class SpaceModelService {
|
||||
createSpaceModelDto: CreateSpaceModelDto,
|
||||
params: ProjectParam,
|
||||
) {
|
||||
const { modelName, subspaceModels, spaceProductModels } =
|
||||
createSpaceModelDto;
|
||||
const { modelName, subspaceModels } = createSpaceModelDto;
|
||||
const project = await this.validateProject(params.projectUuid);
|
||||
|
||||
const queryRunner = this.dataSource.createQueryRunner();
|
||||
@ -72,13 +69,6 @@ export class SpaceModelService {
|
||||
);
|
||||
}
|
||||
|
||||
if (spaceProductModels) {
|
||||
await this.spaceProductModelService.createSpaceProductModels(
|
||||
spaceProductModels,
|
||||
savedSpaceModel,
|
||||
queryRunner,
|
||||
);
|
||||
}
|
||||
await queryRunner.commitTransaction();
|
||||
|
||||
return new SuccessResponseDto({
|
||||
|
||||
@ -1,47 +0,0 @@
|
||||
import {
|
||||
SpaceModelEntity,
|
||||
SpaceProductItemModelRepository,
|
||||
SpaceProductModelEntity,
|
||||
} from '@app/common/modules/space-model';
|
||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||
import { CreateProductItemModelDto } from '../dtos';
|
||||
import { QueryRunner } from 'typeorm';
|
||||
import { BaseProductItemService } from '../common';
|
||||
|
||||
@Injectable()
|
||||
export class SpaceProductItemModelService extends BaseProductItemService {
|
||||
constructor(
|
||||
private readonly spaceProductItemRepository: SpaceProductItemModelRepository,
|
||||
) {
|
||||
super();
|
||||
}
|
||||
|
||||
async createProdutItemModel(
|
||||
itemModelDtos: CreateProductItemModelDto[],
|
||||
spaceProductModel: SpaceProductModelEntity,
|
||||
spaceModel: SpaceModelEntity,
|
||||
queryRunner: QueryRunner,
|
||||
) {
|
||||
await this.validateTags(itemModelDtos, queryRunner, spaceModel);
|
||||
try {
|
||||
const productItems = itemModelDtos.map((dto) =>
|
||||
queryRunner.manager.create(this.spaceProductItemRepository.target, {
|
||||
tag: dto.tag,
|
||||
spaceProductModel,
|
||||
}),
|
||||
);
|
||||
|
||||
await queryRunner.manager.save(productItems);
|
||||
} catch (error) {
|
||||
if (error instanceof HttpException) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
throw new HttpException(
|
||||
error.message ||
|
||||
'An unexpected error occurred while creating product items.',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,65 +0,0 @@
|
||||
import { QueryRunner } from 'typeorm';
|
||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||
import { CreateProductModelDto } from '../dtos';
|
||||
import { SpaceProductItemModelService } from './space-product-item-model.service';
|
||||
import { BaseProductModelService } from '../common';
|
||||
import { ProductService } from 'src/product/services';
|
||||
import {
|
||||
SpaceModelEntity,
|
||||
SpaceProductModelRepository,
|
||||
} from '@app/common/modules/space-model';
|
||||
|
||||
@Injectable()
|
||||
export class SpaceProductModelService extends BaseProductModelService {
|
||||
constructor(
|
||||
private readonly spaceProductModelRepository: SpaceProductModelRepository,
|
||||
private readonly spaceProductItemModelService: SpaceProductItemModelService,
|
||||
productService: ProductService,
|
||||
) {
|
||||
super(productService);
|
||||
}
|
||||
|
||||
async createSpaceProductModels(
|
||||
spaceProductModelDtos: CreateProductModelDto[],
|
||||
spaceModel: SpaceModelEntity,
|
||||
queryRunner: QueryRunner,
|
||||
) {
|
||||
try {
|
||||
const productModels = await Promise.all(
|
||||
spaceProductModelDtos.map(async (dto) => {
|
||||
const product = await this.getProduct(dto.productUuid);
|
||||
return queryRunner.manager.create(
|
||||
this.spaceProductModelRepository.target,
|
||||
{
|
||||
product,
|
||||
spaceModel,
|
||||
},
|
||||
);
|
||||
}),
|
||||
);
|
||||
|
||||
const savedProductModels = await queryRunner.manager.save(productModels);
|
||||
|
||||
await Promise.all(
|
||||
spaceProductModelDtos.map((dto, index) => {
|
||||
const savedModel = savedProductModels[index];
|
||||
return this.spaceProductItemModelService.createProdutItemModel(
|
||||
dto.items,
|
||||
savedModel,
|
||||
spaceModel,
|
||||
queryRunner,
|
||||
);
|
||||
}),
|
||||
);
|
||||
} catch (error) {
|
||||
if (error instanceof HttpException) {
|
||||
throw error;
|
||||
}
|
||||
throw new HttpException(
|
||||
error.message ||
|
||||
'An unexpected error occurred while creating product models.',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,3 +1 @@
|
||||
export * from './subspace-model.service';
|
||||
export * from './subspace-product-item-model.service';
|
||||
export * from './subspace-product-model.service';
|
||||
|
||||
@ -9,7 +9,6 @@ import {
|
||||
ModifySubspacesModelDto,
|
||||
} from '../../dtos';
|
||||
import { QueryRunner } from 'typeorm';
|
||||
import { SubspaceProductModelService } from './subspace-product-model.service';
|
||||
import {
|
||||
IDeletedSubsaceModelInterface,
|
||||
IModifySubspaceModelInterface,
|
||||
@ -21,7 +20,6 @@ import { DeleteSubspaceModelDto } from 'src/space-model/dtos/subspaces-model-dto
|
||||
export class SubSpaceModelService {
|
||||
constructor(
|
||||
private readonly subspaceModelRepository: SubspaceModelRepository,
|
||||
private readonly subSpaceProducetModelService: SubspaceProductModelService,
|
||||
) {}
|
||||
|
||||
async createSubSpaceModels(
|
||||
@ -44,16 +42,9 @@ export class SubSpaceModelService {
|
||||
const addedSubspaces = await Promise.all(
|
||||
subSpaceModelDtos.map(async (dto, index) => {
|
||||
const subspaceModel = newSubspaces[index];
|
||||
const productModels =
|
||||
await this.subSpaceProducetModelService.createSubspaceProductModels(
|
||||
dto.spaceProductModels,
|
||||
spaceModel,
|
||||
subspaceModel,
|
||||
queryRunner,
|
||||
);
|
||||
|
||||
return {
|
||||
subspaceModel,
|
||||
productModels,
|
||||
};
|
||||
}),
|
||||
);
|
||||
@ -103,15 +94,6 @@ export class SubSpaceModelService {
|
||||
updateResult.subspaceName = dto.subspaceName;
|
||||
}
|
||||
|
||||
if (dto.updatedProductModels) {
|
||||
await this.subSpaceProducetModelService.modifySubspaceProductModels(
|
||||
dto.updatedProductModels,
|
||||
spaceModel,
|
||||
subspaceModel,
|
||||
queryRunner,
|
||||
);
|
||||
}
|
||||
|
||||
updateResults.push(updateResult);
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
@ -168,13 +150,6 @@ export class SubSpaceModelService {
|
||||
{ disabled: true },
|
||||
);
|
||||
|
||||
if (subspaceModel.productModels.length > 0) {
|
||||
await this.subSpaceProducetModelService.disableProductModels(
|
||||
subspaceModel.productModels,
|
||||
queryRunner,
|
||||
);
|
||||
}
|
||||
|
||||
deleteResults.push({ uuid: dto.subspaceUuid });
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
|
||||
@ -1,187 +0,0 @@
|
||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||
import { QueryRunner } from 'typeorm';
|
||||
import {
|
||||
SpaceModelEntity,
|
||||
SubspaceProductItemModelEntity,
|
||||
SubspaceProductItemModelRepository,
|
||||
SubspaceProductModelEntity,
|
||||
} from '@app/common/modules/space-model';
|
||||
import { BaseProductItemService } from '../../common';
|
||||
import {
|
||||
CreateProductItemModelDto,
|
||||
DeleteProductItemModelDto,
|
||||
ProductItemModelModificationDto,
|
||||
UpdateProductItemModelDto,
|
||||
} from '../../dtos';
|
||||
import { IModifiedProductItemsModelsInterface } from 'src/space-model/interfaces';
|
||||
|
||||
@Injectable()
|
||||
export class SubspaceProductItemModelService extends BaseProductItemService {
|
||||
constructor(
|
||||
private readonly subspaceProductItemRepository: SubspaceProductItemModelRepository,
|
||||
) {
|
||||
super();
|
||||
}
|
||||
|
||||
async createProductItemModel(
|
||||
itemModelDtos: CreateProductItemModelDto[],
|
||||
subspaceProductModel: SubspaceProductModelEntity,
|
||||
spaceModel: SpaceModelEntity,
|
||||
queryRunner: QueryRunner,
|
||||
): Promise<SubspaceProductItemModelEntity[]> {
|
||||
if (!subspaceProductModel) {
|
||||
throw new HttpException(
|
||||
'The spaceProductModel parameter is required but was not provided.',
|
||||
HttpStatus.BAD_REQUEST,
|
||||
);
|
||||
}
|
||||
|
||||
await this.validateTags(itemModelDtos, queryRunner, spaceModel);
|
||||
|
||||
try {
|
||||
const productItems = itemModelDtos.map((dto) =>
|
||||
queryRunner.manager.create(this.subspaceProductItemRepository.target, {
|
||||
tag: dto.tag,
|
||||
subspaceProductModel,
|
||||
}),
|
||||
);
|
||||
return await this.saveProductItems(
|
||||
productItems,
|
||||
this.subspaceProductItemRepository,
|
||||
queryRunner,
|
||||
);
|
||||
} catch (error) {
|
||||
this.handleException(
|
||||
error,
|
||||
'An unexpected error occurred while creating product items.',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
if (dtos.length === 0) return;
|
||||
const productItemModels = await Promise.all(
|
||||
dtos.map(async (dto) => {
|
||||
const productItemModel = await this.findOne(dto.productModelUuid);
|
||||
productItemModel.disabled = true;
|
||||
return productItemModel;
|
||||
}),
|
||||
);
|
||||
|
||||
const disabledItemModels = await this.disableProductItemModels(
|
||||
productItemModels,
|
||||
queryRunner,
|
||||
);
|
||||
|
||||
return disabledItemModels.map((item) => item.uuid);
|
||||
} catch (error) {
|
||||
this.handleException(error, 'Failed to delete SpaceModels.');
|
||||
}
|
||||
}
|
||||
|
||||
async disableProductItemModels(
|
||||
productItemModels: SubspaceProductItemModelEntity[],
|
||||
queryRunner: QueryRunner,
|
||||
): Promise<SubspaceProductItemModelEntity[]> {
|
||||
productItemModels.forEach((model) => (model.disabled = true));
|
||||
return (await this.saveProductItems(
|
||||
productItemModels,
|
||||
this.subspaceProductItemRepository,
|
||||
queryRunner,
|
||||
)) as SubspaceProductItemModelEntity[];
|
||||
}
|
||||
|
||||
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 Product Item Models.');
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -1,239 +0,0 @@
|
||||
import {
|
||||
SpaceModelEntity,
|
||||
SubspaceModelEntity,
|
||||
SubspaceProductModelEntity,
|
||||
SubspaceProductModelRepository,
|
||||
} from '@app/common/modules/space-model';
|
||||
import { In, QueryRunner } from 'typeorm';
|
||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||
|
||||
import { SubspaceProductItemModelService } from './subspace-product-item-model.service';
|
||||
import {
|
||||
CreateProductModelDto,
|
||||
DeleteProductModelDto,
|
||||
ProductModelModificationDto,
|
||||
UpdateProductModelDto,
|
||||
} from '../../dtos';
|
||||
import { BaseProductModelService } from '../../common';
|
||||
import { ProductService } from 'src/product/services';
|
||||
import {
|
||||
IModifiedProductModelsInterface,
|
||||
IUpdatedProductModelInterface,
|
||||
ProductModelInterface,
|
||||
} from '../../interfaces';
|
||||
|
||||
@Injectable()
|
||||
export class SubspaceProductModelService extends BaseProductModelService {
|
||||
constructor(
|
||||
private readonly subpaceProductModelRepository: SubspaceProductModelRepository,
|
||||
productService: ProductService,
|
||||
private readonly subspaceProductItemModelService: SubspaceProductItemModelService,
|
||||
) {
|
||||
super(productService);
|
||||
}
|
||||
|
||||
async createSubspaceProductModels(
|
||||
spaceProductModelDtos: CreateProductModelDto[],
|
||||
spaceModel: SpaceModelEntity,
|
||||
subspaceModel: SubspaceModelEntity,
|
||||
queryRunner: QueryRunner,
|
||||
): Promise<ProductModelInterface[]> {
|
||||
try {
|
||||
if (!spaceProductModelDtos?.length) return;
|
||||
|
||||
const productModels = await Promise.all(
|
||||
spaceProductModelDtos.map(async (dto) => {
|
||||
const product = await this.getProduct(dto.productUuid);
|
||||
return queryRunner.manager.create(
|
||||
this.subpaceProductModelRepository.target,
|
||||
{
|
||||
product,
|
||||
subspaceModel,
|
||||
},
|
||||
);
|
||||
}),
|
||||
);
|
||||
|
||||
const savedProductModels = await queryRunner.manager.save(productModels);
|
||||
|
||||
const newProductModels = await Promise.all(
|
||||
spaceProductModelDtos.map(async (dto, index) => {
|
||||
const savedModel = savedProductModels[index];
|
||||
const productItemModels =
|
||||
await this.subspaceProductItemModelService.createProductItemModel(
|
||||
dto.items,
|
||||
savedModel,
|
||||
spaceModel,
|
||||
queryRunner,
|
||||
);
|
||||
return {
|
||||
productModel: savedModel,
|
||||
productItemModels,
|
||||
} as ProductModelInterface;
|
||||
}),
|
||||
);
|
||||
return newProductModels;
|
||||
} catch (error) {
|
||||
if (error instanceof HttpException) {
|
||||
throw error;
|
||||
}
|
||||
throw new HttpException(
|
||||
error.message ||
|
||||
'An unexpected error occurred while creating product models.',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async updateSubspaceProductModels(
|
||||
dtos: UpdateProductModelDto[],
|
||||
spaceModel: SpaceModelEntity,
|
||||
queryRunner: QueryRunner,
|
||||
): Promise<IUpdatedProductModelInterface[]> {
|
||||
try {
|
||||
const updatedProductModels = await Promise.all(
|
||||
dtos.map(async (dto) => {
|
||||
const productModel = await this.findOne(dto.productModelUuid);
|
||||
const productModifiedItemModel =
|
||||
await this.subspaceProductItemModelService.modifyProductItemModel(
|
||||
dto.items,
|
||||
productModel,
|
||||
spaceModel,
|
||||
queryRunner,
|
||||
);
|
||||
return {
|
||||
productModelUuid: productModel.uuid,
|
||||
productModifiedItemModel,
|
||||
};
|
||||
}),
|
||||
);
|
||||
|
||||
return updatedProductModels;
|
||||
} catch (error) {
|
||||
if (error instanceof HttpException) {
|
||||
throw error;
|
||||
}
|
||||
throw new HttpException(
|
||||
'Failed to update Subspace product model',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async removeSubspaceProductModels(
|
||||
deleteDtos: DeleteProductModelDto[],
|
||||
transactionRunner: QueryRunner,
|
||||
): Promise<string[]> {
|
||||
try {
|
||||
const productModels = await Promise.all(
|
||||
deleteDtos.map((dto) => this.findOne(dto.productModelUuid)),
|
||||
);
|
||||
|
||||
return await this.disableProductModels(productModels, transactionRunner);
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
'Failed to remove subspace product models',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async disableProductModels(
|
||||
productModels: SubspaceProductModelEntity[],
|
||||
transactionRunner: QueryRunner,
|
||||
): Promise<string[]> {
|
||||
try {
|
||||
const productModelUuids = productModels.map((model) => model.uuid);
|
||||
|
||||
await transactionRunner.manager.update(
|
||||
this.subpaceProductModelRepository.target,
|
||||
{ uuid: In(productModelUuids) },
|
||||
{ disabled: true },
|
||||
);
|
||||
|
||||
const itemModelDisables = productModels.map((model) =>
|
||||
model.itemModels.length > 0
|
||||
? this.subspaceProductItemModelService.disableProductItemModels(
|
||||
model.itemModels,
|
||||
transactionRunner,
|
||||
)
|
||||
: Promise.resolve(),
|
||||
);
|
||||
|
||||
await Promise.all(itemModelDisables);
|
||||
|
||||
return productModelUuids;
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
'Failed to disable product models',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async modifySubspaceProductModels(
|
||||
modificationDto: ProductModelModificationDto,
|
||||
spaceEntity: SpaceModelEntity,
|
||||
subspaceEntity: SubspaceModelEntity,
|
||||
transactionRunner: QueryRunner,
|
||||
): Promise<IModifiedProductModelsInterface> {
|
||||
const modifiedProductModels: IModifiedProductModelsInterface = {};
|
||||
|
||||
try {
|
||||
await Promise.all([
|
||||
modificationDto.add
|
||||
? this.createSubspaceProductModels(
|
||||
modificationDto.add,
|
||||
spaceEntity,
|
||||
subspaceEntity,
|
||||
transactionRunner,
|
||||
).then((addedModels) => {
|
||||
modifiedProductModels.add = addedModels;
|
||||
})
|
||||
: Promise.resolve(),
|
||||
modificationDto.update
|
||||
? this.updateSubspaceProductModels(
|
||||
modificationDto.update,
|
||||
spaceEntity,
|
||||
transactionRunner,
|
||||
).then((updatedModels) => {
|
||||
modifiedProductModels.update = updatedModels;
|
||||
})
|
||||
: Promise.resolve(),
|
||||
modificationDto.delete
|
||||
? this.removeSubspaceProductModels(
|
||||
modificationDto.delete,
|
||||
transactionRunner,
|
||||
).then((deletedModels) => {
|
||||
modifiedProductModels.delete = deletedModels;
|
||||
})
|
||||
: Promise.resolve(),
|
||||
]);
|
||||
|
||||
return modifiedProductModels;
|
||||
} catch (error) {
|
||||
if (error instanceof HttpException) {
|
||||
throw error;
|
||||
}
|
||||
throw new HttpException(
|
||||
'Failed to modify subspace product models',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async findOne(uuid: string): Promise<SubspaceProductModelEntity> {
|
||||
const productModel = await this.subpaceProductModelRepository.findOne({
|
||||
where: {
|
||||
uuid,
|
||||
},
|
||||
relations: ['itemModels'],
|
||||
});
|
||||
if (!productModel)
|
||||
throw new HttpException(
|
||||
`Subspace Product model with uuid ${uuid} not found`,
|
||||
HttpStatus.NOT_FOUND,
|
||||
);
|
||||
return productModel;
|
||||
}
|
||||
}
|
||||
@ -2,24 +2,13 @@ import { SpaceRepositoryModule } from '@app/common/modules/space/space.repositor
|
||||
import { Module } from '@nestjs/common';
|
||||
import { ConfigModule } from '@nestjs/config';
|
||||
import { SpaceModelController } from './controllers';
|
||||
import {
|
||||
SpaceModelService,
|
||||
SpaceProductItemModelService,
|
||||
SpaceProductModelService,
|
||||
SubSpaceModelService,
|
||||
SubspaceProductItemModelService,
|
||||
} from './services';
|
||||
import { SpaceModelService, SubSpaceModelService } from './services';
|
||||
import {
|
||||
SpaceModelRepository,
|
||||
SpaceProductItemModelRepository,
|
||||
SpaceProductModelRepository,
|
||||
SubspaceModelRepository,
|
||||
SubspaceProductItemModelRepository,
|
||||
SubspaceProductModelRepository,
|
||||
} from '@app/common/modules/space-model';
|
||||
import { ProjectRepository } from '@app/common/modules/project/repositiories';
|
||||
import { ProductRepository } from '@app/common/modules/product/repositories';
|
||||
import { SubspaceProductModelService } from './services/subspace/subspace-product-model.service';
|
||||
import { PropogateSubspaceHandler } from './handlers';
|
||||
import { CqrsModule } from '@nestjs/cqrs';
|
||||
import { SpaceRepository } from '@app/common/modules/space';
|
||||
@ -41,16 +30,8 @@ const CommandHandlers = [PropogateSubspaceHandler];
|
||||
SpaceRepository,
|
||||
ProjectRepository,
|
||||
SubSpaceModelService,
|
||||
SpaceProductModelService,
|
||||
SubspaceModelRepository,
|
||||
SpaceProductModelRepository,
|
||||
ProductRepository,
|
||||
SpaceProductItemModelService,
|
||||
SpaceProductItemModelRepository,
|
||||
SubspaceProductItemModelService,
|
||||
SubspaceProductItemModelRepository,
|
||||
SubspaceProductModelService,
|
||||
SubspaceProductModelRepository,
|
||||
SubspaceRepository,
|
||||
SubspaceProductRepository,
|
||||
SubspaceProductItemRepository,
|
||||
|
||||
@ -6,7 +6,6 @@ import {
|
||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||
import { CreateSpaceProductItemDto } from '../../dtos';
|
||||
import { QueryRunner } from 'typeorm';
|
||||
import { SpaceProductModelEntity } from '@app/common/modules/space-model';
|
||||
import { BaseProductItemService } from '../../common';
|
||||
|
||||
@Injectable()
|
||||
@ -54,34 +53,4 @@ export class SpaceProductItemService extends BaseProductItemService {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async createSpaceProductItemFromModel(
|
||||
spaceProduct: SpaceProductEntity,
|
||||
spaceProductModel: SpaceProductModelEntity,
|
||||
queryRunner: QueryRunner,
|
||||
) {
|
||||
const spaceProductItemModels = spaceProductModel.items;
|
||||
if (!spaceProductItemModels?.length) return;
|
||||
|
||||
try {
|
||||
const productItems = spaceProductItemModels.map((model) =>
|
||||
queryRunner.manager.create(this.spaceProductItemRepository.target, {
|
||||
tag: model.tag,
|
||||
spaceProduct,
|
||||
}),
|
||||
);
|
||||
|
||||
await queryRunner.manager.save(productItems);
|
||||
} catch (error) {
|
||||
if (error instanceof HttpException) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
throw new HttpException(
|
||||
error.message ||
|
||||
'An unexpected error occurred while creating product items.',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -5,7 +5,6 @@ import { SpaceProductEntity } from '@app/common/modules/space/entities/space-pro
|
||||
import { In, QueryRunner } from 'typeorm';
|
||||
import { ProductAssignmentDto } from '../../dtos';
|
||||
import { SpaceProductItemService } from '../space-product-items';
|
||||
import { SpaceModelEntity } from '@app/common/modules/space-model';
|
||||
import { ProductEntity } from '@app/common/modules/product/entities';
|
||||
import { ProductService } from 'src/product/services';
|
||||
|
||||
@ -17,39 +16,6 @@ export class SpaceProductService {
|
||||
private readonly productService: ProductService,
|
||||
) {}
|
||||
|
||||
async createFromModel(
|
||||
spaceModel: SpaceModelEntity,
|
||||
space: SpaceEntity,
|
||||
queryRunner: QueryRunner,
|
||||
) {
|
||||
const spaceProductModels = spaceModel.spaceProductModels;
|
||||
if (!spaceProductModels?.length) return;
|
||||
const newSpaceProducts = [];
|
||||
|
||||
spaceProductModels.map((spaceProductModel) => {
|
||||
newSpaceProducts.push(
|
||||
queryRunner.manager.create(SpaceProductEntity, {
|
||||
space: space,
|
||||
product: spaceProductModel.product,
|
||||
spaceProductModel: spaceProductModel,
|
||||
}),
|
||||
);
|
||||
});
|
||||
if (newSpaceProducts.length > 0) {
|
||||
await queryRunner.manager.save(SpaceProductEntity, newSpaceProducts);
|
||||
await Promise.all(
|
||||
newSpaceProducts.map((spaceProduct, index) => {
|
||||
const spaceProductModel = spaceProductModels[index];
|
||||
return this.spaceProductItemService.createSpaceProductItemFromModel(
|
||||
spaceProduct,
|
||||
spaceProductModel,
|
||||
queryRunner,
|
||||
);
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async assignProductsToSpace(
|
||||
space: SpaceEntity,
|
||||
products: ProductAssignmentDto[],
|
||||
|
||||
@ -108,12 +108,6 @@ export class SpaceService {
|
||||
products,
|
||||
queryRunner,
|
||||
);
|
||||
} else if (spaceModel && spaceModel.spaceProductModels.length) {
|
||||
await this.spaceProductService.createFromModel(
|
||||
spaceModel,
|
||||
newSpace,
|
||||
queryRunner,
|
||||
);
|
||||
}
|
||||
await queryRunner.commitTransaction();
|
||||
|
||||
|
||||
@ -6,10 +6,6 @@ import {
|
||||
SubspaceProductEntity,
|
||||
SubspaceProductItemEntity,
|
||||
} from '@app/common/modules/space';
|
||||
import {
|
||||
SubspaceProductItemModelEntity,
|
||||
SubspaceProductModelEntity,
|
||||
} from '@app/common/modules/space-model';
|
||||
import { SubspaceProductItemRepository } from '@app/common/modules/space/repositories/subspace.repository';
|
||||
import { CreateSpaceProductItemDto } from '../../dtos';
|
||||
import { BaseProductItemService } from '../../common';
|
||||
@ -22,44 +18,6 @@ export class SubspaceProductItemService extends BaseProductItemService {
|
||||
super();
|
||||
}
|
||||
|
||||
async createItemFromModel(
|
||||
product: SubspaceProductEntity,
|
||||
productModel: SubspaceProductModelEntity,
|
||||
queryRunner: QueryRunner,
|
||||
): Promise<void> {
|
||||
const itemModels = productModel.itemModels;
|
||||
|
||||
if (!itemModels?.length) return;
|
||||
|
||||
try {
|
||||
const productItems = itemModels.map((model) =>
|
||||
this.createProductItem(product, model, queryRunner),
|
||||
);
|
||||
|
||||
await queryRunner.manager.save(
|
||||
this.productItemRepository.target,
|
||||
productItems,
|
||||
);
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
error.message || 'An error occurred while creating product items.',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private createProductItem(
|
||||
product: SubspaceProductEntity,
|
||||
model: SubspaceProductItemModelEntity,
|
||||
queryRunner: QueryRunner,
|
||||
): Partial<any> {
|
||||
return queryRunner.manager.create(this.productItemRepository.target, {
|
||||
tag: model.tag,
|
||||
product,
|
||||
model,
|
||||
});
|
||||
}
|
||||
|
||||
async createItemFromDtos(
|
||||
product: SubspaceProductEntity,
|
||||
itemDto: CreateSpaceProductItemDto[],
|
||||
|
||||
@ -6,10 +6,6 @@ import {
|
||||
SubspaceEntity,
|
||||
SubspaceProductEntity,
|
||||
} from '@app/common/modules/space';
|
||||
import {
|
||||
SubspaceModelEntity,
|
||||
SubspaceProductModelEntity,
|
||||
} from '@app/common/modules/space-model';
|
||||
import { SubspaceProductItemService } from './subspace-product-item.service';
|
||||
import { ProductAssignmentDto } from 'src/space/dtos';
|
||||
import { ProductService } from 'src/product/services';
|
||||
@ -22,52 +18,6 @@ export class SubspaceProductService {
|
||||
private readonly productService: ProductService,
|
||||
) {}
|
||||
|
||||
async createFromModel(
|
||||
subspaceModel: SubspaceModelEntity,
|
||||
subspace: SubspaceEntity,
|
||||
queryRunner: QueryRunner,
|
||||
): Promise<void> {
|
||||
const productModels = subspaceModel.productModels;
|
||||
if (!productModels?.length) return;
|
||||
|
||||
try {
|
||||
const newSpaceProducts = productModels.map((productModel) =>
|
||||
this.createSubspaceProductEntity(subspace, productModel),
|
||||
);
|
||||
|
||||
const subspaceProducts = await queryRunner.manager.save(
|
||||
SubspaceProductEntity,
|
||||
newSpaceProducts,
|
||||
);
|
||||
|
||||
await Promise.all(
|
||||
subspaceProducts.map((subspaceProduct, index) =>
|
||||
this.subspaceProductItemService.createItemFromModel(
|
||||
subspaceProduct,
|
||||
productModels[index],
|
||||
queryRunner,
|
||||
),
|
||||
),
|
||||
);
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
`Transaction failed: Unable to create subspace products ${error}`,
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private createSubspaceProductEntity(
|
||||
subspace: SubspaceEntity,
|
||||
productModel: SubspaceProductModelEntity,
|
||||
): Partial<SubspaceProductEntity> {
|
||||
return {
|
||||
subspace,
|
||||
product: productModel.product,
|
||||
model: productModel,
|
||||
};
|
||||
}
|
||||
|
||||
async createFromDto(
|
||||
productDtos: ProductAssignmentDto[],
|
||||
subspace: SubspaceEntity,
|
||||
|
||||
@ -66,17 +66,7 @@ export class SubSpaceService {
|
||||
subSpaceModel,
|
||||
}));
|
||||
|
||||
const subspaces = await this.createSubspaces(subspaceData, queryRunner);
|
||||
|
||||
await Promise.all(
|
||||
subSpaceModels.map((model, index) => {
|
||||
this.productService.createFromModel(
|
||||
model,
|
||||
subspaces[index],
|
||||
queryRunner,
|
||||
);
|
||||
}),
|
||||
);
|
||||
await this.createSubspaces(subspaceData, queryRunner);
|
||||
}
|
||||
|
||||
async createSubspacesFromDto(
|
||||
|
||||
Reference in New Issue
Block a user