"use strict";

var restify = require('restify');
var async = require('async');
var Anyfetch = require('anyfetch');


module.exports.get = function get(req, res, next) {
  if(!req.query.query) {
    return next(new restify.ConflictError());
  }
  async.waterfall([
    function getDocuments(cb) {
      var anyfetchClient = new Anyfetch(req.accessToken.token);
      anyfetchClient.getDocuments({ search: req.query.query, render_templates: true }, cb);
    },
    function remapDocuments(documentsRes, cb) {
      res.send(200, documentsRes.body.data.map(function mapDocuments(document) {
        return {
          type: document.document_type.name,
          id: document.id,
          date: document.modification_date,
          snippet: document.rendered_snippet,
          title: document.rendered_title
        };
      }));
      cb();
    }
  ], next);
};

"use strict";

var restify = require('restify');
var async = require('async');
var Anyfetch = require('anyfetch');


module.exports.get = function get(req, res, next) {
  if(!req.query.query) {
    return next(new restify.ConflictError("Missing query parameter"));
  }
  async.waterfall([
    function getDocuments(cb) {
      var anyfetchClient = new Anyfetch(req.accessToken.token);
      anyfetchClient.getDocuments({ search: req.query.query, render_templates: true }, cb);
    },
    function remapDocuments(documentsRes, cb) {
      res.send(200, documentsRes.body.data.map(function mapDocuments(document) {
        return {
          type: document.document_type.name,
          id: document.id,
          date: document.modification_date,
          snippet: document.rendered_snippet,
          title: document.rendered_title
        };
      }));
      cb();
    }
  ], next);
};

Add a message when refusing due to a lack of query parameter
Add a message when refusing due to a lack of query parameter
