Skip to content

Commit

Permalink
fix(core): non existing attrs in append or update operations should b…
Browse files Browse the repository at this point in the history
…e ignored

- when an attribute is ignored in such an operation, it should not raise an error
  • Loading branch information
bobeal committed Dec 4, 2023
1 parent 5b32d87 commit 6a3824f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ data class UpdateResult(
updated = this.updated.plus(other.updated),
notUpdated = this.notUpdated.plus(other.notUpdated)
)

@JsonIgnore
fun hasSuccessfulUpdate(): Boolean =
this.updated.isNotEmpty()
}

val EMPTY_UPDATE_RESULT: UpdateResult = UpdateResult(emptyList(), emptyList())
Expand Down Expand Up @@ -48,7 +52,8 @@ data class UpdateAttributeResult(
this.updateOperationResult in listOf(
UpdateOperationResult.APPENDED,
UpdateOperationResult.REPLACED,
UpdateOperationResult.UPDATED
UpdateOperationResult.UPDATED,
UpdateOperationResult.IGNORED
)
}

Expand All @@ -62,9 +67,6 @@ enum class UpdateOperationResult {
fun isSuccessResult(): Boolean = listOf(APPENDED, REPLACED, UPDATED).contains(this)
}

fun UpdateResult.hasSuccessfulUpdate(): Boolean =
this.updated.isNotEmpty()

fun updateResultFromDetailedResult(updateStatuses: List<UpdateAttributeResult>): UpdateResult {
val updated = updateStatuses.filter { it.isSuccessfullyUpdated() }
.map { UpdatedDetails(it.attributeName, it.datasetId, it.updateOperationResult) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -492,33 +492,32 @@ class EntityPayloadService(
expandedAttributes: ExpandedAttributes,
disallowOverwrite: Boolean,
sub: Sub?
): Either<APIException, UpdateResult> =
either {
val (coreAttrs, otherAttrs) =
expandedAttributes.toList().partition { JSONLD_EXPANDED_ENTITY_SPECIFIC_MEMBERS.contains(it.first) }
val createdAt = ZonedDateTime.now(ZoneOffset.UTC)
): Either<APIException, UpdateResult> = either {
val (coreAttrs, otherAttrs) =
expandedAttributes.toList().partition { JSONLD_EXPANDED_ENTITY_SPECIFIC_MEMBERS.contains(it.first) }
val createdAt = ZonedDateTime.now(ZoneOffset.UTC)

val operationType =
if (disallowOverwrite) APPEND_ATTRIBUTES
else APPEND_ATTRIBUTES_OVERWRITE_ALLOWED
val coreUpdateResult = updateCoreAttributes(entityUri, coreAttrs, createdAt, operationType).bind()
val attrsUpdateResult = temporalEntityAttributeService.appendEntityAttributes(
entityUri,
otherAttrs.toMap().toNgsiLdAttributes().bind(),
expandedAttributes,
disallowOverwrite,
createdAt,
sub
).bind()
val operationType =
if (disallowOverwrite) APPEND_ATTRIBUTES
else APPEND_ATTRIBUTES_OVERWRITE_ALLOWED
val coreUpdateResult = updateCoreAttributes(entityUri, coreAttrs, createdAt, operationType).bind()
val attrsUpdateResult = temporalEntityAttributeService.appendEntityAttributes(
entityUri,
otherAttrs.toMap().toNgsiLdAttributes().bind(),
expandedAttributes,
disallowOverwrite,
createdAt,
sub
).bind()

val updateResult = coreUpdateResult.mergeWith(attrsUpdateResult)
// update modifiedAt in entity if at least one attribute has been added
if (updateResult.hasSuccessfulUpdate()) {
val teas = temporalEntityAttributeService.getForEntity(entityUri, emptySet())
updateState(entityUri, createdAt, teas).bind()
}
updateResult
val updateResult = coreUpdateResult.mergeWith(attrsUpdateResult)
// update modifiedAt in entity if at least one attribute has been added
if (updateResult.hasSuccessfulUpdate()) {
val teas = temporalEntityAttributeService.getForEntity(entityUri, emptySet())
updateState(entityUri, createdAt, teas).bind()
}
updateResult
}

@Transactional
suspend fun updateAttributes(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ class TemporalEntityAttributeService(
UpdateAttributeResult(
attributeName,
datasetId,
UpdateOperationResult.IGNORED,
UpdateOperationResult.FAILED,
"Unknown attribute $attributeName with datasetId $datasetId in entity $entityId"
)
}
Expand Down Expand Up @@ -819,7 +819,7 @@ class TemporalEntityAttributeService(
UpdateAttributeResult(
attributeName,
datasetId,
UpdateOperationResult.IGNORED,
UpdateOperationResult.FAILED,
"Unknown attribute $attributeName with datasetId $datasetId in entity $entityId"
)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import arrow.core.left
import arrow.core.raise.either
import arrow.core.right
import com.egm.stellio.search.authorization.AuthorizationService
import com.egm.stellio.search.model.hasSuccessfulUpdate
import com.egm.stellio.search.service.EntityEventService
import com.egm.stellio.search.service.EntityPayloadService
import com.egm.stellio.search.service.QueryService
Expand Down

0 comments on commit 6a3824f

Please sign in to comment.