Skip to content

Commit

Permalink
[-] Linter - ES-Lint compliance on services/resources-getter.js (#267)
Browse files Browse the repository at this point in the history
  • Loading branch information
larcin authored and arnaudbesnier committed Jun 13, 2019
1 parent cae1fbc commit d0121de
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 94 deletions.
3 changes: 2 additions & 1 deletion .eslint-bin/js-list.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
"src/services/apimap-field-builder.js",
"src/services/apimap-field-type-detector.js",
"src/services/resource-finder.js",
"src/services/resources-getter.js",
"src/services/search-builder.js",
"src/utils/database.js",
"src/utils/operators.js",
"src/utils/orm.js",
"src/utils/records-decorator.js",
"test/index.js",
"test/services/apimap-field-builder.js"
]
]
192 changes: 99 additions & 93 deletions src/services/resources-getter.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
'use strict';
var _ = require('lodash');
var P = require('bluebird');
var Operators = require('../utils/operators');
var OperatorValueParser = require('./operator-value-parser');
var Interface = require('forest-express');
var CompositeKeysManager = require('./composite-keys-manager');
var QueryBuilder = require('./query-builder');
var SearchBuilder = require('./search-builder');
var LiveQueryChecker = require('./live-query-checker');
var ErrorHTTP422 = require('./errors').ErrorHTTP422;
const _ = require('lodash');
const P = require('bluebird');
const Operators = require('../utils/operators');
const OperatorValueParser = require('./operator-value-parser');
const Interface = require('forest-express');
const CompositeKeysManager = require('./composite-keys-manager');
const QueryBuilder = require('./query-builder');
const SearchBuilder = require('./search-builder');
const LiveQueryChecker = require('./live-query-checker');
const { ErrorHTTP422 } = require('./errors');

function ResourcesGetter(model, opts, params) {
var schema = Interface.Schemas.schemas[model.name];
var queryBuilder = new QueryBuilder(model, opts, params);
var segmentScope;
var segmentWhere;
var OPERATORS = new Operators(opts);
var primaryKey = _.keys(model.primaryKeys)[0];

var fieldNamesRequested = (function() {
const schema = Interface.Schemas.schemas[model.name];
const queryBuilder = new QueryBuilder(model, opts, params);
let segmentScope;
let segmentWhere;
const OPERATORS = new Operators(opts);
const primaryKey = _.keys(model.primaryKeys)[0];

function getFieldNamesRequested() {
if (!params.fields || !params.fields[model.name]) { return null; }

// NOTICE: Populate the necessary associations for filters
var associationsForQuery = [];
_.each(params.filter, function (values, key) {
const associationsForQuery = [];
_.each(params.filter, (values, key) => {
if (key.indexOf(':') !== -1) {
var association = key.split(':')[0];
const association = key.split(':')[0];
associationsForQuery.push(association);
}
});
Expand All @@ -34,26 +33,34 @@ function ResourcesGetter(model, opts, params) {
associationsForQuery.push(params.sort.split('.')[0]);
}

// NOTICE: Force the primaryKey retrieval to store the records properly in
// the client.
return _.union([primaryKey], params.fields[model.name].split(','),
associationsForQuery);
})();
// NOTICE: Force the primaryKey retrieval to store the records properly in the client.
return _.union(
[primaryKey],
params.fields[model.name].split(','),
associationsForQuery,
);
}

const fieldNamesRequested = getFieldNamesRequested();

var searchBuilder = new SearchBuilder(model, opts, params,
fieldNamesRequested);
var hasSmartFieldSearch = false;
const searchBuilder = new SearchBuilder(
model,
opts,
params,
fieldNamesRequested,
);
let hasSmartFieldSearch = false;

function handleFilterParams() {
var where = {};
var conditions = [];
const where = {};
const conditions = [];

_.each(params.filter, function (values, key) {
_.each(params.filter, (values, key) => {
if (key.indexOf(':') !== -1) {
key = '$' + key.replace(':', '.') + '$';
key = `$${key.replace(':', '.')}$`;
}
values.split(',').forEach(function (value) {
var condition = {};
values.split(',').forEach((value) => {
const condition = {};
condition[key] = new OperatorValueParser(opts)
.perform(model, key, value, params.timezone);
conditions.push(condition);
Expand All @@ -68,8 +75,8 @@ function ResourcesGetter(model, opts, params) {
}

function getWhere() {
return new P(function (resolve, reject) {
var where = {};
return new P((resolve, reject) => {
const where = {};
where[OPERATORS.AND] = [];

if (params.search) {
Expand All @@ -85,85 +92,86 @@ function ResourcesGetter(model, opts, params) {
}

if (params.segmentQuery) {
var queryToFilterRecords = params.segmentQuery.trim();
const queryToFilterRecords = params.segmentQuery.trim();
new LiveQueryChecker().perform(queryToFilterRecords);

// WARNING: Choosing the first connection might generate issues if the model
// does not belongs to this database.
opts.connections[0]
// WARNING: Choosing the first connection might generate issues if the model does not
// belongs to this database.
return opts.connections[0]
.query(queryToFilterRecords, {
type: opts.sequelize.QueryTypes.SELECT,
})
.then(function (results) {
var recordIds = results.map(function (result) {
return result[primaryKey] || result.id;
});
var condition = { [primaryKey]: {} };
.then((results) => {
const recordIds = results.map(result => result[primaryKey] || result.id);
const condition = { [primaryKey]: {} };
condition[primaryKey][OPERATORS.IN] = recordIds;
where[OPERATORS.AND].push(condition);

return resolve(where);
}, function (error) {
var errorMessage = 'Invalid SQL query for this Live Query segment:\n' + error.message;
}, (error) => {
const errorMessage = `Invalid SQL query for this Live Query segment:\n${error.message}`;
Interface.logger.error(errorMessage);
reject(new ErrorHTTP422(errorMessage));
});
} else {
return resolve(where);
}
return resolve(where);
});
}


function getRecords() {
var scope = segmentScope ? model.scope(segmentScope) : model.unscoped();
var include = queryBuilder.getIncludes(model, fieldNamesRequested);
const scope = segmentScope ? model.scope(segmentScope) : model.unscoped();
const include = queryBuilder.getIncludes(model, fieldNamesRequested);

return getWhere()
.then(function (where) {
var findAllOpts = {
where: where,
include: include,
.then((where) => {
const findAllOpts = {
where,
include,
order: queryBuilder.getOrder(),
offset: queryBuilder.getSkip(),
limit: queryBuilder.getLimit()
limit: queryBuilder.getLimit(),
};

if (params.search) {
_.each(schema.fields, function (field) {
_.each(schema.fields, (field) => {
if (field.search) {
try {
field.search(findAllOpts, params.search);
hasSmartFieldSearch = true;
} catch (error) {
Interface.logger.error('Cannot search properly on Smart Field ' +
field.field, error);
Interface.logger.error(
`Cannot search properly on Smart Field ${field.field}`,
error,
);
}
}
});

var fieldsSearched = searchBuilder.getFieldsSearched();
const fieldsSearched = searchBuilder.getFieldsSearched();
if (fieldsSearched.length === 0 && !hasSmartFieldSearch) {
if (!params.searchExtended ||
!searchBuilder.hasExtendedSearchConditions()) {
// NOTICE: No search condition has been set for the current search, no record can be found.
// NOTICE: No search condition has been set for the current search, no record can be
// found.
return [];
}
}
}

return scope.findAll(findAllOpts);
});
});
}

function countRecords() {
var scope = segmentScope ? model.scope(segmentScope) : model.unscoped();
var include = queryBuilder.getIncludes(model, fieldNamesRequested);
const scope = segmentScope ? model.scope(segmentScope) : model.unscoped();
const include = queryBuilder.getIncludes(model, fieldNamesRequested);

return getWhere()
.then(function (where) {
var options = {
include: include,
where: where
.then((where) => {
const options = {
include,
where,
};

if (!primaryKey) {
Expand All @@ -172,37 +180,41 @@ function ResourcesGetter(model, opts, params) {
}

if (params.search) {
_.each(schema.fields, function (field) {
_.each(schema.fields, (field) => {
if (field.search) {
try {
field.search(options, params.search);
hasSmartFieldSearch = true;
} catch (error) {
Interface.logger.error('Cannot search properly on Smart Field ' +
field.field, error);
Interface.logger.error(
`Cannot search properly on Smart Field ${field.field}`,
error,
);
}
}
});

var fieldsSearched = searchBuilder.getFieldsSearched();
const fieldsSearched = searchBuilder.getFieldsSearched();
if (fieldsSearched.length === 0 && !hasSmartFieldSearch) {
if (!params.searchExtended ||
!searchBuilder.hasExtendedSearchConditions()) {
// NOTICE: No search condition has been set for the current search, no record can be found.
// NOTICE: No search condition has been set for the current search, no record can be
// found.
return 0;
}
}
}

return scope.count(options);
});
});
}

function getSegment() {
if (schema.segments && params.segment) {
var segment = _.find(schema.segments, function (segment) {
return segment.name === params.segment;
});
const segment = _.find(
schema.segments,
schemaSegment => schemaSegment.name === params.segment,
);

segmentScope = segment.scope;
segmentWhere = segment.where;
Expand All @@ -213,27 +225,25 @@ function ResourcesGetter(model, opts, params) {
getSegment();
if (_.isFunction(segmentWhere)) {
return segmentWhere(params)
.then(function (where) {
.then((where) => {
segmentWhere = where;
return;
});
} else {
return new P(function (resolve) { return resolve(); });
}
return P.resolve();
}

this.perform = function () {
return getSegmentCondition()
this.perform = () =>
getSegmentCondition()
.then(getRecords)
.then(function (records) {
var fieldsSearched = null;
.then((records) => {
let fieldsSearched = null;

if (params.search) {
fieldsSearched = searchBuilder.getFieldsSearched();
}

if (schema.isCompositePrimary) {
records.forEach(function (record) {
records.forEach((record) => {
record.forestCompositePrimary =
new CompositeKeysManager(model, schema, record)
.createCompositePrimary();
Expand All @@ -242,12 +252,8 @@ function ResourcesGetter(model, opts, params) {

return [records, fieldsSearched];
});
};

this.count = function () {
return getSegmentCondition()
.then(countRecords);
};
this.count = () => getSegmentCondition().then(countRecords);
}

module.exports = ResourcesGetter;

0 comments on commit d0121de

Please sign in to comment.