Skip to content

Commit

Permalink
[-] Linter - ES-Lint compliance on stat-getters (ForestAdmin#275)
Browse files Browse the repository at this point in the history
* [-] Linter - ES-Lint compliance on stat-getters

* Add a change description

* Reapply the lodash, moment, bluebird global imports
  • Loading branch information
larcin authored Jun 25, 2019
1 parent a7b97e8 commit 57e8755
Show file tree
Hide file tree
Showing 6 changed files with 263 additions and 237 deletions.
4 changes: 4 additions & 0 deletions .eslint-bin/js-list.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@
"src/index.js",
"src/services/apimap-field-builder.js",
"src/services/apimap-field-type-detector.js",
"src/services/leaderboard-stat-getter.js",
"src/services/line-stat-getter.js",
"src/services/operator-value-parser.js",
"src/services/pie-stat-getter.js",
"src/services/resource-finder.js",
"src/services/resources-getter.js",
"src/services/search-builder.js",
"src/services/value-stat-getter.js",
"src/utils/database.js",
"src/utils/operators.js",
"src/utils/orm.js",
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Change Log

## [Unreleased]
### Changed
- Technical - Apply ESLint rules to old files.

## RELEASE 3.2.3 - 2019-06-21
### Fixed
Expand Down
81 changes: 39 additions & 42 deletions src/services/leaderboard-stat-getter.js
Original file line number Diff line number Diff line change
@@ -1,62 +1,59 @@
'use strict';
var _ = require('lodash');
var Interface = require('forest-express');
var BaseStatGetter = require('./base-stat-getter');
import _ from 'lodash';
import { Schemas } from 'forest-express';
import BaseStatGetter from './base-stat-getter';

function LeaderboardStatGetter(model, modelRelationship, params, options) {
BaseStatGetter.call(this, model, params, options);

var labelField = params.label_field;
var aggregate = params.aggregate.toUpperCase();
var aggregateField = params.aggregate_field;
var limit = params.limit;
var schema = Interface.Schemas.schemas[model.name];
var schemaRelationship = Interface.Schemas.schemas[modelRelationship.name];
var associationAs = schema.name;
const labelField = params.label_field;
const aggregate = params.aggregate.toUpperCase();
const aggregateField = params.aggregate_field;
const { limit } = params;
const schema = Schemas.schemas[model.name];
const schemaRelationship = Schemas.schemas[modelRelationship.name];
let associationAs = schema.name;

_.each(modelRelationship.associations, function (association) {
_.each(modelRelationship.associations, (association) => {
if (association.target.name === model.name && association.as) {
associationAs = association.as;
}
});

var groupBy = associationAs + '.' + labelField;
const groupBy = `${associationAs}.${labelField}`;

function getAggregateField() {
// NOTICE: As MySQL cannot support COUNT(table_name.*) syntax, fieldName cannot be '*'.
var fieldName = aggregateField || schemaRelationship.primaryKeys[0] ||
const fieldName = aggregateField || schemaRelationship.primaryKeys[0] ||
schemaRelationship.fields[0].field;
return schemaRelationship.name + '.' + fieldName;
return `${schemaRelationship.name}.${fieldName}`;
}

this.perform = function () {
return modelRelationship
.unscoped()
.findAll({
attributes: [
[options.sequelize.fn(aggregate, options.sequelize.col(getAggregateField())), 'value']
],
include: [{
model: model,
attributes: [labelField],
as: associationAs,
required: true,
}],
group: groupBy,
order: [[options.sequelize.literal('value'), 'DESC']],
limit: limit,
raw: true
})
.then(function (records) {
records = records.map(function (data) {
data.key = data[groupBy];
delete data[groupBy];
return data;
});

return { value: records };
this.perform = () => modelRelationship
.unscoped()
.findAll({
attributes: [
[options.sequelize.fn(aggregate, options.sequelize.col(getAggregateField())), 'value'],
],
include: [{
model,
attributes: [labelField],
as: associationAs,
required: true,
}],
group: groupBy,
order: [[options.sequelize.literal('value'), 'DESC']],
limit,
raw: true,
})
.then((records) => {
records = records.map((data) => {
data.key = data[groupBy];
delete data[groupBy];
return data;
});
};

return { value: records };
});
}

module.exports = LeaderboardStatGetter;
Loading

0 comments on commit 57e8755

Please sign in to comment.