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
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
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));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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 = {
|
|
|
|
getFeatures: getFeatures
|
|
|
|
};
|
|
|
|
|