mirror of
https://github.com/Unleash/unleash.git
synced 2025-05-17 01:17:29 +02:00
This simplifies stores to just be storage interaction, they no longer react to events. Controllers now call services and awaits the result from the call. When the service calls are returned the database is updated. This simplifies testing dramatically, cause you know that your state is updated when returned from a call, rather than hoping the store has picked up the event (which really was a command) and reacted to it. Events are still emitted from eventStore, so other parts of the app can react to events as they're being sent out. As part of the move to services, we now also emit an application-created event when we see a new client application. Fixes: #685 Fixes: #595
84 lines
2.6 KiB
JavaScript
84 lines
2.6 KiB
JavaScript
const strategySchema = require('./strategy-schema');
|
|
const NameExistsError = require('../error/name-exists-error');
|
|
const {
|
|
STRATEGY_CREATED,
|
|
STRATEGY_DELETED,
|
|
STRATEGY_UPDATED,
|
|
} = require('../event-type');
|
|
|
|
class StrategyService {
|
|
constructor({ strategyStore, eventStore }, { getLogger }) {
|
|
this.strategyStore = strategyStore;
|
|
this.eventStore = eventStore;
|
|
this.logger = getLogger('services/strategy-service.js');
|
|
}
|
|
|
|
async getStrategies() {
|
|
return this.strategyStore.getStrategies();
|
|
}
|
|
|
|
async getStrategy(name) {
|
|
return this.strategyStore.getStrategy(name);
|
|
}
|
|
|
|
async removeStrategy(strategyName, userName) {
|
|
const strategy = await this.strategyStore.getStrategy(strategyName);
|
|
await this._validateEditable(strategy);
|
|
await this.strategyStore.deleteStrategy({ name: strategyName });
|
|
await this.eventStore.store({
|
|
type: STRATEGY_DELETED,
|
|
createdBy: userName,
|
|
data: {
|
|
name: strategyName,
|
|
},
|
|
});
|
|
}
|
|
|
|
async createStrategy(value, userName) {
|
|
const strategy = await strategySchema.validateAsync(value);
|
|
await this._validateStrategyName(strategy);
|
|
await this.strategyStore.createStrategy(strategy);
|
|
await this.eventStore.store({
|
|
type: STRATEGY_CREATED,
|
|
createdBy: userName,
|
|
data: strategy,
|
|
});
|
|
}
|
|
|
|
async updateStrategy(input, userName) {
|
|
const value = await strategySchema.validateAsync(input);
|
|
const strategy = await this.strategyStore.getStrategy(input.name);
|
|
await this._validateEditable(strategy);
|
|
await this.strategyStore.updateStrategy(value);
|
|
await this.eventStore.store({
|
|
type: STRATEGY_UPDATED,
|
|
createdBy: userName,
|
|
data: value,
|
|
});
|
|
}
|
|
|
|
async _validateStrategyName(data) {
|
|
return new Promise((resolve, reject) => {
|
|
this.strategyStore
|
|
.getStrategy(data.name)
|
|
.then(() =>
|
|
reject(
|
|
new NameExistsError(
|
|
`Strategy with name ${data.name} already exist!`,
|
|
),
|
|
),
|
|
)
|
|
.catch(() => resolve(data));
|
|
});
|
|
}
|
|
|
|
// This check belongs in the store.
|
|
_validateEditable(strategy) {
|
|
if (strategy.editable === false) {
|
|
throw new Error(`Cannot edit strategy ${strategy.name}`);
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = StrategyService;
|