Skip to content

Commit 458400c

Browse files
committed
Added currentVersion for debugging and give possible error when differ.
1 parent 9af16dc commit 458400c

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

src/main/kotlin/no/nav/klage/document/api/DocumentController.kt

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package no.nav.klage.document.api
22

33
import io.swagger.v3.oas.annotations.Operation
44
import io.swagger.v3.oas.annotations.tags.Tag
5+
import no.nav.klage.document.api.views.DocumentUpdateInput
56
import no.nav.klage.document.api.views.DocumentVersionView
67
import no.nav.klage.document.api.views.DocumentView
78
import no.nav.klage.document.config.SecurityConfiguration.Companion.ISSUER_AAD
@@ -49,11 +50,18 @@ class DocumentController(
4950
@PutMapping("/{documentId}")
5051
fun updateDocument(
5152
@PathVariable("documentId") documentId: UUID,
52-
@RequestBody json: String
53+
@RequestBody(required = false) json: String?,
54+
@RequestBody(required = false) input: DocumentUpdateInput?,
5355
): DocumentView {
56+
val jsonToUse = input?.json ?: json!!
57+
5458
log("updateDocument called with id $documentId")
55-
secureLogger.debug("updateDocument with id {}: received json: {}", documentId, json)
56-
return mapToDocumentView(documentService.updateDocument(documentId, json))
59+
secureLogger.debug("updateDocument with id {}: current version: {} received json: {}", documentId, input?.currentVersion, jsonToUse)
60+
return mapToDocumentView(documentService.updateDocument(
61+
documentId = documentId,
62+
json = jsonToUse,
63+
currentVersion = input?.currentVersion,
64+
))
5765
}
5866

5967
@Operation(
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package no.nav.klage.document.api.views
2+
3+
data class DocumentUpdateInput(
4+
val json: String,
5+
val currentVersion: Int?,
6+
)

src/main/kotlin/no/nav/klage/document/service/DocumentService.kt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import no.nav.klage.document.repositories.CommentRepository
77
import no.nav.klage.document.repositories.DocumentRepository
88
import no.nav.klage.document.repositories.DocumentVersionRepository
99
import no.nav.klage.document.util.TokenUtil
10+
import no.nav.klage.document.util.getLogger
11+
import no.nav.klage.document.util.getSecureLogger
1012
import org.springframework.stereotype.Service
1113
import org.springframework.transaction.annotation.Transactional
1214
import java.time.LocalDateTime
@@ -21,6 +23,12 @@ class DocumentService(
2123
private val tokenUtil: TokenUtil,
2224
) {
2325

26+
companion object {
27+
@Suppress("JAVA_CLASS_ON_COMPANION")
28+
private val logger = getLogger(javaClass.enclosingClass)
29+
private val secureLogger = getSecureLogger()
30+
}
31+
2432
fun createDocument(json: String): DocumentVersion {
2533
val now = LocalDateTime.now()
2634

@@ -43,9 +51,14 @@ class DocumentService(
4351
)
4452
}
4553

46-
fun updateDocument(documentId: UUID, json: String): DocumentVersion {
54+
fun updateDocument(documentId: UUID, json: String, currentVersion: Int?): DocumentVersion {
4755
val now = LocalDateTime.now()
4856
val latestVersionNumber = documentVersionRepository.findLatestVersionNumber(documentId = documentId)
57+
58+
if (currentVersion != null && latestVersionNumber != currentVersion) {
59+
logger.warn("latest version {} does not match clients current version {}", latestVersionNumber, currentVersion)
60+
}
61+
4962
val documentVersion = documentVersionRepository.findByDocumentIdAndVersion(documentId = documentId, version = latestVersionNumber)
5063
return documentVersionRepository.save(
5164
DocumentVersion(

0 commit comments

Comments
 (0)