2022-03-29 14:59:14 +02:00
|
|
|
import { IUnleashConfig } from '../types/option';
|
|
|
|
import { IEventStore } from '../types/stores/event-store';
|
|
|
|
import { IUnleashStores } from '../types';
|
|
|
|
import { Logger } from '../logger';
|
2022-04-06 15:01:50 +02:00
|
|
|
import NameExistsError from '../error/name-exists-error';
|
2022-03-29 14:59:14 +02:00
|
|
|
import { ISegmentStore } from '../types/stores/segment-store';
|
|
|
|
import { IFeatureStrategy, ISegment } from '../types/model';
|
|
|
|
import { segmentSchema } from './segment-schema';
|
|
|
|
import {
|
|
|
|
SEGMENT_CREATED,
|
|
|
|
SEGMENT_DELETED,
|
|
|
|
SEGMENT_UPDATED,
|
|
|
|
} from '../types/events';
|
|
|
|
import User from '../types/user';
|
|
|
|
import { IFeatureStrategiesStore } from '../types/stores/feature-strategies-store';
|
2022-04-01 10:53:52 +02:00
|
|
|
import BadDataError from '../error/bad-data-error';
|
2022-03-29 14:59:14 +02:00
|
|
|
|
|
|
|
export class SegmentService {
|
|
|
|
private logger: Logger;
|
|
|
|
|
|
|
|
private segmentStore: ISegmentStore;
|
|
|
|
|
|
|
|
private featureStrategiesStore: IFeatureStrategiesStore;
|
|
|
|
|
|
|
|
private eventStore: IEventStore;
|
|
|
|
|
2022-06-02 10:44:45 +02:00
|
|
|
private config: IUnleashConfig;
|
|
|
|
|
2022-03-29 14:59:14 +02:00
|
|
|
constructor(
|
|
|
|
{
|
|
|
|
segmentStore,
|
|
|
|
featureStrategiesStore,
|
|
|
|
eventStore,
|
|
|
|
}: Pick<
|
|
|
|
IUnleashStores,
|
|
|
|
'segmentStore' | 'featureStrategiesStore' | 'eventStore'
|
|
|
|
>,
|
2022-06-02 10:44:45 +02:00
|
|
|
config: IUnleashConfig,
|
2022-03-29 14:59:14 +02:00
|
|
|
) {
|
|
|
|
this.segmentStore = segmentStore;
|
|
|
|
this.featureStrategiesStore = featureStrategiesStore;
|
|
|
|
this.eventStore = eventStore;
|
2022-06-02 10:44:45 +02:00
|
|
|
this.logger = config.getLogger('services/segment-service.ts');
|
|
|
|
this.config = config;
|
2022-03-29 14:59:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async get(id: number): Promise<ISegment> {
|
|
|
|
return this.segmentStore.get(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
async getAll(): Promise<ISegment[]> {
|
|
|
|
return this.segmentStore.getAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
async getActive(): Promise<ISegment[]> {
|
|
|
|
return this.segmentStore.getActive();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Used by unleash-enterprise.
|
|
|
|
async getByStrategy(strategyId: string): Promise<ISegment[]> {
|
|
|
|
return this.segmentStore.getByStrategy(strategyId);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Used by unleash-enterprise.
|
|
|
|
async getStrategies(id: number): Promise<IFeatureStrategy[]> {
|
|
|
|
return this.featureStrategiesStore.getStrategiesBySegment(id);
|
|
|
|
}
|
|
|
|
|
2022-08-22 16:35:43 +02:00
|
|
|
async create(
|
|
|
|
data: unknown,
|
|
|
|
user: Partial<Pick<User, 'username' | 'email'>>,
|
|
|
|
): Promise<ISegment> {
|
2022-03-29 14:59:14 +02:00
|
|
|
const input = await segmentSchema.validateAsync(data);
|
2022-06-02 10:44:45 +02:00
|
|
|
this.validateSegmentValuesLimit(input);
|
2022-04-06 15:01:50 +02:00
|
|
|
await this.validateName(input.name);
|
2022-03-29 14:59:14 +02:00
|
|
|
const segment = await this.segmentStore.create(input, user);
|
|
|
|
|
|
|
|
await this.eventStore.store({
|
|
|
|
type: SEGMENT_CREATED,
|
|
|
|
createdBy: user.email || user.username,
|
|
|
|
data: segment,
|
|
|
|
});
|
2022-08-22 16:35:43 +02:00
|
|
|
|
|
|
|
return segment;
|
2022-03-29 14:59:14 +02:00
|
|
|
}
|
|
|
|
|
2022-08-22 16:35:43 +02:00
|
|
|
async update(
|
|
|
|
id: number,
|
|
|
|
data: unknown,
|
|
|
|
user: Partial<Pick<User, 'username' | 'email'>>,
|
|
|
|
): Promise<void> {
|
2022-03-29 14:59:14 +02:00
|
|
|
const input = await segmentSchema.validateAsync(data);
|
2022-06-02 10:44:45 +02:00
|
|
|
this.validateSegmentValuesLimit(input);
|
2022-03-29 14:59:14 +02:00
|
|
|
const preData = await this.segmentStore.get(id);
|
2022-04-06 15:01:50 +02:00
|
|
|
|
|
|
|
if (preData.name !== input.name) {
|
|
|
|
await this.validateName(input.name);
|
|
|
|
}
|
|
|
|
|
2022-03-29 14:59:14 +02:00
|
|
|
const segment = await this.segmentStore.update(id, input);
|
|
|
|
|
|
|
|
await this.eventStore.store({
|
|
|
|
type: SEGMENT_UPDATED,
|
|
|
|
createdBy: user.email || user.username,
|
|
|
|
data: segment,
|
|
|
|
preData,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async delete(id: number, user: User): Promise<void> {
|
2022-06-07 14:06:45 +02:00
|
|
|
const segment = await this.segmentStore.get(id);
|
2022-03-29 14:59:14 +02:00
|
|
|
await this.segmentStore.delete(id);
|
|
|
|
await this.eventStore.store({
|
|
|
|
type: SEGMENT_DELETED,
|
|
|
|
createdBy: user.email || user.username,
|
|
|
|
data: segment,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-06-08 15:41:02 +02:00
|
|
|
async cloneStrategySegments(
|
|
|
|
sourceStrategyId: string,
|
|
|
|
targetStrategyId: string,
|
|
|
|
): Promise<void> {
|
|
|
|
const sourceStrategySegments = await this.getByStrategy(
|
|
|
|
sourceStrategyId,
|
|
|
|
);
|
|
|
|
|
|
|
|
await Promise.all(
|
|
|
|
sourceStrategySegments.map((sourceStrategySegment) => {
|
|
|
|
return this.addToStrategy(
|
|
|
|
sourceStrategySegment.id,
|
|
|
|
targetStrategyId,
|
|
|
|
);
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-03-29 14:59:14 +02:00
|
|
|
// Used by unleash-enterprise.
|
|
|
|
async addToStrategy(id: number, strategyId: string): Promise<void> {
|
2022-04-01 10:53:52 +02:00
|
|
|
await this.validateStrategySegmentLimit(strategyId);
|
2022-03-29 14:59:14 +02:00
|
|
|
await this.segmentStore.addToStrategy(id, strategyId);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Used by unleash-enterprise.
|
|
|
|
async removeFromStrategy(id: number, strategyId: string): Promise<void> {
|
|
|
|
await this.segmentStore.removeFromStrategy(id, strategyId);
|
|
|
|
}
|
2022-04-01 10:53:52 +02:00
|
|
|
|
2022-04-06 15:01:50 +02:00
|
|
|
async validateName(name: string): Promise<void> {
|
|
|
|
if (!name) {
|
|
|
|
throw new BadDataError('Segment name cannot be empty');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (await this.segmentStore.existsByName(name)) {
|
|
|
|
throw new NameExistsError('Segment name already exists');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-01 10:53:52 +02:00
|
|
|
private async validateStrategySegmentLimit(
|
|
|
|
strategyId: string,
|
|
|
|
): Promise<void> {
|
2022-06-02 10:44:45 +02:00
|
|
|
const { strategySegmentsLimit } = this.config;
|
2022-04-01 10:53:52 +02:00
|
|
|
|
2022-06-02 10:44:45 +02:00
|
|
|
if (
|
|
|
|
(await this.getByStrategy(strategyId)).length >=
|
|
|
|
strategySegmentsLimit
|
|
|
|
) {
|
2022-04-01 10:53:52 +02:00
|
|
|
throw new BadDataError(
|
2022-06-02 10:44:45 +02:00
|
|
|
`Strategies may not have more than ${strategySegmentsLimit} segments`,
|
2022-04-01 10:53:52 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-02 10:44:45 +02:00
|
|
|
private validateSegmentValuesLimit(segment: Omit<ISegment, 'id'>): void {
|
|
|
|
const { segmentValuesLimit } = this.config;
|
2022-04-01 10:53:52 +02:00
|
|
|
|
|
|
|
const valuesCount = segment.constraints
|
|
|
|
.flatMap((constraint) => constraint.values?.length ?? 0)
|
|
|
|
.reduce((acc, length) => acc + length, 0);
|
|
|
|
|
2022-06-02 10:44:45 +02:00
|
|
|
if (valuesCount > segmentValuesLimit) {
|
2022-04-01 10:53:52 +02:00
|
|
|
throw new BadDataError(
|
2022-06-02 10:44:45 +02:00
|
|
|
`Segments may not have more than ${segmentValuesLimit} values`,
|
2022-04-01 10:53:52 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2022-03-29 14:59:14 +02:00
|
|
|
}
|