fixed delete subspace model propagation

This commit is contained in:
hannathkadher
2025-03-11 01:25:27 +04:00
parent aa609266c7
commit 9122137922
3 changed files with 77 additions and 25 deletions

View File

@ -13,7 +13,7 @@ import {
SubspaceModelProductAllocationRepoitory,
TagModel,
} from '@app/common/modules/space-model';
import { DataSource, QueryRunner } from 'typeorm';
import { DataSource, In, QueryRunner } from 'typeorm';
import { SubSpaceService } from 'src/space/services';
import { TagService } from 'src/space/services/tag';
import {
@ -96,33 +96,59 @@ export class PropogateUpdateSpaceModelHandler
) {
const subspaces = await this.subspaceRepository.find({
where: {
subSpaceModel: {
uuid: subspaceModel.subspaceModel.uuid,
},
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,
});
}
if (!subspaces.length) return;
const allocationUuidsToRemove = subspaces.flatMap((subspace) =>
subspace.productAllocations.map((allocation) => allocation.uuid),
);
if (allocationUuidsToRemove.length) {
await this.subspaceProductRepository.delete(allocationUuidsToRemove);
}
await this.subspaceRepository.update(
{ uuid: subspace.uuid },
{ uuid: In(subspaces.map((s) => s.uuid)) },
{ disabled: true },
);
const relocatedAllocations = subspaceModel.relocatedAllocations || [];
if (!relocatedAllocations.length) return;
for (const space of spaces) {
for (const { allocation, tags = [] } of relocatedAllocations) {
const spaceAllocation = await this.spaceProductRepository.findOne({
where: {
inheritedFromModel: { uuid: allocation.uuid },
space: { uuid: space.uuid },
},
relations: ['tags'],
});
if (spaceAllocation) {
if (tags.length) {
spaceAllocation.tags.push(...tags);
await this.spaceProductRepository.save(spaceAllocation);
}
} else {
const newSpaceAllocation = this.spaceProductRepository.create({
space,
inheritedFromModel: allocation,
tags: allocation.tags,
product: allocation.product,
});
await this.spaceProductRepository.save(newSpaceAllocation);
}
}
}
}

View File

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

View File

@ -13,7 +13,10 @@ import { ModifyAction } from '@app/common/constants/modify-action.enum';
import { ProcessTagDto } from 'src/tags/dtos';
import { TagService } from 'src/tags/services';
import { SubspaceModelProductAllocationService } from './subspace-model-product-allocation.service';
import { ISingleSubspaceModel } from 'src/space-model/interfaces';
import {
IRelocatedAllocation,
ISingleSubspaceModel,
} from 'src/space-model/interfaces';
import { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
import { SubSpaceService } from 'src/space/services/subspace';
@ -182,7 +185,7 @@ export class SubSpaceModelService {
): Promise<ISingleSubspaceModel[]> {
try {
if (!deleteDtos || deleteDtos.length === 0) {
return;
return [];
}
const deleteResults = [];
@ -209,6 +212,8 @@ export class SubSpaceModelService {
(subspace) => subspace.productAllocations,
);
const relocatedAllocationsMap = new Map<string, IRelocatedAllocation[]>();
if (allocationsToRemove.length > 0) {
const spaceAllocationsMap = new Map<
string,
@ -218,6 +223,7 @@ export class SubSpaceModelService {
for (const allocation of allocationsToRemove) {
const product = allocation.product;
const tags = allocation.tags;
const subspaceUuid = allocation.subspaceModel.uuid;
const spaceAllocationKey = `${spaceModel.uuid}-${product.uuid}`;
@ -237,7 +243,10 @@ export class SubSpaceModelService {
}
}
const movedToAlreadyExistingSpaceAllocations: IRelocatedAllocation[] =
[];
const spaceAllocation = spaceAllocationsMap.get(spaceAllocationKey);
if (spaceAllocation) {
const existingTagUuids = new Set(
spaceAllocation.tags.map((tag) => tag.uuid),
@ -247,6 +256,10 @@ export class SubSpaceModelService {
);
if (newTags.length > 0) {
movedToAlreadyExistingSpaceAllocations.push({
tags: newTags,
allocation: spaceAllocation,
});
spaceAllocation.tags.push(...newTags);
await queryRunner.manager.save(spaceAllocation);
}
@ -259,8 +272,21 @@ export class SubSpaceModelService {
tags: tags,
},
);
movedToAlreadyExistingSpaceAllocations.push({
allocation: newSpaceAllocation,
tags: tags,
});
await queryRunner.manager.save(newSpaceAllocation);
}
if (movedToAlreadyExistingSpaceAllocations.length > 0) {
if (!relocatedAllocationsMap.has(subspaceUuid)) {
relocatedAllocationsMap.set(subspaceUuid, []);
}
relocatedAllocationsMap
.get(subspaceUuid)
.push(...movedToAlreadyExistingSpaceAllocations);
}
}
await queryRunner.manager.remove(
@ -289,7 +315,7 @@ export class SubSpaceModelService {
return subspaces.map((subspace) => ({
subspaceModel: subspace,
action: ModifyAction.DELETE,
tags: [],
relocatedAllocations: relocatedAllocationsMap.get(subspace.uuid) || [],
}));
} catch (error) {
if (error instanceof QueryFailedError) {