mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-27 19:14:54 +00:00
178 lines
5.2 KiB
TypeScript
178 lines
5.2 KiB
TypeScript
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';
|
|
|
|
@CommandHandler(PropogateSubspaceCommand)
|
|
export class PropogateSubspaceHandler
|
|
implements ICommandHandler<PropogateSubspaceCommand>
|
|
{
|
|
private readonly logger = new Logger(PropogateSubspaceHandler.name);
|
|
|
|
constructor(
|
|
private readonly spaceRepository: SpaceRepository,
|
|
private readonly subspaceRepository: SubspaceRepository,
|
|
private readonly productRepository: SubspaceProductRepository,
|
|
private readonly productItemRepository: SubspaceProductItemRepository,
|
|
) {}
|
|
|
|
async execute(command: PropogateSubspaceCommand): Promise<void> {
|
|
try {
|
|
const newSubspaceModels = command.param?.new;
|
|
|
|
if (!newSubspaceModels || newSubspaceModels.length === 0) {
|
|
this.logger.warn('No new subspace models provided.');
|
|
return;
|
|
}
|
|
|
|
const spaceModelUuid =
|
|
newSubspaceModels[0]?.subspaceModel?.spaceModel?.uuid;
|
|
|
|
if (!spaceModelUuid) {
|
|
this.logger.error(
|
|
'Space model UUID is missing in the command parameters.',
|
|
);
|
|
return;
|
|
}
|
|
|
|
const spaces = await this.getSpacesByModel(spaceModelUuid);
|
|
|
|
if (spaces.length === 0) {
|
|
this.logger.warn(`No spaces found for model UUID: ${spaceModelUuid}`);
|
|
return;
|
|
}
|
|
|
|
await this.processSubspaces(newSubspaceModels, spaces);
|
|
} catch (error) {
|
|
this.logger.error(
|
|
'Error in PropogateSubspaceHandler execution',
|
|
error.stack,
|
|
);
|
|
}
|
|
}
|
|
|
|
private async processSubspaces(
|
|
newSubspaceModels: any[],
|
|
spaces: SpaceEntity[],
|
|
) {
|
|
for (const newSubspaceModel of newSubspaceModels) {
|
|
for (const space of spaces) {
|
|
try {
|
|
const subspace = await this.createSubspace(newSubspaceModel, space);
|
|
|
|
if (newSubspaceModel.productModels?.length > 0) {
|
|
await this.processProducts(
|
|
newSubspaceModel.productModels,
|
|
subspace,
|
|
);
|
|
}
|
|
} catch (error) {
|
|
this.logger.error(
|
|
`Failed to create subspace for space ID: ${space.uuid}`,
|
|
error.stack,
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private async createSubspace(newSubspaceModel: any, space: SpaceEntity) {
|
|
const subspace = this.subspaceRepository.create({
|
|
subspaceName: newSubspaceModel.subspaceModel.subspaceName,
|
|
space,
|
|
subSpaceModel: newSubspaceModel.subspaceModel,
|
|
});
|
|
|
|
const createdSubspace = await this.subspaceRepository.save(subspace);
|
|
this.logger.log(
|
|
`Subspace created for space ${space.uuid} with name ${createdSubspace.subspaceName}`,
|
|
);
|
|
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,
|
|
productCount: productModel.productModel.productCount,
|
|
model: productModel.productModel,
|
|
});
|
|
|
|
const createdSubspaceProduct =
|
|
await this.productRepository.save(subspaceProduct);
|
|
this.logger.log(
|
|
`Product added to subspace ${subspace.id} with count ${createdSubspaceProduct.productCount}`,
|
|
);
|
|
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({
|
|
where: {
|
|
spaceModel: { uuid },
|
|
},
|
|
});
|
|
} catch (error) {
|
|
this.logger.error(
|
|
`Failed to fetch spaces for model UUID: ${uuid}`,
|
|
error.stack,
|
|
);
|
|
throw error;
|
|
}
|
|
}
|
|
}
|