Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | 60x 60x 60x 60x 147x 147x 147x 3x 8x 4x 3x 2x 2x 4x 3x 3x 1x 3x 2x 2x 10x 7x 7x 5x 5x 8x 7x 5x 4x 4x 7x 7x 2x 5x 8x 2x 60x 60x | import { Logger } from '../logger'; import { IUnleashConfig } from '../types/option'; import { IUnleashStores } from '../types/stores'; import { IEventStore } from '../types/stores/event-store'; import { IMinimalStrategy, IStrategy, IStrategyStore, } from '../types/stores/strategy-store'; import NotFoundError from '../error/notfound-error'; const strategySchema = require('./strategy-schema'); const NameExistsError = require('../error/name-exists-error'); const { STRATEGY_CREATED, STRATEGY_DELETED, STRATEGY_DEPRECATED, STRATEGY_REACTIVATED, STRATEGY_UPDATED, } = require('../types/events'); class StrategyService { private logger: Logger; private strategyStore: IStrategyStore; private eventStore: IEventStore; constructor( { strategyStore, eventStore, }: Pick<IUnleashStores, 'strategyStore' | 'eventStore'>, { getLogger }: Pick<IUnleashConfig, 'getLogger'>, ) { this.strategyStore = strategyStore; this.eventStore = eventStore; this.logger = getLogger('services/strategy-service.js'); } async getStrategies(): Promise<IStrategy[]> { return this.strategyStore.getAll(); } async getStrategy(name: string): Promise<IStrategy> { return this.strategyStore.get(name); } async removeStrategy( strategyName: string, userName: string, ): Promise<void> { const strategy = await this.strategyStore.get(strategyName); await this._validateEditable(strategy); await this.strategyStore.delete(strategyName); await this.eventStore.store({ type: STRATEGY_DELETED, createdBy: userName, data: { name: strategyName, }, }); } async deprecateStrategy( strategyName: string, userName: string, ): Promise<void> { if (await this.strategyStore.exists(strategyName)) { // Check existence await this.strategyStore.deprecateStrategy({ name: strategyName }); await this.eventStore.store({ type: STRATEGY_DEPRECATED, createdBy: userName, data: { name: strategyName, }, }); } else { throw new NotFoundError( `Could not find strategy with name ${strategyName}`, ); } } async reactivateStrategy( strategyName: string, userName: string, ): Promise<void> { await this.strategyStore.get(strategyName); // Check existence await this.strategyStore.reactivateStrategy({ name: strategyName }); await this.eventStore.store({ type: STRATEGY_REACTIVATED, createdBy: userName, data: { name: strategyName, }, }); } async createStrategy( value: IMinimalStrategy, userName: string, ): Promise<void> { const strategy = await strategySchema.validateAsync(value); strategy.deprecated = false; await this._validateStrategyName(strategy); await this.strategyStore.createStrategy(strategy); await this.eventStore.store({ type: STRATEGY_CREATED, createdBy: userName, data: strategy, }); } async updateStrategy( input: IMinimalStrategy, userName: string, ): Promise<void> { const value = await strategySchema.validateAsync(input); const strategy = await this.strategyStore.get(input.name); await this._validateEditable(strategy); await this.strategyStore.updateStrategy(value); await this.eventStore.store({ type: STRATEGY_UPDATED, createdBy: userName, data: value, }); } private _validateStrategyName( data: Pick<IStrategy, 'name'>, ): Promise<Pick<IStrategy, 'name'>> { return new Promise((resolve, reject) => { this.strategyStore .get(data.name) .then(() => reject( new NameExistsError( `Strategy with name ${data.name} already exist!`, ), ), ) .catch(() => resolve(data)); }); } // This check belongs in the store. _validateEditable(strategy: IStrategy): void { if (strategy.editable === false) { throw new Error(`Cannot edit strategy ${strategy.name}`); } } } export default StrategyService; module.exports = StrategyService; |