const { GraphQLYogaError } = require('@graphql-yoga/node')

export async function addPostToCollection(userId, collectionId, postId) {
  const collection = await Collection.findValidCollectionForUser(userId, collectionId)

  // TODO: Validate that the post can be added to the collection
  const post = await Post.find(postId)

  if (!post) {
    throw new GraphQLYogaError('Not a valid post')
  }

  const order = await CollectionsPost.query(q => {
    q.select(bookshelf.knex.raw('max("order") as max_order'))
    q.where({ collection_id: collectionId })
  })
  .fetch()
  .then(result => result.get('max_order'))

  await new CollectionsPost({ user_id: userId, collection_id: collectionId, post_id: post.id, order: order ? order + 1 : 0 }).save()

  return { success: true }
}

export function createCollection(userId, data) {
  const whitelist = mapKeys(pick(data, ['name', 'groupId']), (v, k) => snakeCase(k))
  if (isEmpty(whitelist)) return Promise.resolve(null)
  return Collection.create({ userId, ...data })
}

export async function removePostFromCollection(userId, collectionId, postId) {
  const collection = await Collection.findValidCollectionForUser(userId, collectionId)

  const linkedPost = await CollectionsPost.query(q => q.where({ collection_id: collectionId, post_id: postId })).fetch()

  if (!linkedPost) {
    throw new GraphQLYogaError('Not a valid post')
  }

  await linkedPost.destroy()

  return { success: true }
}

export async function reorderPostInCollection(userId, collectionId, postId, newOrderIndex) {
  const collection = await Collection.findValidCollectionForUser(userId, collectionId)
  const linkedPost = await CollectionsPost.query(q => q.where({ collection_id: collectionId, post_id: postId })).fetch()

  if (!linkedPost) {
    throw new GraphQLYogaError('Not a valid post')
  }

  const oldOrder = linkedPost.get('order')

  if (oldOrder > newOrderIndex) {
    await CollectionsPost.query()
      .where({ collection_id: collectionId })
      .andWhere('order', '>=', newOrderIndex)
      .andWhere('order', '<', oldOrder)
      .update({ order: bookshelf.knex.raw('?? + 1', ['order']) })
  } else if (oldOrder < newOrderIndex) {
    await CollectionsPost.query()
      .where({ collection_id: collectionId })
      .andWhere('order', '<=', newOrderIndex)
      .andWhere('order', '>', oldOrder)
      .update({ order: bookshelf.knex.raw('?? - 1', ['order']) })
  }

  await linkedPost.save({ order: newOrderIndex })

  return { success: true }
}

@@ -1,12 +1,71 @@
+import %7B isEmpty, mapKeys, pick, snakeCase %7D from 'lodash'%0A
 const %7B Grap

Fix bugs in creating collections