fix issue on deleting subspace

This commit is contained in:
hannathkadher
2025-03-06 22:51:19 +04:00
parent a68f4c1847
commit 61fc699cf8
3 changed files with 121 additions and 86 deletions

View File

@ -88,7 +88,13 @@ export class SpaceModelProductAllocationService {
}
if (isTagNeeded) {
await this.validateTagWithinSpaceModel(queryRunner, tag, spaceModel);
const hasTags = await this.validateTagWithinSpaceModel(
queryRunner,
tag,
spaceModel,
);
if (hasTags) continue;
let allocation = existingAllocations.get(tag.product.uuid);
if (!allocation) {
@ -384,11 +390,16 @@ export class SpaceModelProductAllocationService {
queryRunner: QueryRunner,
tag: NewTagEntity,
spaceModel: SpaceModelEntity,
) {
): Promise<boolean> {
const existingAllocationsForProduct = await queryRunner.manager.find(
SpaceModelProductAllocationEntity,
{
where: { spaceModel, product: tag.product },
where: {
spaceModel: {
uuid: spaceModel.uuid,
},
product: tag.product,
},
relations: ['tags'],
},
);
@ -404,11 +415,9 @@ export class SpaceModelProductAllocationService {
);
if (isDuplicateTag) {
throw new HttpException(
`Tag ${tag.uuid} is already allocated to product ${tag.product.uuid} within this space (${spaceModel.uuid}).`,
HttpStatus.BAD_REQUEST,
);
return true;
}
return false;
}
async clearAllAllocations(spaceModelUuid: string, queryRunner: QueryRunner) {

View File

@ -255,7 +255,6 @@ export class SubspaceModelProductAllocationService {
return deletedAllocations;
} catch (error) {
console.log(error);
throw this.handleError(error, `Failed to delete tags in subspace model`);
}
}
@ -309,68 +308,74 @@ export class SubspaceModelProductAllocationService {
SubspaceModelProductAllocationEntity,
{
where: {
subspaceModel: { uuid: subspaceDto.subspaceModel.uuid },
tags: {
uuid: deletedTag.tagUuid,
},
subspaceModel: {
uuid: subspaceDto.subspaceModel.uuid,
},
},
relations: ['tags', 'product', 'subspace'],
relations: ['tags', 'product', 'subspaceModel'],
},
);
const isCommonTag = allocation.tags.some(
(tag) => tag.uuid === deletedTag.tagUuid,
);
if (allocation && isCommonTag) {
const tagEntity = allocation.tags.find(
if (allocation) {
const isCommonTag = allocation.tags.some(
(tag) => tag.uuid === deletedTag.tagUuid,
);
allocation.tags = allocation.tags.filter(
(tag) => tag.uuid !== deletedTag.tagUuid,
);
if (allocation && isCommonTag) {
const tagEntity = allocation.tags.find(
(tag) => tag.uuid === deletedTag.tagUuid,
);
await queryRunner.manager.save(allocation);
allocation.tags = allocation.tags.filter(
(tag) => tag.uuid !== deletedTag.tagUuid,
);
const productAllocationExistInSubspace =
await queryRunner.manager.findOne(
SubspaceModelProductAllocationEntity,
{
where: {
subspaceModel: {
uuid: subspaceDto.subspaceModel.uuid,
await queryRunner.manager.save(allocation);
const productAllocationExistInSubspace =
await queryRunner.manager.findOne(
SubspaceModelProductAllocationEntity,
{
where: {
subspaceModel: {
uuid: subspaceDto.subspaceModel.uuid,
},
product: { uuid: allocation.product.uuid },
},
product: { uuid: allocation.product.uuid },
relations: ['tags'],
},
relations: ['tags'],
},
);
if (productAllocationExistInSubspace) {
productAllocationExistInSubspace.tags.push(tagEntity);
await queryRunner.manager.save(
productAllocationExistInSubspace,
);
} else {
const newProductAllocation = queryRunner.manager.create(
SubspaceModelProductAllocationEntity,
{
subspaceModel: subspaceModel.subspaceModel,
product: allocation.product,
tags: [tagEntity],
},
);
await queryRunner.manager.save(newProductAllocation);
}
// Remove the tag from processedTags to prevent duplication
processedTags = processedTags.filter(
(tag) => tag.uuid !== deletedTag.tagUuid,
);
if (productAllocationExistInSubspace) {
productAllocationExistInSubspace.tags.push(tagEntity);
await queryRunner.manager.save(
productAllocationExistInSubspace,
// Remove the tag from subspaceDto.tags to ensure it's not processed again while processing other dtos.
subspaceDto.tags = subspaceDto.tags.filter(
(tagDto) => tagDto.tagUuid !== deletedTag.tagUuid,
);
} else {
const newProductAllocation = queryRunner.manager.create(
SubspaceModelProductAllocationEntity,
{
subspaceModel: subspaceModel.subspaceModel,
product: allocation.product,
tags: [tagEntity],
},
);
await queryRunner.manager.save(newProductAllocation);
}
// Remove the tag from processedTags to prevent duplication
processedTags = processedTags.filter(
(tag) => tag.uuid !== deletedTag.tagUuid,
);
// Remove the tag from subspaceDto.tags to ensure it's not processed again while processing other dtos.
subspaceDto.tags = subspaceDto.tags.filter(
(tagDto) => tagDto.tagUuid !== deletedTag.tagUuid,
);
}
}
}

View File

@ -103,38 +103,57 @@ export class SubSpaceModelService {
projectUuid: string,
spaceTagUpdateDtos?: ModifyTagModelDto[],
) {
if (!dtos || dtos.length === 0) {
return;
try {
if (!dtos || dtos.length === 0) {
return;
}
const addDtos = dtos.filter((dto) => dto.action === ModifyAction.ADD);
const combinedDtos = dtos.filter(
(dto) => dto.action !== ModifyAction.ADD,
);
const deleteDtos = dtos.filter(
(dto) => dto.action === ModifyAction.DELETE,
);
const updatedModels = await this.updateSubspaceModel(
combinedDtos,
spaceModel,
queryRunner,
);
const addedModels = await this.handleAddAction(
addDtos,
spaceModel,
queryRunner,
);
const combineModels = [...addedModels, ...updatedModels];
await this.productAllocationService.updateAllocations(
combineModels,
projectUuid,
queryRunner,
spaceModel,
spaceTagUpdateDtos,
);
await this.deleteSubspaceModels(deleteDtos, queryRunner, spaceModel);
} catch (error) {
console.error('Error in modifySubspaceModels:', error);
throw new HttpException(
{
status: HttpStatus.INTERNAL_SERVER_ERROR,
message: 'An error occurred while modifying subspace models',
error: error.message,
},
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
const addDtos = dtos.filter((dto) => dto.action === ModifyAction.ADD);
const combinedDtos = dtos.filter((dto) => dto.action !== ModifyAction.ADD);
const deleteDtos = dtos.filter((dto) => dto.action === ModifyAction.DELETE);
const updatedModels = await this.updateSubspaceModel(
combinedDtos,
spaceModel,
queryRunner,
);
const addedModels = await this.handleAddAction(
addDtos,
spaceModel,
queryRunner,
);
const combineModels = [...addedModels, ...updatedModels];
await this.productAllocationService.updateAllocations(
combineModels,
projectUuid,
queryRunner,
spaceModel,
spaceTagUpdateDtos,
);
await this.deleteSubspaceModels(deleteDtos, queryRunner);
}
async deleteSubspaceModels(
deleteDtos: ModifySubspaceModelDto[],
queryRunner: QueryRunner,
spaceModel: SpaceModelEntity,
) {
try {
if (!deleteDtos || deleteDtos.length === 0) {
@ -164,6 +183,7 @@ export class SubSpaceModelService {
const allocationsToRemove = subspaces.flatMap(
(subspace) => subspace.productAllocations,
);
if (allocationsToRemove.length > 0) {
const spaceAllocationsMap = new Map<
string,
@ -173,7 +193,6 @@ export class SubSpaceModelService {
for (const allocation of allocationsToRemove) {
const product = allocation.product;
const tags = allocation.tags;
const spaceModel = allocation.subspaceModel.spaceModel;
const spaceAllocationKey = `${spaceModel.uuid}-${product.uuid}`;
@ -230,12 +249,13 @@ export class SubSpaceModelService {
.delete()
.from('subspace_model_product_tags')
.where(
'subspace_model_product_allocation_id NOT IN ' +
'subspace_model_product_allocation_uuid NOT IN (' +
queryRunner.manager
.createQueryBuilder()
.select('uuid')
.select('allocation.uuid')
.from(SubspaceModelProductAllocationEntity, 'allocation')
.getQuery(),
.getQuery() +
')',
)
.execute();
@ -418,6 +438,7 @@ export class SubSpaceModelService {
where: { uuid: In(subspaceUuids) },
relations: [
'productAllocations',
'productAllocations.product',
'productAllocations.tags',
'spaceModel',
],