Skip to content

Commit a97cb4b

Browse files
authored
Merge pull request #93 from navikt/post-delete
Adds POST method for deleting comment. Requires body input.
2 parents 201de36 + 67d2661 commit a97cb4b

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import io.swagger.v3.oas.annotations.Operation
44
import io.swagger.v3.oas.annotations.tags.Tag
55
import no.nav.klage.document.api.views.CommentInput
66
import no.nav.klage.document.api.views.CommentView
7+
import no.nav.klage.document.api.views.DeleteCommentInput
78
import no.nav.klage.document.api.views.ModifyCommentInput
89
import no.nav.klage.document.config.SecurityConfiguration.Companion.ISSUER_AAD
910
import no.nav.klage.document.domain.Comment
@@ -117,17 +118,32 @@ class CommentsController(
117118
return mapCommentToView(commentService.getComment(commentId = commentId))
118119
}
119120

121+
@Deprecated("Use POST /{commentId}/delete")
120122
@Operation(
121123
summary = "Delete a given comment (includes possible thread)",
122124
description = "Delete a given comment (includes possible thread)"
123125
)
124126
@DeleteMapping("/{commentId}")
125-
fun deleteCommentWithPossibleThread(
127+
fun deleteCommentWithPossibleThreadDeprecated(
126128
@PathVariable("documentId") documentId: UUID,
127129
@PathVariable("commentId") commentId: UUID
130+
) {
131+
log("deleteCommentWithPossibleThreadDeprecated called with id $documentId and commentId $commentId")
132+
commentService.deleteComment(commentId = commentId, loggedInIdent = getIdent()!!, behandlingTildeltIdent = null)
133+
}
134+
135+
@Operation(
136+
summary = "Delete a given comment (includes possible thread)",
137+
description = "Delete a given comment (includes possible thread)"
138+
)
139+
@PostMapping("/{commentId}/delete")
140+
fun deleteCommentWithPossibleThread(
141+
@PathVariable("documentId") documentId: UUID,
142+
@PathVariable("commentId") commentId: UUID,
143+
@RequestBody deleteCommentInput: DeleteCommentInput
128144
) {
129145
log("deleteCommentWithPossibleThread called with id $documentId and commentId $commentId")
130-
commentService.deleteComment(commentId = commentId, loggedInIdent = getIdent()!!)
146+
commentService.deleteComment(commentId = commentId, loggedInIdent = getIdent()!!, behandlingTildeltIdent = deleteCommentInput.behandlingTildeltIdent)
131147
}
132148

133149
private fun mapCommentToView(comment: Comment): CommentView =

src/main/kotlin/no/nav/klage/document/api/views/CommentInput.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,7 @@ data class CommentInput(
1313
data class ModifyCommentInput(
1414
val text: String,
1515
)
16+
17+
data class DeleteCommentInput(
18+
val behandlingTildeltIdent: String?,
19+
)

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,16 @@ class CommentService(private val commentRepository: CommentRepository) {
6565
return comment
6666
}
6767

68-
fun deleteComment(commentId: UUID, loggedInIdent: String) {
68+
fun deleteComment(
69+
commentId: UUID,
70+
loggedInIdent: String,
71+
behandlingTildeltIdent: String?
72+
) {
73+
val loggedInIsDocumentOwner = loggedInIdent == behandlingTildeltIdent
6974
val comment = commentRepository.getReferenceById(commentId)
70-
if (comment.authorIdent != loggedInIdent) {
71-
throw MissingAccessException("Not allowed to delete others comment")
75+
if (!loggedInIsDocumentOwner && comment.authorIdent != loggedInIdent) {
76+
throw MissingAccessException("Not allowed to delete others comment when not document owner")
7277
}
7378
commentRepository.delete(comment)
7479
}
75-
7680
}

0 commit comments

Comments
 (0)