({ commentsDb, handleModeration })
| 1 | import makeComment from '../comment' |
| 2 | export 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 | } |
no outgoing calls
no test coverage detected