var maps = require('@google/maps')
var config = require('dotenv').config();
var googleMapsClient = maps.createClient({
  key: process.env.GOOGLE_API_KEY
});


var tripPoints = [];

/**
 * Generate a trip with params
 *
 * @param {Object} params {bot: {}, origin: {}}
 * @param {Function} callback
 *
 * @return {Array} point {"lat": 50.60459528287801,"lng": 2.9086303710937496 }
 */
var generate_trip = function(params, callback) {
    var latLngs = params.bot.zone.properties.latLngs;
    var bbox = params.bot.zone.properties.bbox;
    if (!params.origin) {
      var start_points = geometry.randomPointInPolygon(latLngs, bbox);
    }
    else {
      var start_points = params.origin
    }
    var end_points = geometry.randomPointInPolygon(latLngs, bbox);
    
    var coords = { origin: start_points, destination: end_points };
    trip.generate_directions(coords, function(directions){
      trip.generate_road(directions, callback);  
    });
  };

var generate_directions = function(coords, callback) {
    console.log("generate_directions");
    googleMapsClient.directions(coords, function(err, response) {
      if (err) {
        console.log(err)
        return false; // Should also return err
      } else {
        var steps = response.json.routes[0].legs[0].steps;
        tripPoints = [];
        steps.forEach(function(step, index) {
          var lat, lng;
          if (index == steps.length) {
            lat = step.end_location.lat;
            lng = step.end_location.lng;
          } else {
            lat = step.start_location.lat;
            lng = step.start_location.lng;
          }
          var stepLatLng = { lat: lat, lng: lng };
          tripPoints.push(stepLatLng);
        });
        console.log(tripPoints);
        callback(tripPoints);
        return true;
      }
    });
  };

var generate_road = function(directions, callback) {
    console.log("generate_road");
    googleMapsClient.snapToRoads({path: directions, interpolate: true}, function(err, response) {
      if (err) {
        console.log(err)
        return false; // Should also return err
      } else {
        tripPoints = [];
        var steps = response.json.snappedPoints;
        console.log(steps);
        steps.forEach(function(step, index) {
          console.log(step);
          var lat, lng;
          lat = step.location.latitude;
          lng = step.location.longitude;
          var stepLatLng = { lat: lat, lng: lng, placeId: step.placeId};
          tripPoints.push(stepLatLng);
        });
        callback(tripPoints);
        return true;
      }
    });
  }

trip = {

  tripPoints: tripPoints,

  /**
   * Generate a trip with params
   *
   * @param {Object} params {bot: {}, origin: {"lat": 50.6045,"lng": 2.9086 }}
   * @param {Function} callback
   *
   * @return {Array} points [{"lat": 50.6045,"lng": 2.9086 }, {"lat": 50.6045,"lng": 2.9086 }, {"lat": 50.6045,"lng": 2.9086 }]
   */
  generate_trip: generate_trip,

  generate_directions: generate_directions,

  generate_road: generate_road
};

module.exports = trip;

@@ -405,22 +405,27 @@
 unction(
-params
+bot, origin
 , callba
@@ -448,23 +448,16 @@
 tLngs = 
-params.
 bot.zone
@@ -492,23 +492,16 @@
  bbox = 
-params.
 bot.zone
@@ -530,22 +530,28 @@
 if (
-!params.origin
+origin === undefined
 ) %7B%0A
@@ -626,20 +626,16 @@
 );%0A    %7D
-%0A   
  else %7B%0A
@@ -663,15 +663,8 @@
 s = 
-params.
 orig
@@ -1699,41 +1699,8 @@
 %7D);%0A
-        console.log(tripPoints);%0A
     
@@ -2136,36 +2136,8 @@
 ts;%0A
-        console.log(steps);%0A
     
@@ -2182,37 +2182,8 @@
 ) %7B%0A
-          console.log(step);%0A
     

Fix optional params test