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

Function makeEditComment

src/use-cases/edit-comment.js:2–29  ·  view source on GitHub ↗
({ commentsDb, handleModeration })

Source from the content-addressed store, hash-verified

1import makeComment from '../comment'
2export default function makeEditComment ({ commentsDb, handleModeration }) {
3 return async function editComment ({ id, ...changes } = {}) {
4 if (!id) {
5 throw new Error('You must supply an id.')
6 }
7 if (!changes.text) {
8 throw new Error('You must supply text.')
9 }
10 const existing = await commentsDb.findById({ id })
11
12 if (!existing) {
13 throw new RangeError('Comment not found.')
14 }
15 const comment = makeComment({ ...existing, ...changes, modifiedOn: null })
16 if (comment.getHash() === existing.hash) {
17 return existing
18 }
19 const moderated = await handleModeration({ comment })
20 const updated = await commentsDb.update({
21 id: moderated.getId(),
22 published: moderated.isPublished(),
23 modifiedOn: moderated.getModifiedOn(),
24 text: moderated.getText(),
25 hash: moderated.getHash()
26 })
27 return { ...existing, ...updated }
28 }
29}

Callers 2

index.jsFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected