Skip to content

Commit

Permalink
Fix #217 : StateTypeParam are no longer inserted in duplicate
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre-Gilles committed Sep 10, 2017
1 parent 209c80c commit 507c783
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 4 deletions.
1 change: 1 addition & 0 deletions api/core/statetypeparam/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

module.exports.create = require('./stateTypeParam.create.js');
module.exports.cleanDuplicate = require('./stateTypeParam.cleanDuplicate.js');
module.exports.getByStateType = require('./stateTypeParam.getByStateType.js');
5 changes: 5 additions & 0 deletions api/core/statetypeparam/stateTypeParam.cleanDuplicate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var queries = require('./stateTypeParam.queries.js');

module.exports = function(){
return gladys.utils.sql(queries.cleanDuplicateStateTypeParams, []);
};
13 changes: 12 additions & 1 deletion api/core/statetypeparam/stateTypeParam.create.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
var queries = require('./stateTypeParam.queries.js');

module.exports = function (stateTypeParam){
return StateTypeParam.create(stateTypeParam);

// test if StateTypeParam already exist
return gladys.utils.sql(queries.getByStateTypeAndVariableName, [stateTypeParam.statetype, stateTypeParam.variablename])
.then((rows) => {

// if StateTypeParam already exist, update it
if(rows.length) return StateTypeParam.update({id: rows[0].id}, stateTypeParam);

// if not, update it
return StateTypeParam.create(stateTypeParam);
})
};
12 changes: 11 additions & 1 deletion api/core/statetypeparam/stateTypeParam.queries.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@

module.exports = {
getByStateType: 'SELECT * FROM statetypeparam WHERE statetype = ?;'
getByStateType: 'SELECT * FROM statetypeparam WHERE statetype = ?;',
getByStateTypeAndVariableName: 'SELECT * FROM statetypeparam WHERE statetype = ? AND variablename = ?;',
cleanDuplicateStateTypeParams: `
DELETE FROM statetypeparam
WHERE id NOT IN
(
SELECT MIN(id) as rowId
FROM (SELECT * FROM statetypeparam) AS stp
GROUP BY variablename, statetype
);
`
};
6 changes: 5 additions & 1 deletion api/core/update/update.getStates.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ module.exports = function(user) {

if(stateTypes === 'Not Found') return Promise.reject(new Error('Not Found'));

return gladys.stateType.insertBatch(stateTypes);
// Insert all stateTypes
return gladys.stateType.insertBatch(stateTypes)

// then clean duplicate (due to old versions of Gladys :/)
.then(() => gladys.stateTypeParam.cleanDuplicate());
});
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
var should = require('should');
var validateStateTypeParam = require('../../validator/stateTypeParamValidator.js');

describe('StateTypeParam', function() {

describe('cleanDuplicate', function() {

it('should cleanDuplicate', function (done) {


gladys.stateTypeParam.cleanDuplicate()
.then(function(){

done();
}).catch(done);

});

});

});
19 changes: 18 additions & 1 deletion test/unit/api/core/statetypeparam/stateTypeParam.create.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ describe('StateTypeParam', function() {

describe('create', function() {

it('should return StateTypeParam created', function (done) {
it('should return StateTypeParam updated', function (done) {

var stateTypeParam = {
statetype: 1,
Expand All @@ -21,6 +21,23 @@ describe('StateTypeParam', function() {
}).catch(done);

});

it('should return StateTypeParam created', function (done) {

var stateTypeParam = {
statetype: 1,
variablename: 'test2',
name: 'Test'
};

gladys.stateTypeParam.create(stateTypeParam)
.then(function(result){

validateStateTypeParam(result);
done();
}).catch(done);

});

});

Expand Down

0 comments on commit 507c783

Please sign in to comment.