1
+ package no.nav.klage.document.api
2
+
3
+ import io.swagger.v3.oas.annotations.Operation
4
+ import io.swagger.v3.oas.annotations.tags.Tag
5
+ import no.nav.klage.document.api.views.CommentInput
6
+ import no.nav.klage.document.api.views.CommentView
7
+ import no.nav.klage.document.api.views.ModifyCommentInput
8
+ import no.nav.klage.document.config.SecurityConfiguration.Companion.ISSUER_AAD
9
+ import no.nav.klage.document.domain.Comment
10
+ import no.nav.klage.document.service.CommentService
11
+ import no.nav.klage.document.util.getLogger
12
+ import no.nav.klage.document.util.getSecureLogger
13
+ import no.nav.security.token.support.core.api.ProtectedWithClaims
14
+ import no.nav.security.token.support.core.context.TokenValidationContextHolder
15
+ import org.springframework.web.bind.annotation.*
16
+ import java.util.*
17
+
18
+ @RestController
19
+ @ProtectedWithClaims(issuer = ISSUER_AAD )
20
+ @Tag(name = " kabal-smart-editor-api" )
21
+ @RequestMapping(" /documents/{documentId}/comments" )
22
+ class CommentsController (
23
+ private val commentService : CommentService ,
24
+ private val tokenValidationContextHolder : TokenValidationContextHolder
25
+ ) {
26
+
27
+ companion object {
28
+ @Suppress(" JAVA_CLASS_ON_COMPANION" )
29
+ private val logger = getLogger(javaClass.enclosingClass)
30
+ private val secureLogger = getSecureLogger()
31
+ }
32
+
33
+ @Operation(
34
+ summary = " Create comment for a given document" ,
35
+ description = " Create comment for a given document"
36
+ )
37
+ @PostMapping
38
+ fun createComment (
39
+ @PathVariable(" documentId" ) documentId : UUID ,
40
+ @RequestBody commentInput : CommentInput
41
+ ): CommentView {
42
+ log(" createComment called with id $documentId " )
43
+ return mapCommentToView(
44
+ commentService.createComment(
45
+ documentId = documentId,
46
+ text = commentInput.text,
47
+ authorName = commentInput.author.name,
48
+ authorIdent = commentInput.author.ident
49
+ )
50
+ )
51
+ }
52
+
53
+ @Operation(
54
+ summary = " Get all comments for a given document" ,
55
+ description = " Get all comments for a given document"
56
+ )
57
+ @GetMapping
58
+ fun getAllCommentsWithPossibleThreads (
59
+ @PathVariable(" documentId" ) documentId : UUID
60
+ ): List <CommentView > {
61
+ log(" getAllCommentsWithPossibleThreads called with id $documentId " )
62
+ return commentService.getComments(documentId).map { mapCommentToView(it) }
63
+ }
64
+
65
+ @Operation(
66
+ summary = " Reply to a given comment" ,
67
+ description = " Reply to a given comment"
68
+ )
69
+ @PostMapping(" /{commentId}/replies" )
70
+ fun replyToComment (
71
+ @PathVariable(" documentId" ) documentId : UUID ,
72
+ @PathVariable(" commentId" ) commentId : UUID ,
73
+ @RequestBody commentInput : CommentInput ,
74
+ ): CommentView {
75
+ log(" replyToComment called with id $documentId and commentId $commentId " )
76
+ return mapCommentToView(
77
+ commentService.replyToComment(
78
+ documentId = documentId,
79
+ parentCommentId = commentId,
80
+ text = commentInput.text,
81
+ authorName = commentInput.author.name,
82
+ authorIdent = commentInput.author.ident
83
+ )
84
+ )
85
+ }
86
+
87
+ @Operation(
88
+ summary = " Modify a given comment" ,
89
+ description = " Modify a given comment"
90
+ )
91
+ @PatchMapping(" /{commentId}" )
92
+ fun modifyComment (
93
+ @PathVariable(" documentId" ) documentId : UUID ,
94
+ @PathVariable(" commentId" ) commentId : UUID ,
95
+ @RequestBody modifyCommentInput : ModifyCommentInput ,
96
+ ): CommentView {
97
+ log(" modifyComment called with id $documentId and commentId $commentId " )
98
+ return mapCommentToView(
99
+ commentService.setCommentText(
100
+ commentId = commentId,
101
+ text = modifyCommentInput.text,
102
+ loggedInIdent = getIdent()!!
103
+ )
104
+ )
105
+ }
106
+
107
+ @Operation(
108
+ summary = " Get a given comment" ,
109
+ description = " Get a given comment"
110
+ )
111
+ @GetMapping(" /{commentId}" )
112
+ fun getCommentWithPossibleThread (
113
+ @PathVariable(" documentId" ) documentId : UUID ,
114
+ @PathVariable(" commentId" ) commentId : UUID
115
+ ): CommentView {
116
+ log(" getCommentWithPossibleThread called with id $documentId and commentId $commentId " )
117
+ return mapCommentToView(commentService.getComment(commentId = commentId))
118
+ }
119
+
120
+ @Operation(
121
+ summary = " Delete a given comment (includes possible thread)" ,
122
+ description = " Delete a given comment (includes possible thread)"
123
+ )
124
+ @DeleteMapping(" /{commentId}" )
125
+ fun deleteCommentWithPossibleThread (
126
+ @PathVariable(" documentId" ) documentId : UUID ,
127
+ @PathVariable(" commentId" ) commentId : UUID
128
+ ) {
129
+ log(" deleteCommentWithPossibleThread called with id $documentId and commentId $commentId " )
130
+ commentService.deleteComment(commentId = commentId, loggedInIdent = getIdent()!! )
131
+ }
132
+
133
+ private fun mapCommentToView (comment : Comment ): CommentView =
134
+ CommentView (
135
+ id = comment.id,
136
+ text = comment.text,
137
+ author = CommentView .Author (
138
+ name = comment.authorName,
139
+ ident = comment.authorIdent
140
+ ),
141
+ comments = comment.comments.map { mapCommentToView(it) },
142
+ created = comment.created,
143
+ modified = comment.modified
144
+ )
145
+
146
+ private fun log (message : String ) {
147
+ logger.debug(message)
148
+ secureLogger.debug(" {}. On-behalf-of: {}" , message, getIdent())
149
+ }
150
+
151
+ fun getIdent (): String? =
152
+ tokenValidationContextHolder.tokenValidationContext.getJwtToken(ISSUER_AAD )
153
+ .jwtTokenClaims?.get(" NAVident" )?.toString()
154
+
155
+ }
0 commit comments