From 5e9de023acbdd06bb0364a956dfc8553b34bf28a Mon Sep 17 00:00:00 2001 From: hannathkadher Date: Mon, 10 Mar 2025 12:07:57 +0400 Subject: [PATCH] update add handler --- .../handlers/propate-subspace-handler.ts | 53 +++++++++++++++---- 1 file changed, 44 insertions(+), 9 deletions(-) diff --git a/src/space-model/handlers/propate-subspace-handler.ts b/src/space-model/handlers/propate-subspace-handler.ts index 2bef42a..64fc750 100644 --- a/src/space-model/handlers/propate-subspace-handler.ts +++ b/src/space-model/handlers/propate-subspace-handler.ts @@ -1,8 +1,15 @@ import { CommandHandler, ICommandHandler } from '@nestjs/cqrs'; import { PropogateUpdateSpaceModelCommand } from '../commands'; import { SpaceRepository } from '@app/common/modules/space'; -import { SubspaceRepository } from '@app/common/modules/space/repositories/subspace.repository'; -import { SpaceModelEntity, TagModel } from '@app/common/modules/space-model'; +import { + SubspaceProductAllocationRepository, + SubspaceRepository, +} from '@app/common/modules/space/repositories/subspace.repository'; +import { + SpaceModelEntity, + SubspaceModelProductAllocationRepoitory, + TagModel, +} from '@app/common/modules/space-model'; import { DataSource, QueryRunner } from 'typeorm'; import { SubSpaceService } from 'src/space/services'; import { TagService } from 'src/space/services/tag'; @@ -23,17 +30,15 @@ export class PropogateUpdateSpaceModelHandler private readonly dataSource: DataSource, private readonly subSpaceService: SubSpaceService, private readonly tagService: TagService, + private readonly subspaceModelProductRepository: SubspaceModelProductAllocationRepoitory, + private readonly subspaceProductRepository: SubspaceProductAllocationRepository, ) {} async execute(command: PropogateUpdateSpaceModelCommand): Promise { - console.log("propagate"); - const { spaceModel, subspaceModels, spaces } = command.param; + const { subspaceModels, spaces } = command.param; if (!subspaceModels || subspaceModels.length === 0) return; - - if (!spaces) { - return; - } + if (!spaces || spaces.length === 0) return; for (const subspaceModel of subspaceModels) { if (subspaceModel.action === ModifyAction.ADD) { @@ -46,7 +51,37 @@ export class PropogateUpdateSpaceModelHandler subspaceModel: ISingleSubspaceModel, spaces: SpaceEntity[], ) { - console.log(`subspace is ${JSON.stringify(subspaceModel)}`); + const subspaceModelAllocations = + await this.subspaceModelProductRepository.find({ + where: { + subspaceModel: { + uuid: subspaceModel.subspaceModel.uuid, + }, + }, + relations: ['tags', 'product'], + }); + + for (const space of spaces) { + const subspace = this.subspaceRepository.create({ + subspaceName: subspaceModel.subspaceModel.subspaceName, + space: space, + }); + + subspace.subSpaceModel = subspaceModel.subspaceModel; + await this.subspaceRepository.save(subspace); + + if (subspaceModelAllocations?.length > 0) { + for (const allocation of subspaceModelAllocations) { + const subspaceAllocation = this.subspaceProductRepository.create({ + subspace: subspace, + product: allocation.product, + tags: allocation.tags, + inheritedFromModel: allocation, + }); + await this.subspaceProductRepository.save(subspaceAllocation); + } + } + } } async updateSubspaceModels(