const { get } = require('lodash')
const config = require('../../../config')
const { authorisedRequest } = require('../../lib/authorised-request')

function getAdvisers (token) {
  return authorisedRequest(token, `${config.apiRoot}/adviser/?limit=100000&offset=0`)
    .then(response => {
      const results = response.results.filter(adviser => get(adviser, 'name', '').trim().length)

      return {
        results,
        count: results.length,
      }
    })
}

function getAdviser (token, id) {
  return authorisedRequest(token, `${config.apiRoot}/adviser/${id}/`)
}

async function fetchAdviserSearchResults (token, params) {
  const url = `${config.apiRoot}/adviser/?autocomplete=${params.term}&is_active=${params.is_active}`
  const adviserResults = await authorisedRequest(token, { url })
  return adviserResults.results
}

module.exports = {
  getAdvisers,
  getAdviser,
  fetchAdviserSearchResults,
}

const { get } = require('lodash')
const config = require('../../../config')
const { authorisedRequest } = require('../../lib/authorised-request')

function getAdvisers (token) {
  return authorisedRequest(token, `${config.apiRoot}/adviser/?limit=100000&offset=0`)
    .then(response => {
      const results = response.results.filter(adviser => get(adviser, 'name', '').trim().length)

      return {
        results,
        count: results.length,
      }
    })
}

function getAdviser (token, id) {
  return authorisedRequest(token, `${config.apiRoot}/adviser/${id}/`)
}

async function fetchAdviserSearchResults (token, params) {
  const url = `${config.apiRoot}/adviser/?autocomplete=${params.term}`
  const adviserResults = await authorisedRequest(token, { url })
  return adviserResults.results
}

module.exports = {
  getAdvisers,
  getAdviser,
  fetchAdviserSearchResults,
}

Remove the param to filter inactive advisers
Remove the param to filter inactive advisers
