update add handler

This commit is contained in:
hannathkadher
2025-03-10 12:07:57 +04:00
parent 283e9406b6
commit 5e9de023ac

View File

@ -1,8 +1,15 @@
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs'; import { CommandHandler, ICommandHandler } from '@nestjs/cqrs';
import { PropogateUpdateSpaceModelCommand } from '../commands'; import { PropogateUpdateSpaceModelCommand } from '../commands';
import { SpaceRepository } from '@app/common/modules/space'; import { SpaceRepository } from '@app/common/modules/space';
import { SubspaceRepository } from '@app/common/modules/space/repositories/subspace.repository'; import {
import { SpaceModelEntity, TagModel } from '@app/common/modules/space-model'; 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 { DataSource, QueryRunner } from 'typeorm';
import { SubSpaceService } from 'src/space/services'; import { SubSpaceService } from 'src/space/services';
import { TagService } from 'src/space/services/tag'; import { TagService } from 'src/space/services/tag';
@ -23,17 +30,15 @@ export class PropogateUpdateSpaceModelHandler
private readonly dataSource: DataSource, private readonly dataSource: DataSource,
private readonly subSpaceService: SubSpaceService, private readonly subSpaceService: SubSpaceService,
private readonly tagService: TagService, private readonly tagService: TagService,
private readonly subspaceModelProductRepository: SubspaceModelProductAllocationRepoitory,
private readonly subspaceProductRepository: SubspaceProductAllocationRepository,
) {} ) {}
async execute(command: PropogateUpdateSpaceModelCommand): Promise<void> { async execute(command: PropogateUpdateSpaceModelCommand): Promise<void> {
console.log("propagate"); const { subspaceModels, spaces } = command.param;
const { spaceModel, subspaceModels, spaces } = command.param;
if (!subspaceModels || subspaceModels.length === 0) return; if (!subspaceModels || subspaceModels.length === 0) return;
if (!spaces || spaces.length === 0) return;
if (!spaces) {
return;
}
for (const subspaceModel of subspaceModels) { for (const subspaceModel of subspaceModels) {
if (subspaceModel.action === ModifyAction.ADD) { if (subspaceModel.action === ModifyAction.ADD) {
@ -46,7 +51,37 @@ export class PropogateUpdateSpaceModelHandler
subspaceModel: ISingleSubspaceModel, subspaceModel: ISingleSubspaceModel,
spaces: SpaceEntity[], 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( async updateSubspaceModels(