From b7d18a24a104d1f158c6d9f3482bf4451401bd5f Mon Sep 17 00:00:00 2001 From: Benoit Orihuela Date: Fri, 13 Dec 2024 17:12:08 +0000 Subject: [PATCH] fix(core): allows to check existence of an attribute whatever its datasetId (#1284) --- .../search/entity/service/EntityAttributeService.kt | 8 ++++++-- .../stellio/search/entity/service/EntityService.kt | 3 ++- .../entity/service/EntityAttributeServiceTests.kt | 12 ++++++++++++ 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/search-service/src/main/kotlin/com/egm/stellio/search/entity/service/EntityAttributeService.kt b/search-service/src/main/kotlin/com/egm/stellio/search/entity/service/EntityAttributeService.kt index 95d57a242..adf1ff565 100644 --- a/search-service/src/main/kotlin/com/egm/stellio/search/entity/service/EntityAttributeService.kt +++ b/search-service/src/main/kotlin/com/egm/stellio/search/entity/service/EntityAttributeService.kt @@ -545,8 +545,12 @@ class EntityAttributeService( suspend fun checkEntityAndAttributeExistence( entityId: URI, attributeName: ExpandedTerm, - datasetId: URI? = null + datasetId: URI? = null, + anyAttributeInstance: Boolean = false ): Either { + val datasetIdFilter = + if (anyAttributeInstance) "" + else datasetId.toDatasetIdFilter() val selectQuery = """ select @@ -560,7 +564,7 @@ class EntityAttributeService( from temporal_entity_attribute where entity_id = :entity_id and attribute_name = :attribute_name - ${datasetId.toDatasetIdFilter()} + $datasetIdFilter ) as attributeNameExists; """.trimIndent() diff --git a/search-service/src/main/kotlin/com/egm/stellio/search/entity/service/EntityService.kt b/search-service/src/main/kotlin/com/egm/stellio/search/entity/service/EntityService.kt index 992308b08..800d741a5 100644 --- a/search-service/src/main/kotlin/com/egm/stellio/search/entity/service/EntityService.kt +++ b/search-service/src/main/kotlin/com/egm/stellio/search/entity/service/EntityService.kt @@ -605,7 +605,8 @@ class EntityService( entityAttributeService.checkEntityAndAttributeExistence( entityId, attributeName, - datasetId + datasetId, + deleteAll ).bind() entityAttributeService.deleteAttribute( entityId, diff --git a/search-service/src/test/kotlin/com/egm/stellio/search/entity/service/EntityAttributeServiceTests.kt b/search-service/src/test/kotlin/com/egm/stellio/search/entity/service/EntityAttributeServiceTests.kt index ea16a72be..f0ede34f6 100644 --- a/search-service/src/test/kotlin/com/egm/stellio/search/entity/service/EntityAttributeServiceTests.kt +++ b/search-service/src/test/kotlin/com/egm/stellio/search/entity/service/EntityAttributeServiceTests.kt @@ -639,6 +639,18 @@ class EntityAttributeServiceTests : WithTimescaleContainer, WithKafkaContainer { .shouldSucceed() } + @Test + fun `it should return a right unit if entiy and attribute exist whatever its datasetId`() = runTest { + val rawEntity = loadSampleData("beehive_multi_instance_property.jsonld") + + coEvery { attributeInstanceService.create(any()) } returns Unit.right() + + entityAttributeService.createAttributes(rawEntity, APIC_COMPOUND_CONTEXTS) + + entityAttributeService.checkEntityAndAttributeExistence(beehiveTestCId, INCOMING_PROPERTY, null, true) + .shouldSucceed() + } + @Test fun `it should return a left attribute not found if entity exists but not the attribute`() = runTest { val rawEntity = loadSampleData()