Merge pull request #281 from SyncrowIOT/bugfix/update-space-model-on-tag-reuse

This commit is contained in:
hannathkadher
2025-03-06 09:36:53 +04:00
committed by GitHub
4 changed files with 94 additions and 28 deletions
@@ -124,15 +124,56 @@ export class SpaceModelProductAllocationService {
modifySubspaceModels?: ModifySubspaceModelDto[],
): Promise<void> {
try {
const addDtos = dtos.filter((dto) => dto.action === ModifyAction.ADD);
const deleteDtos = dtos.filter(
(dto) => dto.action === ModifyAction.DELETE,
);
const addTagDtos: ProcessTagDto[] = addDtos.map((dto) => ({
name: dto.name,
productUuid: dto.productUuid,
uuid: dto.newTagUuid,
}));
const processedTags = await this.tagService.processTags(
addTagDtos,
projectUuid,
queryRunner,
);
const addTagUuidMap = new Map<string, ModifyTagModelDto>();
processedTags.forEach((tag, index) => {
addTagUuidMap.set(tag.uuid, addDtos[index]);
});
const addTagUuids = new Set(processedTags.map((tag) => tag.uuid));
const deleteTagUuids = new Set(deleteDtos.map((dto) => dto.tagUuid));
const tagsToIgnore = new Set(
[...addTagUuids].filter((uuid) => deleteTagUuids.has(uuid)),
);
const filteredDtos = dtos.filter(
(dto) =>
!(
tagsToIgnore.has(dto.tagUuid) ||
(dto.action === ModifyAction.ADD &&
tagsToIgnore.has(
[...addTagUuidMap.keys()].find(
(uuid) => addTagUuidMap.get(uuid) === dto,
),
))
),
);
await Promise.all([
this.processAddActions(
dtos,
filteredDtos,
projectUuid,
spaceModel,
queryRunner,
modifySubspaceModels,
),
this.processDeleteActions(dtos, queryRunner),
this.processDeleteActions(filteredDtos, queryRunner),
]);
} catch (error) {
throw this.handleError(error, 'Error while updating product allocations');
@@ -238,7 +279,7 @@ export class SpaceModelProductAllocationService {
): Promise<SpaceModelProductAllocationEntity[]> {
try {
if (!dtos || dtos.length === 0) {
throw new Error('No DTOs provided for deletion.');
return;
}
const tagUuidsToDelete = dtos
@@ -108,7 +108,7 @@ export class SubSpaceModelService {
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 deleteDtos = dtos.filter((dto) => dto.action === ModifyAction.DELETE);
const updatedModels = await this.updateSubspaceModel(
combinedDtos,
@@ -137,7 +137,7 @@ export class SubSpaceModelService {
) {
try {
if (!deleteDtos || deleteDtos.length === 0) {
throw new Error('No subspaces provided for deletion.');
return;
}
const deleteResults = [];
@@ -306,8 +306,6 @@ export class SubSpaceModelService {
for (const dto of dtos) {
if (!dto.subspaceName) continue;
await this.checkDuplicateNames(dto.subspaceName, spaceModel.uuid);
const existingSubspace = await queryRunner.manager.findOne(
this.subspaceModelRepository.target,
{ where: { uuid: dto.uuid } },
@@ -317,6 +315,9 @@ export class SubSpaceModelService {
existingSubspace &&
existingSubspace.subspaceName !== dto.subspaceName
) {
if (existingSubspace.subspaceName !== dto.subspaceName) {
await this.checkDuplicateNames(dto.subspaceName, spaceModel.uuid);
}
existingSubspace.subspaceName = dto.subspaceName;
await queryRunner.manager.save(existingSubspace);
updatedSubspaces.push({
-2
View File
@@ -139,8 +139,6 @@ export class SpaceService {
message: 'Space created successfully',
});
} catch (error) {
console.log('error', error);
await queryRunner.rollbackTransaction();
if (error instanceof HttpException) {
+45 -19
View File
@@ -67,31 +67,52 @@ export class TagService {
}
async processTags(
tags: ProcessTagDto[],
tagDtos: ProcessTagDto[],
projectUuid: string,
queryRunner?: QueryRunner,
): Promise<NewTagEntity[]> {
try {
if (!tags || tags.length === 0) {
if (!tagDtos || tagDtos.length === 0) {
return [];
}
const newTags: CreateTagDto[] = [];
const newTagDtos: CreateTagDto[] = [];
const existingTagUuids: string[] = [];
let fetchedExistingTags: NewTagEntity[] = [];
const directlyFetchedTags: NewTagEntity[] = [];
// Separate new tags and existing tag UUIDs
for (const tag of tags) {
if (tag.uuid) {
existingTagUuids.push(tag.uuid);
// Separate existing tag UUIDs and new tag DTOs
for (const tagDto of tagDtos) {
if (tagDto.uuid) {
existingTagUuids.push(tagDto.uuid);
} else {
newTags.push(tag);
if (!tagDto.name || !tagDto.productUuid) {
throw new HttpException(
`Tag name or product UUID is missing`,
HttpStatus.BAD_REQUEST,
);
}
const existingTag = await queryRunner.manager.findOne(NewTagEntity, {
where: {
name: tagDto.name,
product: { uuid: tagDto.productUuid },
project: { uuid: projectUuid },
},
relations: ['product'],
});
if (!existingTag) {
newTagDtos.push(tagDto);
} else {
directlyFetchedTags.push(existingTag);
}
}
}
let existingTags: NewTagEntity[] = [];
// Fetch existing tags using UUIDs
if (existingTagUuids.length > 0) {
existingTags = await (queryRunner
fetchedExistingTags = await (queryRunner
? queryRunner.manager.find(NewTagEntity, {
where: {
uuid: In(existingTagUuids),
@@ -108,9 +129,9 @@ export class TagService {
}));
}
// Check for missing UUIDs
if (existingTags.length !== existingTagUuids.length) {
const foundUuids = new Set(existingTags.map((tag) => tag.uuid));
// Ensure all provided UUIDs exist in the database
if (fetchedExistingTags.length !== existingTagUuids.length) {
const foundUuids = new Set(fetchedExistingTags.map((tag) => tag.uuid));
const missingUuids = existingTagUuids.filter(
(uuid) => !foundUuids.has(uuid),
);
@@ -121,16 +142,21 @@ export class TagService {
);
}
let createdTags: NewTagEntity[] = [];
let newlyCreatedTags: NewTagEntity[] = [];
if (newTags.length > 0) {
createdTags = await this.bulkCreateTags(
{ projectUuid, tags: newTags },
if (newTagDtos.length > 0) {
newlyCreatedTags = await this.bulkCreateTags(
{ projectUuid, tags: newTagDtos },
queryRunner,
);
}
const allTags = [...existingTags, ...createdTags];
// Combine all found and created tags
const allTags = [
...fetchedExistingTags,
...newlyCreatedTags,
...directlyFetchedTags,
];
return allTags;
} catch (error) {