mirror of
https://github.com/Unleash/unleash.git
synced 2024-12-28 00:06:53 +01:00
Check if strategy exsit before deleting it
This commit is contained in:
parent
cb1607c903
commit
94f78797df
@ -5,6 +5,7 @@ var strategyDb = require('./strategyDb');
|
||||
var logger = require('./logger');
|
||||
var NameExistsError = require('./error/NameExistsError');
|
||||
var ValidationError = require('./error/ValidationError');
|
||||
var NotFoundError = require('./error/NotFoundError');
|
||||
var validateRequest = require('./error/validateRequest');
|
||||
var extractUser = require('./extractUser');
|
||||
|
||||
@ -23,15 +24,28 @@ module.exports = function (app) {
|
||||
});
|
||||
|
||||
app.delete('/strategies/:name', function (req, res) {
|
||||
eventStore.create({
|
||||
type: eventType.strategyDeleted,
|
||||
createdBy: extractUser(req),
|
||||
data: {
|
||||
name: req.params.name
|
||||
}
|
||||
})
|
||||
.then(function () { res.status(200).end(); })
|
||||
.catch(function () { res.status(500).end(); });
|
||||
var strategyName = req.params.name;
|
||||
|
||||
strategyDb.getStrategy(strategyName)
|
||||
.then(function() {
|
||||
return eventStore.create({
|
||||
type: eventType.strategyDeleted,
|
||||
createdBy: extractUser(req),
|
||||
data: {
|
||||
name: strategyName
|
||||
}
|
||||
});
|
||||
})
|
||||
.then(function() {
|
||||
res.status(200).end();
|
||||
})
|
||||
.catch(NotFoundError, function() {
|
||||
res.status(404).end();
|
||||
})
|
||||
.catch(function(err) {
|
||||
logger.error('Could not delete strategy='+strategyName, err);
|
||||
res.status(500).end();
|
||||
});
|
||||
});
|
||||
|
||||
app.post('/strategies', function (req, res) {
|
||||
|
@ -2,6 +2,7 @@ var eventStore = require('./eventStore');
|
||||
var eventType = require('./eventType');
|
||||
var logger = require('./logger');
|
||||
var knex = require('./dbPool');
|
||||
var NotFoundError = require('./error/NotFoundError');
|
||||
var STRATEGY_COLUMNS = ['name', 'description', 'parameters_template'];
|
||||
|
||||
eventStore.on(eventType.strategyCreated, function (event) {
|
||||
@ -35,7 +36,7 @@ function getStrategy(name) {
|
||||
|
||||
function rowToStrategy(row) {
|
||||
if (!row) {
|
||||
throw new Error('invalid row');
|
||||
throw new NotFoundError('No strategy found');
|
||||
}
|
||||
|
||||
return {
|
||||
|
@ -55,7 +55,14 @@ describe('The strategy api', function () {
|
||||
|
||||
it('deletes a new strategy', function (done) {
|
||||
request
|
||||
.delete('/strategies/deletable')
|
||||
.delete('/strategies/usersWithEmail')
|
||||
.expect(200, done);
|
||||
});
|
||||
|
||||
it('can\'t delete a strategy that dose not exist', function(done) {
|
||||
request
|
||||
.delete('/strategies/unknown')
|
||||
.expect(404, done);
|
||||
});
|
||||
|
||||
});
|
Loading…
Reference in New Issue
Block a user