Skip to content

Commit

Permalink
[*] Pie Charts - Fix potential bad "dateonly" values for projects usi…
Browse files Browse the repository at this point in the history
…ng Sequelize 4+ (#189)
  • Loading branch information
arnaudbesnier committed Jun 17, 2018
1 parent 5e53453 commit 2bcb1de
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
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]
### Fixed
- Pie Charts - Fix potential bad "dateonly" values for projects using Sequelize 4+.

## RELEASE 2.12.0 - 2018-06-14
### Added
Expand Down
7 changes: 3 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
var _ = require('lodash');
var P = require('bluebird');
var Interface = require('forest-express');
var orm = require('./utils/orm');

var REGEX_VERSION = /(\d+\.)?(\d+\.)?(\*|\d+)/;

exports.collection = Interface.collection;
Expand Down Expand Up @@ -35,10 +37,7 @@ exports.init = function(opts) {
exports.getOrmVersion = function () {
if (!opts.sequelize) { return null; }

var ormVersion = opts.sequelize.version.match(REGEX_VERSION);
if (ormVersion && ormVersion[0]) {
return ormVersion[0];
}
return orm.getVersion(opts.sequelize);
};

exports.getDatabaseType = function () {
Expand Down
5 changes: 4 additions & 1 deletion services/pie-stat-getter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
var _ = require('lodash');
var P = require('bluebird');
var moment = require('moment');
var orm = require('../utils/orm');
var BaseStatGetter = require('./base-stat-getter');
var Database = require('../utils/database');
var Interface = require('forest-express');
Expand All @@ -14,6 +15,8 @@ var ALIAS_AGGREGATE = 'forest_alias_aggregate';
function PieStatGetter(model, params, opts) {
BaseStatGetter.call(this, model, params, opts);

var needsDateOnlyFormating = orm.isVersionLessThan4(opts.sequelize);

var schema = Interface.Schemas.schemas[model.name];
var associationSplit,associationCollection, associationField,
associationSchema, field;
Expand Down Expand Up @@ -80,7 +83,7 @@ function PieStatGetter(model, params, opts) {

if (field.type === 'Date') {
key = moment(record[ALIAS_GROUP_BY]).format('DD/MM/YYYY HH:mm:ss');
} else if (field.type === 'Dateonly') {
} else if (field.type === 'Dateonly' && needsDateOnlyFormating) {
var offsetServer = moment().utcOffset() / 60;
var dateonly = moment.utc(record[ALIAS_GROUP_BY])
.add(offsetServer, 'h');
Expand Down
23 changes: 23 additions & 0 deletions utils/orm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';
var semver = require('semver');

var REGEX_VERSION = /(\d+\.)?(\d+\.)?(\*|\d+)/;

var getVersion = function (sequelize) {
var version = sequelize.version.match(REGEX_VERSION);
if (version && version[0]) {
return version[0];
}
return null;
};

var isVersionLessThan4 = function (sequelize) {
try {
return semver.lt(getVersion(sequelize), '4.0.0');
} catch (error) {
return true;
}
};

exports.getVersion = getVersion;
exports.isVersionLessThan4 = isVersionLessThan4;

0 comments on commit 2bcb1de

Please sign in to comment.