2014-10-23 09:50:23 +02:00
|
|
|
var eventStore = require('./eventStore'),
|
|
|
|
eventType = require('./eventType'),
|
2014-10-23 12:03:46 +02:00
|
|
|
logger = require('./logger'),
|
2014-10-23 13:38:02 +02:00
|
|
|
Promise = require('bluebird'),
|
|
|
|
dbPool = require('./dbPool');
|
2014-10-23 09:50:23 +02:00
|
|
|
|
|
|
|
eventStore.on(eventType.featureCreated, function (event) {
|
2014-10-23 13:38:02 +02:00
|
|
|
var sql = 'INSERT INTO features(name, enabled, strategy_name, parameters) VALUES ($1, $2, $3, $4)';
|
|
|
|
var params = [
|
|
|
|
event.data.name,
|
|
|
|
event.data.enabled ? 1 : 0,
|
|
|
|
event.data.strategy,
|
|
|
|
event.data.parameters
|
|
|
|
];
|
|
|
|
|
|
|
|
dbPool.query(sql, params, function(err) {
|
|
|
|
if(err) {
|
|
|
|
logger.error('Could not insert feature, error was: ', err);
|
|
|
|
}
|
|
|
|
});
|
2014-10-23 09:50:23 +02:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2014-10-23 16:00:51 +02:00
|
|
|
eventStore.on(eventType.featureUpdated, function (event) {
|
|
|
|
var sql, params;
|
|
|
|
var changeRequest = event.data;
|
|
|
|
|
|
|
|
switch (changeRequest.field) {
|
|
|
|
case 'enabled':
|
|
|
|
sql = 'UPDATE features SET enabled = $1 WHERE name=$2';
|
|
|
|
params = [event.data.value ? 1 : 0, event.data.name];
|
|
|
|
break;
|
|
|
|
case 'strategy':
|
|
|
|
sql = 'UPDATE features SET strategy_name = $1 WHERE name=$2';
|
|
|
|
params = [event.data.value, event.data.name];
|
|
|
|
break;
|
|
|
|
case 'parameters':
|
|
|
|
sql = 'UPDATE features SET parameters = $1 WHERE name=$2';
|
|
|
|
params = [event.data.value, event.data.name];
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(sql && params) {
|
|
|
|
dbPool.query(sql, params, function(err) {
|
|
|
|
if(err) {
|
|
|
|
logger.error('Could not update feature, error was: ', err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
2014-10-23 16:02:12 +02:00
|
|
|
logger.error('Could not handle feature-update event', event);
|
2014-10-23 16:00:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2014-10-23 09:50:23 +02:00
|
|
|
function getFeatures() {
|
2014-10-23 13:51:37 +02:00
|
|
|
var sql = 'SELECT name, enabled, strategy_name as strategy, parameters FROM features ORDER BY created_at';
|
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
dbPool.query(sql, function(err, res) {
|
|
|
|
if(err) {reject(err);}
|
|
|
|
resolve(res.rows.map(mapToToggle));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-10-23 15:17:04 +02:00
|
|
|
function getFeature(name) {
|
|
|
|
var sql = 'SELECT name, enabled, strategy_name as strategy, parameters FROM features WHERE name=$1';
|
|
|
|
|
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
dbPool.query(sql, [name], function(err, res) {
|
|
|
|
if(err) {reject(err);}
|
|
|
|
|
|
|
|
if(res.rows.length === 1) {
|
|
|
|
resolve(mapToToggle(res.rows[0]));
|
|
|
|
} else {
|
|
|
|
reject();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-10-23 13:51:37 +02:00
|
|
|
function mapToToggle(row) {
|
|
|
|
return {
|
|
|
|
name: row.name,
|
|
|
|
enabled: row.enabled > 0,
|
|
|
|
strategy: row.strategy,
|
|
|
|
parameters: row.parameters
|
|
|
|
};
|
2014-10-23 09:50:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
2014-10-23 15:17:04 +02:00
|
|
|
getFeatures: getFeatures,
|
|
|
|
getFeature: getFeature
|
2014-10-23 09:50:23 +02:00
|
|
|
};
|
|
|
|
|