fix duplication

This commit is contained in:
hannathkadher
2025-03-11 13:43:23 +04:00
parent c62c952544
commit f9f9592603
5 changed files with 87 additions and 44 deletions

View File

@ -14,6 +14,7 @@ import { ModifyAction } from '@app/common/constants/modify-action.enum';
import { NewTagEntity } from '@app/common/modules/tag';
import { ProductEntity } from '@app/common/modules/product/entities';
import { SpaceRepository } from '@app/common/modules/space';
import { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
import { ProjectEntity } from '@app/common/modules/project/entities';
@Injectable()
@ -30,11 +31,10 @@ export class SpaceModelProductAllocationService {
tags: ProcessTagDto[],
queryRunner?: QueryRunner,
modifySubspaceModels?: ModifySubspaceModelDto[],
spaces?: SpaceEntity[],
): Promise<SpaceModelProductAllocationEntity[]> {
try {
if (!tags.length) {
return [];
}
if (!tags.length) return [];
const processedTags = await this.tagService.processTags(
tags,
@ -56,7 +56,7 @@ export class SpaceModelProductAllocationService {
SubspaceModelProductAllocationEntity,
{
where: {
product: tag.product,
product: { uuid: tag.product.uuid },
subspaceModel: { spaceModel: { uuid: spaceModel.uuid } },
tags: { uuid: tag.uuid },
},
@ -81,7 +81,6 @@ export class SpaceModelProductAllocationService {
)
) {
isTagNeeded = true;
break;
}
}
@ -94,9 +93,7 @@ export class SpaceModelProductAllocationService {
spaceModel,
);
if (hasTags) {
continue;
}
if (hasTags) continue;
let allocation = existingAllocations.get(tag.product.uuid);
if (!allocation) {
@ -123,11 +120,9 @@ export class SpaceModelProductAllocationService {
if (productAllocations.length > 0) {
await this.saveAllocations(productAllocations, queryRunner);
}
return productAllocations;
} catch (error) {
console.error(
`[ERROR] Failed to create product allocations: ${error.message}`,
);
throw this.handleError(error, 'Failed to create product allocations');
}
}
@ -138,6 +133,7 @@ export class SpaceModelProductAllocationService {
spaceModel: SpaceModelEntity,
queryRunner: QueryRunner,
modifySubspaceModels?: ModifySubspaceModelDto[],
spaces?: SpaceEntity[],
): Promise<void> {
try {
const addDtos = dtos.filter((dto) => dto.action === ModifyAction.ADD);
@ -188,8 +184,15 @@ export class SpaceModelProductAllocationService {
spaceModel,
queryRunner,
modifySubspaceModels,
spaces,
),
this.processDeleteActions(
filteredDtos,
queryRunner,
spaceModel,
project,
spaces,
),
this.processDeleteActions(filteredDtos, queryRunner, spaceModel),
]);
} catch (error) {
throw this.handleError(error, 'Error while updating product allocations');
@ -202,6 +205,7 @@ export class SpaceModelProductAllocationService {
spaceModel: SpaceModelEntity,
queryRunner: QueryRunner,
modifySubspaceModels?: ModifySubspaceModelDto[],
spaces?: SpaceEntity[],
): Promise<void> {
const addDtos: ProcessTagDto[] = dtos
.filter((dto) => dto.action === ModifyAction.ADD)
@ -218,6 +222,7 @@ export class SpaceModelProductAllocationService {
addDtos,
queryRunner,
modifySubspaceModels,
spaces,
);
}
}
@ -247,11 +252,17 @@ export class SpaceModelProductAllocationService {
): Promise<SpaceModelProductAllocationEntity | null> {
return queryRunner
? queryRunner.manager.findOne(SpaceModelProductAllocationEntity, {
where: { spaceModel, product: product },
where: {
spaceModel: { uuid: spaceModel.uuid },
product: { uuid: product.uuid },
},
relations: ['tags'],
})
: this.spaceModelProductAllocationRepository.findOne({
where: { spaceModel, product: product },
where: {
spaceModel: { uuid: spaceModel.uuid },
product: { uuid: product.uuid },
},
relations: ['tags'],
});
}
@ -293,6 +304,8 @@ export class SpaceModelProductAllocationService {
dtos: ModifyTagModelDto[],
queryRunner: QueryRunner,
spaceModel: SpaceModelEntity,
project: ProjectEntity,
spaces?: SpaceEntity[],
): Promise<SpaceModelProductAllocationEntity[]> {
try {
if (!dtos || dtos.length === 0) {
@ -391,18 +404,18 @@ export class SpaceModelProductAllocationService {
spaceModel: {
uuid: spaceModel.uuid,
},
product: tag.product,
product: {
uuid: tag.product.uuid,
},
},
relations: ['tags'],
},
);
//Flatten all existing tags for this product in the space
const existingTagsForProduct = existingAllocationsForProduct.flatMap(
(allocation) => allocation.tags,
);
// Check if the tag is already assigned to the same product in this space
const isDuplicateTag = existingTagsForProduct.some(
(existingTag) => existingTag.uuid === tag.uuid,
);

View File

@ -76,7 +76,7 @@ export class SubspaceModelProductAllocationService {
const existingTagInSameSpace = await (queryRunner
? queryRunner.manager.findOne(SubspaceModelProductAllocationEntity, {
where: {
product: tag.product,
product: { uuid: tag.product.uuid },
subspaceModel: { spaceModel: subspaceModel.spaceModel },
tags: { uuid: tag.uuid }, // Ensure the exact tag is checked
},
@ -84,7 +84,7 @@ export class SubspaceModelProductAllocationService {
})
: this.subspaceModelProductAllocationRepository.findOne({
where: {
product: tag.product,
product: { uuid: tag.product.uuid },
subspaceModel: { spaceModel: subspaceModel.spaceModel },
tags: { uuid: tag.uuid },
},
@ -105,11 +105,17 @@ export class SubspaceModelProductAllocationService {
//Check if there are existing allocations for this product in the subspace
const existingAllocationsForProduct = await (queryRunner
? queryRunner.manager.find(SubspaceModelProductAllocationEntity, {
where: { subspaceModel, product: tag.product },
where: {
subspaceModel: { uuid: subspaceModel.uuid },
product: { uuid: tag.product.uuid },
},
relations: ['tags'],
})
: this.subspaceModelProductAllocationRepository.find({
where: { subspaceModel, product: tag.product },
where: {
subspaceModel: { uuid: subspaceModel.uuid },
product: { uuid: tag.product.uuid },
},
relations: ['tags'],
}));