Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: bug column name when sequelize model mapping attribute has field… #283

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
- Naming - Rename opts to options in stat getters.
- Technical - Apply ESLint rules to old files.

### Fixed
- Charts - Fix group by on fields having an unconventional column name.
- Charts - Fix aggregation on fields having an unconventional column name.

## RELEASE 3.2.3 - 2019-06-21
### Fixed
- Filters - Fix potential chart / records retrieval errors (Regression introduced in v3.2.2).
Expand Down
8 changes: 6 additions & 2 deletions src/services/line-stat-getter.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@ function LineStatGetter(model, params, options) {
// cannot be '*'.
const fieldName = params.aggregate_field || schema.primaryKeys[0] ||
schema.fields[0].field;
return `${schema.name}.${fieldName}`;
const schemaField = schema.fields.find(schemaField => schemaField.field === fieldName);
const columnName = schemaField ? schemaField.columnName : fieldName;
return `${schema.name}.${columnName}`;
}

function getGroupByDateField() {
const fieldName = params.group_by_date_field;
return `${schema.name}.${fieldName}`;
const schemaField = schema.fields.find(schemaField => schemaField.field === fieldName);
const columnName = schemaField ? schemaField.columnName : fieldName;
return `${schema.name}.${columnName}`;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you could factorise this logic in a dedicated method.
It would improve the code maintenance and readability.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe there is already a method for this purpose in the existing code.

}

const groupByDateField = getGroupByDateField();
Expand Down
19 changes: 19 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ const HasManyDissociator = require('../src/services/has-many-dissociator');
username: { type: Sequelize.STRING },
password: { type: Sequelize.STRING },
createdAt: { type: Sequelize.DATE },
mappedCreatedAt: { field: 'createdAt', type: Sequelize.DATE },
updatedAt: { type: Sequelize.DATE },
resetPasswordToken: { type: Sequelize.STRING },
uuid: { type: Sequelize.UUID },
Expand Down Expand Up @@ -318,6 +319,24 @@ const HasManyDissociator = require('../src/services/has-many-dissociator');
});

describe('A simple Line Chart per day on an empty users table', () => {
it('should generate a valid SQL query when models mapped to another field', (done) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"when models mapped to another field" should actually be in a describe in my opinion:

describe('A simple Line Chart per day on an empty users table', () => {
  describe('with a group_by_date_field  field having a conventional name', () => {
    it('should generate a valid SQL query', (done) => {
      // TEST
    });
  });

  describe('with a group_by_date_field  field having an unconventional name', () => {
    it('should generate a valid SQL query', (done) => {
      // TEST
    });
  });
});

new LineStatGetter(models.user, {
type: 'Line',
collection: 'user',
timezone: 'Europe/Paris',
group_by_date_field: 'mappedCreatedAt',
aggregate: 'Count',
time_range: 'Day',
filters: [],
}, sequelizeOptions)
.perform()
.then((stat) => {
expect(stat.value.length).equal(1);
done();
})
.catch(done);
});

it('should generate a valid SQL query', (done) => {
new LineStatGetter(models.user, {
type: 'Line',
Expand Down