Skip to content

Commit e1a97e3

Browse files
authored
Merge pull request #156 from navikt/versions
Support for versions.
2 parents 3c6e179 + 5f1a568 commit e1a97e3

File tree

16 files changed

+452
-219
lines changed

16 files changed

+452
-219
lines changed

deploy/nais.yaml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,6 @@ spec:
6666
rules:
6767
- application: kabal-frontend
6868
- application: kabal-api
69-
outbound:
70-
rules:
71-
- application: kabal-json-to-pdf
7269
azure:
7370
application:
7471
enabled: true

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

Lines changed: 46 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,16 @@ 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
6+
import no.nav.klage.document.api.views.DocumentVersionView
57
import no.nav.klage.document.api.views.DocumentView
68
import no.nav.klage.document.config.SecurityConfiguration.Companion.ISSUER_AAD
7-
import no.nav.klage.document.domain.Document
9+
import no.nav.klage.document.domain.DocumentVersion
810
import no.nav.klage.document.service.DocumentService
11+
import no.nav.klage.document.util.TokenUtil
912
import no.nav.klage.document.util.getLogger
1013
import no.nav.klage.document.util.getSecureLogger
1114
import no.nav.security.token.support.core.api.ProtectedWithClaims
12-
import no.nav.security.token.support.core.context.TokenValidationContextHolder
13-
import org.springframework.http.HttpHeaders
14-
import org.springframework.http.HttpStatus
15-
import org.springframework.http.MediaType
16-
import org.springframework.http.ResponseEntity
1715
import org.springframework.web.bind.annotation.*
1816
import java.util.*
1917

@@ -23,7 +21,7 @@ import java.util.*
2321
@RequestMapping("/documents")
2422
class DocumentController(
2523
private val documentService: DocumentService,
26-
private val tokenValidationContextHolder: TokenValidationContextHolder
24+
private val tokenUtil: TokenUtil,
2725
) {
2826

2927
companion object {
@@ -52,21 +50,28 @@ class DocumentController(
5250
@PutMapping("/{documentId}")
5351
fun updateDocument(
5452
@PathVariable("documentId") documentId: UUID,
55-
@RequestBody json: String
53+
@RequestBody(required = false) input: DocumentUpdateInput,
5654
): DocumentView {
5755
log("updateDocument called with id $documentId")
58-
secureLogger.debug("updateDocument with id {}: received json: {}", documentId, json)
59-
return mapToDocumentView(documentService.updateDocument(documentId, json))
56+
secureLogger.debug("updateDocument with id {}: current version: {} received json: {}", documentId, input.currentVersion, input.json)
57+
return mapToDocumentView(documentService.updateDocument(
58+
documentId = documentId,
59+
json = input.json,
60+
currentVersion = input.currentVersion,
61+
))
6062
}
6163

6264
@Operation(
6365
summary = "Get document",
6466
description = "Get document"
6567
)
66-
@GetMapping("/{documentId}")
67-
fun getDocument(@PathVariable("documentId") documentId: UUID): DocumentView {
68-
log("getDocument called with id $documentId")
69-
return mapToDocumentView(documentService.getDocument(documentId))
68+
@GetMapping("/{documentId}", "/{documentId}/versions/{version}")
69+
fun getDocument(
70+
@PathVariable("documentId") documentId: UUID,
71+
@PathVariable("version", required = false) version: Int?,
72+
): DocumentView {
73+
log("getDocument called with id $documentId and version $version")
74+
return mapToDocumentView(documentService.getDocument(documentId = documentId, version = version))
7075
}
7176

7277
@Operation(
@@ -80,43 +85,42 @@ class DocumentController(
8085
}
8186

8287
@Operation(
83-
summary = "Generer PDF",
84-
description = "Generer PDF"
88+
summary = "Get document versions",
89+
description = "Get document versions"
8590
)
86-
@ResponseBody
87-
@GetMapping("/{documentId}/pdf")
88-
fun getDocumentAsPDF(
89-
@PathVariable("documentId") documentId: UUID
90-
): ResponseEntity<ByteArray> {
91-
log("getDocumentAsPDF with id : $documentId")
92-
93-
val pdfDocument = documentService.getDocumentAsPDF(documentId)
91+
@GetMapping("/{documentId}/versions")
92+
fun getDocumentVersions(@PathVariable("documentId") documentId: UUID): List<DocumentVersionView> {
93+
log("getDocumentVersions called with id $documentId")
94+
val documentVersions = documentService.getDocumentVersions(documentId = documentId)
9495

95-
val responseHeaders = HttpHeaders()
96-
responseHeaders.contentType = MediaType.APPLICATION_PDF
97-
responseHeaders.add("Content-Disposition", "inline; filename=${pdfDocument.filename}.pdf")
98-
return ResponseEntity(
99-
pdfDocument.bytes,
100-
responseHeaders,
101-
HttpStatus.OK
102-
)
96+
return documentVersions.map {
97+
mapToDocumentVersionView(it)
98+
}
10399
}
104100

105-
private fun mapToDocumentView(document: Document): DocumentView =
101+
private fun mapToDocumentView(documentVersion: DocumentVersion): DocumentView =
106102
DocumentView(
107-
id = document.id,
108-
json = document.json,
109-
created = document.created,
110-
modified = document.modified
103+
id = documentVersion.documentId,
104+
documentId = documentVersion.documentId,
105+
version = documentVersion.version,
106+
json = documentVersion.json,
107+
authorNavIdent = documentVersion.authorNavIdent,
108+
created = documentVersion.created,
109+
modified = documentVersion.modified
110+
)
111+
112+
private fun mapToDocumentVersionView(documentVersion: DocumentVersion): DocumentVersionView =
113+
DocumentVersionView(
114+
documentId = documentVersion.documentId,
115+
version = documentVersion.version,
116+
authorNavIdent = documentVersion.authorNavIdent,
117+
created = documentVersion.created,
118+
modified = documentVersion.modified
111119
)
112120

113121
private fun log(message: String) {
114122
logger.debug(message)
115-
secureLogger.debug("{}. On-behalf-of: {}", message, getIdent())
123+
secureLogger.debug("{}. On-behalf-of: {}", message, tokenUtil.getIdent())
116124
}
117125

118-
fun getIdent(): String? =
119-
tokenValidationContextHolder.tokenValidationContext.getJwtToken(ISSUER_AAD)
120-
.jwtTokenClaims?.get("NAVident")?.toString()
121-
122126
}
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/api/views/DocumentView.kt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,19 @@ import java.time.LocalDateTime
44
import java.util.*
55

66
data class DocumentView(
7-
val id: UUID,
87
val json: String,
8+
val documentId: UUID,
9+
val id: UUID,
10+
val version: Int,
11+
val authorNavIdent: String?,
912
val created: LocalDateTime,
1013
val modified: LocalDateTime
1114
)
15+
16+
data class DocumentVersionView(
17+
val documentId: UUID,
18+
val version: Int,
19+
val authorNavIdent: String?,
20+
val created: LocalDateTime,
21+
val modified: LocalDateTime,
22+
)

src/main/kotlin/no/nav/klage/document/clients/KabalJsonToPdfClient.kt

Lines changed: 0 additions & 37 deletions
This file was deleted.

src/main/kotlin/no/nav/klage/document/config/KabalJsonToPdfClientConfiguration.kt

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/main/kotlin/no/nav/klage/document/domain/Document.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,10 @@ import java.util.*
1212
class Document(
1313
@Id
1414
val id: UUID = UUID.randomUUID(),
15-
@Column(name = "json")
16-
var json: String,
1715
@Column(name = "created")
1816
val created: LocalDateTime,
1917
@Column(name = "modified")
20-
var modified: LocalDateTime
18+
var modified: LocalDateTime,
2119
) {
2220
override fun equals(other: Any?): Boolean {
2321
if (this === other) return true
@@ -35,7 +33,7 @@ class Document(
3533
}
3634

3735
override fun toString(): String {
38-
return "Document(id=$id, json='$json', created=$created, modified=$modified)"
36+
return "Document(id=$id, created=$created, modified=$modified)"
3937
}
4038

4139
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package no.nav.klage.document.domain
2+
3+
import jakarta.persistence.Column
4+
import jakarta.persistence.Entity
5+
import jakarta.persistence.Id
6+
import jakarta.persistence.IdClass
7+
import jakarta.persistence.Table
8+
import java.time.LocalDateTime
9+
import java.util.*
10+
11+
@Entity
12+
@Table(name = "document_version", schema = "klage")
13+
@IdClass(DocumentVersionId::class)
14+
class DocumentVersion(
15+
@Id
16+
val documentId: UUID,
17+
@Id
18+
val version: Int,
19+
@Column(name = "json")
20+
var json: String,
21+
@Column(name = "created")
22+
val created: LocalDateTime,
23+
@Column(name = "modified")
24+
var modified: LocalDateTime,
25+
@Column(name = "author_nav_ident")
26+
var authorNavIdent: String,
27+
) {
28+
override fun equals(other: Any?): Boolean {
29+
if (this === other) return true
30+
if (javaClass != other?.javaClass) return false
31+
32+
other as DocumentVersion
33+
34+
if (documentId != other.documentId) return false
35+
if (version != other.version) return false
36+
37+
return true
38+
}
39+
40+
override fun hashCode(): Int {
41+
return documentId.hashCode()
42+
}
43+
44+
override fun toString(): String {
45+
return "Document(id=$documentId, version=$version, json='$json', created=$created, modified=$modified, authorNavIdent='$authorNavIdent')"
46+
}
47+
48+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package no.nav.klage.document.domain
2+
3+
import jakarta.persistence.Embeddable
4+
import java.io.Serializable
5+
import java.util.*
6+
7+
@Embeddable
8+
data class DocumentVersionId(
9+
val documentId: UUID,
10+
val version: Int = 1,
11+
): Serializable

src/main/kotlin/no/nav/klage/document/domain/PDFDocument.kt

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)