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 605994f9f..8eaf78df8 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 @@ -55,7 +55,6 @@ import org.springframework.r2dbc.core.bind import org.springframework.stereotype.Service import org.springframework.transaction.annotation.Transactional import java.net.URI -import java.time.ZoneOffset import java.time.ZonedDateTime @Service @@ -90,7 +89,8 @@ class EntityService( val attributesMetadata = ngsiLdEntity.prepareAttributes().bind() logger.debug("Creating entity {}", ngsiLdEntity.id) - createEntityPayload(ngsiLdEntity, expandedEntity, createdAt, sub).bind() + createEntityPayload(ngsiLdEntity, expandedEntity, createdAt).bind() + scopeService.createHistory(ngsiLdEntity, createdAt, sub).bind() entityAttributeService.createAttributes( ngsiLdEntity, expandedEntity, @@ -111,8 +111,7 @@ class EntityService( suspend fun createEntityPayload( ngsiLdEntity: NgsiLdEntity, expandedEntity: ExpandedEntity, - createdAt: ZonedDateTime, - sub: Sub? = null + createdAt: ZonedDateTime ): Either = either { val specificAccessPolicy = ngsiLdEntity.getSpecificAccessPolicy()?.bind() databaseClient.sql( @@ -135,9 +134,6 @@ class EntityService( .bind("payload", Json.of(serializeObject(expandedEntity.populateCreationTimeDate(createdAt).members))) .bind("specific_access_policy", specificAccessPolicy?.toString()) .execute() - .map { - scopeService.createHistory(ngsiLdEntity, createdAt, sub) - } } @Transactional @@ -204,7 +200,8 @@ class EntityService( entityAttributeService.deleteAttributes(entityId, ngsiLdDateTime()).bind() val replacedAt = ngsiLdDateTime() - replaceEntityPayload(ngsiLdEntity, expandedEntity, replacedAt, sub).bind() + replaceEntityPayload(ngsiLdEntity, expandedEntity, replacedAt).bind() + scopeService.replace(ngsiLdEntity, replacedAt, sub).bind() entityAttributeService.createAttributes( ngsiLdEntity, expandedEntity, @@ -224,8 +221,7 @@ class EntityService( suspend fun replaceEntityPayload( ngsiLdEntity: NgsiLdEntity, expandedEntity: ExpandedEntity, - replacedAt: ZonedDateTime, - sub: Sub? = null + replacedAt: ZonedDateTime ): Either = either { val specificAccessPolicy = ngsiLdEntity.getSpecificAccessPolicy()?.bind() val createdAt = retrieveCreatedAt(ngsiLdEntity.id).bind() @@ -250,9 +246,6 @@ class EntityService( .bind("payload", Json.of(serializedPayload)) .bind("specific_access_policy", specificAccessPolicy?.toString()) .execute() - .map { - scopeService.replaceHistoryEntry(ngsiLdEntity, createdAt, sub) - } } private suspend fun retrieveCreatedAt(entityId: URI): Either = @@ -464,7 +457,7 @@ class EntityService( expandedAttributes: ExpandedAttributes, sub: Sub? = null ): Either = either { - val createdAt = ZonedDateTime.now(ZoneOffset.UTC) + val createdAt = ngsiLdDateTime() expandedAttributes.forEach { (attributeName, expandedAttributeInstances) -> expandedAttributeInstances.forEach { expandedAttributeInstance -> val jsonLdAttribute = mapOf(attributeName to listOf(expandedAttributeInstance)) @@ -619,7 +612,6 @@ class EntityService( val entity = permanentyDeleteEntityPayload(entityId).bind() entityAttributeService.permanentlyDeleteAttributes(entityId).bind() - scopeService.deleteHistory(entityId).bind() authorizationService.removeRightsOnEntity(entityId).bind() entityEventService.publishEntityDeleteEvent(sub, entity) @@ -695,7 +687,7 @@ class EntityService( authorizationService.userCanUpdateEntity(entityId, sub.toOption()).bind() if (attributeName == NGSILD_SCOPE_PROPERTY) { - scopeService.delete(entityId).bind() + scopeService.permanentlyDelete(entityId).bind() } else { entityAttributeService.checkEntityAndAttributeExistence( entityId, diff --git a/search-service/src/main/kotlin/com/egm/stellio/search/scope/ScopeService.kt b/search-service/src/main/kotlin/com/egm/stellio/search/scope/ScopeService.kt index 2968f7cc6..8cb37778b 100644 --- a/search-service/src/main/kotlin/com/egm/stellio/search/scope/ScopeService.kt +++ b/search-service/src/main/kotlin/com/egm/stellio/search/scope/ScopeService.kt @@ -37,6 +37,8 @@ import com.egm.stellio.shared.util.JsonLdUtils import com.egm.stellio.shared.util.JsonLdUtils.NGSILD_SCOPE_PROPERTY import com.egm.stellio.shared.util.JsonUtils.serializeObject import com.egm.stellio.shared.util.Sub +import com.egm.stellio.shared.util.getSubFromSecurityContext +import com.egm.stellio.shared.util.ngsiLdDateTime import io.r2dbc.postgresql.codec.Json import org.springframework.r2dbc.core.DatabaseClient import org.springframework.r2dbc.core.bind @@ -72,7 +74,7 @@ class ScopeService( suspend fun addHistoryEntry( entityId: URI, scopes: List, - temportalProperty: TemporalProperty, + temporalProperty: TemporalProperty, createdAt: ZonedDateTime, sub: Sub? = null ): Either = @@ -85,7 +87,7 @@ class ScopeService( .bind("entity_id", entityId) .bind("value", scopes.toTypedArray()) .bind("time", createdAt) - .bind("time_property", temportalProperty.toString()) + .bind("time_property", temporalProperty.toString()) .bind("sub", sub) .execute() @@ -341,12 +343,12 @@ class ScopeService( } @Transactional - suspend fun replaceHistoryEntry( + suspend fun replace( ngsiLdEntity: NgsiLdEntity, createdAt: ZonedDateTime, sub: Sub? = null ): Either = either { - deleteHistory(ngsiLdEntity.id).bind() + delete(ngsiLdEntity.id).bind() createHistory(ngsiLdEntity, createdAt, sub).bind() } @@ -364,10 +366,17 @@ class ScopeService( .execute() .bind() - deleteHistory(entityId).bind() + addHistoryEntry( + entityId, + emptyList(), + TemporalProperty.DELETED_AT, + ngsiLdDateTime(), + getSubFromSecurityContext().getOrNull() + ) } - suspend fun deleteHistory(entityId: URI): Either = + @Transactional + suspend fun permanentlyDelete(entityId: URI): Either = databaseClient.sql( """ DELETE FROM scope_history diff --git a/search-service/src/main/kotlin/com/egm/stellio/search/temporal/service/TemporalService.kt b/search-service/src/main/kotlin/com/egm/stellio/search/temporal/service/TemporalService.kt index f760a2af0..4ec13363f 100644 --- a/search-service/src/main/kotlin/com/egm/stellio/search/temporal/service/TemporalService.kt +++ b/search-service/src/main/kotlin/com/egm/stellio/search/temporal/service/TemporalService.kt @@ -93,7 +93,7 @@ class TemporalService( if (isDeleted) { val (expandedEntity, ngsiLdEntity) = parseExpandedInstances(sortedJsonLdInstances, jsonLdTemporalEntity).bind() - entityService.createEntityPayload(ngsiLdEntity, expandedEntity, ngsiLdDateTime(), sub).bind() + entityService.createEntityPayload(ngsiLdEntity, expandedEntity, ngsiLdDateTime()).bind() } entityService.upsertAttributes( entityId, diff --git a/search-service/src/test/kotlin/com/egm/stellio/search/scope/ScopeServiceTests.kt b/search-service/src/test/kotlin/com/egm/stellio/search/scope/ScopeServiceTests.kt index dca484c85..537391e45 100644 --- a/search-service/src/test/kotlin/com/egm/stellio/search/scope/ScopeServiceTests.kt +++ b/search-service/src/test/kotlin/com/egm/stellio/search/scope/ScopeServiceTests.kt @@ -23,7 +23,6 @@ import com.egm.stellio.shared.util.shouldSucceed import com.egm.stellio.shared.util.shouldSucceedAndResult import com.egm.stellio.shared.util.shouldSucceedWith import com.egm.stellio.shared.util.toUri -import kotlinx.coroutines.runBlocking import kotlinx.coroutines.test.runTest import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.AfterEach @@ -64,10 +63,6 @@ class ScopeServiceTests : WithTimescaleContainer, WithKafkaContainer { @AfterEach fun clearEntityPayloadTable() { r2dbcEntityTemplate.delete().from("entity_payload").all().block() - - runBlocking { - scopeService.delete(beehiveTestCId) - } } @Suppress("unused")