@@ -2,18 +2,16 @@ package no.nav.klage.document.api
2
2
3
3
import io.swagger.v3.oas.annotations.Operation
4
4
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
5
7
import no.nav.klage.document.api.views.DocumentView
6
8
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
8
10
import no.nav.klage.document.service.DocumentService
11
+ import no.nav.klage.document.util.TokenUtil
9
12
import no.nav.klage.document.util.getLogger
10
13
import no.nav.klage.document.util.getSecureLogger
11
14
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
17
15
import org.springframework.web.bind.annotation.*
18
16
import java.util.*
19
17
@@ -23,7 +21,7 @@ import java.util.*
23
21
@RequestMapping(" /documents" )
24
22
class DocumentController (
25
23
private val documentService : DocumentService ,
26
- private val tokenValidationContextHolder : TokenValidationContextHolder
24
+ private val tokenUtil : TokenUtil ,
27
25
) {
28
26
29
27
companion object {
@@ -52,21 +50,28 @@ class DocumentController(
52
50
@PutMapping(" /{documentId}" )
53
51
fun updateDocument (
54
52
@PathVariable(" documentId" ) documentId : UUID ,
55
- @RequestBody json : String
53
+ @RequestBody(required = false ) input : DocumentUpdateInput ,
56
54
): DocumentView {
57
55
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
+ ))
60
62
}
61
63
62
64
@Operation(
63
65
summary = " Get document" ,
64
66
description = " Get document"
65
67
)
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))
70
75
}
71
76
72
77
@Operation(
@@ -80,43 +85,42 @@ class DocumentController(
80
85
}
81
86
82
87
@Operation(
83
- summary = " Generer PDF " ,
84
- description = " Generer PDF "
88
+ summary = " Get document versions " ,
89
+ description = " Get document versions "
85
90
)
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)
94
95
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
+ }
103
99
}
104
100
105
- private fun mapToDocumentView (document : Document ): DocumentView =
101
+ private fun mapToDocumentView (documentVersion : DocumentVersion ): DocumentView =
106
102
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
111
119
)
112
120
113
121
private fun log (message : String ) {
114
122
logger.debug(message)
115
- secureLogger.debug(" {}. On-behalf-of: {}" , message, getIdent())
123
+ secureLogger.debug(" {}. On-behalf-of: {}" , message, tokenUtil. getIdent())
116
124
}
117
125
118
- fun getIdent (): String? =
119
- tokenValidationContextHolder.tokenValidationContext.getJwtToken(ISSUER_AAD )
120
- .jwtTokenClaims?.get(" NAVident" )?.toString()
121
-
122
126
}
0 commit comments