diff --git a/src/space-model/services/tag-model.service.ts b/src/space-model/services/tag-model.service.ts index b663590..b23bc7e 100644 --- a/src/space-model/services/tag-model.service.ts +++ b/src/space-model/services/tag-model.service.ts @@ -519,6 +519,23 @@ export class TagModelService { // Extract tags marked for addition in spaceTags const spaceTagsToAdd = spaceTags.filter((tag) => tag.action === 'add'); + const subspaceTagsToAdd = subspaceModels.flatMap( + (subspace) => subspace.tags?.filter((tag) => tag.action === 'add') || [], + ); + + const subspaceTagsToDelete = subspaceModels.flatMap( + (subspace) => + subspace.tags?.filter((tag) => tag.action === 'delete') || [], + ); + + const subspaceTagsToDeleteUuids = new Set( + subspaceTagsToDelete.map((tag) => tag.uuid), + ); + + const commonTagsInSubspaces = subspaceTagsToAdd.filter((tag) => + subspaceTagsToDeleteUuids.has(tag.uuid), + ); + // Find UUIDs of tags that are common between spaceTagsToAdd and subspace tags marked for deletion const commonTagUuids = new Set( spaceTagsToAdd @@ -536,11 +553,25 @@ export class TagModelService { ); // Modify subspaceModels by removing tags with UUIDs present in commonTagUuids - const modifiedSubspaces = subspaceModels.map((subspace) => ({ + let modifiedSubspaces = subspaceModels.map((subspace) => ({ ...subspace, tags: subspace.tags?.filter((tag) => !commonTagUuids.has(tag.uuid)) || [], })); + modifiedSubspaces = modifiedSubspaces.map((subspace) => ({ + ...subspace, + tags: + subspace.tags?.filter( + (tag) => + !( + tag.action === 'delete' && + commonTagsInSubspaces.some( + (commonTag) => commonTag.uuid === tag.uuid, + ) + ), + ) || [], + })); + return modifiedSubspaces; } } diff --git a/src/space/services/tag/tag.service.ts b/src/space/services/tag/tag.service.ts index 2dee0c7..be783b2 100644 --- a/src/space/services/tag/tag.service.ts +++ b/src/space/services/tag/tag.service.ts @@ -543,6 +543,23 @@ export class TagService { // Extract tags marked for addition in spaceTags const spaceTagsToAdd = spaceTags?.filter((tag) => tag.action === 'add'); + const subspaceTagsToAdd = subspaceModels.flatMap( + (subspace) => subspace.tags?.filter((tag) => tag.action === 'add') || [], + ); + + const subspaceTagsToDelete = subspaceModels.flatMap( + (subspace) => + subspace.tags?.filter((tag) => tag.action === 'delete') || [], + ); + + const subspaceTagsToDeleteUuids = new Set( + subspaceTagsToDelete.map((tag) => tag.uuid), + ); + + const commonTagsInSubspaces = subspaceTagsToAdd.filter((tag) => + subspaceTagsToDeleteUuids.has(tag.uuid), + ); + // Find UUIDs of tags that are common between spaceTagsToAdd and subspace tags marked for deletion const commonTagUuids = new Set( spaceTagsToAdd @@ -560,11 +577,25 @@ export class TagService { ); // Modify subspaceModels by removing tags with UUIDs present in commonTagUuids - const modifiedSubspaces = subspaceModels.map((subspace) => ({ + let modifiedSubspaces = subspaceModels.map((subspace) => ({ ...subspace, tags: subspace.tags?.filter((tag) => !commonTagUuids.has(tag.uuid)) || [], })); + modifiedSubspaces = modifiedSubspaces.map((subspace) => ({ + ...subspace, + tags: + subspace.tags?.filter( + (tag) => + !( + tag.action === 'delete' && + commonTagsInSubspaces.some( + (commonTag) => commonTag.uuid === tag.uuid, + ) + ), + ) || [], + })); + return modifiedSubspaces; } }