MCPcopy
hub / github.com/dev-mastery/comments-api / makeRemoveComment

Function makeRemoveComment

src/use-cases/remove-comment.js:3–94  ·  view source on GitHub ↗
({ commentsDb })

Source from the content-addressed store, hash-verified

1import makeComment from '../comment'
2
3export default function makeRemoveComment ({ commentsDb }) {
4 return async function removeComment ({ id } = {}) {
5 if (!id) {
6 throw new Error('You must supply a comment id.')
7 }
8
9 const commentToDelete = await commentsDb.findById({ id })
10
11 if (!commentToDelete) {
12 return deleteNothing()
13 }
14
15 if (await hasReplies(commentToDelete)) {
16 return softDelete(commentToDelete)
17 }
18
19 if (await isOnlyReplyOfDeletedParent(commentToDelete)) {
20 return deleteCommentAndParent(commentToDelete)
21 }
22
23 return hardDelete(commentToDelete)
24 }
25
26 async function hasReplies ({ id: commentId }) {
27 const replies = await commentsDb.findReplies({
28 commentId,
29 publishedOnly: false
30 })
31 return replies.length > 0
32 }
33
34 async function isOnlyReplyOfDeletedParent (comment) {
35 if (!comment.replyToId) {
36 return false
37 }
38 const parent = await commentsDb.findById({ id: comment.replyToId })
39 if (parent && makeComment(parent).isDeleted()) {
40 const replies = await commentsDb.findReplies({
41 commentId: parent.id,
42 publishedOnly: false
43 })
44 return replies.length === 1
45 }
46 return false
47 }
48
49 function deleteNothing () {
50 return {
51 deletedCount: 0,
52 softDelete: false,
53 message: 'Comment not found, nothing to delete.'
54 }
55 }
56
57 async function softDelete (commentInfo) {
58 const toDelete = makeComment(commentInfo)
59 toDelete.markDeleted()
60 await commentsDb.update({

Callers 2

index.jsFile · 0.85

Calls 6

deleteNothingFunction · 0.85
hasRepliesFunction · 0.85
softDeleteFunction · 0.85
deleteCommentAndParentFunction · 0.85
hardDeleteFunction · 0.85

Tested by

no test coverage detected