var express = require('express');
var router = express.Router();
var pgb = require('../pg-binding');
var common = require('../common.js');
var settings = require

/**
 * @api {get} /custom_get_parcels_list Parcel/Num relationships List
 * @apiName custom_get_parcels_list
 * @apiGroup Custom
 *
 * @apiParam {String} tenure_type Options: own, lease, occupy, informal occupy
 * @apiParamExample  Query String Example:
 *  curl -i http://localhost/custom/get_parcels_list?tenure_type=own,lease
 * @apiSuccess {Object} response A feature collection with zero to many features
 * @apiSuccess {String} response.type "Feature Collection"
 * @apiSuccess {Object[]} response.features An array of feature objects
 * @apiSuccess {String} response.features.type "Feature"
 * @apiSuccess {Object} response.features.geometry GeoJSON geometry object
 * @apiSuccess {Object} response.features.properties GeoJSON feature's properties
 * @apiSuccess {Integer} response.features.properties.id parcel id
 * @apiSuccess {Timestamp} response.features.properties.time_created timestamp with timezone
 * @apiSuccess {Numeric} response.features.properties.area area of parcel geometry
 * @apiSuccess {String} response.features.properties.tenure_type type of relationship tenure
 * @apiSuccess {Integer} response.features.properties.num_relationships number of associated relationships
 * @apiExample {curl} Example usage:
 *     curl -i http://localhost/custom_get_parcels_list
 *
 * @apiSuccessExample {json} Success-Response:
 *     HTTP/1.1 200 OK
 *
 * {
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "geometry": null,
            "properties": {
                "id": 45,
                "time_created": "2015-08-24T14:03:27.144363-07:00",
                "area": null,
                "tenure_type": "own",
                "num_relationships": 6
            }
        },
        {
            "type": "Feature",
            "geometry": null,
            "properties": {
                "id": 44,
                "time_created": "2015-08-24T14:03:27.144363-07:00",
                "area": null,
                "tenure_type": "lease",
                "num_relationships": 5
            }
        },
        {
            "type": "Feature",
            "geometry": null,
            "properties": {
                "id": 43,
                "time_created": "2015-08-24T14:03:27.144363-07:00",
                "area": null,
                "tenure_type": "occupy",
                "num_relationships": 2
            }
        }
    ]
}
 *
 * */
router.get('/get_parcels_list', common.parseQueryOptions, function(req, res, next) {

    var args = common.getArguments(req);
    var options = {};
    var tenure_type;
    var obj = {};

    if (args.tenure_type) {
        obj = createWhereClause(args.tenure_type);
        options.whereClause = 'WHERE ' + obj.str + '';
    } else {
        obj.uriList = [];
    }

    req.queryModifiers.sort_by = "ORDER BY time_created DESC";
    var sql = common.featureCollectionSQL("show_parcels_list", req.queryModifiers, options.whereClause);

    // Handle bad requests; arg must tenure_type
    if (Object.keys(args).length > 0 && Object.keys(args).indexOf('tenure_type') == -1) {
        res.status(400).json({error: "Bad Request; invalid 'tenure_type' option"});
    } else {

        common.tableColumnQuery("show_parcel_list")
            .then(function(response){

                var sql = common.featureCollectionSQL("show_parcel_list", req.queryModifiers, options.whereClause);

                return pgb.queryDeferred(sql,{paramValues:obj.uriList});
            })
            .then(function(result){

                res.status(200).json(result[0].response);

            })
            .catch(function(err){
                next(err);
            })
            .done();
    }

});

function createWhereClause(arr) {
    var obj = {};

    obj.str = '';
    obj.uriList = arr.split(',');

    obj.uriList.forEach(function (val, i) {
        if (i < obj.uriList.length - 1) {
            obj.str += 'tenure_type::text[] @> ARRAY[$' + (i + 1) +'] OR ';
        } else {
            obj.str += 'tenure_type::text[] @> ARRAY[$' + (i + 1) + ']';
        }
    });

    return obj;
}

module.exports = router;

@@ -2981,389 +2981,135 @@
 y = 
-%22ORDER BY time_created DESC%22;%0A    var sql = common.featureCollectionSQL(%22show_parcels_list%22, req.queryModifiers, options.whereClause)
+req.queryModifiers.sort_by %7C%7C %22time_created,id%22
 ;%0A
-%0A
     
-// Handle bad requests; arg must tenure_type%0A    if (Object.keys(args).length %3E 0 && Object.keys(args).indexOf('tenure_type') == -1) %7B%0A        res.status(400).json(%7Berror: %22Bad Request; invalid 'tenure_type' option%22%7D);%0A    %7D else %7B%0A%0A   
+req.queryModifiers.sort_dir = req.queryModifiers.sort_dir %7C%7C %22DESC%22;%0A%0A
     
- 
 comm
@@ -3148,36 +3148,32 @@
 _list%22)%0A        
-    
 .then(function(r
@@ -3179,28 +3179,24 @@
 response)%7B%0A%0A
-    
             
@@ -3292,28 +3292,24 @@
 reClause);%0A%0A
-    
             
@@ -3369,35 +3369,27 @@
 %7D);%0A        
-    %7D)%0A    
+%7D)%0A
         .the
@@ -3405,28 +3405,24 @@
 n(result)%7B%0A%0A
-    
             
@@ -3468,35 +3468,27 @@
 );%0A%0A        
-    %7D)%0A    
+%7D)%0A
         .cat
@@ -3517,20 +3517,16 @@
         
-    
 next(err
@@ -3532,35 +3532,27 @@
 r);%0A        
- 
-   %7D)%0A    
+%7D)%0A
         .don
@@ -3556,21 +3556,16 @@
 done();%0A
-    %7D
 %0A%0A%7D);%0A%0Af

Remove option check from custom endpoint.