mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 08:14:55 +00:00
fixed delete subspace model propagation
This commit is contained in:
@ -13,7 +13,7 @@ import {
|
|||||||
SubspaceModelProductAllocationRepoitory,
|
SubspaceModelProductAllocationRepoitory,
|
||||||
TagModel,
|
TagModel,
|
||||||
} from '@app/common/modules/space-model';
|
} from '@app/common/modules/space-model';
|
||||||
import { DataSource, QueryRunner } from 'typeorm';
|
import { DataSource, In, 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';
|
||||||
import {
|
import {
|
||||||
@ -96,33 +96,59 @@ export class PropogateUpdateSpaceModelHandler
|
|||||||
) {
|
) {
|
||||||
const subspaces = await this.subspaceRepository.find({
|
const subspaces = await this.subspaceRepository.find({
|
||||||
where: {
|
where: {
|
||||||
subSpaceModel: {
|
subSpaceModel: { uuid: subspaceModel.subspaceModel.uuid },
|
||||||
uuid: subspaceModel.subspaceModel.uuid,
|
|
||||||
},
|
|
||||||
disabled: false,
|
disabled: false,
|
||||||
},
|
},
|
||||||
relations: [
|
relations: [
|
||||||
'productAllocations',
|
'productAllocations',
|
||||||
'productAllocations.product',
|
'productAllocations.product',
|
||||||
'productAllocations.tags',
|
'productAllocations.tags',
|
||||||
'space',
|
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
if (!subspaces || subspaces.length === 0) return;
|
|
||||||
|
|
||||||
for (const subspace of subspaces) {
|
if (!subspaces.length) return;
|
||||||
const allocationsToRemove = subspace.productAllocations;
|
|
||||||
if (allocationsToRemove.length > 0) {
|
const allocationUuidsToRemove = subspaces.flatMap((subspace) =>
|
||||||
for (const allocation of allocationsToRemove) {
|
subspace.productAllocations.map((allocation) => allocation.uuid),
|
||||||
await this.subspaceProductRepository.delete({
|
);
|
||||||
uuid: allocation.uuid,
|
|
||||||
|
if (allocationUuidsToRemove.length) {
|
||||||
|
await this.subspaceProductRepository.delete(allocationUuidsToRemove);
|
||||||
|
}
|
||||||
|
|
||||||
|
await this.subspaceRepository.update(
|
||||||
|
{ 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
await this.subspaceRepository.update(
|
|
||||||
{ uuid: subspace.uuid },
|
|
||||||
{ disabled: true },
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -6,13 +6,13 @@ import { ModifyTagModelDto } from '../dtos';
|
|||||||
import { ModifyAction } from '@app/common/constants/modify-action.enum';
|
import { ModifyAction } from '@app/common/constants/modify-action.enum';
|
||||||
import { NewTagEntity } from '@app/common/modules/tag';
|
import { NewTagEntity } from '@app/common/modules/tag';
|
||||||
|
|
||||||
|
export interface IRelocatedAllocation {
|
||||||
|
allocation: SpaceModelProductAllocationEntity;
|
||||||
|
tags?: NewTagEntity[];
|
||||||
|
}
|
||||||
export interface ISingleSubspaceModel {
|
export interface ISingleSubspaceModel {
|
||||||
subspaceModel: SubspaceModelEntity;
|
subspaceModel: SubspaceModelEntity;
|
||||||
action: ModifyAction;
|
action: ModifyAction;
|
||||||
tags: ModifyTagModelDto[];
|
tags?: ModifyTagModelDto[];
|
||||||
movedToNewSpaceAllocation?: SpaceModelProductAllocationEntity[];
|
relocatedAllocations?: IRelocatedAllocation[];
|
||||||
movedToAlreadyExistingSpaceAllocation?: {
|
|
||||||
allocation: SpaceModelProductAllocationEntity;
|
|
||||||
tags: NewTagEntity[];
|
|
||||||
}[];
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -13,7 +13,10 @@ import { ModifyAction } from '@app/common/constants/modify-action.enum';
|
|||||||
import { ProcessTagDto } from 'src/tags/dtos';
|
import { ProcessTagDto } from 'src/tags/dtos';
|
||||||
import { TagService } from 'src/tags/services';
|
import { TagService } from 'src/tags/services';
|
||||||
import { SubspaceModelProductAllocationService } from './subspace-model-product-allocation.service';
|
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 { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
|
||||||
import { SubSpaceService } from 'src/space/services/subspace';
|
import { SubSpaceService } from 'src/space/services/subspace';
|
||||||
|
|
||||||
@ -182,7 +185,7 @@ export class SubSpaceModelService {
|
|||||||
): Promise<ISingleSubspaceModel[]> {
|
): Promise<ISingleSubspaceModel[]> {
|
||||||
try {
|
try {
|
||||||
if (!deleteDtos || deleteDtos.length === 0) {
|
if (!deleteDtos || deleteDtos.length === 0) {
|
||||||
return;
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
const deleteResults = [];
|
const deleteResults = [];
|
||||||
@ -209,6 +212,8 @@ export class SubSpaceModelService {
|
|||||||
(subspace) => subspace.productAllocations,
|
(subspace) => subspace.productAllocations,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const relocatedAllocationsMap = new Map<string, IRelocatedAllocation[]>();
|
||||||
|
|
||||||
if (allocationsToRemove.length > 0) {
|
if (allocationsToRemove.length > 0) {
|
||||||
const spaceAllocationsMap = new Map<
|
const spaceAllocationsMap = new Map<
|
||||||
string,
|
string,
|
||||||
@ -218,6 +223,7 @@ export class SubSpaceModelService {
|
|||||||
for (const allocation of allocationsToRemove) {
|
for (const allocation of allocationsToRemove) {
|
||||||
const product = allocation.product;
|
const product = allocation.product;
|
||||||
const tags = allocation.tags;
|
const tags = allocation.tags;
|
||||||
|
const subspaceUuid = allocation.subspaceModel.uuid;
|
||||||
|
|
||||||
const spaceAllocationKey = `${spaceModel.uuid}-${product.uuid}`;
|
const spaceAllocationKey = `${spaceModel.uuid}-${product.uuid}`;
|
||||||
|
|
||||||
@ -237,7 +243,10 @@ export class SubSpaceModelService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const movedToAlreadyExistingSpaceAllocations: IRelocatedAllocation[] =
|
||||||
|
[];
|
||||||
const spaceAllocation = spaceAllocationsMap.get(spaceAllocationKey);
|
const spaceAllocation = spaceAllocationsMap.get(spaceAllocationKey);
|
||||||
|
|
||||||
if (spaceAllocation) {
|
if (spaceAllocation) {
|
||||||
const existingTagUuids = new Set(
|
const existingTagUuids = new Set(
|
||||||
spaceAllocation.tags.map((tag) => tag.uuid),
|
spaceAllocation.tags.map((tag) => tag.uuid),
|
||||||
@ -247,6 +256,10 @@ export class SubSpaceModelService {
|
|||||||
);
|
);
|
||||||
|
|
||||||
if (newTags.length > 0) {
|
if (newTags.length > 0) {
|
||||||
|
movedToAlreadyExistingSpaceAllocations.push({
|
||||||
|
tags: newTags,
|
||||||
|
allocation: spaceAllocation,
|
||||||
|
});
|
||||||
spaceAllocation.tags.push(...newTags);
|
spaceAllocation.tags.push(...newTags);
|
||||||
await queryRunner.manager.save(spaceAllocation);
|
await queryRunner.manager.save(spaceAllocation);
|
||||||
}
|
}
|
||||||
@ -259,8 +272,21 @@ export class SubSpaceModelService {
|
|||||||
tags: tags,
|
tags: tags,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
movedToAlreadyExistingSpaceAllocations.push({
|
||||||
|
allocation: newSpaceAllocation,
|
||||||
|
tags: tags,
|
||||||
|
});
|
||||||
await queryRunner.manager.save(newSpaceAllocation);
|
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(
|
await queryRunner.manager.remove(
|
||||||
@ -289,7 +315,7 @@ export class SubSpaceModelService {
|
|||||||
return subspaces.map((subspace) => ({
|
return subspaces.map((subspace) => ({
|
||||||
subspaceModel: subspace,
|
subspaceModel: subspace,
|
||||||
action: ModifyAction.DELETE,
|
action: ModifyAction.DELETE,
|
||||||
tags: [],
|
relocatedAllocations: relocatedAllocationsMap.get(subspace.uuid) || [],
|
||||||
}));
|
}));
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error instanceof QueryFailedError) {
|
if (error instanceof QueryFailedError) {
|
||||||
|
|||||||
Reference in New Issue
Block a user