Update post factories with guard clause

This commit is contained in:
mattwr18 2020-01-27 17:32:09 +01:00
parent b48b93dd0d
commit d6a5b7ce65

View File

@ -53,17 +53,18 @@ export default function create() {
const post = await neodeInstance.create('Post', args) const post = await neodeInstance.create('Post', args)
let { comment, commentContent } = args const { commentContent } = args
delete args.comment let comment
delete args.commentContent delete args.commentContent
if (comment && commentContent) throw new Error('You provided both comment and commentContent') if (commentContent)
if (commentContent) comment = await neodeInstance.find('Comment', commentContent) comment = await factoryInstance.create('Comment', {
comment = contentExcerpt: commentContent,
comment || post,
(await factoryInstance.create('Comment', { content: commentContent, post, author })) author,
})
await post.relateTo(author, 'author') await post.relateTo(author, 'author')
await post.relateTo(comment, 'comments') if (comment) await post.relateTo(comment, 'comments')
await Promise.all(categories.map(c => c.relateTo(post, 'post'))) await Promise.all(categories.map(c => c.relateTo(post, 'post')))
await Promise.all(tags.map(t => t.relateTo(post, 'post'))) await Promise.all(tags.map(t => t.relateTo(post, 'post')))
return post return post