propagate

This commit is contained in:
hannathkadher
2025-03-10 15:13:04 +04:00
parent f6d47d0e39
commit aa609266c7
2 changed files with 53 additions and 2 deletions

View File

@ -1,6 +1,9 @@
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs';
import { PropogateUpdateSpaceModelCommand } from '../commands';
import { SpaceRepository } from '@app/common/modules/space';
import {
SpaceProductAllocationRepository,
SpaceRepository,
} from '@app/common/modules/space';
import {
SubspaceProductAllocationRepository,
SubspaceRepository,
@ -32,6 +35,7 @@ export class PropogateUpdateSpaceModelHandler
private readonly tagService: TagService,
private readonly subspaceModelProductRepository: SubspaceModelProductAllocationRepoitory,
private readonly subspaceProductRepository: SubspaceProductAllocationRepository,
private readonly spaceProductRepository: SpaceProductAllocationRepository,
) {}
async execute(command: PropogateUpdateSpaceModelCommand): Promise<void> {
@ -43,6 +47,8 @@ export class PropogateUpdateSpaceModelHandler
for (const subspaceModel of subspaceModels) {
if (subspaceModel.action === ModifyAction.ADD) {
await this.addSubspaceModel(subspaceModel, spaces);
} else if (subspaceModel.action === ModifyAction.DELETE) {
await this.deleteSubspaceModel(subspaceModel, spaces);
}
}
}
@ -84,6 +90,42 @@ export class PropogateUpdateSpaceModelHandler
}
}
async deleteSubspaceModel(
subspaceModel: ISingleSubspaceModel,
spaces: SpaceEntity[],
) {
const subspaces = await this.subspaceRepository.find({
where: {
subSpaceModel: {
uuid: subspaceModel.subspaceModel.uuid,
},
disabled: false,
},
relations: [
'productAllocations',
'productAllocations.product',
'productAllocations.tags',
'space',
],
});
if (!subspaces || subspaces.length === 0) return;
for (const subspace of subspaces) {
const allocationsToRemove = subspace.productAllocations;
if (allocationsToRemove.length > 0) {
for (const allocation of allocationsToRemove) {
await this.subspaceProductRepository.delete({
uuid: allocation.uuid,
});
}
}
await this.subspaceRepository.update(
{ uuid: subspace.uuid },
{ disabled: true },
);
}
}
async updateSubspaceModels(
subspaceModels: UpdatedSubspaceModelPayload[],
queryRunner: QueryRunner,

View File

@ -1,9 +1,18 @@
import { SubspaceModelEntity } from '@app/common/modules/space-model';
import {
SpaceModelProductAllocationEntity,
SubspaceModelEntity,
} from '@app/common/modules/space-model';
import { ModifyTagModelDto } from '../dtos';
import { ModifyAction } from '@app/common/constants/modify-action.enum';
import { NewTagEntity } from '@app/common/modules/tag';
export interface ISingleSubspaceModel {
subspaceModel: SubspaceModelEntity;
action: ModifyAction;
tags: ModifyTagModelDto[];
movedToNewSpaceAllocation?: SpaceModelProductAllocationEntity[];
movedToAlreadyExistingSpaceAllocation?: {
allocation: SpaceModelProductAllocationEntity;
tags: NewTagEntity[];
}[];
}