Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ dependencies {
implementation("org.postgresql:postgresql")

implementation("io.micrometer:micrometer-registry-prometheus")
implementation("io.micrometer:micrometer-tracing-bridge-brave")

implementation("ch.qos.logback:logback-classic")
implementation("net.logstash.logback:logstash-logback-encoder:$logstashVersion")
Expand Down
6 changes: 3 additions & 3 deletions deploy/nais.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ spec:
timeout: 1
resources:
limits:
memory: 1024Mi
memory: 2024Mi
requests:
cpu: 40m
memory: 256Mi
cpu: 80m
memory: 1024Mi
ingresses:
{{#each ingresses as |ingress|}}
- {{ingress}}
Expand Down
58 changes: 14 additions & 44 deletions src/main/kotlin/no/nav/klage/document/api/DocumentController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import no.nav.klage.document.api.views.DocumentUpdateInput
import no.nav.klage.document.api.views.DocumentVersionView
import no.nav.klage.document.api.views.DocumentView
import no.nav.klage.document.config.SecurityConfiguration.Companion.ISSUER_AAD
import no.nav.klage.document.domain.DocumentVersion
import no.nav.klage.document.service.DocumentService
import no.nav.klage.document.util.TokenUtil
import no.nav.klage.document.util.getLogger
Expand Down Expand Up @@ -40,7 +39,7 @@ class DocumentController(
): DocumentView {
log("createDocument")
secureLogger.debug("createDocument: received json: {}", input.json)
return mapToDocumentView(documentService.createDocument(json = input.json, data = input.data))
return documentService.createDocument(json = input.json, data = input.data)
}

@Operation(
Expand All @@ -61,24 +60,20 @@ class DocumentController(
)

return try {
mapToDocumentView(
documentService.updateDocument(
documentId = documentId,
json = input.json,
data = input.data,
currentVersion = input.currentVersion,
)
documentService.updateDocument(
documentId = documentId,
json = input.json,
data = input.data,
currentVersion = input.currentVersion,
)

} catch (e: Exception) {
logger.warn("Failed to update document $documentId. Trying one more time.", e)

mapToDocumentView(
documentService.updateDocument(
documentId = documentId,
json = input.json,
data = input.data,
currentVersion = input.currentVersion,
)
documentService.updateDocument(
documentId = documentId,
json = input.json,
data = input.data,
currentVersion = input.currentVersion,
)
}
}
Expand All @@ -93,7 +88,7 @@ class DocumentController(
@PathVariable("version", required = false) version: Int?,
): DocumentView {
log("getDocument called with id $documentId and version $version")
return mapToDocumentView(documentService.getDocument(documentId = documentId, version = version))
return documentService.getDocument(documentId = documentId, version = version)
}

@Operation(
Expand All @@ -113,34 +108,9 @@ class DocumentController(
@GetMapping("/{documentId}/versions")
fun getDocumentVersions(@PathVariable("documentId") documentId: UUID): List<DocumentVersionView> {
log("getDocumentVersions called with id $documentId")
val documentVersions = documentService.getDocumentVersions(documentId = documentId)

return documentVersions.map {
mapToDocumentVersionView(it)
}
return documentService.getDocumentVersions(documentId = documentId)
}

private fun mapToDocumentView(documentVersion: DocumentVersion): DocumentView =
DocumentView(
id = documentVersion.documentId,
documentId = documentVersion.documentId,
version = documentVersion.version,
json = documentVersion.json,
data = documentVersion.data,
authorNavIdent = documentVersion.authorNavIdent,
created = documentVersion.created,
modified = documentVersion.modified
)

private fun mapToDocumentVersionView(documentVersion: DocumentVersion): DocumentVersionView =
DocumentVersionView(
documentId = documentVersion.documentId,
version = documentVersion.version,
authorNavIdent = documentVersion.authorNavIdent,
created = documentVersion.created,
modified = documentVersion.modified
)

private fun log(message: String) {
logger.debug(message)
secureLogger.debug("{}. On-behalf-of: {}", message, tokenUtil.getIdentNullable())
Expand Down
36 changes: 0 additions & 36 deletions src/main/kotlin/no/nav/klage/document/config/CustomTraceFilter.kt

This file was deleted.

6 changes: 3 additions & 3 deletions src/main/kotlin/no/nav/klage/document/domain/Document.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import java.util.*
class Document(
@Id
val id: UUID = UUID.randomUUID(),
@Column(name = "data")
var data: String?,
@Column(name = "created")
val created: LocalDateTime,
@Column(name = "modified")
Expand All @@ -23,9 +25,7 @@ class Document(

other as Document

if (id != other.id) return false

return true
return id == other.id
}

override fun hashCode(): Int {
Expand Down
17 changes: 13 additions & 4 deletions src/main/kotlin/no/nav/klage/document/domain/DocumentVersion.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ class DocumentVersion(
val version: Int,
@Column(name = "json")
var json: String,
@Column(name = "data")
var data: String?,
@Column(name = "created")
val created: LocalDateTime,
@Column(name = "modified")
Expand All @@ -40,8 +38,19 @@ class DocumentVersion(
}

override fun toString(): String {
return "DocumentVersion(documentId=$documentId, version=$version, json='$json', data=$data, created=$created, modified=$modified, authorNavIdent='$authorNavIdent')"
return "DocumentVersion(documentId=$documentId, version=$version, json='$json', created=$created, modified=$modified, authorNavIdent='$authorNavIdent')"
}


}

/**
* Using this when we don't need the full DocumentVersion object, just a subset of the fields.
* Otherwise, we would have to fetch the full object from the database and that's too much data.
*/
data class ShortDocumentVersion(
val documentId: UUID,
val version: Int,
val authorNavIdent: String,
val created: LocalDateTime,
val modified: LocalDateTime,
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,24 @@ package no.nav.klage.document.repositories

import no.nav.klage.document.domain.DocumentVersion
import no.nav.klage.document.domain.DocumentVersionId
import no.nav.klage.document.domain.ShortDocumentVersion
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Query
import java.util.*

interface DocumentVersionRepository : JpaRepository<DocumentVersion, DocumentVersionId> {

fun findByDocumentId(documentId: UUID): List<DocumentVersion>

@Query(
"""
SELECT new no.nav.klage.document.domain.ShortDocumentVersion(documentId, version, authorNavIdent, created, modified)
FROM DocumentVersion
WHERE documentId = :documentId
ORDER BY version
"""
)
fun findVersionsByDocumentId(documentId: UUID): List<ShortDocumentVersion>

fun deleteByDocumentId(documentId: UUID)
}
100 changes: 69 additions & 31 deletions src/main/kotlin/no/nav/klage/document/service/DocumentService.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package no.nav.klage.document.service

import no.nav.klage.document.api.views.DocumentVersionView
import no.nav.klage.document.api.views.DocumentView
import no.nav.klage.document.domain.Document
import no.nav.klage.document.domain.DocumentVersion
import no.nav.klage.document.domain.DocumentVersionId
import no.nav.klage.document.domain.ShortDocumentVersion
import no.nav.klage.document.repositories.CommentRepository
import no.nav.klage.document.repositories.DocumentRepository
import no.nav.klage.document.repositories.DocumentVersionRepository
Expand Down Expand Up @@ -31,30 +34,33 @@ class DocumentService(
private val secureLogger = getSecureLogger()
}

fun createDocument(json: String, data: String?): DocumentVersion {
fun createDocument(json: String, data: String?): DocumentView {
val now = LocalDateTime.now()

val document = documentRepository.save(
Document(
data = data,
created = now,
modified = now,
)
)

return documentVersionRepository.save(
DocumentVersion(
documentId = document.id,
version = 1,
json = json,
data = data,
authorNavIdent = tokenUtil.getIdent(),
created = now,
modified = now,
)
return mapToDocumentView(
documentVersionRepository.save(
DocumentVersion(
documentId = document.id,
version = 1,
json = json,
authorNavIdent = tokenUtil.getIdent(),
created = now,
modified = now,
)
),
document = document
)
}

fun updateDocument(documentId: UUID, json: String, data: String?, currentVersion: Int?): DocumentVersion {
fun updateDocument(documentId: UUID, json: String, data: String?, currentVersion: Int?): DocumentView {
val now = LocalDateTime.now()
val latestVersionNumber = latestDocumentRepository.findById(documentId).get().currentVersion

Expand Down Expand Up @@ -83,27 +89,37 @@ class DocumentService(
version = latestVersionNumber
)
).get()
return documentVersionRepository.save(
DocumentVersion(
documentId = documentVersion.documentId,
version = latestVersionNumber + 1,
json = json,
data = data,
created = now,
modified = now,
authorNavIdent = tokenUtil.getIdent()
)

val document = documentRepository.findById(documentId).get()
document.data = data
document.modified = now

return mapToDocumentView(
documentVersion = documentVersionRepository.save(
DocumentVersion(
documentId = documentVersion.documentId,
version = latestVersionNumber + 1,
json = json,
created = now,
modified = now,
authorNavIdent = tokenUtil.getIdent()
)
),
document = document
)
}

fun getDocument(documentId: UUID, version: Int?): DocumentVersion {
fun getDocument(documentId: UUID, version: Int?): DocumentView {
val versionToUse = version ?: latestDocumentRepository.findById(documentId).get().currentVersion
return documentVersionRepository.findById(
DocumentVersionId(
documentId = documentId,
version = versionToUse
)
).get()
return mapToDocumentView(
documentVersion = documentVersionRepository.findById(
DocumentVersionId(
documentId = documentId,
version = versionToUse
)
).get(),
document = documentRepository.findById(documentId).get()
)
}

fun deleteDocument(documentId: UUID) {
Expand All @@ -113,8 +129,30 @@ class DocumentService(
documentRepository.deleteById(documentId)
}

fun getDocumentVersions(documentId: UUID): List<DocumentVersion> {
return documentVersionRepository.findByDocumentId(documentId = documentId).sortedBy { it.version }
fun getDocumentVersions(documentId: UUID): List<DocumentVersionView> {
return documentVersionRepository.findVersionsByDocumentId(documentId = documentId)
.map { mapToDocumentVersionView(it) }
}

private fun mapToDocumentView(documentVersion: DocumentVersion, document: Document): DocumentView =
DocumentView(
id = documentVersion.documentId,
documentId = documentVersion.documentId,
version = documentVersion.version,
json = documentVersion.json,
data = document.data,
authorNavIdent = documentVersion.authorNavIdent,
created = documentVersion.created,
modified = documentVersion.modified
)

private fun mapToDocumentVersionView(documentVersion: ShortDocumentVersion): DocumentVersionView =
DocumentVersionView(
documentId = documentVersion.documentId,
version = documentVersion.version,
authorNavIdent = documentVersion.authorNavIdent,
created = documentVersion.created,
modified = documentVersion.modified
)

}
2 changes: 1 addition & 1 deletion src/main/resources/application-dev-gcp.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
spring:
datasource:
hikari:
maximum-pool-size: 5
maximum-pool-size: 4
Loading