2016-06-18 21:53:18 +02:00
|
|
|
'use strict';
|
2016-10-26 10:43:11 +02:00
|
|
|
|
2016-10-27 20:51:21 +02:00
|
|
|
const NotFoundError = require('../error/notfound-error');
|
2020-04-14 22:29:11 +02:00
|
|
|
|
2021-01-21 13:47:08 +01:00
|
|
|
const STRATEGY_COLUMNS = [
|
|
|
|
'name',
|
|
|
|
'description',
|
|
|
|
'parameters',
|
|
|
|
'built_in',
|
|
|
|
'deprecated',
|
|
|
|
];
|
2016-11-05 10:16:48 +01:00
|
|
|
const TABLE = 'strategies';
|
2014-11-17 22:39:04 +01:00
|
|
|
|
2016-11-05 10:16:48 +01:00
|
|
|
class StrategyStore {
|
2021-01-18 12:32:19 +01:00
|
|
|
constructor(db, getLogger) {
|
2016-11-05 10:16:48 +01:00
|
|
|
this.db = db;
|
2019-04-30 21:14:23 +02:00
|
|
|
this.logger = getLogger('strategy-store.js');
|
2016-11-05 10:16:48 +01:00
|
|
|
}
|
2014-12-08 20:56:22 +01:00
|
|
|
|
2020-09-18 09:05:09 +02:00
|
|
|
async getStrategies() {
|
|
|
|
const rows = await this.db
|
2016-05-01 22:53:09 +02:00
|
|
|
.select(STRATEGY_COLUMNS)
|
2016-11-05 10:16:48 +01:00
|
|
|
.from(TABLE)
|
2020-09-18 09:05:09 +02:00
|
|
|
.orderBy('name', 'asc');
|
|
|
|
|
|
|
|
return rows.map(this.rowToStrategy);
|
2016-05-01 22:53:09 +02:00
|
|
|
}
|
|
|
|
|
2020-09-18 09:05:09 +02:00
|
|
|
async getEditableStrategies() {
|
|
|
|
const rows = await this.db
|
2019-03-14 17:56:02 +01:00
|
|
|
.select(STRATEGY_COLUMNS)
|
|
|
|
.from(TABLE)
|
|
|
|
.where({ built_in: 0 }) // eslint-disable-line
|
2020-09-18 09:05:09 +02:00
|
|
|
.orderBy('name', 'asc');
|
|
|
|
return rows.map(this.rowToEditableStrategy);
|
2019-03-14 17:56:02 +01:00
|
|
|
}
|
|
|
|
|
2020-09-18 09:05:09 +02:00
|
|
|
async getStrategy(name) {
|
2016-11-05 10:16:48 +01:00
|
|
|
return this.db
|
2016-05-01 22:53:09 +02:00
|
|
|
.first(STRATEGY_COLUMNS)
|
2016-11-05 10:16:48 +01:00
|
|
|
.from(TABLE)
|
2016-06-18 21:53:18 +02:00
|
|
|
.where({ name })
|
2016-11-05 10:16:48 +01:00
|
|
|
.then(this.rowToStrategy);
|
2016-05-01 22:53:09 +02:00
|
|
|
}
|
2014-11-17 22:39:04 +01:00
|
|
|
|
2017-06-28 10:17:14 +02:00
|
|
|
rowToStrategy(row) {
|
2016-05-01 22:53:09 +02:00
|
|
|
if (!row) {
|
|
|
|
throw new NotFoundError('No strategy found');
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
name: row.name,
|
2017-06-28 21:10:43 +02:00
|
|
|
editable: row.built_in !== 1,
|
2016-05-01 22:53:09 +02:00
|
|
|
description: row.description,
|
2016-12-12 17:09:44 +01:00
|
|
|
parameters: row.parameters,
|
2021-01-21 13:47:08 +01:00
|
|
|
deprecated: row.deprecated,
|
2016-05-01 22:53:09 +02:00
|
|
|
};
|
2014-11-17 22:39:04 +01:00
|
|
|
}
|
|
|
|
|
2019-03-14 17:56:02 +01:00
|
|
|
rowToEditableStrategy(row) {
|
|
|
|
if (!row) {
|
|
|
|
throw new NotFoundError('No strategy found');
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
name: row.name,
|
|
|
|
description: row.description,
|
|
|
|
parameters: row.parameters,
|
2021-01-21 13:47:08 +01:00
|
|
|
deprecated: row.deprecated,
|
2019-03-14 17:56:02 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-06-28 10:17:14 +02:00
|
|
|
eventDataToRow(data) {
|
2016-05-01 22:53:09 +02:00
|
|
|
return {
|
|
|
|
name: data.name,
|
|
|
|
description: data.description,
|
2016-12-12 17:09:44 +01:00
|
|
|
parameters: JSON.stringify(data.parameters),
|
2016-05-01 22:53:09 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-01-18 12:32:19 +01:00
|
|
|
async createStrategy(data) {
|
2016-11-05 10:16:48 +01:00
|
|
|
this.db(TABLE)
|
|
|
|
.insert(this.eventDataToRow(data))
|
2017-06-28 10:17:14 +02:00
|
|
|
.catch(err =>
|
2020-04-14 22:29:11 +02:00
|
|
|
this.logger.error('Could not insert strategy, error: ', err),
|
2017-06-28 10:17:14 +02:00
|
|
|
);
|
2016-05-01 22:53:09 +02:00
|
|
|
}
|
2016-12-17 12:31:23 +01:00
|
|
|
|
2021-01-18 12:32:19 +01:00
|
|
|
async updateStrategy(data) {
|
2016-12-17 12:31:23 +01:00
|
|
|
this.db(TABLE)
|
|
|
|
.where({ name: data.name })
|
|
|
|
.update(this.eventDataToRow(data))
|
2017-06-28 10:17:14 +02:00
|
|
|
.catch(err =>
|
2020-04-14 22:29:11 +02:00
|
|
|
this.logger.error('Could not update strategy, error: ', err),
|
2017-06-28 10:17:14 +02:00
|
|
|
);
|
2021-01-21 13:47:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async deprecateStrategy({ name }) {
|
|
|
|
this.db(TABLE)
|
|
|
|
.where({ name })
|
|
|
|
.update({ deprecated: true })
|
|
|
|
.catch(err =>
|
|
|
|
this.logger.error('Could not deprecate strategy, error: ', err),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
async reactivateStrategy({ name }) {
|
|
|
|
this.db(TABLE)
|
|
|
|
.where({ name })
|
|
|
|
.update({ deprecated: false })
|
|
|
|
.catch(err =>
|
|
|
|
this.logger.error(
|
|
|
|
'Could not reactivate strategy, error: ',
|
|
|
|
err,
|
|
|
|
),
|
|
|
|
);
|
2016-12-17 12:31:23 +01:00
|
|
|
}
|
2019-03-13 19:10:13 +01:00
|
|
|
|
2021-01-18 12:32:19 +01:00
|
|
|
async deleteStrategy({ name }) {
|
2019-03-13 19:10:13 +01:00
|
|
|
return this.db(TABLE)
|
|
|
|
.where({ name })
|
|
|
|
.del()
|
|
|
|
.catch(err => {
|
2019-04-30 21:14:23 +02:00
|
|
|
this.logger.error('Could not delete strategy, error: ', err);
|
2019-03-13 19:10:13 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-01-18 12:32:19 +01:00
|
|
|
async importStrategy(data) {
|
2019-03-14 17:56:02 +01:00
|
|
|
const rowData = this.eventDataToRow(data);
|
|
|
|
return this.db(TABLE)
|
|
|
|
.where({ name: rowData.name, built_in: 0 }) // eslint-disable-line
|
|
|
|
.update(rowData)
|
|
|
|
.then(result =>
|
2020-04-14 22:29:11 +02:00
|
|
|
result === 0 ? this.db(TABLE).insert(rowData) : result,
|
2019-03-14 17:56:02 +01:00
|
|
|
)
|
2019-03-13 19:10:13 +01:00
|
|
|
.catch(err =>
|
2020-04-14 22:29:11 +02:00
|
|
|
this.logger.error('Could not import strategy, error: ', err),
|
2019-03-13 19:10:13 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-01-18 12:32:19 +01:00
|
|
|
async dropStrategies() {
|
2019-03-13 19:10:13 +01:00
|
|
|
return this.db(TABLE)
|
|
|
|
.where({ built_in: 0 }) // eslint-disable-line
|
|
|
|
.delete()
|
|
|
|
.catch(err =>
|
2020-04-14 22:29:11 +02:00
|
|
|
this.logger.error('Could not drop strategies, error: ', err),
|
2019-03-13 19:10:13 +01:00
|
|
|
);
|
|
|
|
}
|
2017-06-28 10:17:14 +02:00
|
|
|
}
|
2014-11-17 22:39:04 +01:00
|
|
|
|
2016-11-05 10:16:48 +01:00
|
|
|
module.exports = StrategyStore;
|