import { IUnleashStores } from '../types/stores'; import { IUnleashConfig } from '../types/option'; import { Logger } from '../logger'; import { IEnvironment } from '../types/model'; import { UNIQUE_CONSTRAINT_VIOLATION } from '../error/db-error'; import NameExistsError from '../error/name-exists-error'; import { environmentSchema } from './state-schema'; import NotFoundError from '../error/notfound-error'; import { IEnvironmentStore } from '../types/stores/environment-store'; import { IFeatureStrategiesStore } from '../types/stores/feature-strategies-store'; import { IFeatureEnvironmentStore } from '../types/stores/feature-environment-store'; export default class EnvironmentService { private logger: Logger; private environmentStore: IEnvironmentStore; private featureStrategiesStore: IFeatureStrategiesStore; private featureEnvironmentStore: IFeatureEnvironmentStore; constructor( { environmentStore, featureStrategiesStore, featureEnvironmentStore, }: Pick< IUnleashStores, | 'environmentStore' | 'featureStrategiesStore' | 'featureEnvironmentStore' >, { getLogger }: Pick, ) { this.logger = getLogger('services/environment-service.ts'); this.environmentStore = environmentStore; this.featureStrategiesStore = featureStrategiesStore; this.featureEnvironmentStore = featureEnvironmentStore; } async getAll(): Promise { return this.environmentStore.getAll(); } async get(name: string): Promise { return this.environmentStore.get(name); } async delete(name: string): Promise { return this.environmentStore.delete(name); } async create(env: IEnvironment): Promise { await environmentSchema.validateAsync(env); return this.environmentStore.upsert(env); } async update( name: string, env: Pick, ): Promise { const exists = await this.environmentStore.exists(name); if (exists) { return this.environmentStore.upsert({ ...env, name }); } throw new NotFoundError(`Could not find environment ${name}`); } async addEnvironmentToProject( environment: string, projectId: string, ): Promise { try { await this.featureEnvironmentStore.connectProject( environment, projectId, ); await this.featureEnvironmentStore.connectFeatures( environment, projectId, ); } catch (e) { if (e.code === UNIQUE_CONSTRAINT_VIOLATION) { throw new NameExistsError( `${projectId} already has the environment ${environment} enabled`, ); } throw e; } } async removeEnvironmentFromProject( environment: string, projectId: string, ): Promise { await this.featureEnvironmentStore.disconnectFeatures( environment, projectId, ); await this.featureEnvironmentStore.disconnectProject( environment, projectId, ); } }