# all files / keystone/lib/list/ getSearchFilters.js

4.39% Statements 5/114
0% Branches 0/110
0% Functions 0/4
4.39% Lines 5/114

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    2351×
    1×
    1×

    1×

    1×
     var _ = require('lodash');
    var moment = require('moment');
    var utils = require('keystone-utils');

    /**
     * Gets filters for a Mongoose query that will search for the provided string,
     * based on the searchFields List option.
     *
     * Also accepts a filters object from `processFilters()`, any of which may
     * override the search string.
     *
     * NOTE: This function is deprecated in favor of List.prototype.addSearchToQuery
     * and will be removed in a later version.
     *
     * Example:
     *     list.getSearchFilters('jed') // returns { name: /jed/i }
     *
     * @param {String} query
     * @param {Object} additional filters
     */
    function getSearchFilters (search, add) {
    	var filters = {};
    	var list = this;

    	search = String(search || '').trim();

    	if (search.length) {
    		var searchFilter;
    		var searchParts = search.split(' ');
    		var searchRx = new RegExp(utils.escapeRegExp(search), 'i');
    		var splitSearchRx = new RegExp((searchParts.length > 1) ? _.map(searchParts, utils.escapeRegExp).join('|') : search, 'i');
    		var searchFields = this.get('searchFields');
    		var searchFilters = [];
    		var searchIdField = utils.isValidObjectId(search);

    		if (typeof searchFields === 'string') {
    			searchFields = searchFields.split(',');
    		}

    		searchFields.forEach(function (path) {
    			path = path.trim();

    			if (path === '__name__') {
    				path = list.mappings.name;
    			}

    			var field = list.fields[path];

    			if (field && field.type === 'name') {
    				var first = {};
    				first[field.paths.first] = splitSearchRx;
    				var last = {};
    				last[field.paths.last] = splitSearchRx;
    				searchFilter = {};
    				searchFilter.$or = [first, last];
    				searchFilters.push(searchFilter);
    			} else {
    				searchFilter = {};
    				searchFilter[path] = searchRx;
    				searchFilters.push(searchFilter);
    			}
    		});

    		if (list.autokey) {
    			searchFilter = {};
    			searchFilter[list.autokey.path] = searchRx;
    			searchFilters.push(searchFilter);
    		}

    		if (searchIdField) {
    			searchFilter = {};
    			searchFilter._id = search;
    			searchFilters.push(searchFilter);
    		}

    		if (searchFilters.length > 1) {
    			filters.$or = searchFilters;
    		} else if (searchFilters.length) {
    			filters = searchFilters[0];
    		}

    	}

    	if (add) {
    		_.forEach(add, function (filter) {
    			var cond;
    			var path = filter.key;
    			var value = filter.value;

    			switch (filter.field.type) {
    				case 'boolean':
    					if (!value || value === 'false') {
    						filters[path] = { $ne: true };
    					} else {
    						filters[path] = true;
    					}
    					break;

    				case 'localfile':
    				case 'cloudinaryimage':
    				case 'cloudinaryimages':
    				case 's3file':
    				case 'name':
    				case 'password':
    					// TODO
    					break;

    				case 'location':
    					_.forEach(['street1', 'suburb', 'state', 'postcode', 'country'], function (pathKey, i) {
    						var value = filter.value[i];
    						if (value) {
    							filters[filter.field.paths[pathKey]] = new RegExp(utils.escapeRegExp(value), 'i');
    						}
    					});
    					break;

    				case 'relationship':
    					if (value) {
    						if (filter.field.many) {
    							filters[path] = (filter.inverse) ? { $nin: [value] } : { $in: [value] };
    						} else {
    							filters[path] = (filter.inverse) ? { $ne: value } : value;
    						}
    					} else {
    						if (filter.field.many) {
    							filters[path] = (filter.inverse) ? { $not: { $size: 0 } } : { $size: 0 };
    						} else {
    							filters[path] = (filter.inverse) ? { $ne: null } : null;
    						}
    					}
    					break;

    				case 'select':
    					if (filter.value) {
    						filters[path] = (filter.inverse) ? { $ne: value } : value;
    					} else {
    						filters[path] = (filter.inverse) ? { $nin: ['', null] } : { $in: ['', null] };
    					}
    					break;

    				case 'number':
    				case 'money':
    					if (filter.operator === 'bt') {
    						value = [
    							utils.number(value[0]),
    							utils.number(value[1]),
    						];
    						if (!isNaN(value[0]) && !isNaN(value[1])) {
    							filters[path] = {
    								$gte: value[0],
    								$lte: value[1],
    							};
    						}
    						else {
    							filters[path] = null;
    						}
    					} else {
    						value = utils.number(value);
    						if (!isNaN(value)) {
    							if (filter.operator === 'gt') {
    								filters[path] = { $gt: value };
    							}
    							else if (filter.operator === 'lt') {
    								filters[path] = { $lt: value };
    							}
    							else {
    								filters[path] = value;
    							}
    						}
    						else {
    							filters[path] = null;
    						}
    					}
    					break;

    				case 'date':
    				case 'datetime':
    					if (filter.operator === 'bt') {
    						value = [
    							moment(value[0]),
    							moment(value[1]),
    						];
    						if ((value[0] && value[0].isValid()) && (value[1] && value[0].isValid())) {
    							filters[path] = {
    								$gte: moment(value[0]).startOf('day').toDate(),
    								$lte: moment(value[1]).endOf('day').toDate(),
    							};
    						}
    					} else {
    						value = moment(value);
    						if (value && value.isValid()) {
    							var start = moment(value).startOf('day').toDate();
    							var end = moment(value).endOf('day').toDate();
    							if (filter.operator === 'gt') {
    								filters[path] = { $gt: end };
    							} else if (filter.operator === 'lt') {
    								filters[path] = { $lt: start };
    							} else {
    								filters[path] = { $lte: end, $gte: start };
    							}
    						}
    					}
    					break;

    				case 'text':
    				case 'textarea':
    				case 'html':
    				case 'email':
    				case 'url':
    				case 'key':
    					if (filter.exact) {
    						if (value) {
    							cond = new RegExp('^' + utils.escapeRegExp(value) + '$', 'i');
    							filters[path] = filter.inverse ? { $not: cond } : cond;
    						} else {
    							if (filter.inverse) {
    								filters[path] = { $nin: ['', null] };
    							} else {
    								filters[path] = { $in: ['', null] };
    							}
    						}
    					} else if (value) {
    						cond = new RegExp(utils.escapeRegExp(value), 'i');
    						filters[path] = filter.inverse ? { $not: cond } : cond;
    					}
    					break;

    			}
    		});
    	}
    	return filters;
    }

    module.exports = getSearchFilters;