2017-06-28 10:20:22 +02:00
|
|
|
'use strict';
|
|
|
|
|
2018-12-05 21:07:45 +01:00
|
|
|
const Controller = require('../controller');
|
2017-06-28 10:20:22 +02:00
|
|
|
|
|
|
|
const eventType = require('../../event-type');
|
|
|
|
const NameExistsError = require('../../error/name-exists-error');
|
|
|
|
const extractUser = require('../../extract-user');
|
|
|
|
const strategySchema = require('./strategy-schema');
|
2018-12-05 21:16:51 +01:00
|
|
|
const { handleErrors } = require('./util');
|
2018-12-19 10:36:56 +01:00
|
|
|
const {
|
|
|
|
DELETE_STRATEGY,
|
|
|
|
CREATE_STRATEGY,
|
|
|
|
UPDATE_STRATEGY,
|
|
|
|
} = require('../../permissions');
|
2017-06-28 10:20:22 +02:00
|
|
|
const version = 1;
|
|
|
|
|
2018-12-05 21:07:45 +01:00
|
|
|
class StrategyController extends Controller {
|
2018-12-19 14:50:01 +01:00
|
|
|
constructor(config) {
|
|
|
|
super(config);
|
2019-04-30 21:14:23 +02:00
|
|
|
this.logger = config.getLogger('/admin-api/strategy.js');
|
2018-12-19 14:50:01 +01:00
|
|
|
this.strategyStore = config.stores.strategyStore;
|
|
|
|
this.eventStore = config.stores.eventStore;
|
2018-12-05 21:07:45 +01:00
|
|
|
|
|
|
|
this.get('/', this.getAllStratgies);
|
|
|
|
this.get('/:name', this.getStrategy);
|
2018-12-19 10:36:56 +01:00
|
|
|
this.delete('/:name', this.removeStrategy, DELETE_STRATEGY);
|
|
|
|
this.post('/', this.createStrategy, CREATE_STRATEGY);
|
|
|
|
this.put('/:strategyName', this.updateStrategy, UPDATE_STRATEGY);
|
2017-06-28 10:20:22 +02:00
|
|
|
}
|
|
|
|
|
2018-12-05 21:07:45 +01:00
|
|
|
async getAllStratgies(req, res) {
|
|
|
|
const strategies = await this.strategyStore.getStrategies();
|
|
|
|
res.json({ version, strategies });
|
|
|
|
}
|
|
|
|
|
|
|
|
async getStrategy(req, res) {
|
|
|
|
try {
|
|
|
|
const name = req.params.name;
|
|
|
|
const strategy = await this.strategyStore.getStrategy(name);
|
|
|
|
res.json(strategy).end();
|
|
|
|
} catch (err) {
|
|
|
|
res.status(404).json({ error: 'Could not find strategy' });
|
2017-06-28 21:10:43 +02:00
|
|
|
}
|
2018-12-05 21:07:45 +01:00
|
|
|
}
|
2017-06-28 21:10:43 +02:00
|
|
|
|
2018-12-05 21:07:45 +01:00
|
|
|
async removeStrategy(req, res) {
|
|
|
|
const strategyName = req.params.name;
|
2017-06-28 21:10:43 +02:00
|
|
|
|
2018-12-05 21:07:45 +01:00
|
|
|
try {
|
|
|
|
const strategy = await this.strategyStore.getStrategy(strategyName);
|
|
|
|
await this._validateEditable(strategy);
|
|
|
|
await this.eventStore.store({
|
|
|
|
type: eventType.STRATEGY_DELETED,
|
|
|
|
createdBy: extractUser(req),
|
|
|
|
data: {
|
|
|
|
name: strategyName,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
res.status(200).end();
|
|
|
|
} catch (error) {
|
2019-04-30 21:14:23 +02:00
|
|
|
handleErrors(res, this.logger, error);
|
2018-12-05 21:07:45 +01:00
|
|
|
}
|
|
|
|
}
|
2017-06-28 10:20:22 +02:00
|
|
|
|
2018-12-05 21:07:45 +01:00
|
|
|
async createStrategy(req, res) {
|
|
|
|
try {
|
2020-01-02 19:23:52 +01:00
|
|
|
const value = await strategySchema.validateAsync(req.body);
|
|
|
|
await this._validateStrategyName(value);
|
2018-12-05 21:07:45 +01:00
|
|
|
await this.eventStore.store({
|
|
|
|
type: eventType.STRATEGY_CREATED,
|
|
|
|
createdBy: extractUser(req),
|
2020-01-02 19:23:52 +01:00
|
|
|
data: value,
|
2018-12-05 21:07:45 +01:00
|
|
|
});
|
|
|
|
res.status(201).end();
|
|
|
|
} catch (error) {
|
2019-04-30 21:14:23 +02:00
|
|
|
handleErrors(res, this.logger, error);
|
2018-12-05 21:07:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async updateStrategy(req, res) {
|
|
|
|
const input = req.body;
|
|
|
|
|
|
|
|
try {
|
2020-01-02 19:23:52 +01:00
|
|
|
const value = await strategySchema.validateAsync(input);
|
2018-12-05 21:07:45 +01:00
|
|
|
const strategy = await this.strategyStore.getStrategy(input.name);
|
|
|
|
await this._validateEditable(strategy);
|
|
|
|
|
|
|
|
await this.eventStore.store({
|
|
|
|
type: eventType.STRATEGY_UPDATED,
|
|
|
|
createdBy: extractUser(req),
|
2020-01-02 19:23:52 +01:00
|
|
|
data: value,
|
2018-12-05 21:07:45 +01:00
|
|
|
});
|
|
|
|
res.status(200).end();
|
|
|
|
} catch (error) {
|
2019-04-30 21:14:23 +02:00
|
|
|
handleErrors(res, this.logger, error);
|
2018-12-05 21:07:45 +01:00
|
|
|
}
|
|
|
|
}
|
2017-06-28 10:20:22 +02:00
|
|
|
|
2018-12-05 21:07:45 +01:00
|
|
|
// This check belongs in the store.
|
|
|
|
async _validateStrategyName(data) {
|
2017-06-28 21:10:43 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
2018-12-05 21:07:45 +01:00
|
|
|
this.strategyStore
|
2017-06-28 21:10:43 +02:00
|
|
|
.getStrategy(data.name)
|
|
|
|
.then(() =>
|
2018-12-05 21:07:45 +01:00
|
|
|
reject(
|
|
|
|
new NameExistsError(
|
|
|
|
`Strategy with name ${data.name} already exist!`
|
|
|
|
)
|
|
|
|
)
|
2017-06-28 21:10:43 +02:00
|
|
|
)
|
|
|
|
.catch(() => resolve(data));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-12-05 21:07:45 +01:00
|
|
|
// This check belongs in the store.
|
|
|
|
_validateEditable(strategy) {
|
|
|
|
if (strategy.editable === false) {
|
|
|
|
throw new Error(`Cannot edit strategy ${strategy.name}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = StrategyController;
|