class CollaboratorsController < ApplicationController

  before_action :login_required

  def create
    account = Account.lookup(pick(params, :email)[:email])
    return not_found if !account
    return conflict if account.id == current_account.id
    return bad_request(:error => 'That account has been disabled.') if account.role == Account::DISABLED
    
    success = current_project.add_collaborator account
    # TODO: Are we actually expecting the contents of `account`? [JR]
    # - If we're using `account.errors`, can be reduced to `json(account)`
    # - If we're not using it, can be reduced to `conflict`
    return json(account, 409) unless success
    json account.to_json(:include_organization => true)
  end

  def destroy
    account = Account.find(params[:id])
    current_project.remove_collaborator account
    json nil
  end

  private

  def current_project
    Project.accessible(current_account).find(params[:project_id])
  end

end

class CollaboratorsController < ApplicationController

  before_action :login_required

  def create
    account = Account.lookup(pick(params, :email)[:email])
    return not_found if !account
    return conflict if account.id == current_account.id
    return bad_request(:error => 'That account has been disabled.') if account.role == Account::DISABLED
    # TODO: If we're not using `account.errors`, can be reduced to 
    # `bad_request` [JR]
    return json(account, 400) unless current_project.add_collaborator account
    json account.to_json(:include_organization => true)
  end

  def destroy
    account = Account.find(params[:id])
    current_project.remove_collaborator account
    json nil
  end

  private

  def current_project
    Project.accessible(current_account).find(params[:project_id])
  end

end

Change a stray 409 to a 400
Change a stray 409 to a 400
